From 210a55c1f7e633e795d22da4bc2bc4f6fa5b6c65 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Mon, 29 Mar 2021 14:35:09 +0800 Subject: [PATCH 01/24] Update cellular library to MIT license --- LICENSE | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/LICENSE b/LICENSE index 1bb4f21e..9cf10627 100644 --- a/LICENSE +++ b/LICENSE @@ -1,15 +1,19 @@ -Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +MIT License -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 607a810f41cb388751d5790f4169512f71175034 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 10:01:27 +0800 Subject: [PATCH 02/24] Use PktRespMutex in pktio thread * Use PktRespMutex in pktio thread for shared variables --- source/cellular_common.c | 2 + source/cellular_common_api.c | 10 +++ source/cellular_pkthandler.c | 62 +++++++++++++++++-- source/cellular_pktio.c | 10 ++- .../private/cellular_common_internal.h | 4 +- .../include/private/cellular_pktio_internal.h | 2 +- 6 files changed, 81 insertions(+), 9 deletions(-) diff --git a/source/cellular_common.c b/source/cellular_common.c index 03852666..988974f5 100644 --- a/source/cellular_common.c +++ b/source/cellular_common.c @@ -1115,6 +1115,7 @@ CellularError_t _Cellular_RegisterUndefinedRespCallback( CellularContext_t * pCo else { /* undefinedRespCallback can be set to NULL to unregister the callback. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->undefinedRespCallback = undefinedRespCallback; if( pContext->undefinedRespCallback != NULL ) @@ -1125,6 +1126,7 @@ CellularError_t _Cellular_RegisterUndefinedRespCallback( CellularContext_t * pCo { pContext->pUndefinedRespCBContext = NULL; } + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; diff --git a/source/cellular_common_api.c b/source/cellular_common_api.c index c936b22d..5c010d54 100644 --- a/source/cellular_common_api.c +++ b/source/cellular_common_api.c @@ -206,8 +206,10 @@ CellularError_t Cellular_CommonRegisterUrcNetworkRegistrationEventCallback( Cell } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->cbEvents.networkRegistrationCallback = networkRegistrationCallback; pContext->cbEvents.pNetworkRegistrationCallbackContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; @@ -231,8 +233,10 @@ CellularError_t Cellular_CommonRegisterUrcPdnEventCallback( CellularHandle_t cel } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->cbEvents.pdnEventCallback = pdnEventCallback; pContext->cbEvents.pPdnEventCallbackContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; @@ -256,8 +260,10 @@ CellularError_t Cellular_CommonRegisterUrcSignalStrengthChangedCallback( Cellula } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->cbEvents.signalStrengthChangedCallback = signalStrengthChangedCallback; pContext->cbEvents.pSignalStrengthChangedCallbackContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; @@ -281,8 +287,10 @@ CellularError_t Cellular_CommonRegisterUrcGenericCallback( CellularHandle_t cell } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->cbEvents.genericCallback = genericCallback; pContext->cbEvents.pGenericCallbackContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; @@ -306,8 +314,10 @@ CellularError_t Cellular_CommonRegisterModemEventCallback( CellularHandle_t cell } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->cbEvents.modemEventCallback = modemEventCallback; pContext->cbEvents.pModemEventCallbackContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); } return cellularStatus; diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index dd591129..564cd142 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -93,7 +93,6 @@ static CellularPktStatus_t _convertAndQueueRespPacket( CellularContext_t * pCont if( ( pBuf != NULL ) ) { pAtResp = ( const CellularATCommandResponse_t * ) pBuf; - PlatformMutex_Lock( &pContext->PktRespMutex ); if( pAtResp->status == false ) { @@ -115,8 +114,6 @@ static CellularPktStatus_t _convertAndQueueRespPacket( CellularContext_t * pCont pktStatus = CELLULAR_PKT_STATUS_FAILURE; LogError( ( "_convertAndQueueRespPacket: Got a response when the Resp Q is full!!" ) ); } - - PlatformMutex_Unlock( &pContext->PktRespMutex ); } else { @@ -217,12 +214,16 @@ static CellularPktStatus_t _Cellular_AtcmdRequestTimeoutWithCallbackRaw( Cellula } else { + LogDebug( ( ">>>>>Start sending [%s]<<<<<", atReq.pAtCmd ) ); + /* Fill in request info structure. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pktRespCB = atReq.respCallback; - LogDebug( ( ">>>>>Start sending [%s]<<<<<", atReq.pAtCmd ) ); pContext->pPktUsrData = atReq.pData; pContext->PktUsrDataLen = ( uint16_t ) atReq.dataLen; pContext->pCurrentCmd = atReq.pAtCmd; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + pktStatus = _Cellular_PktioSendAtCmd( pContext, atReq.pAtCmd, atReq.atCmdType, atReq.pAtRspPrefix ); if( pktStatus != CELLULAR_PKT_STATUS_OK ) @@ -252,9 +253,11 @@ static CellularPktStatus_t _Cellular_AtcmdRequestTimeoutWithCallbackRaw( Cellula } /* No command is waiting response. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; pContext->pktRespCB = NULL; pContext->pCurrentCmd = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); LogDebug( ( "<<<<>>>>Start sending Data <<<<<" ) ); + + /* Send the packet. Data send is regarded as CELLULAR_AT_NO_RESULT. Only + * success or error token is expected in the result. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->PktioAtCmdType = CELLULAR_AT_NO_RESULT; - /* Send the packet. */ + PlatformMutex_Unlock( &pContext->PktRespMutex ); + *dataReq.pSentDataLength = _Cellular_PktioSendData( pContext, dataReq.pData, dataReq.dataLen ); if( *dataReq.pSentDataLength != dataReq.dataLen ) @@ -298,7 +306,9 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex /* End pattern for specific modem. */ if( ( pktStatus == CELLULAR_PKT_STATUS_OK ) && ( dataReq.pEndPattern != NULL ) ) { + PlatformMutex_Lock( &pContext->PktRespMutex ); sendEndPatternLen = _Cellular_PktioSendData( pContext, dataReq.pEndPattern, dataReq.endPatternLen ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); if( sendEndPatternLen != dataReq.endPatternLen ) { @@ -331,7 +341,11 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex LogError( ( "pkt_recv status=%d, data sending timed out", pktStatus ) ); } + /* Set AT command type to CELLULAR_AT_NO_COMMAND for timeout case here. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + LogDebug( ( "<<<<>>>>", pktStatus ) ); } @@ -573,11 +587,21 @@ CellularPktStatus_t _Cellular_AtcmdRequestSuccessToken( CellularContext_t * pCon else { _Cellular_PktHandlerAcquirePktRequestMutex( pContext ); + + /* Set the extra Token table for this AT command. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->tokenTable.pCellularSrcExtraTokenSuccessTable = pCellularSrcTokenSuccessTable; pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize = cellularSrcTokenSuccessTableSize; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + pktStatus = _Cellular_AtcmdRequestTimeoutWithCallbackRaw( pContext, atReq, atTimeoutMS ); + + /* Clear the extra Token table for this AT command. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize = 0; pContext->tokenTable.pCellularSrcExtraTokenSuccessTable = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + _Cellular_PktHandlerReleasePktRequestMutex( pContext ); } @@ -602,11 +626,21 @@ CellularPktStatus_t _Cellular_TimeoutAtcmdDataRecvRequestWithCallback( CellularC else { _Cellular_PktHandlerAcquirePktRequestMutex( pContext ); + + /* Set the data receive prefix. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pktDataPrefixCB = pktDataPrefixCallback; pContext->pDataPrefixCBContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + pktStatus = _Cellular_AtcmdRequestTimeoutWithCallbackRaw( pContext, atReq, timeoutMS ); + + /* Clear the data receive prefix. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pktDataPrefixCB = NULL; pContext->pDataPrefixCBContext = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + _Cellular_PktHandlerReleasePktRequestMutex( pContext ); } @@ -634,11 +668,20 @@ CellularPktStatus_t _Cellular_AtcmdDataSend( CellularContext_t * pContext, else { _Cellular_PktHandlerAcquirePktRequestMutex( pContext ); + + /* Set the data send prefix callback. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pktDataSendPrefixCB = pktDataSendPrefixCallback; pContext->pDataSendPrefixCBContext = pCallbackContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + pktStatus = _Cellular_AtcmdRequestTimeoutWithCallbackRaw( pContext, atReq, atTimeoutMS ); + + /* Clear the data send prefix callback. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pDataSendPrefixCBContext = NULL; pContext->pktDataSendPrefixCB = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); if( pktStatus == CELLULAR_PKT_STATUS_OK ) { @@ -682,11 +725,20 @@ CellularPktStatus_t _Cellular_TimeoutAtcmdDataSendSuccessToken( CellularContext_ else { _Cellular_PktHandlerAcquirePktRequestMutex( pContext ); + + /* Set the extra token table. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->tokenTable.pCellularSrcExtraTokenSuccessTable = pCellularSrcTokenSuccessTable; pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize = cellularSrcTokenSuccessTableSize; + PlatformMutex_Lock( &pContext->PktRespMutex ); + pktStatus = _Cellular_AtcmdRequestTimeoutWithCallbackRaw( pContext, atReq, atTimeoutMS ); + + /* Clear the extra token table. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize = 0; pContext->tokenTable.pCellularSrcExtraTokenSuccessTable = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); if( pktStatus == CELLULAR_PKT_STATUS_OK ) { diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 9f006564..f147497c 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -1046,7 +1046,11 @@ static void _pktioReadThread( void * pUserData ) /* Keep Reading until there is no more bytes in comm interface. */ do { + /* When pktio thread is parsing the response from modem, pktio + * thread holds the PktRespMutex until no RX data from comm interface. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); bytesRead = _handleRxDataEvent( pContext, &pAtResp ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); } while( ( bytesRead != 0U ) ); } else @@ -1205,6 +1209,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->pRespPrefix = pAtRspPrefix; pContext->PktioAtCmdType = atType; newCmdLen = cmdLen; @@ -1216,6 +1221,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, ( void ) pContext->pCommIntf->send( pContext->hPktioCommIntf, ( const uint8_t * ) &pContext->pktioSendBuf, newCmdLen, CELLULAR_COMM_IF_SEND_TIMEOUT_MS, &sentLen ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); } } @@ -1225,7 +1231,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, /*-----------------------------------------------------------*/ /* Sends data to the modem. */ -uint32_t _Cellular_PktioSendData( const CellularContext_t * pContext, +uint32_t _Cellular_PktioSendData( CellularContext_t * pContext, const uint8_t * pData, uint32_t dataLen ) { @@ -1245,8 +1251,10 @@ uint32_t _Cellular_PktioSendData( const CellularContext_t * pContext, } else { + PlatformMutex_Lock( &pContext->PktRespMutex ); ( void ) pContext->pCommIntf->send( pContext->hPktioCommIntf, pData, dataLen, CELLULAR_COMM_IF_SEND_TIMEOUT_MS, &sentLen ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); } LogDebug( ( "PktioSendData sent %d bytes", sentLen ) ); diff --git a/source/include/private/cellular_common_internal.h b/source/include/private/cellular_common_internal.h index 798560dc..f2e5e61a 100644 --- a/source/include/private/cellular_common_internal.h +++ b/source/include/private/cellular_common_internal.h @@ -113,8 +113,8 @@ struct CellularContext CellularTokenTable_t tokenTable; /* Packet handler. */ - PlatformMutex_t pktRequestMutex; - PlatformMutex_t PktRespMutex; + PlatformMutex_t pktRequestMutex; /* The mutex for sending request. */ + PlatformMutex_t PktRespMutex; /* The mutex for parsing the response from modem. */ QueueHandle_t pktRespQueue; CellularATCommandResponseReceivedCallback_t pktRespCB; CellularATCommandDataPrefixCallback_t pktDataPrefixCB; /* Data prefix callback function for socket receive function. */ diff --git a/source/include/private/cellular_pktio_internal.h b/source/include/private/cellular_pktio_internal.h index 2ec8e51f..9d4771e0 100644 --- a/source/include/private/cellular_pktio_internal.h +++ b/source/include/private/cellular_pktio_internal.h @@ -129,7 +129,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, * * @return The data actually send to the comm interface. */ -uint32_t _Cellular_PktioSendData( const CellularContext_t * pContext, +uint32_t _Cellular_PktioSendData( CellularContext_t * pContext, const uint8_t * pData, uint32_t dataLen ); From ca85365037f33b130cc4a7c7f8f7fcb6f12d3f1e Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 17:33:15 +0800 Subject: [PATCH 03/24] Update pktio to handle undefined message when sending AT command --- source/cellular_pktio.c | 42 +++- test/unit-test/cellular_pktio_utest.c | 321 ++++++++++++++++++++++++-- 2 files changed, 337 insertions(+), 26 deletions(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index f147497c..af3cfbc2 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -240,10 +240,17 @@ static CellularPktStatus_t _processIntermediateResponse( char * pLine, break; case CELLULAR_AT_MULTI_DATA_WO_PREFIX: - default: _saveATData( pLine, pResp ); pkStatus = CELLULAR_PKT_STATUS_PENDING_BUFFER; break; + + default: + /* Unexpected message received when sending the AT command. */ + LogInfo( ( "Undefind message received %s when sending AT command type %d.", + pLine, atType ) ); + + pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + break; } return pkStatus; @@ -686,30 +693,36 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, if( pkStatus == CELLULAR_PKT_STATUS_OK ) { + /* This command is completed. Call the user callback to parse the result. */ if( pContext->pPktioHandlepktCB != NULL ) { ( void ) pContext->pPktioHandlepktCB( pContext, AT_SOLICITED, *ppAtResp ); } + /* Clean the command type. Further response from cellular modem won't be + * regarded as AT_COLICITED response. */ + pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; + FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); } else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_BUFFER ) { - /* Check data prefix first then store the data if this command has data response. */ + /* This commaned expects raw data to be appended to buffer. Check data + * prefix first then store the data if this command has data response. */ + } + else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_DATA ) + { + /* The command expects more response line. */ } else { - if( pkStatus != CELLULAR_PKT_STATUS_PENDING_DATA ) - { - ( void ) memset( pContext->pktioReadBuf, 0, PKTIO_READ_BUFFER_SIZE + 1U ); - pContext->pPktioReadPtr = NULL; - FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); - /* pContext->pCurrentCmd is not NULL since it is a solicited response. */ - LogError( ( "processLine ERROR, cleaning up! Current command %s", pContext->pCurrentCmd ) ); - } + /* A unexpected message received when sending the AT command.Try to + * handle it with undefined response callback. */ + pContext->recvdMsgType = AT_UNDEFINED; } } - else + + if( pContext->recvdMsgType == AT_UNDEFINED ) { /* Pktio receives AT_UNDEFINED response from modem. This could be module specific * response. Cellular module registers the callback function through _Cellular_RegisterUndefinedRespCallback @@ -724,7 +737,12 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, pContext->pPktioReadPtr = NULL; pContext->partialDataRcvdLen = 0; FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); - pkStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + else + { + /* The undefined response callback handle this message without problem. */ + pkStatus = CELLULAR_PKT_STATUS_OK; } } diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index 86a76cc8..1ee93f20 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -83,6 +83,12 @@ struct _cellularCommContext int test3; }; +typedef enum commIfRecvType +{ + COMM_IF_RECV_CUSTOM_STRING, + COMM_IF_RECV_NORMAL +} commIfRecvType_t; + static uint16_t pktioEvtMask = 0x0000U; static MockPlatformEventGroup_t evtGroup = { 0 }; @@ -113,6 +119,11 @@ static int setpktDataPrefixCBReturn = 0; static int32_t customCallbackContext = 0; +static commIfRecvType_t testCommIfRecvType = COMM_IF_RECV_NORMAL; +static char *pCommIntfRecvCustomString = NULL; + +static bool atCmdStatusUndefindTest = false; + /* Try to Keep this map in Alphabetical order. */ /* FreeRTOS Cellular Common Library porting interface. */ /* coverity[misra_c_2012_rule_8_7_violation] */ @@ -208,6 +219,9 @@ void setUp() setpktDataPrefixCBReturn = 0; testInfiniteLoop = 0; evtGroupHandle = &evtGroup; + + testCommIfRecvType = COMM_IF_RECV_NORMAL; + pCommIntfRecvCustomString = NULL; } /* Called after each test method. */ @@ -233,6 +247,16 @@ void dummyDelay( uint32_t milliseconds ) ( void ) milliseconds; } +void MockPlatformMutex_Unlock( PlatformMutex_t * pMutex ) +{ + ( void ) pMutex; +} + +void MockPlatformMutex_Lock( PlatformMutex_t * pMutex ) +{ + ( void ) pMutex; +} + void * mock_malloc( size_t size ) { return ( void * ) malloc( size ); @@ -360,7 +384,7 @@ bool Platform_CreateDetachedThread( void ( * threadRoutine )( void * pArgument ) return threadReturn; } -static CellularCommInterfaceError_t _prvCommIntfOpen( CellularCommInterfaceReceiveCallback_t receiveCallback, +static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiveCallback_t receiveCallback, void * pUserData, CellularCommInterfaceHandle_t * pCommInterfaceHandle ) { @@ -376,7 +400,7 @@ static CellularCommInterfaceError_t _prvCommIntfOpen( CellularCommInterfaceRecei return commIntRet; } -static CellularCommInterfaceError_t _prvCommIntfOpenCallrecvCallbackNullContext( CellularCommInterfaceReceiveCallback_t receiveCallback, +static CellularCommInterfaceError_t prvCommIntfOpenCallrecvCallbackNullContext( CellularCommInterfaceReceiveCallback_t receiveCallback, void * pUserData, CellularCommInterfaceHandle_t * pCommInterfaceHandle ) { @@ -394,7 +418,7 @@ static CellularCommInterfaceError_t _prvCommIntfOpenCallrecvCallbackNullContext( return commIntRet; } -static CellularCommInterfaceError_t _prvCommIntfClose( CellularCommInterfaceHandle_t commInterfaceHandle ) +static CellularCommInterfaceError_t prvCommIntfClose( CellularCommInterfaceHandle_t commInterfaceHandle ) { CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; @@ -403,7 +427,7 @@ static CellularCommInterfaceError_t _prvCommIntfClose( CellularCommInterfaceHand return commIntRet; } -static CellularCommInterfaceError_t _prvCommIntfSend( CellularCommInterfaceHandle_t commInterfaceHandle, +static CellularCommInterfaceError_t prvCommIntfSend( CellularCommInterfaceHandle_t commInterfaceHandle, const uint8_t * pData, uint32_t dataLength, uint32_t timeoutMilliseconds, @@ -422,11 +446,11 @@ static CellularCommInterfaceError_t _prvCommIntfSend( CellularCommInterfaceHandl return commIntRet; } -static CellularCommInterfaceError_t _prvCommIntfReceive( CellularCommInterfaceHandle_t commInterfaceHandle, - uint8_t * pBuffer, - uint32_t bufferLength, - uint32_t timeoutMilliseconds, - uint32_t * pDataReceivedLength ) +static CellularCommInterfaceError_t prvCommIntfReceiveNormal( CellularCommInterfaceHandle_t commInterfaceHandle, + uint8_t * pBuffer, + uint32_t bufferLength, + uint32_t timeoutMilliseconds, + uint32_t * pDataReceivedLength ) { CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; @@ -566,12 +590,67 @@ static CellularCommInterfaceError_t _prvCommIntfReceive( CellularCommInterfaceHa return commIntRet; } + +static CellularCommInterfaceError_t prvCommIntfReceiveCustomString( CellularCommInterfaceHandle_t commInterfaceHandle, + uint8_t * pBuffer, + uint32_t bufferLength, + uint32_t timeoutMilliseconds, + uint32_t * pDataReceivedLength ) +{ + CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; + + ( void ) commInterfaceHandle; + ( void ) pBuffer; + ( void ) bufferLength; + ( void ) timeoutMilliseconds; + ( void ) pDataReceivedLength; + + if( recvCount > 0 ) + { + recvCount--; + + strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, strlen( pCommIntfRecvCustomString ) ); + *pDataReceivedLength = strlen( pCommIntfRecvCustomString ); + } + else + { + *pDataReceivedLength = 0; + } + return commIntRet; +} + +static CellularCommInterfaceError_t prvCommIntfReceive( CellularCommInterfaceHandle_t commInterfaceHandle, + uint8_t * pBuffer, + uint32_t bufferLength, + uint32_t timeoutMilliseconds, + uint32_t * pDataReceivedLength ) +{ + CellularCommInterfaceError_t commIfRet; + if( testCommIfRecvType == COMM_IF_RECV_CUSTOM_STRING ) + { + commIfRet = prvCommIntfReceiveCustomString( commInterfaceHandle, + pBuffer, + bufferLength, + timeoutMilliseconds, + pDataReceivedLength ); + } + else + { + commIfRet = prvCommIntfReceiveNormal( commInterfaceHandle, + pBuffer, + bufferLength, + timeoutMilliseconds, + pDataReceivedLength ); + } + return commIfRet; +} + static CellularCommInterface_t CellularCommInterface = { - .open = _prvCommIntfOpen, - .send = _prvCommIntfSend, - .recv = _prvCommIntfReceive, - .close = _prvCommIntfClose + .open = prvCommIntfOpen, + .send = prvCommIntfSend, + .recv = prvCommIntfReceive, + .close = prvCommIntfClose }; static void _shutdownCallback( CellularContext_t * pContext ) @@ -582,6 +661,31 @@ static void _shutdownCallback( CellularContext_t * pContext ) } } +static CellularPktStatus_t prvUndefinedHandlePacket( CellularContext_t * pContext, + _atRespType_t atRespType, + const void * pBuf ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + const CellularATCommandResponse_t * pAtResp = NULL; + + switch( atRespType ) + { + case AT_SOLICITED: + pAtResp = ( const CellularATCommandResponse_t * ) pBuf; + atCmdStatusUndefindTest = pAtResp->status; + break; + + case AT_UNSOLICITED: + break; + + default: + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + break; + } + + return pktStatus; +} + CellularPktStatus_t PktioHandlePacketCallback_t( CellularContext_t * pContext, _atRespType_t atRespType, const void * pBuffer ) @@ -822,7 +926,7 @@ void test__Cellular_PktioInit_Thread_Comm_Open_ReceiveCallback_Null_Context_Fail context.pPktioShutdownCB = _shutdownCallback; /* Assign the comm interface to pContext. */ - commIntf.open = _prvCommIntfOpenCallrecvCallbackNullContext; + commIntf.open = prvCommIntfOpenCallrecvCallbackNullContext; /* Check that CELLULAR_PKT_STATUS_OK is returned. */ pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); @@ -1303,6 +1407,195 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_AT_UNDEFINED_callback_fail( v TEST_ASSERT_EQUAL_INT32( 0, customCallbackContext ); } +/** + * @brief Test undefined message callback handling okay in pktio thread when sending AT command. + * + * The following sequence should have no error : + * 1. Sending CELLULAR_AT_NO_RESULT type message ( for example, AT+CFUN=1 ) + * 2. Receiving "OK\r\n", CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n" + * 3. The AT command should success. The UNKNOW_TOKEN should cause a call to undefined callback. + * 4. Okay is returned in the undefined response callback. + */ +void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Assign the comm interface to pContext. */ + context.pCommIntf = pCommIntf; + context.pPktioShutdownCB = _shutdownCallback; + + /* Test the rx_data event with CELLULAR_AT_NO_RESULT type AT command. */ + atCmdStatusUndefindTest = false; + pktioEvtMask = PKTIO_EVT_MASK_RX_DATA; + recvCount = 1; + atCmdType = CELLULAR_AT_NO_RESULT; + testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; + pCommIntfRecvCustomString = "OK\r\n"CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n"; + + /* Setup the callback to handle the undefined message. */ + context.undefinedRespCallback = undefinedRespCallback; + customCallbackContext = 0; + context.pUndefinedRespCBContext = &customCallbackContext; + + /* Copy the token table. */ + ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); + + /* Check that CELLULAR_PKT_STATUS_OK is returned. */ + threadReturn = true; /* Set pktio thread return flag. */ + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Verify the command should return success. */ + TEST_ASSERT_EQUAL( atCmdStatusUndefindTest, true ); + + /* Verify undefinedRespCallback is called and the expected string is received. */ + TEST_ASSERT_EQUAL_INT32( 1, customCallbackContext ); +} + +/** + * @brief Test undefined message callback handling okay in pktio thread when sending AT command. + * + * The following sequence should have no error : + * 1. Sending CELLULAR_AT_NO_RESULT type message ( for example, AT+CFUN=1 ) + * 2. Receiving CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n", "OK\r\n" + * 3. The AT command should success. The UNKNOW_TOKEN should cause a call to undefined callback. + * 4. Okay is returned in the undefined response callback. + */ +void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_okay( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Assign the comm interface to pContext. */ + context.pCommIntf = pCommIntf; + context.pPktioShutdownCB = _shutdownCallback; + + /* Test the rx_data event with CELLULAR_AT_NO_RESULT type AT command. */ + atCmdStatusUndefindTest = false; + pktioEvtMask = PKTIO_EVT_MASK_RX_DATA; + recvCount = 1; + atCmdType = CELLULAR_AT_NO_RESULT; + testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; + pCommIntfRecvCustomString = CELLULAR_AT_UNDEFINED_STRING_RESP"\r\nOK\r\n"; + + /* Setup the callback to handle the undefined message. */ + context.undefinedRespCallback = undefinedRespCallback; + customCallbackContext = 0; + context.pUndefinedRespCBContext = &customCallbackContext; + + /* Copy the token table. */ + ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); + + /* Check that CELLULAR_PKT_STATUS_OK is returned. */ + threadReturn = true; /* Set pktio thread return flag. */ + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Verify the command should return success. */ + TEST_ASSERT_EQUAL( atCmdStatusUndefindTest, true ); + + /* Verify undefinedRespCallback is called and the expected string is received. */ + TEST_ASSERT_EQUAL_INT32( 1, customCallbackContext ); +} + +/** + * @brief Test undefined message callback handling error in pktio thread when sending AT command. + * + * The following sequence should have no error : + * 1. Sending CELLULAR_AT_NO_RESULT type message ( for example, AT+CFUN=1 ) + * 2. Receiving CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n", "OK\r\n" + * 3. The AT command should success. The UNKNOW_TOKEN should cause a call to undefined callback. + * 4. Error is returned in the undefined response callback. This would flush the + * pktio read buffer. + */ +void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_fail( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Assign the comm interface to pContext. */ + context.pCommIntf = pCommIntf; + context.pPktioShutdownCB = _shutdownCallback; + + /* Test the rx_data event with CELLULAR_AT_NO_RESULT type AT command. */ + atCmdStatusUndefindTest = false; + pktioEvtMask = PKTIO_EVT_MASK_RX_DATA; + recvCount = 1; + atCmdType = CELLULAR_AT_NO_RESULT; + testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; + pCommIntfRecvCustomString = "UNKNOWN_UNDEFINED\r\nOK\r\n"; + + /* Setup the callback to handle the undefined message. */ + context.undefinedRespCallback = undefinedRespCallback; + customCallbackContext = 1; + context.pUndefinedRespCBContext = &customCallbackContext; + + /* Copy the token table. */ + ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); + + /* Check that CELLULAR_PKT_STATUS_OK is returned. */ + threadReturn = true; /* Set pktio thread return flag. */ + pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Verify the command should has error. */ + TEST_ASSERT_EQUAL( atCmdStatusUndefindTest, false ); + + /* Verify undefinedRespCallback is called but the expected string is not received. */ + TEST_ASSERT_EQUAL_INT32( 0, customCallbackContext ); +} + +/** + * @brief Test undefined message no callback in pktio thread when sending AT command. + * + * The following sequence should have no error : + * 1. Sending CELLULAR_AT_NO_RESULT type message ( for example, AT+CFUN=1 ) + * 2. Receiving CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n", "OK\r\n" + * 3. The AT command should success. The UNKNOW_TOKEN should cause a call to undefined callback. + * 4. The command callback won't be called. This would cause a timeout in pkthandler. + */ +void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_no_callback_fail( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Assign the comm interface to pContext. */ + context.pCommIntf = pCommIntf; + context.pPktioShutdownCB = _shutdownCallback; + + /* Test the rx_data event with CELLULAR_AT_NO_RESULT type AT command. */ + atCmdStatusUndefindTest = false; + pktioEvtMask = PKTIO_EVT_MASK_RX_DATA; + recvCount = 1; + atCmdType = CELLULAR_AT_NO_RESULT; + testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; + pCommIntfRecvCustomString = "UNKNOWN_UNDEFINED\r\nOK\r\n"; + + /* Copy the token table. */ + ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); + + /* Check that CELLULAR_PKT_STATUS_OK is returned. */ + threadReturn = true; /* Set pktio thread return flag. */ + pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Verify the command should has error. */ + TEST_ASSERT_EQUAL( atCmdStatusUndefindTest, false ); +} + /** * @brief Test thread receiving rx data event with CELLULAR_AT_MULTI_WO_PREFIX resp for _Cellular_PktioInit to return CELLULAR_PKT_STATUS_OK. */ From ca70d3fadfc4c42e6c206cecfe7676730ea7276a Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 18:05:25 +0800 Subject: [PATCH 04/24] Fix uncrustify --- source/cellular_common.c | 1 + .../private/cellular_common_internal.h | 4 +- test/unit-test/cellular_pktio_utest.c | 41 ++++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/source/cellular_common.c b/source/cellular_common.c index 988974f5..911cc536 100644 --- a/source/cellular_common.c +++ b/source/cellular_common.c @@ -1126,6 +1126,7 @@ CellularError_t _Cellular_RegisterUndefinedRespCallback( CellularContext_t * pCo { pContext->pUndefinedRespCBContext = NULL; } + PlatformMutex_Unlock( &pContext->PktRespMutex ); } diff --git a/source/include/private/cellular_common_internal.h b/source/include/private/cellular_common_internal.h index f2e5e61a..4fcd9cce 100644 --- a/source/include/private/cellular_common_internal.h +++ b/source/include/private/cellular_common_internal.h @@ -113,8 +113,8 @@ struct CellularContext CellularTokenTable_t tokenTable; /* Packet handler. */ - PlatformMutex_t pktRequestMutex; /* The mutex for sending request. */ - PlatformMutex_t PktRespMutex; /* The mutex for parsing the response from modem. */ + PlatformMutex_t pktRequestMutex; /* The mutex for sending request. */ + PlatformMutex_t PktRespMutex; /* The mutex for parsing the response from modem. */ QueueHandle_t pktRespQueue; CellularATCommandResponseReceivedCallback_t pktRespCB; CellularATCommandDataPrefixCallback_t pktDataPrefixCB; /* Data prefix callback function for socket receive function. */ diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index 1ee93f20..9ae0a19f 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -120,7 +120,7 @@ static int setpktDataPrefixCBReturn = 0; static int32_t customCallbackContext = 0; static commIfRecvType_t testCommIfRecvType = COMM_IF_RECV_NORMAL; -static char *pCommIntfRecvCustomString = NULL; +static char * pCommIntfRecvCustomString = NULL; static bool atCmdStatusUndefindTest = false; @@ -385,8 +385,8 @@ bool Platform_CreateDetachedThread( void ( * threadRoutine )( void * pArgument ) } static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiveCallback_t receiveCallback, - void * pUserData, - CellularCommInterfaceHandle_t * pCommInterfaceHandle ) + void * pUserData, + CellularCommInterfaceHandle_t * pCommInterfaceHandle ) { CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; @@ -401,8 +401,8 @@ static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiv } static CellularCommInterfaceError_t prvCommIntfOpenCallrecvCallbackNullContext( CellularCommInterfaceReceiveCallback_t receiveCallback, - void * pUserData, - CellularCommInterfaceHandle_t * pCommInterfaceHandle ) + void * pUserData, + CellularCommInterfaceHandle_t * pCommInterfaceHandle ) { CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; @@ -428,10 +428,10 @@ static CellularCommInterfaceError_t prvCommIntfClose( CellularCommInterfaceHandl } static CellularCommInterfaceError_t prvCommIntfSend( CellularCommInterfaceHandle_t commInterfaceHandle, - const uint8_t * pData, - uint32_t dataLength, - uint32_t timeoutMilliseconds, - uint32_t * pDataSentLength ) + const uint8_t * pData, + uint32_t dataLength, + uint32_t timeoutMilliseconds, + uint32_t * pDataSentLength ) { CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS; @@ -616,16 +616,18 @@ static CellularCommInterfaceError_t prvCommIntfReceiveCustomString( CellularComm { *pDataReceivedLength = 0; } + return commIntRet; } static CellularCommInterfaceError_t prvCommIntfReceive( CellularCommInterfaceHandle_t commInterfaceHandle, - uint8_t * pBuffer, - uint32_t bufferLength, - uint32_t timeoutMilliseconds, - uint32_t * pDataReceivedLength ) + uint8_t * pBuffer, + uint32_t bufferLength, + uint32_t timeoutMilliseconds, + uint32_t * pDataReceivedLength ) { CellularCommInterfaceError_t commIfRet; + if( testCommIfRecvType == COMM_IF_RECV_CUSTOM_STRING ) { commIfRet = prvCommIntfReceiveCustomString( commInterfaceHandle, @@ -642,6 +644,7 @@ static CellularCommInterfaceError_t prvCommIntfReceive( CellularCommInterfaceHan timeoutMilliseconds, pDataReceivedLength ); } + return commIfRet; } @@ -1434,7 +1437,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback( vo recvCount = 1; atCmdType = CELLULAR_AT_NO_RESULT; testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; - pCommIntfRecvCustomString = "OK\r\n"CELLULAR_AT_UNDEFINED_STRING_RESP"\r\n"; + pCommIntfRecvCustomString = "OK\r\n"CELLULAR_AT_UNDEFINED_STRING_RESP "\r\n"; /* Setup the callback to handle the undefined message. */ context.undefinedRespCallback = undefinedRespCallback; @@ -1445,7 +1448,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback( vo ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - threadReturn = true; /* Set pktio thread return flag. */ + threadReturn = true; /* Set pktio thread return flag. */ pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); @@ -1483,7 +1486,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_oka recvCount = 1; atCmdType = CELLULAR_AT_NO_RESULT; testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; - pCommIntfRecvCustomString = CELLULAR_AT_UNDEFINED_STRING_RESP"\r\nOK\r\n"; + pCommIntfRecvCustomString = CELLULAR_AT_UNDEFINED_STRING_RESP "\r\nOK\r\n"; /* Setup the callback to handle the undefined message. */ context.undefinedRespCallback = undefinedRespCallback; @@ -1494,7 +1497,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_oka ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - threadReturn = true; /* Set pktio thread return flag. */ + threadReturn = true; /* Set pktio thread return flag. */ pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); @@ -1544,7 +1547,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_fai ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - threadReturn = true; /* Set pktio thread return flag. */ + threadReturn = true; /* Set pktio thread return flag. */ pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); @@ -1588,7 +1591,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_no_callback_ ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - threadReturn = true; /* Set pktio thread return flag. */ + threadReturn = true; /* Set pktio thread return flag. */ pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); From b819f3698b3b31861de8093e231c121b07fa9522 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 18:33:30 +0800 Subject: [PATCH 05/24] Fix lexicon --- lexicon.txt | 3 +++ source/cellular_pktio.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lexicon.txt b/lexicon.txt index c101ded0..f7097e64 100644 --- a/lexicon.txt +++ b/lexicon.txt @@ -547,6 +547,8 @@ pktioreadthread pktiosendatcmd pktiosenddata pktioshutdown +pktrequestmutex +pktrespmutex pktrespqueue pktrxcallback pktstatus @@ -799,6 +801,7 @@ technoloyg tempvalue testindex testresultnumber +threadreturn threadroutine ticktype timeoutatcmddatarecvrequestwithcallback diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index af3cfbc2..d59944f4 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -707,7 +707,7 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, } else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_BUFFER ) { - /* This commaned expects raw data to be appended to buffer. Check data + /* This command expects raw data to be appended to buffer. Check data * prefix first then store the data if this command has data response. */ } else if( pkStatus == CELLULAR_PKT_STATUS_PENDING_DATA ) From d456d7562176ad98a3e2a366b273ff928bc1ced3 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 18:39:24 +0800 Subject: [PATCH 06/24] Fix unit test compile warning --- test/unit-test/cellular_pktio_utest.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index 9ae0a19f..f18d22bc 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -671,6 +671,8 @@ static CellularPktStatus_t prvUndefinedHandlePacket( CellularContext_t * pContex CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; const CellularATCommandResponse_t * pAtResp = NULL; + ( void ) pContext; + switch( atRespType ) { case AT_SOLICITED: From d59fa57809b05757eeb31db4f49813a396319f91 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 18:42:34 +0800 Subject: [PATCH 07/24] Update size --- docs/doxygen/include/size_table.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/doxygen/include/size_table.md b/docs/doxygen/include/size_table.md index 5e8032d3..03cf9593 100644 --- a/docs/doxygen/include/size_table.md +++ b/docs/doxygen/include/size_table.md @@ -24,27 +24,27 @@ cellular_common_api.c +
0.7K
0.6K
-
0.5K
cellular_common.c -
1.6K
+
1.7K
1.5K
cellular_pkthandler.c -
1.5K
-
1.4K
+
1.7K
+
1.6K
cellular_pktio.c -
1.8K
-
1.6K
+
1.9K
+
1.7K
Total estimates -
14.2K
-
12.9K
+
14.7K
+
13.3K
From 14d1e2c9616f2d3be35f6028bab807a486fc76bb Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Thu, 29 Sep 2022 22:34:40 +0800 Subject: [PATCH 08/24] Fix typo --- source/cellular_pktio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index d59944f4..560349ae 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -700,7 +700,7 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, } /* Clean the command type. Further response from cellular modem won't be - * regarded as AT_COLICITED response. */ + * regarded as AT_SOLICITED response. */ pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); From 7b94c6b8ef52c91a537d7c07fc2441dec37f15c0 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 15:59:35 +0800 Subject: [PATCH 09/24] Reduce the lock range --- source/cellular_pkthandler.c | 29 ++++- source/cellular_pktio.c | 78 +++++++----- source/include/cellular_config_defaults.h | 12 ++ .../private/cellular_common_internal.h | 1 + test/unit-test/cellular_pkthandler_utest.c | 113 +++++++++++++++++- test/unit-test/cellular_pktio_utest.c | 57 ++++++--- 6 files changed, 243 insertions(+), 47 deletions(-) diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index 564cd142..6e6424aa 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -514,6 +514,7 @@ CellularPktStatus_t _Cellular_HandlePacket( CellularContext_t * pContext, const void * pBuf ) { CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char * pLine = NULL; if( pContext != NULL ) { @@ -527,9 +528,35 @@ CellularPktStatus_t _Cellular_HandlePacket( CellularContext_t * pContext, pktStatus = _processUrcPacket( pContext, pBuf ); break; + case AT_UNDEFINED: + /* Handle AT_UNDEFINED message type. */ + pLine = ( char * ) pBuf; + LogInfo( ( "AT_UNDEFINED type message %s received.", atRespType, pLine ) ); + + /* undefined message received. Try to handle it with cellular module + * specific handler. */ + if( pContext->undefinedRespCallback == NULL ) + { + LogError( ( "No undefined callback for AT_UNDEFINED type message %s received.", + atRespType, pLine ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + else + { + pktStatus = pContext->undefinedRespCallback( pContext->pUndefinedRespCBContext, pLine ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "undefinedRespCallback returns error %d for AT_UNDEFINED type message %s received.", + pktStatus, atRespType, pLine ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + } + + break; + default: pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; - LogError( ( "_Cellular_HandlePacket Callback type (%d) error", atRespType ) ); break; } } diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 560349ae..7b02c29f 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -75,18 +75,17 @@ static void _saveATData( char * pLine, CellularATCommandResponse_t * pResp ); static CellularPktStatus_t _processIntermediateResponse( char * pLine, CellularATCommandResponse_t * pResp, - CellularATCommandType_t atType, - const char * pRespPrefix ); + CellularATCommandType_t atType ); static CellularATCommandResponse_t * _Cellular_AtResponseNew( void ); static void _Cellular_AtResponseFree( CellularATCommandResponse_t * pResp ); -static CellularPktStatus_t _Cellular_ProcessLine( const CellularContext_t * pContext, +static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext, char * pLine, CellularATCommandResponse_t * pResp, CellularATCommandType_t atType, const char * pRespPrefix ); static bool urcTokenWoPrefix( const CellularContext_t * pContext, const char * pLine ); -static _atRespType_t _getMsgType( const CellularContext_t * pContext, +static _atRespType_t _getMsgType( CellularContext_t * pContext, const char * pLine, const char * pRespPrefix ); static CellularCommInterfaceError_t _Cellular_PktRxCallBack( void * pUserData, @@ -182,13 +181,10 @@ static void _saveATData( char * pLine, static CellularPktStatus_t _processIntermediateResponse( char * pLine, CellularATCommandResponse_t * pResp, - CellularATCommandType_t atType, - const char * pRespPrefix ) + CellularATCommandType_t atType ) { CellularPktStatus_t pkStatus = CELLULAR_PKT_STATUS_PENDING_DATA; - ( void ) pRespPrefix; - switch( atType ) { case CELLULAR_AT_WO_PREFIX: @@ -302,7 +298,7 @@ static void _Cellular_AtResponseFree( CellularATCommandResponse_t * pResp ) /*-----------------------------------------------------------*/ -static CellularPktStatus_t _Cellular_ProcessLine( const CellularContext_t * pContext, +static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext, char * pLine, CellularATCommandResponse_t * pResp, CellularATCommandType_t atType, @@ -317,6 +313,9 @@ static CellularPktStatus_t _Cellular_ProcessLine( const CellularContext_t * pCon uint32_t tokenErrorTableSize = 0; uint32_t tokenExtraTableSize = 0; + /* Lock the response mutex when processing the input line. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); + if( ( pContext->tokenTable.pCellularSrcTokenErrorTable != NULL ) && ( pContext->tokenTable.pCellularSrcTokenSuccessTable != NULL ) ) { @@ -364,7 +363,7 @@ static CellularPktStatus_t _Cellular_ProcessLine( const CellularContext_t * pCon } else { - pkStatus = _processIntermediateResponse( pLine, pResp, atType, pRespPrefix ); + pkStatus = _processIntermediateResponse( pLine, pResp, atType ); } } } @@ -378,6 +377,8 @@ static CellularPktStatus_t _Cellular_ProcessLine( const CellularContext_t * pCon pkStatus ) ); } + PlatformMutex_Unlock( &pContext->PktRespMutex ); + return pkStatus; } @@ -405,7 +406,7 @@ static bool urcTokenWoPrefix( const CellularContext_t * pContext, /*-----------------------------------------------------------*/ -static _atRespType_t _getMsgType( const CellularContext_t * pContext, +static _atRespType_t _getMsgType( CellularContext_t * pContext, const char * pLine, const char * pRespPrefix ) { @@ -414,6 +415,9 @@ static _atRespType_t _getMsgType( const CellularContext_t * pContext, bool inputWithPrefix = false; bool inputWithSrcPrefix = false; + /* Lock the response mutex when deciding message type. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); + if( pContext->tokenTable.pCellularUrcTokenWoPrefixTable == NULL ) { atStatus = CELLULAR_AT_ERROR; @@ -460,6 +464,8 @@ static _atRespType_t _getMsgType( const CellularContext_t * pContext, } } + PlatformMutex_Unlock( &pContext->PktRespMutex ); + return atRespType; } @@ -576,9 +582,11 @@ static char * _Cellular_ReadLine( CellularContext_t * pContext, if( bufferEmptyLength > 0 ) { + PlatformMutex_Lock( &pContext->PktRespMutex ); ( void ) pContext->pCommIntf->recv( pContext->hPktioCommIntf, ( uint8_t * ) pRead, bufferEmptyLength, CELLULAR_COMM_IF_RECV_TIMEOUT_MS, &bytesRead ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); if( bytesRead > 0U ) { @@ -701,7 +709,10 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, /* Clean the command type. Further response from cellular modem won't be * regarded as AT_SOLICITED response. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; + pContext->pRespPrefix = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); } @@ -725,10 +736,13 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, if( pContext->recvdMsgType == AT_UNDEFINED ) { /* Pktio receives AT_UNDEFINED response from modem. This could be module specific - * response. Cellular module registers the callback function through _Cellular_RegisterUndefinedRespCallback - * to handle the response. */ - if( ( pContext->undefinedRespCallback == NULL ) || - ( pContext->undefinedRespCallback( pContext->pUndefinedRespCBContext, pLine ) != CELLULAR_PKT_STATUS_OK ) ) + * response. Call the packet handler callback to handle this message. */ + if( pContext->pPktioHandlepktCB != NULL ) + { + pkStatus = pContext->pPktioHandlepktCB( pContext, AT_UNDEFINED, pLine ); + } + + if( pkStatus != CELLULAR_PKT_STATUS_OK ) { LogError( ( "recvdMsgType is AT_UNDEFINED for Message: %s, cmd %s", pLine, @@ -797,6 +811,19 @@ static bool _preprocessLine( CellularContext_t * pContext, bool keepProcess = true; CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATCommandDataPrefixCallback_t pktDataPrefixCB = NULL; + void * pDataPrefixCBContext = NULL; + CellularATCommandDataSendPrefixCallback_t pktDataSendPrefixCB = NULL; + void * pDataSendPrefixCBContext = NULL; + + /* Acquire the repsonse lock to keep consistency. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); + pktDataPrefixCB = pContext->pktDataPrefixCB; + pDataPrefixCBContext = pContext->pDataPrefixCBContext; + pktDataSendPrefixCB = pContext->pktDataSendPrefixCB; + pDataSendPrefixCBContext = pContext->pDataSendPrefixCBContext; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + /* The line only has change line. */ if( *pBytesRead <= 0U ) { @@ -806,13 +833,13 @@ static bool _preprocessLine( CellularContext_t * pContext, } else { - if( pContext->pktDataSendPrefixCB != NULL ) + if( pktDataSendPrefixCB != NULL ) { /* Check if the AT command response is the data send prefix. * Data send prefix is an SRC success token for data send AT commmand. * It is used to indicate modem can receive data now. */ /* This function may fix the data stream if the data send prefix is not a line. */ - pktStatus = pContext->pktDataSendPrefixCB( pContext->pDataSendPrefixCBContext, pTempLine, pBytesRead ); + pktStatus = pktDataSendPrefixCB( pDataSendPrefixCBContext, pTempLine, pBytesRead ); if( pktStatus != CELLULAR_PKT_STATUS_OK ) { @@ -820,7 +847,7 @@ static bool _preprocessLine( CellularContext_t * pContext, keepProcess = false; } } - else if( pContext->pktDataPrefixCB != NULL ) + else if( pktDataPrefixCB != NULL ) { /* Check if the AT command response is the data receive prefix. * Data receive prefix is an SRC success token for data receive AT commnad. @@ -829,9 +856,9 @@ static bool _preprocessLine( CellularContext_t * pContext, /* This function may fix the data stream if the AT response and data * received are in the same line. */ - pktStatus = pContext->pktDataPrefixCB( pContext->pDataPrefixCBContext, - pTempLine, *pBytesRead, - ppStartOfData, &pContext->dataLength ); + pktStatus = pktDataPrefixCB( pDataPrefixCBContext, + pTempLine, *pBytesRead, + ppStartOfData, &pContext->dataLength ); if( pktStatus == CELLULAR_PKT_STATUS_OK ) { @@ -949,7 +976,7 @@ static void _handleAllReceived( CellularContext_t * pContext, bytesRead = bytesRead - 1U; } - /* Preprocess line. */ + /* Preprocess line. Unlock the response mutex since callback function is called. */ keepProcess = _preprocessLine( pContext, pTempLine, &bytesRead, &pStartOfData ); if( keepProcess == true ) @@ -1064,11 +1091,7 @@ static void _pktioReadThread( void * pUserData ) /* Keep Reading until there is no more bytes in comm interface. */ do { - /* When pktio thread is parsing the response from modem, pktio - * thread holds the PktRespMutex until no RX data from comm interface. */ - PlatformMutex_Lock( &pContext->PktRespMutex ); bytesRead = _handleRxDataEvent( pContext, &pAtResp ); - PlatformMutex_Unlock( &pContext->PktRespMutex ); } while( ( bytesRead != 0U ) ); } else @@ -1228,7 +1251,8 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, else { PlatformMutex_Lock( &pContext->PktRespMutex ); - pContext->pRespPrefix = pAtRspPrefix; + strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); + pContext->pRespPrefix = pContext->pktRespPrefixBuf; pContext->PktioAtCmdType = atType; newCmdLen = cmdLen; newCmdLen += 1U; /* Include space for \r. */ diff --git a/source/include/cellular_config_defaults.h b/source/include/cellular_config_defaults.h index f8e7303a..10b08705 100644 --- a/source/include/cellular_config_defaults.h +++ b/source/include/cellular_config_defaults.h @@ -387,6 +387,18 @@ #define CELLULAR_AT_COMMAND_RAW_TIMEOUT_MS ( 5000U ) #endif +/** + * @brief Cellular AT command response prefix string length.
+ * + * The maximum length of AT command response prefix string. + * + * Possible values:`Any positive integer`
+ * Default value (if undefined): 32 + */ +#ifndef CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH + #define CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ( 32U ) +#endif + /** * @brief Macro that is called in the cellular library for logging "Error" level * messages. diff --git a/source/include/private/cellular_common_internal.h b/source/include/private/cellular_common_internal.h index 4fcd9cce..4d8011e7 100644 --- a/source/include/private/cellular_common_internal.h +++ b/source/include/private/cellular_common_internal.h @@ -135,6 +135,7 @@ struct CellularContext char pktioReadBuf[ PKTIO_READ_BUFFER_SIZE + 1 ]; char * pPktioReadPtr; const char * pRespPrefix; + char pktRespPrefixBuf[ CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ]; CellularATCommandType_t PktioAtCmdType; _atRespType_t recvdMsgType; CellularUndefinedRespCallback_t undefinedRespCallback; diff --git a/test/unit-test/cellular_pkthandler_utest.c b/test/unit-test/cellular_pkthandler_utest.c index 49ca2579..292208d7 100644 --- a/test/unit-test/cellular_pkthandler_utest.c +++ b/test/unit-test/cellular_pkthandler_utest.c @@ -56,12 +56,16 @@ #define CELLULAR_SAMPLE_PREFIX_STRING_LARGE_INPUT "+CPIN:Story for Littel Red Riding Hood: Once upon a time there was a dear little girl who was loved by every one who looked at her, but most of all by her grandmother, and there was nothing that she would not have given to the child. Once she gave her a little cap of red velvet, which suited her so well that she would never wear anything else. So she was always called Little Red Riding Hood." #define CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ( 32U ) +#define CELLULAR_AT_UNDEFINED_STRING_RESP "undefined_string" + static uint16_t queueData = CELLULAR_PKT_STATUS_BAD_PARAM; static int32_t queueReturnFail = 0; static int32_t queueCreateFail = 0; static int32_t pktRespCBReturn = 0; static bool passCompareString = false; static char * pCompareString = NULL; +static int32_t undefinedCallbackContext = 0; +static char undefinedCallbackStr[] = CELLULAR_AT_UNDEFINED_STRING_RESP; void cellularAtParseTokenHandler( CellularContext_t * pContext, char * pInputStr ); @@ -393,6 +397,30 @@ static char * getStringAfterColon( char * pInputStr ) return ret ? ret + 1 : pInputStr + strlen( pInputStr ); } +static CellularPktStatus_t undefinedRespCallback( void * pCallbackContext, + const char * pLine ) +{ + CellularPktStatus_t undefineReturnStatus = CELLULAR_PKT_STATUS_OK; + + /* Verify pCallbackContext. */ + TEST_ASSERT_EQUAL_PTR( &undefinedCallbackContext, pCallbackContext ); + + /* Verify pLine. */ + if( strcmp( CELLULAR_AT_UNDEFINED_STRING_RESP, pLine ) == 0 ) + { + *( ( int32_t * ) pCallbackContext ) = 1; + undefineReturnStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + *( ( int32_t * ) pCallbackContext ) = 0; + undefineReturnStatus = CELLULAR_PKT_STATUS_FAILURE; + } + + return undefineReturnStatus; +} + + /* ========================================================================== */ /** @@ -651,7 +679,7 @@ void test__Cellular_HandlePacket_AT_UNSOLICITED_Input_String_Less_Than_Urc_Token } /** - * @brief Test that null buffer AT_UNDEFINED case for _Cellular_HandlePacket. + * @brief Test that null buffer invalid message type case for _Cellular_HandlePacket. */ void test__Cellular_HandlePacket_Wrong_RespType( void ) { @@ -660,11 +688,92 @@ void test__Cellular_HandlePacket_Wrong_RespType( void ) memset( &context, 0, sizeof( CellularContext_t ) ); - pktStatus = _Cellular_HandlePacket( &context, AT_UNDEFINED, NULL ); + /* Send invalid message type. */ + pktStatus = _Cellular_HandlePacket( &context, AT_UNDEFINED + 1, NULL ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_BAD_PARAM, pktStatus ); } +/** + * @brief Test _Cellular_HandlePacket function with AT_UNDEFINED message type. + * + * AT_UNDEFINED message type is received. A callback function handles the message + * without problem. CELLULAR_PKT_STATUS_OK should be returned. + */ +void test__Cellular_HandlePacket_AT_UNDEFINED_with_undefined_callback_okay( void ) +{ + CellularContext_t context; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char undefinedCallbackStr[] = CELLULAR_AT_UNDEFINED_STRING_RESP; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Set undefined response callback. */ + undefinedCallbackContext = 0; + context.undefinedRespCallback = undefinedRespCallback; + context.pUndefinedRespCBContext = &undefinedCallbackContext; + + /* Send AT_UNDEFINED message. */ + pktStatus = _Cellular_HandlePacket( &context, AT_UNDEFINED, undefinedCallbackStr ); + + /* CELLULAR_PKT_STATUS_OK should be returned, since it is handled. */ + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Verify undefinedRespCallback is called and the expected string is received. */ + TEST_ASSERT_EQUAL_INT32( 1, undefinedCallbackContext ); +} + +/** + * @brief Test _Cellular_HandlePacket function with AT_UNDEFINED message type. + * + * AT_UNDEFINED message type is received. A callback function handles the message + * but fail to recognized the undefined response. CELLULAR_PKT_STATUS_INVALID_DATA + * should be returned. + */ +void test__Cellular_HandlePacket_AT_UNDEFINED_with_undefined_callback_fail( void ) +{ + CellularContext_t context; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char undefinedCallbackStr[] = "RandomString"; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Set undefined response callback. */ + undefinedCallbackContext = 1; + context.undefinedRespCallback = undefinedRespCallback; + context.pUndefinedRespCBContext = &undefinedCallbackContext; + + /* Send AT_UNDEFINED message. */ + pktStatus = _Cellular_HandlePacket( &context, AT_UNDEFINED, undefinedCallbackStr ); + + /* CELLULAR_PKT_STATUS_INVALID_DATA should be returned. */ + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_INVALID_DATA, pktStatus ); + + /* Verify undefinedRespCallback is called and the expected string is received. */ + TEST_ASSERT_EQUAL_INT32( 0, undefinedCallbackContext ); +} + +/** + * @brief Test _Cellular_HandlePacket function with AT_UNDEFINED message type. + * + * AT_UNDEFINED message type is received. No callback function is registered to handle + * the undefined message. CELLULAR_PKT_STATUS_INVALID_DATA should be returned. + */ +void test__Cellular_HandlePacket_AT_UNDEFINED_without_undefined_callback( void ) +{ + CellularContext_t context; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char undefinedCallbackStr[] = CELLULAR_AT_UNDEFINED_STRING_RESP; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Send AT_UNDEFINED message. */ + pktStatus = _Cellular_HandlePacket( &context, AT_UNDEFINED, undefinedCallbackStr ); + + /* CELLULAR_PKT_STATUS_INVALID_DATA should be returned. */ + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_INVALID_DATA, pktStatus ); +} + /** * @brief Test that URC with colon for _Cellular_HandlePacket when token is not in the token table. */ diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index f18d22bc..2c9f04f2 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -609,7 +609,7 @@ static CellularCommInterfaceError_t prvCommIntfReceiveCustomString( CellularComm { recvCount--; - strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, strlen( pCommIntfRecvCustomString ) ); + strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, bufferLength ); *pDataReceivedLength = strlen( pCommIntfRecvCustomString ); } else @@ -670,22 +670,45 @@ static CellularPktStatus_t prvUndefinedHandlePacket( CellularContext_t * pContex { CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; const CellularATCommandResponse_t * pAtResp = NULL; + char * pLine = NULL; - ( void ) pContext; - - switch( atRespType ) + if( pContext != NULL ) { - case AT_SOLICITED: - pAtResp = ( const CellularATCommandResponse_t * ) pBuf; - atCmdStatusUndefindTest = pAtResp->status; - break; + switch( atRespType ) + { + case AT_SOLICITED: + pAtResp = ( const CellularATCommandResponse_t * ) pBuf; + atCmdStatusUndefindTest = pAtResp->status; + break; + + case AT_UNSOLICITED: + break; + + default: + pLine = ( char * ) pBuf; - case AT_UNSOLICITED: - break; + /* undefined message received. Try to handle it with cellular module + * specific handler. */ + if( pContext->undefinedRespCallback == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + else + { + pktStatus = pContext->undefinedRespCallback( pContext->pUndefinedRespCBContext, pLine ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + } - default: - pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; - break; + break; + } + } + else + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; } return pktStatus; @@ -1370,7 +1393,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_AT_UNDEFINED_callback_okay( v ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); /* Verify undefinedRespCallback is called and the expected string is received. */ @@ -1405,7 +1428,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_AT_UNDEFINED_callback_fail( v ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); /* Check that CELLULAR_PKT_STATUS_OK is returned. */ - pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); /* Verify undefinedRespCallback is called and the expected string is not received. */ @@ -1550,7 +1573,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_callback_fai /* Check that CELLULAR_PKT_STATUS_OK is returned. */ threadReturn = true; /* Set pktio thread return flag. */ - pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); /* Verify the command should has error. */ @@ -1594,7 +1617,7 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_SRC_AT_undefined_no_callback_ /* Check that CELLULAR_PKT_STATUS_OK is returned. */ threadReturn = true; /* Set pktio thread return flag. */ - pktStatus = _Cellular_PktioInit( &context, PktioHandlePacketCallback_t ); + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); /* Verify the command should has error. */ From 6cbb2d2831e104230bf3d3e5d929581c5496fc22 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 16:15:30 +0800 Subject: [PATCH 10/24] Update for CI --- source/cellular_pkthandler.c | 58 ++++++++++++++++++++++-------------- source/cellular_pktio.c | 3 ++ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index 6e6424aa..f7173ac6 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -81,6 +81,8 @@ static void _Cellular_ProcessGenericUrc( const CellularContext_t * pContext, static CellularPktStatus_t _atParseGetHandler( CellularContext_t * pContext, const char * pTokenPtr, char * pSavePtr ); +static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext, + const char *pLine ); /*-----------------------------------------------------------*/ @@ -493,6 +495,38 @@ static CellularPktStatus_t _atParseGetHandler( CellularContext_t * pContext, /*-----------------------------------------------------------*/ +/* + * @brief Handle AT_UNDEFINED message type. + */ +static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext, + const char *pLine ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + /* undefined message received. Try to handle it with cellular module + * specific handler. */ + if( pContext->undefinedRespCallback == NULL ) + { + LogError( ( "No undefined callback for AT_UNDEFINED type message %s received.", + atRespType, pLine ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + else + { + pktStatus = pContext->undefinedRespCallback( pContext->pUndefinedRespCBContext, pLine ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "undefinedRespCallback returns error %d for AT_UNDEFINED type message %s received.", + pktStatus, atRespType, pLine ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + } + return pktStatus; +} + +/*-----------------------------------------------------------*/ + void _Cellular_PktHandlerCleanup( CellularContext_t * pContext ) { if( ( pContext != NULL ) && ( pContext->pktRespQueue != NULL ) ) @@ -514,7 +548,6 @@ CellularPktStatus_t _Cellular_HandlePacket( CellularContext_t * pContext, const void * pBuf ) { CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; - char * pLine = NULL; if( pContext != NULL ) { @@ -529,29 +562,8 @@ CellularPktStatus_t _Cellular_HandlePacket( CellularContext_t * pContext, break; case AT_UNDEFINED: - /* Handle AT_UNDEFINED message type. */ - pLine = ( char * ) pBuf; LogInfo( ( "AT_UNDEFINED type message %s received.", atRespType, pLine ) ); - - /* undefined message received. Try to handle it with cellular module - * specific handler. */ - if( pContext->undefinedRespCallback == NULL ) - { - LogError( ( "No undefined callback for AT_UNDEFINED type message %s received.", - atRespType, pLine ) ); - pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; - } - else - { - pktStatus = pContext->undefinedRespCallback( pContext->pUndefinedRespCBContext, pLine ); - - if( pktStatus != CELLULAR_PKT_STATUS_OK ) - { - LogError( ( "undefinedRespCallback returns error %d for AT_UNDEFINED type message %s received.", - pktStatus, atRespType, pLine ) ); - pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; - } - } + pktStatus = _handleUndefinedMessage( pContext, pBuf ); break; diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 7b02c29f..a76684e6 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -313,6 +313,9 @@ static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext, uint32_t tokenErrorTableSize = 0; uint32_t tokenExtraTableSize = 0; + /* This variable is used in wanring message. */ + ( void ) pRespPrefix; + /* Lock the response mutex when processing the input line. */ PlatformMutex_Lock( &pContext->PktRespMutex ); From 4b24847261fd803be63f127d2f42ce7385ea9633 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 16:19:34 +0800 Subject: [PATCH 11/24] Fix memory and CI again --- docs/doxygen/include/size_table.md | 6 +++--- source/cellular_pkthandler.c | 5 +++-- source/cellular_pktio.c | 4 ++-- test/unit-test/cellular_pkthandler_utest.c | 1 - 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/doxygen/include/size_table.md b/docs/doxygen/include/size_table.md index 03cf9593..7a4aecf9 100644 --- a/docs/doxygen/include/size_table.md +++ b/docs/doxygen/include/size_table.md @@ -39,12 +39,12 @@ cellular_pktio.c +
2.0K
1.9K
-
1.7K
Total estimates -
14.7K
-
13.3K
+
14.8K
+
13.5K
diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index f7173ac6..b7d4a6ed 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -82,7 +82,7 @@ static CellularPktStatus_t _atParseGetHandler( CellularContext_t * pContext, const char * pTokenPtr, char * pSavePtr ); static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext, - const char *pLine ); + const char * pLine ); /*-----------------------------------------------------------*/ @@ -499,7 +499,7 @@ static CellularPktStatus_t _atParseGetHandler( CellularContext_t * pContext, * @brief Handle AT_UNDEFINED message type. */ static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext, - const char *pLine ) + const char * pLine ) { CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; @@ -522,6 +522,7 @@ static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; } } + return pktStatus; } diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index a76684e6..99f7ab3a 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -313,7 +313,7 @@ static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext, uint32_t tokenErrorTableSize = 0; uint32_t tokenExtraTableSize = 0; - /* This variable is used in wanring message. */ + /* This variable is used in warning message. */ ( void ) pRespPrefix; /* Lock the response mutex when processing the input line. */ @@ -819,7 +819,7 @@ static bool _preprocessLine( CellularContext_t * pContext, CellularATCommandDataSendPrefixCallback_t pktDataSendPrefixCB = NULL; void * pDataSendPrefixCBContext = NULL; - /* Acquire the repsonse lock to keep consistency. */ + /* Acquire the response lock to keep consistency. */ PlatformMutex_Lock( &pContext->PktRespMutex ); pktDataPrefixCB = pContext->pktDataPrefixCB; pDataPrefixCBContext = pContext->pDataPrefixCBContext; diff --git a/test/unit-test/cellular_pkthandler_utest.c b/test/unit-test/cellular_pkthandler_utest.c index 292208d7..bc16e8b6 100644 --- a/test/unit-test/cellular_pkthandler_utest.c +++ b/test/unit-test/cellular_pkthandler_utest.c @@ -65,7 +65,6 @@ static int32_t pktRespCBReturn = 0; static bool passCompareString = false; static char * pCompareString = NULL; static int32_t undefinedCallbackContext = 0; -static char undefinedCallbackStr[] = CELLULAR_AT_UNDEFINED_STRING_RESP; void cellularAtParseTokenHandler( CellularContext_t * pContext, char * pInputStr ); From 0c1f0a2ef6c833edb9207dd7ed72e598351f4052 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 16:55:15 +0800 Subject: [PATCH 12/24] Fix CI --- source/cellular_pkthandler.c | 2 -- source/cellular_pktio.c | 6 +++++- test/unit-test/cellular_pktio_utest.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index b7d4a6ed..7b3173b5 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -308,9 +308,7 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex /* End pattern for specific modem. */ if( ( pktStatus == CELLULAR_PKT_STATUS_OK ) && ( dataReq.pEndPattern != NULL ) ) { - PlatformMutex_Lock( &pContext->PktRespMutex ); sendEndPatternLen = _Cellular_PktioSendData( pContext, dataReq.pEndPattern, dataReq.endPatternLen ); - PlatformMutex_Unlock( &pContext->PktRespMutex ); if( sendEndPatternLen != dataReq.endPatternLen ) { diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 99f7ab3a..2f6a9a36 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -735,6 +735,10 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, pContext->recvdMsgType = AT_UNDEFINED; } } + else + { + /* This is AT_UNDEFINED when not sending the AT command. */ + } if( pContext->recvdMsgType == AT_UNDEFINED ) { @@ -1254,7 +1258,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, else { PlatformMutex_Lock( &pContext->PktRespMutex ); - strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); + ( void ) strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); pContext->pRespPrefix = pContext->pktRespPrefixBuf; pContext->PktioAtCmdType = atType; newCmdLen = cmdLen; diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index 2c9f04f2..dd68d84c 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -609,7 +609,7 @@ static CellularCommInterfaceError_t prvCommIntfReceiveCustomString( CellularComm { recvCount--; - strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, bufferLength ); + strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, strlen( pCommIntfRecvCustomString ) ); *pDataReceivedLength = strlen( pCommIntfRecvCustomString ); } else From d6672700d1a4de80b313146deaa8499dc78e555c Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 18:03:39 +0800 Subject: [PATCH 13/24] Add more test case for coverage --- test/unit-test/cellular_pktio_utest.c | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index dd68d84c..4f8fc62b 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -609,7 +609,7 @@ static CellularCommInterfaceError_t prvCommIntfReceiveCustomString( CellularComm { recvCount--; - strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, strlen( pCommIntfRecvCustomString ) ); + ( void ) strncpy( ( char * ) pBuffer, pCommIntfRecvCustomString, bufferLength ); *pDataReceivedLength = strlen( pCommIntfRecvCustomString ); } else @@ -1941,6 +1941,49 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_URC_TOKEN_STRING_RESP( void ) TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); } +/** + * @brief Test RX data event _handle_data function. + * + * pktio read thread is in data mode and expects more data to be received. Modem returns + * not enough data to pktio. These data will be stored as partial data. + */ +void test__Cellular_PktioInit_Thread_Rx_Data_Event_handle_data( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + + memset( &context, 0, sizeof( CellularContext_t ) ); + + /* Assign the comm interface to pContext. */ + context.pCommIntf = pCommIntf; + context.pPktioShutdownCB = _shutdownCallback; + + /* Test the rx_data event with CELLULAR_AT_NO_RESULT type AT command. */ + pktioEvtMask = PKTIO_EVT_MASK_RX_DATA; + recvCount = 1; + atCmdType = CELLULAR_AT_NO_RESULT; + testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING; + pCommIntfRecvCustomString = "12345"; + + /* Copy the token table. */ + ( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) ); + + /* Pktio read thread enter data mode by setting dataLength. */ + context.dataLength = 10; + + /* Check that CELLULAR_PKT_STATUS_OK is returned. */ + threadReturn = true; /* Set pktio thread return flag. */ + pktStatus = _Cellular_PktioInit( &context, prvUndefinedHandlePacket ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); + + /* Store 5 bytes in the partial data. */ + TEST_ASSERT_EQUAL_UINT( context.partialDataRcvdLen, 5 ); + + /* The data is incomplete. pPktioReadPtr should be the same pktioReadBuf. */ + TEST_ASSERT_EQUAL( context.pPktioReadPtr, context.pktioReadBuf ); +} + /** * @brief Test pkio aborted event case for _Cellular_PktioInit to return CELLULAR_PKT_STATUS_FAILURE. */ From 1c627e706844ce72591c9554c2a67efe11dba278 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 18:19:05 +0800 Subject: [PATCH 14/24] Add response prefix test case --- source/cellular_pkthandler.c | 8 +++++--- source/cellular_pktio.c | 11 +++++++++-- test/unit-test/cellular_pktio_utest.c | 28 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index 7b3173b5..31fa570e 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -501,12 +501,14 @@ static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext { CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + LogInfo( ( "AT_UNDEFINED message received %s\r\n", pLine ) ); + /* undefined message received. Try to handle it with cellular module * specific handler. */ if( pContext->undefinedRespCallback == NULL ) { LogError( ( "No undefined callback for AT_UNDEFINED type message %s received.", - atRespType, pLine ) ); + pLine ) ); pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; } else @@ -516,7 +518,7 @@ static CellularPktStatus_t _handleUndefinedMessage( CellularContext_t * pContext if( pktStatus != CELLULAR_PKT_STATUS_OK ) { LogError( ( "undefinedRespCallback returns error %d for AT_UNDEFINED type message %s received.", - pktStatus, atRespType, pLine ) ); + pktStatus, pLine ) ); pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; } } @@ -561,13 +563,13 @@ CellularPktStatus_t _Cellular_HandlePacket( CellularContext_t * pContext, break; case AT_UNDEFINED: - LogInfo( ( "AT_UNDEFINED type message %s received.", atRespType, pLine ) ); pktStatus = _handleUndefinedMessage( pContext, pBuf ); break; default: pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + LogError( ( "_Cellular_HandlePacket Callback type (%d) error", atRespType ) ); break; } } diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 2f6a9a36..6b0f8284 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -1258,8 +1258,15 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, else { PlatformMutex_Lock( &pContext->PktRespMutex ); - ( void ) strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); - pContext->pRespPrefix = pContext->pktRespPrefixBuf; + if( pAtRspPrefix != NULL ) + { + ( void ) strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); + pContext->pRespPrefix = pContext->pktRespPrefixBuf; + } + else + { + pContext->pRespPrefix = NULL; + } pContext->PktioAtCmdType = atType; newCmdLen = cmdLen; newCmdLen += 1U; /* Include space for \r. */ diff --git a/test/unit-test/cellular_pktio_utest.c b/test/unit-test/cellular_pktio_utest.c index 4f8fc62b..fabf0a86 100644 --- a/test/unit-test/cellular_pktio_utest.c +++ b/test/unit-test/cellular_pktio_utest.c @@ -2212,6 +2212,34 @@ void test__Cellular_PktioSendAtCmd_Happy_Path( void ) TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); } +/** + * @brief Test that happy path for _Cellular_PktioSendAtCmd. + */ +void test__Cellular_PktioSendAtCmd_Happy_Path_No_Prefix( void ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularContext_t context; + CellularCommInterface_t * pCommIntf = &CellularCommInterface; + struct _cellularCommContext commInterfaceHandle = { 0 }; + CellularAtReq_t atReqSetRatPriority = + { + "ATE0", + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + memset( &context, 0, sizeof( CellularContext_t ) ); + context.pCommIntf = pCommIntf; + context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle; + pktStatus = _Cellular_PktioSendAtCmd( &context, atReqSetRatPriority.pAtCmd, + atReqSetRatPriority.atCmdType, + atReqSetRatPriority.pAtRspPrefix ); + TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus ); +} + /** * @brief Test that any NULL parameter for _Cellular_PktioSendData. */ From 4fd030d1a32dea5426049a5cad5b154a54c0d446 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Fri, 30 Sep 2022 18:21:26 +0800 Subject: [PATCH 15/24] Fix formatting --- source/cellular_pktio.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 6b0f8284..bacfb0c4 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -1258,6 +1258,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, else { PlatformMutex_Lock( &pContext->PktRespMutex ); + if( pAtRspPrefix != NULL ) { ( void ) strncpy( pContext->pktRespPrefixBuf, pAtRspPrefix, CELLULAR_CONFIG_MAX_PREFIX_STRING_LENGTH ); @@ -1267,6 +1268,7 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, { pContext->pRespPrefix = NULL; } + pContext->PktioAtCmdType = atType; newCmdLen = cmdLen; newCmdLen += 1U; /* Include space for \r. */ From 93c9fad912e311b2169ca900900f5ef7892feea0 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 4 Oct 2022 10:41:21 +0800 Subject: [PATCH 16/24] Remove unnecessary comment --- source/cellular_pktio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index bacfb0c4..9ac83691 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -983,7 +983,7 @@ static void _handleAllReceived( CellularContext_t * pContext, bytesRead = bytesRead - 1U; } - /* Preprocess line. Unlock the response mutex since callback function is called. */ + /* Preprocess line. */ keepProcess = _preprocessLine( pContext, pTempLine, &bytesRead, &pStartOfData ); if( keepProcess == true ) From f9343a354db33fbb2dbd2cc2ac90c88a4e54c741 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 4 Oct 2022 14:47:30 +0800 Subject: [PATCH 17/24] Update HL7802 for ATE0 * ATE0 is the first AT command sent may still contains echo. Change the comand type to without response --- modules/hl7802/cellular_hl7802.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/hl7802/cellular_hl7802.c b/modules/hl7802/cellular_hl7802.c index f23239c3..f4d7edd8 100644 --- a/modules/hl7802/cellular_hl7802.c +++ b/modules/hl7802/cellular_hl7802.c @@ -335,6 +335,15 @@ CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) { CellularError_t cellularStatus = CELLULAR_SUCCESS; CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetWoPrefix = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; CellularAtReq_t atReqGetNoResult = { NULL, @@ -351,8 +360,9 @@ CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) if( pContext != NULL ) { /* Disable echo. */ - atReqGetNoResult.pAtCmd = "ATE0"; - cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult, + atReqGetWoPrefix.pAtCmd = "ATE0"; + atReqGetWoPrefix.atCmdType = CELLULAR_AT_WO_PREFIX; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetWoPrefix, CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); if( cellularStatus == CELLULAR_SUCCESS ) From 9049b6ebb947f57b2a11548cc96a9dee5d7e73c0 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 4 Oct 2022 16:33:38 +0800 Subject: [PATCH 18/24] Update HL7802 ATE0 type --- modules/hl7802/cellular_hl7802.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/hl7802/cellular_hl7802.c b/modules/hl7802/cellular_hl7802.c index f4d7edd8..00c4c2ae 100644 --- a/modules/hl7802/cellular_hl7802.c +++ b/modules/hl7802/cellular_hl7802.c @@ -338,7 +338,7 @@ CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) CellularAtReq_t atReqGetWoPrefix = { NULL, - CELLULAR_AT_NO_RESULT, + CELLULAR_AT_WO_PREFIX, NULL, NULL, NULL, @@ -361,7 +361,6 @@ CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) { /* Disable echo. */ atReqGetWoPrefix.pAtCmd = "ATE0"; - atReqGetWoPrefix.atCmdType = CELLULAR_AT_WO_PREFIX; cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetWoPrefix, CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); From 647e5c297905551045bb9976a85be2835bdc467d Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 4 Oct 2022 17:19:29 +0800 Subject: [PATCH 19/24] Update ATE0 --- modules/hl7802/cellular_hl7802.c | 4 ++-- modules/sara_r4/cellular_r4.c | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/hl7802/cellular_hl7802.c b/modules/hl7802/cellular_hl7802.c index 00c4c2ae..99cd9873 100644 --- a/modules/hl7802/cellular_hl7802.c +++ b/modules/hl7802/cellular_hl7802.c @@ -473,8 +473,8 @@ CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) if( cellularStatus == CELLULAR_SUCCESS ) { Platform_Delay( CELLULAR_HL7802_RESET_DELAY_MS ); - atReqGetNoResult.pAtCmd = "ATE0"; - cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult, + atReqGetWoPrefix.pAtCmd = "ATE0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetWoPrefix, CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); } } diff --git a/modules/sara_r4/cellular_r4.c b/modules/sara_r4/cellular_r4.c index 8efbc5b9..07b4e444 100644 --- a/modules/sara_r4/cellular_r4.c +++ b/modules/sara_r4/cellular_r4.c @@ -262,7 +262,15 @@ CellularError_t rebootCellularModem( CellularContext_t * pContext, CellularError_t cellularStatus = CELLULAR_SUCCESS; CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; uint32_t count = 0; - + CellularAtReq_t atReqGetWoPrefix = + { + NULL, + CELLULAR_AT_WO_PREFIX, + NULL, + NULL, + NULL, + 0 + }; CellularAtReq_t atReqGetNoResult = { "AT+CFUN=15", @@ -283,9 +291,9 @@ CellularError_t rebootCellularModem( CellularContext_t * pContext, { LogInfo( ( "rebootCellularModem: Use ATE0 command to test modem status." ) ); - atReqGetNoResult.pAtCmd = "ATE0"; + atReqGetWoPrefix.pAtCmd = "ATE0"; - pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, ENBABLE_MODULE_UE_REBOOT_POLL_TIME ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetWoPrefix, ENBABLE_MODULE_UE_REBOOT_POLL_TIME ); cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); if( cellularStatus == CELLULAR_SUCCESS ) From 56c75f9f7ecd5696e39eac61ce816c90a456ab25 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Tue, 4 Oct 2022 18:13:57 +0800 Subject: [PATCH 20/24] Remove comm interface resp lock --- source/cellular_pktio.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index 9ac83691..d58a557d 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -585,11 +585,9 @@ static char * _Cellular_ReadLine( CellularContext_t * pContext, if( bufferEmptyLength > 0 ) { - PlatformMutex_Lock( &pContext->PktRespMutex ); ( void ) pContext->pCommIntf->recv( pContext->hPktioCommIntf, ( uint8_t * ) pRead, bufferEmptyLength, CELLULAR_COMM_IF_RECV_TIMEOUT_MS, &bytesRead ); - PlatformMutex_Unlock( &pContext->PktRespMutex ); if( bytesRead > 0U ) { @@ -1276,10 +1274,11 @@ CellularPktStatus_t _Cellular_PktioSendAtCmd( CellularContext_t * pContext, ( void ) strncpy( pContext->pktioSendBuf, pAtCmd, cmdLen ); pContext->pktioSendBuf[ cmdLen ] = '\r'; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + ( void ) pContext->pCommIntf->send( pContext->hPktioCommIntf, ( const uint8_t * ) &pContext->pktioSendBuf, newCmdLen, CELLULAR_COMM_IF_SEND_TIMEOUT_MS, &sentLen ); - PlatformMutex_Unlock( &pContext->PktRespMutex ); } } @@ -1309,10 +1308,8 @@ uint32_t _Cellular_PktioSendData( CellularContext_t * pContext, } else { - PlatformMutex_Lock( &pContext->PktRespMutex ); ( void ) pContext->pCommIntf->send( pContext->hPktioCommIntf, pData, dataLen, CELLULAR_COMM_IF_SEND_TIMEOUT_MS, &sentLen ); - PlatformMutex_Unlock( &pContext->PktRespMutex ); } LogDebug( ( "PktioSendData sent %d bytes", sentLen ) ); From aa3956ec3bc3ffece0f42ab45c71134afff8304c Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 5 Oct 2022 07:13:51 +0800 Subject: [PATCH 21/24] Fix typo --- source/cellular_pkthandler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/cellular_pkthandler.c b/source/cellular_pkthandler.c index 31fa570e..13e99427 100644 --- a/source/cellular_pkthandler.c +++ b/source/cellular_pkthandler.c @@ -770,7 +770,7 @@ CellularPktStatus_t _Cellular_TimeoutAtcmdDataSendSuccessToken( CellularContext_ PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->tokenTable.pCellularSrcExtraTokenSuccessTable = pCellularSrcTokenSuccessTable; pContext->tokenTable.cellularSrcExtraTokenSuccessTableSize = cellularSrcTokenSuccessTableSize; - PlatformMutex_Lock( &pContext->PktRespMutex ); + PlatformMutex_Unlock( &pContext->PktRespMutex ); pktStatus = _Cellular_AtcmdRequestTimeoutWithCallbackRaw( pContext, atReq, atTimeoutMS ); From 35591ad6754f755e9d035fb75de5059197edbd88 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 5 Oct 2022 09:04:34 +0800 Subject: [PATCH 22/24] Update size --- docs/doxygen/include/size_table.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/doxygen/include/size_table.md b/docs/doxygen/include/size_table.md index 7a4aecf9..18d9c478 100644 --- a/docs/doxygen/include/size_table.md +++ b/docs/doxygen/include/size_table.md @@ -40,11 +40,11 @@ cellular_pktio.c
2.0K
-
1.9K
+
1.8K
Total estimates
14.8K
-
13.5K
+
13.4K
From 2fb0fb76d8c101de5fb7215167aba9af852f0e85 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 5 Oct 2022 09:59:44 +0800 Subject: [PATCH 23/24] Fix comment --- source/cellular_pktio.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index d58a557d..aad65183 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -708,7 +708,7 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, ( void ) pContext->pPktioHandlepktCB( pContext, AT_SOLICITED, *ppAtResp ); } - /* Clean the command type. Further response from cellular modem won't be + /* Reset the command type. Further response from cellular modem won't be * regarded as AT_SOLICITED response. */ PlatformMutex_Lock( &pContext->PktRespMutex ); pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; @@ -752,10 +752,20 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, LogError( ( "recvdMsgType is AT_UNDEFINED for Message: %s, cmd %s", pLine, ( pContext->pCurrentCmd != NULL ? pContext->pCurrentCmd : "NULL" ) ) ); + + /* Reset the command type. */ + PlatformMutex_Lock( &pContext->PktRespMutex ); + pContext->PktioAtCmdType = CELLULAR_AT_NO_COMMAND; + pContext->pRespPrefix = NULL; + PlatformMutex_Unlock( &pContext->PktRespMutex ); + + /* Clean the read buffer and read pointer. */ ( void ) memset( pContext->pktioReadBuf, 0, PKTIO_READ_BUFFER_SIZE + 1U ); pContext->pPktioReadPtr = NULL; pContext->partialDataRcvdLen = 0; FREE_AT_RESPONSE_AND_SET_NULL( *ppAtResp ); + + /* Return invalid data error code. */ pkStatus = CELLULAR_PKT_STATUS_INVALID_DATA; } else From 635b0bfaa2318fc01bae83b1df1a4420b6171c20 Mon Sep 17 00:00:00 2001 From: Ching-Hsin Lee Date: Wed, 5 Oct 2022 10:10:06 +0800 Subject: [PATCH 24/24] Update complexity --- source/cellular_pktio.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/cellular_pktio.c b/source/cellular_pktio.c index aad65183..687d9760 100644 --- a/source/cellular_pktio.c +++ b/source/cellular_pktio.c @@ -771,7 +771,6 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext, else { /* The undefined response callback handle this message without problem. */ - pkStatus = CELLULAR_PKT_STATUS_OK; } }