Skip to content

Commit

Permalink
Add BG96 support direct push buffer
Browse files Browse the repository at this point in the history
* define CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET to enable this
  function.
  • Loading branch information
chinglee-iot committed Apr 9, 2023
1 parent c33a362 commit 2fdb9c3
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 15 deletions.
16 changes: 13 additions & 3 deletions source/cellular_bg96.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ CellularError_t Cellular_ModuleInit( const CellularContext_t * pContext,
( void ) memset( &cellularBg96Context, 0, sizeof( cellularModuleContext_t ) );

/* Create the mutex for DNS. */
status = PlatformMutex_Create( &cellularBg96Context.dnsQueryMutex, false );
status = PlatformMutex_Create( &cellularBg96Context.contextMutex, false );

if( status == false )
{
Expand All @@ -171,14 +171,24 @@ CellularError_t Cellular_ModuleInit( const CellularContext_t * pContext,

if( cellularBg96Context.pktDnsQueue == NULL )
{
PlatformMutex_Destroy( &cellularBg96Context.dnsQueryMutex );
PlatformMutex_Destroy( &cellularBg96Context.contextMutex );
cellularStatus = CELLULAR_NO_MEMORY;
}
else
{
*ppModuleContext = ( void * ) &cellularBg96Context;
}
}

#if CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET
{
/* Register the URC data callback. */
if( cellularStatus == CELLULAR_SUCCESS )
{
cellularStatus = _Cellular_RegisterUrcDataCallback( pContext, Cellular_BG96UrcDataCallback, pContext );
}
}
#endif /* CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET. */
}

return cellularStatus;
Expand All @@ -202,7 +212,7 @@ CellularError_t Cellular_ModuleCleanUp( const CellularContext_t * pContext )
vQueueDelete( cellularBg96Context.pktDnsQueue );

/* Delete the mutex for DNS. */
PlatformMutex_Destroy( &cellularBg96Context.dnsQueryMutex );
PlatformMutex_Destroy( &cellularBg96Context.contextMutex );
}

return cellularStatus;
Expand Down
22 changes: 21 additions & 1 deletion source/cellular_bg96.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
#define DATA_SEND_TIMEOUT_MS ( 50000UL )
#define DATA_READ_TIMEOUT_MS ( 50000UL )

#ifndef CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET
#define CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET 1
#endif

#ifndef CELLULAR_BG96_DIRECT_PUSH_SOCKET_BUFFER_SIZE
#define CELLULAR_BG96_DIRECT_PUSH_SOCKET_BUFFER_SIZE ( 2048UL )
#endif /* CELLULAR_BG96_DIRECT_PUSH_SOCKET_BUFFER_SIZE. */

/**
* @brief DNS query result.
*/
Expand All @@ -71,12 +79,19 @@ typedef void ( * CellularDnsResultEventCallback_t )( cellularModuleContext_t * p

typedef struct cellularModuleContext
{
PlatformMutex_t contextMutex; /* Mutex for module context. */

/* DNS related variables. */
PlatformMutex_t dnsQueryMutex; /* DNS query mutex to protect the following data. */
QueueHandle_t pktDnsQueue; /* DNS queue to receive the DNS query result. */
uint8_t dnsResultNumber; /* DNS query result number. */
uint8_t dnsIndex; /* DNS query current index. */
char * pDnsUsrData; /* DNS user data to store the result. */

#if CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET
uint8_t pSocketBuffer[ CELLULAR_NUM_SOCKET_MAX ][ CELLULAR_BG96_DIRECT_PUSH_SOCKET_BUFFER_SIZE ];
uint32_t pSocketDataSize[ CELLULAR_NUM_SOCKET_MAX ];
#endif /* CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET. */

CellularDnsResultEventCallback_t dnsEventCallback;
/* Forward declaration to declar the callback function prototype. */
/* coverity[misra_c_2012_rule_1_1_violation]. */
Expand All @@ -85,6 +100,11 @@ typedef struct cellularModuleContext
CellularPktStatus_t _Cellular_ParseSimstat( char * pInputStr,
CellularSimCardState_t * pSimState );

CellularPktStatus_t Cellular_BG96UrcDataCallback( void * pCallbackContext,
char * pLine,
uint32_t lineLength,
uint32_t * pUrcBufferLength );

extern CellularAtParseTokenMap_t CellularUrcHandlerTable[];
extern uint32_t CellularUrcHandlerTableSize;

Expand Down
73 changes: 63 additions & 10 deletions source/cellular_bg96_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1800,12 +1800,6 @@ static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext,
socketHandle->socketState ) );
cellularStatus = CELLULAR_INTERNAL_FAILURE;
}
else if( dataAccessMode != CELLULAR_ACCESSMODE_BUFFER )
{
LogError( ( "storeAccessModeAndAddress, Access mode not supported %d",
dataAccessMode ) );
cellularStatus = CELLULAR_UNSUPPORTED;
}
else
{
socketHandle->remoteSocketAddress.port = pRemoteSocketAddress->port;
Expand Down Expand Up @@ -2563,7 +2557,7 @@ CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle,
cellularStatus = CELLULAR_SOCKET_CLOSED;
}
}
else
else if( socketHandle->dataMode == CELLULAR_ACCESSMODE_BUFFER )
{
/* Update recvLen to maximum module length. */
if( CELLULAR_MAX_RECV_DATA_LEN <= bufferLength )
Expand Down Expand Up @@ -2594,6 +2588,65 @@ CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle,
cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
}
}
else
{
#if CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET
{
if( socketHandle->dataMode == CELLULAR_ACCESSMODE_DIRECT_PUSH )
{
/* Copy from the buffer. */
cellularModuleContext_t * pModuleContext = NULL;
cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext );

if( cellularStatus != CELLULAR_SUCCESS )
{
pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE;
}
else
{
uint8_t *pSocketDataPtr;
uint32_t socketDataLength;

PlatformMutex_Lock( &pModuleContext->contextMutex );

pSocketDataPtr = &pModuleContext->pSocketBuffer[ socketHandle->socketId ];
socketDataLength = pModuleContext->pSocketDataSize[ socketHandle->socketId ];

if( bufferLength > socketDataLength )
{
*pReceivedDataLength = socketDataLength;
}
else
{
*pReceivedDataLength = bufferLength;
}

/* Copy the data to the socket buffer. */
memcpy( pBuffer, pSocketDataPtr, *pReceivedDataLength );

/* Garbage collection. Decrease the size of data in socket buffer
* and move the data to start of socket buffer. */
pModuleContext->pSocketDataSize[ socketHandle->socketId ] -= *pReceivedDataLength;
memmove( pSocketDataPtr, &pSocketDataPtr[ *pReceivedDataLength ], ( socketDataLength - *pReceivedDataLength ) );

PlatformMutex_Unlock( &pModuleContext->contextMutex );
}
}
else
{
LogError( ( "storeAccessModeAndAddress, Access mode not supported %d.",
socketHandle->dataMode ) );
cellularStatus = CELLULAR_UNSUPPORTED;
}
}
#else /* CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET. */
{
LogError( ( "storeAccessModeAndAddress, Access mode not supported %d.",
socketHandle->dataMode ) );
cellularStatus = CELLULAR_UNSUPPORTED;
}
#endif /* CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET. */
}

return cellularStatus;
}
Expand Down Expand Up @@ -3133,7 +3186,7 @@ CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle,

if( cellularStatus == CELLULAR_SUCCESS )
{
PlatformMutex_Lock( &pModuleContext->dnsQueryMutex );
PlatformMutex_Lock( &pModuleContext->contextMutex );
pModuleContext->dnsResultNumber = 0;
pModuleContext->dnsIndex = 0;
( void ) xQueueReset( pModuleContext->pktDnsQueue );
Expand All @@ -3154,7 +3207,7 @@ CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle,
{
LogError( ( "Cellular_GetHostByName: couldn't resolve host name" ) );
cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
PlatformMutex_Unlock( &pModuleContext->dnsQueryMutex );
PlatformMutex_Unlock( &pModuleContext->contextMutex );
}
}

Expand All @@ -3175,7 +3228,7 @@ CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle,
cellularStatus = CELLULAR_TIMEOUT;
}

PlatformMutex_Unlock( &pModuleContext->dnsQueryMutex );
PlatformMutex_Unlock( &pModuleContext->contextMutex );
}

return cellularStatus;
Expand Down
Loading

0 comments on commit 2fdb9c3

Please sign in to comment.