Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BG96 support direct push socket #10

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 == 1 )
{
/* Register the URC data callback. */
if( cellularStatus == CELLULAR_SUCCESS )
{
cellularStatus = _Cellular_RegisterInputBufferCallback( pContext, Cellular_BG96InputBufferCallback, 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
44 changes: 32 additions & 12 deletions source/cellular_bg96.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@
/* *INDENT-ON* */

/* AT Command timeout for PDN activation */
#define PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS ( 150000UL )
#define PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS ( 150000UL )

/* AT Command timeout for PDN deactivation. */
#define PDN_DEACTIVATION_PACKET_REQ_TIMEOUT_MS ( 40000UL )
#define PDN_DEACTIVATION_PACKET_REQ_TIMEOUT_MS ( 40000UL )

/* AT Command timeout for Socket connection */
#define SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ( 150000UL )
#define SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ( 150000UL )

#define PACKET_REQ_TIMEOUT_MS ( 5000UL )
#define PACKET_REQ_TIMEOUT_MS ( 5000UL )

/* AT Command timeout for Socket disconnection */
#define SOCKET_DISCONNECT_PACKET_REQ_TIMEOUT_MS ( 12000UL )
#define SOCKET_DISCONNECT_PACKET_REQ_TIMEOUT_MS ( 12000UL )

#define DATA_SEND_TIMEOUT_MS ( 50000UL )
#define DATA_READ_TIMEOUT_MS ( 50000UL )
#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 0
#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. */
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 == 1 )
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_BG96InputBufferCallback( void * pInputBufferCallbackContext,
char * pBuffer,
uint32_t bufferLength,
uint32_t * pBufferLengthHandled );

extern CellularAtParseTokenMap_t CellularUrcHandlerTable[];
extern uint32_t CellularUrcHandlerTableSize;

Expand Down
111 changes: 82 additions & 29 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 @@ -2544,6 +2538,11 @@ CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle,
LogError( ( "Cellular_SocketRecv: Invalid socket handle." ) );
cellularStatus = CELLULAR_INVALID_HANDLE;
}
else if( socketHandle->socketId >= CELLULAR_NUM_SOCKET_MAX )
{
LogError( ( "Cellular_SocketRecv: Invalid socket index." ) );
cellularStatus = CELLULAR_BAD_PARAMETER;
}
else if( ( pBuffer == NULL ) || ( pReceivedDataLength == NULL ) || ( bufferLength == 0U ) )
{
LogError( ( "Cellular_SocketRecv: Bad input Param." ) );
Expand All @@ -2565,33 +2564,87 @@ CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle,
}
else
{
/* Update recvLen to maximum module length. */
if( CELLULAR_MAX_RECV_DATA_LEN <= bufferLength )
if( socketHandle->dataMode == CELLULAR_ACCESSMODE_BUFFER )
{
recvLen = ( uint32_t ) CELLULAR_MAX_RECV_DATA_LEN;
}
/* Update recvLen to maximum module length. */
if( CELLULAR_MAX_RECV_DATA_LEN <= bufferLength )
{
recvLen = ( uint32_t ) CELLULAR_MAX_RECV_DATA_LEN;
}

/* Update receive timeout to default timeout if not set with setsocketopt. */
if( socketHandle->recvTimeoutMs != 0U )
{
recvTimeout = socketHandle->recvTimeoutMs;
/* Update receive timeout to default timeout if not set with setsocketopt. */
if( socketHandle->recvTimeoutMs != 0U )
{
recvTimeout = socketHandle->recvTimeoutMs;
}

/* Form the AT command. */

/* The return value of snprintf is not used.
* The max length of the string is fixed and checked offline. */
/* coverity[misra_c_2012_rule_21_6_violation]. */
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE,
"%s%ld,%ld", "AT+QIRD=", socketHandle->socketId, recvLen );
pktStatus = _Cellular_TimeoutAtcmdDataRecvRequestWithCallback( pContext,
atReqSocketRecv, recvTimeout, socketRecvDataPrefix, NULL );

if( pktStatus != CELLULAR_PKT_STATUS_OK )
{
/* Reset data handling parameters. */
LogError( ( "_Cellular_RecvData: Data Receive fail, pktStatus: %d", pktStatus ) );
cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
}
}

/* Form the AT command. */
#if ( CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET == 1 )
else if( socketHandle->dataMode == CELLULAR_ACCESSMODE_DIRECT_PUSH )
{
/* Socket data is returned in URC with direct push mode and store in
* in the buffer of module context. Copy the data from the buffer and
* decrease the data length of the buffer. */
cellularModuleContext_t * pModuleContext = NULL;
uint8_t * pSocketDataPtr;
uint32_t socketDataLength;

/* The return value of snprintf is not used.
* The max length of the string is fixed and checked offline. */
/* coverity[misra_c_2012_rule_21_6_violation]. */
( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE,
"%s%ld,%ld", "AT+QIRD=", socketHandle->socketId, recvLen );
pktStatus = _Cellular_TimeoutAtcmdDataRecvRequestWithCallback( pContext,
atReqSocketRecv, recvTimeout, socketRecvDataPrefix, NULL );
cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext );

if( pktStatus != CELLULAR_PKT_STATUS_OK )
if( cellularStatus != CELLULAR_SUCCESS )
{
pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE;
}
else
{
PlatformMutex_Lock( &pModuleContext->contextMutex );

pSocketDataPtr = ( uint8_t * ) &pModuleContext->pSocketBuffer[ socketHandle->socketId ];
socketDataLength = pModuleContext->pSocketDataSize[ socketHandle->socketId ];
chinglee-iot marked this conversation as resolved.
Show resolved Hide resolved

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 );
}
}
#endif /* CELLULAR_BG96_SUPPPORT_DIRECT_PUSH_SOCKET. */
else
{
/* Reset data handling parameters. */
LogError( ( "_Cellular_RecvData: Data Receive fail, pktStatus: %d", pktStatus ) );
cellularStatus = _Cellular_TranslatePktStatus( pktStatus );
LogError( ( "storeAccessModeAndAddress, Access mode not supported %d.",
socketHandle->dataMode ) );
cellularStatus = CELLULAR_UNSUPPORTED;
}
}

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