From 73bbc0cd7de7f4162cbb018af05aaf776cb0cef0 Mon Sep 17 00:00:00 2001 From: aleks Date: Mon, 11 Nov 2019 11:58:56 +0100 Subject: [PATCH] freemodbus: fix clang warnings * Original commit: espressif/esp-idf@cf4c95532f52838e6cc1c4a62d70d76ca65ca836 --- components/freemodbus/modbus/include/mb.h | 2 + .../freemodbus/modbus/include/mbproto.h | 2 +- components/freemodbus/modbus/mb.c | 15 ++-- components/freemodbus/modbus/mb_m.c | 77 ++++++++++++------- components/freemodbus/modbus/rtu/mbrtu_m.c | 2 +- components/freemodbus/port/portevent_m.c | 12 +-- 6 files changed, 71 insertions(+), 39 deletions(-) diff --git a/components/freemodbus/modbus/include/mb.h b/components/freemodbus/modbus/include/mb.h index 594dcbf..d44b4c0 100644 --- a/components/freemodbus/modbus/include/mb.h +++ b/components/freemodbus/modbus/include/mb.h @@ -76,6 +76,8 @@ PR_BEGIN_EXTERN_C /* ----------------------- Type definitions ---------------------------------*/ #ifndef _MB_M_H +#define MB_FUNC_CODE_MAX 127 + /*! \ingroup modbus * \brief Modbus serial transmission modes (RTU/ASCII). * diff --git a/components/freemodbus/modbus/include/mbproto.h b/components/freemodbus/modbus/include/mbproto.h index 786aaf4..f84cb3e 100644 --- a/components/freemodbus/modbus/include/mbproto.h +++ b/components/freemodbus/modbus/include/mbproto.h @@ -53,7 +53,7 @@ PR_BEGIN_EXTERN_C #define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 ) #define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 ) #define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 ) -#define MB_FUNC_ERROR ( 128 ) +#define MB_FUNC_ERROR ( 128u ) /* ----------------------- Type definitions ---------------------------------*/ typedef enum { diff --git a/components/freemodbus/modbus/mb.c b/components/freemodbus/modbus/mb.c index d6b7530..e64c857 100644 --- a/components/freemodbus/modbus/mb.c +++ b/components/freemodbus/modbus/mb.c @@ -229,7 +229,7 @@ eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler ) int i; eMBErrorCode eStatus; - if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) + if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= MB_FUNC_CODE_MAX ) ) { ENTER_CRITICAL_SECTION( ); if( pxHandler != NULL ) @@ -332,7 +332,7 @@ eMBDisable( void ) eMBErrorCode eMBPoll( void ) { - static UCHAR *ucMBFrame; + static UCHAR *ucMBFrame = NULL; static UCHAR ucRcvAddress; static UCHAR ucFunctionCode; static USHORT usLength; @@ -373,8 +373,10 @@ eMBPoll( void ) break; case EV_EXECUTE: - ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); - ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; + if ( !ucMBFrame ) { + return MB_EILLSTATE; + } + ESP_LOGD(MB_PORT_TAG, "%s:EV_EXECUTE", __func__); ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; eException = MB_EX_ILLEGAL_FUNCTION; for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { @@ -416,7 +418,10 @@ eMBPoll( void ) case EV_FRAME_SENT: ESP_LOGD(MB_PORT_TAG, "%s:EV_FRAME_SENT", __func__); break; + + default: + break; } } - return MB_ENOERR; + return eStatus; } diff --git a/components/freemodbus/modbus/mb_m.c b/components/freemodbus/modbus/mb_m.c index 43b8028..f9f5f5d 100644 --- a/components/freemodbus/modbus/mb_m.c +++ b/components/freemodbus/modbus/mb_m.c @@ -44,17 +44,17 @@ #include "mbfunc.h" #include "mbport.h" -#if MB_MASTER_RTU_ENABLED == 1 +#if MB_MASTER_RTU_ENABLED #include "mbrtu.h" #endif -#if MB_MASTER_ASCII_ENABLED == 1 +#if MB_MASTER_ASCII_ENABLED #include "mbascii.h" #endif -#if MB_MASTER_TCP_ENABLED == 1 +#if MB_MASTER_TCP_ENABLED #include "mbtcp.h" #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 #define MB_PORT_HAS_CLOSE 1 @@ -264,25 +264,28 @@ eMBMasterDisable( void ) eMBErrorCode eMBMasterPoll( void ) { - static UCHAR *ucMBFrame; + static UCHAR *ucMBFrame = NULL; static UCHAR ucRcvAddress; static UCHAR ucFunctionCode; static USHORT usLength; static eMBException eException; - int i , j; + int i; + int j; eMBErrorCode eStatus = MB_ENOERR; eMBMasterEventType eEvent; eMBMasterErrorEventType errorType; /* 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( xMBMasterPortEventGet( &eEvent ) == TRUE ) { + if( xMBMasterPortEventGet( &eEvent ) == TRUE ) + { switch ( eEvent ) { case EV_MASTER_NO_EVENT: @@ -308,40 +311,57 @@ eMBMasterPoll( void ) case EV_MASTER_FRAME_RECEIVED: eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); // 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); ( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE ); - } else { + } + else + { vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); } break; case EV_MASTER_EXECUTE: + if ( !ucMBFrame ) + { + return MB_EILLSTATE; + } ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__); ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF]; eException = MB_EX_ILLEGAL_FUNCTION; /* 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]; - } 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. */ - if (xMasterFuncHandlers[i].ucFunctionCode == 0) { + if (xMasterFuncHandlers[i].ucFunctionCode == 0) + { break; } - else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) { + if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) + { vMBMasterSetCBRunInMasterMode(TRUE); /* If master request is broadcast, * the master need execute function for all slave. */ - if ( xMBMasterRequestIsBroadcast() ) { + if ( xMBMasterRequestIsBroadcast() ) + { usLength = usMBMasterGetPDUSndLength(); - for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){ + for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++) + { vMBMasterSetDestAddress(j); eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); } - } else { + } + else + { eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength); } vMBMasterSetCBRunInMasterMode(FALSE); @@ -350,10 +370,13 @@ eMBMasterPoll( void ) } } /* 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); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); - } else { + } + else + { vMBMasterCBRequestSuccess( ); vMBMasterRunResRelease( ); } @@ -363,10 +386,10 @@ eMBMasterPoll( void ) /* Master is busy now. */ vMBMasterGetPDUSndBuf( &ucMBFrame ); 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); } - break; case EV_MASTER_FRAME_SENT: ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__); @@ -396,6 +419,9 @@ eMBMasterPoll( void ) } vMBMasterRunResRelease(); break; + default: + ESP_LOGD(MB_PORT_TAG, "%s: incorrect event triggered = %d.", __func__, eEvent); + break; } } else { // 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.*/ -eMBMasterTimerMode xMBMasterGetCurTimerMode( void ) +eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void ) { return eMasterCurTimerMode; } /* The master request is broadcast? */ -BOOL xMBMasterRequestIsBroadcast( void ){ +BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void ){ return xFrameIsBroadcast; } @@ -480,5 +506,4 @@ void vMBMasterRequestSetType( BOOL xIsBroadcast ){ xFrameIsBroadcast = xIsBroadcast; } - -#endif // MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 +#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED diff --git a/components/freemodbus/modbus/rtu/mbrtu_m.c b/components/freemodbus/modbus/rtu/mbrtu_m.c index f9abd12..f4c1ef4 100644 --- a/components/freemodbus/modbus/rtu/mbrtu_m.c +++ b/components/freemodbus/modbus/rtu/mbrtu_m.c @@ -362,7 +362,7 @@ BOOL MB_PORT_ISR_ATTR xMBMasterRTUTimerExpired(void) /* An error occured while receiving the frame. */ case STATE_M_RX_ERROR: vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); - xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); + xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS); break; /* Function called in an illegal state. */ diff --git a/components/freemodbus/port/portevent_m.c b/components/freemodbus/port/portevent_m.c index abb6fd7..ed023cb 100644 --- a/components/freemodbus/port/portevent_m.c +++ b/components/freemodbus/port/portevent_m.c @@ -119,18 +119,18 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent) EventBits_t uxBits; BOOL xEventHappened = FALSE; uxBits = xEventGroupWaitBits( - xEventGroupMasterHdl, // The event group being tested. - MB_EVENT_POLL_MASK, // The bits within the event group to wait for. - pdTRUE, // Masked bits should be cleared before returning. - pdFALSE, // Don't wait for both bits, either bit will do. - portMAX_DELAY); // Wait forever for either bit to be set. + xEventGroupMasterHdl, // The event group being tested. + MB_EVENT_POLL_MASK, // The bits within the event group to wait for. + pdTRUE, // Masked bits should be cleared before returning. + pdFALSE, // Don't wait for both bits, either bit will do. + portMAX_DELAY); // Wait forever for either bit to be set. // Check if poll event is correct if (uxBits & MB_EVENT_POLL_MASK) { *eEvent = (eMBMasterEventType)(uxBits); xEventHappened = TRUE; } 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; } return xEventHappened;