Skip to content

Commit

Permalink
Update cellular platform header APIs (#166)
Browse files Browse the repository at this point in the history
* Move the following FreeRTOS functions and data structure to platform and add CELLULAR_CONFIG_PLATFORM_FREERTOS for backward compatibility. Default to 1 for backward compatibility.
* Add CELLULAR_CONFIG_ASSERT config. Default value is configASSERT
* Remove critical section dependency. The common layer function is not thread safe. Thread safe should be handled by application.
  • Loading branch information
chinglee-iot authored May 21, 2024
1 parent 6486160 commit aff376d
Show file tree
Hide file tree
Showing 9 changed files with 642 additions and 103 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 @@ -30,7 +30,7 @@
<tr>
<td>cellular_common.c</td>
<td><center>1.7K</center></td>
<td><center>1.6K</center></td>
<td><center>1.5K</center></td>
</tr>
<tr>
<td>cellular_pkthandler.c</td>
Expand All @@ -45,6 +45,6 @@
<tr>
<td><b>Total estimates</b></td>
<td><b><center>15.1K</center></b></td>
<td><b><center>13.8K</center></b></td>
<td><b><center>13.7K</center></b></td>
</tr>
</table>
14 changes: 7 additions & 7 deletions source/cellular_3gpp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ static CellularError_t queryNetworkStatus( CellularContext_t * pContext,
CellularNetworkRegType_t recvRegType = regType;
CellularAtReq_t atReqGetResult = { 0 };

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );
atReqGetResult.pAtCmd = pCommand;
atReqGetResult.atCmdType = CELLULAR_AT_MULTI_WITH_PREFIX;
atReqGetResult.pAtRspPrefix = pPrefix;
Expand Down Expand Up @@ -1449,7 +1449,7 @@ static CellularError_t atcmdQueryRegStatus( CellularContext_t * pContext,
const cellularAtData_t * pLibAtData = NULL;
CellularNetworkRegistrationStatus_t psRegStatus = REGISTRATION_STATUS_UNKNOWN;

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

cellularStatus = queryNetworkStatus( pContext, "AT+CREG?", "+CREG", CELLULAR_REG_TYPE_CREG );

Expand Down Expand Up @@ -2078,7 +2078,7 @@ CellularError_t Cellular_CommonGetIPAddress( CellularHandle_t cellularHandle,

void _Cellular_DestroyAtDataMutex( CellularContext_t * pContext )
{
configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

PlatformMutex_Destroy( &( pContext->libAtDataMutex ) );
}
Expand All @@ -2089,7 +2089,7 @@ bool _Cellular_CreateAtDataMutex( CellularContext_t * pContext )
{
bool status = false;

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

status = PlatformMutex_Create( &( pContext->libAtDataMutex ), false );

Expand All @@ -2100,7 +2100,7 @@ bool _Cellular_CreateAtDataMutex( CellularContext_t * pContext )

void _Cellular_LockAtDataMutex( CellularContext_t * pContext )
{
configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

PlatformMutex_Lock( &( pContext->libAtDataMutex ) );
}
Expand All @@ -2109,7 +2109,7 @@ void _Cellular_LockAtDataMutex( CellularContext_t * pContext )

void _Cellular_UnlockAtDataMutex( CellularContext_t * pContext )
{
configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

PlatformMutex_Unlock( &( pContext->libAtDataMutex ) );
}
Expand All @@ -2123,7 +2123,7 @@ void _Cellular_InitAtData( CellularContext_t * pContext,
{
cellularAtData_t * pLibAtData = NULL;

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

pLibAtData = &( pContext->libAtData );

Expand Down
16 changes: 2 additions & 14 deletions source/cellular_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ static CellularContext_t * _Cellular_AllocContext( void )
CellularContext_t * pContext = NULL;
uint8_t i = 0;

taskENTER_CRITICAL();

for( i = 0; i < CELLULAR_CONTEXT_MAX; i++ )
{
if( cellularContextTable[ i ] == NULL )
Expand All @@ -144,8 +142,6 @@ static CellularContext_t * _Cellular_AllocContext( void )
}
}

taskEXIT_CRITICAL();

return pContext;
}

Expand All @@ -155,8 +151,6 @@ static void _Cellular_FreeContext( CellularContext_t * pContext )
{
uint8_t i = 0;

taskENTER_CRITICAL();

for( i = 0; i < CELLULAR_CONTEXT_MAX; i++ )
{
if( cellularContextTable[ i ] == pContext )
Expand All @@ -170,8 +164,6 @@ static void _Cellular_FreeContext( CellularContext_t * pContext )
break;
}
}

taskEXIT_CRITICAL();
}

/*-----------------------------------------------------------*/
Expand All @@ -188,7 +180,7 @@ static CellularError_t libOpen( CellularContext_t * pContext )
CellularError_t cellularStatus = CELLULAR_SUCCESS;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

PlatformMutex_Lock( &( pContext->libStatusMutex ) );

Expand Down Expand Up @@ -235,7 +227,7 @@ static void libClose( CellularContext_t * pContext )
bool bOpened = false;
uint8_t i = 0;

configASSERT( pContext != NULL );
CELLULAR_CONFIG_ASSERT( pContext != NULL );

PlatformMutex_Lock( &( pContext->libStatusMutex ) );
bOpened = pContext->bLibOpened;
Expand Down Expand Up @@ -538,8 +530,6 @@ CellularError_t _Cellular_CreateSocketData( CellularContext_t * pContext,
CellularSocketContext_t * pSocketData = NULL;
uint8_t socketId = 0;

taskENTER_CRITICAL();

for( socketId = 0; socketId < CELLULAR_NUM_SOCKET_MAX; socketId++ )
{
if( pContext->pSocketData[ socketId ] == NULL )
Expand Down Expand Up @@ -570,8 +560,6 @@ CellularError_t _Cellular_CreateSocketData( CellularContext_t * pContext,
}
}

taskEXIT_CRITICAL();

if( cellularStatus == CELLULAR_NO_MEMORY )
{
LogError( ( "_Cellular_CreateSocket, Out of memory" ) );
Expand Down
18 changes: 9 additions & 9 deletions source/cellular_pkthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static CellularPktStatus_t _convertAndQueueRespPacket( CellularContext_t * pCont
}

/* Notify calling thread, Not blocking immediately comes back if the queue is full. */
if( xQueueSend( pContext->pktRespQueue, ( void * ) &pktStatus, ( TickType_t ) 0 ) != pdPASS )
if( PlatformQueue_Send( pContext->pktRespQueue, ( void * ) &pktStatus, ( PlatformTickType_t ) 0 ) != platformPASS )
{
pktStatus = CELLULAR_PKT_STATUS_FAILURE;
LogError( ( "_convertAndQueueRespPacket: Got a response when the Resp Q is full!!" ) );
Expand Down Expand Up @@ -203,7 +203,7 @@ static CellularPktStatus_t _Cellular_AtcmdRequestTimeoutWithCallbackRaw( Cellula
{
CellularPktStatus_t respCode = CELLULAR_PKT_STATUS_OK;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
BaseType_t qRet = pdFALSE;
PlatformBaseType_t qRet = platformFALSE;

if( atReq.pAtCmd == NULL )
{
Expand Down Expand Up @@ -231,9 +231,9 @@ static CellularPktStatus_t _Cellular_AtcmdRequestTimeoutWithCallbackRaw( Cellula
else
{
/* Wait for a response. */
qRet = xQueueReceive( pContext->pktRespQueue, &respCode, pdMS_TO_TICKS( timeoutMS ) );
qRet = PlatformQueue_Receive( pContext->pktRespQueue, &respCode, pdMS_TO_TICKS( timeoutMS ) );

if( qRet == pdTRUE )
if( qRet == platformTRUE )
{
pktStatus = ( CellularPktStatus_t ) respCode;

Expand Down Expand Up @@ -270,7 +270,7 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex
{
CellularPktStatus_t respCode = CELLULAR_PKT_STATUS_OK;
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
BaseType_t qStatus = pdFALSE;
PlatformBaseType_t qStatus = platformFALSE;
uint32_t sendEndPatternLen = 0U;

if( ( dataReq.pData == NULL ) || ( dataReq.pSentDataLength == NULL ) )
Expand Down Expand Up @@ -312,9 +312,9 @@ static CellularPktStatus_t _Cellular_DataSendWithTimeoutDelayRaw( CellularContex
/* Wait for a response. */
if( pktStatus == CELLULAR_PKT_STATUS_OK )
{
qStatus = xQueueReceive( pContext->pktRespQueue, &respCode, pdMS_TO_TICKS( timeoutMs ) );
qStatus = PlatformQueue_Receive( pContext->pktRespQueue, &respCode, pdMS_TO_TICKS( timeoutMs ) );

if( qStatus == pdTRUE )
if( qStatus == platformTRUE )
{
pktStatus = ( CellularPktStatus_t ) respCode;

Expand Down Expand Up @@ -509,7 +509,7 @@ void _Cellular_PktHandlerCleanup( CellularContext_t * pContext )
_Cellular_PktHandlerAcquirePktRequestMutex( pContext );
/* This is platform dependent api. */

( void ) vQueueDelete( pContext->pktRespQueue );
( void ) PlatformQueue_Delete( pContext->pktRespQueue );
pContext->pktRespQueue = NULL;
_Cellular_PktHandlerReleasePktRequestMutex( pContext );
}
Expand Down Expand Up @@ -782,7 +782,7 @@ CellularPktStatus_t _Cellular_PktHandlerInit( CellularContext_t * pContext )
if( pContext != NULL )
{
/* Create the response queue which is used to post responses to the sender. */
pContext->pktRespQueue = xQueueCreate( 1, ( uint32_t ) sizeof( CellularPktStatus_t ) );
pContext->pktRespQueue = PlatformQueue_Create( 1, ( uint32_t ) sizeof( CellularPktStatus_t ) );

if( pContext->pktRespQueue == NULL )
{
Expand Down
62 changes: 31 additions & 31 deletions source/cellular_pktio.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static void _saveData( char * pLine,
LogDebug( ( "_saveData : Save data %p with length %u", pLine, ( unsigned int ) dataLen ) );

pNew = ( CellularATCommandLine_t * ) Platform_Malloc( sizeof( CellularATCommandLine_t ) );
configASSERT( ( pNew != NULL ) );
CELLULAR_CONFIG_ASSERT( ( pNew != NULL ) );

/* Reuse the pktio buffer instead of allocate. */
pNew->pLine = pLine;
Expand Down Expand Up @@ -287,7 +287,7 @@ static CellularATCommandResponse_t * _Cellular_AtResponseNew( void )
CellularATCommandResponse_t * pNew = NULL;

pNew = ( CellularATCommandResponse_t * ) Platform_Malloc( sizeof( CellularATCommandResponse_t ) );
configASSERT( ( pNew != NULL ) );
CELLULAR_CONFIG_ASSERT( ( pNew != NULL ) );

( void ) memset( ( void * ) pNew, 0, sizeof( CellularATCommandResponse_t ) );

Expand Down Expand Up @@ -520,7 +520,7 @@ static CellularCommInterfaceError_t _Cellular_PktRxCallBack( void * pUserData,
CellularCommInterfaceHandle_t commInterfaceHandle )
{
const CellularContext_t * pContext = ( CellularContext_t * ) pUserData;
BaseType_t xHigherPriorityTaskWoken = pdFALSE, xResult = pdFALSE;
PlatformBaseType_t xHigherPriorityTaskWoken = platformFALSE, xResult = platformFALSE;
CellularCommInterfaceError_t retComm = IOT_COMM_INTERFACE_SUCCESS;

( void ) commInterfaceHandle; /* Comm if is not used in this function. */
Expand All @@ -532,13 +532,13 @@ static CellularCommInterfaceError_t _Cellular_PktRxCallBack( void * pUserData,
}
else
{
xResult = ( BaseType_t ) PlatformEventGroup_SetBitsFromISR( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( EventBits_t ) PKTIO_EVT_MASK_RX_DATA,
&xHigherPriorityTaskWoken );
xResult = PlatformEventGroup_SetBitsFromISR( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( PlatformEventBits_t ) PKTIO_EVT_MASK_RX_DATA,
&xHigherPriorityTaskWoken );

if( xResult == pdPASS )
if( xResult == platformPASS )
{
if( xHigherPriorityTaskWoken == pdTRUE )
if( xHigherPriorityTaskWoken == platformTRUE )
{
retComm = IOT_COMM_INTERFACE_SUCCESS;
}
Expand Down Expand Up @@ -1201,7 +1201,7 @@ static uint32_t _handleRxDataEvent( CellularContext_t * pContext )
static void _pktioReadThread( void * pUserData )
{
CellularContext_t * pContext = ( CellularContext_t * ) pUserData;
PlatformEventGroup_EventBits uxBits = 0;
PlatformEventBits_t uxBits = 0;
uint32_t bytesRead = 0U;

/* Open main communication port. */
Expand All @@ -1210,24 +1210,24 @@ static void _pktioReadThread( void * pUserData )
&( pContext->hPktioCommIntf ) ) == IOT_COMM_INTERFACE_SUCCESS ) )
{
/* Send thread started event. */
( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( EventBits_t ) PKTIO_EVT_MASK_STARTED );
( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( PlatformEventBits_t ) PKTIO_EVT_MASK_STARTED );

do
{
/* Wait events for abort thread or rx data available. */
uxBits = ( PlatformEventGroup_EventBits ) PlatformEventGroup_WaitBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORT | ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_RX_DATA ),
pdTRUE,
pdFALSE,
portMAX_DELAY );
uxBits = PlatformEventGroup_WaitBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORT | ( PlatformEventBits_t ) PKTIO_EVT_MASK_RX_DATA ),
platformTRUE,
platformFALSE,
platformMAX_DELAY );

if( ( uxBits & ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORT ) != 0U )
if( ( uxBits & ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORT ) != 0U )
{
LogDebug( ( "Abort received, cleaning up!" ) );
FREE_AT_RESPONSE_AND_SET_NULL( pContext->pAtCmdResp );
break;
}
else if( ( uxBits & ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_RX_DATA ) != 0U )
else if( ( uxBits & ( PlatformEventBits_t ) PKTIO_EVT_MASK_RX_DATA ) != 0U )
{
/* Keep Reading until there is no more bytes in comm interface. */
do
Expand All @@ -1250,7 +1250,7 @@ static void _pktioReadThread( void * pUserData )
LogError( ( "Comm port open failed" ) );
}

( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( EventBits_t ) PKTIO_EVT_MASK_ABORTED );
( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORTED );

/* Call the shutdown callback if it is defined. */
if( pContext->pPktioShutdownCB != NULL )
Expand All @@ -1263,15 +1263,15 @@ static void _pktioReadThread( void * pUserData )

static void _PktioInitProcessReadThreadStatus( CellularContext_t * pContext )
{
PlatformEventGroup_EventBits uxBits = 0;
PlatformEventBits_t uxBits = 0;

uxBits = ( PlatformEventGroup_EventBits ) PlatformEventGroup_WaitBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_STARTED | ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORTED ),
pdTRUE,
pdFALSE,
( ( PlatformTickType ) ~( 0UL ) ) );
uxBits = ( PlatformEventBits_t ) PlatformEventGroup_WaitBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( ( PlatformEventBits_t ) PKTIO_EVT_MASK_STARTED | ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORTED ),
platformTRUE,
platformFALSE,
( ( PlatformTickType_t ) ~( 0UL ) ) );

if( ( uxBits & ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORTED ) != ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ABORTED )
if( ( uxBits & ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORTED ) != ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORTED )
{
pContext->bPktioUp = true;
}
Expand Down Expand Up @@ -1367,7 +1367,7 @@ CellularPktStatus_t _Cellular_PktioInit( CellularContext_t * pContext,
{
pContext->pPktioHandlepktCB = handlePacketCb;
( void ) PlatformEventGroup_ClearBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent,
( ( PlatformEventGroup_EventBits ) PKTIO_EVT_MASK_ALL_EVENTS ) );
( ( PlatformEventBits_t ) PKTIO_EVT_MASK_ALL_EVENTS ) );

/* Create the Read thread. */
status = Platform_CreateDetachedThread( _pktioReadThread,
Expand Down Expand Up @@ -1516,19 +1516,19 @@ uint32_t _Cellular_PktioSendData( CellularContext_t * pContext,

void _Cellular_PktioShutdown( CellularContext_t * pContext )
{
PlatformEventGroup_EventBits uxBits = 0;
PlatformEventBits_t uxBits = 0;

if( ( pContext != NULL ) && ( pContext->bPktioUp ) )
{
if( pContext->pPktioCommEvent != NULL )
{
( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( EventBits_t ) PKTIO_EVT_MASK_ABORT );
uxBits = ( PlatformEventGroup_EventBits ) PlatformEventGroup_GetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent );
( void ) PlatformEventGroup_SetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent, ( PlatformEventBits_t ) PKTIO_EVT_MASK_ABORT );
uxBits = ( PlatformEventBits_t ) PlatformEventGroup_GetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent );

while( ( PlatformEventGroup_EventBits ) ( uxBits & PKTIO_EVT_MASK_ABORTED ) != ( PlatformEventGroup_EventBits ) ( PKTIO_EVT_MASK_ABORTED ) )
while( ( PlatformEventBits_t ) ( uxBits & PKTIO_EVT_MASK_ABORTED ) != ( PlatformEventBits_t ) ( PKTIO_EVT_MASK_ABORTED ) )
{
Platform_Delay( PKTIO_SHUTDOWN_WAIT_INTERVAL_MS );
uxBits = ( PlatformEventGroup_EventBits ) PlatformEventGroup_GetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent );
uxBits = ( PlatformEventBits_t ) PlatformEventGroup_GetBits( ( PlatformEventGroupHandle_t ) pContext->pPktioCommEvent );
}

( void ) PlatformEventGroup_Delete( pContext->pPktioCommEvent );
Expand Down
Loading

0 comments on commit aff376d

Please sign in to comment.