Skip to content

Commit

Permalink
Support AT command with no success result code (#138)
Browse files Browse the repository at this point in the history
* Add CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE and CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE at command type
  • Loading branch information
chinglee-iot authored Jun 13, 2023
1 parent f09e135 commit 4bc022e
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
</tr>
<tr>
<td>cellular_pktio.c</td>
<td><center>2.1K</center></td>
<td><center>2.2K</center></td>
<td><center>1.9K</center></td>
</tr>
<tr>
<td><b>Total estimates</b></td>
<td><b><center>14.9K</center></b></td>
<td><b><center>15.0K</center></b></td>
<td><b><center>13.6K</center></b></td>
</tr>
</table>
4 changes: 4 additions & 0 deletions lexicon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ pclosedcallbackcontext
pcomminterface
pcomminterfacehandle
pcommintf
pcommintfrecvcustomstring
pcontext
pcomparestring
pcurrentcmd
Expand Down Expand Up @@ -551,6 +552,7 @@ pinputptr
pinputstr
pitm
pkio
pkstatus
pkt
pktdataprefixcallback
pktdataprefixcb
Expand Down Expand Up @@ -644,6 +646,8 @@ prl
processmodemrdy
processpowerdown
prssivalue
prvpacketcallbackerror
prvpacketcallbacksuccess
ps
psaveptr
psentdatalength
Expand Down
31 changes: 22 additions & 9 deletions source/cellular_pktio.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ static CellularPktStatus_t _processIntermediateResponse( char * pLine,
pkStatus = CELLULAR_PKT_STATUS_PENDING_BUFFER;
break;

case CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE:
case CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE:
/* Save the line in the response. */
_saveATData( pLine, pResp );

/* Returns CELLULAR_PKT_STATUS_OK to indicate that the response of the
* command is received. No success result code is expected. Set the response
* status to true here. */
pkStatus = CELLULAR_PKT_STATUS_OK;
pResp->status = true;
break;

default:
/* Unexpected message received when sending the AT command. */
LogInfo( ( "Undefind message received %s when sending AT command type %d.",
Expand Down Expand Up @@ -374,20 +386,20 @@ static CellularPktStatus_t _Cellular_ProcessLine( CellularContext_t * pContext,
pResp->status = false;
pkStatus = CELLULAR_PKT_STATUS_OK;
}
else
{
pkStatus = _processIntermediateResponse( pLine, pResp, atType );
}
}

if( result != true )
{
pkStatus = _processIntermediateResponse( pLine, pResp, atType );
}
}

if( ( result == true ) && ( pResp->status == false ) )
{
LogWarn( ( "Modem return ERROR: line %s, cmd : %s, respPrefix %s, status: %d",
( pContext->pCurrentCmd != NULL ? pContext->pCurrentCmd : "NULL" ),
LogWarn( ( "Modem return ERROR: line %s, cmd : %s, respPrefix %s",
pLine,
( pRespPrefix != NULL ? pRespPrefix : "NULL" ),
pkStatus ) );
( pContext->pCurrentCmd != NULL ? pContext->pCurrentCmd : "NULL" ),
( pRespPrefix != NULL ? pRespPrefix : "NULL" ) ) );
}

PlatformMutex_Unlock( &pContext->PktRespMutex );
Expand Down Expand Up @@ -470,7 +482,8 @@ static _atRespType_t _getMsgType( CellularContext_t * pContext,
if( ( ( pContext->PktioAtCmdType != CELLULAR_AT_NO_COMMAND ) && ( pRespPrefix == NULL ) ) ||
( pContext->PktioAtCmdType == CELLULAR_AT_MULTI_DATA_WO_PREFIX ) ||
( pContext->PktioAtCmdType == CELLULAR_AT_WITH_PREFIX ) ||
( pContext->PktioAtCmdType == CELLULAR_AT_MULTI_WITH_PREFIX ) )
( pContext->PktioAtCmdType == CELLULAR_AT_MULTI_WITH_PREFIX ) ||
( pContext->PktioAtCmdType == CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE ) )
{
atRespType = AT_SOLICITED;
}
Expand Down
16 changes: 9 additions & 7 deletions source/include/cellular_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,15 @@ typedef enum CellularPktStatus
*/
typedef enum CellularATCommandType
{
CELLULAR_AT_NO_RESULT, /**< no response expected, only OK, ERROR etc. */
CELLULAR_AT_WO_PREFIX, /**< string response without a prefix. */
CELLULAR_AT_WITH_PREFIX, /**< string response with a prefix. */
CELLULAR_AT_MULTI_WITH_PREFIX, /**< multiple line response all start with a prefix. */
CELLULAR_AT_MULTI_WO_PREFIX, /**< multiple line response with or without a prefix. */
CELLULAR_AT_MULTI_DATA_WO_PREFIX, /**< multiple line data response with or without a prefix. */
CELLULAR_AT_NO_COMMAND /**< no command is waiting response. */
CELLULAR_AT_NO_RESULT, /**< no response expected, only OK, ERROR etc. */
CELLULAR_AT_WO_PREFIX, /**< string response without a prefix. */
CELLULAR_AT_WITH_PREFIX, /**< string response with a prefix. */
CELLULAR_AT_MULTI_WITH_PREFIX, /**< multiple line response all start with a prefix. */
CELLULAR_AT_MULTI_WO_PREFIX, /**< multiple line response with or without a prefix. */
CELLULAR_AT_MULTI_DATA_WO_PREFIX, /**< multiple line data response with or without a prefix. */
CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE, /**< string response without a prefix and no result code is expected. */
CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE, /**< string response WITH a prefix and no result code is expected. */
CELLULAR_AT_NO_COMMAND /**< no command is waiting response. */
} CellularATCommandType_t;

/**
Expand Down
219 changes: 219 additions & 0 deletions test/unit-test/cellular_pktio_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,51 @@ static void prvInputBufferCommIntfRecvCallback( void )
pCommIntfRecvCustomString = URC_DATA_CALLBACK_MATCH_STR_PART2;
}

static CellularPktStatus_t prvPacketCallbackError( CellularContext_t * pContext,
_atRespType_t atRespType,
const void * pBuffer )
{
const CellularATCommandResponse_t * pAtResp = ( const CellularATCommandResponse_t * ) pBuffer;

( void ) pContext;

/* Verify the response type is AT_SOLICITED. */
TEST_ASSERT_EQUAL( AT_SOLICITED, atRespType );

/* Verify that no item is added to the response. */
TEST_ASSERT_NOT_EQUAL( NULL, pAtResp );
TEST_ASSERT_EQUAL( NULL, pAtResp->pItm );

/* Verify that the response indicate error. */
TEST_ASSERT_EQUAL( false, pAtResp->status );

return CELLULAR_PKT_STATUS_OK;
}

static CellularPktStatus_t prvPacketCallbackSuccess( CellularContext_t * pContext,
_atRespType_t atRespType,
const void * pBuffer )
{
const CellularATCommandResponse_t * pAtResp = ( const CellularATCommandResponse_t * ) pBuffer;
int cmpResult;

( void ) pContext;

/* Verify the response type is AT_SOLICITED. */
TEST_ASSERT_EQUAL( AT_SOLICITED, atRespType );

/* Verify the string is the same as expected. */
TEST_ASSERT_NOT_EQUAL( NULL, pAtResp );
TEST_ASSERT_NOT_EQUAL( NULL, pAtResp->pItm );
TEST_ASSERT_NOT_EQUAL( NULL, pAtResp->pItm->pLine );
cmpResult = strncmp( pAtResp->pItm->pLine, pCommIntfRecvCustomString, strlen( pAtResp->pItm->pLine ) );
TEST_ASSERT_EQUAL( 0, cmpResult );

/* Verify that the response indicate error. */
TEST_ASSERT_EQUAL( true, pAtResp->status );

return CELLULAR_PKT_STATUS_OK;
}

/* ========================================================================== */

Expand Down Expand Up @@ -2141,6 +2186,180 @@ void test__Cellular_PktioInit_Thread_Rx_Data_Event_CELLULAR_AT_WO_PREFIX_STRING_
TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus );
}

/**
* @brief _processIntermediateResponse - Successfully handle AT command type CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE.
*
* Successfully handle at command type CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE. Verify
* the response string in the callback function.
*
* <b>Coverage</b>
* @code{c}
* case CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE:
* ...
* _saveATData( pLine, pResp );
*
* pktStatus = CELLULAR_PKT_STATUS_OK;
* break;
* @endcode
* The CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE case.
*/
void test__Cellular_PktioInit_Thread_Rx_Data_Event_CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE_success( void )
{
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;

threadReturn = true;
memset( &context, 0, sizeof( CellularContext_t ) );

/* copy the token table. */
( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) );

/* Assign the comm interface to pContext. */
context.pCommIntf = pCommIntf;
context.pPktioShutdownCB = _shutdownCallback;

/* Test the rx_data event with CELLULAR_AT_WO_PREFIX resp. */
pktioEvtMask = PKTIO_EVT_MASK_RX_DATA;
recvCount = 1;
atCmdType = CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE;
testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING;
pCommIntfRecvCustomString = "12345\r\n"; /* Dummy string to be verified in the callback. */

/* Check that CELLULAR_PKT_STATUS_OK is returned. */
pktStatus = _Cellular_PktioInit( &context, prvPacketCallbackSuccess );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus );

/* The result is verified in prvPacketCallbackSuccess. */
}

/**
* @brief _processIntermediateResponse - Modem returns error when sending AT command type CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE.
*
* Modem returns error when sending AT command type CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE.
*/
void test__Cellular_PktioInit_Thread_Rx_Data_Event_CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE_error( void )
{
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;

threadReturn = true;
memset( &context, 0, sizeof( CellularContext_t ) );

/* copy the token table. */
( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) );

/* Assign the comm interface to pContext. */
context.pCommIntf = pCommIntf;
context.pPktioShutdownCB = _shutdownCallback;

/* Test the rx_data event with CELLULAR_AT_WO_PREFIX resp. */
pktioEvtMask = PKTIO_EVT_MASK_RX_DATA;
recvCount = 1;
atCmdType = CELLULAR_AT_WO_PREFIX_NO_RESULT_CODE;
testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING;
pCommIntfRecvCustomString = "ERROR\r\n"; /* Return one of the error token. */

/* Check that CELLULAR_PKT_STATUS_OK is returned. */
pktStatus = _Cellular_PktioInit( &context, prvPacketCallbackError );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus );

/* The result is verified in prvPacketCallbackError. */
}


/**
* @brief _processIntermediateResponse - Successfully handle AT command type CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE.
*
* Successfully handle at command type CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE. Verify
* the response string in the callback function.
*
* <b>Coverage</b>
* @code{c}
* case CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE:
* _saveATData( pLine, pResp );
*
* pkStatus = CELLULAR_PKT_STATUS_OK;
* break;
* @endcode
* The CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE case.
*/
void test__Cellular_PktioInit_Thread_Rx_Data_Event_CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE_success( void )
{
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;

threadReturn = true;
memset( &context, 0, sizeof( CellularContext_t ) );

/* copy the token table. */
( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) );

/* Assign the comm interface to pContext. */
context.pCommIntf = pCommIntf;
context.pPktioShutdownCB = _shutdownCallback;

/* Test the rx_data event with CELLULAR_AT_WO_PREFIX resp. */
pktioEvtMask = PKTIO_EVT_MASK_RX_DATA;
recvCount = 1;
atCmdType = CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE;
testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING;
context.pRespPrefix = "+CMD_PREFIX";
pCommIntfRecvCustomString = "+CMD_PREFIX:12345\r\n"; /* Dummy string to be verified in the callback. */

/* Check that CELLULAR_PKT_STATUS_OK is returned. */
pktStatus = _Cellular_PktioInit( &context, prvPacketCallbackSuccess );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus );

/* The result is verified in prvPacketCallbackSuccess. */
}

/**
* @brief _processIntermediateResponse - Modem returns error when sending AT command type CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE.
*
* Modem returns error when sending AT command type CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE.
*/
void test__Cellular_PktioInit_Thread_Rx_Data_Event_CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE_error( void )
{
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;

threadReturn = true;
memset( &context, 0, sizeof( CellularContext_t ) );

/* copy the token table. */
( void ) memcpy( &context.tokenTable, &tokenTable, sizeof( CellularTokenTable_t ) );

/* Assign the comm interface to pContext. */
context.pCommIntf = pCommIntf;
context.pPktioShutdownCB = _shutdownCallback;

/* Test the rx_data event with CELLULAR_AT_WO_PREFIX resp. */
pktioEvtMask = PKTIO_EVT_MASK_RX_DATA;
recvCount = 1;
atCmdType = CELLULAR_AT_WITH_PREFIX_NO_RESULT_CODE;
testCommIfRecvType = COMM_IF_RECV_CUSTOM_STRING;
context.pRespPrefix = "+CMD_PREFIX";
pCommIntfRecvCustomString = "ERROR\r\n"; /* Return one of the error token. */

/* Check that CELLULAR_PKT_STATUS_OK is returned. */
pktStatus = _Cellular_PktioInit( &context, prvPacketCallbackError );

/* Verification. */
TEST_ASSERT_EQUAL( CELLULAR_PKT_STATUS_OK, pktStatus );

/* The result is verified in prvPacketCallbackError. */
}

/**
* @brief Test thread receiving rx data event with success token for _Cellular_PktioInit to return CELLULAR_PKT_STATUS_OK.
*/
Expand Down

0 comments on commit 4bc022e

Please sign in to comment.