Update transport interface for compatibility (#844)

Update transport interface for compatibility

* Update the network transport that using socket wrapper to depend on socket wrapper only.
* AT command timeout should not be changed for cellular socket buffer access mode.
* mbedTLS config include using MBEDTLS_CONFIG_FILE macro.
* Remove strnlen usage in using_mbedtls_pkcs11.
This commit is contained in:
chinglee-iot 2022-08-26 10:10:15 +08:00 committed by GitHub
parent 2e772110e3
commit 0fc242b7db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 125 additions and 119 deletions

View file

@ -89,9 +89,6 @@ extern uint8_t CellularSocketPdnContextId;
#define CELLULAR_SOCKET_OPEN_TIMEOUT_TICKS ( portMAX_DELAY ) #define CELLULAR_SOCKET_OPEN_TIMEOUT_TICKS ( portMAX_DELAY )
#define CELLULAR_SOCKET_CLOSE_TIMEOUT_TICKS ( pdMS_TO_TICKS( 10000U ) ) #define CELLULAR_SOCKET_CLOSE_TIMEOUT_TICKS ( pdMS_TO_TICKS( 10000U ) )
/* Cellular socket AT command timeout. */
#define CELLULAR_SOCKET_RECV_TIMEOUT_MS ( 1000UL )
/* Time conversion constants. */ /* Time conversion constants. */
#define _MILLISECONDS_PER_SECOND ( 1000 ) /**< @brief Milliseconds per second. */ #define _MILLISECONDS_PER_SECOND ( 1000 ) /**< @brief Milliseconds per second. */
#define _MILLISECONDS_PER_TICK ( _MILLISECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Milliseconds per FreeRTOS tick. */ #define _MILLISECONDS_PER_TICK ( _MILLISECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Milliseconds per FreeRTOS tick. */
@ -447,7 +444,6 @@ static BaseType_t prvSetupSocketRecvTimeout( cellularSocketWrapper_t * pCellular
static BaseType_t prvSetupSocketSendTimeout( cellularSocketWrapper_t * pCellularSocketContext, static BaseType_t prvSetupSocketSendTimeout( cellularSocketWrapper_t * pCellularSocketContext,
TickType_t sendTimeout ) TickType_t sendTimeout )
{ {
CellularError_t socketStatus = CELLULAR_SUCCESS;
BaseType_t retSetSockOpt = SOCKETS_ERROR_NONE; BaseType_t retSetSockOpt = SOCKETS_ERROR_NONE;
uint32_t sendTimeoutMs = 0; uint32_t sendTimeoutMs = 0;
CellularSocketHandle_t cellularSocketHandle = NULL; CellularSocketHandle_t cellularSocketHandle = NULL;
@ -478,18 +474,6 @@ static BaseType_t prvSetupSocketSendTimeout( cellularSocketWrapper_t * pCellular
pCellularSocketContext->sendTimeout = sendTimeout; pCellularSocketContext->sendTimeout = sendTimeout;
sendTimeoutMs = TICKS_TO_MS( sendTimeout ); sendTimeoutMs = TICKS_TO_MS( sendTimeout );
} }
socketStatus = Cellular_SocketSetSockOpt( CellularHandle,
cellularSocketHandle,
CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT,
CELLULAR_SOCKET_OPTION_SEND_TIMEOUT,
( const uint8_t * ) &sendTimeoutMs,
sizeof( uint32_t ) );
if( socketStatus != CELLULAR_SUCCESS )
{
retSetSockOpt = SOCKETS_EINVAL;
}
} }
return retSetSockOpt; return retSetSockOpt;
@ -599,7 +583,6 @@ BaseType_t Sockets_Connect( Socket_t * pTcpSocket,
CellularSocketAddress_t serverAddress = { 0 }; CellularSocketAddress_t serverAddress = { 0 };
EventBits_t waitEventBits = 0; EventBits_t waitEventBits = 0;
BaseType_t retConnect = SOCKETS_ERROR_NONE; BaseType_t retConnect = SOCKETS_ERROR_NONE;
const uint32_t defaultReceiveTimeoutMs = CELLULAR_SOCKET_RECV_TIMEOUT_MS;
/* Create a new TCP socket. */ /* Create a new TCP socket. */
cellularSocketStatus = Cellular_CreateSocket( CellularHandle, cellularSocketStatus = Cellular_CreateSocket( CellularHandle,
@ -660,23 +643,6 @@ BaseType_t Sockets_Connect( Socket_t * pTcpSocket,
retConnect = prvCellularSocketRegisterCallback( cellularSocketHandle, pCellularSocketContext ); retConnect = prvCellularSocketRegisterCallback( cellularSocketHandle, pCellularSocketContext );
} }
/* Setup cellular socket recv AT command default timeout. */
if( retConnect == SOCKETS_ERROR_NONE )
{
cellularSocketStatus = Cellular_SocketSetSockOpt( CellularHandle,
cellularSocketHandle,
CELLULAR_SOCKET_OPTION_LEVEL_TRANSPORT,
CELLULAR_SOCKET_OPTION_RECV_TIMEOUT,
( const uint8_t * ) &defaultReceiveTimeoutMs,
sizeof( uint32_t ) );
if( cellularSocketStatus != CELLULAR_SUCCESS )
{
IotLogError( "Failed to setup cellular AT command receive timeout %d.", cellularSocketStatus );
retConnect = SOCKETS_SOCKET_ERROR;
}
}
/* Setup cellular socket send/recv timeout. */ /* Setup cellular socket send/recv timeout. */
if( retConnect == SOCKETS_ERROR_NONE ) if( retConnect == SOCKETS_ERROR_NONE )
{ {
@ -923,7 +889,7 @@ int32_t Sockets_Send( Socket_t xSocket,
} }
} }
IotLogDebug( "Sockets_Send expect %d write %d", len, sentLength ); IotLogDebug( "Sockets_Send expect %d write %d", xDataLength, sentLength );
} }
return retSendLength; return retSendLength;

View file

@ -53,6 +53,9 @@
#define LIBRARY_LOG_LEVEL LOG_INFO #define LIBRARY_LOG_LEVEL LOG_INFO
#endif #endif
extern void vLoggingPrintf( const char * pcFormatString,
... );
#include "logging_stack.h" #include "logging_stack.h"
/************ End of logging configuration ****************/ /************ End of logging configuration ****************/

View file

@ -75,6 +75,8 @@ extern void vLoggingPrintf( const char * pcFormatString,
/************ End of logging configuration ****************/ /************ End of logging configuration ****************/
#define SOCKETS_INVALID_SOCKET ( ( Socket_t ) ~0U )
/** /**
* @brief Establish a connection to server. * @brief Establish a connection to server.
* *

View file

@ -36,10 +36,6 @@
/* FreeRTOS includes. */ /* FreeRTOS includes. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
/* TLS transport header. */ /* TLS transport header. */
#include "using_mbedtls.h" #include "using_mbedtls.h"
@ -700,9 +696,9 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
{ {
sslContextFree( &( pTlsTransportParams->sslContext ) ); sslContextFree( &( pTlsTransportParams->sslContext ) );
if( pTlsTransportParams->tcpSocket != FREERTOS_INVALID_SOCKET ) if( pTlsTransportParams->tcpSocket != SOCKETS_INVALID_SOCKET )
{ {
( void ) FreeRTOS_closesocket( pTlsTransportParams->tcpSocket ); ( void ) Sockets_Disconnect( pTlsTransportParams->tcpSocket );
} }
} }
} }

View file

@ -39,10 +39,6 @@
/* FreeRTOS includes. */ /* FreeRTOS includes. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
/* TLS transport header. */ /* TLS transport header. */
#include "using_mbedtls_pkcs11.h" #include "using_mbedtls_pkcs11.h"
@ -530,12 +526,25 @@ static CK_RV readCertificateIntoContext( SSLContext_t * pSslContext,
CK_RV xResult = CKR_OK; CK_RV xResult = CKR_OK;
CK_ATTRIBUTE xTemplate = { 0 }; CK_ATTRIBUTE xTemplate = { 0 };
CK_OBJECT_HANDLE xCertObj = 0; CK_OBJECT_HANDLE xCertObj = 0;
size_t labelLength;
char * pcNullTerminator = NULL;
/* Check for NULL character within pkcs11configMAX_LABEL_LENGTH. */
pcNullTerminator = memchr( pcLabelName, '\0', pkcs11configMAX_LABEL_LENGTH );
if( NULL != pcNullTerminator )
{
labelLength = ( size_t )( pcNullTerminator - pcLabelName );
}
else
{
/* If NULL character not found set length to pkcs11configMAX_LABEL_LENGTH. */
labelLength = pkcs11configMAX_LABEL_LENGTH;
}
/* Get the handle of the certificate. */ /* Get the handle of the certificate. */
xResult = xFindObjectWithLabelAndClass( pSslContext->xP11Session, xResult = xFindObjectWithLabelAndClass( pSslContext->xP11Session,
pcLabelName, pcLabelName,
strnlen( pcLabelName, labelLength,
pkcs11configMAX_LABEL_LENGTH ),
xClass, xClass,
&xCertObj ); &xCertObj );
@ -648,11 +657,25 @@ static CK_RV initializeClientKeys( SSLContext_t * pxCtx,
if( CKR_OK == xResult ) if( CKR_OK == xResult )
{ {
size_t labelLength;
char * pcNullTerminator = NULL;
/* Check for NULL character within pkcs11configMAX_LABEL_LENGTH. */
pcNullTerminator = memchr( pcLabelName, '\0', pkcs11configMAX_LABEL_LENGTH );
if( NULL != pcNullTerminator )
{
labelLength = ( size_t )( pcNullTerminator - pcLabelName );
}
else
{
/* If NULL character not found set length to pkcs11configMAX_LABEL_LENGTH. */
labelLength = pkcs11configMAX_LABEL_LENGTH;
}
/* Get the handle of the device private key. */ /* Get the handle of the device private key. */
xResult = xFindObjectWithLabelAndClass( pxCtx->xP11Session, xResult = xFindObjectWithLabelAndClass( pxCtx->xP11Session,
pcLabelName, pcLabelName,
strnlen( pcLabelName, labelLength,
pkcs11configMAX_LABEL_LENGTH ),
CKO_PRIVATE_KEY, CKO_PRIVATE_KEY,
&pxCtx->xP11PrivateKey ); &pxCtx->xP11PrivateKey );
} }
@ -901,9 +924,10 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
if( returnStatus != TLS_TRANSPORT_SUCCESS ) if( returnStatus != TLS_TRANSPORT_SUCCESS )
{ {
if( ( pNetworkContext != NULL ) && if( ( pNetworkContext != NULL ) &&
( pTlsTransportParams->tcpSocket != FREERTOS_INVALID_SOCKET ) ) ( pTlsTransportParams != NULL ) &&
( pTlsTransportParams->tcpSocket != SOCKETS_INVALID_SOCKET ) )
{ {
( void ) FreeRTOS_closesocket( pTlsTransportParams->tcpSocket ); ( void ) Sockets_Disconnect( pTlsTransportParams->tcpSocket );
} }
} }
else else

View file

@ -74,7 +74,7 @@ extern void vLoggingPrintf( const char * pcFormatString,
/************ End of logging configuration ****************/ /************ End of logging configuration ****************/
/* FreeRTOS+TCP include. */ /* FreeRTOS+TCP include. */
#include "FreeRTOS_Sockets.h" #include "sockets_wrapper.h"
/* Transport interface include. */ /* Transport interface include. */
#include "transport_interface.h" #include "transport_interface.h"

View file

@ -31,13 +31,16 @@
/* FreeRTOS includes. */ /* FreeRTOS includes. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "FreeRTOS_Sockets.h"
/* Sockets wrapper includes. */ /* Sockets wrapper includes. */
#include "sockets_wrapper.h" #include "sockets_wrapper.h"
/* mbed TLS includes. */ /* mbed TLS includes. */
#include "mbedtls_config.h" #if !defined( MBEDTLS_CONFIG_FILE )
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "threading_alt.h" #include "threading_alt.h"
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"

View file

@ -34,7 +34,11 @@
#include "FreeRTOS_Sockets.h" #include "FreeRTOS_Sockets.h"
/* mbed TLS includes. */ /* mbed TLS includes. */
#include "mbedtls_config.h" #if !defined( MBEDTLS_CONFIG_FILE )
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "threading_alt.h" #include "threading_alt.h"
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"

View file

@ -34,7 +34,11 @@
#include "FreeRTOS_Sockets.h" #include "FreeRTOS_Sockets.h"
/* mbed TLS includes. */ /* mbed TLS includes. */
#include "mbedtls_config.h" #if !defined( MBEDTLS_CONFIG_FILE )
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "threading_alt.h" #include "threading_alt.h"
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"
@ -180,7 +184,8 @@ int mbedtls_platform_mutex_unlock( mbedtls_threading_mutex_t * pMutex )
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/** #ifdef _WIN32
/**
* @brief Function to generate a random number. * @brief Function to generate a random number.
* *
* @param[in] data Callback context. * @param[in] data Callback context.
@ -191,11 +196,11 @@ int mbedtls_platform_mutex_unlock( mbedtls_threading_mutex_t * pMutex )
* @return 0 if no critical failures occurred, * @return 0 if no critical failures occurred,
* MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
*/ */
int mbedtls_platform_entropy_poll( void * data, int mbedtls_platform_entropy_poll( void * data,
unsigned char * output, unsigned char * output,
size_t len, size_t len,
size_t * olen ) size_t * olen )
{ {
int status = 0; int status = 0;
NTSTATUS rngStatus = 0; NTSTATUS rngStatus = 0;
@ -223,11 +228,13 @@ int mbedtls_platform_entropy_poll( void * data,
} }
return status; return status;
} }
#endif
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/** #ifdef _WIN32
/**
* @brief Function to generate a random number based on a hardware poll. * @brief Function to generate a random number based on a hardware poll.
* *
* For this FreeRTOS Windows port, this function is redirected by calling * For this FreeRTOS Windows port, this function is redirected by calling
@ -241,12 +248,13 @@ int mbedtls_platform_entropy_poll( void * data,
* @return 0 if no critical failures occurred, * @return 0 if no critical failures occurred,
* MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
*/ */
int mbedtls_hardware_poll( void * data, int mbedtls_hardware_poll( void * data,
unsigned char * output, unsigned char * output,
size_t len, size_t len,
size_t * olen ) size_t * olen )
{ {
return mbedtls_platform_entropy_poll( data, output, len, olen ); return mbedtls_platform_entropy_poll( data, output, len, olen );
} }
#endif
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/