From 70f0685be1c2acb1e48e18a227a517e5cc5f83e4 Mon Sep 17 00:00:00 2001 From: ActoryOu Date: Wed, 28 Sep 2022 12:11:01 +0800 Subject: [PATCH] Add cellular modules into repo. --- .../cellular_bg96.c | 358 ++ .../cellular_bg96.h | 106 + .../cellular_bg96_api.c | 3206 +++++++++++++++++ .../cellular_bg96_urc_handler.c | 807 +++++ .../cellular_bg96_wrapper.c | 268 ++ .../cellular_hl7802.c | 585 +++ .../cellular_hl7802.h | 150 + .../cellular_hl7802_api.c | 2788 ++++++++++++++ .../cellular_hl7802_urc_handler.c | 349 ++ .../cellular_hl7802_wrapper.c | 269 ++ .../cellular_r4.c | 562 +++ .../cellular_r4.h | 164 + .../cellular_r4_api.c | 2906 +++++++++++++++ .../cellular_r4_urc_handler.c | 656 ++++ .../cellular_r4_wrapper.c | 263 ++ 15 files changed, 13437 insertions(+) create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.h create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_api.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_urc_handler.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_wrapper.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.h create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_api.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_urc_handler.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_wrapper.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.h create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_api.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_urc_handler.c create mode 100644 FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_wrapper.c diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.c new file mode 100644 index 0000000000..87fe2a4eb6 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.c @@ -0,0 +1,358 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* The config header is always included first. */ + + +#include +#include "cellular_platform.h" +#include "cellular_config.h" +#include "cellular_config_defaults.h" +#include "cellular_common.h" +#include "cellular_common_portable.h" +#include "cellular_bg96.h" + +/*-----------------------------------------------------------*/ + +#define ENBABLE_MODULE_UE_RETRY_COUNT ( 3U ) +#define ENBABLE_MODULE_UE_RETRY_TIMEOUT ( 5000U ) +#define BG96_NWSCANSEQ_CMD_MAX_SIZE ( 29U ) /* The length of AT+QCFG="nwscanseq",020301,1\0. */ + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq ); + +/*-----------------------------------------------------------*/ + +static cellularModuleContext_t cellularBg96Context = { 0 }; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenErrorTable[] = +{ "ERROR", "BUSY", "NO CARRIER", "NO ANSWER", "NO DIALTONE", "ABORTED", "+CMS ERROR", "+CME ERROR", "SEND FAIL" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenErrorTableSize = sizeof( CellularSrcTokenErrorTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenSuccessTable[] = +{ "OK", "CONNECT", "SEND OK", ">" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenSuccessTableSize = sizeof( CellularSrcTokenSuccessTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularUrcTokenWoPrefixTable[] = +{ "NORMAL POWER DOWN", "PSM POWER DOWN", "RDY" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcTokenWoPrefixTableSize = sizeof( CellularUrcTokenWoPrefixTable ) / sizeof( char * ); + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint8_t tryCount = 0; + + if( pAtReq == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + for( ; tryCount < ENBABLE_MODULE_UE_RETRY_COUNT; tryCount++ ) + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, *pAtReq, ENBABLE_MODULE_UE_RETRY_TIMEOUT ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + break; + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static bool appendRatList( char * pRatList, + CellularRat_t cellularRat ) +{ + bool retValue = true; + + /* Configure RAT Searching Sequence to default radio access technology. */ + switch( cellularRat ) + { + case CELLULAR_RAT_CATM1: + strcat( pRatList, "02" ); + break; + + case CELLULAR_RAT_NBIOT: + strcat( pRatList, "03" ); + break; + + case CELLULAR_RAT_GSM: + strcat( pRatList, "01" ); + break; + + default: + /* Configure RAT Searching Sequence to automatic. */ + retValue = false; + break; + } + + return retValue; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleInit( const CellularContext_t * pContext, + void ** ppModuleContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + bool status = false; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ppModuleContext == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Initialize the module context. */ + ( void ) memset( &cellularBg96Context, 0, sizeof( cellularModuleContext_t ) ); + + /* Create the mutex for DNS. */ + status = PlatformMutex_Create( &cellularBg96Context.dnsQueryMutex, false ); + + if( status == false ) + { + cellularStatus = CELLULAR_NO_MEMORY; + } + else + { + /* Create the queue for DNS. */ + cellularBg96Context.pktDnsQueue = xQueueCreate( 1, sizeof( cellularDnsQueryResult_t ) ); + + if( cellularBg96Context.pktDnsQueue == NULL ) + { + PlatformMutex_Destroy( &cellularBg96Context.dnsQueryMutex ); + cellularStatus = CELLULAR_NO_MEMORY; + } + else + { + *ppModuleContext = ( void * ) &cellularBg96Context; + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleCleanUp( const CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else + { + /* Delete DNS queue. */ + vQueueDelete( cellularBg96Context.pktDnsQueue ); + + /* Delete the mutex for DNS. */ + PlatformMutex_Destroy( &cellularBg96Context.dnsQueryMutex ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + CellularAtReq_t atReqGetWithResult = + { + NULL, + CELLULAR_AT_MULTI_WO_PREFIX, + NULL, + NULL, + NULL, + 0 + }; + char ratSelectCmd[ BG96_NWSCANSEQ_CMD_MAX_SIZE ] = "AT+QCFG=\"nwscanseq\","; + bool retAppendRat = true; + + if( pContext != NULL ) + { + /* Disable echo. */ + atReqGetWithResult.pAtCmd = "ATE0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetWithResult ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Disable DTR function. */ + atReqGetNoResult.pAtCmd = "AT&D0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + #ifndef CELLULAR_CONFIG_DISABLE_FLOW_CONTROL + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Enable RTS/CTS hardware flow control. */ + atReqGetNoResult.pAtCmd = "AT+IFC=2,2"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + #endif + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Setting URC output port. */ + #if defined( CELLULAR_BG96_URC_PORT_USBAT ) || defined( BG96_URC_PORT_USBAT ) + atReqGetNoResult.pAtCmd = "AT+QURCCFG=\"urcport\",\"usbat\""; + #else + atReqGetNoResult.pAtCmd = "AT+QURCCFG=\"urcport\",\"uart1\""; + #endif + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Configure Band configuration to all bands. */ + atReqGetNoResult.pAtCmd = "AT+QCFG=\"band\",f,400a0e189f,a0e189f"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Configure RAT(s) to be Searched to Automatic. */ + atReqGetNoResult.pAtCmd = "AT+QCFG=\"nwscanmode\",0,1"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Configure Network Category to be Searched under LTE RAT to LTE Cat M1 and Cat NB1. */ + atReqGetNoResult.pAtCmd = "AT+QCFG=\"iotopmode\",2,1"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT ); + configASSERT( retAppendRat == true ); + + #ifdef CELLULAR_CONFIG_DEFAULT_RAT_2 + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT_2 ); + configASSERT( retAppendRat == true ); + #endif + + #ifdef CELLULAR_CONFIG_DEFAULT_RAT_3 + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT_3 ); + configASSERT( retAppendRat == true ); + #endif + + strcat( ratSelectCmd, ",1" ); /* Take effect immediately. */ + atReqGetNoResult.pAtCmd = ratSelectCmd; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + atReqGetNoResult.pAtCmd = "AT+CFUN=1"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUrc( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + atReqGetNoResult.pAtCmd = "AT+COPS=3,2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CGREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CEREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CTZR=1"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.h b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.h new file mode 100644 index 0000000000..bd4917ba34 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96.h @@ -0,0 +1,106 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#ifndef __CELLULAR_BG96_H__ +#define __CELLULAR_BG96_H__ + +/* *INDENT-OFF* */ +#ifdef __cplusplus + extern "C" { +#endif +/* *INDENT-ON* */ + +/* AT Command timeout for PDN activation */ +#define PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS ( 150000UL ) + +/* AT Command timeout for PDN deactivation. */ +#define PDN_DEACTIVATION_PACKET_REQ_TIMEOUT_MS ( 40000UL ) + +/* AT Command timeout for Socket connection */ +#define SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ( 150000UL ) + +#define PACKET_REQ_TIMEOUT_MS ( 5000UL ) + +/* AT Command timeout for Socket disconnection */ +#define SOCKET_DISCONNECT_PACKET_REQ_TIMEOUT_MS ( 12000UL ) + +#define DATA_SEND_TIMEOUT_MS ( 50000UL ) +#define DATA_READ_TIMEOUT_MS ( 50000UL ) + +/** + * @brief DNS query result. + */ +typedef enum cellularDnsQueryResult +{ + CELLULAR_DNS_QUERY_SUCCESS, + CELLULAR_DNS_QUERY_FAILED, + CELLULAR_DNS_QUERY_MAX, + CELLULAR_DNS_QUERY_UNKNOWN +} cellularDnsQueryResult_t; + +typedef struct cellularModuleContext cellularModuleContext_t; + +/** + * @brief DNS query URC callback fucntion. + */ +typedef void ( * CellularDnsResultEventCallback_t )( cellularModuleContext_t * pModuleContext, + char * pDnsResult, + char * pDnsUsrData ); + +typedef struct cellularModuleContext +{ + /* 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. */ + CellularDnsResultEventCallback_t dnsEventCallback; + /* Forward declaration to declar the callback function prototype. */ + /* coverity[misra_c_2012_rule_1_1_violation]. */ +} cellularModuleContext_t; + +CellularPktStatus_t _Cellular_ParseSimstat( char * pInputStr, + CellularSimCardState_t * pSimState ); + +extern CellularAtParseTokenMap_t CellularUrcHandlerTable[]; +extern uint32_t CellularUrcHandlerTableSize; + +extern const char * CellularSrcTokenErrorTable[]; +extern uint32_t CellularSrcTokenErrorTableSize; + +extern const char * CellularSrcTokenSuccessTable[]; +extern uint32_t CellularSrcTokenSuccessTableSize; + +extern const char * CellularUrcTokenWoPrefixTable[]; +extern uint32_t CellularUrcTokenWoPrefixTableSize; + +/* *INDENT-OFF* */ +#ifdef __cplusplus + } +#endif +/* *INDENT-ON* */ + +#endif /* ifndef __CELLULAR_BG96_H__ */ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_api.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_api.c new file mode 100644 index 0000000000..57b641d8a0 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_api.c @@ -0,0 +1,3206 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* Standard includes. */ +#include +#include +#include +#include + +#include "cellular_platform.h" +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common_api.h" +#include "cellular_common.h" +#include "cellular_at_core.h" +#include "cellular_bg96.h" + +/*-----------------------------------------------------------*/ + +#define CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ( 32U ) +#define CELLULAR_AT_CMD_QUERY_DNS_MAX_SIZE ( 280U ) + +#define SIGNAL_QUALITY_POS_SYSMODE ( 1U ) +#define SIGNAL_QUALITY_POS_GSM_LTE_RSSI ( 2U ) +#define SIGNAL_QUALITY_POS_LTE_RSRP ( 3U ) +#define SIGNAL_QUALITY_POS_LTE_SINR ( 4U ) +#define SIGNAL_QUALITY_POS_LTE_RSRQ ( 5U ) +#define SIGNAL_QUALITY_SINR_MIN_VALUE ( -20 ) +#define SIGNAL_QUALITY_SINR_DIVISIBILITY_FACTOR ( 5 ) + +#define COPS_POS_MODE ( 1U ) +#define COPS_POS_FORMAT ( 2U ) +#define COPS_POS_MCC_MNC_OPER_NAME ( 3U ) +#define COPS_POS_RAT ( 4U ) + +/* AT command timeout for Get IP Address by Domain Name. */ +#define DNS_QUERY_TIMEOUT_MS ( 60000UL ) + +/* Length of HPLMN including RAT. */ +#define CRSM_HPLMN_RAT_LENGTH ( 9U ) + +/* Windows simulator implementation. */ +#if defined( _WIN32 ) || defined( _WIN64 ) + #define strtok_r strtok_s +#endif + +#define PRINTF_BINARY_PATTERN_INT4 "%c%c%c%c" +#define PRINTF_BYTE_TO_BINARY_INT4( i ) \ + ( ( ( ( i ) & 0x08UL ) != 0UL ) ? '1' : '0' ), \ + ( ( ( ( i ) & 0x04UL ) != 0UL ) ? '1' : '0' ), \ + ( ( ( ( i ) & 0x02UL ) != 0UL ) ? '1' : '0' ), \ + ( ( ( ( i ) & 0x01UL ) != 0UL ) ? '1' : '0' ) + +#define PRINTF_BINARY_PATTERN_INT8 \ + PRINTF_BINARY_PATTERN_INT4 PRINTF_BINARY_PATTERN_INT4 +#define PRINTF_BYTE_TO_BINARY_INT8( i ) \ + PRINTF_BYTE_TO_BINARY_INT4( ( i ) >> 4 ), PRINTF_BYTE_TO_BINARY_INT4( i ) + +#define QPSMS_POS_MODE ( 0U ) +#define QPSMS_POS_RAU ( 1U ) +#define QPSMS_POS_RDY_TIMER ( 2U ) +#define QPSMS_POS_TAU ( 3U ) +#define QPSMS_POS_ACTIVE_TIME ( 4U ) + +#define CELLULAR_PDN_STATUS_POS_CONTEXT_ID ( 0U ) +#define CELLULAR_PDN_STATUS_POS_CONTEXT_STATE ( 1U ) +#define CELLULAR_PDN_STATUS_POS_CONTEXT_TYPE ( 2U ) +#define CELLULAR_PDN_STATUS_POS_IP_ADDRESS ( 3U ) + +#define RAT_PRIOIRTY_STRING_LENGTH ( 2U ) +#define RAT_PRIOIRTY_LIST_LENGTH ( 3U ) + +#define INVALID_PDN_INDEX ( 0xFFU ) + +#define DATA_PREFIX_STRING "+QIRD:" +#define DATA_PREFIX_STRING_LENGTH ( 6U ) +#define DATA_PREFIX_STRING_CHANGELINE_LENGTH ( 2U ) /* The length of the change line "\r\n". */ + +#define MAX_QIRD_STRING_PREFIX_STRING ( 14U ) /* The max data prefix string is "+QIRD: 1460\r\n" */ + +/*-----------------------------------------------------------*/ + +/** + * @brief Parameters involved in receiving data through sockets + */ +typedef struct _socketDataRecv +{ + uint32_t * pDataLen; + uint8_t * pData; + CellularSocketAddress_t * pRemoteSocketAddress; +} _socketDataRecv_t; + +/*-----------------------------------------------------------*/ + +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ); +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t controlSignalStrengthIndication( CellularContext_t * pContext, + bool enable ); +static CellularPktStatus_t _Cellular_RecvFuncGetIccid( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularPktStatus_t _Cellular_RecvFuncGetImsi( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static bool _checkCrsmMemoryStatus( const char * pToken ); +static bool _checkCrsmReadStatus( const char * pToken ); +static bool _parseHplmn( char * pToken, + void * pData ); +static CellularPktStatus_t _Cellular_RecvFuncGetHplmn( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularPktStatus_t _Cellular_RecvFuncGetSimCardStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularSimCardLockState_t _getSimLockState( char * pToken ); +static CellularPktStatus_t _Cellular_RecvFuncGetSimLockStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularATError_t parsePdnStatusContextId( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ); +static CellularATError_t parsePdnStatusContextState( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ); +static CellularATError_t parsePdnStatusContextType( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ); +static CellularATError_t getPdnStatusParseToken( char * pToken, + uint8_t tokenIndex, + CellularPdnStatus_t * pPdnStatusBuffers ); +static CellularATError_t getPdnStatusParseLine( char * pRespLine, + CellularPdnStatus_t * pPdnStatusBuffers ); +static CellularPktStatus_t _Cellular_RecvFuncGetPdnStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t buildSocketConnect( CellularSocketHandle_t socketHandle, + char * pCmdBuf ); +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ); +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularATError_t parseQpsmsMode( char * pToken, + CellularPsmSettings_t * pPsmSettings ); +static CellularATError_t parseQpsmsRau( char * pToken, + CellularPsmSettings_t * pPsmSettings ); +static CellularATError_t parseQpsmsRdyTimer( char * pToken, + CellularPsmSettings_t * pPsmSettings ); +static CellularATError_t parseQpsmsTau( char * pToken, + CellularPsmSettings_t * pPsmSettings ); +static CellularATError_t parseQpsmsActiveTime( char * pToken, + CellularPsmSettings_t * pPsmSettings ); +static CellularATError_t parseGetPsmToken( char * pToken, + uint8_t tokenIndex, + CellularPsmSettings_t * pPsmSettings ); +static CellularRat_t convertRatPriority( char * pRatString ); +static CellularPktStatus_t _Cellular_RecvFuncGetRatPriority( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularPktStatus_t _Cellular_RecvFuncGetPsmSettings( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ); +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ); +static CellularError_t registerDnsEventCallback( cellularModuleContext_t * pModuleContext, + CellularDnsResultEventCallback_t dnsEventCallback, + char * pDnsUsrData ); +static void _dnsResultCallback( cellularModuleContext_t * pModuleContext, + char * pDnsResult, + char * pDnsUsrData ); +static uint32_t appendBinaryPattern( char * cmdBuf, + uint32_t cmdLen, + uint32_t value, + bool endOfString ); +static CellularPktStatus_t socketSendDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t * pBytesRead ); + +/*-----------------------------------------------------------*/ + +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ) +{ + char * pToken = NULL, * pTmpQcsqPayload = pQcsqPayload; + int32_t tempValue = 0; + bool parseStatus = true; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( ( pSignalInfo == NULL ) || ( pQcsqPayload == NULL ) ) + { + LogError( ( "_parseSignalQuality: Invalid Input Parameters" ) ); + parseStatus = false; + } + + if( ( parseStatus == true ) && ( Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ) == CELLULAR_AT_SUCCESS ) ) + { + if( ( strcmp( pToken, "GSM" ) != 0 ) && + ( strcmp( pToken, "CAT-M1" ) != 0 ) && + ( strcmp( pToken, "CAT-NB1" ) != 0 ) ) + { + parseStatus = false; + } + } + else + { + LogDebug( ( "_parseSignalQuality: No Valid RAT in QCSQ Response" ) ); + parseStatus = false; + } + + if( ( parseStatus == true ) && ( Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ) == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rssi = ( int16_t ) tempValue; + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSSI. Token %s", pToken ) ); + parseStatus = false; + } + } + else + { + parseStatus = false; + } + + if( ( parseStatus == true ) && ( Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ) == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rsrp = ( int16_t ) tempValue; + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSRP. Token %s", pToken ) ); + parseStatus = false; + } + } + else + { + parseStatus = false; + } + + if( ( parseStatus == true ) && ( Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ) == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* SINR is reported as an integer value ranging from 0 to 250 representing 1/5 of a dB. + * Value 0 correspond to -20 dBm and 250 corresponds to +30 dBm. */ + pSignalInfo->sinr = ( int16_t ) ( SIGNAL_QUALITY_SINR_MIN_VALUE + ( ( tempValue ) / ( SIGNAL_QUALITY_SINR_DIVISIBILITY_FACTOR ) ) ); + } + else + { + LogError( ( "_parseSignalQuality: Error in processing SINR. pToken %s", pToken ) ); + parseStatus = false; + } + } + else + { + parseStatus = false; + } + + if( ( parseStatus == true ) && ( Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ) == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rsrq = ( int16_t ) tempValue; + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSRQ. Token %s", pToken ) ); + parseStatus = false; + } + } + else + { + parseStatus = false; + } + + return parseStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularSignalInfo_t * pSignalInfo = ( CellularSignalInfo_t * ) pData; + bool parseStatus = true; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSignalInfo == NULL ) || ( dataLen != sizeof( CellularSignalInfo_t ) ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSignalInfo: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + parseStatus = _parseSignalQuality( pInputLine, pSignalInfo ); + + if( parseStatus != true ) + { + pSignalInfo->rssi = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->bars = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t controlSignalStrengthIndication( CellularContext_t * pContext, + bool enable ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + uint8_t enable_value = 0; + CellularAtReq_t atReqControlSignalStrengthIndication = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + if( enable == true ) + { + enable_value = 1; + } + else + { + enable_value = 0; + } + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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, "AT+QINDCFG=\"csq\",%u", enable_value ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqControlSignalStrengthIndication ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetIccid( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pRespLine = NULL; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) ) + { + LogError( ( "getIccid: Response in invalid " ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pRespLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Removing QCCID Prefix in AT Response. */ + atCoreStatus = Cellular_ATRemovePrefix( &pRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Storing the ICCID value in the AT Response. */ + if( strlen( pRespLine ) < ( ( size_t ) CELLULAR_ICCID_MAX_SIZE + 1U ) ) + { + ( void ) strncpy( pData, pRespLine, dataLen ); + } + else + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetImsi( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pRespLine = NULL; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) ) + { + LogError( ( "getImsi: Response in invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + /* Removing all the Spaces in the AT Response. */ + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( strlen( pRespLine ) < ( CELLULAR_IMSI_MAX_SIZE + 1U ) ) + { + ( void ) strncpy( ( char * ) pData, pRespLine, dataLen ); + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static bool _checkCrsmMemoryStatus( const char * pToken ) +{ + bool memoryStatus = true; + + if( pToken == NULL ) + { + LogError( ( "Input Parameter NULL" ) ); + memoryStatus = false; + } + + if( memoryStatus ) + { + /* checking the value sw2 in AT command response for memory problem during CRSM read. + * Refer 3GPP Spec TS 51.011 Section 9.4. */ + if( strcmp( pToken, "64" ) == 0 ) + { + LogError( ( "_checkCrsmMemoryStatus: Error in Processing HPLMN: CRSM Memory Error" ) ); + memoryStatus = false; + } + } + + return memoryStatus; +} + +/*-----------------------------------------------------------*/ + +static bool _checkCrsmReadStatus( const char * pToken ) +{ + bool readStatus = true; + + if( pToken == NULL ) + { + LogError( ( "Input Parameter NULL" ) ); + readStatus = false; + } + + if( readStatus ) + { + /* checking the parameter sw1 in AT command response for successful CRSM read. + * Refer 3GPP Spec TS 51.011 Section 9.4. */ + if( ( strcmp( pToken, "144" ) != 0 ) && + ( strcmp( pToken, "145" ) != 0 ) && + ( strcmp( pToken, "146" ) != 0 ) ) + { + LogError( ( "_checkCrsmReadStatus: Error in Processing HPLMN: CRSM Read Error" ) ); + readStatus = false; + } + } + + return readStatus; +} + +/*-----------------------------------------------------------*/ + +static bool _parseHplmn( char * pToken, + void * pData ) +{ + bool parseStatus = true; + CellularPlmnInfo_t * plmn = ( CellularPlmnInfo_t * ) pData; + + if( ( pToken == NULL ) || ( pData == NULL ) ) + { + LogError( ( "Input Parameter NULL" ) ); + parseStatus = false; + } + + if( parseStatus == true ) + { + /* Checking if the very first HPLMN entry in AT command Response is valid*/ + if( ( strlen( pToken ) < ( CRSM_HPLMN_RAT_LENGTH ) ) || ( strncmp( pToken, "FFFFFF", 6 ) == 0 ) ) + { + LogError( ( "_parseHplmn: Error in Processing HPLMN: Invalid Token %s", pToken ) ); + parseStatus = false; + } + } + + if( parseStatus == true ) + { + /* Returning only the very first HPLMN present in EFHPLMNwACT in SIM. + * EF-HPLMNwACT can contain a maximum of 10 HPLMN entries in decreasing order of priority. + * In this implementation, returning the very first HPLMN is the PLMN priority list. */ + /* Refer TS 51.011 Section 10.3.37 for encoding. */ + plmn->mcc[ 0 ] = pToken[ 1 ]; + plmn->mcc[ 1 ] = pToken[ 0 ]; + plmn->mcc[ 2 ] = pToken[ 3 ]; + plmn->mnc[ 0 ] = pToken[ 5 ]; + plmn->mnc[ 1 ] = pToken[ 4 ]; + + if( pToken[ 2 ] != 'F' ) + { + plmn->mnc[ 2 ] = pToken[ 2 ]; + plmn->mnc[ 3 ] = '\0'; + } + else + { + plmn->mnc[ 2 ] = '\0'; + } + } + + return parseStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetHplmn( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + bool parseStatus = true; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pCrsmResponse = NULL, * pToken = NULL; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) || + ( pData == NULL ) || ( dataLen != sizeof( CellularPlmnInfo_t ) ) ) + { + LogError( ( "GetHplmn: Response is invalid " ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pCrsmResponse = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pCrsmResponse ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Removing the CRSM prefix in AT Response. */ + atCoreStatus = Cellular_ATRemovePrefix( &pCrsmResponse ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Removing All quotes in the AT Response. */ + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pCrsmResponse ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Getting the next token separated by comma in At Response*/ + atCoreStatus = Cellular_ATGetNextTok( &pCrsmResponse, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + parseStatus = _checkCrsmReadStatus( pToken ); + + if( !parseStatus ) + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pCrsmResponse, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + parseStatus = _checkCrsmMemoryStatus( pToken ); + + if( !parseStatus ) + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pCrsmResponse, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + parseStatus = _parseHplmn( pToken, pData ); + + if( !parseStatus ) + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSimCardStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + const char * pTokenPtr = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularSimCardState_t * pSimCardState = ( CellularSimCardState_t * ) pData; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSimStatus: response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pData == NULL ) || ( dataLen != sizeof( CellularSimCardState_t ) ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pInputLine ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + /* remove the token prefix. */ + pTokenPtr = strtok_r( pInputLine, ":", &pInputLine ); + + /* check the token prefix. */ + if( pTokenPtr == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_RESPONSE; + } + else + { + pktStatus = _Cellular_ParseSimstat( pInputLine, pSimCardState ); + } + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularSimCardLockState_t _getSimLockState( char * pToken ) +{ + CellularSimCardLockState_t tempState = CELLULAR_SIM_CARD_LOCK_UNKNOWN; + + if( pToken != NULL ) + { + if( strcmp( pToken, "READY" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_READY; + } + else if( strcmp( pToken, "SIM PIN" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PIN; + } + else if( strcmp( pToken, "SIM PUK" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PUK; + } + else if( strcmp( pToken, "SIM PIN2" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PIN2; + } + else if( strcmp( pToken, "SIM PUK2" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PUK2; + } + else if( strcmp( pToken, "PH-NET PIN" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PH_NET_PIN; + } + else if( strcmp( pToken, "PH-NET PUK" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PH_NET_PUK; + } + else if( strcmp( pToken, "PH-NETSUB PIN" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PH_NETSUB_PIN; + } + else if( strcmp( pToken, "PH-NETSUB PUK" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_PH_NETSUB_PUK; + } + else if( strcmp( pToken, "PH-SP PIN" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_SP_PIN; + } + else if( strcmp( pToken, "PH-SP PUK" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_SP_PUK; + } + else if( strcmp( pToken, "PH-CORP PIN" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_CORP_PIN; + } + else if( strcmp( pToken, "PH-CORP PUK" ) == 0 ) + { + tempState = CELLULAR_SIM_CARD_CORP_PUK; + } + else + { + LogError( ( "Unknown SIM Lock State %s", pToken ) ); + } + } + + return tempState; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSimLockStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pToken = NULL, * pInputStr = NULL; + CellularSimCardLockState_t * pSimLockState = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) || + ( pData == NULL ) || ( dataLen != sizeof( CellularSimCardLockState_t ) ) ) + { + LogError( ( " Get SIM lock State: Response data is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputStr = pAtResp->pItm->pLine; + pSimLockState = ( CellularSimCardLockState_t * ) pData; + + if( strlen( pInputStr ) == 0U ) + { + LogError( ( "Get SIM lock State: Input data is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + } + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputStr ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemovePrefix( &pInputStr ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + LogDebug( ( "SIM Lock State: %s", pToken ) ); + *pSimLockState = _getSimLockState( pToken ); + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextId( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MIN ) && + ( tempValue <= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MAX ) ) + { + pPdnStatusBuffers->contextId = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in Processing Context Id. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextState( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pPdnStatusBuffers->state = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing PDN Status Buffer state. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextType( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue < ( int32_t ) CELLULAR_PDN_CONTEXT_TYPE_MAX ) ) + { + /* Variable "tempValue" is ensured that it is valid and within + * a valid range. Hence, assigning the value of the variable to + * pdnContextType with a enum cast. */ + /* coverity[misra_c_2012_rule_10_5_violation] */ + pPdnStatusBuffers->pdnContextType = ( CellularPdnContextType_t ) tempValue; + } + else + { + LogError( ( "Error in processing PDN State Buffer Status. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getPdnStatusParseToken( char * pToken, + uint8_t tokenIndex, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + switch( tokenIndex ) + { + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_ID ): + LogDebug( ( "Context Id: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextId( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_STATE ): + LogDebug( ( "Context State: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextState( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_TYPE ): + LogDebug( ( "Context Type: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextType( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_IP_ADDRESS ): + LogDebug( ( "IP address: %s", pToken ) ); + ( void ) memcpy( ( void * ) pPdnStatusBuffers->ipAddress.ipAddress, + ( void * ) pToken, CELLULAR_IP_ADDRESS_MAX_SIZE + 1U ); + + if( pPdnStatusBuffers->pdnContextType == CELLULAR_PDN_CONTEXT_IPV4 ) + { + pPdnStatusBuffers->ipAddress.ipAddressType = CELLULAR_IP_ADDRESS_V4; + } + else if( pPdnStatusBuffers->pdnContextType == CELLULAR_PDN_CONTEXT_IPV6 ) + { + pPdnStatusBuffers->ipAddress.ipAddressType = CELLULAR_IP_ADDRESS_V6; + } + else + { + LogError( ( "Unknown pdnContextType %d", pPdnStatusBuffers->pdnContextType ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + + break; + + default: + LogError( ( "Unknown token in getPdnStatusParseToken %s %d", + pToken, tokenIndex ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getPdnStatusParseLine( char * pRespLine, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + char * pToken = NULL; + char * pLocalRespLine = pRespLine; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + uint8_t tokenIndex = 0; + + atCoreStatus = Cellular_ATRemovePrefix( &pLocalRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pLocalRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalRespLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( ( pToken != NULL ) && ( atCoreStatus == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = getPdnStatusParseToken( pToken, tokenIndex, pPdnStatusBuffers ); + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + LogInfo( ( "getPdnStatusParseToken %s index %d failed", pToken, tokenIndex ) ); + } + + tokenIndex++; + + if( Cellular_ATGetNextTok( &pLocalRespLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetPdnStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pRespLine = NULL; + CellularPdnStatus_t * pPdnStatusBuffers = ( CellularPdnStatus_t * ) pData; + uint8_t numStatusBuffers = ( uint8_t ) dataLen; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const CellularATCommandLine_t * pCommnadItem = NULL; + + if( pContext == NULL ) + { + LogError( ( "GetPdnStatus: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) ) + { + LogError( ( "GetPdnStatus: Response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pPdnStatusBuffers == NULL ) || ( numStatusBuffers < 1U ) ) + { + LogError( ( "GetPdnStatus: PDN Status bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetPdnStatus: no activated PDN" ) ); + pPdnStatusBuffers[ 0 ].contextId = INVALID_PDN_INDEX; + pktStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + pCommnadItem = pAtResp->pItm; + + while( ( numStatusBuffers != 0U ) && ( pCommnadItem != NULL ) ) + { + pRespLine = pCommnadItem->pLine; + atCoreStatus = getPdnStatusParseLine( pRespLine, pPdnStatusBuffers ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "getPdnStatusParseLine parse %s failed", pRespLine ) ); + break; + } + + pPdnStatusBuffers++; + numStatusBuffers--; + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t buildSocketConnect( CellularSocketHandle_t socketHandle, + char * pCmdBuf ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + char protocol[ 15 ]; + + if( pCmdBuf == NULL ) + { + LogError( ( "buildSocketConnect: Invalid command buffer" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( socketHandle->socketProtocol == CELLULAR_SOCKET_PROTOCOL_TCP ) + { + ( void ) strcpy( protocol, "TCP" ); + } + else + { + ( void ) strcpy( protocol, "UDP SERVICE" ); + } + + /* 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( pCmdBuf, CELLULAR_AT_CMD_MAX_SIZE, + "%s%d,%ld,\"%s\",\"%s\",%d,%d,%d", + "AT+QIOPEN=", + socketHandle->contextId, + socketHandle->socketId, + protocol, + socketHandle->remoteSocketAddress.ipAddress.ipAddress, + socketHandle->remoteSocketAddress.port, + socketHandle->localPort, + socketHandle->dataMode ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const char * pInputLine = NULL; + uint32_t dataLenToCopy = 0; + + /* Check if the received data size is greater than the output buffer size. */ + if( *pDataRecv->pDataLen > outBufSize ) + { + LogError( ( "Data is turncated, received data length %d, out buffer size %d", + *pDataRecv->pDataLen, outBufSize ) ); + dataLenToCopy = outBufSize; + *pDataRecv->pDataLen = outBufSize; + } + else + { + dataLenToCopy = *pDataRecv->pDataLen; + } + + /* Data is stored in the next intermediate response. */ + if( pAtResp->pItm->pNext != NULL ) + { + pInputLine = pAtResp->pItm->pNext->pLine; + + if( ( pInputLine != NULL ) && ( dataLenToCopy > 0U ) ) + { + /* Copy the data to the out buffer. */ + ( void ) memcpy( ( void * ) pDataRecv->pData, ( const void * ) pInputLine, dataLenToCopy ); + } + else + { + LogError( ( "Receive Data: Data pointer NULL" ) ); + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + } + else if( *pDataRecv->pDataLen == 0U ) + { + /* Receive command success but no data. */ + LogDebug( ( "Receive Data: no data" ) ); + } + else + { + LogError( ( "Receive Data: Intermediate response empty" ) ); + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char * pInputLine = NULL, * pToken = NULL; + const _socketDataRecv_t * pDataRecv = ( _socketDataRecv_t * ) pData; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "Receive Data: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "Receive Data: response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pDataRecv == NULL ) || ( pDataRecv->pData == NULL ) || ( pDataRecv->pDataLen == NULL ) ) + { + LogError( ( "Receive Data: Bad param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + /* parse the datalen. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( int32_t ) 0 ) && ( tempValue < ( ( int32_t ) CELLULAR_MAX_RECV_DATA_LEN + 1 ) ) ) + { + *pDataRecv->pDataLen = ( uint32_t ) tempValue; + } + else + { + LogError( ( "Error in Data Length Processing: No valid digit found. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Process the data buffer. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = getDataFromResp( pAtResp, pDataRecv, dataLen ); + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseQpsmsMode( char * pToken, + CellularPsmSettings_t * pPsmSettings ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pPsmSettings->mode = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing mode. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseQpsmsRau( char * pToken, + CellularPsmSettings_t * pPsmSettings ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( tempValue >= 0 ) + { + pPsmSettings->periodicRauValue = ( uint32_t ) tempValue; + } + else + { + LogError( ( "Error in processing Periodic Processing RAU value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseQpsmsRdyTimer( char * pToken, + CellularPsmSettings_t * pPsmSettings ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( tempValue >= 0 ) + { + pPsmSettings->gprsReadyTimer = ( uint32_t ) tempValue; + } + else + { + LogError( ( "Error in processing Periodic Processing GPRS Ready Timer value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseQpsmsTau( char * pToken, + CellularPsmSettings_t * pPsmSettings ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( tempValue >= 0 ) + { + pPsmSettings->periodicTauValue = ( uint32_t ) tempValue; + } + else + { + LogError( ( "Error in processing Periodic TAU value value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseQpsmsActiveTime( char * pToken, + CellularPsmSettings_t * pPsmSettings ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( tempValue >= 0 ) + { + pPsmSettings->activeTimeValue = ( uint32_t ) tempValue; + } + else + { + LogError( ( "Error in processing Periodic Processing Active time value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseGetPsmToken( char * pToken, + uint8_t tokenIndex, + CellularPsmSettings_t * pPsmSettings ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + switch( tokenIndex ) + { + case QPSMS_POS_MODE: + atCoreStatus = parseQpsmsMode( pToken, pPsmSettings ); + break; + + case QPSMS_POS_RAU: + atCoreStatus = parseQpsmsRau( pToken, pPsmSettings ); + break; + + case QPSMS_POS_RDY_TIMER: + atCoreStatus = parseQpsmsRdyTimer( pToken, pPsmSettings ); + break; + + case QPSMS_POS_TAU: + atCoreStatus = parseQpsmsTau( pToken, pPsmSettings ); + break; + + case QPSMS_POS_ACTIVE_TIME: + atCoreStatus = parseQpsmsActiveTime( pToken, pPsmSettings ); + break; + + default: + LogDebug( ( "Unknown Parameter Position in AT+QPSMS Response" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularRat_t convertRatPriority( char * pRatString ) +{ + CellularRat_t retRat = CELLULAR_RAT_INVALID; + + if( strncmp( pRatString, "01", RAT_PRIOIRTY_STRING_LENGTH ) == 0 ) + { + retRat = CELLULAR_RAT_GSM; + } + else if( strncmp( pRatString, "02", RAT_PRIOIRTY_STRING_LENGTH ) == 0 ) + { + retRat = CELLULAR_RAT_CATM1; + } + else if( strncmp( pRatString, "03", RAT_PRIOIRTY_STRING_LENGTH ) == 0 ) + { + retRat = CELLULAR_RAT_NBIOT; + } + else + { + LogDebug( ( "Invalid RAT string %s", pRatString ) ); + } + + return retRat; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetRatPriority( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularRat_t * pRatPriorities = NULL; + char pTempString[ RAT_PRIOIRTY_STRING_LENGTH + 1 ] = { "\0" }; /* The return RAT has two chars plus NULL char. */ + uint32_t ratIndex = 0; + uint32_t maxRatPriorityLength = ( dataLen > RAT_PRIOIRTY_LIST_LENGTH ? RAT_PRIOIRTY_LIST_LENGTH : dataLen ); + + if( pContext == NULL ) + { + LogError( ( "GetRatPriority: Invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) || ( dataLen == 0U ) ) + { + LogError( ( "GetRatPriority: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + pRatPriorities = ( CellularRat_t * ) pData; + + /* Response string +QCFG:"nwscanseq",020301 => pToken : +QCFG:"nwscanseq", pInputLine : 020301. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + /* Response string 020301 => pToken : 020301, pInputLine : NULL. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( strlen( pToken ) != ( RAT_PRIOIRTY_STRING_LENGTH * RAT_PRIOIRTY_LIST_LENGTH ) ) + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + memset( pRatPriorities, CELLULAR_RAT_INVALID, dataLen ); + + for( ratIndex = 0; ratIndex < maxRatPriorityLength; ratIndex++ ) + { + memcpy( pTempString, &pToken[ ratIndex * RAT_PRIOIRTY_STRING_LENGTH ], RAT_PRIOIRTY_STRING_LENGTH ); + pRatPriorities[ ratIndex ] = convertRatPriority( pTempString ); + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetPsmSettings( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL, * pToken = NULL; + uint8_t tokenIndex = 0; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPsmSettings_t * pPsmSettings = NULL; + + if( pContext == NULL ) + { + LogError( ( "GetPsmSettings: Invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) || ( dataLen != sizeof( CellularPsmSettings_t ) ) ) + { + LogError( ( "GetPsmSettings: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + pPsmSettings = ( CellularPsmSettings_t * ) pData; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( pToken != NULL ) + { + if( tokenIndex == 0 ) + { + atCoreStatus = parseGetPsmToken( pToken, tokenIndex, pPsmSettings ); + } + else + { + parseGetPsmToken( pToken, tokenIndex, pPsmSettings ); + } + + tokenIndex++; + + if( *pInputLine == ',' ) + { + *pInputLine = '\0'; + pToken = pInputLine; + *pToken = '\0'; + pInputLine = &pInputLine[ 1 ]; + } + else if( Cellular_ATGetNextTok( &pInputLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + else + { + /* Empty Else MISRA 15.7 */ + } + } + } + + LogDebug( ( "PSM setting: mode: %d, RAU: %d, RDY_Timer: %d, TAU: %d, Active_time: %d", + pPsmSettings->mode, + pPsmSettings->periodicRauValue, + pPsmSettings->gprsReadyTimer, + pPsmSettings->periodicTauValue, + pPsmSettings->activeTimeValue ) ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ) +{ + char * pDataStart = NULL; + uint32_t prefixLineLength = 0U; + int32_t tempValue = 0; + CellularATError_t atResult = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t i = 0; + char pLocalLine[ MAX_QIRD_STRING_PREFIX_STRING + 1 ] = "\0"; + uint32_t localLineLength = MAX_QIRD_STRING_PREFIX_STRING > lineLength ? lineLength : MAX_QIRD_STRING_PREFIX_STRING; + + ( void ) pCallbackContext; + + if( ( pLine == NULL ) || ( ppDataStart == NULL ) || ( pDataLength == NULL ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + /* Check if the message is a data response. */ + if( strncmp( pLine, DATA_PREFIX_STRING, DATA_PREFIX_STRING_LENGTH ) == 0 ) + { + strncpy( pLocalLine, pLine, MAX_QIRD_STRING_PREFIX_STRING ); + pLocalLine[ MAX_QIRD_STRING_PREFIX_STRING ] = '\0'; + pDataStart = pLocalLine; + + /* Add a '\0' char at the end of the line. */ + for( i = 0; i < localLineLength; i++ ) + { + if( ( pDataStart[ i ] == '\r' ) || ( pDataStart[ i ] == '\n' ) ) + { + pDataStart[ i ] = '\0'; + prefixLineLength = i; + break; + } + } + + if( i == localLineLength ) + { + LogDebug( ( "Data prefix invalid line : %s", pLocalLine ) ); + pDataStart = NULL; + } + } + + if( pDataStart != NULL ) + { + atResult = Cellular_ATStrtoi( &pDataStart[ DATA_PREFIX_STRING_LENGTH ], 10, &tempValue ); + + if( ( atResult == CELLULAR_AT_SUCCESS ) && ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) CELLULAR_MAX_RECV_DATA_LEN ) ) + { + if( ( prefixLineLength + DATA_PREFIX_STRING_CHANGELINE_LENGTH ) > lineLength ) + { + /* More data is required. */ + *pDataLength = 0; + pDataStart = NULL; + pktStatus = CELLULAR_PKT_STATUS_SIZE_MISMATCH; + } + else + { + pDataStart = &pLine[ prefixLineLength ]; + pDataStart[ 0 ] = '\0'; + pDataStart = &pDataStart[ DATA_PREFIX_STRING_CHANGELINE_LENGTH ]; + *pDataLength = ( uint32_t ) tempValue; + } + + LogDebug( ( "DataLength %p at pktIo = %d", pDataStart, *pDataLength ) ); + } + else + { + *pDataLength = 0; + pDataStart = NULL; + LogError( ( "Data response received with wrong size" ) ); + } + } + + *ppDataStart = pDataStart; + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pRemoteSocketAddress == NULL ) || ( socketHandle == NULL ) ) + { + LogError( ( "storeAccessModeAndAddress: Invalid socket address" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_ALLOCATED ) + { + LogError( ( "storeAccessModeAndAddress, bad socket state %d", + 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; + socketHandle->dataMode = dataAccessMode; + socketHandle->remoteSocketAddress.ipAddress.ipAddressType = + pRemoteSocketAddress->ipAddress.ipAddressType; + ( void ) strncpy( socketHandle->remoteSocketAddress.ipAddress.ipAddress, + pRemoteSocketAddress->ipAddress.ipAddress, + CELLULAR_IP_ADDRESS_MAX_SIZE + 1U ); + } + + return cellularStatus; +} + + +/*-----------------------------------------------------------*/ + +static CellularError_t registerDnsEventCallback( cellularModuleContext_t * pModuleContext, + CellularDnsResultEventCallback_t dnsEventCallback, + char * pDnsUsrData ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pModuleContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else + { + pModuleContext->dnsEventCallback = dnsEventCallback; + pModuleContext->pDnsUsrData = pDnsUsrData; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static void _dnsResultCallback( cellularModuleContext_t * pModuleContext, + char * pDnsResult, + char * pDnsUsrData ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pToken = NULL, * pDnsResultStr = pDnsResult; + int32_t dnsResultNumber = 0; + cellularDnsQueryResult_t dnsQueryResult = CELLULAR_DNS_QUERY_UNKNOWN; + + if( pModuleContext != NULL ) + { + if( pModuleContext->dnsResultNumber == ( uint8_t ) 0 ) + { + atCoreStatus = Cellular_ATGetNextTok( &pDnsResultStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pDnsResultStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &dnsResultNumber ); + + if( ( atCoreStatus == CELLULAR_AT_SUCCESS ) && ( dnsResultNumber >= 0 ) && + ( dnsResultNumber <= ( int32_t ) UINT8_MAX ) ) + { + pModuleContext->dnsResultNumber = ( uint8_t ) dnsResultNumber; + } + else + { + LogDebug( ( "_dnsResultCallback convert string failed %s", pToken ) ); + } + } + } + else if( ( pModuleContext->dnsIndex < pModuleContext->dnsResultNumber ) && ( pDnsResultStr != NULL ) ) + { + pModuleContext->dnsIndex = pModuleContext->dnsIndex + ( uint8_t ) 1; + + ( void ) strncpy( pDnsUsrData, pDnsResultStr, CELLULAR_IP_ADDRESS_MAX_SIZE ); + ( void ) registerDnsEventCallback( pModuleContext, NULL, NULL ); + dnsQueryResult = CELLULAR_DNS_QUERY_SUCCESS; + + if( xQueueSend( pModuleContext->pktDnsQueue, &dnsQueryResult, ( TickType_t ) 0 ) != pdPASS ) + { + LogDebug( ( "_dnsResultCallback sends pktDnsQueue fail" ) ); + } + } + else + { + LogDebug( ( "_dnsResultCallback spurious DNS response" ) ); + } + } +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetRatPriority( CellularHandle_t cellularHandle, + const CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + uint8_t i = 0; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSetRatPriority = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /** Using AT+QCFG="nwscanseq",, to set the RAT priorities while searching. + * can take value 01 for GSM, 02 for CAT M1 and 03 for CAT NB1. + * can take value 0 for take effect after reboot and 1 for take effect immediately. + * If < effect > is omitted, RAT priority takes effect immediately. + * e.g. AT+QCFG="nwscanseq",020301,1 for decreasing RAT Priorities CAT M1, CAT NB1, GSM to take effect immediately. */ + ( void ) strcpy( cmdBuf, "AT+QCFG=\"nwscanseq\"," ); + + while( i < ratPrioritiesLength ) + { + if( pRatPriorities[ i ] == CELLULAR_RAT_GSM ) + { + ( void ) strcat( cmdBuf, "01" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_CATM1 ) + { + ( void ) strcat( cmdBuf, "02" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_NBIOT ) + { + ( void ) strcat( cmdBuf, "03" ); + } + else + { + cellularStatus = CELLULAR_BAD_PARAMETER; + break; + } + + i++; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetRatPriority ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRatPriority( CellularHandle_t cellularHandle, + CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength, + uint8_t * pReceiveRatPrioritesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t ratIndex = 0; + + CellularAtReq_t atReqSetRatPriority = + { + "AT+QCFG=\"nwscanseq\"", + CELLULAR_AT_WITH_PREFIX, + "+QCFG", + _Cellular_RecvFuncGetRatPriority, + pRatPriorities, + ( uint16_t ) ratPrioritiesLength, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) || + ( pReceiveRatPrioritesLength == NULL ) ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetRatPriority ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + for( ratIndex = 0; ratIndex < ratPrioritiesLength; ratIndex++ ) + { + if( pRatPriorities[ ratIndex ] == CELLULAR_RAT_INVALID ) + { + break; + } + } + + *pReceiveRatPrioritesLength = ratIndex; + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetDns( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pDnsServerAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSetDns = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pDnsServerAddress == NULL ) + { + LogError( ( "Cellular_SetDns: Invalid parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_IsValidPdn( contextId ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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_MAX_SIZE, "%s%d,\"%s\"", "AT+QIDNSCFG=", contextId, pDnsServerAddress ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetDns ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SetDns: couldn't set the DNS, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetPsmSettings( CellularHandle_t cellularHandle, + CellularPsmSettings_t * pPsmSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetPsm = + { + "AT+QPSMS?", + CELLULAR_AT_WITH_PREFIX, + "+QPSMS", + _Cellular_RecvFuncGetPsmSettings, + pPsmSettings, + sizeof( CellularPsmSettings_t ), + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pPsmSettings == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* initialize the data. */ + ( void ) memset( pPsmSettings, 0, sizeof( CellularPsmSettings_t ) ); + pPsmSettings->mode = 0xFF; + + /* we should always query the PSMsettings from the network. */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetPsm ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_GetPsmSettings: couldn't retrieve PSM settings" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static uint32_t appendBinaryPattern( char * cmdBuf, + uint32_t cmdLen, + uint32_t value, + bool endOfString ) +{ + uint32_t retLen = 0; + + if( cmdBuf != NULL ) + { + if( value != 0U ) + { + /* 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, cmdLen, "\"" PRINTF_BINARY_PATTERN_INT8 "\"%c", + PRINTF_BYTE_TO_BINARY_INT8( value ), endOfString ? '\0' : ',' ); + } + else + { + /* 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, cmdLen, "%c", endOfString ? '\0' : ',' ); + } + + retLen = strlen( cmdBuf ); + } + + return retLen; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketSendDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t * pBytesRead ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + if( ( pLine == NULL ) || ( pBytesRead == NULL ) ) + { + LogError( ( "socketSendDataPrefix: pLine is invalid or pBytesRead is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pCallbackContext != NULL ) + { + LogError( ( "socketSendDataPrefix: pCallbackContext is not NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( *pBytesRead != 2U ) + { + LogDebug( ( "socketSendDataPrefix: pBytesRead %u %s is not 1", *pBytesRead, pLine ) ); + } + else + { + /* After the data prefix, there should not be any data in stream. + * Cellular commmon processes AT command in lines. Add a '\0' after '>'. */ + if( strcmp( pLine, "> " ) == 0 ) + { + pLine[ 1 ] = '\n'; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetPsmSettings( CellularHandle_t cellularHandle, + const CellularPsmSettings_t * pPsmSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + uint32_t cmdBufLen = 0; + CellularAtReq_t atReqSetPsm = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pPsmSettings == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* 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_MAX_SIZE, "AT+QPSMS=%d,", pPsmSettings->mode ); + cmdBufLen = strlen( cmdBuf ); + cmdBufLen = cmdBufLen + appendBinaryPattern( &cmdBuf[ cmdBufLen ], ( CELLULAR_AT_CMD_MAX_SIZE - cmdBufLen ), + pPsmSettings->periodicRauValue, false ); + cmdBufLen = cmdBufLen + appendBinaryPattern( &cmdBuf[ cmdBufLen ], ( CELLULAR_AT_CMD_MAX_SIZE - cmdBufLen ), + pPsmSettings->gprsReadyTimer, false ); + cmdBufLen = cmdBufLen + appendBinaryPattern( &cmdBuf[ cmdBufLen ], ( CELLULAR_AT_CMD_MAX_SIZE - cmdBufLen ), + pPsmSettings->periodicTauValue, false ); + cmdBufLen = cmdBufLen + appendBinaryPattern( &cmdBuf[ cmdBufLen ], ( CELLULAR_AT_CMD_MAX_SIZE - cmdBufLen ), + pPsmSettings->activeTimeValue, true ); + + LogDebug( ( "PSM setting: %s ", cmdBuf ) ); + + if( cmdBufLen < CELLULAR_AT_CMD_MAX_SIZE ) + { + /* we should always query the PSMsettings from the network. */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetPsm ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SetPsmSettings: couldn't set PSM settings" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + else + { + cellularStatus = CELLULAR_NO_MEMORY; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_DeactivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqDeactPdn = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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%d", "AT+QIDEACT=", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqDeactPdn, PDN_DEACTIVATION_PACKET_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_DeactivatePdn: can't deactivate PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ActivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqActPdn = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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%d", "AT+QIACT=", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqActPdn, PDN_ACTIVATION_PACKET_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_ActivatePdn: can't activate PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetPdnConfig( CellularHandle_t cellularHandle, + uint8_t contextId, + const CellularPdnConfig_t * pPdnConfig ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSetPdn = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + if( pPdnConfig == NULL ) + { + LogError( ( "Cellular_ATCommandRaw: Input parameter is NULL" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = _Cellular_IsValidPdn( contextId ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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_MAX_SIZE, "%s%d,%d,\"%s\",\"%s\",\"%s\",%d", + "AT+QICSGP=", + contextId, + pPdnConfig->pdnContextType, + pPdnConfig->apnName, + pPdnConfig->username, + pPdnConfig->password, + pPdnConfig->pdnAuthType ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetPdn ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SetPdnConfig: can't set PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSignalInfo( CellularHandle_t cellularHandle, + CellularSignalInfo_t * pSignalInfo ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularRat_t rat = CELLULAR_RAT_INVALID; + CellularAtReq_t atReqQuerySignalInfo = + { + "AT+QCSQ", + CELLULAR_AT_WITH_PREFIX, + "+QCSQ", + _Cellular_RecvFuncGetSignalInfo, + pSignalInfo, + sizeof( CellularSignalInfo_t ), + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSignalInfo == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_GetCurrentRat( pContext, &rat ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqQuerySignalInfo ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + /* If the convert failed, the API will return CELLULAR_INVALID_SIGNAL_BAR_VALUE in bars field. */ + ( void ) _Cellular_ComputeSignalBars( rat, pSignalInfo ); + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint8_t * pBuffer, + uint32_t bufferLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pReceivedDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + uint32_t recvTimeout = DATA_READ_TIMEOUT_MS; + uint32_t recvLen = bufferLength; + _socketDataRecv_t dataRecv = + { + pReceivedDataLength, + pBuffer, + NULL + }; + CellularAtReq_t atReqSocketRecv = + { + cmdBuf, + CELLULAR_AT_MULTI_DATA_WO_PREFIX, + "+QIRD", + _Cellular_RecvFuncData, + ( void * ) &dataRecv, + bufferLength, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "_Cellular_CheckLibraryStatus failed." ) ); + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketRecv: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pBuffer == NULL ) || ( pReceivedDataLength == NULL ) || ( bufferLength == 0U ) ) + { + LogError( ( "Cellular_SocketRecv: Bad input Param." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_CONNECTED ) + { + /* Check the socket connection state. */ + LogInfo( ( "Cellular_SocketRecv: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + /* 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; + } + + /* 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 ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketSend( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + const uint8_t * pData, + uint32_t dataLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pSentDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t sendTimeout = DATA_SEND_TIMEOUT_MS; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketSend = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + CellularAtDataReq_t atDataReqSocketSend = + { + pData, + dataLength, + pSentDataLength, + NULL, + 0 + }; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "_Cellular_CheckLibraryStatus failed." ) ); + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketSend: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( pSentDataLength == NULL ) || ( dataLength == 0U ) ) + { + LogError( ( "Cellular_SocketSend: Invalid parameter." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_CONNECTED ) + { + /* Check the socket connection state. */ + LogInfo( ( "Cellular_SocketSend: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + /* Send data length check. */ + if( dataLength > ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN ) + { + atDataReqSocketSend.dataLen = ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN; + } + + /* Check send timeout. If not set by setsockopt, use default value. */ + if( socketHandle->sendTimeoutMs != 0U ) + { + sendTimeout = socketHandle->sendTimeoutMs; + } + + /* 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+QISEND=", socketHandle->socketId, atDataReqSocketSend.dataLen ); + + pktStatus = _Cellular_AtcmdDataSend( pContext, atReqSocketSend, atDataReqSocketSend, + socketSendDataPrefix, NULL, + PACKET_REQ_TIMEOUT_MS, sendTimeout, 0U ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketSend: Data send fail, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketClose( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSockClose = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( socketHandle == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else + { + if( socketHandle->socketState == SOCKETSTATE_CONNECTING ) + { + LogWarn( ( "Cellular_SocketClose: Socket state is SOCKETSTATE_CONNECTING." ) ); + } + + if( ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) || + ( socketHandle->socketState == SOCKETSTATE_CONNECTED ) || + ( socketHandle->socketState == SOCKETSTATE_DISCONNECTED ) ) + { + /* 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", "AT+QICLOSE=", socketHandle->socketId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSockClose, + SOCKET_DISCONNECT_PACKET_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketClose: Socket close failed, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + } + } + + /* Ignore the result from the info, and force to remove the socket. */ + cellularStatus = _Cellular_RemoveSocketData( pContext, socketHandle ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketConnect( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketConnect = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Cellular_SocketConnect: _Cellular_CheckLibraryStatus failed." ) ); + } + else if( pRemoteSocketAddress == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket address." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( socketHandle->socketState == SOCKETSTATE_CONNECTED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + LogError( ( "Cellular_SocketConnect: Not allowed in state %d.", socketHandle->socketState ) ); + cellularStatus = CELLULAR_NOT_ALLOWED; + } + else + { + cellularStatus = storeAccessModeAndAddress( pContext, socketHandle, dataAccessMode, pRemoteSocketAddress ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Builds the Socket connect command. */ + cellularStatus = buildSocketConnect( socketHandle, cmdBuf ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Set the socket state to connecting state. If cellular modem returns error, + * revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_CONNECTING; + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketConnect, + SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketConnect: Socket connect failed, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + /* Revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_ALLOCATED; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_GetPdnStatus( CellularHandle_t cellularHandle, + CellularPdnStatus_t * pPdnStatusBuffers, + uint8_t numStatusBuffers, + uint8_t * pNumStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + const CellularPdnStatus_t * pTempPdnStatusBuffer = pPdnStatusBuffers; + uint8_t numBuffers = 0; + CellularAtReq_t atReqGetPdnStatus = + { + "AT+QIACT?", + CELLULAR_AT_WITH_PREFIX, + "+QIACT", + _Cellular_RecvFuncGetPdnStatus, + pPdnStatusBuffers, + numStatusBuffers, + }; + + if( ( pTempPdnStatusBuffer == NULL ) || ( pNumStatus == NULL ) || ( numStatusBuffers < 1u ) ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + LogWarn( ( "_Cellular_GetPdnStatus: Bad input Parameter " ) ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetPdnStatus ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Populate the Valid number of statuses. */ + *pNumStatus = 0; + numBuffers = numStatusBuffers; + + while( numBuffers != 0U ) + { + /* Check if the PDN state is valid. The context ID of the first + * invalid PDN status is set to FF. */ + if( ( pTempPdnStatusBuffer->contextId <= CELLULAR_PDN_CONTEXT_ID_MAX ) && + ( pTempPdnStatusBuffer->contextId != INVALID_PDN_INDEX ) ) + { + ( *pNumStatus ) += 1U; + } + else + { + break; + } + + numBuffers--; + pTempPdnStatusBuffer++; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardStatus( CellularHandle_t cellularHandle, + CellularSimCardStatus_t * pSimCardStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetSimCardStatus = + { + "AT+QSIMSTAT?", + CELLULAR_AT_WITH_PREFIX, + "+QSIMSTAT", + _Cellular_RecvFuncGetSimCardStatus, + &pSimCardStatus->simCardState, + sizeof( CellularSimCardState_t ), + }; + CellularAtReq_t atReqGetSimLockStatus = + { + "AT+CPIN?", + CELLULAR_AT_WITH_PREFIX, + "+CPIN", + _Cellular_RecvFuncGetSimLockStatus, + &pSimCardStatus->simCardLockState, + sizeof( CellularSimCardLockState_t ), + }; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSimCardStatus == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Initialize the sim state and the sim lock state. */ + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_UNKNOWN; + pSimCardStatus->simCardLockState = CELLULAR_SIM_CARD_LOCK_UNKNOWN; + + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetSimCardStatus ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetSimLockStatus ); + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + LogDebug( ( "_Cellular_GetSimStatus, Sim Insert State[%d], Lock State[%d]", + pSimCardStatus->simCardState, pSimCardStatus->simCardLockState ) ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardInfo( CellularHandle_t cellularHandle, + CellularSimCardInfo_t * pSimCardInfo ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + CellularAtReq_t atReqGetIccid = + { + "AT+QCCID", + CELLULAR_AT_WITH_PREFIX, + "+QCCID", + _Cellular_RecvFuncGetIccid, + pSimCardInfo->iccid, + CELLULAR_ICCID_MAX_SIZE + 1U, + }; + CellularAtReq_t atReqGetImsi = + { + "AT+CIMI", + CELLULAR_AT_WO_PREFIX, + NULL, + _Cellular_RecvFuncGetImsi, + pSimCardInfo->imsi, + CELLULAR_IMSI_MAX_SIZE + 1U, + }; + CellularAtReq_t atReqGetHplmn = + { + "AT+CRSM=176,28514,0,0,0", + CELLULAR_AT_WITH_PREFIX, + "+CRSM", + _Cellular_RecvFuncGetHplmn, + &pSimCardInfo->plmn, + sizeof( CellularPlmnInfo_t ), + }; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSimCardInfo == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + ( void ) memset( pSimCardInfo, 0, sizeof( CellularSimCardInfo_t ) ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetImsi ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetHplmn ); + } + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetIccid ); + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + else + { + LogDebug( ( "SimInfo updated: IMSI:%s, Hplmn:%s%s, ICCID:%s", + pSimCardInfo->imsi, pSimCardInfo->plmn.mcc, pSimCardInfo->plmn.mnc, + pSimCardInfo->iccid ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcSignalStrengthChangedCallback( CellularHandle_t cellularHandle, + CellularUrcSignalStrengthChangedCallback_t signalStrengthChangedCallback, + void * pCallbackContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + + /* pContext is checked in the common library. */ + cellularStatus = Cellular_CommonRegisterUrcSignalStrengthChangedCallback( + cellularHandle, signalStrengthChangedCallback, pCallbackContext ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( signalStrengthChangedCallback != NULL ) + { + cellularStatus = controlSignalStrengthIndication( pContext, true ); + } + else + { + cellularStatus = controlSignalStrengthIndication( pContext, false ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pcHostName, + char * pResolvedAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_QUERY_DNS_MAX_SIZE ]; + cellularDnsQueryResult_t dnsQueryResult = CELLULAR_DNS_QUERY_UNKNOWN; + cellularModuleContext_t * pModuleContext = NULL; + CellularAtReq_t atReqQueryDns = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pcHostName == NULL ) || ( pResolvedAddress == NULL ) ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_IsValidPdn( contextId ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + PlatformMutex_Lock( &pModuleContext->dnsQueryMutex ); + pModuleContext->dnsResultNumber = 0; + pModuleContext->dnsIndex = 0; + ( void ) xQueueReset( pModuleContext->pktDnsQueue ); + cellularStatus = registerDnsEventCallback( pModuleContext, _dnsResultCallback, pResolvedAddress ); + } + + /* Send the AT command and wait the URC result. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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_QUERY_DNS_MAX_SIZE, + "AT+QIDNSGIP=%u,\"%s\"", contextId, pcHostName ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqQueryDns ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_GetHostByName: couldn't resolve host name" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + PlatformMutex_Unlock( &pModuleContext->dnsQueryMutex ); + } + } + + /* URC handler calls the callback to unblock this function. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( xQueueReceive( pModuleContext->pktDnsQueue, &dnsQueryResult, + pdMS_TO_TICKS( DNS_QUERY_TIMEOUT_MS ) ) == pdTRUE ) + { + if( dnsQueryResult != CELLULAR_DNS_QUERY_SUCCESS ) + { + cellularStatus = CELLULAR_UNKNOWN; + } + } + else + { + ( void ) registerDnsEventCallback( pModuleContext, NULL, NULL ); + cellularStatus = CELLULAR_TIMEOUT; + } + + PlatformMutex_Unlock( &pModuleContext->dnsQueryMutex ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +CellularError_t Cellular_Init( CellularHandle_t * pCellularHandle, + const CellularCommInterface_t * pCommInterface ) +{ + CellularTokenTable_t cellularTokenTable = + { + .pCellularUrcHandlerTable = CellularUrcHandlerTable, + .cellularPrefixToParserMapSize = CellularUrcHandlerTableSize, + .pCellularSrcTokenErrorTable = CellularSrcTokenErrorTable, + .cellularSrcTokenErrorTableSize = CellularSrcTokenErrorTableSize, + .pCellularSrcTokenSuccessTable = CellularSrcTokenSuccessTable, + .cellularSrcTokenSuccessTableSize = CellularSrcTokenSuccessTableSize, + .pCellularUrcTokenWoPrefixTable = CellularUrcTokenWoPrefixTable, + .cellularUrcTokenWoPrefixTableSize = CellularUrcTokenWoPrefixTableSize, + .pCellularSrcExtraTokenSuccessTable = NULL, + .cellularSrcExtraTokenSuccessTableSize = 0 + }; + + return Cellular_CommonInit( pCellularHandle, pCommInterface, &cellularTokenTable ); +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_urc_handler.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_urc_handler.c new file mode 100644 index 0000000000..d872c0d9b3 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_urc_handler.c @@ -0,0 +1,807 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include +#include + +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_common.h" +#include "cellular_common_api.h" +#include "cellular_common_portable.h" +#include "cellular_bg96.h" + +/*-----------------------------------------------------------*/ + +static void _Cellular_ProcessPowerDown( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessPsmPowerDown( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessModemRdy( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessSocketOpen( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessSocketurc( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessSimstat( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessIndication( CellularContext_t * pContext, + char * pInputLine ); + +/*-----------------------------------------------------------*/ + +/* Try to Keep this map in Alphabetical order. */ +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularAtParseTokenMap_t CellularUrcHandlerTable[] = +{ + { "CEREG", Cellular_CommonUrcProcessCereg }, + { "CGREG", Cellular_CommonUrcProcessCgreg }, + { "CREG", Cellular_CommonUrcProcessCreg }, + { "NORMAL POWER DOWN", _Cellular_ProcessPowerDown }, + { "PSM POWER DOWN", _Cellular_ProcessPsmPowerDown }, + { "QIND", _Cellular_ProcessIndication }, + { "QIOPEN", _Cellular_ProcessSocketOpen }, + { "QIURC", _Cellular_ProcessSocketurc }, + { "QSIMSTAT", _Cellular_ProcessSimstat }, + { "RDY", _Cellular_ProcessModemRdy } +}; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcHandlerTableSize = sizeof( CellularUrcHandlerTable ) / sizeof( CellularAtParseTokenMap_t ); + +/*-----------------------------------------------------------*/ + +/* internal function of _parseSocketOpen to reduce complexity. */ +static CellularPktStatus_t _parseSocketOpenNextTok( const char * pToken, + uint32_t sockIndex, + CellularSocketContext_t * pSocketData ) +{ + int32_t sockStatus = 0; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &sockStatus ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( sockStatus != 0 ) + { + pSocketData->socketState = SOCKETSTATE_DISCONNECTED; + LogError( ( "_parseSocketOpen: Socket open failed, conn %d, status %d", sockIndex, sockStatus ) ); + } + else + { + pSocketData->socketState = SOCKETSTATE_CONNECTED; + LogDebug( ( "_parseSocketOpen: Socket open success, conn %d", sockIndex ) ); + } + + /* Indicate the upper layer about the socket open status. */ + if( pSocketData->openCallback != NULL ) + { + if( sockStatus != 0 ) + { + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPEN_FAILED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + else + { + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPENED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + } + else + { + LogError( ( ( "_parseSocketOpen: Socket open callback for conn %d is not set!!", sockIndex ) ) ); + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessSocketOpen( CellularContext_t * pContext, + char * pInputLine ) +{ + char * pUrcStr = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + uint32_t sockIndex = 0; + int32_t tempValue = 0; + CellularSocketContext_t * pSocketData = NULL; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( pInputLine == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pUrcStr = pInputLine; + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pUrcStr ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue < ( int32_t ) CELLULAR_NUM_SOCKET_MAX ) ) + { + sockIndex = ( uint32_t ) tempValue; + } + else + { + LogError( ( ( "Error processing in Socket index. token %s", pToken ) ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSocketData = _Cellular_GetSocketData( pContext, sockIndex ); + + if( pSocketData != NULL ) + { + atCoreStatus = Cellular_ATGetNextTok( &pUrcStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pktStatus = _parseSocketOpenNextTok( pToken, sockIndex, pSocketData ); + } + } + else + { + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogDebug( ( "Socket Open URC Parse failure" ) ); + } +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _parseUrcIndicationCsq( const CellularContext_t * pContext, + char * pUrcStr ) +{ + char * pToken = NULL; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + int32_t retStrtoi = 0; + int16_t csqRssi = CELLULAR_INVALID_SIGNAL_VALUE, csqBer = CELLULAR_INVALID_SIGNAL_VALUE; + CellularSignalInfo_t signalInfo = { 0 }; + char * pLocalUrcStr = pUrcStr; + + if( ( pContext == NULL ) || ( pUrcStr == NULL ) ) + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + else + { + /* Parse the RSSI index from string and convert it. */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &retStrtoi ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( retStrtoi >= INT16_MIN ) && ( retStrtoi <= ( int32_t ) INT16_MAX ) ) + { + cellularStatus = _Cellular_ConvertCsqSignalRssi( ( int16_t ) retStrtoi, &csqRssi ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Parse the BER index from string and convert it. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &retStrtoi ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( retStrtoi >= INT16_MIN ) && + ( retStrtoi <= ( int32_t ) INT16_MAX ) ) + { + cellularStatus = _Cellular_ConvertCsqSignalBer( ( int16_t ) retStrtoi, &csqBer ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Handle the callback function. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + signalInfo.rssi = csqRssi; + signalInfo.rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.ber = csqBer; + signalInfo.bars = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + _Cellular_SignalStrengthChangedCallback( pContext, CELLULAR_URC_EVENT_SIGNAL_CHANGED, &signalInfo ); + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessIndication( CellularContext_t * pContext, + char * pInputLine ) +{ + char * pUrcStr = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + /* Check context status. */ + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( pInputLine == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pUrcStr = pInputLine; + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pUrcStr ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pUrcStr ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( strstr( pToken, "csq" ) != NULL ) + { + pktStatus = _parseUrcIndicationCsq( ( const CellularContext_t * ) pContext, pUrcStr ); + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogDebug( ( "UrcIndication Parse failure" ) ); + } +} + +/*-----------------------------------------------------------*/ + +static void _informDataReadyToUpperLayer( CellularSocketContext_t * pSocketData ) +{ + /* Indicate the upper layer about the data reception. */ + if( ( pSocketData != NULL ) && ( pSocketData->dataReadyCallback != NULL ) ) + { + pSocketData->dataReadyCallback( pSocketData, pSocketData->pDataReadyCallbackContext ); + } + else + { + LogError( ( ( "_parseSocketUrc: Data ready callback not set!!" ) ) ); + } +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _parseSocketUrcRecv( const CellularContext_t * pContext, + char * pUrcStr ) +{ + char * pToken = NULL; + char * pLocalUrcStr = pUrcStr; + int32_t tempValue = 0; + uint32_t sockIndex = 0; + CellularSocketContext_t * pSocketData = NULL; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + atCoreStatus = Cellular_ATGetNextTok( &pLocalUrcStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue < ( int32_t ) CELLULAR_NUM_SOCKET_MAX ) ) + { + sockIndex = ( uint32_t ) tempValue; + } + else + { + LogError( ( ( "Error in processing SockIndex. Token %s", pToken ) ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSocketData = _Cellular_GetSocketData( pContext, sockIndex ); + + if( pSocketData != NULL ) + { + if( pSocketData->dataMode == CELLULAR_ACCESSMODE_BUFFER ) + { + /* Data received indication in buffer mode, need to fetch the data. */ + LogDebug( ( "Data Received on socket Conn Id %d", sockIndex ) ); + _informDataReadyToUpperLayer( pSocketData ); + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _parseSocketUrcClosed( const CellularContext_t * pContext, + char * pUrcStr ) +{ + char * pToken = NULL; + char * pLocalUrcStr = pUrcStr; + int32_t tempValue = 0; + uint32_t sockIndex = 0; + CellularSocketContext_t * pSocketData = NULL; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + atCoreStatus = Cellular_ATGetNextTok( &pLocalUrcStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( tempValue < ( int32_t ) CELLULAR_NUM_SOCKET_MAX ) + { + sockIndex = ( uint32_t ) tempValue; + } + else + { + LogError( ( ( "Error in processing Socket Index. Token %s", pToken ) ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSocketData = _Cellular_GetSocketData( pContext, sockIndex ); + + if( pSocketData != NULL ) + { + pSocketData->socketState = SOCKETSTATE_DISCONNECTED; + LogDebug( ( "Socket closed. Conn Id %d", sockIndex ) ); + + /* Indicate the upper layer about the socket close. */ + if( pSocketData->closedCallback != NULL ) + { + pSocketData->closedCallback( pSocketData, pSocketData->pClosedCallbackContext ); + } + else + { + LogInfo( ( "_parseSocketUrc: Socket close callback not set!!" ) ); + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _parseSocketUrcAct( const CellularContext_t * pContext, + char * pUrcStr ) +{ + int32_t tempValue = 0; + char * pToken = NULL; + char * pLocalUrcStr = pUrcStr; + uint8_t contextId = 0; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + atCoreStatus = Cellular_ATGetNextTok( &pLocalUrcStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( ( tempValue >= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MIN ) && + ( tempValue <= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MAX ) ) ) + { + contextId = ( uint8_t ) tempValue; + + if( _Cellular_IsValidPdn( contextId ) == CELLULAR_SUCCESS ) + { + LogDebug( ( "PDN deactivated. Context Id %d", contextId ) ); + /* Indicate the upper layer about the PDN deactivate. */ + _Cellular_PdnEventCallback( pContext, CELLULAR_URC_EVENT_PDN_DEACTIVATED, contextId ); + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + LogError( ( ( "Error in processing Context Id. Token %s", pToken ) ) ); + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _parseSocketUrcDns( const CellularContext_t * pContext, + char * pUrcStr ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + cellularModuleContext_t * pModuleContext = NULL; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( pUrcStr == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + } + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + if( pModuleContext->dnsEventCallback != NULL ) + { + pModuleContext->dnsEventCallback( pModuleContext, pUrcStr, pModuleContext->pDnsUsrData ); + } + else + { + LogDebug( ( "_parseSocketUrcDns: spurious DNS response!!" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_DATA; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessSocketurc( CellularContext_t * pContext, + char * pInputLine ) +{ + char * pUrcStr = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( pInputLine == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pUrcStr = pInputLine; + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pUrcStr ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pUrcStr ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Check if this is a data receive indication. */ + + /* this whole if as a function and return pktstatus + * take iotat_getnexttok inside + * convert atcore status to pktstatus. */ + if( strstr( pToken, "recv" ) != NULL ) + { + pktStatus = _parseSocketUrcRecv( pContext, pUrcStr ); + } + else if( strcmp( pToken, "closed" ) == 0 ) + { + pktStatus = _parseSocketUrcClosed( pContext, pUrcStr ); + } + else if( strcmp( pToken, "pdpdeact" ) == 0 ) + { + pktStatus = _parseSocketUrcAct( pContext, pUrcStr ); + } + else if( strcmp( pToken, "dnsgip" ) == 0 ) + { + pktStatus = _parseSocketUrcDns( pContext, pUrcStr ); + } + else + { + /* Empty else MISRA 15.7 */ + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogDebug( ( "Socketurc Parse failure" ) ); + } +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessSimstat( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularSimCardState_t simCardState = CELLULAR_SIM_CARD_UNKNOWN; + + if( pContext != NULL ) + { + ( void ) _Cellular_ParseSimstat( pInputLine, &simCardState ); + } +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessPowerDown( CellularContext_t * pContext, + char * pInputLine ) +{ + /* The token is the pInputLine. No need to process the pInputLine. */ + ( void ) pInputLine; + + if( pContext == NULL ) + { + LogError( ( ( "_Cellular_ProcessPowerDown: Context not set" ) ) ); + } + else + { + LogDebug( ( "_Cellular_ProcessPowerDown: Modem Power down event received" ) ); + _Cellular_ModemEventCallback( pContext, CELLULAR_MODEM_EVENT_POWERED_DOWN ); + } +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessPsmPowerDown( CellularContext_t * pContext, + char * pInputLine ) +{ + /* The token is the pInputLine. No need to process the pInputLine. */ + ( void ) pInputLine; + + if( pContext == NULL ) + { + LogError( ( ( "_Cellular_ProcessPowerDown: Context not set" ) ) ); + } + else + { + LogDebug( ( "_Cellular_ProcessPsmPowerDown: Modem PSM power down event received" ) ); + _Cellular_ModemEventCallback( pContext, CELLULAR_MODEM_EVENT_PSM_ENTER ); + } +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static void _Cellular_ProcessModemRdy( CellularContext_t * pContext, + char * pInputLine ) +{ + /* The token is the pInputLine. No need to process the pInputLine. */ + ( void ) pInputLine; + + if( pContext == NULL ) + { + LogWarn( ( "_Cellular_ProcessModemRdy: Context not set" ) ); + } + else + { + LogDebug( ( "_Cellular_ProcessModemRdy: Modem Ready event received" ) ); + _Cellular_ModemEventCallback( pContext, CELLULAR_MODEM_EVENT_BOOTUP_OR_REBOOT ); + } +} + +/*-----------------------------------------------------------*/ + +/* Cellular common prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularPktStatus_t _Cellular_ParseSimstat( char * pInputStr, + CellularSimCardState_t * pSimState ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pToken = NULL; + char * pLocalInputStr = pInputStr; + int32_t tempValue = 0; + + if( ( pInputStr == NULL ) || ( strlen( pInputStr ) == 0U ) || + ( strlen( pInputStr ) < 2U ) || ( pSimState == NULL ) ) + { + LogError( ( ( "_Cellular_ProcessQsimstat Input data is invalid %s", pInputStr ) ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputStr, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + LogDebug( ( "QSIMSTAT URC Enable: %s", pToken ) ); + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + LogDebug( ( " Sim insert status: %s", pToken ) ); + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue < ( int32_t ) CELLULAR_SIM_CARD_STATUS_MAX ) ) + { + /* Variable "tempValue" is ensured that it is valid and within + * a valid range. Hence, assigning the value at the pointer of + * type cellular_SimCardState_t with an enum cast. */ + /* coverity[misra_c_2012_rule_10_5_violation] */ + *pSimState = ( CellularSimCardState_t ) tempValue; + } + else + { + LogError( ( ( "Error in processing SIM state. token %s", pToken ) ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_wrapper.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_wrapper.c new file mode 100644 index 0000000000..e2109c0e18 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Quectel-BG96/cellular_bg96_wrapper.c @@ -0,0 +1,268 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* The config header is always included first. */ +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include + +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common.h" +#include "cellular_common_api.h" + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_Cleanup( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonCleanup( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcNetworkRegistrationEventCallback( CellularHandle_t cellularHandle, + CellularUrcNetworkRegistrationCallback_t networkRegistrationCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcNetworkRegistrationEventCallback( cellularHandle, networkRegistrationCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcPdnEventCallback( CellularHandle_t cellularHandle, + CellularUrcPdnEventCallback_t pdnEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcPdnEventCallback( cellularHandle, pdnEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcGenericCallback( CellularHandle_t cellularHandle, + CellularUrcGenericCallback_t genericCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcGenericCallback( cellularHandle, genericCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterModemEventCallback( CellularHandle_t cellularHandle, + CellularModemEventCallback_t modemEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterModemEventCallback( cellularHandle, modemEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ATCommandRaw( CellularHandle_t cellularHandle, + const char * pATCommandPrefix, + const char * pATCommandPayload, + CellularATCommandType_t atCommandType, + CellularATCommandResponseReceivedCallback_t responseReceivedCallback, + void * pData, + uint16_t dataLen ) +{ + return Cellular_CommonATCommandRaw( cellularHandle, pATCommandPrefix, pATCommandPayload, atCommandType, + responseReceivedCallback, pData, dataLen ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_CreateSocket( CellularHandle_t cellularHandle, + uint8_t pdnContextId, + CellularSocketDomain_t socketDomain, + CellularSocketType_t socketType, + CellularSocketProtocol_t socketProtocol, + CellularSocketHandle_t * pSocketHandle ) +{ + return Cellular_CommonCreateSocket( cellularHandle, pdnContextId, socketDomain, socketType, + socketProtocol, pSocketHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketSetSockOpt( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOptionLevel_t optionLevel, + CellularSocketOption_t option, + const uint8_t * pOptionValue, + uint32_t optionValueLength ) +{ + return Cellular_CommonSocketSetSockOpt( cellularHandle, socketHandle, optionLevel, option, + pOptionValue, optionValueLength ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterDataReadyCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketDataReadyCallback_t dataReadyCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterDataReadyCallback( cellularHandle, socketHandle, + dataReadyCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterSocketOpenCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOpenCallback_t socketOpenCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterSocketOpenCallback( cellularHandle, socketHandle, + socketOpenCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterClosedCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketClosedCallback_t closedCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterClosedCallback( cellularHandle, socketHandle, + closedCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOn( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOn( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOff( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOff( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetIPAddress( CellularHandle_t cellularHandle, + uint8_t contextId, + char * pBuffer, + uint32_t bufferLength ) +{ + return Cellular_CommonGetIPAddress( cellularHandle, contextId, pBuffer, bufferLength ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetModemInfo( CellularHandle_t cellularHandle, + CellularModemInfo_t * pModemInfo ) +{ + return Cellular_CommonGetModemInfo( cellularHandle, pModemInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetEidrxSettings( CellularHandle_t cellularHandle, + CellularEidrxSettingsList_t * pEidrxSettingsList ) +{ + return Cellular_CommonGetEidrxSettings( cellularHandle, pEidrxSettingsList ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetEidrxSettings( CellularHandle_t cellularHandle, + const CellularEidrxSettings_t * pEidrxSettings ) +{ + return Cellular_CommonSetEidrxSettings( cellularHandle, pEidrxSettings ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRegisteredNetwork( CellularHandle_t cellularHandle, + CellularPlmnInfo_t * pNetworkInfo ) +{ + return Cellular_CommonGetRegisteredNetwork( cellularHandle, pNetworkInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetNetworkTime( CellularHandle_t cellularHandle, + CellularTime_t * pNetworkTime ) +{ + return Cellular_CommonGetNetworkTime( cellularHandle, pNetworkTime ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetServiceStatus( CellularHandle_t cellularHandle, + CellularServiceStatus_t * pServiceStatus ) +{ + return Cellular_CommonGetServiceStatus( cellularHandle, pServiceStatus ); +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.c new file mode 100644 index 0000000000..bdfc7380b3 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.c @@ -0,0 +1,585 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* The config header is always included first. */ + +#include + +#include "cellular_config.h" +#include "cellular_config_defaults.h" +#include "cellular_common.h" +#include "cellular_common_portable.h" +#include "cellular_hl7802.h" + +/*-----------------------------------------------------------*/ + +#define ENBABLE_MODULE_UE_RETRY_COUNT ( 6U ) +#define HL7802_MAX_BAND_CFG ( 21U ) +#define HL7802_KSELACQ_CMD_MAX_SIZE ( 19U ) /* The length of AT+KSELACQ=0,1,2,3\0. */ + +/*-----------------------------------------------------------*/ + +typedef struct Hl7802BandConfig +{ + char catm1BandCfg[ HL7802_MAX_BAND_CFG ]; + char nbiotBandCfg[ HL7802_MAX_BAND_CFG ]; + char gsmBandCfg[ HL7802_MAX_BAND_CFG ]; +} Hl7802BandConfig_t; + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq, + uint32_t timeoutMs ); +static CellularError_t getBandCfg( CellularContext_t * pContext, + Hl7802BandConfig_t * pBandCfg ); +static CellularPktStatus_t recvFuncGetBandCfg( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); + +/*-----------------------------------------------------------*/ + +static cellularModuleContext_t cellularHl7802Context = { 0 }; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenErrorTable[] = +{ "ERROR", "BUSY", "NO CARRIER", "NO ANSWER", "NO DIALTONE", "ABORTED", "+CMS ERROR", "+CME ERROR", "SEND FAIL" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenErrorTableSize = sizeof( CellularSrcTokenErrorTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenSuccessTable[] = +{ "OK" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenSuccessTableSize = sizeof( CellularSrcTokenSuccessTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularUrcTokenWoPrefixTable[] = { 0 }; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcTokenWoPrefixTableSize = 0; + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq, + uint32_t timeoutMs ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint8_t tryCount = 0; + + if( pAtReq == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + for( ; tryCount < ENBABLE_MODULE_UE_RETRY_COUNT; tryCount++ ) + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, *pAtReq, timeoutMs ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + break; + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t recvFuncGetBandCfg( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + Hl7802BandConfig_t * pBandCfg = NULL; + const CellularATCommandLine_t * pCommnadItem = NULL; + char * pInputLine = NULL; + char * pToken = NULL; + char * pRatBand = NULL; + + if( pContext == NULL ) + { + LogError( ( "recvFuncGetBandCfg: Invalid context." ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pAtResp == NULL ) + { + LogError( ( "recvFuncGetBandCfg: Invalid pAtResp." ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pData == NULL ) || ( dataLen != sizeof( Hl7802BandConfig_t ) ) ) + { + LogError( ( "recvFuncGetBandCfg: Invalid pData %p or dataLen %u.", pData, dataLen ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pBandCfg = ( Hl7802BandConfig_t * ) pData; + pCommnadItem = pAtResp->pItm; + + while( pCommnadItem != NULL ) + { + pInputLine = pCommnadItem->pLine; + LogDebug( ( "recvFuncGetBandCfg: input line %s", pInputLine ) ); + + /* Remove the line prefix. */ + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + /* Parse the RAT field. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + switch( *pToken ) + { + case '0': + pRatBand = pBandCfg->catm1BandCfg; + break; + + case '1': + pRatBand = pBandCfg->nbiotBandCfg; + break; + + case '2': + pRatBand = pBandCfg->gsmBandCfg; + break; + + default: + pRatBand = NULL; + LogError( ( "recvFuncGetBandCfg: unknown RAT %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* Copy the band configuration. */ + strncpy( pRatBand, pInputLine, HL7802_MAX_BAND_CFG ); + } + else + { + LogError( ( "recvFuncGetBandCfg process response line error" ) ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + break; + } + + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t getBandCfg( CellularContext_t * pContext, + Hl7802BandConfig_t * pBandCfg ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetBndCfg = + { + "AT+KBNDCFG?", + CELLULAR_AT_MULTI_WITH_PREFIX, + "+KBNDCFG", + recvFuncGetBandCfg, + pBandCfg, + sizeof( Hl7802BandConfig_t ) + }; + + /* pContext and pBandCfg are checked in Cellular_ModuleEnableUe function. */ + ( void ) memset( pBandCfg, 0, sizeof( Hl7802BandConfig_t ) ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetBndCfg, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "getBandCfg: couldn't retrieve band configurations." ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static bool appendRatList( char * pRatList, + CellularRat_t cellularRat ) +{ + bool retValue = true; + + switch( cellularRat ) + { + case CELLULAR_RAT_CATM1: + strcat( pRatList, ",1" ); + break; + + case CELLULAR_RAT_NBIOT: + strcat( pRatList, ",2" ); + break; + + case CELLULAR_RAT_GSM: + strcat( pRatList, ",3" ); + break; + + default: + /* Unsupported RAT. */ + retValue = false; + break; + } + + return retValue; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleInit( const CellularContext_t * pContext, + void ** ppModuleContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint32_t i = 0; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ppModuleContext == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Initialize the module context. */ + ( void ) memset( &cellularHl7802Context, 0, sizeof( cellularModuleContext_t ) ); + + for( i = 0; i < TCP_SESSION_TABLE_LEGNTH; i++ ) + { + cellularHl7802Context.pSessionMap[ i ] = INVALID_SOCKET_INDEX; + } + + *ppModuleContext = ( void * ) &cellularHl7802Context; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleCleanUp( const CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + Hl7802BandConfig_t bandCfg = { 0 }; + char ratSelectCmd[ HL7802_KSELACQ_CMD_MAX_SIZE ] = "AT+KSELACQ=0"; + bool retAppendRat = true; + + if( pContext != NULL ) + { + /* Disable echo. */ + atReqGetNoResult.pAtCmd = "ATE0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Disable DTR function. */ + atReqGetNoResult.pAtCmd = "AT&D0"; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + #ifndef CELLULAR_CONFIG_DISABLE_FLOW_CONTROL + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Enable RTS/CTS hardware flow control. */ + atReqGetNoResult.pAtCmd = "AT+IFC=2,2"; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + #endif + + /* Set Radio Access Technology. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* In the Write format, =0 is used to switch to the first RAT + * in the preferred RAT list (PRL), and fall back to subsequent RATS + * in the PRL if cell coverage is lost. If the PRL is empty, switch to + * CAT-M1. To set the PRL, see AT+KSELACQ. */ + atReqGetNoResult.pAtCmd = "AT+KSRAT=0"; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + /* Set Default Radio Access Technology. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT ); + configASSERT( retAppendRat == true ); + + #ifdef CELLULAR_CONFIG_DEFAULT_RAT_2 + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT_2 ); + configASSERT( retAppendRat == true ); + #endif + + #ifdef CELLULAR_CONFIG_DEFAULT_RAT_3 + retAppendRat = appendRatList( ratSelectCmd, CELLULAR_CONFIG_DEFAULT_RAT_3 ); + configASSERT( retAppendRat == true ); + #endif + + atReqGetNoResult.pAtCmd = ratSelectCmd; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_KSELACQ_TIMEOUT_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + /* Set Configured LTE Band. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = getBandCfg( pContext, &bandCfg ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( strcmp( bandCfg.catm1BandCfg, CELLULAR_CONFIG_HL7802_CATM1_BAND ) != 0 ) + { + LogInfo( ( "Cellular_ModuleEnableUE : CAT-M1 band desired %s actual %s", + CELLULAR_CONFIG_HL7802_CATM1_BAND, bandCfg.catm1BandCfg ) ); + atReqGetNoResult.pAtCmd = "AT+KBNDCFG=0,"CELLULAR_CONFIG_HL7802_CATM1_BAND; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( strcmp( bandCfg.nbiotBandCfg, CELLULAR_CONFIG_HL7802_NBIOT_BAND ) != 0 ) + { + LogInfo( ( "Cellular_ModuleEnableUE : NBIOT band desired %s actual %s", + CELLULAR_CONFIG_HL7802_NBIOT_BAND, bandCfg.nbiotBandCfg ) ); + atReqGetNoResult.pAtCmd = "AT+KBNDCFG=1,"CELLULAR_CONFIG_HL7802_NBIOT_BAND; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + /* Disable standalone sleep mode. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + atReqGetNoResult.pAtCmd = "AT+KSLEEP=2"; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + /* Force initialization of radio to consider new configured bands. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + atReqGetNoResult.pAtCmd = "AT+CFUN=1,1"; + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_30_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + /* Disable echo after reboot device. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + Platform_Delay( CELLULAR_HL7802_RESET_DELAY_MS ); + atReqGetNoResult.pAtCmd = "ATE0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUrc( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + atReqGetNoResult.pAtCmd = "AT+COPS=3,2"; + ( void ) _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_120_SECONDS_MS ); + + atReqGetNoResult.pAtCmd = "AT+CREG=2"; + ( void ) _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + atReqGetNoResult.pAtCmd = "AT+CEREG=2"; + ( void ) _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + atReqGetNoResult.pAtCmd = "AT+CTZR=1"; + ( void ) _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSocketId( CellularContext_t * pContext, + uint8_t sessionId ) +{ + cellularModuleContext_t * pModuleContext = NULL; + uint32_t socketIndex = INVALID_SOCKET_INDEX; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext != NULL ) + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + else + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + + if( ( cellularStatus == CELLULAR_SUCCESS ) && + ( sessionId >= MIN_TCP_SESSION_ID ) && ( sessionId <= MAX_TCP_SESSION_ID ) ) + { + socketIndex = pModuleContext->pSessionMap[ sessionId ]; + } + + return socketIndex; +} + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSessionId( CellularContext_t * pContext, + uint32_t socketIndex ) +{ + cellularModuleContext_t * pModuleContext = NULL; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint32_t sessionId = INVALID_SESSION_ID; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_GetSessionId invalid cellular context" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogError( ( "_Cellular_GetSessionId invalid socketIndex" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + for( sessionId = 0; sessionId < TCP_SESSION_TABLE_LEGNTH; sessionId++ ) + { + if( pModuleContext->pSessionMap[ sessionId ] == socketIndex ) + { + break; + } + } + + /* Mapping is not found in the session mapping table. */ + if( sessionId == TCP_SESSION_TABLE_LEGNTH ) + { + sessionId = INVALID_SESSION_ID; + } + } + else + { + sessionId = INVALID_SESSION_ID; + } + + return sessionId; +} diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.h b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.h new file mode 100644 index 0000000000..059e7f537b --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802.h @@ -0,0 +1,150 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#ifndef __CELLULAR_HL7802_H__ +#define __CELLULAR_HL7802_H__ + +/* *INDENT-OFF* */ +#ifdef __cplusplus + extern "C" { +#endif +/* *INDENT-ON* */ + +#define MIN_TCP_SESSION_ID ( 1U ) +#define MAX_TCP_SESSION_ID ( 6U ) +#define TCP_SESSION_TABLE_LEGNTH ( MAX_TCP_SESSION_ID + 1 ) + +#define INVALID_SOCKET_INDEX ( UINT32_MAX ) +#define INVALID_SESSION_ID ( UINT32_MAX ) + +/* Delay after AT+CFUN=1,1 commands. */ +#ifndef CELLULAR_HL7802_RESET_DELAY_MS + #define CELLULAR_HL7802_RESET_DELAY_MS ( 3000U ) +#endif + +/* AT command recommended timeout value for HL7802. Reference HL7802 AT Commands + * Interface Guide to setup the timeout value for each AT commands. */ +#define CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ( 2000U ) +#define CELLULAR_HL7802_AT_TIMEOUT_5_SECONDS_MS ( 5000U ) +#define CELLULAR_HL7802_AT_TIMEOUT_30_SECONDS_MS ( 30000U ) +#define CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS ( 60000U ) +#define CELLULAR_HL7802_AT_TIMEOUT_120_SECONDS_MS ( 120000U ) + +/* Define the following timeout value since they are content dependent or no recommended value. */ +#ifndef CELLULAR_HL7802_AT_KSELACQ_TIMEOUT_MS + #define CELLULAR_HL7802_AT_KSELACQ_TIMEOUT_MS CELLULAR_HL7802_AT_TIMEOUT_5_SECONDS_MS +#endif + +#ifndef CELLULAR_HL7802_AT_KEDRXCFG_TIMEOUT_MS + #define CELLULAR_HL7802_AT_KEDRXCFG_TIMEOUT_MS CELLULAR_HL7802_AT_TIMEOUT_5_SECONDS_MS +#endif + +#ifndef CELLULAR_HL7802_AT_KCNXUP_TIMEOUT_MS + #define CELLULAR_HL7802_AT_KCNXUP_TIMEOUT_MS CELLULAR_HL7802_AT_TIMEOUT_30_SECONDS_MS +#endif + +#ifndef CELLULAR_HL7802_AT_KCNXDOWN_TIMEOUT_MS + #define CELLULAR_HL7802_AT_KCNXDOWN_TIMEOUT_MS CELLULAR_HL7802_AT_TIMEOUT_30_SECONDS_MS +#endif + +/* Band configuration for HL7802. */ +#ifndef CELLULAR_CONFIG_HL7802_CATM1_BAND + /* Default enable all bands. */ + #define CELLULAR_CONFIG_HL7802_CATM1_BAND "0002000000000F0F1B9F" +#endif + +#ifndef CELLULAR_CONFIG_HL7802_NBIOT_BAND + /* Default enable all bands. */ + #define CELLULAR_CONFIG_HL7802_NBIOT_BAND "0002000000000B0F189F" +#endif + +/*-----------------------------------------------------------*/ + +typedef struct cellularModuleContext +{ + uint32_t pSessionMap[ TCP_SESSION_TABLE_LEGNTH ]; +} cellularModuleContext_t; + +typedef enum tcpSocketState +{ + TCP_SOCKET_STATE_NOT_DEFINED = 0, + TCP_SOCKET_STATE_DEFINED_BUT_NOT_USED = 1, + TCP_SOCKET_STATE_OPENING_AND_CONNECTING = 2, + TCP_SOCKET_STATE_CONNECTION_UP = 3, + TCP_SOCKET_STATE_CONNECTION_CLOSING = 4, + TCP_SOCKET_STATE_CLOSED = 5, + TCP_SOCKET_STATE_MAX +} tcpSocketState_t; + +typedef enum tcpConnectionFailure +{ + TCP_NOTIF_OK = -1, + TCP_NOTIF_NETWORK_ERROR = 0, + TCP_NOTIF_NO_MORE_SOCKETS_AVAILABLE = 1, + TCP_NOTIF_MEMORY_PROBLEM = 2, + TCP_NOTIF_DNS_ERROR = 3, + TCP_NOTIF_TCP_DISCONNECTION = 4, + TCP_NOTIF_TCP_CONNECTION_ERROR = 5, + TCP_NOTIF_GENERIC_ERROR = 6, + TCP_NOTIF_FAIL_TO_ACCEPT_CLIENT_REQUEST = 7, + TCP_NOTIF_KTCPSND_WAITING_FOR_MORE_OR_LESS_CHARACTERS = 8, + TCP_NOTIF_BAD_SESSION_ID = 9, + TCP_NOTIF_SESSION_IS_ALREADY_RUNNING = 10, + TCP_NOTIF_ALL_SESSIONS_ARE_USED = 11, + TCP_NOTIF_SOCKET_CONNECTION_TIMEOUT_ERROR = 12, + TCP_NOTIF_SSL_CONNECTION_ERROR = 13, + TCP_NOTIF_SSL_INITIALIZATION_ERROR = 14, + TCP_NOTIF_MAX, +} tcpConnectionFailure_t; + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSocketId( CellularContext_t * pContext, + uint8_t sessionId ); + +uint32_t _Cellular_GetSessionId( CellularContext_t * pContext, + uint32_t socketIndex ); + +/*-----------------------------------------------------------*/ + +extern CellularAtParseTokenMap_t CellularUrcHandlerTable[]; +extern uint32_t CellularUrcHandlerTableSize; + +extern const char * CellularSrcTokenErrorTable[]; +extern uint32_t CellularSrcTokenErrorTableSize; + +extern const char * CellularSrcTokenSuccessTable[]; +extern uint32_t CellularSrcTokenSuccessTableSize; + +extern const char * CellularUrcTokenWoPrefixTable[]; +extern uint32_t CellularUrcTokenWoPrefixTableSize; + +/* *INDENT-OFF* */ +#ifdef __cplusplus + } +#endif +/* *INDENT-ON* */ + +#endif /* ifndef __CELLULAR_HL7802_H__ */ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_api.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_api.c new file mode 100644 index 0000000000..ddab82a14d --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_api.c @@ -0,0 +1,2788 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include +#include +#include + +/* Cellular includes. */ +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common_api.h" +#include "cellular_common.h" +#include "cellular_at_core.h" + +/* Cellular module includes. */ +#include "cellular_hl7802.h" + +/*-----------------------------------------------------------*/ + +#define CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ( 32U ) + +#define SOCKET_END_PATTERN "--EOF--Pattern--" +#define SOCKET_END_PATTERN_LEN ( 16U ) + +#define SOCKET_DATA_CONNECT_TOKEN "CONNECT" +#define SOCKET_DATA_CONNECT_TOKEN_LEN ( 7U ) + +#define CELLULAR_CEDRXS_POS_ACT ( 0U ) +#define CELLULAR_CEDRXS_POS_RAT ( 1U ) +#define CELLULAR_CEDRXS_POS_REQUESTED_EDRX_VALUE ( 2U ) +#define CELLULAR_CEDRXS_POS_NW_PROVIDED_EDRX_VALUE ( 3U ) +#define CELLULAR_CEDRXS_POS_REQUESTED_PTW_VALUE ( 4U ) +#define CELLULAR_CEDRXS_POS_NW_PROVIDED_PTW_VALUE ( 5U ) + +#define RAT_PRIOIRTY_LIST_LENGTH ( 3U ) + +#define SIGNAL_QUALITY_CSQ_UNKNOWN ( 99 ) +#define SIGNAL_QUALITY_CSQ_BER_MIN ( 0 ) +#define SIGNAL_QUALITY_CSQ_BER_MAX ( 7 ) + +#define INVALID_PDN_INDEX ( 0xFFU ) + +#define CELLULAR_PDN_STATUS_POS_CONTEXT_ID ( 0U ) +#define CELLULAR_PDN_STATUS_POS_GPRS ( 1U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_APN ( 2U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_LOGIN ( 3U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_PASSWORD ( 4U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_AF ( 5U ) +#define CELLULAR_PDN_STATUS_POS_IP ( 6U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_DNS1 ( 7U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_DNS2 ( 8U ) /* Ignored pos. */ +#define CELLULAR_PDN_STATUS_POS_STATE ( 9U ) + +#define CELLULAR_PDN_STATE_DISCONNECTED ( 0U ) +#define CELLULAR_PDN_STATE_CONNECTING ( 1U ) +#define CELLULAR_PDN_STATE_CONNECTED ( 2U ) +#define CELLULAR_PDN_STATE_IDLE ( 3U ) +#define CELLULAR_PDN_STATE_DISCONNECTING ( 4U ) + +#define KSELACQ_RAT_CATM_CHAR ( '1' ) +#define KSELACQ_RAT_NBIOT_CHAR ( '2' ) +#define KSELACQ_RAT_GSM_CHAR ( '3' ) + +#define CELLULAR_PORT_NUM_CHAR_LEN ( 6 ) + +#define CELLULAR_REMOTE_IP_ADDR_MAX_LENGTH ( 127 ) + +/*-----------------------------------------------------------*/ + +/** + * @brief Parameters involved in receiving data through sockets + */ +typedef struct _socketDataRecv +{ + uint32_t * pDataLen; + uint8_t * pData; +} _socketDataRecv_t; + +typedef struct socketStat +{ + tcpSocketState_t status; /* TCP socket state. */ + tcpConnectionFailure_t tcpNotif; /* 1 if socket/connection is OK, if an error has happened. */ + uint16_t remData; /* Remaining bytes in the socket buffer, waiting to be sent. */ + uint16_t rcvData; /* Received bytes, can be read with +KTCPRCV command. */ +} socketStat_t; + +/*-----------------------------------------------------------*/ + +static const char * _socketSendSuccesTokenTable[] = { SOCKET_DATA_CONNECT_TOKEN }; +static const uint32_t _socketSendSuccesTokenTableLength = 1; + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t _Cellular_RecvFuncGetSocketStat( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t _Cellular_GetSocketStat( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + socketStat_t * pSocketStat ); +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ); +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ); +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t buildSocketConfig( CellularSocketHandle_t socketHandle, + char * pCmdBuf ); +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ); +static CellularPktStatus_t _Cellular_RecvFuncGetTcpCfgSessionId( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t _Cellular_getTcpCfgSessionId( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + uint8_t * pSessionId ); +static CellularATError_t parseEidrxToken( char * pToken, + uint8_t tokenIndex, + CellularEidrxSettingsList_t * pEidrxSettingsList, + uint8_t count ); +static CellularATError_t parseEidrxLine( char * pInputLine, + uint8_t count, + CellularEidrxSettingsList_t * pEidrxSettingsList ); +static CellularPktStatus_t _Cellular_RecvFuncGetEidrxSettings( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularRat_t convertKselacqIndexToRat( char ratIndex ); +static CellularPktStatus_t _Cellular_RecvFuncGetRatPriority( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static int16_t convertCesqSignalRxlev( int32_t rxlevValue ); +static int16_t convertCesqSignalRsrq( int32_t rsrqValue ); +static int16_t convertCesqSignalRsrp( int32_t rsrpValue ); +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ); +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSocketStat( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL, * pToken = NULL; + socketStat_t * pSocketStat = ( socketStat_t * ) pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "GetSocketStat: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSocketStat == NULL ) || ( dataLen != sizeof( socketStat_t ) ) ) + { + LogError( ( "GetSocketStat: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSocketStat: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + /* +KTCPSTAT: ,,,,. */ + /* Remove prefix and leading space. */ + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pInputLine ); + } + + /* Get token status. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= TCP_SOCKET_STATE_NOT_DEFINED ) && ( tempValue < TCP_SOCKET_STATE_MAX ) ) + { + pSocketStat->status = ( tcpSocketState_t ) tempValue; + } + else + { + LogError( ( "GetSocketStat unknown status %d", tempValue ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Get token tcp_notif. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= TCP_NOTIF_OK ) && ( tempValue < TCP_NOTIF_MAX ) ) + { + pSocketStat->tcpNotif = ( tcpConnectionFailure_t ) tempValue; + } + else + { + LogError( ( "GetSocketStat unknown tcp_notif %d", tempValue ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Get token rem_data. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue < UINT16_MAX ) ) + { + pSocketStat->remData = ( uint16_t ) tempValue; + } + else + { + LogError( ( "GetSocketStat unknown rem_data %d", tempValue ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Get token rcv_data. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue < UINT16_MAX ) ) + { + pSocketStat->rcvData = ( uint16_t ) tempValue; + } + else + { + LogError( ( "GetSocketStat unknown rcvData %d", tempValue ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_GetSocketStat( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + socketStat_t * pSocketStat ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketStat = + { + cmdBuf, + CELLULAR_AT_WITH_PREFIX, + "+KTCPSTAT", + _Cellular_RecvFuncGetSocketStat, + pSocketStat, + sizeof( socketStat_t ), + }; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint32_t sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + /* Internal function. Caller checks parameters. */ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "AT+KTCPSTAT=%lu", sessionId ); + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketStat, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t * pRecvDataLength = ( uint32_t * ) pCallbackContext; + + if( ( pLine == NULL ) || ( ppDataStart == NULL ) || ( pDataLength == NULL ) || ( pCallbackContext == NULL ) ) + { + LogError( ( "socketRecvData: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( lineLength < ( SOCKET_DATA_CONNECT_TOKEN_LEN + 2U ) ) && ( *pRecvDataLength > 0 ) ) + { + /* Need more data to decide the data prefix. */ + pktStatus = CELLULAR_PKT_STATUS_SIZE_MISMATCH; + } + else + { + if( strncmp( pLine, SOCKET_DATA_CONNECT_TOKEN, SOCKET_DATA_CONNECT_TOKEN_LEN ) == 0 ) + { + /* The string length of "CONNECT\r\n". */ + *ppDataStart = ( char * ) &pLine[ SOCKET_DATA_CONNECT_TOKEN_LEN + 2 ]; /* Indicate the data start in pLine. */ + *pDataLength = *pRecvDataLength; /* Return the data length from pCallbackContext. */ + *pRecvDataLength = 0; + } + else + { + /* Prefix string which is not "CONNECT" does't indicate data start. Set data length to 0.*/ + *pDataLength = 0; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const char * pInputLine = NULL; + uint32_t dataLenToCopy = 0; + + /* Check if the received data size is greater than the output buffer size. */ + if( *pDataRecv->pDataLen > outBufSize ) + { + LogError( ( "Data is turncated, received data length %d, out buffer size %d", + *pDataRecv->pDataLen, outBufSize ) ); + dataLenToCopy = outBufSize; + *pDataRecv->pDataLen = outBufSize; + } + else + { + dataLenToCopy = *pDataRecv->pDataLen; + } + + /* Data is stored in the next intermediate response. */ + if( pAtResp->pItm->pNext != NULL ) + { + pInputLine = pAtResp->pItm->pNext->pLine; + + if( ( pInputLine != NULL ) && ( dataLenToCopy > 0U ) ) + { + /* Copy the data to the out buffer. */ + ( void ) memcpy( ( void * ) pDataRecv->pData, ( const void * ) pInputLine, dataLenToCopy ); + } + else + { + LogError( ( "Receive Data: paramerter error, data pointer %p, data to copy %d", + pInputLine, dataLenToCopy ) ); + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + } + else if( *pDataRecv->pDataLen == 0U ) + { + /* Receive command success but no data. */ + LogDebug( ( "Receive Data: no data" ) ); + } + else + { + LogError( ( "Receive Data: Intermediate response empty" ) ); + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char * pInputLine = NULL, * pEndPatternLine = NULL; + const _socketDataRecv_t * pDataRecv = ( _socketDataRecv_t * ) pData; + + if( pContext == NULL ) + { + LogError( ( "Receive Data: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "Receive Data: response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp->pItm->pNext == NULL ) || ( pAtResp->pItm->pNext->pNext == NULL ) ) + { + LogError( ( "Receive Data: response data or end pattern is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pDataRecv == NULL ) || ( pDataRecv->pData == NULL ) || ( pDataRecv->pDataLen == NULL ) ) + { + LogError( ( "Receive Data: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; /* The first item is the data prefix. */ + pEndPatternLine = pAtResp->pItm->pNext->pNext->pLine; /* The third item is the end pattern. */ + + /* Check the data prefix token "CONNECT". */ + if( strncmp( pInputLine, SOCKET_DATA_CONNECT_TOKEN, SOCKET_DATA_CONNECT_TOKEN_LEN ) != 0 ) + { + LogError( ( "response item error in prefix CONNECT" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + + /* Check the data end pattern. */ + if( strncmp( pEndPatternLine, SOCKET_END_PATTERN, SOCKET_END_PATTERN_LEN ) != 0 ) + { + LogError( ( "response item error in end pattern"SOCKET_END_PATTERN ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + + /* Process the data buffer. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = getDataFromResp( pAtResp, pDataRecv, dataLen ); + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t buildSocketConfig( CellularSocketHandle_t socketHandle, + char * pCmdBuf ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + /* +1 size in buffer for ',' */ + char portBuf[ CELLULAR_PORT_NUM_CHAR_LEN + 1 ] = { 0 }; + + if( pCmdBuf == NULL ) + { + LogDebug( ( "buildSocketConfig: Invalid command buffer" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketProtocol != CELLULAR_SOCKET_PROTOCOL_TCP ) + { + LogError( ( "buildSocketConfig: socket protocol unsupported %d", + socketHandle->socketProtocol ) ); + cellularStatus = CELLULAR_UNSUPPORTED; + } + else if( strlen( socketHandle->remoteSocketAddress.ipAddress.ipAddress ) > CELLULAR_REMOTE_IP_ADDR_MAX_LENGTH ) + { + /* The maximum length of domain name is 127 in HL7802. */ + LogError( ( "buildSocketConfig: the remote server's address is too long, length=%u", + strlen( socketHandle->remoteSocketAddress.ipAddress.ipAddress ) ) ); + cellularStatus = CELLULAR_UNSUPPORTED; + } + else + { + /* Form the AT command. */ + + /* The return value of snprintf is not used. + * The max length of the string is fixed and checked offline. + * Reserve buffer for port setting. */ + /* coverity[misra_c_2012_rule_21_6_violation]. */ + ( void ) snprintf( pCmdBuf, CELLULAR_AT_CMD_MAX_SIZE - sizeof( portBuf ), + "AT+KTCPCFG=%u,0,\"%s\",%u", + socketHandle->contextId, + socketHandle->remoteSocketAddress.ipAddress.ipAddress, + socketHandle->remoteSocketAddress.port ); + + /* Set the local port in the end of command buffer string if localPort is not 0. */ + if( socketHandle->localPort > 0 ) + { + ( void ) snprintf( portBuf, sizeof( portBuf ), + ",%u", + socketHandle->localPort ); + + /* Because the length of host's IP address is limited, + * the buffer size must be enough for port setting. */ + strcat( pCmdBuf, portBuf ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pRemoteSocketAddress == NULL ) || ( socketHandle == NULL ) ) + { + LogDebug( ( "storeAccessModeAndAddress: Invalid socket address" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_ALLOCATED ) + { + LogError( ( "storeAccessModeAndAddress, bad socket state %d", + 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; + socketHandle->dataMode = dataAccessMode; + socketHandle->remoteSocketAddress.ipAddress.ipAddressType = + pRemoteSocketAddress->ipAddress.ipAddressType; + ( void ) strncpy( socketHandle->remoteSocketAddress.ipAddress.ipAddress, + pRemoteSocketAddress->ipAddress.ipAddress, + CELLULAR_IP_ADDRESS_MAX_SIZE + 1U ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetTcpCfgSessionId( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + uint8_t * pSessionId = pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "GetTcpCfgSessionId: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSessionId == NULL ) || ( dataLen != sizeof( uint8_t ) ) ) + { + LogError( ( "GetTcpCfgSessionId: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetTcpCfgSessionId: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pInputLine, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + *pSessionId = ( uint8_t ) tempValue; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_getTcpCfgSessionId( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + uint8_t * pSessionId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketConnect = + { + cmdBuf, + CELLULAR_AT_WITH_PREFIX, + "+KTCPCFG", + _Cellular_RecvFuncGetTcpCfgSessionId, + pSessionId, + sizeof( uint8_t ), + }; + + /* Internal function. Caller checks parameters. */ + cellularStatus = buildSocketConfig( socketHandle, cmdBuf ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketConnect, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketConnect: Socket connect failed, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseEidrxToken( char * pToken, + uint8_t tokenIndex, + CellularEidrxSettingsList_t * pEidrxSettingsList, + uint8_t count ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + switch( tokenIndex ) + { + case CELLULAR_CEDRXS_POS_ACT: + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pEidrxSettingsList->eidrxList[ count ].mode = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing mode value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + case CELLULAR_CEDRXS_POS_RAT: + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pEidrxSettingsList->eidrxList[ count ].rat = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing Requested rat value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + + case CELLULAR_CEDRXS_POS_REQUESTED_EDRX_VALUE: + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pEidrxSettingsList->eidrxList[ count ].requestedEdrxVaue = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing Requested Edrx value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + case CELLULAR_CEDRXS_POS_NW_PROVIDED_EDRX_VALUE: + /* Unused value. */ + break; + + case CELLULAR_CEDRXS_POS_REQUESTED_PTW_VALUE: + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + pEidrxSettingsList->eidrxList[ count ].pagingTimeWindow = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in processing Requested paging time window value. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + case CELLULAR_CEDRXS_POS_NW_PROVIDED_PTW_VALUE: + /* Unused value. */ + break; + + default: + LogDebug( ( "Unknown Parameter Position %u in AT+CEDRXS Response", tokenIndex ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parseEidrxLine( char * pInputLine, + uint8_t count, + CellularEidrxSettingsList_t * pEidrxSettingsList ) +{ + char * pToken = NULL; + char * pLocalInputLine = pInputLine; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + uint8_t tokenIndex = 0; + + atCoreStatus = Cellular_ATRemovePrefix( &pLocalInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pLocalInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( pToken != NULL ) + { + if( parseEidrxToken( pToken, tokenIndex, pEidrxSettingsList, count ) != CELLULAR_AT_SUCCESS ) + { + LogInfo( ( "parseEidrxToken %s index %d failed", pToken, tokenIndex ) ); + } + + tokenIndex++; + + if( Cellular_ATGetNextTok( &pLocalInputLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetEidrxSettings( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + uint8_t count = 0; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularEidrxSettingsList_t * pEidrxSettingsList = NULL; + const CellularATCommandLine_t * pCommnadItem = NULL; + + if( pContext == NULL ) + { + LogError( ( "GetEidrxSettings: Invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || + ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || + ( pData == NULL ) || + ( dataLen != CELLULAR_EDRX_LIST_MAX_SIZE ) ) + { + LogError( ( "GetEidrxSettings: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pEidrxSettingsList = ( CellularEidrxSettingsList_t * ) pData; + pCommnadItem = pAtResp->pItm; + + while( pCommnadItem != NULL ) + { + pInputLine = pCommnadItem->pLine; + atCoreStatus = parseEidrxLine( pInputLine, count, pEidrxSettingsList ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "GetEidrx: Parsing Error encountered, atCoreStatus: %d", atCoreStatus ) ); + } + else + { + LogDebug( ( "GetEidrx setting[%d]: RAT: %d, Value: 0x%x", + count, pEidrxSettingsList->eidrxList[ count ].rat, pEidrxSettingsList->eidrxList[ count ].requestedEdrxVaue ) ); + } + + pCommnadItem = pCommnadItem->pNext; + count++; + pEidrxSettingsList->count = count; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularRat_t convertKselacqIndexToRat( char ratIndex ) +{ + CellularRat_t rat = CELLULAR_RAT_INVALID; + + switch( ratIndex ) + { + case KSELACQ_RAT_CATM_CHAR: + rat = CELLULAR_RAT_CATM1; + break; + + case KSELACQ_RAT_NBIOT_CHAR: + rat = CELLULAR_RAT_NBIOT; + break; + + case KSELACQ_RAT_GSM_CHAR: + rat = CELLULAR_RAT_GSM; + break; + + default: + break; + } + + return rat; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetRatPriority( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularRat_t * pRatPriorities = NULL; + uint32_t ratIndex = 0; + uint32_t maxRatPriorityLength = ( dataLen > RAT_PRIOIRTY_LIST_LENGTH ? RAT_PRIOIRTY_LIST_LENGTH : dataLen ); + + if( pContext == NULL ) + { + LogError( ( "GetRatPriority: Invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) || ( dataLen == 0U ) ) + { + LogError( ( "GetRatPriority: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + pRatPriorities = ( CellularRat_t * ) pData; + + /* Response string "+KSELACQ: 1,2,3". */ + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + for( ratIndex = 0; ratIndex < maxRatPriorityLength; ratIndex++ ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pToken ); + } + + if( ( atCoreStatus == CELLULAR_AT_SUCCESS ) && ( strlen( pToken ) == 1 ) ) + { + pRatPriorities[ ratIndex ] = convertKselacqIndexToRat( pToken[ 0 ] ); + } + else + { + pRatPriorities[ ratIndex ] = CELLULAR_RAT_INVALID; + } + } + } + + for( ; ratIndex < dataLen; ratIndex++ ) + { + pRatPriorities[ ratIndex ] = CELLULAR_RAT_INVALID; + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Received signal strength level (see 3GPP TS 45.008 [20] subclause 8.1.4) + * 0 rssi < -110 dBm : assume -111 dBm to indicate the signal is detectable but fairly week. + * 1 -110 dBm <= rssi < -109 dBm + * 2 -109 dBm <= rssi < -108 dBm + * ... + * 61 -50 dBm <= rssi < -49 dBm + * 62 -49 dBm <= rssi < -48 dBm + * 63 -48 dBm <= rssi + * 99 not known or not detectable */ +static int16_t convertCesqSignalRxlev( int32_t rxlevValue ) +{ + int16_t rssidBm = 0; + + if( ( rxlevValue >= 0 ) && ( rxlevValue <= 63 ) ) + { + rssidBm = ( int16_t ) ( ( -111 ) + ( rxlevValue ) ); + } + else + { + rssidBm = CELLULAR_INVALID_SIGNAL_VALUE; + } + + return rssidBm; +} + +/*-----------------------------------------------------------*/ + +/* 0 rsrq < -19.5 dB + * 1 -19.5 dB <= rsrq < -19 dB + * 2 -19 dB <= rsrq < -18.5 dB + * ... + * 32 -4 dB <= rsrq < -3.5 dB + * 33 -3.5 dB <= rsrq < -3 dB + * 34 -3 dB <= rsrq + * 255 Not known or not detectable. */ +static int16_t convertCesqSignalRsrq( int32_t rsrqValue ) +{ + int16_t rsrqDb = 0; + + if( ( rsrqValue >= 0 ) && ( rsrqValue <= 34 ) ) + { + rsrqDb = ( int16_t ) ( ( -20 ) + ( rsrqValue * 0.5 ) ); + } + else + { + rsrqDb = CELLULAR_INVALID_SIGNAL_VALUE; + } + + return rsrqDb; +} + +/*-----------------------------------------------------------*/ + +/* 0 rsrp < -140 dBm + * 1 -140 dBm <= rsrp < -139 dBm + * 2 -139 dBm <= rsrp < -138 dBm + * ... + * 95 -46 dBm <= rsrp < -45 dBm + * 96 -45 dBm <= rsrp < -44 dBm + * 97 -44 dBm <= rsrp + * 255 Not known or not detectable. */ +static int16_t convertCesqSignalRsrp( int32_t rsrpValue ) +{ + int16_t rsrpDb = 0; + + if( ( rsrpValue >= 0 ) && ( rsrpValue <= 97 ) ) + { + rsrpDb = ( int16_t ) ( ( -141 ) + ( rsrpValue ) ); + } + else + { + rsrpDb = CELLULAR_INVALID_SIGNAL_VALUE; + } + + return rsrpDb; +} + +/*-----------------------------------------------------------*/ + +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ) +{ + char * pToken = NULL, * pTmpQcsqPayload = pQcsqPayload; + int32_t tempValue = 0; + int16_t berValue = 0; + bool parseStatus = true; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* The cesq payload format is ,,,,,. */ + + /* rxlev. */ + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rssi = convertCesqSignalRxlev( tempValue ); + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RXLEV. Token %s", pToken ) ); + parseStatus = false; + } + } + + /* ber. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( ( atCoreStatus == CELLULAR_AT_SUCCESS ) && ( tempValue <= INT16_MAX ) && ( tempValue >= INT16_MIN ) ) + { + /* As RXQUAL values in the table in 3GPP TS 45.008 [20] subclause 8.2.4. + * The CESQ ber signal value has the same scale with the AT+CSQ ber signal value. */ + cellularStatus = _Cellular_ConvertCsqSignalBer( ( int16_t ) tempValue, &berValue ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pSignalInfo->ber = berValue; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + else + { + LogError( ( "_parseSignalQuality: Error in processing BER. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Skip rscp. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + /* Skip ecno. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + /* rsrq. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rsrq = convertCesqSignalRsrq( tempValue ); + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSRQ. Token %s", pToken ) ); + parseStatus = false; + } + } + + /* rsrp. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSignalInfo->rsrp = convertCesqSignalRsrp( tempValue ); + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSRP. Token %s", pToken ) ); + parseStatus = false; + } + } + + return parseStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularSignalInfo_t * pSignalInfo = ( CellularSignalInfo_t * ) pData; + bool parseStatus = true; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( pContext == NULL ) + { + LogError( ( "GetSignalInfo: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSignalInfo == NULL ) || ( dataLen != sizeof( CellularSignalInfo_t ) ) ) + { + LogError( ( "GetSignalInfo: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSignalInfo: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + parseStatus = _parseSignalQuality( pInputLine, pSignalInfo ); + } + + if( parseStatus != true ) + { + pSignalInfo->rssi = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->bars = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetDns( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pDnsServerAddress ) +{ + ( void ) cellularHandle; + ( void ) contextId; + ( void ) pDnsServerAddress; + + /* Modem use dynamic DNS addresses. Return unsupported. */ + return CELLULAR_UNSUPPORTED; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint8_t * pBuffer, + uint32_t bufferLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pReceivedDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + uint32_t recvTimeout = CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS; + uint32_t recvLen = bufferLength; + _socketDataRecv_t dataRecv = + { + pReceivedDataLength, + pBuffer + }; + CellularAtReq_t atReqSocketRecv = + { + cmdBuf, + CELLULAR_AT_MULTI_DATA_WO_PREFIX, + "CONNECT", + _Cellular_RecvFuncData, + ( void * ) &dataRecv, + bufferLength, + }; + socketStat_t socketStat = { 0 }; + uint32_t socktCmdDataLength = 0; + uint32_t sessionId = 0; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "_Cellular_CheckLibraryStatus failed." ) ); + } + else if( socketHandle == NULL ) + { + LogError( ( "_Cellular_RecvData: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pBuffer == NULL ) || ( pReceivedDataLength == NULL ) || ( bufferLength == 0U ) ) + { + LogError( ( "_Cellular_RecvData: Bad input Param." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( ( socketHandle->socketState != SOCKETSTATE_CONNECTED ) && ( socketHandle->socketState != SOCKETSTATE_DISCONNECTED ) ) + { + /* Check the socket connection state. */ + /* For SOCKETSTATE_DISCONNECTED, there may be packets that can be read even though socket is disconnected by remote. */ + LogInfo( ( "Cellular_SocketRecv: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketRecv : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Calculate the read length. */ + cellularStatus = _Cellular_GetSocketStat( cellularHandle, socketHandle, &socketStat ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + socktCmdDataLength = socketStat.rcvData; + } + else + { + socktCmdDataLength = 0; + } + + /* Update recvLen to maximum module length. */ + if( CELLULAR_MAX_RECV_DATA_LEN <= bufferLength ) + { + recvLen = ( uint32_t ) CELLULAR_MAX_RECV_DATA_LEN; + } + + /* Update the recvLen to available data length in cellular module. */ + if( recvLen > socktCmdDataLength ) + { + recvLen = socktCmdDataLength; + } + + *pReceivedDataLength = recvLen; + socktCmdDataLength = recvLen; + + /* Update receive timeout to default timeout if not set with setsocketopt. */ + if( recvLen > 0 ) + { + if( socketHandle->recvTimeoutMs != 0U ) + { + recvTimeout = socketHandle->recvTimeoutMs; + } + + /* 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%u,%u", "AT+KTCPRCV=", sessionId, recvLen ); + pktStatus = _Cellular_TimeoutAtcmdDataRecvRequestWithCallback( pContext, + atReqSocketRecv, recvTimeout, socketRecvDataPrefix, &socktCmdDataLength ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + /* Reset data handling parameters. */ + LogError( ( "Cellular_SocketRecv: Data Receive fail, pktStatus: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketSend( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + const uint8_t * pData, + uint32_t dataLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pSentDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t sendTimeout = CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketSend = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + CellularAtDataReq_t atDataReqSocketSend = + { + pData, + dataLength, + pSentDataLength, + ( const uint8_t * ) SOCKET_END_PATTERN, + SOCKET_END_PATTERN_LEN + }; + uint32_t sessionId = 0; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "_Cellular_CheckLibraryStatus failed." ) ); + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketSend: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( pSentDataLength == NULL ) || ( dataLength == 0U ) ) + { + LogError( ( "Cellular_SocketSend: Invalid parameter." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_CONNECTED ) + { + /* Check the socket connection state. */ + LogInfo( ( "Cellular_SocketSend: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketSend : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Send data length check. */ + if( dataLength > ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN ) + { + atDataReqSocketSend.dataLen = ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN; + } + + /* Check send timeout. If not set by setsockopt, use default value. */ + if( socketHandle->sendTimeoutMs != 0U ) + { + sendTimeout = socketHandle->sendTimeoutMs; + } + + /* 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%u,%u", + "AT+KTCPSND=", sessionId, atDataReqSocketSend.dataLen ); + + pktStatus = _Cellular_TimeoutAtcmdDataSendSuccessToken( pContext, atReqSocketSend, atDataReqSocketSend, + CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS, sendTimeout, + _socketSendSuccesTokenTable, _socketSendSuccesTokenTableLength ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + if( socketHandle->socketState == SOCKETSTATE_DISCONNECTED ) + { + LogWarn( ( "Cellular_SocketSend: Data send fail. Socket is closed during the send operation." ) ); + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + else + { + LogError( ( "Cellular_SocketSend: Data send fail, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketClose( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketClose = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + uint32_t sessionId = 0U; + cellularModuleContext_t * pModuleContext = NULL; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( socketHandle == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else + { + if( socketHandle->socketState == SOCKETSTATE_CONNECTING ) + { + LogWarn( ( "Cellular_SocketClose: Socket state is SOCKETSTATE_CONNECTING." ) ); + } + + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketSend : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Remove the mapping. */ + pModuleContext->pSessionMap[ sessionId ] = INVALID_SOCKET_INDEX; + + /* Close the socket. */ + if( socketHandle->socketState == SOCKETSTATE_CONNECTED ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "%s%lu,1", + "AT+KTCPCLOSE=", sessionId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketClose, + CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS ); + + /* Delete the socket config. */ + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogWarn( ( "Cellular_SocketClose: AT+KTCPCLOSE fail, PktRet: %d", pktStatus ) ); + } + } + + if( ( socketHandle->socketState == SOCKETSTATE_CONNECTED ) || + ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) || + ( socketHandle->socketState == SOCKETSTATE_DISCONNECTED ) ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "%s%lu", + "AT+KTCPDEL=", sessionId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketClose, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketClose: AT+KTCPDEL fail, PktRet: %d", pktStatus ) ); + } + } + + /* Ignore the result from the info, and force to remove the socket. */ + cellularStatus = _Cellular_RemoveSocketData( pContext, socketHandle ); + } + + return CELLULAR_SUCCESS; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketConnect( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + uint8_t sessionId = 0; + CellularAtReq_t atReqSocketConnect = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + cellularModuleContext_t * pModuleContext = NULL; + + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "_Cellular_CheckLibraryStatus failed." ) ); + } + else if( pRemoteSocketAddress == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket address." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( socketHandle->socketState == SOCKETSTATE_CONNECTED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + LogError( ( "Cellular_SocketConnect: Not allowed in state %d.", socketHandle->socketState ) ); + cellularStatus = CELLULAR_NOT_ALLOWED; + } + else + { + cellularStatus = storeAccessModeAndAddress( pContext, socketHandle, dataAccessMode, pRemoteSocketAddress ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + /* Set socket config and get session id. The session id is defined by the modem. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Builds the Socket connect command. */ + cellularStatus = _Cellular_getTcpCfgSessionId( pContext, socketHandle, &sessionId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Create the reverse table to store the socketIndex to sessionId. */ + pModuleContext->pSessionMap[ sessionId ] = socketHandle->socketId; + } + } + + /* Start the tcp connection. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Set the socket state to connecting state. If cellular modem returns error, + * revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_CONNECTING; + + /* 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_MAX_SIZE, + "AT+KTCPCNX=%u", sessionId ); + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketConnect, + CELLULAR_HL7802_AT_TIMEOUT_30_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketConnect: Socket connect failed, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + /* Revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_ALLOCATED; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardStatus( CellularHandle_t cellularHandle, + CellularSimCardStatus_t * pSimCardStatus ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* Parameters are checked in this API. */ + cellularStatus = Cellular_CommonGetSimCardLockStatus( cellularHandle, pSimCardStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = Cellular_CommonGetSimCardLockStatus( cellularHandle, pSimCardStatus ); + + if( ( cellularStatus == CELLULAR_SUCCESS ) && + ( pSimCardStatus->simCardLockState != CELLULAR_SIM_CARD_INVALID ) && + ( pSimCardStatus->simCardLockState != CELLULAR_SIM_CARD_LOCK_UNKNOWN ) ) + { + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_INSERTED; + } + else + { + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_UNKNOWN; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcSignalStrengthChangedCallback( CellularHandle_t cellularHandle, + CellularUrcSignalStrengthChangedCallback_t signalStrengthChangedCallback, + void * pCallbackContext ) +{ + ( void ) cellularHandle; + ( void ) signalStrengthChangedCallback; + ( void ) pCallbackContext; + return CELLULAR_UNSUPPORTED; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pcHostName, + char * pResolvedAddress ) +{ + ( void ) cellularHandle; + ( void ) contextId; + ( void ) pcHostName; + ( void ) pResolvedAddress; + return CELLULAR_UNSUPPORTED; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_DeactivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqDeactPdn = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s=%u", "AT+KCNXDOWN", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqDeactPdn, + CELLULAR_HL7802_AT_KCNXDOWN_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_DeactivatePdn: can't deactivate PDN, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncPacketSwitchStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + bool * pPacketSwitchStatus = ( bool * ) pData; + + if( pContext == NULL ) + { + LogError( ( "PacketSwitchStatus: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( dataLen != sizeof( bool ) ) ) + { + LogError( ( "GetPacketSwitchStatus: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetPacketSwitchStatus: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + + /* Remove prefix. */ + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + /* Remove leading space. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( *pInputLine == '0' ) + { + *pPacketSwitchStatus = false; + } + else if( *pInputLine == '1' ) + { + *pPacketSwitchStatus = true; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_GetPacketSwitchStatus( CellularHandle_t cellularHandle, + bool * pPacketSwitchStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqPacketSwitchStatus = + { + "AT+CGATT?", + CELLULAR_AT_WITH_PREFIX, + "+CGATT", + _Cellular_RecvFuncPacketSwitchStatus, + pPacketSwitchStatus, + sizeof( bool ), + }; + + /* Internal function. Callee check parameters. */ + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqPacketSwitchStatus, + CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ActivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + bool packetSwitchStatus = false; + + CellularAtReq_t atReqActPdn = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Check packet switch attach first. If not attached, attach packet switch first. */ + cellularStatus = _Cellular_GetPacketSwitchStatus( cellularHandle, &packetSwitchStatus ); + + if( ( cellularStatus == CELLULAR_SUCCESS ) && ( packetSwitchStatus == false ) ) + { + LogError( ( "Activate Packet switch" ) ); + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s", "AT+CGATT=1" ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqActPdn, + CELLULAR_HL7802_AT_TIMEOUT_60_SECONDS_MS ); + } + else if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Packet switch query failed" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + LogInfo( ( "Packet switch attached" ) ); + } + + /* Check the current attach status. If not activated, activate the PDN context ID. */ + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s=%u", "AT+KCNXUP", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqActPdn, + CELLULAR_HL7802_AT_KCNXUP_TIMEOUT_MS ); + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_ActivatePdn: can't activate PDN, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextId( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MIN ) && + ( tempValue <= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MAX ) ) + { + pPdnStatusBuffers->contextId = ( uint8_t ) tempValue; + } + else + { + LogError( ( "Error in Processing Context Id. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextState( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + int32_t tempValue = 0; + CellularATError_t atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && + ( tempValue <= ( int32_t ) UINT8_MAX ) ) + { + /* Remap the status. */ + switch( tempValue ) + { + case CELLULAR_PDN_STATE_CONNECTED: + pPdnStatusBuffers->state = 1; + break; + + case CELLULAR_PDN_STATE_DISCONNECTED: + case CELLULAR_PDN_STATE_CONNECTING: + case CELLULAR_PDN_STATE_IDLE: + case CELLULAR_PDN_STATE_DISCONNECTING: + pPdnStatusBuffers->state = 0; + break; + + default: + LogError( ( "parsePdnStatusContextState unknown status : %d", tempValue ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + } + else + { + LogError( ( "Error in processing PDN Status Buffer state. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t parsePdnStatusContextType( char * pToken, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( strcmp( pToken, "IPV4" ) == 0 ) + { + pPdnStatusBuffers->pdnContextType = CELLULAR_PDN_CONTEXT_IPV4; + pPdnStatusBuffers->ipAddress.ipAddressType = CELLULAR_IP_ADDRESS_V4; + } + else if( strcmp( pToken, "IPV6" ) == 0 ) + { + pPdnStatusBuffers->pdnContextType = CELLULAR_PDN_CONTEXT_IPV6; + pPdnStatusBuffers->ipAddress.ipAddressType = CELLULAR_IP_ADDRESS_V6; + } + else if( strcmp( pToken, "IPV4V6" ) == 0 ) + { + pPdnStatusBuffers->pdnContextType = CELLULAR_PDN_CONTEXT_IPV4V6; + /* The IP address depends on the format returned. */ + } + else + { + LogError( ( "parsePdnStatusContextType : unknown token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getPdnStatusParseToken( char * pToken, + uint8_t tokenIndex, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + switch( tokenIndex ) + { + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_ID ): + LogDebug( ( "CELLULAR_PDN_STATUS_POS_CONTEXT_ID: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextId( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_AF ): + LogDebug( ( "CELLULAR_PDN_STATUS_POS_AF: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextType( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_STATE ): + LogDebug( ( "CELLULAR_PDN_STATUS_POS_STATE: %s", pToken ) ); + atCoreStatus = parsePdnStatusContextState( pToken, pPdnStatusBuffers ); + break; + + case ( CELLULAR_PDN_STATUS_POS_APN ): + case ( CELLULAR_PDN_STATUS_POS_LOGIN ): + case ( CELLULAR_PDN_STATUS_POS_PASSWORD ): + case ( CELLULAR_PDN_STATUS_POS_IP ): + case ( CELLULAR_PDN_STATUS_POS_GPRS ): + case ( CELLULAR_PDN_STATUS_POS_DNS1 ): + case ( CELLULAR_PDN_STATUS_POS_DNS2 ): + LogDebug( ( "CELLULAR_PDN_STATUS Ignored index %d : %s", tokenIndex, pToken ) ); + atCoreStatus = CELLULAR_AT_SUCCESS; + break; + + default: + LogError( ( "Unknown token in getPdnStatusParseToken %s %d", + pToken, tokenIndex ) ); + atCoreStatus = CELLULAR_AT_ERROR; + break; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getPdnStatusParseLine( char * pRespLine, + CellularPdnStatus_t * pPdnStatusBuffers ) +{ + char * pToken = NULL; + char * pLocalRespLine = pRespLine; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + uint8_t tokenIndex = 0; + + atCoreStatus = Cellular_ATRemovePrefix( &pLocalRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pLocalRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalRespLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( ( pToken != NULL ) && ( atCoreStatus == CELLULAR_AT_SUCCESS ) ) + { + atCoreStatus = getPdnStatusParseToken( pToken, tokenIndex, pPdnStatusBuffers ); + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + LogError( ( "getPdnStatusParseToken %s index %d failed", pToken, tokenIndex ) ); + } + + tokenIndex++; + + if( pLocalRespLine[ 0 ] == ',' ) + { + pToken = pLocalRespLine; + pLocalRespLine[ 0 ] = '\0'; + pLocalRespLine = &pLocalRespLine[ 1 ]; + } + else + { + if( Cellular_ATGetNextTok( &pLocalRespLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetPdnStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pRespLine = NULL; + CellularPdnStatus_t * pPdnStatusBuffers = ( CellularPdnStatus_t * ) pData; + uint8_t numStatusBuffers = ( uint8_t ) dataLen; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const CellularATCommandLine_t * pCommnadItem = NULL; + + if( pContext == NULL ) + { + LogError( ( "GetPdnStatus: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) ) + { + LogError( ( "GetPdnStatus: Response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pPdnStatusBuffers == NULL ) || ( numStatusBuffers < 1U ) ) + { + LogError( ( "GetPdnStatus: PDN Status bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetPdnStatus: no activated PDN" ) ); + pPdnStatusBuffers[ 0 ].contextId = INVALID_PDN_INDEX; + pktStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + pCommnadItem = pAtResp->pItm; + + while( ( numStatusBuffers != 0U ) && ( pCommnadItem != NULL ) ) + { + pRespLine = pCommnadItem->pLine; + atCoreStatus = getPdnStatusParseLine( pRespLine, pPdnStatusBuffers ); + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "getPdnStatusParseLine parse %s failed", pRespLine ) ); + break; + } + + pPdnStatusBuffers++; + numStatusBuffers--; + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_GetPdnStatus( CellularHandle_t cellularHandle, + CellularPdnStatus_t * pPdnStatusBuffers, + uint8_t numStatusBuffers, + uint8_t * pNumStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + const CellularPdnStatus_t * pTempPdnStatusBuffer = pPdnStatusBuffers; + uint8_t numBuffers = 0; + CellularAtReq_t atReqGetPdnStatus = + { + "AT+KCNXCFG?", + CELLULAR_AT_WITH_PREFIX, + "+KCNXCFG", + _Cellular_RecvFuncGetPdnStatus, + pPdnStatusBuffers, + numStatusBuffers, + }; + + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pTempPdnStatusBuffer == NULL ) || ( pNumStatus == NULL ) || ( numStatusBuffers < 1u ) ) + { + LogDebug( ( "_Cellular_GetPdnStatus: Bad input Parameter " ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetPdnStatus, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Populate the Valid number of statuses. */ + *pNumStatus = 0; + numBuffers = numStatusBuffers; + + while( numBuffers != 0U ) + { + /* Check if the PDN state is valid. The context ID of the first + * invalid PDN status is set to FF. */ + if( pTempPdnStatusBuffer->contextId <= CELLULAR_PDN_CONTEXT_ID_MAX ) + { + ( *pNumStatus ) += 1U; + } + else + { + break; + } + + numBuffers--; + pTempPdnStatusBuffer++; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetEidrxSettings( CellularHandle_t cellularHandle, + CellularEidrxSettingsList_t * pEidrxSettingsList ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetEidrx = + { + "AT+KEDRXCFG?", + CELLULAR_AT_MULTI_WITH_PREFIX, + "+KEDRXCFG", + _Cellular_RecvFuncGetEidrxSettings, + pEidrxSettingsList, + CELLULAR_EDRX_LIST_MAX_SIZE, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pEidrxSettingsList == NULL ) + { + LogDebug( ( "Cellular_GetEidrxSettings : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + ( void ) memset( pEidrxSettingsList, 0, sizeof( CellularEidrxSettingsList_t ) ); + /* Query the pEidrxSettings from the network. */ + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetEidrx, + CELLULAR_HL7802_AT_KEDRXCFG_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_GetEidrxSettings: couldn't retrieve Eidrx settings" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetEidrxSettings( CellularHandle_t cellularHandle, + const CellularEidrxSettings_t * pEidrxSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSetEidrx = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pEidrxSettings == NULL ) + { + LogDebug( ( "Cellular_SetEidrxSettings : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* 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_MAX_SIZE, "%s%u,%u,%u,%u", + "AT+KEDRXCFG=", + pEidrxSettings->mode, + pEidrxSettings->rat, + pEidrxSettings->requestedEdrxVaue, + pEidrxSettings->pagingTimeWindow ); + LogDebug( ( "Eidrx setting: %s ", cmdBuf ) ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSetEidrx, + CELLULAR_HL7802_AT_KEDRXCFG_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "_Cellular_SetEidrxSettings: couldn't set Eidrx settings" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetRatPriority( CellularHandle_t cellularHandle, + const CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + uint8_t i = 0; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSetRatPriority = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) ) + { + LogDebug( ( "Cellular_SetRatPriority : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Reboot is required for this command. */ + ( void ) strcpy( cmdBuf, "AT+KSELACQ=0," ); + + for( i = 0; i < ratPrioritiesLength; i++ ) + { + if( pRatPriorities[ i ] == CELLULAR_RAT_CATM1 ) + { + ( void ) strcat( cmdBuf, "1" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_NBIOT ) + { + ( void ) strcat( cmdBuf, "2" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_GSM ) + { + ( void ) strcat( cmdBuf, "3" ); + } + else + { + LogDebug( ( "Cellular_SetRatPriority : unsupported mode %d", pRatPriorities[ i ] ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + break; + } + + if( i != ( ratPrioritiesLength - 1 ) ) + { + ( void ) strcat( cmdBuf, "," ); + } + } + + LogError( ( " setRatPriority cmdbuf %s", cmdBuf ) ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSetRatPriority, + CELLULAR_HL7802_AT_KSELACQ_TIMEOUT_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRatPriority( CellularHandle_t cellularHandle, + CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength, + uint8_t * pReceiveRatPrioritesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t ratIndex = 0; + + CellularAtReq_t atReqSetRatPriority = + { + "AT+KSELACQ?", + CELLULAR_AT_WITH_PREFIX, + "+KSELACQ", + _Cellular_RecvFuncGetRatPriority, + pRatPriorities, + ( uint16_t ) ratPrioritiesLength, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) || + ( pReceiveRatPrioritesLength == NULL ) ) + { + LogDebug( ( "Cellular_GetRatPriority : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSetRatPriority, + CELLULAR_HL7802_AT_KSELACQ_TIMEOUT_MS ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + for( ratIndex = 0; ratIndex < ratPrioritiesLength; ratIndex++ ) + { + if( pRatPriorities[ ratIndex ] == CELLULAR_RAT_INVALID ) + { + break; + } + } + + *pReceiveRatPrioritesLength = ratIndex; + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSignalInfo( CellularHandle_t cellularHandle, + CellularSignalInfo_t * pSignalInfo ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularRat_t rat = CELLULAR_RAT_INVALID; + CellularAtReq_t atReqQuerySignalInfo = + { + "AT+CESQ", + CELLULAR_AT_WITH_PREFIX, + "+CESQ", + _Cellular_RecvFuncGetSignalInfo, + pSignalInfo, + sizeof( CellularSignalInfo_t ), + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "_Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSignalInfo == NULL ) + { + LogDebug( ( "Cellular_GetSignalInfo : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_GetCurrentRat( pContext, &rat ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Init the signal value. */ + pSignalInfo->rssi = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->bars = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqQuerySignalInfo, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + /* If the convert failed, the API will return CELLULAR_INVALID_SIGNAL_BAR_VALUE in bars field. */ + ( void ) _Cellular_ComputeSignalBars( rat, pSignalInfo ); + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetPdnConfig( CellularHandle_t cellularHandle, + uint8_t contextId, + const CellularPdnConfig_t * pPdnConfig ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atGprsConnectionConfighReq = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + /* Parameters are also checked in Cellular_CommonSetPdnConfig. */ + cellularStatus = Cellular_CommonSetPdnConfig( cellularHandle, contextId, pPdnConfig ); + + /* Set the GPRS connection configuration. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+KCNXCFG=%u,\"GPRS\",\"%s\"", + contextId, pPdnConfig->apnName ); + LogDebug( ( "cmd %s", cmdBuf ) ); + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atGprsConnectionConfighReq, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_PacketSwitchAttach: failed, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetPsmSettings( CellularHandle_t cellularHandle, + const CellularPsmSettings_t * pPsmSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atKsleepReq = + { + cmdBuf, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + /* Parameters are checked in this function. */ + cellularStatus = Cellular_CommonSetPsmSettings( cellularHandle, pPsmSettings ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Sleep mode driven by a HW signal (DTR). Sleep level is hibernate. + * Can be woken up by WAKE_UP signal or T3412 timer expiration. */ + if( pPsmSettings->mode == 1 ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+KSLEEP=0,2,30" ); + } + else + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+KSLEEP=2" ); + } + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atKsleepReq, + CELLULAR_HL7802_AT_TIMEOUT_2_SECONDS_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_PacketSwitchAttach: failed, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +CellularError_t Cellular_Init( CellularHandle_t * pCellularHandle, + const CellularCommInterface_t * pCommInterface ) +{ + CellularTokenTable_t cellularTokenTable = + { + .pCellularUrcHandlerTable = CellularUrcHandlerTable, + .cellularPrefixToParserMapSize = CellularUrcHandlerTableSize, + .pCellularSrcTokenErrorTable = CellularSrcTokenErrorTable, + .cellularSrcTokenErrorTableSize = CellularSrcTokenErrorTableSize, + .pCellularSrcTokenSuccessTable = CellularSrcTokenSuccessTable, + .cellularSrcTokenSuccessTableSize = CellularSrcTokenSuccessTableSize, + .pCellularUrcTokenWoPrefixTable = CellularUrcTokenWoPrefixTable, + .cellularUrcTokenWoPrefixTableSize = CellularUrcTokenWoPrefixTableSize, + .pCellularSrcExtraTokenSuccessTable = NULL, + .cellularSrcExtraTokenSuccessTableSize = 0 + }; + + return Cellular_CommonInit( pCellularHandle, pCommInterface, &cellularTokenTable ); +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_urc_handler.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_urc_handler.c new file mode 100644 index 0000000000..7855ab2e87 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_urc_handler.c @@ -0,0 +1,349 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include +#include + +#include "cellular_types.h" +#include "cellular_common.h" +#include "cellular_common_api.h" +#include "cellular_common_portable.h" + +/* Cellular module includes. */ +#include "cellular_hl7802.h" + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessKtcpInd( CellularContext_t * pContext, + char * pInputLine ); +static void handleTcpNotif( CellularSocketContext_t * pSocketData, + uint8_t tcpNotif, + uint32_t sessionId ); +static void _cellular_UrcProcessKtcpNotif( CellularContext_t * pContext, + char * pInputLine ); +static void _cellular_UrcProcessKtcpData( CellularContext_t * pContext, + char * pInputLine ); + +/*-----------------------------------------------------------*/ + +/* Try to Keep this map in Alphabetical order. */ +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularAtParseTokenMap_t CellularUrcHandlerTable[] = +{ + { "CEREG", Cellular_CommonUrcProcessCereg }, + { "CREG", Cellular_CommonUrcProcessCreg }, + { "KTCP_DATA", _cellular_UrcProcessKtcpData }, /* TCP data URC. */ + { "KTCP_IND", _cellular_UrcProcessKtcpInd }, /* TCP status URC. */ + { "KTCP_NOTIF", _cellular_UrcProcessKtcpNotif } /* TCP connection failure. */ +}; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcHandlerTableSize = sizeof( CellularUrcHandlerTable ) / sizeof( CellularAtParseTokenMap_t ); + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessKtcpInd( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +KTCP_IND: , 1 + * This URC indicate connection success. */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "error parsing _cellular_UrcProcessKtcpInd session ID" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogWarn( ( "_cellular_UrcProcessKtcpInd : unknown session data received. " + "The session %u may not be closed properly in previous execution.", sessionId ) ); + } + else + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessKtcpInd : invalid socket index %u", socketIndex ) ); + } + else if( pSocketData->openCallback == NULL ) + { + LogDebug( ( "_cellular_UrcProcessKtcpInd : Open callback not set!!" ) ); + } + else + { + LogDebug( ( "Notify session %d with socket opened\r\n", sessionId ) ); + pSocketData->socketState = SOCKETSTATE_CONNECTED; + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPENED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +static void handleTcpNotif( CellularSocketContext_t * pSocketData, + uint8_t tcpNotif, + uint32_t sessionId ) +{ + /* Suppress warning message if log level is not debug. */ + ( void ) sessionId; + + switch( tcpNotif ) + { + case TCP_NOTIF_TCP_DISCONNECTION: /* TCP disconnection by the server or remote client. */ + + pSocketData->socketState = SOCKETSTATE_DISCONNECTED; + + if( pSocketData->closedCallback != NULL ) + { + LogDebug( ( "Notify session %d with socket disconnected\r\n", sessionId ) ); + pSocketData->closedCallback( pSocketData, pSocketData->pClosedCallbackContext ); + } + + break; + + case TCP_NOTIF_TCP_CONNECTION_ERROR: /* TCP connection error. */ + + pSocketData->socketState = SOCKETSTATE_DISCONNECTED; + + if( pSocketData->openCallback != NULL ) + { + LogDebug( ( "Notify session %d with socket open failed\r\n", sessionId ) ); + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPEN_FAILED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + + break; + + default: + break; + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessKtcpNotif( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + tcpConnectionFailure_t tcpNotif = TCP_NOTIF_OK; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +KTCP_NOTIF: , + * This URC indicate connection problem. */ + + /* Remove leading space. */ + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pLocalInputLine ); + + /* Parse the session ID. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "error parsing _cellular_UrcProcessKtcpInd session ID" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Parse the tcp notif. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= TCP_NOTIF_OK ) && ( tempValue <= TCP_NOTIF_MAX ) ) + { + tcpNotif = ( tcpConnectionFailure_t ) tempValue; + } + else + { + LogError( ( "error parsing _cellular_UrcProcessKtcpInd session ID" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogWarn( ( "_cellular_UrcProcessKtcpNotif : unknown session data received. " + "The session %u may not be closed properly in previous execution.", sessionId ) ); + } + else + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessKtcpNotif : invalid socket index %u", socketIndex ) ); + } + else + { + handleTcpNotif( pSocketData, tcpNotif, sessionId ); + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessKtcpData( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +KTCP_DATA: , + * This URC indicate connection problem. */ + + /* parse the session ID. */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "error parsing _cellular_UrcProcessKtcpData session ID" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Indicate the upper layer about the data reception. */ + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogWarn( ( "_cellular_UrcProcessKtcpData : unknown session data received. " + "The session %u may not be closed properly in previous execution.", sessionId ) ); + } + else + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessKtcpData : invalid socket index %u", socketIndex ) ); + } + else if( pSocketData->dataReadyCallback == NULL ) + { + LogDebug( ( "_cellular_UrcProcessKtcpData : Data ready callback not set!!" ) ); + } + else + { + pSocketData->dataReadyCallback( pSocketData, pSocketData->pDataReadyCallbackContext ); + } + } + } + } +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_wrapper.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_wrapper.c new file mode 100644 index 0000000000..8b5967dca5 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-Sierra-Wireless-HL7802/cellular_hl7802_wrapper.c @@ -0,0 +1,269 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* The config header is always included first. */ +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include + +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common.h" +#include "cellular_common_api.h" + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_Cleanup( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonCleanup( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcNetworkRegistrationEventCallback( CellularHandle_t cellularHandle, + CellularUrcNetworkRegistrationCallback_t networkRegistrationCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcNetworkRegistrationEventCallback( cellularHandle, networkRegistrationCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcPdnEventCallback( CellularHandle_t cellularHandle, + CellularUrcPdnEventCallback_t pdnEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcPdnEventCallback( cellularHandle, pdnEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcGenericCallback( CellularHandle_t cellularHandle, + CellularUrcGenericCallback_t genericCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcGenericCallback( cellularHandle, genericCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterModemEventCallback( CellularHandle_t cellularHandle, + CellularModemEventCallback_t modemEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterModemEventCallback( cellularHandle, modemEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ATCommandRaw( CellularHandle_t cellularHandle, + const char * pATCommandPrefix, + const char * pATCommandPayload, + CellularATCommandType_t atCommandType, + CellularATCommandResponseReceivedCallback_t responseReceivedCallback, + void * pData, + uint16_t dataLen ) +{ + return Cellular_CommonATCommandRaw( cellularHandle, pATCommandPrefix, pATCommandPayload, atCommandType, + responseReceivedCallback, pData, dataLen ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_CreateSocket( CellularHandle_t cellularHandle, + uint8_t pdnContextId, + CellularSocketDomain_t socketDomain, + CellularSocketType_t socketType, + CellularSocketProtocol_t socketProtocol, + CellularSocketHandle_t * pSocketHandle ) +{ + return Cellular_CommonCreateSocket( cellularHandle, pdnContextId, socketDomain, socketType, + socketProtocol, pSocketHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketSetSockOpt( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOptionLevel_t optionLevel, + CellularSocketOption_t option, + const uint8_t * pOptionValue, + uint32_t optionValueLength ) +{ + return Cellular_CommonSocketSetSockOpt( cellularHandle, socketHandle, optionLevel, option, + pOptionValue, optionValueLength ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterDataReadyCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketDataReadyCallback_t dataReadyCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterDataReadyCallback( cellularHandle, socketHandle, + dataReadyCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterSocketOpenCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOpenCallback_t socketOpenCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterSocketOpenCallback( cellularHandle, socketHandle, + socketOpenCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterClosedCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketClosedCallback_t closedCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterClosedCallback( cellularHandle, socketHandle, + closedCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOn( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOn( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOff( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOff( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetIPAddress( CellularHandle_t cellularHandle, + uint8_t contextId, + char * pBuffer, + uint32_t bufferLength ) +{ + return Cellular_CommonGetIPAddress( cellularHandle, contextId, pBuffer, bufferLength ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetModemInfo( CellularHandle_t cellularHandle, + CellularModemInfo_t * pModemInfo ) +{ + return Cellular_CommonGetModemInfo( cellularHandle, pModemInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRegisteredNetwork( CellularHandle_t cellularHandle, + CellularPlmnInfo_t * pNetworkInfo ) +{ + return Cellular_CommonGetRegisteredNetwork( cellularHandle, pNetworkInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetNetworkTime( CellularHandle_t cellularHandle, + CellularTime_t * pNetworkTime ) +{ + return Cellular_CommonGetNetworkTime( cellularHandle, pNetworkTime ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetServiceStatus( CellularHandle_t cellularHandle, + CellularServiceStatus_t * pServiceStatus ) +{ + return Cellular_CommonGetServiceStatus( cellularHandle, pServiceStatus ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardInfo( CellularHandle_t cellularHandle, + CellularSimCardInfo_t * pSimCardInfo ) +{ + return Cellular_CommonGetSimCardInfo( cellularHandle, pSimCardInfo ); +} + +/*-----------------------------------------------------------*/ + + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetPsmSettings( CellularHandle_t cellularHandle, + CellularPsmSettings_t * pPsmSettings ) +{ + return Cellular_CommonGetPsmSettings( cellularHandle, pPsmSettings ); +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.c new file mode 100644 index 0000000000..741425d945 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.c @@ -0,0 +1,562 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +#include + +#include "cellular_platform.h" +#include "cellular_common.h" +#include "cellular_common_portable.h" +#include "cellular_r4.h" + +/*-----------------------------------------------------------*/ + +#define ENBABLE_MODULE_UE_RETRY_COUNT ( 3U ) +#define ENBABLE_MODULE_UE_RETRY_TIMEOUT ( 5000U ) +#define ENBABLE_MODULE_UE_REBOOT_POLL_TIME ( 2000U ) +#define ENBABLE_MODULE_UE_REBOOT_MAX_TIME ( 25000U ) + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq ); + +/*-----------------------------------------------------------*/ + +static cellularModuleContext_t cellularHl7802Context = { 0 }; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenErrorTable[] = +{ "ERROR", "BUSY", "NO CARRIER", "NO ANSWER", "NO DIALTONE", "ABORTED", "+CMS ERROR", "+CME ERROR", "SEND FAIL" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenErrorTableSize = sizeof( CellularSrcTokenErrorTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularSrcTokenSuccessTable[] = +{ "OK", "@" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularSrcTokenSuccessTableSize = sizeof( CellularSrcTokenSuccessTable ) / sizeof( char * ); + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +const char * CellularUrcTokenWoPrefixTable[] = +{ "RDY" }; +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcTokenWoPrefixTableSize = sizeof( CellularUrcTokenWoPrefixTable ) / sizeof( char * ); + +/*-----------------------------------------------------------*/ + +static CellularError_t sendAtCommandWithRetryTimeout( CellularContext_t * pContext, + const CellularAtReq_t * pAtReq ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint8_t tryCount = 0; + + if( pAtReq == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + for( ; tryCount < ENBABLE_MODULE_UE_RETRY_COUNT; tryCount++ ) + { + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, *pAtReq, ENBABLE_MODULE_UE_RETRY_TIMEOUT ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + break; + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleInit( const CellularContext_t * pContext, + void ** ppModuleContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint32_t i = 0; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ppModuleContext == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Initialize the module context. */ + ( void ) memset( &cellularHl7802Context, 0, sizeof( cellularModuleContext_t ) ); + + for( i = 0; i < TCP_SESSION_TABLE_LEGNTH; i++ ) + { + cellularHl7802Context.pSessionMap[ i ] = INVALID_SOCKET_INDEX; + } + + *ppModuleContext = ( void * ) &cellularHl7802Context; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleCleanUp( const CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* Parse AT response for current MNO profile */ +static CellularPktStatus_t _Cellular_RecvFuncGetCurrentMNOProfile( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + MNOProfileType_t * pCurrentMNOProfile = ( MNOProfileType_t * ) pData; + char * pToken = NULL; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetCurrentMNOProfile: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( dataLen != sizeof( MNOProfileType_t ) ) ) + { + LogError( ( "_Cellular_RecvFuncGetCurrentMNOProfile: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "_Cellular_RecvFuncGetCurrentMNOProfile: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + + /* Remove prefix. */ + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + /* Remove leading space. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MNO_PROFILE_SW_DEFAULT ) && ( tempValue <= MNO_PROFILE_STANDARD_EUROPE ) ) + { + *pCurrentMNOProfile = tempValue; + LogInfo( ( "_Cellular_RecvFuncGetCurrentMNOProfile: pCurrentMNOProfile [%d]", *pCurrentMNOProfile ) ); + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ +/* Get modem's current MNO profile */ +static CellularError_t _Cellular_GetCurrentMNOProfile( CellularContext_t * pContext, + MNOProfileType_t * pCurrentMNOProfile ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqGetCurrentMNOProfile = + { + "AT+UMNOPROF?", + CELLULAR_AT_WITH_PREFIX, + "+UMNOPROF", + _Cellular_RecvFuncGetCurrentMNOProfile, + NULL, + sizeof( MNOProfileType_t ), + }; + + atReqGetCurrentMNOProfile.pData = pCurrentMNOProfile; + + /* Internal function. Callee check parameters. */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetCurrentMNOProfile ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* Reboot modem and wait for ready state. */ + +CellularError_t rebootCellularModem( CellularContext_t * pContext, + bool disablePsm, + bool disableEidrx ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t count = 0; + + CellularAtReq_t atReqGetNoResult = + { + "AT+CFUN=15", + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + LogInfo( ( "rebootCellularModem: Rebooting Modem." ) ); + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + Platform_Delay( ENBABLE_MODULE_UE_REBOOT_POLL_TIME ); + count = count + ENBABLE_MODULE_UE_REBOOT_POLL_TIME; + + /* wait for modem after reboot*/ + while( count < ENBABLE_MODULE_UE_REBOOT_MAX_TIME ) + { + LogInfo( ( "rebootCellularModem: Use ATE0 command to test modem status." ) ); + + atReqGetNoResult.pAtCmd = "ATE0"; + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqGetNoResult, ENBABLE_MODULE_UE_REBOOT_POLL_TIME ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + LogInfo( ( "rebootCellularModem: Modem is now available." ) ); + + Platform_Delay( ENBABLE_MODULE_UE_REBOOT_POLL_TIME * 3 ); + + /* Query current PSM settings. */ + atReqGetNoResult.pAtCmd = "AT+CPSMS?"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + + if( disablePsm && ( cellularStatus == CELLULAR_SUCCESS ) ) + { + LogInfo( ( "rebootCellularModem: Disable +CPSMS setting." ) ); + atReqGetNoResult.pAtCmd = "AT+CPSMS=0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( disableEidrx && ( cellularStatus == CELLULAR_SUCCESS ) ) + { + LogInfo( ( "rebootCellularModem: Disable +CEDRXS setting." ) ); + atReqGetNoResult.pAtCmd = "AT+CEDRXS=0,4"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + break; + } + else + { + LogWarn( ( "rebootCellularModem: Modem is not ready. Retry sending ATE0." ) ); + } + + count = count + ENBABLE_MODULE_UE_REBOOT_POLL_TIME; + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUE( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + CellularAtReq_t atReqGetWithResult = + { + NULL, + CELLULAR_AT_WO_PREFIX, + NULL, + NULL, + NULL, + 0 + }; + char pAtCmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { 0 }; + + if( pContext != NULL ) + { + /* Disable echo. */ + atReqGetWithResult.pAtCmd = "ATE0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetWithResult ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Disable DTR function. */ + atReqGetNoResult.pAtCmd = "AT&D0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + #ifndef CELLULAR_CONFIG_DISABLE_FLOW_CONTROL + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Enable RTS/CTS hardware flow control. */ + atReqGetNoResult.pAtCmd = "AT+IFC=2,2"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + #endif + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Report verbose mobile termination error. */ + atReqGetNoResult.pAtCmd = "AT+CMEE=2"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Setup mobile network operator profiles. */ + /* Setting +UMNOPROF profile will automatically select suitable values for +URAT and +UBANDMASK. */ + + /* Check current MNO profile first to avoid unneccessary modem reboot. */ + MNOProfileType_t currentMNOProfile = MNO_PROFILE_NOT_SET; + cellularStatus = _Cellular_GetCurrentMNOProfile( pContext, ¤tMNOProfile ); + + LogInfo( ( "Cellular_ModuleEnableUE: currentMNOProfile = [%d], desiredProfile = [%d]", currentMNOProfile, CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE ) ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Set MNO profile if not set already */ + if( ( currentMNOProfile != CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE ) && ( CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE != MNO_PROFILE_NOT_SET ) ) + { + atReqGetNoResult.pAtCmd = pAtCmdBuf; + ( void ) snprintf( ( char * ) atReqGetNoResult.pAtCmd, CELLULAR_AT_CMD_MAX_SIZE, "%s%d", "AT+COPS=2;+UMNOPROF=", CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE ); + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = rebootCellularModem( pContext, true, true ); + } + } + + #ifdef CELLULAR_CONFIG_SARA_R4_REBOOT_ON_INIT + else + { + cellularStatus = rebootCellularModem( pContext, true, true ); + } + #endif + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + atReqGetNoResult.pAtCmd = "AT+COPS=0"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + atReqGetNoResult.pAtCmd = "AT+CFUN=1"; + cellularStatus = sendAtCommandWithRetryTimeout( pContext, &atReqGetNoResult ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ModuleEnableUrc( CellularContext_t * pContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularAtReq_t atReqGetNoResult = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + atReqGetNoResult.pAtCmd = "AT+COPS=3,2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CGREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CEREG=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + atReqGetNoResult.pAtCmd = "AT+CTZR=1"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + /* TODO: +CGEV URC enable. */ + /* atReqGetNoResult.pAtCmd = "AT+CGEREP=2,0"; */ + /* (void)_Cellular_AtcmdRequestWithCallback(pContext, atReqGetNoResult); */ + + /* Power saving mode URC enable. */ + atReqGetNoResult.pAtCmd = "AT+UPSMR=1"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + /* Mobile termination event reporting +CIEV URC enable. */ + atReqGetNoResult.pAtCmd = "AT+CMER=1,0,0,2,1"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + /* Enable signal level change indication via +CIEV URC. (To enable all indications, set to 4095) */ + atReqGetNoResult.pAtCmd = "AT+UCIND=2"; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + /* Enable greeting message "RDY" on modem bootup. */ + atReqGetNoResult.pAtCmd = "AT+CSGT=1,\"RDY\""; + ( void ) _Cellular_AtcmdRequestWithCallback( pContext, atReqGetNoResult ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSocketId( CellularContext_t * pContext, + uint8_t sessionId ) +{ + cellularModuleContext_t * pModuleContext = NULL; + uint32_t socketIndex = INVALID_SOCKET_INDEX; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + if( pContext != NULL ) + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + else + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + + if( ( cellularStatus == CELLULAR_SUCCESS ) && ( sessionId <= ( ( uint8_t ) MAX_TCP_SESSION_ID ) ) ) + { + socketIndex = pModuleContext->pSessionMap[ sessionId ]; + } + + return socketIndex; +} + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSessionId( CellularContext_t * pContext, + uint32_t socketIndex ) +{ + cellularModuleContext_t * pModuleContext = NULL; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint32_t sessionId = INVALID_SESSION_ID; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_GetSessionId invalid cellular context" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogError( ( "_Cellular_GetSessionId invalid socketIndex" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + for( sessionId = 0; sessionId < TCP_SESSION_TABLE_LEGNTH; sessionId++ ) + { + if( pModuleContext->pSessionMap[ sessionId ] == socketIndex ) + { + break; + } + } + + /* Mapping is not found in the session mapping table. */ + if( sessionId == TCP_SESSION_TABLE_LEGNTH ) + { + sessionId = INVALID_SESSION_ID; + } + } + else + { + sessionId = INVALID_SESSION_ID; + } + + return sessionId; +} diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.h b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.h new file mode 100644 index 0000000000..b5640cb66e --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4.h @@ -0,0 +1,164 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#ifndef __CELLULAR_R4_H__ +#define __CELLULAR_R4_H__ + +/* *INDENT-OFF* */ +#ifdef __cplusplus + extern "C" { +#endif +/* *INDENT-ON* */ + +/*-----------------------------------------------------------*/ + +#define MIN_TCP_SESSION_ID ( 0 ) +#define MAX_TCP_SESSION_ID ( 6 ) +#define TCP_SESSION_TABLE_LEGNTH ( MAX_TCP_SESSION_ID + 1 ) + +#define INVALID_SOCKET_INDEX ( UINT32_MAX ) +#define INVALID_SESSION_ID ( UINT32_MAX ) + +/*-----------------------------------------------------------*/ + +typedef struct cellularModuleContext +{ + uint32_t pSessionMap[ TCP_SESSION_TABLE_LEGNTH ]; +} cellularModuleContext_t; + +/*-----------------------------------------------------------*/ + +uint32_t _Cellular_GetSocketId( CellularContext_t * pContext, + uint8_t sessionId ); + +uint32_t _Cellular_GetSessionId( CellularContext_t * pContext, + uint32_t socketIndex ); + +CellularError_t rebootCellularModem( CellularContext_t * pContext, + bool disablePsm, + bool disableEidrx ); + +/*-----------------------------------------------------------*/ + +/** + * @brief Cellular MNO profiles. + */ +typedef enum MNOProfileType +{ + MNO_PROFILE_SW_DEFAULT = 0, + MNO_PROFILE_SIM_ICCID_IMSI_SELECT = 1, + MNO_PROFILE_ATT = 2, + MNO_PROFILE_VERIZON = 3, + MNO_PROFILE_TELSTRA = 4, + MNO_PROFILE_TMOBILE = 5, + MNO_PROFILE_CHINA_TELECOM = 6, + MNO_PROFILE_SPRINT = 8, + MNO_PROFILE_VODAFONE = 19, + MNO_PROFILE_GLOBAL = 90, + MNO_PROFILE_STANDARD_EUROPE = 100, + MNO_PROFILE_NOT_SET = 999 +} MNOProfileType_t; + +/*-----------------------------------------------------------*/ + +/* Select network MNO profile. Default value is MNO_PROFILE_NOT_SET */ +#ifndef CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE + #define CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE ( MNO_PROFILE_NOT_SET ) +#endif + +/* + * By default socket is closed in normal mode i.e. flag is 0. + * In normal mode, +USOCL can take time to close socket (Max timeout is 120 sec). + * To avoid wait, socket can be closed in async mode via flag. + * In mode, socket close will be notified via +UUSOCL URC. + * Drawback of mode is that if try to deactivate context (e.g. AT+CGACT=0,1). + * prior to socket close URC, AT command will result in ERROR. + */ +#define CELLULAR_CONFIG_SET_SOCKET_CLOSE_ASYNC_MODE ( 0U ) + +/*-----------------------------------------------------------*/ + +/* MAX valid PDP contexts */ +#define MAX_PDP_CONTEXTS ( 4U ) + +#define DEFAULT_BEARER_CONTEXT_ID ( 1U ) /* SARA-R4 default bearer context */ + +#define CELULAR_PDN_CONTEXT_TYPE_MAX_SIZE ( 7U ) /* The length of IP type e.g. IPV4V6. */ + +/*-----------------------------------------------------------*/ + +/* +CGDCONT PDN context definition tokens */ +#define CELLULAR_PDN_STATUS_POS_CONTEXT_ID ( 0U ) +#define CELLULAR_PDN_STATUS_POS_CONTEXT_TYPE ( 1U ) +#define CELLULAR_PDN_STATUS_POS_APN_NAME ( 2U ) +#define CELLULAR_PDN_STATUS_POS_IP_ADDRESS ( 3U ) + +/* +CGACT PDN context activation tokens */ +#define CELLULAR_PDN_ACT_STATUS_POS_CONTEXT_ID ( 0U ) +#define CELLULAR_PDN_ACT_STATUS_POS_CONTEXT_STATE ( 1U ) + +/** + * @brief Context info from +CGDCONT (Context IP type, APN name, IP Address) + */ +typedef struct CellularPdnContextInfo +{ + bool contextsPresent[ MAX_PDP_CONTEXTS ]; /**< Context present in +CGDCONT response or not. */ + char ipType[ MAX_PDP_CONTEXTS ][ CELULAR_PDN_CONTEXT_TYPE_MAX_SIZE ]; /**< PDN Context type. */ + char apnName[ MAX_PDP_CONTEXTS ][ CELLULAR_APN_MAX_SIZE ]; /**< APN name. */ + char ipAddress[ MAX_PDP_CONTEXTS ][ CELLULAR_IP_ADDRESS_MAX_SIZE ]; /**< IP address. */ +} CellularPdnContextInfo_t; + +/** + * @brief Context state from +CGACT + */ +typedef struct CellularPdnContextActInfo +{ + bool contextsPresent[ MAX_PDP_CONTEXTS ]; /**< Context present in +CGACT response or not. */ + bool contextActState[ MAX_PDP_CONTEXTS ]; /**< Context active state from +CGACT response. */ +} CellularPdnContextActInfo_t; + +/*-----------------------------------------------------------*/ + +extern CellularAtParseTokenMap_t CellularUrcHandlerTable[]; +extern uint32_t CellularUrcHandlerTableSize; + +extern const char * CellularSrcTokenErrorTable[]; +extern uint32_t CellularSrcTokenErrorTableSize; + +extern const char * CellularSrcTokenSuccessTable[]; +extern uint32_t CellularSrcTokenSuccessTableSize; + +extern const char * CellularUrcTokenWoPrefixTable[]; +extern uint32_t CellularUrcTokenWoPrefixTableSize; + +/*-----------------------------------------------------------*/ + +/* *INDENT-OFF* */ +#ifdef __cplusplus + } +#endif +/* *INDENT-ON* */ + +#endif /* ifndef __CELLULAR_R4_H__ */ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_api.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_api.c new file mode 100644 index 0000000000..741d0ae96f --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_api.c @@ -0,0 +1,2906 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include +#include + +/* Cellular includes. */ +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common_api.h" +#include "cellular_common.h" +#include "cellular_at_core.h" + +/* Cellular module incliudes. */ +#include "cellular_r4.h" + +/*-----------------------------------------------------------*/ + +/* TODO : confirm the value. */ +#define PDN_ACT_PACKET_REQ_TIMEOUT_MS ( 150000UL ) + +#define PDN_DEACT_PACKET_REQ_TIMEOUT_MS ( 40000UL ) + +#define GPRS_ATTACH_REQ_TIMEOUT_MS ( 180000UL ) + +#define DNS_QUERY_REQ_TIMEOUT_MS ( 120000UL ) + +#define SOCKET_CLOSE_PACKET_REQ_TIMEOUT_MS ( 120000U ) + +#define SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ( 120000U ) + +#define CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ( 32U ) + +#define DATA_SEND_TIMEOUT_MS ( 10000U ) + +#define PACKET_REQ_TIMEOUT_MS ( 10000U ) + +#define DATA_READ_TIMEOUT_MS ( 50000UL ) + +#define SOCKET_DATA_PREFIX_TOKEN "+USORD: " +#define SOCKET_DATA_PREFIX_TOKEN_LEN ( 8U ) +#define SOCKET_DATA_PREFIX_STRING_LENGTH ( SOCKET_DATA_PREFIX_TOKEN_LEN + 9U ) +#define RAT_PRIOIRTY_LIST_LENGTH ( 3U ) + +/** + * @brief Parameters involved in receiving data through sockets + */ +typedef struct _socketDataRecv +{ + uint32_t * pDataLen; + uint8_t * pData; +} _socketDataRecv_t; + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ); +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ); +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ); +static CellularPktStatus_t _Cellular_RecvFuncGetSocketId( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t _Cellular_GetSocketNumber( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + uint8_t * pSessionId ); + +CellularError_t Cellular_SetPdnConfig( CellularHandle_t cellularHandle, + uint8_t contextId, + const CellularPdnConfig_t * pPdnConfig ); + +static CellularError_t _Cellular_GetPacketSwitchStatus( CellularHandle_t cellularHandle, + bool * pPacketSwitchStatus ); + +static CellularError_t _Cellular_GetContextActivationStatus( CellularHandle_t cellularHandle, + CellularPdnContextActInfo_t * pPdpContextsActInfo ); + +static bool _parseExtendedSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ); +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ); +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ); +static CellularError_t controlSignalStrengthIndication( CellularContext_t * pContext, + bool enable ); + +CellularError_t Cellular_SetPsmSettings( CellularHandle_t cellularHandle, + const CellularPsmSettings_t * pPsmSettings ); + +static CellularError_t _Cellular_isSockOptSupport( CellularSocketOptionLevel_t optionLevel, + CellularSocketOption_t option ); + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t lineLength, + char ** ppDataStart, + uint32_t * pDataLength ) +{ + char * pToken = NULL; + int32_t tempValue = 0; + CellularATError_t atResult = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char pLocalLine[ SOCKET_DATA_PREFIX_STRING_LENGTH + 1 ] = { '\0' }; + char * pDataStart = pLocalLine; + + ( void ) lineLength; + + /* pCallbackContext is not used in this function. It should be NULL. */ + if( ( pLine == NULL ) || ( ppDataStart == NULL ) || ( pDataLength == NULL ) || ( pCallbackContext != NULL ) ) + { + LogError( ( "socketRecvData: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( strncmp( pLine, SOCKET_DATA_PREFIX_TOKEN, SOCKET_DATA_PREFIX_TOKEN_LEN ) != 0 ) + { + /* Prefix string which is not SOCKET_DATA_PREFIX_TOKEN does't indicate data start. + * Set data length to 0.*/ + *ppDataStart = NULL; + *pDataLength = 0; + } + else + { + /* The string length of "+USORD: [,],"". */ + strncpy( pLocalLine, pLine, SOCKET_DATA_PREFIX_STRING_LENGTH ); + pDataStart = &pLocalLine[ SOCKET_DATA_PREFIX_TOKEN_LEN ]; + + /* Skip the socket number. */ + atResult = Cellular_ATGetNextTok( &pDataStart, &pToken ); + + /* Parse the receive data length. */ + if( ( atResult == CELLULAR_AT_SUCCESS ) && ( pDataStart[ 0 ] == '"' ) ) + { + /* Peek the next symbol. If it is \", then length is ommited. Received byte is 0. */ + LogDebug( ( "socketRecvData: 0 received bytes." ) ); + *pDataLength = 0U; + *ppDataStart = NULL; + } + else if( atResult == CELLULAR_AT_SUCCESS ) + { + /* Parse the length. */ + atResult = Cellular_ATGetNextTok( &pDataStart, &pToken ); + + if( atResult == CELLULAR_AT_SUCCESS ) + { + atResult = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( ( tempValue < 0 ) && ( ( uint32_t ) tempValue > CELLULAR_MAX_RECV_DATA_LEN ) ) + { + LogError( ( "socketRecvData: Bad parameters" ) ); + atResult = CELLULAR_AT_ERROR; + } + } + + if( atResult == CELLULAR_AT_SUCCESS ) + { + /* Change +USROD: ,," to +USROD: ,,\r. */ + pDataStart = &pLine[ pDataStart - pLocalLine ]; + + if( pDataStart[ 0 ] == '\"' ) + { + *pDataStart = '\r'; + *ppDataStart = ( char * ) &pDataStart[ 1 ]; + *pDataLength = ( uint32_t ) tempValue; + } + } + } + else + { + LogDebug( ( "socketRecvDataPrefix : pLine [%s] with lineLength [%d] is not data prefix", pLine, lineLength ) ); + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularATError_t getDataFromResp( const CellularATCommandResponse_t * pAtResp, + const _socketDataRecv_t * pDataRecv, + uint32_t outBufSize ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const char * pInputLine = NULL; + uint32_t dataLenToCopy = 0; + + /* Check if the received data size is greater than the output buffer size. */ + if( *pDataRecv->pDataLen > outBufSize ) + { + LogError( ( "Data is turncated, received data length %u, out buffer size %u", + *pDataRecv->pDataLen, outBufSize ) ); + dataLenToCopy = outBufSize; + *pDataRecv->pDataLen = outBufSize; + } + else + { + dataLenToCopy = *pDataRecv->pDataLen; + } + + /* Data is stored in the next intermediate response. */ + pInputLine = pAtResp->pItm->pNext->pLine; + + if( ( pInputLine != NULL ) && ( dataLenToCopy > 0U ) ) + { + /* Copy the data to the out buffer. */ + ( void ) memcpy( ( void * ) pDataRecv->pData, ( const void * ) pInputLine, dataLenToCopy ); + } + else + { + LogError( ( "Receive Data: paramerter error, data pointer %p, data to copy %u", + pInputLine, dataLenToCopy ) ); + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + + return atCoreStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncData( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char * pInputLine = NULL, * pToken = NULL; + const _socketDataRecv_t * pDataRecv = ( _socketDataRecv_t * ) pData; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "Receive Data: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "Receive Data: response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pDataRecv == NULL ) || ( pDataRecv->pData == NULL ) || ( pDataRecv->pDataLen == NULL ) ) + { + LogError( ( "Receive Data: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; /* The first item is the data prefix. */ + + /* Check the data prefix token "+USORD: ". */ + if( strncmp( pInputLine, SOCKET_DATA_PREFIX_TOKEN, SOCKET_DATA_PREFIX_TOKEN_LEN ) != 0 ) + { + LogError( ( "response item error in prefix CONNECT" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( pAtResp->pItm->pNext == NULL ) + { + /* Modem return +USORD: 0,"". No data returned since there is no data + * length field in modem response. */ + *pDataRecv->pDataLen = 0; + } + else + { + /* Parse the data length. */ + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( tempValue >= 0 ) + { + *pDataRecv->pDataLen = ( uint32_t ) tempValue; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Process the data buffer. Modem may also return +USORD: 0,0,"" with 0 data length. + * Process the data response only when data length is greater than 0. */ + if( ( atCoreStatus == CELLULAR_AT_SUCCESS ) && ( *pDataRecv->pDataLen > 0U ) ) + { + atCoreStatus = getDataFromResp( pAtResp, pDataRecv, dataLen ); + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t storeAccessModeAndAddress( CellularContext_t * pContext, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pRemoteSocketAddress == NULL ) || ( socketHandle == NULL ) ) + { + LogDebug( ( "storeAccessModeAndAddress: Invalid socket address" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_ALLOCATED ) + { + LogError( ( "storeAccessModeAndAddress, bad socket state %d", + 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; + socketHandle->dataMode = dataAccessMode; + socketHandle->remoteSocketAddress.ipAddress.ipAddressType = + pRemoteSocketAddress->ipAddress.ipAddressType; + ( void ) strncpy( socketHandle->remoteSocketAddress.ipAddress.ipAddress, + pRemoteSocketAddress->ipAddress.ipAddress, + CELLULAR_IP_ADDRESS_MAX_SIZE + 1U ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library types. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetSocketId( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + uint8_t * pSessionId = pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + int32_t tempValue = 0; + + if( pContext == NULL ) + { + LogError( ( "GetSocketId: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSessionId == NULL ) || ( dataLen != sizeof( uint8_t ) ) ) + { + LogError( ( "GetSocketId: Bad parameters" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSocketId: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pInputLine, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + *pSessionId = ( uint8_t ) tempValue; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_GetSocketNumber( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + uint8_t * pSessionId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqSocketConnect = + { + "AT+USOCR=6,0", + CELLULAR_AT_WITH_PREFIX, + "+USOCR", + _Cellular_RecvFuncGetSocketId, + NULL, + sizeof( uint8_t ), + }; + + ( void ) socketHandle; + + atReqSocketConnect.pData = pSessionId; + + /* Internal function. Caller checks parameters. */ + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSocketConnect ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "_Cellular_GetSocketNumber: get socekt number failed PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketRecv( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint8_t * pBuffer, + uint32_t bufferLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pReceivedDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + uint32_t recvTimeout = DATA_READ_TIMEOUT_MS; + uint32_t recvLen = bufferLength; + _socketDataRecv_t dataRecv = { 0 }; + CellularAtReq_t atReqSocketRecv = + { + NULL, + CELLULAR_AT_MULTI_DATA_WO_PREFIX, + "+USORD", + _Cellular_RecvFuncData, + NULL, + 0, + }; + uint32_t sessionId = 0; + + dataRecv.pDataLen = pReceivedDataLength; + dataRecv.pData = pBuffer; + + atReqSocketRecv.pAtCmd = cmdBuf; + atReqSocketRecv.pData = ( void * ) &dataRecv; + atReqSocketRecv.dataLen = bufferLength; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Cellular_SocketRecv: _Cellular_CheckLibraryStatus failed." ) ); + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketRecv: Invalid socketHandle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pBuffer == NULL ) || ( pReceivedDataLength == NULL ) || ( bufferLength == 0U ) ) + { + LogError( ( "Cellular_SocketRecv: Bad input Param." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_CONNECTED ) + { + /* Check the socket connection state. */ + LogInfo( ( "Cellular_SocketRecv: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketRecv : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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( recvLen > 0 ) + { + if( socketHandle->recvTimeoutMs != 0U ) + { + recvTimeout = socketHandle->recvTimeoutMs; + } + + /* 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%u,%u", "AT+USORD=", sessionId, recvLen ); + pktStatus = _Cellular_TimeoutAtcmdDataRecvRequestWithCallback( pContext, + atReqSocketRecv, + recvTimeout, + socketRecvDataPrefix, + NULL ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + /* Reset data handling parameters. */ + LogError( ( "Cellular_SocketRecv: Data Receive fail, pktStatus: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularPktStatus_t socketSendDataPrefix( void * pCallbackContext, + char * pLine, + uint32_t * pBytesRead ) +{ + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + if( ( pLine == NULL ) || ( pBytesRead == NULL ) ) + { + LogError( ( "socketSendDataPrefix: pLine is invalid or pBytesRead is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pCallbackContext != NULL ) + { + LogError( ( "socketSendDataPrefix: pCallbackContext is not NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( *pBytesRead != 1U ) + { + LogDebug( ( "socketSendDataPrefix: pBytesRead %u %s is not 1", *pBytesRead, pLine ) ); + } + else + { + /* After the data prefix, there should not be any data in stream. + * Cellular commmon processes AT command in lines. Add a '\0' after '@'. */ + if( strcmp( pLine, "@" ) == 0 ) + { + pLine[ 1 ] = '\n'; + *pBytesRead = 2; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_SocketSend( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + const uint8_t * pData, + uint32_t dataLength, + /* coverity[misra_c_2012_rule_8_13_violation] */ + uint32_t * pSentDataLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint32_t sendTimeout = DATA_SEND_TIMEOUT_MS; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketSend = { 0 }; + CellularAtDataReq_t atDataReqSocketSend = { 0 }; + uint32_t sessionId = 0; + + atReqSocketSend.atCmdType = CELLULAR_AT_NO_RESULT; + atReqSocketSend.pAtCmd = cmdBuf; + + atDataReqSocketSend.pData = pData; + atDataReqSocketSend.dataLen = dataLength; + atDataReqSocketSend.pSentDataLength = pSentDataLength; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_SocketSend: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( socketHandle == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( pSentDataLength == NULL ) || ( dataLength == 0U ) ) + { + LogDebug( ( "Cellular_SocketSend: Invalid parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle->socketState != SOCKETSTATE_CONNECTED ) + { + /* Check the socket connection state. */ + LogInfo( ( "Cellular_SocketSend: socket state %d is not connected.", socketHandle->socketState ) ); + + if( ( socketHandle->socketState == SOCKETSTATE_ALLOCATED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + cellularStatus = CELLULAR_SOCKET_NOT_CONNECTED; + } + else + { + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + } + else + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketSend : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Send data length check. */ + if( dataLength > ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN ) + { + atDataReqSocketSend.dataLen = ( uint32_t ) CELLULAR_MAX_SEND_DATA_LEN; + } + + /* Check send timeout. If not set by setsockopt, use default value. */ + if( socketHandle->sendTimeoutMs != 0U ) + { + sendTimeout = socketHandle->sendTimeoutMs; + } + + /* 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%u,%u", + "AT+USOWR=", sessionId, atDataReqSocketSend.dataLen ); + + pktStatus = _Cellular_AtcmdDataSend( pContext, atReqSocketSend, atDataReqSocketSend, + socketSendDataPrefix, NULL, + PACKET_REQ_TIMEOUT_MS, sendTimeout, 200U ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + if( socketHandle->socketState == SOCKETSTATE_DISCONNECTED ) + { + LogInfo( ( "Cellular_SocketSend: Data send fail, socket already closed" ) ); + cellularStatus = CELLULAR_SOCKET_CLOSED; + } + else + { + LogError( ( "Cellular_SocketSend: Data send fail, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketClose( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + CellularAtReq_t atReqSocketClose = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + uint32_t sessionId = INVALID_SESSION_ID; + cellularModuleContext_t * pModuleContext = NULL; + + atReqSocketClose.pAtCmd = cmdBuf; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_SocketClose: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( socketHandle == NULL ) + { + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else + { + if( socketHandle->socketState == SOCKETSTATE_CONNECTING ) + { + LogWarn( ( "Cellular_SocketClose: Socket state is SOCKETSTATE_CONNECTING." ) ); + } + + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + sessionId = _Cellular_GetSessionId( pContext, socketHandle->socketId ); + + if( sessionId == INVALID_SESSION_ID ) + { + LogError( ( "Cellular_SocketClose : invalid session ID for socket index %u", + socketHandle->socketId ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Remove the mapping. */ + pModuleContext->pSessionMap[ sessionId ] = INVALID_SOCKET_INDEX; + + /* Close the socket. */ + if( socketHandle->socketState == SOCKETSTATE_CONNECTED ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "%s%u,%d", + "AT+USOCL=", sessionId, CELLULAR_CONFIG_SET_SOCKET_CLOSE_ASYNC_MODE ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketClose, SOCKET_CLOSE_PACKET_REQ_TIMEOUT_MS ); + + /* Delete the socket config. */ + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogWarn( ( "Cellular_SocketClose: AT+USOCL fail, PktRet: %d", pktStatus ) ); + } + } + + /* Ignore the result from the info, and force to remove the socket. */ + cellularStatus = _Cellular_RemoveSocketData( pContext, socketHandle ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketConnect( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketAccessMode_t dataAccessMode, + const CellularSocketAddress_t * pRemoteSocketAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + uint8_t sessionId = 0; + CellularAtReq_t atReqSocketConnect = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + cellularModuleContext_t * pModuleContext = NULL; + + atReqSocketConnect.pAtCmd = cmdBuf; + + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Cellular_SocketConnect: _Cellular_CheckLibraryStatus failed." ) ); + } + else if( pRemoteSocketAddress == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket address." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( socketHandle == NULL ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket handle." ) ); + cellularStatus = CELLULAR_INVALID_HANDLE; + } + else if( socketHandle->socketProtocol != CELLULAR_SOCKET_PROTOCOL_TCP ) + { + LogError( ( "Cellular_SocketConnect: Invalid socket protocol." ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else if( ( socketHandle->socketState == SOCKETSTATE_CONNECTED ) || ( socketHandle->socketState == SOCKETSTATE_CONNECTING ) ) + { + LogError( ( "Cellular_SocketConnect: Not allowed in state %d.", socketHandle->socketState ) ); + cellularStatus = CELLULAR_NOT_ALLOWED; + } + else + { + cellularStatus = storeAccessModeAndAddress( pContext, socketHandle, dataAccessMode, pRemoteSocketAddress ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = _Cellular_GetModuleContext( pContext, ( void ** ) &pModuleContext ); + } + + /* Set socket config and get session id. The session id is defined by the modem. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Builds the Socket connect command. */ + cellularStatus = _Cellular_GetSocketNumber( pContext, socketHandle, &sessionId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Create the reverse table to store the socketIndex to sessionId. */ + pModuleContext->pSessionMap[ sessionId ] = socketHandle->socketId; + } + } + + /* Start the tcp connection. */ + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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_MAX_SIZE, + "AT+USOCO=%u,\"%s\",%d,1", + sessionId, + socketHandle->remoteSocketAddress.ipAddress.ipAddress, + socketHandle->remoteSocketAddress.port ); + + /* Set the socket state to connecting state. If cellular modem returns error, + * revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_CONNECTING; + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqSocketConnect, + SOCKET_CONNECT_PACKET_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SocketConnect: Socket connect failed, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + /* Revert the state to allocated state. */ + socketHandle->socketState = SOCKETSTATE_ALLOCATED; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardStatus( CellularHandle_t cellularHandle, + CellularSimCardStatus_t * pSimCardStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_GetSimCardStatus: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSimCardStatus == NULL ) + { + LogWarn( ( "Cellular_GetSimCardStatus: Bad input Parameter " ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* Parameters are checked in this API. */ + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_UNKNOWN; + pSimCardStatus->simCardLockState = CELLULAR_SIM_CARD_LOCK_UNKNOWN; + + cellularStatus = Cellular_CommonGetSimCardLockStatus( cellularHandle, pSimCardStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( ( pSimCardStatus->simCardLockState != CELLULAR_SIM_CARD_INVALID ) && + ( pSimCardStatus->simCardLockState != CELLULAR_SIM_CARD_LOCK_UNKNOWN ) ) + { + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_INSERTED; + } + else + { + pSimCardStatus->simCardState = CELLULAR_SIM_CARD_UNKNOWN; + } + + LogInfo( ( "Cellular_GetSimCardStatus, Sim Insert State[%d], Lock State[%d]", + pSimCardStatus->simCardState, pSimCardStatus->simCardLockState ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_DeactivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + bool packetSwitchStatus = false; + uint32_t i = 0; + + CellularServiceStatus_t serviceStatus = { 0 }; + CellularPdnContextActInfo_t pdpContextsActInfo = { 0 }; + + CellularAtReq_t atReqDeactPdn = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + atReqDeactPdn.pAtCmd = cmdBuf; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Get current network operator settings. */ + cellularStatus = Cellular_CommonGetServiceStatus( cellularHandle, &serviceStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Check the current status of context. */ + cellularStatus = _Cellular_GetContextActivationStatus( cellularHandle, &pdpContextsActInfo ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_DeactivatePdn: Listing operator and context details below." ) ); + + for( i = 0U; i < ( MAX_PDP_CONTEXTS - 1 ); i++ ) + { + /* Print only those contexts that are present in +CGACT response */ + if( pdpContextsActInfo.contextsPresent[ i ] ) + { + LogDebug( ( "Context [%d], Act State [%d], Operator [%d]\r\n", i + 1, + pdpContextsActInfo.contextActState[ i ], serviceStatus.rat ) ); + } + } + + /* Deactivate context if active */ + if( pdpContextsActInfo.contextActState[ contextId - 1 ] == true ) + { + /* Don't deactivate LTE default bearer context */ + /* Otherwise sending AT command "+CGACT=0,1" for deactivation will result in ERROR */ + if( ( serviceStatus.rat >= CELLULAR_RAT_LTE ) && ( contextId == DEFAULT_BEARER_CONTEXT_ID ) ) + { + LogInfo( ( "Cellular_DeactivatePdn: Default Bearer context %d Active. Not allowed to deactivate.", contextId ) ); + cellularStatus = CELLULAR_NOT_ALLOWED; + } + else + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s=0,%u", "AT+CGACT", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqDeactPdn, PDN_DEACT_PACKET_REQ_TIMEOUT_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + if( ( cellularStatus != CELLULAR_SUCCESS ) && ( cellularStatus != CELLULAR_NOT_ALLOWED ) ) + { + LogError( ( "Cellular_DeactivatePdn: can't deactivate PDN, PktRet: %d", pktStatus ) ); + + /* Sometimes +CGACT deactivation fails in 2G. Then check packet switch attach. If attached, detach packet switch. */ + if( ( serviceStatus.rat == CELLULAR_RAT_GSM ) || ( serviceStatus.rat == CELLULAR_RAT_EDGE ) ) + { + cellularStatus = _Cellular_GetPacketSwitchStatus( cellularHandle, &packetSwitchStatus ); + + if( ( cellularStatus == CELLULAR_SUCCESS ) && ( packetSwitchStatus == true ) ) + { + LogError( ( "Deactivate Packet switch" ) ); + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s", "AT+CGATT=0" ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqDeactPdn, GPRS_ATTACH_REQ_TIMEOUT_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + else if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Packet switch query failed" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + else + { + LogInfo( ( "Packet switch detached" ) ); + } + } + } + } + else + { + LogInfo( ( "Cellular_DeactivatePdn: Context id [%d] is already deactive", contextId ) ); + } + } + else + { + LogError( ( "Cellular_DeactivatePdn: Unable to list operator and context details." ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library prototype. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncPacketSwitchStatus( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + bool * pPacketSwitchStatus = ( bool * ) pData; + + if( pContext == NULL ) + { + LogError( ( "PacketSwitchStatus: Invalid handle" ) ); + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pData == NULL ) || ( dataLen != sizeof( bool ) ) ) + { + LogError( ( "GetPacketSwitchStatus: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetPacketSwitchStatus: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + + /* Remove prefix. */ + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + /* Remove leading space. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( *pInputLine == '0' ) + { + *pPacketSwitchStatus = false; + } + else if( *pInputLine == '1' ) + { + *pPacketSwitchStatus = true; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Get PDN context Activation state */ + +static CellularPktStatus_t _Cellular_RecvFuncGetPdpContextActState( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pRespLine = NULL; + CellularPdnContextActInfo_t * pPDPContextsActInfo = ( CellularPdnContextActInfo_t * ) pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const CellularATCommandLine_t * pCommnadItem = NULL; + uint8_t tokenIndex = 0; + uint8_t contextId = 0; + int32_t tempValue = 0; + char * pToken = NULL; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextActState: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pPDPContextsActInfo == NULL ) || ( dataLen != sizeof( CellularPdnContextActInfo_t ) ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pAtResp == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextActState: Response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextActState: no PDN context available" ) ); + pktStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + pCommnadItem = pAtResp->pItm; + + while( pCommnadItem != NULL ) + { + pRespLine = pCommnadItem->pLine; + LogDebug( ( "_Cellular_RecvFuncGetPdpContextActState: pRespLine [%s]", pRespLine ) ); + + /* Removing all the Spaces in the AT Response. */ + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemovePrefix( &pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pRespLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( ( pToken != NULL ) && ( atCoreStatus == CELLULAR_AT_SUCCESS ) ) + { + switch( tokenIndex ) + { + case ( CELLULAR_PDN_ACT_STATUS_POS_CONTEXT_ID ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextActState: Context Id pToken: %s", pToken ) ); + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MIN ) && + ( tempValue <= ( int32_t ) MAX_PDP_CONTEXTS ) ) + { + contextId = ( uint8_t ) tempValue; + pPDPContextsActInfo->contextsPresent[ contextId - 1 ] = true; + LogDebug( ( "_Cellular_RecvFuncGetPdpContextActState: Context Id: %d", contextId ) ); + } + else + { + LogError( ( "_Cellular_RecvFuncGetPdpContextActState: Invalid Context Id. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + case ( CELLULAR_PDN_ACT_STATUS_POS_CONTEXT_STATE ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextActState: Context pToken: %s", pToken ) ); + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pPDPContextsActInfo->contextActState[ contextId - 1 ] = ( bool ) tempValue; + LogDebug( ( "_Cellular_RecvFuncGetPdpContextActState: Context : %d", pPDPContextsActInfo->contextActState[ contextId - 1 ] ) ); + } + + break; + + default: + break; + } + + tokenIndex++; + + if( Cellular_ATGetNextTok( &pRespLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextActState: parse %s failed", pRespLine ) ); + break; + } + + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Check activation status of particular context. */ + +static CellularError_t _Cellular_GetContextActivationStatus( CellularHandle_t cellularHandle, + CellularPdnContextActInfo_t * pPdpContextsActInfo ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + + CellularAtReq_t atReqPacketSwitchStatus = + { + "AT+CGACT?", + CELLULAR_AT_MULTI_WITH_PREFIX, + "+CGACT", + _Cellular_RecvFuncGetPdpContextActState, + NULL, + sizeof( CellularPdnContextActInfo_t ), + }; + + atReqPacketSwitchStatus.pData = pPdpContextsActInfo; + + /* Internal function. Callee check parameters. */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqPacketSwitchStatus ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_GetPacketSwitchStatus( CellularHandle_t cellularHandle, + bool * pPacketSwitchStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularAtReq_t atReqPacketSwitchStatus = + { + "AT+CGATT?", + CELLULAR_AT_WITH_PREFIX, + "+CGATT", + _Cellular_RecvFuncPacketSwitchStatus, + NULL, + sizeof( bool ), + }; + + atReqPacketSwitchStatus.pData = pPacketSwitchStatus; + + /* Internal function. Callee check parameters. */ + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqPacketSwitchStatus, PDN_ACT_PACKET_REQ_TIMEOUT_MS ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ActivatePdn( CellularHandle_t cellularHandle, + uint8_t contextId ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + uint32_t i = 0; + + CellularPdnContextActInfo_t pdpContextsActInfo = { 0 }; + + CellularAtReq_t atReqActPdn = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_IsValidPdn( contextId ); + + atReqActPdn.pAtCmd = cmdBuf; + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Check the current status of context. If not activated, activate the PDN context ID. */ + cellularStatus = _Cellular_GetContextActivationStatus( cellularHandle, &pdpContextsActInfo ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_ActivatePdn: Listing operator and context details below." ) ); + + for( i = 0U; i < ( MAX_PDP_CONTEXTS - 1 ); i++ ) + { + /* Print only those contexts that are present in +CGACT response */ + if( pdpContextsActInfo.contextsPresent[ i ] ) + { + LogDebug( ( "Cellular_ActivatePdn: Context [%d], Act State [%d]\r\n", i + 1, + pdpContextsActInfo.contextActState[ i ] ) ); + } + } + } + + /* Activate context if not already active */ + if( pdpContextsActInfo.contextActState[ contextId - 1 ] == false ) + { + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s=1,%u", "AT+CGACT", contextId ); + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqActPdn, PDN_ACT_PACKET_REQ_TIMEOUT_MS ); + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_ActivatePdn: can't activate PDN, PktRet: %d", pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + else + { + LogInfo( ( "Cellular_ActivatePdn: Context id [%d] is already active", contextId ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +CellularError_t Cellular_GetPdnStatus( CellularHandle_t cellularHandle, + CellularPdnStatus_t * pPdnStatusBuffers, + uint8_t numStatusBuffers, + uint8_t * pNumStatus ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + uint8_t i = 0; + + CellularPdnContextActInfo_t pdpContextsActInfo = { 0 }; + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( ( pPdnStatusBuffers == NULL ) || ( pNumStatus == NULL ) || ( numStatusBuffers < 1u ) ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + LogWarn( ( "_Cellular_GetPdnStatus: Bad input Parameter " ) ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + *pNumStatus = 0U; + + /* Check the current status of contexts. */ + cellularStatus = _Cellular_GetContextActivationStatus( cellularHandle, &pdpContextsActInfo ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_GetPdnStatus: Listing operator and context details below." ) ); + + for( i = 0U; i < ( MAX_PDP_CONTEXTS - 1 ); i++ ) + { + /* Print only those contexts that are present in +CGACT response. */ + if( pdpContextsActInfo.contextsPresent[ i ] ) + { + LogDebug( ( "Context [%d], Act State [%d]\r\n", i + 1, pdpContextsActInfo.contextActState[ i ] ) ); + + if( *pNumStatus < numStatusBuffers ) + { + pPdnStatusBuffers[ *pNumStatus ].contextId = i + 1U; + pPdnStatusBuffers[ *pNumStatus ].state = pdpContextsActInfo.contextActState[ i ]; + *pNumStatus = *pNumStatus + 1U; + } + } + } + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetRatPriority( CellularHandle_t cellularHandle, + const CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + uint8_t i = 0; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqSetRatPriority = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + atReqSetRatPriority.pAtCmd = cmdBuf; + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_SetRatPriority: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) ) + { + LogWarn( ( "Cellular_SetRatPriority: Bad input Parameter " ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* In case of +UMNOPROF=0, AT+URAT set commad is not allowed */ + if( CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE != 0 ) + { + ( void ) strcpy( cmdBuf, "AT+CFUN=4;+URAT=" ); + + while( i < ratPrioritiesLength ) + { + if( ( pRatPriorities[ i ] == CELLULAR_RAT_GSM ) || ( pRatPriorities[ i ] == CELLULAR_RAT_EDGE ) ) + { + ( void ) strcat( cmdBuf, "9" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_CATM1 ) + { + ( void ) strcat( cmdBuf, "7" ); + } + else if( pRatPriorities[ i ] == CELLULAR_RAT_NBIOT ) + { + ( void ) strcat( cmdBuf, "8" ); + } + else + { + cellularStatus = CELLULAR_BAD_PARAMETER; + break; + } + + i++; + + if( i < ratPrioritiesLength ) + { + ( void ) strcat( cmdBuf, "," ); + } + } + + ( void ) strcat( cmdBuf, ";+CFUN=1" ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetRatPriority ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + else + { + LogDebug( ( "Cellular_SetRatPriority: Automatic selection as UMNOPROF profile is 0" ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* Get modem RAT priority setting. */ +/* coverity[misra_c_2012_rule_8_13_violation] */ +static CellularPktStatus_t _Cellular_RecvFuncGetRatPriority( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularRat_t * pRatPriorities = NULL; + uint8_t ratIndex = 0; + uint32_t maxRatPriorityLength = ( dataLen > RAT_PRIOIRTY_LIST_LENGTH ? RAT_PRIOIRTY_LIST_LENGTH : dataLen ); + + if( pContext == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetRatPriority: Invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || + ( pAtResp->pItm->pLine == NULL ) || ( pData == NULL ) || ( dataLen == 0U ) ) + { + LogError( ( "_Cellular_RecvFuncGetRatPriority: Invalid param" ) ); + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pInputLine = pAtResp->pItm->pLine; + pRatPriorities = ( CellularRat_t * ) pData; + + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + memset( pRatPriorities, CELLULAR_RAT_INVALID, dataLen ); + + /* pInputLine : 7,8,9. */ + atCoreStatus = Cellular_ATGetNextTok( &pInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + while( pToken != NULL && ratIndex < maxRatPriorityLength ) + { + LogDebug( ( "_Cellular_RecvFuncGetRatPriority: pToken [%s]", pToken ) ); + + if( strcmp( pToken, "7" ) == 0 ) + { + pRatPriorities[ ratIndex ] = CELLULAR_RAT_CATM1; + LogDebug( ( "_Cellular_RecvFuncGetRatPriority: CELLULAR_RAT_CATM1" ) ); + } + else if( strcmp( pToken, "8" ) == 0 ) + { + pRatPriorities[ ratIndex ] = CELLULAR_RAT_NBIOT; + LogDebug( ( "_Cellular_RecvFuncGetRatPriority: CELLULAR_RAT_NBIOT" ) ); + } + else if( strcmp( pToken, "9" ) == 0 ) + { + pRatPriorities[ ratIndex ] = CELLULAR_RAT_GSM; /* or CELLULAR_RAT_EDGE */ + LogDebug( ( "_Cellular_RecvFuncGetRatPriority: CELLULAR_RAT_GSM" ) ); + } + else + { + LogDebug( ( "_Cellular_RecvFuncGetRatPriority: Invalid RAT string [%s]", pToken ) ); + } + + ratIndex++; + + if( Cellular_ATGetNextTok( &pInputLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRatPriority( CellularHandle_t cellularHandle, + CellularRat_t * pRatPriorities, + uint8_t ratPrioritiesLength, + uint8_t * pReceiveRatPrioritesLength ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + uint8_t ratIndex = 0; + + CellularAtReq_t atReqSetRatPriority = + { + "AT+URAT?", + CELLULAR_AT_WITH_PREFIX, + "+URAT", + _Cellular_RecvFuncGetRatPriority, + NULL, + 0U, + }; + + atReqSetRatPriority.pData = pRatPriorities; + atReqSetRatPriority.dataLen = ( uint16_t ) ratPrioritiesLength; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_GetRatPriority: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pRatPriorities == NULL ) || ( ratPrioritiesLength == 0U ) || + ( ratPrioritiesLength > ( uint8_t ) CELLULAR_MAX_RAT_PRIORITY_COUNT ) || + ( pReceiveRatPrioritesLength == NULL ) ) + { + LogWarn( ( "Cellular_GetRatPriority: Bad input Parameter " ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + /* In case of +UMNOPROF=0, AT+URAT? read commad is not allowed */ + if( CELLULAR_CONFIG_SARA_R4_SET_MNO_PROFILE != 0 ) + { + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetRatPriority ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + for( ratIndex = 0; ratIndex < ratPrioritiesLength; ratIndex++ ) + { + if( pRatPriorities[ ratIndex ] == CELLULAR_RAT_INVALID ) + { + break; + } + } + + *pReceiveRatPrioritesLength = ratIndex; + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + else + { + LogDebug( ( "Cellular_GetRatPriority: Automatic selection as UMNOPROF profile is 0" ) ); + + pRatPriorities[ ratIndex++ ] = CELLULAR_RAT_CATM1; + pRatPriorities[ ratIndex++ ] = CELLULAR_RAT_NBIOT; + pRatPriorities[ ratIndex++ ] = CELLULAR_RAT_GSM; + *pReceiveRatPrioritesLength = RAT_PRIOIRTY_LIST_LENGTH; + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static bool _parseExtendedSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ) +{ + char * pToken = NULL, * pTmpQcsqPayload = pQcsqPayload; + int32_t tempValue = 0; + bool parseStatus = true; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( ( pSignalInfo == NULL ) || ( pQcsqPayload == NULL ) ) + { + LogError( ( "_parseExtendedSignalQuality: Invalid Input Parameters" ) ); + parseStatus = false; + } + + /* +CESQ: ,,,,,. */ + + /* Skip . */ + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + + /* Parse . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( ( atCoreStatus == CELLULAR_AT_SUCCESS ) && ( tempValue <= INT16_MAX ) && ( tempValue >= INT16_MIN ) ) + { + /* + * Bit Error Rate (BER): + * 0..7: as RXQUAL values in the table in 3GPP TS 45.008 [124], subclause 8.2.4 + * 99: not known or not detectable + */ + if( ( tempValue >= 0 ) && ( tempValue <= 7 ) ) + { + pSignalInfo->ber = ( int16_t ) tempValue; + } + else + { + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + } + } + else + { + LogError( ( "_parseExtendedSignalQuality: Error in processing BER. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Skip . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + /* Skip . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + /* Parse . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* + * Reference Signal Received Quality (RSRQ): + * 0: less than -19.5 dB + * 1..33: from -19.5 dB to -3.5 dB with 0.5 dB steps + * 34: -3 dB or greater + * 255: not known or not detectable + */ + if( ( tempValue >= 0 ) && ( tempValue <= 34 ) ) + { + pSignalInfo->rsrq = ( int16_t ) ( ( -20 ) + ( tempValue * 0.5 ) ); + } + else + { + pSignalInfo->rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + } + } + else + { + LogError( ( "_parseExtendedSignalQuality: Error in processing RSRP. Token %s", pToken ) ); + parseStatus = false; + } + } + + /* Parse . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* + * Reference Signal Received Power(RSRP) : + * 0 : less than - 140 dBm + * 1..96 : from - 140 dBm to - 45 dBm with 1 dBm steps + * 97 : -44 dBm or greater + * 255 : not known or not detectable + */ + if( ( tempValue >= 0 ) && ( tempValue <= 97 ) ) + { + pSignalInfo->rsrp = ( int16_t ) ( ( -141 ) + ( tempValue ) ); + } + else + { + pSignalInfo->rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + } + } + else + { + LogError( ( "_parseExtendedSignalQuality: Error in processing RSRP. Token %s", pToken ) ); + parseStatus = false; + } + } + + return parseStatus; +} + +/*-----------------------------------------------------------*/ + +static bool _parseSignalQuality( char * pQcsqPayload, + CellularSignalInfo_t * pSignalInfo ) +{ + char * pToken = NULL, * pTmpQcsqPayload = pQcsqPayload; + int32_t tempValue = 0; + bool parseStatus = true; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + + if( ( pSignalInfo == NULL ) || ( pQcsqPayload == NULL ) ) + { + LogError( ( "_parseSignalQuality: Invalid Input Parameters" ) ); + parseStatus = false; + } + + /* +CSQ: ,. */ + + /* Parse . */ + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* + * The allowed range is 0-31 and 99. + * 0 RSSI of the network <= -113 dBm + * 1 -111 dBm + * 2...30 -109 dBm <= RSSI of the network <= -53 dBm + * 31 -51 dBm <= RSSI of the network + * 99 Not known or not detectable + */ + if( ( tempValue >= 0 ) && ( tempValue <= 31 ) ) + { + pSignalInfo->rssi = ( int16_t ) ( ( -113 ) + ( tempValue * 2 ) ); + } + else if( tempValue == 99 ) + { + pSignalInfo->rssi = -113; + } + else + { + pSignalInfo->rssi = CELLULAR_INVALID_SIGNAL_VALUE; + } + } + else + { + LogError( ( "_parseSignalQuality: Error in processing RSSI. Token %s", pToken ) ); + parseStatus = false; + } + } + + /* Parse . */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pTmpQcsqPayload, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + /* + * The allowed range is 0-7 and 99 : + * In 2G RAT CS dedicated and GPRS packet transfer mode indicates the Bit Error Rate (BER) as specified in 3GPP TS 45.008 + */ + if( ( tempValue >= 0 ) && ( tempValue <= 7 ) ) + { + pSignalInfo->ber = ( int16_t ) tempValue; + } + else + { + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + } + } + else + { + LogError( ( "_parseSignalQuality: Error in processing ber. Token %s", pToken ) ); + parseStatus = false; + } + } + + return parseStatus; +} + +/*-----------------------------------------------------------*/ + +/* parse signal strength response */ + +static CellularPktStatus_t _Cellular_RecvFuncGetSignalInfo( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pInputLine = NULL; + CellularSignalInfo_t * pSignalInfo = ( CellularSignalInfo_t * ) pData; + bool parseStatus = true; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + bool isExtendedResponse = false; + + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_INVALID_HANDLE; + } + else if( ( pSignalInfo == NULL ) || ( dataLen != sizeof( CellularSignalInfo_t ) ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( ( pAtResp == NULL ) || ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "GetSignalInfo: Input Line passed is NULL" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else + { + pInputLine = pAtResp->pItm->pLine; + + if( strstr( pInputLine, "+CESQ" ) ) + { + LogDebug( ( "GetSignalInfo: ExtendedResponse received." ) ); + isExtendedResponse = true; + } + + atCoreStatus = Cellular_ATRemovePrefix( &pInputLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pInputLine ); + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + if( isExtendedResponse ) + { + parseStatus = _parseExtendedSignalQuality( pInputLine, pSignalInfo ); + } + else + { + parseStatus = _parseSignalQuality( pInputLine, pSignalInfo ); + } + + if( parseStatus != true ) + { + pSignalInfo->rssi = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->ber = CELLULAR_INVALID_SIGNAL_VALUE; + pSignalInfo->bars = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +CellularError_t Cellular_GetSignalInfo( CellularHandle_t cellularHandle, + CellularSignalInfo_t * pSignalInfo ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularRat_t rat = CELLULAR_RAT_INVALID; + CellularAtReq_t atReqQuerySignalInfo = + { + "AT+CSQ", + CELLULAR_AT_WITH_PREFIX, + "+CSQ", + _Cellular_RecvFuncGetSignalInfo, + NULL, + sizeof( CellularSignalInfo_t ), + }; + CellularAtReq_t atReqQueryExtendedSignalInfo = + { + "AT+CESQ", + CELLULAR_AT_WITH_PREFIX, + "+CESQ", + _Cellular_RecvFuncGetSignalInfo, + NULL, + sizeof( CellularSignalInfo_t ), + }; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + atReqQuerySignalInfo.pData = pSignalInfo; + atReqQueryExtendedSignalInfo.pData = pSignalInfo; + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_GetSignalInfo: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( pSignalInfo == NULL ) + { + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + cellularStatus = _Cellular_GetCurrentRat( pContext, &rat ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Get +CSQ response */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqQuerySignalInfo ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + /* Get +CESQ response */ + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqQueryExtendedSignalInfo ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + /* If the convert failed, the API will return CELLULAR_INVALID_SIGNAL_BAR_VALUE in bars field. */ + ( void ) _Cellular_ComputeSignalBars( rat, pSignalInfo ); + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetDns( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pDnsServerAddress ) +{ + /* Modem use dynamic DNS addresses. Return unsupported. */ + ( void ) cellularHandle; + ( void ) contextId; + ( void ) pDnsServerAddress; + return CELLULAR_UNSUPPORTED; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t controlSignalStrengthIndication( CellularContext_t * pContext, + bool enable ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_TYPICAL_MAX_SIZE ] = { '\0' }; + uint8_t enable_value = 0; + CellularAtReq_t atReqControlSignalStrengthIndication = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + atReqControlSignalStrengthIndication.pAtCmd = cmdBuf; + + if( enable == true ) + { + enable_value = 1; + } + else + { + enable_value = 0; + } + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( enable_value ) + { + /* Enable signal level change indication via +CIEV URC.*/ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "AT+UCIND=2" ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqControlSignalStrengthIndication ); + } + else + { + /* Disable signal level change indication via +CIEV URC.*/ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_TYPICAL_MAX_SIZE, "AT+UCIND=0" ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqControlSignalStrengthIndication ); + } + + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcSignalStrengthChangedCallback( CellularHandle_t cellularHandle, + CellularUrcSignalStrengthChangedCallback_t signalStrengthChangedCallback, + void * pCallbackContext ) +{ + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + + /* pContext is checked in the common library. */ + cellularStatus = Cellular_CommonRegisterUrcSignalStrengthChangedCallback( + cellularHandle, signalStrengthChangedCallback, pCallbackContext ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + if( signalStrengthChangedCallback != NULL ) + { + cellularStatus = controlSignalStrengthIndication( pContext, true ); + } + else + { + cellularStatus = controlSignalStrengthIndication( pContext, false ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* Resolve Domain name to IP address */ + +static CellularPktStatus_t _Cellular_RecvFuncResolveDomainToIpAddress( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pRespLine = NULL; + char * pResolvedIpAddress = ( char * ) pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const CellularATCommandLine_t * pCommnadItem = NULL; + char * pToken = NULL; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_RecvFuncResolveDomainToIpAddress: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pResolvedIpAddress == NULL ) || ( dataLen != CELLULAR_IP_ADDRESS_MAX_SIZE ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pAtResp == NULL ) + { + LogError( ( "_Cellular_RecvFuncResolveDomainToIpAddress: Response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "_Cellular_RecvFuncResolveDomainToIpAddress: Address not resolved" ) ); + pktStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + pCommnadItem = pAtResp->pItm; + + while( pCommnadItem != NULL ) + { + pRespLine = pCommnadItem->pLine; + LogDebug( ( "_Cellular_RecvFuncResolveDomainToIpAddress: pRespLine [%s]", pRespLine ) ); + + /* Removing all the Spaces in the AT Response. */ + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemovePrefix( &pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pRespLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + ( void ) strncpy( pResolvedIpAddress, pToken, dataLen ); + + LogDebug( ( "_Cellular_RecvFuncResolveDomainToIpAddress: Resolved IP Address: [%s]", pResolvedIpAddress ) ); + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "_Cellular_RecvFuncResolveDomainToIpAddress: parse %s failed", pRespLine ) ); + break; + } + + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + + + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetHostByName( CellularHandle_t cellularHandle, + uint8_t contextId, + const char * pcHostName, + char * pResolvedAddress ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqQueryDns = + { + NULL, + CELLULAR_AT_MULTI_WITH_PREFIX, + "+UDNSRN", + _Cellular_RecvFuncResolveDomainToIpAddress, + NULL, + CELLULAR_IP_ADDRESS_MAX_SIZE, + }; + + ( void ) contextId; + + /* pContext is checked in _Cellular_CheckLibraryStatus function. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + atReqQueryDns.pAtCmd = cmdBuf; + atReqQueryDns.pData = pResolvedAddress; + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_GetHostByName: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( ( pcHostName == NULL ) || ( pResolvedAddress == NULL ) ) + { + LogError( ( "Cellular_GetHostByName: Bad input Parameter " ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, + "AT+UDNSRN=0,\"%s\"", pcHostName ); + + pktStatus = _Cellular_TimeoutAtcmdRequestWithCallback( pContext, atReqQueryDns, DNS_QUERY_REQ_TIMEOUT_MS ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_GetHostByName: couldn't resolve host name" ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* Get PDN context APN name*/ + +static CellularPktStatus_t _Cellular_RecvFuncGetPdpContextSettings( CellularContext_t * pContext, + const CellularATCommandResponse_t * pAtResp, + void * pData, + uint16_t dataLen ) +{ + char * pRespLine = NULL; + CellularPdnContextInfo_t * pPDPContextsInfo = ( CellularPdnContextInfo_t * ) pData; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + const CellularATCommandLine_t * pCommnadItem = NULL; + uint8_t tokenIndex = 0; + uint8_t contextId = 0; + int32_t tempValue = 0; + char * pToken = NULL; + + if( pContext == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextSettings: invalid context" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pPDPContextsInfo == NULL ) || ( dataLen != sizeof( CellularPdnContextInfo_t ) ) ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else if( pAtResp == NULL ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextSettings: Response is invalid" ) ); + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( ( pAtResp->pItm == NULL ) || ( pAtResp->pItm->pLine == NULL ) ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextSettings: no PDN context available" ) ); + pktStatus = CELLULAR_PKT_STATUS_OK; + } + else + { + pRespLine = pAtResp->pItm->pLine; + + pCommnadItem = pAtResp->pItm; + + while( pCommnadItem != NULL ) + { + pRespLine = pCommnadItem->pLine; + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: pRespLine [%s]", pRespLine ) ); + + /* Removing all the Spaces in the AT Response. */ + atCoreStatus = Cellular_ATRemoveAllWhiteSpaces( pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemovePrefix( &pRespLine ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pRespLine ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pRespLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + tokenIndex = 0; + + while( ( pToken != NULL ) && ( atCoreStatus == CELLULAR_AT_SUCCESS ) ) + { + switch( tokenIndex ) + { + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_ID ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: Context Id pToken: %s", pToken ) ); + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( int32_t ) CELLULAR_PDN_CONTEXT_ID_MIN ) && + ( tempValue <= ( int32_t ) MAX_PDP_CONTEXTS ) ) + { + contextId = ( uint8_t ) tempValue; + pPDPContextsInfo->contextsPresent[ contextId - 1 ] = true; + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: Context Id: %d", contextId ) ); + } + else + { + LogError( ( "_Cellular_RecvFuncGetPdpContextSettings: Invalid Context Id. Token %s", pToken ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + break; + + case ( CELLULAR_PDN_STATUS_POS_CONTEXT_TYPE ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: Context Type pToken: %s", pToken ) ); + + ( void ) memcpy( ( void * ) pPDPContextsInfo->ipType[ contextId - 1 ], + ( void * ) pToken, CELULAR_PDN_CONTEXT_TYPE_MAX_SIZE + 1U ); + break; + + case ( CELLULAR_PDN_STATUS_POS_APN_NAME ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: Context APN name pToken: %s", pToken ) ); + + ( void ) memcpy( ( void * ) pPDPContextsInfo->apnName[ contextId - 1 ], + ( void * ) pToken, CELLULAR_APN_MAX_SIZE + 1U ); + break; + + case ( CELLULAR_PDN_STATUS_POS_IP_ADDRESS ): + LogDebug( ( "_Cellular_RecvFuncGetPdpContextSettings: Context IP address pToken: %s", pToken ) ); + + ( void ) memcpy( ( void * ) pPDPContextsInfo->ipAddress[ contextId - 1 ], + ( void * ) pToken, CELLULAR_IP_ADDRESS_MAX_SIZE + 1U ); + break; + + default: + break; + } + + tokenIndex++; + + if( Cellular_ATGetNextTok( &pRespLine, &pToken ) != CELLULAR_AT_SUCCESS ) + { + break; + } + } + } + } + + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "_Cellular_RecvFuncGetPdpContextSettings: parse %s failed", pRespLine ) ); + break; + } + + pCommnadItem = pCommnadItem->pNext; + } + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Set PDN APN name and Authentication setting */ + +CellularError_t Cellular_SetPdnConfig( CellularHandle_t cellularHandle, + uint8_t contextId, + const CellularPdnConfig_t * pPdnConfig ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + char pPdpTypeStr[ CELULAR_PDN_CONTEXT_TYPE_MAX_SIZE ] = { '\0' }; + uint32_t i = 0; + + CellularPdnContextInfo_t pdpContextsInfo = { 0 }; + CellularPdnContextInfo_t * pPdpContextsInfo = &pdpContextsInfo; + + CellularAtReq_t atReqSetPdn = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0, + }; + + atReqSetPdn.pAtCmd = cmdBuf; + + if( pPdnConfig == NULL ) + { + LogDebug( ( "Cellular_SetPdnConfig: Input parameter is NULL" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + switch( pPdnConfig->pdnContextType ) + { + case CELLULAR_PDN_CONTEXT_IPV4: + ( void ) strncpy( pPdpTypeStr, "IP", 3U ); /* 3U is the length of "IP" + '\0'. */ + break; + + case CELLULAR_PDN_CONTEXT_IPV6: + ( void ) strncpy( pPdpTypeStr, "IPV6", 5U ); /* 5U is the length of "IPV6" + '\0'. */ + break; + + case CELLULAR_PDN_CONTEXT_IPV4V6: + ( void ) strncpy( pPdpTypeStr, "IPV4V6", 7U ); /* 7U is the length of "IPV4V6" + '\0'. */ + break; + + default: + LogDebug( ( "Cellular_SetPdnConfig: Invalid pdn context type %d", + CELLULAR_PDN_CONTEXT_IPV4V6 ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + break; + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = _Cellular_IsValidPdn( contextId ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Make sure the library is open. */ + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* Check current APN name and IP type of contextId first to avoid unneccessary network de-registration. */ + /* TODO: This implementation currently assumes only one context in list. Need to add complete contexts list parsing */ + + CellularAtReq_t atReqGetCurrentApnName = + { + "AT+CGDCONT?", + CELLULAR_AT_MULTI_WITH_PREFIX, + "+CGDCONT", + _Cellular_RecvFuncGetPdpContextSettings, + NULL, + sizeof( CellularPdnContextInfo_t ), + }; + atReqGetCurrentApnName.pData = pPdpContextsInfo; + + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqGetCurrentApnName ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + LogDebug( ( "Cellular_SetPdnConfig: Listing operator and context details below." ) ); + + for( i = 0U; i < ( MAX_PDP_CONTEXTS - 1 ); i++ ) + { + /* Print only those contexts that are present in +CGDCONT response */ + if( pdpContextsInfo.contextsPresent[ i ] ) + { + LogDebug( ( "Context [%d], IP Type [%s], APN Name [%s], IP Address [%s]\r\n", i + 1, + pdpContextsInfo.ipType[ i ], ( char * ) pdpContextsInfo.apnName, + ( char * ) pdpContextsInfo.ipAddress ) ); + } + } + } + } + + if( cellularStatus == CELLULAR_SUCCESS ) + { + /* 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]. */ + + if( ( strstr( pdpContextsInfo.apnName[ contextId - 1 ], pPdnConfig->apnName ) == NULL ) || ( strcmp( pdpContextsInfo.ipType[ contextId - 1 ], pPdpTypeStr ) != 0 ) ) + { + if( strcmp( pdpContextsInfo.ipType[ contextId - 1 ], pPdpTypeStr ) != 0 ) + { + LogInfo( ( "Cellular_SetPdnConfig: Setting new IPv (Module IPv:%s != %s)\r\n", pdpContextsInfo.ipType[ contextId - 1 ], pPdpTypeStr ) ); + } + + if( strstr( pdpContextsInfo.apnName[ contextId - 1 ], pPdnConfig->apnName ) == NULL ) + { + LogInfo( ( "Cellular_SetPdnConfig: Setting new APN (Module APN:%s != %s)\r\n", pdpContextsInfo.apnName[ contextId - 1 ], pPdnConfig->apnName ) ); + } + + /* TODO: Add support if + UAUTHREQ is PAP / CHAP */ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "%s%d,\"%s\",\"%s\",,0,0;%s%d,%d%s", /*,\"%s\",\"%s\" TODO: add if +UAUTHREQ is PAP/CHAP */ + "AT+COPS=2;+CGDCONT=", + contextId, + pPdpTypeStr, + pPdnConfig->apnName, + "+UAUTHREQ=", + contextId, + pPdnConfig->pdnAuthType, + ";+COPS=0" ); + /*pPdnConfig->username, */ + /*pPdnConfig->password); */ + + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetPdn ); + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogError( ( "Cellular_SetPdnConfig: can't set PDN, cmdBuf:%s, PktRet: %d", cmdBuf, pktStatus ) ); + cellularStatus = _Cellular_TranslatePktStatus( pktStatus ); + } + } + else + { + LogInfo( ( "Cellular_SetPdnConfig: APN and IPv already set.\r\n" ) ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetPsmSettings( CellularHandle_t cellularHandle, + const CellularPsmSettings_t * pPsmSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqSetPsm = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + atReqSetPsm.pAtCmd = cmdBuf; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Cellular_SetPsmSettings: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( pPsmSettings == NULL ) + { + LogError( ( "Cellular_SetPsmSettings : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + if( pPsmSettings->mode == 1 ) + { + /* + * SARA-R4: To change the command setting issue AT+COPS=2 or AT+CFUN=0 to deregister the module from + * network, issue the +CPSMS command and reboot the module in order to apply the new configuration. */ + /* After PSM mode active, press "PWR_ON" key to awake modem or T3412 timer is expired. */ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+CFUN=0" ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetPsm ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + cellularStatus = Cellular_CommonSetPsmSettings( cellularHandle, pPsmSettings ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = rebootCellularModem( pContext, false, true ); + } + else + { + LogError( ( "Cellular_SetPsmSettings: Unable to set PSM settings." ) ); + } + } + } + else + { + cellularStatus = Cellular_CommonSetPsmSettings( cellularHandle, pPsmSettings ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +static CellularError_t _Cellular_isSockOptSupport( CellularSocketOptionLevel_t optionLevel, + CellularSocketOption_t option ) +{ + CellularError_t err = CELLULAR_UNSUPPORTED; + + if( ( optionLevel == CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT ) && + ( ( option == CELLULAR_SOCKET_OPTION_SEND_TIMEOUT ) || + ( option == CELLULAR_SOCKET_OPTION_RECV_TIMEOUT ) || + ( option == CELLULAR_SOCKET_OPTION_PDN_CONTEXT_ID ) ) ) + { + err = CELLULAR_SUCCESS; + } + else + { + LogWarn( ( "Cellular_SocketSetSockOpt: Option [Level:option=%d:%d] not supported in SARA R4", + optionLevel, option ) ); + } + + return err; +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SetEidrxSettings( CellularHandle_t cellularHandle, + const CellularEidrxSettings_t * pEidrxSettings ) +{ + CellularContext_t * pContext = ( CellularContext_t * ) cellularHandle; + CellularError_t cellularStatus = CELLULAR_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + char cmdBuf[ CELLULAR_AT_CMD_MAX_SIZE ] = { '\0' }; + + CellularAtReq_t atReqSetEidrx = + { + NULL, + CELLULAR_AT_NO_RESULT, + NULL, + NULL, + NULL, + 0 + }; + + atReqSetEidrx.pAtCmd = cmdBuf; + + cellularStatus = _Cellular_CheckLibraryStatus( pContext ); + + if( cellularStatus != CELLULAR_SUCCESS ) + { + LogError( ( "Cellular_SetEidrxSettings: _Cellular_CheckLibraryStatus failed" ) ); + } + else if( pEidrxSettings == NULL ) + { + LogError( ( "Cellular_SetEidrxSettings : Bad parameter" ) ); + cellularStatus = CELLULAR_BAD_PARAMETER; + } + else + { + if( ( pEidrxSettings->mode == 1 ) || ( pEidrxSettings->mode == 2 ) ) + { + /* + * SARA-R4: To change the command setting issue AT+COPS=2 or AT+CFUN=0 to deregister the module from + * network, issue the +CEDRXS command and reboot the module in order to apply the new configuration. + */ + ( void ) snprintf( cmdBuf, CELLULAR_AT_CMD_MAX_SIZE, "AT+CFUN=0" ); + pktStatus = _Cellular_AtcmdRequestWithCallback( pContext, atReqSetEidrx ); + + if( pktStatus == CELLULAR_PKT_STATUS_OK ) + { + cellularStatus = Cellular_CommonSetEidrxSettings( cellularHandle, pEidrxSettings ); + + if( cellularStatus == CELLULAR_SUCCESS ) + { + cellularStatus = rebootCellularModem( pContext, true, false ); + } + else + { + LogError( ( "Cellular_SetEidrxSettings: Unable to set Eidrx settings." ) ); + } + } + } + else + { + cellularStatus = Cellular_CommonSetEidrxSettings( cellularHandle, pEidrxSettings ); + } + } + + return cellularStatus; +} + +/*-----------------------------------------------------------*/ + +CellularError_t Cellular_Init( CellularHandle_t * pCellularHandle, + const CellularCommInterface_t * pCommInterface ) +{ + CellularTokenTable_t cellularTokenTable = { 0 }; + + cellularTokenTable.pCellularUrcHandlerTable = CellularUrcHandlerTable; + cellularTokenTable.cellularPrefixToParserMapSize = CellularUrcHandlerTableSize; + cellularTokenTable.pCellularSrcTokenErrorTable = CellularSrcTokenErrorTable; + cellularTokenTable.cellularSrcTokenErrorTableSize = CellularSrcTokenErrorTableSize; + cellularTokenTable.pCellularSrcTokenSuccessTable = CellularSrcTokenSuccessTable; + cellularTokenTable.cellularSrcTokenSuccessTableSize = CellularSrcTokenSuccessTableSize; + cellularTokenTable.pCellularUrcTokenWoPrefixTable = CellularUrcTokenWoPrefixTable; + cellularTokenTable.cellularUrcTokenWoPrefixTableSize = CellularUrcTokenWoPrefixTableSize; + cellularTokenTable.pCellularSrcExtraTokenSuccessTable = NULL; + cellularTokenTable.cellularSrcExtraTokenSuccessTableSize = 0; + + return Cellular_CommonInit( pCellularHandle, pCommInterface, &cellularTokenTable ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +CellularError_t Cellular_SocketSetSockOpt( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOptionLevel_t optionLevel, + CellularSocketOption_t option, + const uint8_t * pOptionValue, + uint32_t optionValueLength ) +{ + CellularError_t err = CELLULAR_SUCCESS; + + err = _Cellular_isSockOptSupport( optionLevel, option ); + + if( err == CELLULAR_SUCCESS ) + { + err = Cellular_CommonSocketSetSockOpt( cellularHandle, socketHandle, optionLevel, option, + pOptionValue, optionValueLength ); + } + + return err; +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_urc_handler.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_urc_handler.c new file mode 100644 index 0000000000..10308869d3 --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_urc_handler.c @@ -0,0 +1,656 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include +#include + +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_common.h" +#include "cellular_common_api.h" +#include "cellular_common_portable.h" + +/* Cellular module includes. */ +#include "cellular_r4.h" + +/*-----------------------------------------------------------*/ + +/* +UUPSMR URC */ +#define PSM_MODE_EXIT ( 0U ) +#define PSM_MODE_ENTER ( 1U ) +#define PSM_MODE_PREVENT_ENTRY ( 2U ) +#define PSM_MODE_PREVENT_DEEP_ENTRY ( 3U ) + +/* +CIEV URC */ +#define CIEV_POS_MIN ( 1U ) +#define CIEV_POS_SIGNAL ( 2U ) +#define CIEV_POS_SERVICE ( 3U ) +#define CIEV_POS_CALL ( 6U ) +#define CIEV_POS_MAX ( 12U ) + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessUusoco( CellularContext_t * pContext, + char * pInputLine ); +static void _cellular_UrcProcessUusord( CellularContext_t * pContext, + char * pInputLine ); +static void _cellular_UrcProcessUusocl( CellularContext_t * pContext, + char * pInputLine ); + +static void _cellular_UrcProcessUupsmr( CellularContext_t * pContext, + char * pInputLine ); +static void _cellular_UrcProcessCiev( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_ProcessModemRdy( CellularContext_t * pContext, + char * pInputLine ); +static CellularPktStatus_t _parseUrcIndicationCsq( CellularContext_t * pContext, + char * pUrcStr ); +static void _Cellular_UrcProcessCereg( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_UrcProcessCgreg( CellularContext_t * pContext, + char * pInputLine ); +static void _Cellular_UrcProcessCreg( CellularContext_t * pContext, + char * pInputLine ); + +/*-----------------------------------------------------------*/ + +/* Try to Keep this map in Alphabetical order. */ +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularAtParseTokenMap_t CellularUrcHandlerTable[] = +{ + { "CEREG", _Cellular_UrcProcessCereg }, + { "CGREG", _Cellular_UrcProcessCgreg }, + /*{ "CGEV", _cellular_UrcProcessCgev }, / * TODO: PS event reporting URC. * / */ + { "CIEV", _cellular_UrcProcessCiev }, /* PS ACT/DEACT and Signal strength status change indication URC. */ + { "CREG", _Cellular_UrcProcessCreg }, + { "RDY", _Cellular_ProcessModemRdy }, /* Modem bootup indication. */ + { "UUPSMR", _cellular_UrcProcessUupsmr }, /* Power saving mode indication URC. */ + { "UUSOCL", _cellular_UrcProcessUusocl }, /* Socket close URC. */ + { "UUSOCO", _cellular_UrcProcessUusoco }, /* Socket connect URC. */ + { "UUSORD", _cellular_UrcProcessUusord } /* Socket receive URC. */ +}; + +/* FreeRTOS Cellular Common Library porting interface. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +uint32_t CellularUrcHandlerTableSize = sizeof( CellularUrcHandlerTable ) / sizeof( CellularAtParseTokenMap_t ); + +/*-----------------------------------------------------------*/ + +/* Parse PS ACT/DEACT from +CIEV URC indication. */ +/* This URC does not tell which context ID number is ACT/DEACT. */ + +static CellularPktStatus_t _parseUrcIndicationCall( const CellularContext_t * pContext, + char * pUrcStr ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + int32_t isActivated = 0; + /* In SARA-R4, usually context 1 is used for PS. */ + uint8_t contextId = 1; + + if( ( pContext == NULL ) || ( pUrcStr == NULL ) ) + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pUrcStr, 10, &isActivated ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( isActivated >= INT16_MIN ) && ( isActivated <= ( int32_t ) INT16_MAX ) ) + { + LogDebug( ( "_parseUrcIndicationCall: PS status isActivated=[%d]", isActivated ) ); + + /* Handle the callback function. */ + if( isActivated ) + { + LogDebug( ( "_parseUrcIndicationCall: PDN activated. Context Id %d", contextId ) ); + _Cellular_PdnEventCallback( pContext, CELLULAR_URC_EVENT_PDN_ACTIVATED, contextId ); + } + else + { + LogDebug( ( "_parseUrcIndicationCall: PDN deactivated. Context Id %d", contextId ) ); + _Cellular_PdnEventCallback( pContext, CELLULAR_URC_EVENT_PDN_DEACTIVATED, contextId ); + } + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +/* Parse signal level from +CIEV URC indication. */ +/* This URC only gives bar level and not the exact RSSI value. */ + +static CellularPktStatus_t _parseUrcIndicationCsq( CellularContext_t * pContext, + char * pUrcStr ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + int32_t retStrtoi = 0; + int16_t csqBarLevel = CELLULAR_INVALID_SIGNAL_BAR_VALUE; + CellularSignalInfo_t signalInfo = { 0 }; + + if( ( pContext == NULL ) || ( pUrcStr == NULL ) ) + { + atCoreStatus = CELLULAR_AT_BAD_PARAMETER; + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pUrcStr, 10, &retStrtoi ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( retStrtoi >= INT16_MIN ) && ( retStrtoi <= ( int32_t ) INT16_MAX ) ) + { + csqBarLevel = retStrtoi; + } + else + { + atCoreStatus = CELLULAR_AT_ERROR; + } + } + + /* Handle the callback function. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + LogDebug( ( "_parseUrcIndicationCsq: SIGNAL Strength Bar level [%d]", csqBarLevel ) ); + signalInfo.rssi = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.rsrp = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.rsrq = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.ber = CELLULAR_INVALID_SIGNAL_VALUE; + signalInfo.bars = csqBarLevel; + _Cellular_SignalStrengthChangedCallback( pContext, CELLULAR_URC_EVENT_SIGNAL_CHANGED, &signalInfo ); + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + + return pktStatus; +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessCiev( CellularContext_t * pContext, + char * pInputLine ) +{ + char * pUrcStr = NULL, * pToken = NULL; + CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK; + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + int32_t tempValue = 0; + uint8_t indicatorDescr = 0; + + /* Check context status. */ + if( pContext == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_FAILURE; + } + else if( pInputLine == NULL ) + { + pktStatus = CELLULAR_PKT_STATUS_BAD_PARAM; + } + else + { + pUrcStr = pInputLine; + atCoreStatus = Cellular_ATRemoveAllDoubleQuote( pUrcStr ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATRemoveLeadingWhiteSpaces( &pUrcStr ); + } + + /* Extract indicator */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pUrcStr, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( ( int32_t ) CIEV_POS_MIN ) ) && ( tempValue <= ( ( int32_t ) CIEV_POS_MAX ) ) ) + { + indicatorDescr = ( uint8_t ) tempValue; + + switch( indicatorDescr ) + { + case CIEV_POS_SIGNAL: + LogDebug( ( "_cellular_UrcProcessCiev: CIEV_POS_SIGNAL" ) ); + /* This URC only gives bar level and not the exact RSSI value. */ + + /* + * o 0: < -105 dBm + * o 1 : < -93 dBm + * o 2 : < -81 dBm + * o 3 : < -69 dBm + * o 4 : < -57 dBm + * o 5 : >= -57 dBm + */ + /* Parse the signal Bar level from string. */ + pktStatus = _parseUrcIndicationCsq( pContext, pUrcStr ); + break; + + case CIEV_POS_CALL: + LogDebug( ( "_cellular_UrcProcessCiev: CIEV_POS_CALL" ) ); + /* Parse PS ACT/DEACT from +CIEV URC indication. */ + /* This URC does not tell which context ID number is ACT/DEACT. */ + pktStatus = _parseUrcIndicationCall( ( const CellularContext_t * ) pContext, pUrcStr ); + break; + + default: + break; + } + } + else + { + LogError( ( "_cellular_UrcProcessCiev: parsing failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + if( atCoreStatus != CELLULAR_AT_SUCCESS ) + { + pktStatus = _Cellular_TranslateAtCoreStatus( atCoreStatus ); + } + } + + if( pktStatus != CELLULAR_PKT_STATUS_OK ) + { + LogDebug( ( "_cellular_UrcProcessCiev: Parse failure" ) ); + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessUupsmr( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + uint8_t psmState = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +UUPSMR: [,] */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= ( ( int32_t ) PSM_MODE_EXIT ) ) && ( tempValue <= ( ( int32_t ) PSM_MODE_PREVENT_DEEP_ENTRY ) ) ) + { + psmState = ( uint8_t ) tempValue; + + switch( psmState ) + { + case PSM_MODE_EXIT: + LogInfo( ( "_cellular_UrcProcessUupsmr: PSM_MODE_EXIT" ) ); + break; + + case PSM_MODE_ENTER: + LogInfo( ( "_cellular_UrcProcessUupsmr: PSM_MODE_ENTER event received" ) ); + /* Call the callback function. Indicate the upper layer about the PSM state change. */ + _Cellular_ModemEventCallback( pContext, CELLULAR_MODEM_EVENT_PSM_ENTER ); + break; + + case PSM_MODE_PREVENT_ENTRY: + LogInfo( ( "_cellular_UrcProcessUupsmr: PSM_MODE_PREVENT_ENTRY" ) ); + break; + + case PSM_MODE_PREVENT_DEEP_ENTRY: + LogInfo( ( "_cellular_UrcProcessUupsmr: PSM_MODE_PREVENT_DEEP_ENTRY" ) ); + break; + } + } + else + { + LogError( ( "_cellular_UrcProcessUupsmr: parsing failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessUusoco( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + uint8_t socketError = 0; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +UUSOCO: , + * socket_error = 0 : no error, others : error. */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "parsing _cellular_UrcProcessKtcpInd session ID failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + } + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= 0 ) && ( tempValue <= UINT8_MAX ) ) + { + socketError = ( uint8_t ) tempValue; + } + else + { + LogError( ( "parsing _cellular_UrcProcessUusoco socket error failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessUusoco : invalid socket index %u", socketIndex ) ); + } + else + { + if( socketError == 0 ) + { + pSocketData->socketState = SOCKETSTATE_CONNECTED; + LogDebug( ( "Notify session %d with socket opened\r\n", sessionId ) ); + + if( pSocketData->openCallback != NULL ) + { + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPENED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + } + else + { + if( pSocketData->openCallback != NULL ) + { + pSocketData->openCallback( CELLULAR_URC_SOCKET_OPEN_FAILED, + pSocketData, pSocketData->pOpenCallbackContext ); + } + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessUusord( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +UUSOCO: , */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "parsing _cellular_UrcProcessUusord session ID failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Skip data length. */ + + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogWarn( ( "_cellular_UrcProcessUusord : unknown session data received. " + "The session %u may not be closed properly in previous execution.", sessionId ) ); + } + else + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessUusord : invalid socket index %d", socketIndex ) ); + } + else + { + /* Indicate the upper layer about the data reception. */ + if( pSocketData->dataReadyCallback != NULL ) + { + pSocketData->dataReadyCallback( pSocketData, pSocketData->pDataReadyCallbackContext ); + } + else + { + LogDebug( ( "_cellular_UrcProcessUusord: Data ready callback not set!!" ) ); + } + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +static void _cellular_UrcProcessUusocl( CellularContext_t * pContext, + char * pInputLine ) +{ + CellularATError_t atCoreStatus = CELLULAR_AT_SUCCESS; + char * pLocalInputLine = pInputLine; + char * pToken = NULL; + CellularSocketContext_t * pSocketData = NULL; + uint8_t sessionId = 0; + uint32_t socketIndex = 0; + int32_t tempValue = 0; + + if( ( pContext != NULL ) && ( pInputLine != NULL ) ) + { + /* The inputline is in this format +UUSOCL: */ + atCoreStatus = Cellular_ATGetNextTok( &pLocalInputLine, &pToken ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + atCoreStatus = Cellular_ATStrtoi( pToken, 10, &tempValue ); + + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( ( tempValue >= MIN_TCP_SESSION_ID ) && ( tempValue <= MAX_TCP_SESSION_ID ) ) + { + sessionId = ( uint8_t ) tempValue; + socketIndex = _Cellular_GetSocketId( pContext, sessionId ); + } + else + { + LogError( ( "parsing _cellular_UrcProcessUusocl session ID failed" ) ); + atCoreStatus = CELLULAR_AT_ERROR; + } + } + } + + /* Call the callback function of this session. */ + if( atCoreStatus == CELLULAR_AT_SUCCESS ) + { + if( socketIndex == INVALID_SOCKET_INDEX ) + { + LogWarn( ( "_cellular_UrcProcessUusocl : unknown session closed URC received. " + "The session %u may not be closed properly in previous execution.", sessionId ) ); + } + else + { + pSocketData = _Cellular_GetSocketData( pContext, socketIndex ); + + if( pSocketData == NULL ) + { + LogError( ( "_cellular_UrcProcessUusocl : invalid socket index %d", socketIndex ) ); + } + else + { + /* Change the socket state to disconnected. */ + pSocketData->socketState = SOCKETSTATE_DISCONNECTED; + + /* Indicate the upper layer about the data reception. */ + if( pSocketData->closedCallback != NULL ) + { + pSocketData->closedCallback( pSocketData, pSocketData->pClosedCallbackContext ); + } + else + { + LogDebug( ( "_cellular_UrcProcessUusord: Data ready callback not set!!" ) ); + } + } + } + } + } +} + +/*-----------------------------------------------------------*/ + +/* Modem bootup indication. */ + +static void _Cellular_ProcessModemRdy( CellularContext_t * pContext, + char * pInputLine ) +{ + /* The token is the pInputLine. No need to process the pInputLine. */ + ( void ) pInputLine; + + if( pContext == NULL ) + { + LogWarn( ( "_Cellular_ProcessModemRdy: Context not set" ) ); + } + else + { + LogDebug( ( "_Cellular_ProcessModemRdy: Modem Ready event received" ) ); + _Cellular_ModemEventCallback( pContext, CELLULAR_MODEM_EVENT_BOOTUP_OR_REBOOT ); + } +} + +/*-----------------------------------------------------------*/ + +static void _Cellular_UrcProcessCereg( CellularContext_t * pContext, + char * pInputLine ) +{ + ( void ) Cellular_CommonUrcProcessCereg( pContext, pInputLine ); +} + +/*-----------------------------------------------------------*/ + +static void _Cellular_UrcProcessCgreg( CellularContext_t * pContext, + char * pInputLine ) +{ + ( void ) Cellular_CommonUrcProcessCgreg( pContext, pInputLine ); +} + +/*-----------------------------------------------------------*/ + +static void _Cellular_UrcProcessCreg( CellularContext_t * pContext, + char * pInputLine ) +{ + ( void ) Cellular_CommonUrcProcessCreg( pContext, pInputLine ); +} + +/*-----------------------------------------------------------*/ diff --git a/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_wrapper.c b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_wrapper.c new file mode 100644 index 0000000000..b0eb9cea2d --- /dev/null +++ b/FreeRTOS-Plus/Source/FreeRTOS-Cellular-Modules/FreeRTOS-Cellular-Interface-Reference-Implementation-ublox-SARA-R4/cellular_r4_wrapper.c @@ -0,0 +1,263 @@ +/* + * FreeRTOS-Cellular-Interface v1.3.0 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * 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 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. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + */ + +/* The config header is always included first. */ +#include "cellular_config.h" +#include "cellular_config_defaults.h" + +/* Standard includes. */ +#include +#include + +#include "cellular_platform.h" +#include "cellular_types.h" +#include "cellular_api.h" +#include "cellular_common.h" +#include "cellular_common_api.h" + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_Cleanup( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonCleanup( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcNetworkRegistrationEventCallback( CellularHandle_t cellularHandle, + CellularUrcNetworkRegistrationCallback_t networkRegistrationCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcNetworkRegistrationEventCallback( cellularHandle, networkRegistrationCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcPdnEventCallback( CellularHandle_t cellularHandle, + CellularUrcPdnEventCallback_t pdnEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcPdnEventCallback( cellularHandle, pdnEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterUrcGenericCallback( CellularHandle_t cellularHandle, + CellularUrcGenericCallback_t genericCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterUrcGenericCallback( cellularHandle, genericCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RegisterModemEventCallback( CellularHandle_t cellularHandle, + CellularModemEventCallback_t modemEventCallback, + void * pCallbackContext ) +{ + return Cellular_CommonRegisterModemEventCallback( cellularHandle, modemEventCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_ATCommandRaw( CellularHandle_t cellularHandle, + const char * pATCommandPrefix, + const char * pATCommandPayload, + CellularATCommandType_t atCommandType, + CellularATCommandResponseReceivedCallback_t responseReceivedCallback, + void * pData, + uint16_t dataLen ) +{ + return Cellular_CommonATCommandRaw( cellularHandle, pATCommandPrefix, pATCommandPayload, atCommandType, + responseReceivedCallback, pData, dataLen ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_CreateSocket( CellularHandle_t cellularHandle, + uint8_t pdnContextId, + CellularSocketDomain_t socketDomain, + CellularSocketType_t socketType, + CellularSocketProtocol_t socketProtocol, + CellularSocketHandle_t * pSocketHandle ) +{ + return Cellular_CommonCreateSocket( cellularHandle, pdnContextId, socketDomain, socketType, + socketProtocol, pSocketHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterDataReadyCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketDataReadyCallback_t dataReadyCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterDataReadyCallback( cellularHandle, socketHandle, + dataReadyCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterSocketOpenCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketOpenCallback_t socketOpenCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterSocketOpenCallback( cellularHandle, socketHandle, + socketOpenCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_SocketRegisterClosedCallback( CellularHandle_t cellularHandle, + CellularSocketHandle_t socketHandle, + CellularSocketClosedCallback_t closedCallback, + void * pCallbackContext ) +{ + return Cellular_CommonSocketRegisterClosedCallback( cellularHandle, socketHandle, + closedCallback, pCallbackContext ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOn( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOn( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_RfOff( CellularHandle_t cellularHandle ) +{ + return Cellular_CommonRfOff( cellularHandle ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetIPAddress( CellularHandle_t cellularHandle, + uint8_t contextId, + char * pBuffer, + uint32_t bufferLength ) +{ + return Cellular_CommonGetIPAddress( cellularHandle, contextId, pBuffer, bufferLength ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetModemInfo( CellularHandle_t cellularHandle, + CellularModemInfo_t * pModemInfo ) +{ + return Cellular_CommonGetModemInfo( cellularHandle, pModemInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetRegisteredNetwork( CellularHandle_t cellularHandle, + CellularPlmnInfo_t * pNetworkInfo ) +{ + return Cellular_CommonGetRegisteredNetwork( cellularHandle, pNetworkInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetNetworkTime( CellularHandle_t cellularHandle, + CellularTime_t * pNetworkTime ) +{ + return Cellular_CommonGetNetworkTime( cellularHandle, pNetworkTime ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetServiceStatus( CellularHandle_t cellularHandle, + CellularServiceStatus_t * pServiceStatus ) +{ + return Cellular_CommonGetServiceStatus( cellularHandle, pServiceStatus ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetSimCardInfo( CellularHandle_t cellularHandle, + CellularSimCardInfo_t * pSimCardInfo ) +{ + return Cellular_CommonGetSimCardInfo( cellularHandle, pSimCardInfo ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetPsmSettings( CellularHandle_t cellularHandle, + CellularPsmSettings_t * pPsmSettings ) +{ + return Cellular_CommonGetPsmSettings( cellularHandle, pPsmSettings ); +} + +/*-----------------------------------------------------------*/ + +/* FreeRTOS Cellular Library API. */ +/* coverity[misra_c_2012_rule_8_7_violation] */ +CellularError_t Cellular_GetEidrxSettings( CellularHandle_t cellularHandle, + CellularEidrxSettingsList_t * pEidrxSettingsList ) +{ + return Cellular_CommonGetEidrxSettings( cellularHandle, pEidrxSettingsList ); +} + +/*-----------------------------------------------------------*/