freemodbus: fix clang warnings

* Original commit: espressif/esp-idf@cf4c95532f
This commit is contained in:
aleks
2019-11-11 11:58:56 +01:00
parent ae990b6ca3
commit 73bbc0cd7d
6 changed files with 71 additions and 39 deletions

View File

@@ -76,6 +76,8 @@ PR_BEGIN_EXTERN_C
/* ----------------------- Type definitions ---------------------------------*/ /* ----------------------- Type definitions ---------------------------------*/
#ifndef _MB_M_H #ifndef _MB_M_H
#define MB_FUNC_CODE_MAX 127
/*! \ingroup modbus /*! \ingroup modbus
* \brief Modbus serial transmission modes (RTU/ASCII). * \brief Modbus serial transmission modes (RTU/ASCII).
* *

View File

@@ -53,7 +53,7 @@ PR_BEGIN_EXTERN_C
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 ) #define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 ) #define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 ) #define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
#define MB_FUNC_ERROR ( 128 ) #define MB_FUNC_ERROR ( 128u )
/* ----------------------- Type definitions ---------------------------------*/ /* ----------------------- Type definitions ---------------------------------*/
typedef enum typedef enum
{ {

View File

@@ -229,7 +229,7 @@ eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
int i; int i;
eMBErrorCode eStatus; eMBErrorCode eStatus;
if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= MB_FUNC_CODE_MAX ) )
{ {
ENTER_CRITICAL_SECTION( ); ENTER_CRITICAL_SECTION( );
if( pxHandler != NULL ) if( pxHandler != NULL )
@@ -332,7 +332,7 @@ eMBDisable( void )
eMBErrorCode eMBErrorCode
eMBPoll( void ) eMBPoll( void )
{ {
static UCHAR *ucMBFrame; static UCHAR *ucMBFrame = NULL;
static UCHAR ucRcvAddress; static UCHAR ucRcvAddress;
static UCHAR ucFunctionCode; static UCHAR ucFunctionCode;
static USHORT usLength; static USHORT usLength;
@@ -373,8 +373,10 @@ eMBPoll( void )
break; break;
case EV_EXECUTE: case EV_EXECUTE:
ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); if ( !ucMBFrame ) {
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; return MB_EILLSTATE;
}
ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION; eException = MB_EX_ILLEGAL_FUNCTION;
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
{ {
@@ -416,7 +418,10 @@ eMBPoll( void )
case EV_FRAME_SENT: case EV_FRAME_SENT:
ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__);
break; break;
default:
break;
} }
} }
return MB_ENOERR; return eStatus;
} }

View File

@@ -44,17 +44,17 @@
#include "mbfunc.h" #include "mbfunc.h"
#include "mbport.h" #include "mbport.h"
#if MB_MASTER_RTU_ENABLED == 1 #if MB_MASTER_RTU_ENABLED
#include "mbrtu.h" #include "mbrtu.h"
#endif #endif
#if MB_MASTER_ASCII_ENABLED == 1 #if MB_MASTER_ASCII_ENABLED
#include "mbascii.h" #include "mbascii.h"
#endif #endif
#if MB_MASTER_TCP_ENABLED == 1 #if MB_MASTER_TCP_ENABLED
#include "mbtcp.h" #include "mbtcp.h"
#endif #endif
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
#ifndef MB_PORT_HAS_CLOSE #ifndef MB_PORT_HAS_CLOSE
#define MB_PORT_HAS_CLOSE 1 #define MB_PORT_HAS_CLOSE 1
@@ -264,25 +264,28 @@ eMBMasterDisable( void )
eMBErrorCode eMBErrorCode
eMBMasterPoll( void ) eMBMasterPoll( void )
{ {
static UCHAR *ucMBFrame; static UCHAR *ucMBFrame = NULL;
static UCHAR ucRcvAddress; static UCHAR ucRcvAddress;
static UCHAR ucFunctionCode; static UCHAR ucFunctionCode;
static USHORT usLength; static USHORT usLength;
static eMBException eException; static eMBException eException;
int i , j; int i;
int j;
eMBErrorCode eStatus = MB_ENOERR; eMBErrorCode eStatus = MB_ENOERR;
eMBMasterEventType eEvent; eMBMasterEventType eEvent;
eMBMasterErrorEventType errorType; eMBMasterErrorEventType errorType;
/* Check if the protocol stack is ready. */ /* Check if the protocol stack is ready. */
if( eMBState != STATE_ENABLED ) { if( eMBState != STATE_ENABLED )
{
return MB_EILLSTATE; return MB_EILLSTATE;
} }
/* Check if there is a event available. If not return control to caller. /* Check if there is a event available. If not return control to caller.
* Otherwise we will handle the event. */ * Otherwise we will handle the event. */
if( xMBMasterPortEventGet( &eEvent ) == TRUE ) { if( xMBMasterPortEventGet( &eEvent ) == TRUE )
{
switch ( eEvent ) switch ( eEvent )
{ {
case EV_MASTER_NO_EVENT: case EV_MASTER_NO_EVENT:
@@ -308,40 +311,57 @@ eMBMasterPoll( void )
case EV_MASTER_FRAME_RECEIVED: case EV_MASTER_FRAME_RECEIVED:
eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
// Check if the frame is for us. If not ,send an error process event. // Check if the frame is for us. If not ,send an error process event.
if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) ) { if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
{
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus); ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE ); ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
} else { }
else
{
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus); ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} }
break; break;
case EV_MASTER_EXECUTE: case EV_MASTER_EXECUTE:
if ( !ucMBFrame )
{
return MB_EILLSTATE;
}
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__);
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
eException = MB_EX_ILLEGAL_FUNCTION; eException = MB_EX_ILLEGAL_FUNCTION;
/* If receive frame has exception. The receive function code highest bit is 1.*/ /* If receive frame has exception. The receive function code highest bit is 1.*/
if(ucFunctionCode >> 7) { if (ucFunctionCode & MB_FUNC_ERROR)
{
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF]; eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
} else { }
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) { else
{
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */ /* No more function handlers registered. Abort. */
if (xMasterFuncHandlers[i].ucFunctionCode == 0) { if (xMasterFuncHandlers[i].ucFunctionCode == 0)
{
break; break;
} }
else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode)
{
vMBMasterSetCBRunInMasterMode(TRUE); vMBMasterSetCBRunInMasterMode(TRUE);
/* If master request is broadcast, /* If master request is broadcast,
* the master need execute function for all slave. * the master need execute function for all slave.
*/ */
if ( xMBMasterRequestIsBroadcast() ) { if ( xMBMasterRequestIsBroadcast() )
{
usLength = usMBMasterGetPDUSndLength(); usLength = usMBMasterGetPDUSndLength();
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++)
{
vMBMasterSetDestAddress(j); vMBMasterSetDestAddress(j);
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
} }
} else { }
else
{
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
} }
vMBMasterSetCBRunInMasterMode(FALSE); vMBMasterSetCBRunInMasterMode(FALSE);
@@ -350,10 +370,13 @@ eMBMasterPoll( void )
} }
} }
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/ /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
if (eException != MB_EX_NONE) { if (eException != MB_EX_NONE)
{
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} else { }
else
{
vMBMasterCBRequestSuccess( ); vMBMasterCBRequestSuccess( );
vMBMasterRunResRelease( ); vMBMasterRunResRelease( );
} }
@@ -363,10 +386,10 @@ eMBMasterPoll( void )
/* Master is busy now. */ /* Master is busy now. */
vMBMasterGetPDUSndBuf( &ucMBFrame ); vMBMasterGetPDUSndBuf( &ucMBFrame );
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
if (eStatus != MB_ENOERR) { if (eStatus != MB_ENOERR)
{
ESP_LOGD(MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus); ESP_LOGD(MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus);
} }
break; break;
case EV_MASTER_FRAME_SENT: case EV_MASTER_FRAME_SENT:
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__); ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__);
@@ -396,6 +419,9 @@ eMBMasterPoll( void )
} }
vMBMasterRunResRelease(); vMBMasterRunResRelease();
break; break;
default:
ESP_LOGD(MB_PORT_TAG, "%s: incorrect event triggered = %d.", __func__, eEvent);
break;
} }
} else { } else {
// xMBMasterPortEventGet has unbloked the task but the event bits are not set // xMBMasterPortEventGet has unbloked the task but the event bits are not set
@@ -465,13 +491,13 @@ void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
} }
/* Get Modbus Master current timer mode.*/ /* Get Modbus Master current timer mode.*/
eMBMasterTimerMode xMBMasterGetCurTimerMode( void ) eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void )
{ {
return eMasterCurTimerMode; return eMasterCurTimerMode;
} }
/* The master request is broadcast? */ /* The master request is broadcast? */
BOOL xMBMasterRequestIsBroadcast( void ){ BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void ){
return xFrameIsBroadcast; return xFrameIsBroadcast;
} }
@@ -480,5 +506,4 @@ void vMBMasterRequestSetType( BOOL xIsBroadcast ){
xFrameIsBroadcast = xIsBroadcast; xFrameIsBroadcast = xIsBroadcast;
} }
#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
#endif // MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0

View File

@@ -362,7 +362,7 @@ BOOL MB_PORT_ISR_ATTR xMBMasterRTUTimerExpired(void)
/* An error occured while receiving the frame. */ /* An error occured while receiving the frame. */
case STATE_M_RX_ERROR: case STATE_M_RX_ERROR:
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
break; break;
/* Function called in an illegal state. */ /* Function called in an illegal state. */

View File

@@ -119,18 +119,18 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent)
EventBits_t uxBits; EventBits_t uxBits;
BOOL xEventHappened = FALSE; BOOL xEventHappened = FALSE;
uxBits = xEventGroupWaitBits( uxBits = xEventGroupWaitBits(
xEventGroupMasterHdl, // The event group being tested. xEventGroupMasterHdl, // The event group being tested.
MB_EVENT_POLL_MASK, // The bits within the event group to wait for. MB_EVENT_POLL_MASK, // The bits within the event group to wait for.
pdTRUE, // Masked bits should be cleared before returning. pdTRUE, // Masked bits should be cleared before returning.
pdFALSE, // Don't wait for both bits, either bit will do. pdFALSE, // Don't wait for both bits, either bit will do.
portMAX_DELAY); // Wait forever for either bit to be set. portMAX_DELAY); // Wait forever for either bit to be set.
// Check if poll event is correct // Check if poll event is correct
if (uxBits & MB_EVENT_POLL_MASK) { if (uxBits & MB_EVENT_POLL_MASK) {
*eEvent = (eMBMasterEventType)(uxBits); *eEvent = (eMBMasterEventType)(uxBits);
xEventHappened = TRUE; xEventHappened = TRUE;
} else { } else {
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered.", __func__); ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered = %d.", __func__, uxBits);
xEventHappened = FALSE; xEventHappened = FALSE;
} }
return xEventHappened; return xEventHappened;