Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support conditional ignore of Modbus broadcast frames #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modbus/include/mb.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNDiscrete );

/*! \defgroup protocol_mgmt Protocol management
* \code #include "mb.h" \endcode
*/

/*! \brief Callback determining if a broadcast frame should be responded to.
*
* The callback is evaluated once for each incoming Modbus broadcast frame.
* See the <em>MB_SUPPORT_IGNORE_BROADCAST</em>
* configuration macro on how to enable the callback.
*
* \return non-zero to ignore the broadcast frame, zero otherwise.
*/
UCHAR xMBIgnoreBroadcast( void );

#ifdef __cplusplus
PR_END_EXTERN_C
#endif
Expand Down
13 changes: 13 additions & 0 deletions modbus/include/mbconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ PR_BEGIN_EXTERN_C
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( 0 )
#endif

/*! \brief If <em>xMBIgnoreBroadcast</em> should be supported.
*
* Defaults to zero, meaning freemodbus
* behaves classically (always responds to broadcast).
*
* If you enable this, you must implement the mentioned callback function.
* Since this is just a default, you can define the macro externally,
* and avoid modifications to this file.
*/
#ifndef MB_SUPPORT_IGNORE_BROADCAST
#define MB_SUPPORT_IGNORE_BROADCAST ( 0 )
#endif

/*! \brief Maximum number of Modbus functions codes the protocol stack
* should support.
*
Expand Down
6 changes: 5 additions & 1 deletion modbus/mb.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ eMBPoll( void )
if( eStatus == MB_ENOERR )
{
/* Check if the frame is for us. If not ignore the frame. */
#if MB_SUPPORT_IGNORE_BROADCAST > 0
if( ( ucRcvAddress == ucMBAddress ) || ( ( ucRcvAddress == MB_ADDRESS_BROADCAST ) && !xMBIgnoreBroadcast() ) )
#else
if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
#endif
{
( void )xMBPortEventPost( EV_EXECUTE );
}
Expand Down Expand Up @@ -398,7 +402,7 @@ eMBPoll( void )
if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
{
vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
}
}
eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
}
break;
Expand Down