summaryrefslogtreecommitdiff
path: root/apps/modbus/mb.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/modbus/mb.c')
-rw-r--r--apps/modbus/mb.c115
1 files changed, 58 insertions, 57 deletions
diff --git a/apps/modbus/mb.c b/apps/modbus/mb.c
index a86ce343f..9cb0543d8 100644
--- a/apps/modbus/mb.c
+++ b/apps/modbus/mb.c
@@ -72,7 +72,7 @@ static enum
STATE_NOT_INITIALIZED
} eMBState = STATE_NOT_INITIALIZED;
-/* Functions pointer which are initialized in eMBInit( ). Depending on the
+/* Functions pointer which are initialized in eMBInit(). Depending on the
* mode (RTU or ASCII) the are set to the correct implementations.
*/
static peMBFrameSend peMBFrameSendCur;
@@ -85,12 +85,12 @@ static pvMBFrameClose pvMBFrameCloseCur;
* an external event has happend which includes a timeout or the reception
* or transmission of a character.
*/
-bool( *pxMBFrameCBByteReceived ) ( void );
-bool( *pxMBFrameCBTransmitterEmpty ) ( void );
-bool( *pxMBPortCBTimerExpired ) ( void );
+bool(*pxMBFrameCBByteReceived)(void);
+bool(*pxMBFrameCBTransmitterEmpty)(void);
+bool(*pxMBPortCBTimerExpired)(void);
-bool( *pxMBFrameCBReceiveFSMCur ) ( void );
-bool( *pxMBFrameCBTransmitFSMCur ) ( void );
+bool(*pxMBFrameCBReceiveFSMCur)(void);
+bool(*pxMBFrameCBTransmitFSMCur)(void);
/* An array of Modbus functions handlers which associates Modbus function
* codes with implementing functions.
@@ -130,13 +130,14 @@ static xMBFunctionHandler xFuncHandlers[CONFIG_MB_FUNC_HANDLERS_MAX] = {
/* ----------------------- Start implementation -----------------------------*/
eMBErrorCode
-eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity )
+eMBInit(eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity)
{
eMBErrorCode eStatus = MB_ENOERR;
/* check preconditions */
- if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
- ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
+
+ if ((ucSlaveAddress == MB_ADDRESS_BROADCAST) ||
+ (ucSlaveAddress < MB_ADDRESS_MIN) || (ucSlaveAddress > MB_ADDRESS_MAX))
{
eStatus = MB_EINVAL;
}
@@ -144,7 +145,7 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa
{
ucMBAddress = ucSlaveAddress;
- switch ( eMode )
+ switch (eMode)
{
#ifdef CONFIG_MB_RTU_ENABLED
case MB_RTU:
@@ -157,7 +158,7 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa
pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
- eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
+ eStatus = eMBRTUInit(ucMBAddress, ucPort, ulBaudRate, eParity);
break;
#endif
#ifdef CONFIG_MB_ASCII_ENABLED
@@ -171,16 +172,16 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa
pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
- eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
+ eStatus = eMBASCIIInit(ucMBAddress, ucPort, ulBaudRate, eParity);
break;
#endif
default:
eStatus = MB_EINVAL;
}
- if( eStatus == MB_ENOERR )
+ if (eStatus == MB_ENOERR)
{
- if( !xMBPortEventInit( ) )
+ if (!xMBPortEventInit())
{
/* port dependent event module initalization failed. */
eStatus = MB_EPORTERR;
@@ -197,15 +198,15 @@ eMBInit( eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRa
#ifdef CONFIG_MB_TCP_ENABLED
eMBErrorCode
-eMBTCPInit( uint16_t ucTCPPort )
+eMBTCPInit(uint16_t ucTCPPort)
{
eMBErrorCode eStatus = MB_ENOERR;
- if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
+ if ((eStatus = eMBTCPDoInit(ucTCPPort)) != MB_ENOERR)
{
eMBState = STATE_DISABLED;
}
- else if( !xMBPortEventInit( ) )
+ else if (!xMBPortEventInit())
{
/* Port dependent event module initalization failed. */
eStatus = MB_EPORTERR;
@@ -226,33 +227,33 @@ eMBTCPInit( uint16_t ucTCPPort )
#endif
eMBErrorCode
-eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler )
+eMBRegisterCB(uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler)
{
int i;
eMBErrorCode eStatus;
- if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
+ if ((0 < ucFunctionCode) && (ucFunctionCode <= 127))
{
- ENTER_CRITICAL_SECTION( );
- if( pxHandler != NULL )
+ ENTER_CRITICAL_SECTION();
+ if (pxHandler != NULL)
{
- for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ )
+ for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
{
- if( ( xFuncHandlers[i].pxHandler == NULL ) ||
- ( xFuncHandlers[i].pxHandler == pxHandler ) )
+ if ((xFuncHandlers[i].pxHandler == NULL) ||
+ (xFuncHandlers[i].pxHandler == pxHandler))
{
xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
xFuncHandlers[i].pxHandler = pxHandler;
break;
}
}
- eStatus = ( i != CONFIG_MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
+ eStatus = (i != CONFIG_MB_FUNC_HANDLERS_MAX) ? MB_ENOERR : MB_ENORES;
}
else
{
- for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ )
+ for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
{
- if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
+ if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode)
{
xFuncHandlers[i].ucFunctionCode = 0;
xFuncHandlers[i].pxHandler = NULL;
@@ -262,7 +263,7 @@ eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler )
/* Remove can't fail. */
eStatus = MB_ENOERR;
}
- EXIT_CRITICAL_SECTION( );
+ EXIT_CRITICAL_SECTION();
}
else
{
@@ -273,15 +274,15 @@ eMBRegisterCB( uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler )
eMBErrorCode
-eMBClose( void )
+eMBClose(void)
{
eMBErrorCode eStatus = MB_ENOERR;
- if( eMBState == STATE_DISABLED )
+ if (eMBState == STATE_DISABLED)
{
- if( pvMBFrameCloseCur != NULL )
+ if (pvMBFrameCloseCur != NULL)
{
- pvMBFrameCloseCur( );
+ pvMBFrameCloseCur();
}
}
else
@@ -292,14 +293,14 @@ eMBClose( void )
}
eMBErrorCode
-eMBEnable( void )
+eMBEnable(void)
{
eMBErrorCode eStatus = MB_ENOERR;
- if( eMBState == STATE_DISABLED )
+ if (eMBState == STATE_DISABLED)
{
/* Activate the protocol stack. */
- pvMBFrameStartCur( );
+ pvMBFrameStartCur();
eMBState = STATE_ENABLED;
}
else
@@ -310,17 +311,17 @@ eMBEnable( void )
}
eMBErrorCode
-eMBDisable( void )
+eMBDisable(void)
{
eMBErrorCode eStatus;
- if( eMBState == STATE_ENABLED )
+ if (eMBState == STATE_ENABLED)
{
- pvMBFrameStopCur( );
+ pvMBFrameStopCur();
eMBState = STATE_DISABLED;
eStatus = MB_ENOERR;
}
- else if( eMBState == STATE_DISABLED )
+ else if (eMBState == STATE_DISABLED)
{
eStatus = MB_ENOERR;
}
@@ -332,7 +333,7 @@ eMBDisable( void )
}
eMBErrorCode
-eMBPoll( void )
+eMBPoll(void)
{
static uint8_t *ucMBFrame;
static uint8_t ucRcvAddress;
@@ -345,28 +346,28 @@ eMBPoll( void )
eMBEventType eEvent;
/* Check if the protocol stack is ready. */
- if( eMBState != STATE_ENABLED )
+ if (eMBState != STATE_ENABLED)
{
return MB_EILLSTATE;
}
/* Check if there is a event available. If not return control to caller.
* Otherwise we will handle the event. */
- if( xMBPortEventGet( &eEvent ) == true )
+ if (xMBPortEventGet(&eEvent) == true)
{
- switch ( eEvent )
+ switch (eEvent)
{
case EV_READY:
break;
case EV_FRAME_RECEIVED:
- eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
- if( eStatus == MB_ENOERR )
+ eStatus = peMBFrameReceiveCur(&ucRcvAddress, &ucMBFrame, &usLength);
+ if (eStatus == MB_ENOERR)
{
/* Check if the frame is for us. If not ignore the frame. */
- if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
+ if ((ucRcvAddress == ucMBAddress) || (ucRcvAddress == MB_ADDRESS_BROADCAST))
{
- ( void )xMBPortEventPost( EV_EXECUTE );
+ (void)xMBPortEventPost(EV_EXECUTE);
}
}
break;
@@ -374,38 +375,38 @@ eMBPoll( void )
case EV_EXECUTE:
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION;
- for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++ )
+ for(i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */
- if( xFuncHandlers[i].ucFunctionCode == 0 )
+ if (xFuncHandlers[i].ucFunctionCode == 0)
{
break;
}
- else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
+ else if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode)
{
- eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
+ eException = xFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
break;
}
}
/* If the request was not sent to the broadcast address we
* return a reply. */
- if( ucRcvAddress != MB_ADDRESS_BROADCAST )
+ if (ucRcvAddress != MB_ADDRESS_BROADCAST)
{
- if( eException != MB_EX_NONE )
+ if (eException != MB_EX_NONE)
{
/* An exception occured. Build an error frame. */
usLength = 0;
- ucMBFrame[usLength++] = ( uint8_t )( ucFunctionCode | MB_FUNC_ERROR );
+ ucMBFrame[usLength++] = (uint8_t)(ucFunctionCode | MB_FUNC_ERROR);
ucMBFrame[usLength++] = eException;
}
#ifdef CONFIG_MB_ASCII_ENABLED
- if( ( eMBCurrentMode == MB_ASCII ) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
+ if ((eMBCurrentMode == MB_ASCII) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS)
{
- vMBPortTimersDelay( CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
+ vMBPortTimersDelay(CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS);
}
#endif
- eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
+ (void)peMBFrameSendCur(ucMBAddress, ucMBFrame, usLength);
}
break;