Skip to content

Commit

Permalink
allow user provided address checking
Browse files Browse the repository at this point in the history
If user provided address checking is enabled, the user function takes
complete responsibility for validating.  An example function would be:

```
BOOL isMBUserAddressValid( UCHAR ucMBAddress, UCHAR ucRcvAddress )
{
        if (
                /* Accept our originally configured address */
                (ucMBAddress == ucRcvAddress) ||
                /* Accept all even, non-broadcast addresses less than 50 */
                (ucRcvAddress > 0 && ucRcvAddress < 50 && ((ucRcvAddress & 0x1) == 0))
        ) {
                return true;
        }
        return false;
}

```

This allows fixing issues such as: cwalter-at#27
in a more general fashion.

Note that this does not affect the STRICT_ADDRESSING or
QUIRK_REPLY_BROADCAST_READ options, which are _after_ we have decided to
look at the frame.

Signed-off-by: Karl Palsson <[email protected]>
  • Loading branch information
karlp committed Jun 9, 2022
1 parent 4ea6112 commit c9b1010
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions modbus/include/mb.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
UCHAR const *pucAdditional,
USHORT usAdditionalLen );

/*! \ingroup modbus
* \brief Require user validation of the addresses.
* You can use this to "allow" a custom range of modbus addresses,
* instead of just the single address provided in \ref eMBInit
* This function is only called if MB_USER_ADDRESS_VALIDATION > 0
* in your mbconfig.h
*
* \param ucMBAddress Value provided at eMBInit time
* \param ucRcvAddress Value currently being evaluated
* \return true if the address should be accepted
*/
BOOL isMBUserAddressValid( UCHAR ucMBAddress, UCHAR ucRcvAddress );

/*! \ingroup modbus
* \brief Registers a callback handler for a given function code.
*
Expand Down
5 changes: 5 additions & 0 deletions modbus/include/mbconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ PR_BEGIN_EXTERN_C
#endif


#ifndef MB_USER_ADDRESS_VALIDATION
/*! \brief require user code to validate addresses */
#define MB_USER_ADDRESS_VALIDATION ( 0 )
#endif

/*! @} */
#ifdef __cplusplus
PR_END_EXTERN_C
Expand Down
6 changes: 5 additions & 1 deletion modbus/mb.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ eMBPoll( void )
if( eStatus == MB_ENOERR )
{
/* Check if the frame is for us. If not ignore the frame. */
if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
#if MB_USER_ADDRESS_VALIDATION > 0
if( isMBUserAddressValid( ucMBAddress, ucRcvAddress ) )
#else
if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
#endif
{
( void )xMBPortEventPost( EV_EXECUTE );
}
Expand Down

0 comments on commit c9b1010

Please sign in to comment.