From c76e923346effd166b00029007ff3f686940eab7 Mon Sep 17 00:00:00 2001 From: Teemu Takaluoma Date: Fri, 26 Apr 2019 11:18:15 +0300 Subject: [PATCH] mbed-cloud-client 3.1.0 --- CHANGELOG.md | 13 + CMakeLists.txt | 1 + Makefile | 1 + doxygen/mbedcloudclient_doxy | 2 +- .../mbedTLS/mbedTLSConfig_ARIA_mbedOS.h | 6 +- .../Configs/mbedTLS/mbedTLSConfig_FreeRTOS.h | 12 +- .../Configs/mbedTLS/mbedTLSConfig_Linux.h | 4 + .../mbedTLS/mbedTLSConfig_SXOS_SW_TRNG.h | 4 + .../Configs/mbedTLS/mbedTLSConfig_mbedOS.h | 17 +- .../mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG.h | 19 +- .../mbedTLSConfig_mbedOS_SW_TRNG_PSA.h | 20 +- mbed-client-pal/Makefile | 1 + mbed-client-pal/Source/CMakeLists.txt | 6 + .../PAL-Impl/Modules/Networking/pal_network.c | 6 +- .../Source/PAL-Impl/Modules/TLS/pal_TLS.c | 19 +- .../Source/PAL-Impl/Services-API/pal.h | 32 +- .../Source/PAL-Impl/Services-API/pal_TLS.h | 13 +- .../PAL-Impl/Services-API/pal_configuration.h | 2 +- .../PAL-Impl/Services-API/pal_network.h | 14 + .../Port/Platform-API/pal_plat_Crypto.h | 9 +- .../Source/Port/Platform-API/pal_plat_TLS.h | 10 - .../Port/Platform-API/pal_plat_network.h | 9 +- .../Lib_Specific/mbedTLS/TLS/pal_plat_TLS.c | 39 --- .../Networking/LWIP/pal_plat_network.c | 9 + .../Linux/Networking/pal_plat_network.c | 9 + .../SXOS/Networking/pal_plat_network.c | 9 + .../OS_Specific/SXOS/RTOS/pal_plat_rtos.c | 6 +- .../mbedOS/Networking/pal_plat_network.cpp | 90 ++++- .../Storage/Flash/pal_plat_internalFlash.cpp | 2 + mbed-client-pal/Test/Makefile | 1 + .../Test/PAL_Modules/Crypto/pal_crypto_test.c | 4 - .../PAL_Modules/Crypto/pal_crypto_test_data.h | 2 +- .../Test/PAL_Modules/DRBG/pal_drbg_test.c | 2 - .../PAL_Modules/Entropy/pal_entropy_test.c | 5 +- .../PAL_Modules/Networking/pal_socket_test.c | 40 ++- .../Networking/pal_socket_test_runner.c | 1 + .../Test/PAL_Modules/ROT/pal_rot_test.c | 5 +- .../Test/PAL_Modules/RTOS/pal_rtos_test.c | 124 ++++++- .../PAL_Modules/RTOS/pal_rtos_test_runner.c | 4 + .../PAL_Modules/RTOS/pal_rtos_test_utils.c | 34 ++ .../PAL_Modules/RTOS/pal_rtos_test_utils.h | 4 + .../Test/PAL_Modules/SST/pal_sst_tests.c | 1 - .../Test/PAL_Modules/TLS/pal_tls_test.c | 306 +++++++++++------ .../Test/PAL_Modules/TLS/pal_tls_utils.h | 131 ++----- .../Test/PAL_Modules/Time/pal_time_test.c | 4 +- mbed-client-pal/mbed_lib.json | 2 +- .../mbed-client-c/source/include/sn_grs.h | 7 + mbed-client/mbed-client-c/source/sn_nsdl.c | 24 +- .../m2mconnectionhandlerpimpl.h | 8 + .../source/m2mconnectionhandlerpimpl.cpp | 35 ++ mbed-client/mbed-client/m2mbase.h | 8 +- .../mbed-client/m2mconnectionobserver.h | 7 + mbed-client/mbed-client/m2mconstants.h | 10 +- mbed-client/mbed-client/m2mendpoint.h | 4 +- mbed-client/mbed-client/m2minterface.h | 2 + mbed-client/mbed_lib.json | 8 - mbed-client/source/include/m2minterfaceimpl.h | 11 + mbed-client/source/include/m2mnsdlinterface.h | 36 +- mbed-client/source/include/m2mnsdlobserver.h | 2 + mbed-client/source/m2mbase.cpp | 6 +- mbed-client/source/m2minterfaceimpl.cpp | 114 ++++--- mbed-client/source/m2mnsdlinterface.cpp | 87 +++-- source/ConnectorClient.cpp | 321 ++++++++++-------- source/ServiceClient.cpp | 12 +- source/include/ConnectorClient.h | 43 ++- source/include/ServiceClient.h | 8 +- 66 files changed, 1129 insertions(+), 678 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06146b008..840c8e19c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ ## Changelog for Pelion Device Management Client +### Release 3.1.0 (26.04.2019) + +* Fixed client State machine for handling `pause()` handling. Fixes issues when `pause()` call was ignored when other operations were in effect. +* Implemented network status callback handling for client library. Now client will react to changes in network status callbacks to speed up client connection recovery during reconnection. +* Improved internal flagging of client library to enable further optimizations and modularization of client components. + +#### Platform Adaptation Layer (PAL) + +* Improved TLS configuration to optimize RAM usage. +* Improvement header include handling inside PAL layer. +* CMake improvements. +* Improvements for PAL unit tests. + ### Release 3.0.0 (27.03.2019) #### Device Management Connect client diff --git a/CMakeLists.txt b/CMakeLists.txt index 2719b4028..b55bec2b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ ADD_GLOBALDIR(${MBED_CLIENT_SOURCE_DIR}/mbed-client-mbed-tls/mbed-client-mbedtls # pal headers SET(PAL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-pal/Source) +ADD_GLOBALDIR(${PAL_SOURCE_DIR}) ADD_GLOBALDIR(${PAL_SOURCE_DIR}/PAL-Impl/Services-API) ADD_GLOBALDIR(${PAL_SOURCE_DIR}/Port/Platform-API) diff --git a/Makefile b/Makefile index 15af0d7ac..4660aadfb 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ LOCAL_ADD_INCLUDE += \ ${LOCAL_NAME}/mbed-client-pal/Configs/pal_config \ ${LOCAL_NAME}/mbed-client-pal/Configs/pal_config/SXOS \ ${LOCAL_NAME}/mbed-client-pal/Source/PAL-Impl/Services-API \ + ${LOCAL_NAME}/mbed-client-pal/Source \ ${LOCAL_NAME}/mbed-client-pal/Source/Port/Platform-API \ # update client LOCAL_ADD_INCLUDE += \ diff --git a/doxygen/mbedcloudclient_doxy b/doxygen/mbedcloudclient_doxy index 700f575b4..52eca887a 100644 --- a/doxygen/mbedcloudclient_doxy +++ b/doxygen/mbedcloudclient_doxy @@ -38,7 +38,7 @@ PROJECT_NUMBER = # for a project that appears at the top of each page and should give viewer # a quick idea about the purpose of the project. Keep the description short. -PROJECT_BRIEF = "mbed Cloud Client C++ library" +PROJECT_BRIEF = "Device Management Client C++ library" # With the PROJECT_LOGO tag one can specify an logo or icon that is # included in the documentation. The maximum height of the logo should not diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_ARIA_mbedOS.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_ARIA_mbedOS.h index 2e64262ec..83e590a96 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_ARIA_mbedOS.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_ARIA_mbedOS.h @@ -20,7 +20,7 @@ #define PAL_MBEDTLS_USER_CONFIG_H -/*! All of the following definitions are mandatory requirements for correct +/*! All of the following definitions are mandatory requirements for correct * functionality of PAL TLS and Crypto components. * Please do not disable them. */ @@ -318,6 +318,10 @@ #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #include "mbedtls/check_config.h" #endif /* PAL_MBEDTLS_USER_CONFIG_H */ diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_FreeRTOS.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_FreeRTOS.h index 97120ed6c..ec3f3c970 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_FreeRTOS.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_FreeRTOS.h @@ -43,16 +43,16 @@ #define MBEDTLS_FREESCALE_LTC_DES /* Enable use of LTC DES.*/ #endif #define MBEDTLS_FREESCALE_LTC_AES /* Enable use of LTC AES.*/ - #if defined(FSL_FEATURE_LTC_HAS_GCM) && FSL_FEATURE_LTC_HAS_GCM + #if defined(FSL_FEATURE_LTC_HAS_GCM) && FSL_FEATURE_LTC_HAS_GCM #define MBEDTLS_FREESCALE_LTC_AES_GCM /* Enable use of LTC AES GCM.*/ #endif - #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA + #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA #define MBEDTLS_FREESCALE_LTC_PKHA /* Enable use of LTC PKHA.*/ #endif #endif /* Enable MMCAU use in library if there is MMCAU on chip. */ -#if defined(FSL_FEATURE_SOC_MMCAU_COUNT) && (FSL_FEATURE_SOC_MMCAU_COUNT > 0) +#if defined(FSL_FEATURE_SOC_MMCAU_COUNT) && (FSL_FEATURE_SOC_MMCAU_COUNT > 0) #include "fsl_mmcau.h" #define MBEDTLS_FREESCALE_MMCAU_MD5 /* Enable use of MMCAU MD5.*/ @@ -497,7 +497,7 @@ #define MBEDTLS_ECP_DP_SECP256R1_ENABLED //#define MBEDTLS_ECP_DP_SECP384R1_ENABLED //#ifndef MBEDTLS_FREESCALE_LTC_PKHA /* PKHA suports only <=512 */ -//#define MBEDTLS_ECP_DP_SECP521R1_ENABLED +//#define MBEDTLS_ECP_DP_SECP521R1_ENABLED //#endif //#define MBEDTLS_ECP_DP_SECP192K1_ENABLED //#define MBEDTLS_ECP_DP_SECP224K1_ENABLED @@ -2551,6 +2551,10 @@ #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #if defined(TARGET_LIKE_MBED) #include "mbedtls/target_config.h" #endif diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_Linux.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_Linux.h index 306d94a83..655a73b04 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_Linux.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_Linux.h @@ -2652,6 +2652,10 @@ #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #include "check_config.h" #if defined(MBEDTLS_TEST_NULL_ENTROPY) diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_SXOS_SW_TRNG.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_SXOS_SW_TRNG.h index 03b88f0dc..8d45e898c 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_SXOS_SW_TRNG.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_SXOS_SW_TRNG.h @@ -284,6 +284,10 @@ #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + /** * \def MBEDTLS_SSL_RENEGOTIATION * diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS.h index b757294ce..36de5f72d 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS.h @@ -20,7 +20,7 @@ #define PAL_MBEDTLS_USER_CONFIG_H -/*! All of the following definitions are mandatory requirements for correct +/*! All of the following definitions are mandatory requirements for correct * functionality of PAL TLS and Crypto components. * Please do not disable them. */ @@ -223,9 +223,13 @@ #define MBEDTLS_AES_ROM_TABLES #endif //MBEDTLS_AES_ROM_TABLES +// Read SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE from user config file +#ifdef MBED_CLIENT_USER_CONFIG_FILE +#include MBED_CLIENT_USER_CONFIG_FILE +#endif // Reduce IO buffer to save RAM, default is 16KB #ifndef MBEDTLS_SSL_MAX_CONTENT_LEN - #define MBEDTLS_SSL_MAX_CONTENT_LEN 4096 + #define MBEDTLS_SSL_MAX_CONTENT_LEN SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE * 4 #endif //MBEDTLS_SSL_MAX_CONTENT_LEN // needed for Base64 encoding Opaque data for @@ -306,11 +310,20 @@ #undef MBEDTLS_ECP_DP_BP512R1_ENABLED #undef MBEDTLS_ECP_DP_CURVE25519_ENABLED +// Tune elliptic curve configuration. +// This will hit the performance a bit but will decrease the RAM consumption by 4k. +#define MBEDTLS_ECP_WINDOW_SIZE 2 +#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 + // Reduces size particularly in case PSA crypto is used #undef MBEDTLS_CHACHA20_C #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #include "mbedtls/check_config.h" #endif /* PAL_MBEDTLS_USER_CONFIG_H */ diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG.h index 3aaa8b7b0..c42ac81ec 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG.h @@ -20,7 +20,7 @@ #define PAL_MBEDTLS_USER_CONFIG_H -/*! All of the following definitions are mandatory requirements for correct +/*! All of the following definitions are mandatory requirements for correct * functionality of PAL TLS and Crypto components. * Please do not disable them. */ @@ -230,9 +230,13 @@ #define MBEDTLS_AES_ROM_TABLES #endif //MBEDTLS_AES_ROM_TABLES +// Read SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE from user config file +#ifdef MBED_CLIENT_USER_CONFIG_FILE +#include MBED_CLIENT_USER_CONFIG_FILE +#endif // Reduce IO buffer to save RAM, default is 16KB #ifndef MBEDTLS_SSL_MAX_CONTENT_LEN - #define MBEDTLS_SSL_MAX_CONTENT_LEN 4096 + #define MBEDTLS_SSL_MAX_CONTENT_LEN SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE * 4 #endif //MBEDTLS_SSL_MAX_CONTENT_LEN // needed for Base64 encoding Opaque data for @@ -256,7 +260,7 @@ #undef MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED //#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED - + // Remove error messages, save 10KB of ROM #undef MBEDTLS_ERROR_C @@ -326,6 +330,15 @@ #undef MBEDTLS_CHACHAPOLY_C #undef MBEDTLS_POLY1305_C +// Tune elliptic curve configuration. +// This will hit the performance a bit but will decrease the RAM consumption by 4k. +#define MBEDTLS_ECP_WINDOW_SIZE 2 +#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 + +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #include "mbedtls/check_config.h" #endif /* PAL_MBEDTLS_USER_CONFIG_H */ diff --git a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG_PSA.h b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG_PSA.h index 59ed6f19e..b1f84a7e5 100644 --- a/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG_PSA.h +++ b/mbed-client-pal/Configs/mbedTLS/mbedTLSConfig_mbedOS_SW_TRNG_PSA.h @@ -20,7 +20,7 @@ #define PAL_MBEDTLS_USER_CONFIG_H -/*! All of the following definitions are mandatory requirements for correct +/*! All of the following definitions are mandatory requirements for correct * functionality of PAL TLS and Crypto components. * Please do not disable them. */ @@ -227,9 +227,13 @@ #define MBEDTLS_AES_ROM_TABLES #endif //MBEDTLS_AES_ROM_TABLES +// Read SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE from user config file +#ifdef MBED_CLIENT_USER_CONFIG_FILE +#include MBED_CLIENT_USER_CONFIG_FILE +#endif // Reduce IO buffer to save RAM, default is 16KB #ifndef MBEDTLS_SSL_MAX_CONTENT_LEN - #define MBEDTLS_SSL_MAX_CONTENT_LEN 4096 + #define MBEDTLS_SSL_MAX_CONTENT_LEN SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE * 4 #endif //MBEDTLS_SSL_MAX_CONTENT_LEN // needed for Base64 encoding Opaque data for @@ -266,7 +270,7 @@ #undef MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED //#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED - + // Remove error messages, save 10KB of ROM #undef MBEDTLS_ERROR_C @@ -331,6 +335,16 @@ #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED + +// Tune elliptic curve configuration. +// This will hit the performance a bit but will decrease the RAM consumption by 4k. +#define MBEDTLS_ECP_WINDOW_SIZE 2 +#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 + +// Do not save a copy of the peer certificate. +// This will reduce the RAM consumption roughly by 1500 bytes. +#undef MBEDTLS_SSL_KEEP_PEER_CERTIFICATE + #include "mbedtls/check_config.h" #endif /* PAL_MBEDTLS_USER_CONFIG_H */ diff --git a/mbed-client-pal/Makefile b/mbed-client-pal/Makefile index d1ca51ddf..be96a9b84 100644 --- a/mbed-client-pal/Makefile +++ b/mbed-client-pal/Makefile @@ -12,6 +12,7 @@ LOCAL_API_DEPENDS := \ ${API_PLATFORM_DEPENDS} \ LOCAL_ADD_INCLUDE := \ + ${LOCAL_NAME}/Source \ ${LOCAL_NAME}/Source/PAL-Impl \ ${LOCAL_NAME}/Source/PAL-Impl/Services-API \ ${LOCAL_NAME}/Source/Port/Platform-API \ diff --git a/mbed-client-pal/Source/CMakeLists.txt b/mbed-client-pal/Source/CMakeLists.txt index 98b653fac..96fe1cbab 100644 --- a/mbed-client-pal/Source/CMakeLists.txt +++ b/mbed-client-pal/Source/CMakeLists.txt @@ -1,6 +1,12 @@ include_directories(Port/Platform-API) + +# This is so other modules could include pal.h ADD_GLOBALDIR( ${CMAKE_CURRENT_SOURCE_DIR}/PAL-Impl/Services-API) +# Add the current the directory of this of this file as a global dir pal.h could include more qualified paths +# and modules that use PAL could still include pal.h +ADD_GLOBALDIR( ${CMAKE_CURRENT_SOURCE_DIR}) + set (PAL_MODULES_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/PAL-Impl/Modules) set (PAL_PORT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Port/Reference-Impl/OS_Specific/${OS_BRAND}) set (PAL_PORT_GENERIC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Port/Reference-Impl/Generic) diff --git a/mbed-client-pal/Source/PAL-Impl/Modules/Networking/pal_network.c b/mbed-client-pal/Source/PAL-Impl/Modules/Networking/pal_network.c index c409b0410..c9dcab856 100644 --- a/mbed-client-pal/Source/PAL-Impl/Modules/Networking/pal_network.c +++ b/mbed-client-pal/Source/PAL-Impl/Modules/Networking/pal_network.c @@ -63,10 +63,14 @@ palStatus_t pal_registerNetworkInterface(void* networkInterfaceContext, uint32_t { PAL_VALIDATE_ARGUMENTS((networkInterfaceContext == NULL) || (interfaceIndex == NULL)); palStatus_t result = pal_plat_registerNetworkInterface(networkInterfaceContext, interfaceIndex); - return result; } +palStatus_t pal_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *client_arg) +{ + return pal_plat_setConnectionStatusCallback(interfaceIndex, callback, client_arg); +} + palStatus_t pal_unregisterNetworkInterface(uint32_t interfaceIndex) { PAL_VALIDATE_ARGUMENTS(interfaceIndex > PAL_MAX_SUPORTED_NET_INTERFACES - 1); diff --git a/mbed-client-pal/Source/PAL-Impl/Modules/TLS/pal_TLS.c b/mbed-client-pal/Source/PAL-Impl/Modules/TLS/pal_TLS.c index a7da9f2e0..dcd204ca8 100755 --- a/mbed-client-pal/Source/PAL-Impl/Modules/TLS/pal_TLS.c +++ b/mbed-client-pal/Source/PAL-Impl/Modules/TLS/pal_TLS.c @@ -25,7 +25,6 @@ #define TRACE_GROUP "PAL" -PAL_PRIVATE uint8_t g_storedCertSerial[PAL_CERT_ID_SIZE] __attribute__ ((aligned(4))) = {0}; PAL_PRIVATE bool g_trustedServerValid = false; PAL_PRIVATE palMutexID_t g_palTLSHandshakeMutex = NULLPTR; @@ -98,7 +97,6 @@ palStatus_t pal_initTLS(palTLSConfHandle_t palTLSConf, palTLSHandle_t* palTLSHan *palTLSHandle = (palTLSHandle_t)palTLSCtx; } - memset(g_storedCertSerial, 0, sizeof(g_storedCertSerial)); g_trustedServerValid = false; palTLSCtx->retryHandShake = false; @@ -246,22 +244,6 @@ palStatus_t pal_addEntropySource(palEntropySource_f entropyCallback) return status; } -palStatus_t pal_setOwnCertAndPrivateKey(palTLSConfHandle_t palTLSConf, palX509_t* ownCert, palPrivateKey_t* privateKey) -{ -#if (PAL_ENABLE_X509 == 1) - palStatus_t status = PAL_SUCCESS; - palTLSConfService_t* palTLSConfCtx = (palTLSConfService_t*)palTLSConf; - - PAL_VALIDATE_ARGUMENTS (NULLPTR == palTLSConf); - PAL_VALIDATE_ARGUMENTS (NULLPTR == palTLSConfCtx->platTlsConfHandle || NULL == ownCert || NULL == privateKey); - - status = pal_plat_setOwnCertAndPrivateKey(palTLSConfCtx->platTlsConfHandle, ownCert, privateKey); - return status; -#else - return PAL_ERR_NOT_SUPPORTED; -#endif -} - palStatus_t pal_setOwnCertChain(palTLSConfHandle_t palTLSConf, palX509_t* ownCert) { #if (PAL_ENABLE_X509 == 1) @@ -348,6 +330,7 @@ palStatus_t pal_setCAChain(palTLSConfHandle_t palTLSConf, palX509_t* caChain, pa goto finish; } + uint8_t g_storedCertSerial[PAL_CERT_ID_SIZE] __attribute__ ((aligned(4))) = {0}; if (!g_trustedServerValid) { size_t actualLenBytes; diff --git a/mbed-client-pal/Source/PAL-Impl/Services-API/pal.h b/mbed-client-pal/Source/PAL-Impl/Services-API/pal.h index 3c7dcc26a..e243b7344 100644 --- a/mbed-client-pal/Source/PAL-Impl/Services-API/pal.h +++ b/mbed-client-pal/Source/PAL-Impl/Services-API/pal.h @@ -23,24 +23,24 @@ extern "C" { #endif //includes for common headers in PAL -#include "pal_configuration.h" -#include "pal_macros.h" -#include "pal_errors.h" -#include "pal_types.h" +#include "PAL-Impl/Services-API/pal_configuration.h" +#include "PAL-Impl/Services-API/pal_macros.h" +#include "PAL-Impl/Services-API/pal_errors.h" +#include "PAL-Impl/Services-API/pal_types.h" //includes for modules headers. -#include "pal_drbg.h" -#include "pal_fileSystem.h" -#include "pal_rot.h" -#include "pal_rtos.h" -#include "pal_network.h" -#include "pal_time.h" -#include "pal_TLS.h" -#include "pal_Crypto.h" -#include "pal_entropy.h" -#include "pal_update.h" -#include "pal_internalFlash.h" -#include "pal_sst.h" +#include "PAL-Impl/Services-API/pal_drbg.h" +#include "PAL-Impl/Services-API/pal_fileSystem.h" +#include "PAL-Impl/Services-API/pal_rot.h" +#include "PAL-Impl/Services-API/pal_rtos.h" +#include "PAL-Impl/Services-API/pal_network.h" +#include "PAL-Impl/Services-API/pal_time.h" +#include "PAL-Impl/Services-API/pal_TLS.h" +#include "PAL-Impl/Services-API/pal_Crypto.h" +#include "PAL-Impl/Services-API/pal_entropy.h" +#include "PAL-Impl/Services-API/pal_update.h" +#include "PAL-Impl/Services-API/pal_internalFlash.h" +#include "PAL-Impl/Services-API/pal_sst.h" /*! \file pal.h diff --git a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_TLS.h b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_TLS.h index fec8d75fe..42646cf1b 100644 --- a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_TLS.h +++ b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_TLS.h @@ -135,17 +135,6 @@ palStatus_t pal_initTLSConfiguration(palTLSConfHandle_t* palTLSConf, palTLSTrans */ palStatus_t pal_tlsConfigurationFree(palTLSConfHandle_t* palTLSConf); -/*! \brief Set your own certificate chain and private key. - * - * @deprecated This function has been split into two separate functions, `pal_setOwnCertChain()` and `pal_setOwnPrivateKey()`. - * @param[in] palTLSConf: The TLS configuration context. - * @param[in] ownCert: Your own public certificate chain. - * @param[in] privateKey: Your own private key. - * - * \return PAL_SUCCESS on success, or a negative value indicating a specific error code in case of failure. - */ -palStatus_t pal_setOwnCertAndPrivateKey(palTLSConfHandle_t palTLSConf, palX509_t* ownCert, palPrivateKey_t* privateKey); - /*! \brief Set your own certificate chain. * * @param[in] palTLSConf: The TLS configuration context. @@ -160,7 +149,7 @@ palStatus_t pal_setOwnCertChain(palTLSConfHandle_t palTLSConf, palX509_t* ownCer * @param[in] buf: If MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is defined - pointer to a `uintptr_t` type, which contains the PSA handle. * If MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is not defined - pointer to a private key. * @param[in] buf_size: If MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is defined - not relevant, as it is expected that buf points to a `uintptr_t` type. -* If MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is not defined - the size of the private key pointed to by buf. +* If MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT is not defined - the size of the private key pointed to by buf. * @param[out] privateKey: Pointer to an uninitialized `palPrivateKey_t` object. * \return PAL_SUCCESS on success. A negative value indicating a specific error code in case of failure. diff --git a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_configuration.h b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_configuration.h index 0df341475..580d68476 100644 --- a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_configuration.h +++ b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_configuration.h @@ -124,7 +124,7 @@ //! The maximum number of interfaces that can be supported at a time. #ifndef PAL_MAX_SUPORTED_NET_INTERFACES - #define PAL_MAX_SUPORTED_NET_INTERFACES 10 + #define PAL_MAX_SUPORTED_NET_INTERFACES 1 #endif //! Stack size for thread created when calling pal_getAddressInfoAsync diff --git a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_network.h b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_network.h index e846a6b15..72089693c 100644 --- a/mbed-client-pal/Source/PAL-Impl/Services-API/pal_network.h +++ b/mbed-client-pal/Source/PAL-Impl/Services-API/pal_network.h @@ -63,6 +63,12 @@ typedef enum { PAL_AF_INET6 = 10, /*!< \brief IP version 6. */ } palSocketDomain_t; +/*! \brief Network status event. */ +typedef enum { + PAL_NETWORK_STATUS_DISCONNECTED = 0, + PAL_NETWORK_STATUS_CONNECTED = 1 +} palNetworkStatus_t; + /*! \brief Socket types supported by PAL. */ typedef enum { #if PAL_NET_TCP_AND_TLS_SUPPORT @@ -92,6 +98,7 @@ typedef enum { typedef uint8_t palIpV4Addr_t[PAL_IPV4_ADDRESS_SIZE]; typedef uint8_t palIpV6Addr_t[PAL_IPV6_ADDRESS_SIZE]; +typedef void(*connectionStatusCallback) (palNetworkStatus_t status, void *client_arg); /*! \brief Register a network interface for use with PAL sockets. * @@ -223,6 +230,13 @@ palStatus_t pal_getNumberOfNetInterfaces(uint32_t* numInterfaces); */ palStatus_t pal_getNetInterfaceInfo(uint32_t interfaceNum, palNetInterfaceInfo_t* interfaceInfo); +/*! \brief Set listener for connection status events. + * @param[in] interfaceNum Index of the network interface to be listen. + * @param[in] callback Callback that is called when network interface status change. + * @param[in] client_arg The argument which is passed to the callback function. + * \return PAL_SUCCESS (0) in case of success, a specific negative error code in case of failure. + */ +palStatus_t pal_setConnectionStatusCallback(uint32_t interfaceNum, connectionStatusCallback callback, void *client_arg); #define PAL_NET_SOCKET_SELECT_MAX_SOCKETS 8 #define PAL_NET_SOCKET_SELECT_RX_BIT (1) diff --git a/mbed-client-pal/Source/Port/Platform-API/pal_plat_Crypto.h b/mbed-client-pal/Source/Port/Platform-API/pal_plat_Crypto.h index 190000799..a0b76b9e7 100644 --- a/mbed-client-pal/Source/Port/Platform-API/pal_plat_Crypto.h +++ b/mbed-client-pal/Source/Port/Platform-API/pal_plat_Crypto.h @@ -17,8 +17,11 @@ #ifndef _PAL_PLAT_CRYPTO_H_ #define _PAL_PLAT_CRYPTO_H_ -#include "pal_Crypto.h" +#ifdef __cplusplus +extern "C" { +#endif +#include "pal.h" /*! \file pal_plat_Crypto.h * \brief PAL cryptographic - platform. * This file contains cryptographic APIs that need to be implemented in the platform layer. @@ -666,5 +669,7 @@ palStatus_t pal_plat_ECGroupFree(palCurveHandle_t* grp); */ palStatus_t pal_plat_ECGroupInitAndLoad(palCurveHandle_t* grp, palGroupIndex_t index); - +#ifdef __cplusplus +} +#endif #endif //_PAL_PLAT_CRYPTO_H_ diff --git a/mbed-client-pal/Source/Port/Platform-API/pal_plat_TLS.h b/mbed-client-pal/Source/Port/Platform-API/pal_plat_TLS.h index 73460a2a7..7e69e58ea 100644 --- a/mbed-client-pal/Source/Port/Platform-API/pal_plat_TLS.h +++ b/mbed-client-pal/Source/Port/Platform-API/pal_plat_TLS.h @@ -219,16 +219,6 @@ palStatus_t pal_plat_renegotiate(palTLSHandle_t palTLSHandle, uint64_t serverTim */ palStatus_t pal_plat_tlsSetSocket(palTLSConfHandle_t palTLSConf, palTLSSocket_t* socket); -/*! \brief Set your own certificate chain and private key. - * - * @param[in] palTLSConf: The TLS configuration context. - * @param[in] ownCert: Your own public certificate chain. - * @param[in] privateKey: Your own private key. - * - * \return PAL_SUCCESS on success. A negative value indicating a specific error code in case of failure. - */ -palStatus_t pal_plat_setOwnCertAndPrivateKey(palTLSConfHandle_t palTLSConf, palX509_t* ownCert, palPrivateKey_t* privateKey); - /*! \brief Set your own certificate chain. * * @param[in] palTLSConf: The TLS configuration context. diff --git a/mbed-client-pal/Source/Port/Platform-API/pal_plat_network.h b/mbed-client-pal/Source/Port/Platform-API/pal_plat_network.h index 45d9a04d8..0a4be843f 100644 --- a/mbed-client-pal/Source/Port/Platform-API/pal_plat_network.h +++ b/mbed-client-pal/Source/Port/Platform-API/pal_plat_network.h @@ -190,6 +190,14 @@ palStatus_t pal_plat_recv(palSocket_t socket, void* buf, size_t len, size_t* rec */ palStatus_t pal_plat_send(palSocket_t socket, const void* buf, size_t len, size_t* sentDataSize); +/*! \brief Set listener for connection status events. + * @param[in] interfaceIndex Index of the network interface to be listen. + * @param[in] callback Callback that is called when network interface status change. + * @param[in] client_arg The argument which is passed to the callback function. + * \return PAL_SUCCESS (0) in case of success, a specific negative error code in case of failure. + */ +palStatus_t pal_plat_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *client_arg); + #endif //PAL_NET_TCP_AND_TLS_SUPPORT /*! \brief Get an asynchronous network socket. @@ -229,7 +237,6 @@ palStatus_t pal_plat_cancelAddressInfoAsync(palDNSQuery_t queryHandle); #endif // PAL_NET_DNS_SUPPORT - #ifdef __cplusplus } #endif diff --git a/mbed-client-pal/Source/Port/Reference-Impl/Lib_Specific/mbedTLS/TLS/pal_plat_TLS.c b/mbed-client-pal/Source/Port/Reference-Impl/Lib_Specific/mbedTLS/TLS/pal_plat_TLS.c index c71139a45..b99524c05 100755 --- a/mbed-client-pal/Source/Port/Reference-Impl/Lib_Specific/mbedTLS/TLS/pal_plat_TLS.c +++ b/mbed-client-pal/Source/Port/Reference-Impl/Lib_Specific/mbedTLS/TLS/pal_plat_TLS.c @@ -820,45 +820,6 @@ palStatus_t pal_plat_renegotiate(palTLSHandle_t palTLSHandle, uint64_t serverTim #if (PAL_ENABLE_X509 == 1) -palStatus_t pal_plat_setOwnCertAndPrivateKey(palTLSConfHandle_t palTLSConf, palX509_t* ownCert, palPrivateKey_t* privateKey) -{ - palStatus_t status = PAL_SUCCESS; - palTLSConf_t* localConfigCtx = (palTLSConf_t*)palTLSConf; - int32_t platStatus = SSL_LIB_SUCCESS; - - mbedtls_pk_init(&localConfigCtx->pkey); - - - platStatus = mbedtls_x509_crt_parse_der(&localConfigCtx->owncert, (const unsigned char *)ownCert->buffer, ownCert->size); - if (SSL_LIB_SUCCESS != platStatus) - { - status = PAL_ERR_TLS_FAILED_TO_PARSE_CERT; - goto finish; - } - -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT - platStatus = mbedtls_pk_setup_opaque(&localConfigCtx->pkey, *privateKey); - - if (SSL_LIB_SUCCESS != platStatus) - { - status = PAL_ERR_TLS_FAILED_TO_PARSE_KEY; - goto finish; - } -#endif //MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT - - platStatus = mbedtls_ssl_conf_own_cert(localConfigCtx->confCtx, &localConfigCtx->owncert, &localConfigCtx->pkey); - if (SSL_LIB_SUCCESS != platStatus) - { - status = PAL_ERR_TLS_FAILED_TO_SET_CERT; - } - - localConfigCtx->hasKeys = true; - -finish: - PAL_LOG_DBG("TLS set and parse status %" PRIu32 ".", platStatus); - return status; -} - palStatus_t pal_plat_setOwnPrivateKey(palTLSConfHandle_t palTLSConf, palPrivateKey_t* privateKey) { palStatus_t status = PAL_SUCCESS; diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/FreeRTOS/Networking/LWIP/pal_plat_network.c b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/FreeRTOS/Networking/LWIP/pal_plat_network.c index 2f55ad54a..76d073410 100644 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/FreeRTOS/Networking/LWIP/pal_plat_network.c +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/FreeRTOS/Networking/LWIP/pal_plat_network.c @@ -947,3 +947,12 @@ palStatus_t pal_plat_getAddressInfo(const char *hostname, palSocketAddress_t *ad } #endif + +palStatus_t pal_plat_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *arg) +{ + (void)interfaceIndex; + (void)callback; + (void)arg; + + return PAL_ERR_NOT_SUPPORTED; +} diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/Linux/Networking/pal_plat_network.c b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/Linux/Networking/pal_plat_network.c index 2bc1bb249..236ba00e7 100755 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/Linux/Networking/pal_plat_network.c +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/Linux/Networking/pal_plat_network.c @@ -1205,3 +1205,12 @@ palStatus_t pal_plat_getAddressInfo(const char *hostname, palSocketAddress_t *ad } #endif + +palStatus_t pal_plat_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *arg) +{ + (void)interfaceIndex; + (void)callback; + (void)arg; + + return PAL_ERR_NOT_SUPPORTED; +} diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/Networking/pal_plat_network.c b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/Networking/pal_plat_network.c index 80ad416e4..9d276c13e 100644 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/Networking/pal_plat_network.c +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/Networking/pal_plat_network.c @@ -1011,3 +1011,12 @@ palStatus_t pal_plat_getAddressInfo(const char *hostname, palSocketAddress_t *ad } #endif + +palStatus_t pal_plat_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *arg) +{ + (void)interfaceIndex; + (void)callback; + (void)arg; + + return PAL_ERR_NOT_SUPPORTED; +} diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/RTOS/pal_plat_rtos.c b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/RTOS/pal_plat_rtos.c index 388390f94..851d14410 100755 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/RTOS/pal_plat_rtos.c +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/SXOS/RTOS/pal_plat_rtos.c @@ -31,6 +31,7 @@ #include // snprintf #define TRACE_GROUP "PAL" +#define PAL_THREAD_PRIORITY_TRANSLATE(x) ((COS_MMI_TASKS_PRIORITY_BASE + (uint8_t)PAL_osPrioritylast) - x) extern palStatus_t pal_plat_getRandomBufferFromHW(uint8_t *randomBuf, size_t bufSizeBytes, size_t* actualRandomSizeBytes); @@ -221,9 +222,10 @@ palStatus_t pal_plat_osThreadCreate(palThreadFuncPtr function, void* funcArgumen thread->userFunction = function; thread->userFunctionArgument = funcArgument; - // XXX: the priorities need a mapping, so far just use a 0x80 (0 is highest, 0xff lowest priority) + // 0xDC - 0xFA is reserved for MMI task, 0xDC is default + // (0 is highest, 0xff lowest priority) - uint8_t taskPriority = 0x80; + uint8_t taskPriority = PAL_THREAD_PRIORITY_TRANSLATE(priority); // Create and start a task. As the thread cleanup seems to require a COS_StopTask(), // we use a wrapper to call the user provided function and eventually do the cleanup. diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Networking/pal_plat_network.cpp b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Networking/pal_plat_network.cpp index 861cabeca..43180d219 100644 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Networking/pal_plat_network.cpp +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Networking/pal_plat_network.cpp @@ -24,6 +24,12 @@ typedef void(*palSelectCallbackFunction_t)(); +typedef struct pal_plat_NetworkInterface{ + NetworkInterface* interface; + void *clientArg; + connectionStatusCallback connectionStatusCb; +} pal_plat_NetworkInterface_t; + #if defined (__CC_ARM) || defined(__IAR_SYSTEMS_ICC__) void palSelectCallbackNull(void* arg) @@ -242,7 +248,11 @@ void palConnectCallBack() // as the high priority queue and its thread is likely there already thanks to // arm_hal_timer.cpp. Technically the client side does not really care, if the events // were delayed a bit by other events or not. +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT shared_event_queue = mbed_highprio_event_queue(); +#else + shared_event_queue = mbed_event_queue(); +#endif PAL_VALIDATE_CONDITION_WITH_ERROR((shared_event_queue == NULL),NSAPI_ERROR_UNSUPPORTED); Callback mycall(this, &PALSocketWrapper::attachCallback); @@ -403,7 +413,7 @@ void palConnectCallBack() } -PAL_PRIVATE NetworkInterface* s_pal_networkInterfacesSupported[PAL_MAX_SUPORTED_NET_INTERFACES] = { 0 }; +PAL_PRIVATE pal_plat_NetworkInterface_t s_pal_networkInterfacesSupported[PAL_MAX_SUPORTED_NET_INTERFACES] = { 0 }; PAL_PRIVATE uint32_t s_pal_numberOFInterfaces = 0; @@ -411,6 +421,8 @@ PAL_PRIVATE uint32_t s_pal_network_initialized = 0; PAL_PRIVATE palStatus_t create_socket(palSocketDomain_t domain, palSocketType_t type, bool nonBlockingSocket, uint32_t interfaceNum, palAsyncSocketCallback_t callback, void* arg, palSocket_t* socket); +void pal_plat_connectionStatusCallback(void *interfaceIndex, nsapi_event_t status, intptr_t param); + PAL_PRIVATE palStatus_t translateErrorToPALError(int errnoValue) { palStatus_t status; @@ -486,7 +498,7 @@ palStatus_t pal_plat_registerNetworkInterface(void* context, uint32_t* interface for (index = 0; index < s_pal_numberOFInterfaces; index++) // if specific context already registered return exisitng index instead of registering again. { - if (s_pal_networkInterfacesSupported[index] == context) + if (s_pal_networkInterfacesSupported[index].interface == context) { found = true; *interfaceIndex = index; @@ -498,7 +510,7 @@ palStatus_t pal_plat_registerNetworkInterface(void* context, uint32_t* interface { if (s_pal_numberOFInterfaces < PAL_MAX_SUPORTED_NET_INTERFACES) { - s_pal_networkInterfacesSupported[s_pal_numberOFInterfaces] = (NetworkInterface*)context; + s_pal_networkInterfacesSupported[s_pal_numberOFInterfaces].interface = (NetworkInterface*)context; *interfaceIndex = s_pal_numberOFInterfaces; ++s_pal_numberOFInterfaces; } @@ -511,11 +523,28 @@ palStatus_t pal_plat_registerNetworkInterface(void* context, uint32_t* interface return result; } +palStatus_t pal_plat_setConnectionStatusCallback(uint32_t interfaceIndex, connectionStatusCallback callback, void *arg) +{ + palStatus_t result = PAL_SUCCESS; + if (interfaceIndex > PAL_MAX_SUPORTED_NET_INTERFACES - 1) { + result = PAL_ERR_INVALID_ARGUMENT; + } else { + s_pal_networkInterfacesSupported[interfaceIndex].interface->add_event_listener(mbed::callback(&pal_plat_connectionStatusCallback, (void*)interfaceIndex)); + s_pal_networkInterfacesSupported[interfaceIndex].connectionStatusCb = callback; + s_pal_networkInterfacesSupported[interfaceIndex].clientArg = arg; + } + + return result; +} + palStatus_t pal_plat_unregisterNetworkInterface(uint32_t interfaceIndex) { if (interfaceIndex < PAL_MAX_SUPORTED_NET_INTERFACES && - s_pal_networkInterfacesSupported[interfaceIndex]) { - s_pal_networkInterfacesSupported[interfaceIndex] = NULL; + s_pal_networkInterfacesSupported[interfaceIndex].interface) { + s_pal_networkInterfacesSupported[interfaceIndex].interface->remove_event_listener(mbed::callback(&pal_plat_connectionStatusCallback, (void*)interfaceIndex)); + s_pal_networkInterfacesSupported[interfaceIndex].interface = NULL; + s_pal_networkInterfacesSupported[interfaceIndex].clientArg = NULL; + s_pal_networkInterfacesSupported[interfaceIndex].connectionStatusCb = NULL; --s_pal_numberOFInterfaces; return PAL_SUCCESS; } else { @@ -861,7 +890,7 @@ palStatus_t pal_plat_getNetInterfaceInfo(uint32_t interfaceNum, palNetInterfaceI SocketAddress addr; PAL_VALIDATE_ARGUMENTS((interfaceNum >= s_pal_numberOFInterfaces)); - address = s_pal_networkInterfacesSupported[interfaceNum]->get_ip_address(); // ip address returned is a null terminated string + address = s_pal_networkInterfacesSupported[interfaceNum].interface->get_ip_address(); // ip address returned is a null terminated string if (NULL != address) { addr.set_ip_address(address); @@ -1067,13 +1096,13 @@ palStatus_t pal_plat_getAddressInfo(const char *hostname, palSocketAddress_t *ad { palStatus_t result = PAL_SUCCESS; SocketAddress translatedAddress; // by default use the fist supported net interface - TODO: do we need to select a different interface? - if (s_pal_networkInterfacesSupported[0]) { + if (s_pal_networkInterfacesSupported[0].interface) { #if PAL_NET_DNS_IP_SUPPORT == PAL_NET_DNS_ANY - result = s_pal_networkInterfacesSupported[0]->gethostbyname(hostname, &translatedAddress); + result = s_pal_networkInterfacesSupported[0].interface->gethostbyname(hostname, &translatedAddress); #elif PAL_NET_DNS_IP_SUPPORT == PAL_NET_DNS_IPV4_ONLY - result = s_pal_networkInterfacesSupported[0]->gethostbyname(hostname, &translatedAddress, NSAPI_IPv4); + result = s_pal_networkInterfacesSupported[0].interface->gethostbyname(hostname, &translatedAddress, NSAPI_IPv4); #elif PAL_NET_DNS_IP_SUPPORT == PAL_NET_DNS_IPV6_ONLY - result = s_pal_networkInterfacesSupported[0]->gethostbyname(hostname, &translatedAddress, NSAPI_IPv6); + result = s_pal_networkInterfacesSupported[0].interface->gethostbyname(hostname, &translatedAddress, NSAPI_IPv6); #else #error PAL_NET_DNS_IP_SUPPORT is not defined to a valid value. #endif @@ -1121,10 +1150,10 @@ palStatus_t pal_plat_getAddressInfoAsync(pal_asyncAddressInfo* info) #error PAL_NET_DNS_IP_SUPPORT is not defined to a valid value. #endif - if (s_pal_networkInterfacesSupported[0] == NULL) { + if (s_pal_networkInterfacesSupported[0].interface == NULL) { result = PAL_ERR_INVALID_ARGUMENT; } else { - result = s_pal_networkInterfacesSupported[0]->gethostbyname_async(info->hostname, mbed::Callback(pal_plat_getAddressInfoAsync_callback,(void*)info), version); + result = s_pal_networkInterfacesSupported[0].interface->gethostbyname_async(info->hostname, mbed::Callback(pal_plat_getAddressInfoAsync_callback,(void*)info), version); } PAL_LOG_DBG("pal_plat_getAddressInfoAsync result %d", result); @@ -1146,8 +1175,8 @@ palStatus_t pal_plat_getAddressInfoAsync(pal_asyncAddressInfo* info) palStatus_t pal_plat_cancelAddressInfoAsync(palDNSQuery_t queryHandle) { palStatus_t status = PAL_ERR_INVALID_ARGUMENT; - if (s_pal_networkInterfacesSupported[0]) { - status = s_pal_networkInterfacesSupported[0]->gethostbyname_async_cancel(queryHandle); + if (s_pal_networkInterfacesSupported[0].interface) { + status = s_pal_networkInterfacesSupported[0].interface->gethostbyname_async_cancel(queryHandle); } if (PAL_SUCCESS != status) { @@ -1190,7 +1219,7 @@ PAL_PRIVATE palStatus_t create_socket(palSocketDomain_t domain, palSocketType_t if (internalSocket && result == PAL_SUCCESS) { - result = internalSocket->open(s_pal_networkInterfacesSupported[interfaceNum]); + result = internalSocket->open(s_pal_networkInterfacesSupported[interfaceNum].interface); if (result < 0) { result = translateErrorToPALError(result); @@ -1225,7 +1254,38 @@ PAL_PRIVATE palStatus_t create_socket(palSocketDomain_t domain, palSocketType_t { delete internalSocket; delete socketObj; + } return result; } + +void pal_plat_connectionStatusCallback(void *interfaceIndex, nsapi_event_t status, intptr_t param) +{ + uint32_t index = (uint32_t)interfaceIndex; + if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) + { + switch(param) + { + case NSAPI_STATUS_GLOBAL_UP: + if (s_pal_networkInterfacesSupported[index].connectionStatusCb) + { + s_pal_networkInterfacesSupported[index].connectionStatusCb(PAL_NETWORK_STATUS_CONNECTED, + s_pal_networkInterfacesSupported[index].clientArg); + } + break; + + case NSAPI_STATUS_DISCONNECTED: + if (s_pal_networkInterfacesSupported[index].connectionStatusCb) + { + s_pal_networkInterfacesSupported[index].connectionStatusCb(PAL_NETWORK_STATUS_DISCONNECTED, + s_pal_networkInterfacesSupported[index].clientArg); + } + break; + + default: + PAL_LOG_DBG("pal_plat_connectionStatusCallback - ignoring event %d", param); + break; + } + } +} diff --git a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Storage/Flash/pal_plat_internalFlash.cpp b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Storage/Flash/pal_plat_internalFlash.cpp index bfed86743..b73e8d03c 100644 --- a/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Storage/Flash/pal_plat_internalFlash.cpp +++ b/mbed-client-pal/Source/Port/Reference-Impl/OS_Specific/mbedOS/Storage/Flash/pal_plat_internalFlash.cpp @@ -19,6 +19,7 @@ #include "pal.h" #include "pal_plat_internalFlash.h" +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #if ((!defined(PAL_SIMULATOR_FLASH_OVER_FILE_SYSTEM)) || (PAL_SIMULATOR_FLASH_OVER_FILE_SYSTEM == 0)) ////////////////////////////PRIVATE/////////////////////////////////// @@ -113,3 +114,4 @@ PAL_PRIVATE palStatus_t pal_platFlashErrorTranslation(int32_t status) } #endif +#endif // #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT diff --git a/mbed-client-pal/Test/Makefile b/mbed-client-pal/Test/Makefile index cddf5f2ee..a74b139e8 100644 --- a/mbed-client-pal/Test/Makefile +++ b/mbed-client-pal/Test/Makefile @@ -15,6 +15,7 @@ LOCAL_API_DEPENDS := \ ${API_PLATFORM_DEPENDS} \ LOCAL_ADD_INCLUDE := \ + ${PAL_BASE}/Source \ ${PAL_BASE}/Source/PAL-Impl \ ${PAL_BASE}/Source/PAL-Impl/Services-API \ ${PAL_BASE}/Source/Port/Platform-API \ diff --git a/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test.c b/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test.c index d2951cfbd..9721da864 100755 --- a/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test.c +++ b/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test.c @@ -17,14 +17,10 @@ // ---------------------------------------------------------------------------- #include "pal.h" -#include "pal_Crypto.h" #include "unity.h" #include "unity_fixture.h" #include "pal_crypto_test_data.h" #include "ssl.h" -#if !PAL_USE_HW_TRNG -#include "pal_plat_entropy.h" -#endif #include #include #ifdef MBED_CONF_MBED_CLOUD_CLIENT_PSA_SUPPORT diff --git a/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test_data.h b/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test_data.h index 87d2741d9..716a0531b 100755 --- a/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test_data.h +++ b/mbed-client-pal/Test/PAL_Modules/Crypto/pal_crypto_test_data.h @@ -614,7 +614,7 @@ typedef struct palCheckEcKeyTestVector static const palCheckEcKeyTestVector_t check_ec_key_data[3] = { { - { 0x00 }, 0, PAL_ECP_DP_SECP256R1, PAL_CHECK_PRIVATE_KEY, PAL_ERR_PK_KEY_INVALID_FORMAT, PAL_ERR_PARSING_PRIVATE_KEY, false, PAL_ERR_PARSING_PUBLIC_KEY, PAL_ERR_PARSING_PUBLIC_KEY + { 0x00 }, 0, PAL_ECP_DP_SECP256R1, PAL_CHECK_PRIVATE_KEY, PAL_ERR_PK_KEY_INVALID_FORMAT, PAL_ERR_PARSING_PRIVATE_KEY, false, PAL_ERR_PK_KEY_INVALID_FORMAT, PAL_ERR_PARSING_PUBLIC_KEY }, { { diff --git a/mbed-client-pal/Test/PAL_Modules/DRBG/pal_drbg_test.c b/mbed-client-pal/Test/PAL_Modules/DRBG/pal_drbg_test.c index 12cb8069a..0ba7cdcb1 100755 --- a/mbed-client-pal/Test/PAL_Modules/DRBG/pal_drbg_test.c +++ b/mbed-client-pal/Test/PAL_Modules/DRBG/pal_drbg_test.c @@ -19,8 +19,6 @@ #include "pal.h" #include "unity.h" #include "unity_fixture.h" -#include "pal_plat_drbg.h" -#include "pal_plat_drbg.h" #include "test_runners.h" #include diff --git a/mbed-client-pal/Test/PAL_Modules/Entropy/pal_entropy_test.c b/mbed-client-pal/Test/PAL_Modules/Entropy/pal_entropy_test.c index 493a9e4c7..f07a7c166 100644 --- a/mbed-client-pal/Test/PAL_Modules/Entropy/pal_entropy_test.c +++ b/mbed-client-pal/Test/PAL_Modules/Entropy/pal_entropy_test.c @@ -20,10 +20,7 @@ #include "unity.h" #include "unity_fixture.h" #include "pal_plat_entropy.h" -#include "pal_sst.h" -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT -#include "pal_sst.h" -#else +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #include "sotp.h" #endif #include diff --git a/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test.c b/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test.c index fe441720d..d087be71b 100755 --- a/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test.c +++ b/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test.c @@ -15,11 +15,10 @@ *******************************************************************************/ #include "pal.h" -#include "pal_network.h" #include "unity.h" #include "unity_fixture.h" #include "test_runners.h" - +#include "mcc_common_setup.h" #include // config file for keepalive server #include "pal_socket_test_address.h" @@ -67,7 +66,7 @@ PAL_PRIVATE palSocket_t g_testSockets[PAL_NET_TEST_SOCKETS] = {0,0,0,0}; #define PAL_NET_TEST_BUFFERED_UDP_MESSAGE_SIZE (1024 * 256) PAL_PRIVATE uint8_t *g_testRecvBuffer = NULLPTR; PAL_PRIVATE uint8_t *g_testSendBuffer = NULLPTR; - +PAL_PRIVATE bool g_interfaceConnected = false; #define PAL_NET_TEST_ECHO_TEST_SERVER_ADDRESS "echo.mbedcloudtesting.com" // the tests expect to have guaranteed forward progress, even if they fail. So the semaphore @@ -1799,3 +1798,38 @@ TEST(pal_socket, udp_echo) { echo_test(false); } + +#ifdef TARGET_LIKE_MBED +void network_status_event_cb(palNetworkStatus_t status, void *client_arg) +{ + if (status == PAL_NETWORK_STATUS_CONNECTED) { + PAL_PRINTF("Interface connected!"); + g_interfaceConnected = true; + } else { + PAL_PRINTF("Interface disconnected!"); + g_interfaceConnected = false; + } +} +#endif + +TEST(pal_socket, interfaceStatusListener) +{ +#ifdef TARGET_LIKE_MBED + palStatus_t result = PAL_SUCCESS; + + result = pal_registerNetworkInterface(g_palTestNetworkInterface, &g_interfaceCTXIndex); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, result); + + result = pal_setConnectionStatusCallback(g_interfaceCTXIndex, &network_status_event_cb, NULL); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, result); + + g_interfaceConnected = true; + mcc_platform_close_connection(); + TEST_ASSERT_EQUAL(g_interfaceConnected, false); + + mcc_platform_init_connection(); + TEST_ASSERT_EQUAL(g_interfaceConnected, true); +#else + TEST_IGNORE_MESSAGE("Currently only supported on mbed-os!"); +#endif +} diff --git a/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test_runner.c b/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test_runner.c index 46e7ef0ed..6050e34f6 100644 --- a/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test_runner.c +++ b/mbed-client-pal/Test/PAL_Modules/Networking/pal_socket_test_runner.c @@ -39,4 +39,5 @@ TEST_GROUP_RUNNER(pal_socket) RUN_TEST_CASE(pal_socket, keepaliveOff); RUN_TEST_CASE(pal_socket, tcp_echo); RUN_TEST_CASE(pal_socket, udp_echo); + RUN_TEST_CASE(pal_socket, interfaceStatusListener); } diff --git a/mbed-client-pal/Test/PAL_Modules/ROT/pal_rot_test.c b/mbed-client-pal/Test/PAL_Modules/ROT/pal_rot_test.c index b4a93fa47..337f526aa 100755 --- a/mbed-client-pal/Test/PAL_Modules/ROT/pal_rot_test.c +++ b/mbed-client-pal/Test/PAL_Modules/ROT/pal_rot_test.c @@ -20,12 +20,9 @@ #include "unity.h" #include "unity_fixture.h" #include "pal_plat_rot.h" -#include "pal_sst.h" #include #include -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT -#include "pal_sst.h" -#else +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #include "sotp.h" #endif #if (PAL_USE_HW_ROT == 1) diff --git a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test.c b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test.c index 5df14d2f1..07182dcc2 100755 --- a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test.c +++ b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test.c @@ -736,9 +736,6 @@ TEST(pal_rtos, PeriodicTimerStopUnityTest) TEST(pal_rtos, TimerStartUnityTest) { - uint32_t expectedTicks; - uint32_t ticksInFuncError; - palStatus_t status = PAL_SUCCESS; palTimerID_t timerID1 = NULLPTR; palTimerID_t timerID2 = NULLPTR; @@ -1476,3 +1473,124 @@ TEST(pal_rtos, pal_rtc) #endif } +/*! \brief Test for launching and terminating thread in row + * + * Thread is created which does nothing in its thread function but return. On + * test code side calls pal_osThreadCreateWithAlloc + pal_osThreadTerminate in row, + * without waits. This should catch the case where the shutdown OS call happens + * before the eg. the cleanup timer code in Mbed OS version gets ran. + * + * | # | Step | Expected | + * |---|------------------------------------------------|-------------| + * | 1 | Create thread that does nothing but returns | PAL_SUCCESS | + * | 2 | Terminate thread right after creation | PAL_SUCCESS | + */ +TEST(pal_rtos, Thread_launch_and_terminate_in_row) +{ + palStatus_t status; + palThreadID_t threadID = NULLPTR; + + /*#1*/ + status = pal_osThreadCreateWithAlloc(palThreadFunc7, NULL, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + /*#2*/ + status = pal_osThreadTerminate(&threadID); + TEST_ASSERT_EQUAL(PAL_SUCCESS, status); + +} + +/*! \brief Test for creating a thread, which just sleeps + * + * Sleeping thread is created and is terminated right after creation. + * This should tests the stop in waiting-state. + * + * | # | Step | Expected | + * |---|------------------------------------------------|-------------| + * | 1 | Create thread that goes sleep | PAL_SUCCESS | + * | 2 | Wait for 1000ms | - | + * | 3 | Terminate thread | PAL_SUCCESS | + */ +TEST(pal_rtos, Sleep_thread_launch_and_terminate) +{ + palStatus_t status; + palThreadID_t threadID = NULLPTR; + + /*#1*/ + status = pal_osThreadCreateWithAlloc(palThreadFunc8, NULL, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + /*#2*/ + /* Wait for 1 second to make sure thread got time to start and run */ + pal_osDelay(1000); + + /*#3*/ + status = pal_osThreadTerminate(&threadID); + TEST_ASSERT_EQUAL(PAL_SUCCESS, status); +} + +/*! \brief Test for creating a thread, which has only infinite loop + * + * | # | Step | Expected | + * |---|------------------------------------------------|-------------| + * | 1 | Create thread that loops infinitely | PAL_SUCCESS | + * | 2 | Wait for 1000ms | - | + * | 3 | Terminate thread | PAL_SUCCESS | + */ +TEST(pal_rtos, Loop_thread_launch_and_terminate) +{ + palStatus_t status; + palThreadID_t threadID = NULLPTR; + + /*#1*/ + status = pal_osThreadCreateWithAlloc(palThreadFunc9, NULL, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + /*#2*/ + /* Wait for 1 second to make sure thread got time to start and run */ + pal_osDelay(1000); + + /*#3*/ + status = pal_osThreadTerminate(&threadID); + TEST_ASSERT_EQUAL(PAL_SUCCESS, status); +} + +/*! \brief Test for creating a thread, which just sleeps + * + * Sleeping thread is created and is terminated right after creation. + * This should tests the stop in waiting-state. + * + * | # | Step | Expected | + * |---|------------------------------------------------|-------------| + * | 1 | Create semaphore | PAL_SUCCESS | + * | 2 | Create thread that waits for semaphore | PAL_SUCCESS | + * | 3 | Wait for 1000ms | - | + * | 4 | Terminate thread | PAL_SUCCESS | + * | 5 | Delete semaphore | PAL_SUCCESS | + */ +TEST(pal_rtos, Semaphore_wait_thread_launch_and_terminate) +{ + palStatus_t status; + palThreadID_t threadID = NULLPTR; + + /*#1*/ + status = pal_osSemaphoreCreate(0, &semaphore1); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + /*#2*/ + status = pal_osThreadCreateWithAlloc(palThreadFunc10, (void *)semaphore1, PAL_osPriorityNormal, PAL_TEST_THREAD_STACK_SIZE, NULL, &threadID); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + /*#3*/ + /* Wait for 1 second to make sure thread got time to start and run */ + pal_osDelay(1000); + + /*#4*/ + status = pal_osThreadTerminate(&threadID); + TEST_ASSERT_EQUAL(PAL_SUCCESS, status); + + /*#5*/ + status = pal_osSemaphoreDelete(&semaphore1); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + TEST_ASSERT_EQUAL(NULLPTR, semaphore1); +} diff --git a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_runner.c b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_runner.c index ab74d9ab4..80b91e114 100755 --- a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_runner.c +++ b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_runner.c @@ -46,4 +46,8 @@ TEST_GROUP_RUNNER(pal_rtos) RUN_TEST_CASE(pal_rtos, pal_init_test); RUN_TEST_CASE(pal_rtos, Recursive_Mutex_Test); RUN_TEST_CASE(pal_rtos, pal_rtc); + RUN_TEST_CASE(pal_rtos, Thread_launch_and_terminate_in_row); + RUN_TEST_CASE(pal_rtos, Sleep_thread_launch_and_terminate); + RUN_TEST_CASE(pal_rtos, Loop_thread_launch_and_terminate); + RUN_TEST_CASE(pal_rtos, Semaphore_wait_thread_launch_and_terminate); } diff --git a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.c b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.c index ba846444f..efde4dca8 100755 --- a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.c +++ b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.c @@ -187,6 +187,40 @@ void palThreadFunc6(void const *argument) PAL_PRINTF("palThreadFunc6::STAAAAM\n"); } +void palThreadFunc7(void const *argument) +{ + (void) argument; + + /* Just do nothing here.. */ +} + +void palThreadFunc8(void const *argument) +{ + (void) argument; + + /* Sleep when thread is terminated */ + pal_osDelay(PAL_RTOS_WAIT_FOREVER); + +} + +void palThreadFunc9(void const *argument) +{ + (void) argument; + + /* Do the while loop*/ + while(1) {} + +} + +void palThreadFunc10(void const *argument) +{ + palSemaphoreID_t semaphore = (palSemaphoreID_t) argument; + + /* Wait for semaphore - should not never return before thread is terminated*/ + pal_osSemaphoreWait(semaphore, PAL_RTOS_WAIT_FOREVER, NULL); + + TEST_FAIL_MESSAGE("Semaphore released before thread terminated"); +} void palTimerFunc1(void const *argument) { diff --git a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.h b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.h index 83296f5f0..65e3319e2 100755 --- a/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.h +++ b/mbed-client-pal/Test/PAL_Modules/RTOS/pal_rtos_test_utils.h @@ -57,6 +57,10 @@ void palThreadFunc3(void const *argument); void palThreadFunc4(void const *argument); void palThreadFunc5(void const *argument); void palThreadFunc6(void const *argument); +void palThreadFunc7(void const *argument); +void palThreadFunc8(void const *argument); +void palThreadFunc9(void const *argument); +void palThreadFunc10(void const *argument); typedef struct timerArgument{ diff --git a/mbed-client-pal/Test/PAL_Modules/SST/pal_sst_tests.c b/mbed-client-pal/Test/PAL_Modules/SST/pal_sst_tests.c index 923142971..783be7382 100644 --- a/mbed-client-pal/Test/PAL_Modules/SST/pal_sst_tests.c +++ b/mbed-client-pal/Test/PAL_Modules/SST/pal_sst_tests.c @@ -22,7 +22,6 @@ #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #include "kvstore_global_api.h" -#include "pal_sst.h" #define TEST_PAL_SST_MAX_ITEM_NAME_SIZE 120 #define TEST_PAL_SST_MAX_INPUT_DATA_SIZE 10 diff --git a/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_test.c b/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_test.c index 035d71dc9..636bd5194 100755 --- a/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_test.c +++ b/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_test.c @@ -18,21 +18,24 @@ #include "unity_fixture.h" #include "pal.h" #include "pal_tls_utils.h" -#include "pal_network.h" #include "storage.h" #include "test_runners.h" -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT -#include "pal_sst.h" -#else +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #include "sotp.h" #endif +#if !PAL_USE_HW_TRNG +#include "pal_plat_entropy.h" +#endif + #include #define TRACE_GROUP "PAL" +#if (PAL_ENABLE_PSK == 1) #define PAL_TEST_PSK_IDENTITY "Client_identity" - #define PAL_TEST_PSK {0x12,0x34,0x45,0x67,0x89,0x10} +#endif + #define PAL_WAIT_TIME 3 #define HOSTNAME_STR_MAX_LEN 256 @@ -102,6 +105,7 @@ PAL_PRIVATE void socketCallback1( void * arg) } static void setCredentials(palTLSConfHandle_t handle); +static void handshakeTCP(void); //! This structre is for tests only and MUST be the same structure as in the pal_TLS.c file //! For any change done in the original structure, please make sure to change this structure too. @@ -130,7 +134,10 @@ TEST_SETUP(pal_tls) status = pal_init(); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT + sotp_reset(); +#endif + #if !PAL_USE_HW_TRNG // If no hardware trng - entropy must be injected for random to work uint8_t entropy_buf[48] = { 0 }; @@ -138,21 +145,16 @@ TEST_SETUP(pal_tls) TEST_ASSERT(status == PAL_SUCCESS || status == PAL_ERR_ENTROPY_EXISTS); #endif +#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT // Reset storage before pal_initTime since there might be CMAC lefovers // in internal flash which might fail storage access in pal_initTime pal_SSTReset(); -#else - sotp_reset(); #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT // Initialize the time module, as this test uses time functionality status = pal_initTime(); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - // Initialize the time module - status = pal_initTime(); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - if (g_palTestTLSInterfaceCTX == NULL) { PAL_LOG_ERR("error: net interface not configutred correctly"); @@ -167,7 +169,6 @@ TEST_SETUP(pal_tls) status = pal_osSetTime(currentTime); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - } TEST_TEAR_DOWN(pal_tls) @@ -201,7 +202,7 @@ TEST(pal_tls, tlsConfiguration) { palStatus_t status = PAL_SUCCESS; palTLSConfHandle_t palTLSConf = NULLPTR; - palTLSTransportMode_t transportationMode = PAL_TLS_MODE; + palTLSTransportMode_t transportationMode = PAL_TLS_MODE; /*#1*/ status = pal_initTLSConfiguration(&palTLSConf, transportationMode); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -325,13 +326,18 @@ TEST(pal_tls, tlsInitTLS) * | # | Step | Expected | * |---|--------------------------------|-------------| * | 1 | Initialize TLS configuration using `pal_initTLSConfiguration`. | PAL_SUCCESS | -* | 2 | Add keys to the configuration using `pal_setOwnCertAndPrivateKey`. | PAL_SUCCESS | -* | 3 | Initialize TLS context using `pal_initTLS`. | PAL_SUCCESS | -* | 4 | Uninitialize TLS context using `pal_freeTLS`. | PAL_SUCCESS | -* | 5 | Uninitialize TLS configuration using `pal_tlsConfigurationFree`. | PAL_SUCCESS | +* | 2 | Add keys to the configuration using `pal_setOwnCertChain`. | PAL_SUCCESS | +* | 3 | Add keys to the configuration using `pal_setOwnPrivateKey`. | PAL_SUCCESS | +* | 4 | Initialize TLS context using `pal_initTLS`. | PAL_SUCCESS | +* | 5 | Uninitialize TLS context using `pal_freeTLS`. | PAL_SUCCESS | +* | 6 | Uninitialize TLS configuration using `pal_tlsConfigurationFree`. | PAL_SUCCESS | */ TEST(pal_tls, tlsPrivateAndPublicKeys) { + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if (PAL_ENABLE_X509 == 1) palStatus_t status = PAL_SUCCESS; palTLSConfHandle_t palTLSConf = NULLPTR; @@ -345,15 +351,18 @@ TEST(pal_tls, tlsPrivateAndPublicKeys) TEST_ASSERT_NOT_EQUAL(palTLSConf, NULLPTR); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); /*#2*/ - status = pal_setOwnCertAndPrivateKey(palTLSConf, &pubKey, &prvKey); + status = pal_setOwnCertChain(palTLSConf, &pubKey); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); /*#3*/ - status = pal_initTLS(palTLSConf, &palTLSHandle); + status = pal_setOwnPrivateKey(palTLSConf, &prvKey); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); /*#4*/ - status = pal_freeTLS(&palTLSHandle); + status = pal_initTLS(palTLSConf, &palTLSHandle); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); /*#5*/ + status = pal_freeTLS(&palTLSHandle); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + /*#6*/ status = pal_tlsConfigurationFree(&palTLSConf); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); #else @@ -426,9 +435,10 @@ TEST(pal_tls, tlsCACertandPSK) */ TEST(pal_tls, tlsHandshakeTCP) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } palStatus_t status = PAL_SUCCESS; palTLSConfHandle_t palTLSConf = NULLPTR; @@ -458,12 +468,9 @@ TEST(pal_tls, tlsHandshakeTCP) status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -593,9 +600,10 @@ TEST(pal_tls, tlsHandshakeTCP) */ TEST(pal_tls, tlsHandshakeUDP) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } palStatus_t status = PAL_SUCCESS; palTLSConfHandle_t palTLSConf = NULLPTR; @@ -626,12 +634,9 @@ TEST(pal_tls, tlsHandshakeUDP) parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_UDP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -734,9 +739,10 @@ TEST(pal_tls, tlsHandshakeUDP) */ TEST(pal_tls, tlsHandshakeUDPTimeOut) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } palStatus_t status = PAL_SUCCESS; palTLSConfHandle_t palTLSConf = NULLPTR; @@ -765,12 +771,9 @@ TEST(pal_tls, tlsHandshakeUDPTimeOut) /*#2*/ parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_UDP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -862,9 +865,10 @@ TEST(pal_tls, tlsHandshakeUDPTimeOut) */ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -896,12 +900,9 @@ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M) /*#2*/ parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1037,9 +1038,10 @@ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M) */ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M_NoTimeUpdate) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -1061,10 +1063,8 @@ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M_NoTimeUpdate) parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); - //save valid time since the storage was cleared during TEST_SETUP - uint64_t valid_time = PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100; - status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&valid_time, sizeof(uint64_t), false); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + //get and save valid time since the storage was cleared during TEST_SETUP + handshakeTCP(); int32_t temp; status = pal_osSemaphoreCreate(1, &s_semaphoreID); @@ -1088,12 +1088,9 @@ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M_NoTimeUpdate) /*#4*/ status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1232,9 +1229,10 @@ TEST(pal_tls, tlsHandshakeTCP_FutureLWM2M_NoTimeUpdate) */ TEST(pal_tls, tlsHandshakeTCP_ExpiredLWM2MCert) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -1254,6 +1252,9 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredLWM2MCert) status = pal_osSemaphoreCreate(1, &s_semaphoreID); TEST_ASSERT_EQUAL_HEX( PAL_SUCCESS, status); + //get and save valid time since the storage was cleared during TEST_SETUP + handshakeTCP(); + /*#1*/ status = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, true, 0, socketCallback1, &g_socket); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1262,12 +1263,9 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredLWM2MCert) parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1374,7 +1372,7 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredLWM2MCert) /*#15*/ status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)¤tTime, sizeof(currentTime), &actualSavedTimeSize); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - TEST_ASSERT_TRUE(futureTime <= currentTime); + TEST_ASSERT_TRUE(futureTime > currentTime); #else TEST_IGNORE_MESSAGE("Ignored, PAL_USE_SECURE_TIME or PAL_USE_INTERNAL_FLASH not set"); #endif @@ -1409,9 +1407,10 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredLWM2MCert) */ TEST(pal_tls, tlsHandshakeTCP_ExpiredServerCert_Trusted) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -1442,12 +1441,9 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredServerCert_Trusted) /*#2*/ parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1670,9 +1666,10 @@ TEST(pal_tls, tlsHandshakeTCP_ExpiredServerCert_Trusted) */ TEST(pal_tls, tlsHandshakeTCP_FutureTrustedServer_NoTimeUpdate) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -1696,10 +1693,8 @@ TEST(pal_tls, tlsHandshakeTCP_FutureTrustedServer_NoTimeUpdate) status = pal_osSemaphoreCreate(1, &s_semaphoreID); TEST_ASSERT_EQUAL_HEX( PAL_SUCCESS, status); - //save valid time since the storage was cleared during TEST_SETUP - uint64_t valid_time = PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100; - status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&valid_time, sizeof(uint64_t), false); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + // Get valid time + handshakeTCP(); /*#1*/ status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)¤tTime, sizeof(currentTime), &actualSavedTimeSize); @@ -1716,12 +1711,9 @@ TEST(pal_tls, tlsHandshakeTCP_FutureTrustedServer_NoTimeUpdate) /*#3*/ parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -1931,9 +1923,10 @@ TEST(pal_tls, tlsHandshakeTCP_FutureTrustedServer_NoTimeUpdate) */ TEST(pal_tls, tlsHandshakeTCP_NearPastTrustedServer_NoTimeUpdate) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_USE_INTERNAL_FLASH == 1)) palStatus_t status = PAL_SUCCESS; @@ -1959,10 +1952,8 @@ TEST(pal_tls, tlsHandshakeTCP_NearPastTrustedServer_NoTimeUpdate) status = pal_osSemaphoreCreate(1, &s_semaphoreID); TEST_ASSERT_EQUAL_HEX( PAL_SUCCESS, status); - //save valid time since the storage was cleared during TEST_SETUP - uint64_t valid_time = PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100; - status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&valid_time, sizeof(uint64_t), false); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + //Get valid time since the storage was cleared during TEST_SETUP + handshakeTCP(); /*#1*/ status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)¤tTime, sizeof(currentTime), &actualSavedTimeSize); @@ -1978,12 +1969,9 @@ TEST(pal_tls, tlsHandshakeTCP_NearPastTrustedServer_NoTimeUpdate) /*#3*/ status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - status = pal_close(&g_socket); - TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); @@ -2165,6 +2153,93 @@ TEST(pal_tls, tlsHandshakeTCP_NearPastTrustedServer_NoTimeUpdate) #endif } +static void handshakeTCP(void) +{ + palStatus_t status = PAL_SUCCESS; + palTLSConfHandle_t palTLSConf = NULLPTR; + palTLSHandle_t palTLSHandle = NULLPTR; + palTLSTransportMode_t transportationMode = PAL_TLS_MODE; + palSocketAddress_t socketAddr = {0}; + palSocketLength_t addressLength = 0; + #if (PAL_ENABLE_PSK == 1) + const char* identity = PAL_TEST_PSK_IDENTITY; + const char psk[]= PAL_TEST_PSK; + #endif + palTLSSocket_t tlsSocket = { g_socket, &socketAddr, 0, transportationMode }; + struct server_address server; + + status = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, true, 0, socketCallback1, &g_socket); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); + + status = doDnsQuery(server.hostname, &socketAddr, &addressLength); + + if (PAL_SUCCESS != status) + { + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); + } + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + tlsSocket.addressLength = addressLength; + tlsSocket.socket = g_socket; + + status = pal_setSockAddrPort(&socketAddr, server.port); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + int32_t temp; + status = pal_osSemaphoreCreate(1, &s_semaphoreID); + TEST_ASSERT_EQUAL_HEX( PAL_SUCCESS, status); + + do { + status = pal_connect(g_socket, &socketAddr, addressLength); + pal_osSemaphoreWait(s_semaphoreID, 100, &temp); + } while (status == PAL_ERR_SOCKET_IN_PROGRES || status == PAL_ERR_SOCKET_WOULD_BLOCK); + + if (status == PAL_ERR_SOCKET_ALREADY_CONNECTED) { + status = PAL_SUCCESS; + } + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + status = pal_initTLSConfiguration(&palTLSConf, transportationMode); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + TEST_ASSERT_NOT_EQUAL(palTLSConf, NULLPTR); + + status = pal_initTLS(palTLSConf, &palTLSHandle); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + // This code commented out to prevent massive prints from mbedTLS, if you want to see logs from client side, just uncomment them. + //status = pal_sslSetDebugging(palTLSConf, true); + //TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + #if (PAL_ENABLE_X509 == 1) + setCredentials(palTLSConf); + #elif (PAL_ENABLE_PSK == 1) + status = pal_setPSK(palTLSConf, (const unsigned char*)identity, strlen(identity), (const unsigned char*)psk, sizeof(psk)); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + #endif + + status = pal_tlsSetSocket(palTLSConf, &tlsSocket); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + do + { + status = pal_handShake(palTLSHandle, palTLSConf); + pal_osSemaphoreWait(s_semaphoreID, 1000, &temp); + } + while ((PAL_ERR_TLS_WANT_READ == status || PAL_ERR_TLS_WANT_WRITE == status)); + + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + status = pal_freeTLS(&palTLSHandle); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + status = pal_tlsConfigurationFree(&palTLSConf); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); + + status = pal_close(&g_socket); + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); +} + // Introduce helper functions to be used in TCPHandshakeWhileCertVerify_threads test. // The test is only ran if PAL_USE_SECURE_TIME and PAL_ENABLE_X509 are set so helper // functions can also be under those checks @@ -2213,10 +2288,14 @@ static palStatus_t ThreadHandshakeTCP() /*#4*/ do { - status = pal_connect(g_socket, &socketAddr, addressLength); + status = pal_connect(socketTCP, &socketAddr, addressLength); pal_osSemaphoreWait(s_semaphoreID, 1000, &temp); } while (status == PAL_ERR_SOCKET_IN_PROGRES || status == PAL_ERR_SOCKET_WOULD_BLOCK); + if (status == PAL_ERR_SOCKET_ALREADY_CONNECTED) { + status = PAL_SUCCESS; + } + PAL_TLS_INT32_CHECK_NOT_EQUAL_GOTO_FINISH(PAL_SUCCESS, status); /*#5*/ @@ -2451,9 +2530,10 @@ static void runTLSThreadTest(palThreadFuncPtr func1, palThreadFuncPtr func2, pal */ TEST(pal_tls, TCPHandshakeWhileCertVerify_threads) { -#if defined (__LINUX__) || defined(__FREERTOS__) - TEST_IGNORE_MESSAGE("Ignored, Linux PAL tests don't get credentials from mbed_cloud_dev_credentials.c"); -#endif + if (MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY == NULL || + MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE == NULL || MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE == NULL) { + TEST_IGNORE_MESSAGE("Ignored, no credentials from mbed_cloud_dev_credentials.c"); + } #if ((PAL_USE_SECURE_TIME == 1) && (PAL_ENABLE_X509 == 1)) palStatus_t status = PAL_SUCCESS; @@ -2466,11 +2546,11 @@ TEST(pal_tls, TCPHandshakeWhileCertVerify_threads) parseServerAddress(&server, PAL_TLS_TEST_SERVER_ADDRESS_TCP); status = doDnsQuery(server.hostname, &socketAddr, &addressLength); - if ((PAL_ERR_SOCKET_DNS_ERROR == status) || (PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY == status)) + if (PAL_SUCCESS != status) { - PAL_LOG_ERR("error: address lookup returned an address not supported by current configuration cant continue test ( IPv6 add for IPv4 only configuration or IPv4 for IPv6 only configuration or error)"); - return; + PAL_LOG_ERR("DNS query error for %s", PAL_TLS_TEST_SERVER_ADDRESS_TCP); } + TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); status = pal_osSetTime(0); TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); diff --git a/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_utils.h b/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_utils.h index dea2f4713..0eabcce16 100644 --- a/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_utils.h +++ b/mbed-client-pal/Test/PAL_Modules/TLS/pal_tls_utils.h @@ -17,25 +17,34 @@ #ifndef TEST_TLS_PAL_TEST_UTILS_H_ #define TEST_TLS_PAL_TEST_UTILS_H_ -#include "pal_network.h" -#include "pal_TLS.h" - #define DTLS_SERVER_PORT_TIMEOUT 9 //Discard protocol -// This is a workaround for Linux PAL test compilation as credentials -// from mbed_cloud_dev_credentials.c are not included in the build. -#if defined (__LINUX__) || defined(__FREERTOS__) -#define PAL_TLS_TEST_SERVER_ADDRESS "127.0.0.1" +/* Workaround for Linux and Freertos builds. Cloud credentials must be defined in build + * before running tests. Now just defined as NULL to make build work. +*/ +#if defined (__LINUX__) || defined(__FREERTOS__) + +#if defined (__CC_ARM) /* ARM compiler. */ + #warning("You must define mbed cloud credentials before running TLS tests") +#else + #pragma message ("You must define mbed cloud credentials before running TLS tests") #endif -#ifndef PAL_TLS_TEST_SERVER_ADDRESS -// Use bootstrap server as test server by default -// Expect mbed_cloud_dev_credentials.c to be compiled +/* Defined as NULL */ +#define MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI NULL +#define MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY NULL +#define MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE NULL +#define MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE NULL + +#else + extern const char MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI[]; extern const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY[]; extern const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE[]; extern const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE[]; +#endif /* defined (__LINUX__) || defined(__FREERTOS__) */ + #define PAL_TLS_TEST_SERVER_ADDRESS_UDP MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI #define PAL_TLS_TEST_SERVER_ADDRESS_TCP MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI #define PAL_TLS_TEST_DEVICE_PRIVATE_KEY MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY @@ -59,111 +68,13 @@ const unsigned char coap_ping_response[] = { #define PAL_TLS_RESPONSE_MESSAGE coap_ping_response #define PAL_TLS_RESPONSE_SIZE sizeof(coap_ping_response) -#else // #ifndef PAL_TLS_TEST_SERVER_ADDRESS - -// Expect that pal test servers are in use -//ECC Key -const unsigned char g_pubKey[] = -{ - 0x30, 0x82, 0x01, 0x99, 0x30, 0x82, 0x01, 0x3d, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, - 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x05, 0x00, 0x30, 0x29, - 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x03, 0x50, 0x41, 0x4c, 0x31, 0x0c, - 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x03, 0x61, 0x72, 0x6d, 0x31, 0x0b, 0x30, 0x09, - 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x4c, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x33, 0x30, - 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x32, 0x30, 0x31, 0x32, - 0x33, 0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30, 0x30, 0x31, 0x13, 0x30, 0x11, 0x06, - 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x50, 0x41, 0x4c, 0x5f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, - 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x03, 0x61, 0x72, 0x6d, 0x31, 0x0b, - 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x4c, 0x30, 0x59, 0x30, 0x13, 0x06, - 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, - 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x18, 0x8f, 0xc6, 0x40, 0xf3, 0xf4, 0xbc, 0xd9, 0xed, 0x72, - 0x7e, 0x96, 0x8f, 0xcc, 0x56, 0x31, 0x6d, 0x72, 0x1d, 0x4e, 0xd8, 0xb2, 0xc1, 0x5b, 0xcb, 0xcf, - 0xd6, 0xc5, 0xdb, 0x1f, 0xdf, 0x99, 0x21, 0x01, 0xbc, 0x0c, 0x03, 0xb8, 0xa9, 0xbd, 0xd7, 0xb7, - 0x4f, 0x06, 0x8c, 0x5f, 0x6e, 0xe8, 0x64, 0xc9, 0x69, 0x46, 0x96, 0xf8, 0xfd, 0x14, 0xe5, 0xe3, - 0x5b, 0x8c, 0x92, 0x99, 0x04, 0xe6, 0xa3, 0x4d, 0x30, 0x4b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, - 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, - 0xb8, 0x4f, 0xcc, 0xad, 0x6b, 0xad, 0xa1, 0x31, 0x75, 0x74, 0x57, 0xe3, 0x76, 0x9f, 0x5d, 0xc3, - 0x4f, 0xcf, 0x20, 0x53, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, - 0x14, 0xfe, 0x53, 0x4c, 0xfa, 0x9a, 0xc7, 0x2f, 0x0c, 0x72, 0xbf, 0xaa, 0x47, 0xc5, 0x16, 0x92, - 0x67, 0x7f, 0x0e, 0x02, 0xef, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, - 0x02, 0x05, 0x00, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0x97, 0x90, 0xa3, 0x5d, 0xab, - 0xee, 0x4f, 0x56, 0x12, 0x15, 0x0f, 0xbe, 0x3a, 0x39, 0x21, 0x54, 0x44, 0x67, 0x48, 0xf8, 0xb4, - 0x83, 0xbe, 0xda, 0x66, 0x62, 0xa0, 0xa5, 0x67, 0x1c, 0xf4, 0xba, 0x02, 0x20, 0x22, 0x41, 0x3f, - 0x52, 0x3a, 0xaa, 0x9d, 0x3c, 0xf1, 0x99, 0x7d, 0x2c, 0x04, 0x89, 0xff, 0xce, 0x80, 0x56, 0xd1, - 0x9c, 0xd9, 0x19, 0x06, 0xd3, 0xee, 0x41, 0x2b, 0x49, 0x76, 0x27, 0xfe, 0x4e -}; - -const uint8_t g_prvKey[] = -{ - 0x30, 0x78, 0x02, 0x01, 0x01, 0x04, 0x21, 0x00, 0xac, 0xfb, 0x43, 0x6f, 0xa1, 0x3c, 0x2d, 0x1a, - 0x91, 0xd7, 0x58, 0xf8, 0x1f, 0x0f, 0xc5, 0x00, 0x11, 0xe1, 0x56, 0xe6, 0xed, 0x89, 0x96, 0x8b, - 0x12, 0xe6, 0x32, 0x70, 0x82, 0x06, 0x01, 0xed, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, - 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x18, 0x8f, 0xc6, 0x40, 0xf3, 0xf4, - 0xbc, 0xd9, 0xed, 0x72, 0x7e, 0x96, 0x8f, 0xcc, 0x56, 0x31, 0x6d, 0x72, 0x1d, 0x4e, 0xd8, 0xb2, - 0xc1, 0x5b, 0xcb, 0xcf, 0xd6, 0xc5, 0xdb, 0x1f, 0xdf, 0x99, 0x21, 0x01, 0xbc, 0x0c, 0x03, 0xb8, - 0xa9, 0xbd, 0xd7, 0xb7, 0x4f, 0x06, 0x8c, 0x5f, 0x6e, 0xe8, 0x64, 0xc9, 0x69, 0x46, 0x96, 0xf8, - 0xfd, 0x14, 0xe5, 0xe3, 0x5b, 0x8c, 0x92, 0x99, 0x04, 0xe6 -}; - - -const char pal_test_cas[] = -{ - 0x30, 0x82, 0x01, 0x98, 0x30, 0x82, 0x01, 0x3c, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, - 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x05, 0x00, 0x30, 0x29, - 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x03, 0x50, 0x41, 0x4c, 0x31, 0x0c, - 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x03, 0x61, 0x72, 0x6d, 0x31, 0x0b, 0x30, 0x09, - 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x4c, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x33, 0x30, - 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x32, 0x30, 0x31, 0x32, - 0x33, 0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30, 0x29, 0x31, 0x0c, 0x30, 0x0a, 0x06, - 0x03, 0x55, 0x04, 0x03, 0x13, 0x03, 0x50, 0x41, 0x4c, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, - 0x04, 0x0a, 0x13, 0x03, 0x61, 0x72, 0x6d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, - 0x13, 0x02, 0x49, 0x4c, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, - 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xa4, - 0x26, 0x26, 0x29, 0x98, 0x43, 0x6c, 0xc2, 0x2c, 0xbe, 0x0a, 0x90, 0xe3, 0x78, 0x44, 0xa9, 0x15, - 0x0b, 0x02, 0x26, 0xf6, 0x14, 0xda, 0x36, 0x5b, 0x0f, 0x1d, 0x15, 0xb1, 0x56, 0x76, 0xec, 0x7f, - 0x59, 0x6b, 0x9d, 0x69, 0xd9, 0xd8, 0x16, 0x22, 0xf1, 0x90, 0x63, 0xa1, 0xd3, 0xb7, 0xd7, 0x32, - 0x31, 0x8d, 0xc7, 0xbe, 0x8f, 0xc8, 0x20, 0x5a, 0x9d, 0xfa, 0x03, 0x79, 0x7a, 0xcb, 0x7e, 0xa3, - 0x53, 0x30, 0x51, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, - 0xff, 0x02, 0x01, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xfe, - 0x53, 0x4c, 0xfa, 0x9a, 0xc7, 0x2f, 0x0c, 0x72, 0xbf, 0xaa, 0x47, 0xc5, 0x16, 0x92, 0x67, 0x7f, - 0x0e, 0x02, 0xef, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, - 0xfe, 0x53, 0x4c, 0xfa, 0x9a, 0xc7, 0x2f, 0x0c, 0x72, 0xbf, 0xaa, 0x47, 0xc5, 0x16, 0x92, 0x67, - 0x7f, 0x0e, 0x02, 0xef, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, - 0x05, 0x00, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0x97, 0xce, 0xc5, 0x3c, 0x94, 0xb5, - 0xd4, 0x3b, 0xec, 0x92, 0xed, 0x2b, 0x96, 0x3f, 0xdc, 0xae, 0x16, 0xd2, 0x02, 0x6a, 0xa6, 0x8d, - 0xf5, 0xb1, 0x5b, 0x61, 0x95, 0xfc, 0x4d, 0x17, 0x0e, 0xab, 0x02, 0x20, 0x40, 0x7b, 0x27, 0x11, - 0x83, 0x87, 0xfe, 0x62, 0xa2, 0x67, 0x7c, 0xa8, 0x8e, 0xd5, 0xf3, 0xdc, 0x62, 0xf9, 0x35, 0x91, - 0x0b, 0x43, 0x80, 0xc9, 0x12, 0x30, 0x2e, 0x66, 0x64, 0xbf, 0xf2, 0xcb -}; - -#define PAL_TLS_TEST_DEVICE_PRIVATE_KEY g_prvKey -#define PAL_TLS_TEST_SERVER_CA pal_test_cas -#define PAL_TLS_TEST_DEVICE_CERTIFICATE g_pubKey - -// Local test server is http server in tcp cases -const unsigned char http_get_message[] = "GET / HTTP/1.0\r\n\r\n"; - -// Non-confirmable GET with payload '/helloWorld', used in udp cases -const unsigned char coap_helloworld_request[] = { - 0x50, 0x01, 0x57, 0x3e, 0xff, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64 -}; - -#define PAL_TLS_REQUEST_MESSAGE http_get_message -#define PAL_TLS_UDP_REQUEST_MESSAGE coap_helloworld_request -#undef PAL_TLS_RESPONSE_MESSAGE -#define PAL_TLS_RESPONSE_SIZE 256 - -#define PAL_TLS_TEST_SERVER_ADDRESS_UDP PAL_TLS_TEST_SERVER_ADDRESS":4422" -#define PAL_TLS_TEST_SERVER_ADDRESS_TCP PAL_TLS_TEST_SERVER_ADDRESS":5544" - -#endif // #ifndef PAL_TLS_TEST_SERVER_ADDRESS - const uint16_t MAX_CERTIFICATE_SIZE = 1024; +#if (PAL_ENABLE_PSK == 1) const unsigned char g_psk[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const unsigned char g_psk_id[] = "Client_identity"; - +#endif /* (PAL_ENABLE_PSK == 1) */ #endif /* TEST_TLS_PAL_TEST_UTILS_H_ */ diff --git a/mbed-client-pal/Test/PAL_Modules/Time/pal_time_test.c b/mbed-client-pal/Test/PAL_Modules/Time/pal_time_test.c index 1e3b5f842..e748698fc 100755 --- a/mbed-client-pal/Test/PAL_Modules/Time/pal_time_test.c +++ b/mbed-client-pal/Test/PAL_Modules/Time/pal_time_test.c @@ -21,9 +21,7 @@ #include "test_runners.h" #include #include -#ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT -#include "pal_sst.h" -#else +#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT #include "sotp.h" #endif diff --git a/mbed-client-pal/mbed_lib.json b/mbed-client-pal/mbed_lib.json index 79d45a6f3..0d0d74008 100644 --- a/mbed-client-pal/mbed_lib.json +++ b/mbed-client-pal/mbed_lib.json @@ -6,7 +6,7 @@ "pal-dns-api-version": { "help": "Version of DNS API. This requires that PAL_NET_DNS_SUPPORT is defined. Value 0 = sync DNS, 1 = async DNS API for mbed-os 5.8 or older, 2 = async for mbed-os 5.9 or newer", "macro_name": "PAL_DNS_API_VERSION", - "value" : 0 + "value" : 2 }, "pal-max-frag-len": { "help": "Maximum fragment length value for the payload in one packet, doesnt include TLS header and encryption overhead. Is needed for constrainted devices having low MTU sizes, Value 0 = disabled, 1 = MBEDTLS_SSL_MAX_FRAG_LEN_512, 2= MBEDTLS_SSL_MAX_FRAG_LEN_1024, 3 = MBEDTLS_SSL_MAX_FRAG_LEN_2048, 4 = MBEDTLS_SSL_MAX_FRAG_LEN_4096", diff --git a/mbed-client/mbed-client-c/source/include/sn_grs.h b/mbed-client/mbed-client-c/source/include/sn_grs.h index ecd48750a..24cd33c16 100755 --- a/mbed-client/mbed-client-c/source/include/sn_grs.h +++ b/mbed-client/mbed-client-c/source/include/sn_grs.h @@ -66,10 +66,17 @@ struct nsdl_s { uint32_t update_register_token; uint32_t register_token; uint32_t unregister_token; + +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE uint32_t bootstrap_token; +#endif + uint32_t token_seed; unsigned int sn_nsdl_endpoint_registered:1; + +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE unsigned int is_bs_server:1; +#endif struct grs_s *grs; sn_nsdl_ep_parameters_s *ep_information_ptr; // Endpoint parameters, Name, Domain etc.. diff --git a/mbed-client/mbed-client-c/source/sn_nsdl.c b/mbed-client/mbed-client-c/source/sn_nsdl.c index 5543b4131..b6cc89414 100755 --- a/mbed-client/mbed-client-c/source/sn_nsdl.c +++ b/mbed-client/mbed-client-c/source/sn_nsdl.c @@ -50,6 +50,10 @@ #include +#if defined MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE +#define MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE +#endif + /* Defines */ #define TRACE_GROUP "mClt" #define RESOURCE_DIR_LEN 2 @@ -63,7 +67,11 @@ #define OBS_PARAMETER_LEN 3 #define AOBS_PARAMETER_LEN 5 #define COAP_CON_PARAMETER_LEN 3 + +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE #define BS_EP_PARAMETER_LEN 3 +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + #define BS_QUEUE_MODE_PARAMETER_LEN 2 #define RESOURCE_VALUE_PARAMETER_LEN 2 #define FIRMWARE_DOWNLOAD_LEN 2 @@ -76,9 +84,7 @@ #define COAP_DISABLE_OBS_FEATURE MBED_CONF_MBED_CLIENT_COAP_DISABLE_OBS_FEATURE #endif -#if defined MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE -#define MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE -#endif + /* Constants */ static uint8_t ep_name_parameter_string[] = {'e', 'p', '='}; /* Endpoint name. A unique name for the registering node in a domain. */ @@ -101,8 +107,11 @@ static uint8_t firmware_download_uri[] = {'f', 'w'}; /* Path for firmware static uint8_t generic_download_uri[] = {'d', 'o', 'w', 'n', 'l', 'o', 'a', 'd'}; /* Path for generic download. */ /* * OMA BS parameters * */ +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE static uint8_t bs_uri[] = {'b', 's'}; static uint8_t bs_ep_name[] = {'e', 'p', '='}; +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + static uint8_t et_parameter[] = {'e', 't', '='}; /* Endpoint type */ static uint8_t bs_queue_mode[] = {'b', '='}; @@ -238,8 +247,9 @@ uint16_t sn_nsdl_register_endpoint(struct nsdl_s *handle, sn_nsdl_clear_coap_resending_queue(handle); /*** Build endpoint register message ***/ - +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE handle->is_bs_server = false; +#endif /* Allocate memory for header struct */ register_message_ptr = sn_coap_parser_alloc_message(handle->grs->coap); @@ -1696,10 +1706,12 @@ static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *co handle->ep_information_ptr->domain_name_ptr = 0; handle->ep_information_ptr->domain_name_len = 0; } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE else if (coap_packet_ptr->token_len == sizeof(handle->bootstrap_token) && memcmp(coap_packet_ptr->token_ptr, &handle->bootstrap_token, coap_packet_ptr->token_len) == 0) { is_bs_msg = true; } +#endif /* Store the current message token so that we can identify if same operation was initiated from callback */ uint32_t temp_token = 0; @@ -1712,9 +1724,11 @@ static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *co else if (is_update_reg_msg) { temp_token = handle->update_register_token; } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE else if (is_bs_msg) { temp_token = handle->bootstrap_token; } +#endif /* No messages to wait for, or message was not response to our request */ int ret = handle->sn_nsdl_rx_callback(handle, coap_packet_ptr, address_ptr); @@ -1730,9 +1744,11 @@ static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *co else if (is_update_reg_msg && temp_token == handle->update_register_token) { handle->update_register_token = 0; } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE else if (is_bs_msg && temp_token == handle->bootstrap_token) { handle->bootstrap_token = 0; } +#endif return ret; } diff --git a/mbed-client/mbed-client-classic/mbed-client-classic/m2mconnectionhandlerpimpl.h b/mbed-client/mbed-client-classic/mbed-client-classic/m2mconnectionhandlerpimpl.h index c15b988cd..2b68af0ec 100644 --- a/mbed-client/mbed-client-classic/mbed-client-classic/m2mconnectionhandlerpimpl.h +++ b/mbed-client/mbed-client-classic/mbed-client-classic/m2mconnectionhandlerpimpl.h @@ -52,6 +52,12 @@ class M2MConnectionHandlerPimpl { ESocketTimerCallback = 0x80 }; + // NOTE! Check that these values does not overlap with the SocketEvent values + enum InterfaceStatusEvent { + EInterfaceConnected = 0x81, + EInterfaceDisconnected = 0x82 + }; + /** * @brief Constructor */ @@ -228,6 +234,8 @@ class M2MConnectionHandlerPimpl { */ bool send_event(SocketEvent event_type); + void interface_event(palNetworkStatus_t status); + private: typedef struct send_data_queue { uint8_t *data; diff --git a/mbed-client/mbed-client-classic/source/m2mconnectionhandlerpimpl.cpp b/mbed-client/mbed-client-classic/source/m2mconnectionhandlerpimpl.cpp index af5c6c33c..fbcb16a5b 100644 --- a/mbed-client/mbed-client-classic/source/m2mconnectionhandlerpimpl.cpp +++ b/mbed-client/mbed-client-classic/source/m2mconnectionhandlerpimpl.cpp @@ -44,6 +44,13 @@ #error "For async PAL DNS only API v2 or greater is supported on Mbed." #endif +extern "C" void network_status_event(palNetworkStatus_t status, void *client_arg) +{ + assert(client_arg); + M2MConnectionHandlerPimpl* instance = (M2MConnectionHandlerPimpl*)client_arg; + instance->interface_event(status); +} + int8_t M2MConnectionHandlerPimpl::_tasklet_id = -1; // This is called from event loop, but as it is static C function, this is just a wrapper @@ -67,6 +74,13 @@ void M2MConnectionHandlerPimpl::event_handler(arm_event_s *event) switch (event->event_type) { // Event from socket callback method + case M2MConnectionHandlerPimpl::EInterfaceConnected: + _observer.network_interface_status_change(M2MConnectionObserver::NetworkInterfaceConnected); + break; + case M2MConnectionHandlerPimpl::EInterfaceDisconnected: + _observer.network_interface_status_change(M2MConnectionObserver::NetworkInterfaceDisconnected); + break; + case M2MConnectionHandlerPimpl::ESocketCallback: case M2MConnectionHandlerPimpl::ESocketTimerCallback: @@ -683,6 +697,11 @@ void M2MConnectionHandlerPimpl::set_platform_network_handler(void *handler) if (PAL_SUCCESS != pal_registerNetworkInterface(handler, &_net_iface)) { tr_error("M2MConnectionHandlerPimpl::set_platform_network_handler - Interface registration failed."); } + + if (PAL_SUCCESS != pal_setConnectionStatusCallback(_net_iface, network_status_event, this)) { + tr_error("M2MConnectionHandlerPimpl::set_platform_network_handler - Connection status callback set failed."); + } + tr_debug("M2MConnectionHandlerPimpl::set_platform_network_handler - index = %d", _net_iface); } @@ -972,3 +991,19 @@ void M2MConnectionHandlerPimpl::unregister_network_handler() { pal_unregisterNetworkInterface(_net_iface); } + +void M2MConnectionHandlerPimpl::interface_event(palNetworkStatus_t status) +{ + arm_event_s event = {0}; + event.receiver = M2MConnectionHandlerPimpl::_tasklet_id; + event.data_ptr = this; + event.priority = ARM_LIB_HIGH_PRIORITY_EVENT; + + if (status == PAL_NETWORK_STATUS_CONNECTED) { + event.event_type = M2MConnectionHandlerPimpl::EInterfaceConnected; + } else { + event.event_type = M2MConnectionHandlerPimpl::EInterfaceDisconnected; + } + + eventOS_event_send(&event); +} diff --git a/mbed-client/mbed-client/m2mbase.h b/mbed-client/mbed-client/m2mbase.h index 334c3fdef..757abbaac 100644 --- a/mbed-client/mbed-client/m2mbase.h +++ b/mbed-client/mbed-client/m2mbase.h @@ -49,7 +49,7 @@ class M2MEndpoint; /*! \file m2mbase.h * \brief M2MBase. - * This class is the base class based on which all LWM2M object models + * This class is the base class based on which all LwM2M object models * can be created. * * This serves as a base class for Objects, ObjectInstances and Resources. @@ -334,8 +334,8 @@ class M2MBase : public M2MReportObserver { /** * \brief Sets the object to be auto-observable. * - * \note This is not a standard CoAP or LWM2M feature and it only works in mbed Cloud. - * \note This must be called before registration process, since this info must be in a registration message. + * \note This is not a standard CoAP or LwM2M feature and it only works in Device Management. + * \note You must call this before registration process, since this info must be in a registration message. * \note Auto-observable will take higher precedence if both observable methods are set. * * \param auto_observable Is auto-obs feature enabled or not. @@ -778,7 +778,7 @@ class M2MBase : public M2MReportObserver { const uint8_t length); /** - * \brief The data has changed and it needs to be updated into Mbed Cloud. + * \brief The data has changed and it needs to be updated into Device Management. * Current implementation maintains the changed state only in M2MEndpoint. If any of the changes in an * object changes the M2M registration structure, the information is propagated to M2MEndpoint using * this interface. diff --git a/mbed-client/mbed-client/m2mconnectionobserver.h b/mbed-client/mbed-client/m2mconnectionobserver.h index a8dc555c4..cb239ce2d 100644 --- a/mbed-client/mbed-client/m2mconnectionobserver.h +++ b/mbed-client/mbed-client/m2mconnectionobserver.h @@ -37,6 +37,11 @@ public : LWM2MServer }ServerType; + typedef enum { + NetworkInterfaceConnected, + NetworkInterfaceDisconnected + }NetworkInterfaceStatus; + /** * \brief The M2MSocketAddress struct. * A unified container for holding socket address data @@ -80,6 +85,8 @@ public : * \brief Indicates that data has been sent successfully. */ virtual void data_sent() = 0; + + virtual void network_interface_status_change(NetworkInterfaceStatus status) = 0; }; #endif // M2M_CONNECTION_OBSERVER_H__ diff --git a/mbed-client/mbed-client/m2mconstants.h b/mbed-client/mbed-client/m2mconstants.h index 05994db52..777685b25 100644 --- a/mbed-client/mbed-client/m2mconstants.h +++ b/mbed-client/mbed-client/m2mconstants.h @@ -161,7 +161,6 @@ extern const String EMPTY; // Error Strings #define ERROR_REASON_1 "No security object found for Bootstrap" -#define ERROR_REASON_2 "Bootstrap not allowed for now, try later" #define ERROR_REASON_3 "Bootstrap feature is disabled" #define ERROR_REASON_4 "No security object found for Registration" #define ERROR_REASON_5 "Registration not allowed for now, try later" @@ -170,8 +169,6 @@ extern const String EMPTY; #define ERROR_REASON_8 "LWM2M server rejected client registration" #define ERROR_REASON_9 "Client in reconnection mode %s" #define ERROR_REASON_10 "Client cannot connect anymore %s" -#define ERROR_REASON_11 "Bootstrap server URL is not correctly formed" -#define ERROR_REASON_12 "Bootstrap resource is not correctly formed" #define ERROR_REASON_13 "LWM2M server URL is not correctly formed" #define ERROR_REASON_14 "LWM2M server address is not set correctly in client" #define ERROR_REASON_15 "Failed to do full registration because of missing parameters in registration" @@ -179,13 +176,18 @@ extern const String EMPTY; #define ERROR_REASON_17 "Incoming CoAP message parsing failed" #define ERROR_REASON_18 "Sending reg-update failed as lifetime is less than 60 sec" #define ERROR_REASON_19 "LWM2M server URL is not correctly formed" +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE +#define ERROR_REASON_2 "Bootstrap not allowed for now, try later" +#define ERROR_REASON_11 "Bootstrap server URL is not correctly formed" +#define ERROR_REASON_12 "Bootstrap resource is not correctly formed" #define ERROR_REASON_20 "BS PUT fails :%s" #define ERROR_REASON_21 "BS DEL fails :%s" #define ERROR_REASON_22 "BS FIN fails :%s" #define ERROR_REASON_23 "Bootstrap SecureConnection failed" +#define ERROR_REASON_26 "BS FIN fails: missing EST iep param" +#endif #define ERROR_REASON_24 "LWM2M server rejected client unregistration (not-found)" #define ERROR_REASON_25 "Failed to allocate registration message" -#define ERROR_REASON_26 "BS FIN fails: missing EST iep param" #define ERROR_REASON_27 "Call already in progress" #define ERROR_REASON_28 "Message sending timed out" #define ERROR_REASON_29 "Call not allowed for now, wrong state" diff --git a/mbed-client/mbed-client/m2mendpoint.h b/mbed-client/mbed-client/m2mendpoint.h index eff087fb3..3ea2ff2aa 100644 --- a/mbed-client/mbed-client/m2mendpoint.h +++ b/mbed-client/mbed-client/m2mendpoint.h @@ -64,12 +64,12 @@ protected : M2MEndpoint( const M2MEndpoint& /*other*/ ); /* - * \brief Data has been changed and it needs to be updated to Mbed Cloud. + * \brief Data has been changed and it needs to be updated to Device Management. */ virtual void set_changed(); /* - * \brief Clears the changed flag. This can be done when the data has been updated into Mbed Cloud. + * \brief You can clear the flag the when the data has been updated into Device Management. */ void clear_changed(); diff --git a/mbed-client/mbed-client/m2minterface.h b/mbed-client/mbed-client/m2minterface.h index 327474f76..d5bef2a25 100644 --- a/mbed-client/mbed-client/m2minterface.h +++ b/mbed-client/mbed-client/m2minterface.h @@ -142,6 +142,7 @@ class M2MInterface { virtual ~M2MInterface(){} +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Initiates bootstrapping of the client with the provided Bootstrap * Server information. @@ -163,6 +164,7 @@ class M2MInterface { * \brief Finishes bootstrap in cases where client will be the one to finish it. */ virtual void finish_bootstrap() = 0; +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Initiates the registration of a provided security object to the diff --git a/mbed-client/mbed_lib.json b/mbed-client/mbed_lib.json index 64fbaca9f..6610ae641 100644 --- a/mbed-client/mbed_lib.json +++ b/mbed-client/mbed_lib.json @@ -7,14 +7,6 @@ "tcp-keepalive-interval": 90, "disable-bootstrap-feature": null, "coap-disable-obs-feature":null, - "reconnection-loop": { - "help": "Not in used anymore", - "value": 1 - }, - "dns-use-thread": { - "help": "Not in used anymore", - "value": null - }, "dtls_peer_max_timeout": null, "tls-max-retry": null, "sn-coap-max-blockwise-payload-size" : null, diff --git a/mbed-client/source/include/m2minterfaceimpl.h b/mbed-client/source/include/m2minterfaceimpl.h index dd13c0e7c..48c634f95 100644 --- a/mbed-client/source/include/m2minterfaceimpl.h +++ b/mbed-client/source/include/m2minterfaceimpl.h @@ -85,6 +85,7 @@ friend class M2MInterfaceFactory; */ virtual ~M2MInterfaceImpl(); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Initiates bootstrapping of the client with the provided Bootstrap * server information. @@ -104,6 +105,7 @@ friend class M2MInterfaceFactory; * @brief Finishes on going bootstrap in cases where client is the one to finish it. */ virtual void finish_bootstrap(); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Initiates registration of the provided Security object to the @@ -320,6 +322,8 @@ friend class M2MInterfaceFactory; virtual void client_unregistered(); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + virtual void bootstrap_done(); virtual void bootstrap_finish(); @@ -329,6 +333,7 @@ friend class M2MInterfaceFactory; virtual void bootstrap_error_wait(const char *reason); virtual void bootstrap_error(const char *reason); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE virtual void coap_data_processed(); @@ -348,6 +353,8 @@ friend class M2MInterfaceFactory; virtual void data_sent(); + virtual void network_interface_status_change(NetworkInterfaceStatus status); + protected: // from M2MTimerObserver virtual void timer_expired(M2MTimerObserver::Type type); @@ -360,6 +367,7 @@ friend class M2MInterfaceFactory; */ void state_idle(EventData* data); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * When the client starts bootstrap. */ @@ -379,6 +387,7 @@ friend class M2MInterfaceFactory; * When the server has sent response and bootstrapping is done. */ void state_bootstrapped( EventData *data); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * When the client starts register. @@ -466,12 +475,14 @@ friend class M2MInterfaceFactory; */ enum E_States { STATE_IDLE = 0, +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE STATE_BOOTSTRAP, STATE_BOOTSTRAP_ADDRESS_RESOLVED, STATE_BOOTSTRAP_RESOURCE_CREATED, STATE_BOOTSTRAP_WAIT, STATE_BOOTSTRAP_ERROR_WAIT, // 5 STATE_BOOTSTRAPPED, +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE STATE_REGISTER, STATE_REGISTER_ADDRESS_RESOLVED, STATE_REGISTERED, diff --git a/mbed-client/source/include/m2mnsdlinterface.h b/mbed-client/source/include/m2mnsdlinterface.h index eb63a7780..bc8b0278f 100644 --- a/mbed-client/source/include/m2mnsdlinterface.h +++ b/mbed-client/source/include/m2mnsdlinterface.h @@ -145,12 +145,14 @@ class M2MNsdlInterface : public M2MTimerObserver, */ bool remove_nsdl_resource(M2MBase *base); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Creates the bootstrap object. * @param address Bootstrap address. * @return true if created and sent successfully else false. */ bool create_bootstrap_resource(sn_nsdl_addr_s *address); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Sets the register message to the server. @@ -380,18 +382,13 @@ class M2MNsdlInterface : public M2MTimerObserver, */ void send_next_notification(bool clear_token); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Store the "BS finished" response id. * @param msg_id Response id. */ void store_bs_finished_response_id(uint16_t msg_id); - /** - * @brief Store the registration state. - * @param registered Registered to lwm2m server or not. - */ - void set_registration_status(bool registered); - /** * @brief Handle incoming bootstrap PUT message. * @param coap_header, Received CoAP message @@ -403,6 +400,13 @@ class M2MNsdlInterface : public M2MTimerObserver, * @brief Handle bootstrap finish acknowledgement. */ void handle_bootstrap_finish_ack(uint16_t msg_id); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + + /** + * @brief Store the registration state. + * @param registered Registered to lwm2m server or not. + */ + void set_registration_status(bool registered); /** * @brief Returns total retransmission time @@ -549,6 +553,7 @@ class M2MNsdlInterface : public M2MTimerObserver, */ void set_endpoint_lifetime_buffer(int lifetime); +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Handle bootstrap finished message. * @param coap_header, Received CoAP message @@ -570,13 +575,6 @@ class M2MNsdlInterface : public M2MTimerObserver, */ bool parse_bootstrap_message(sn_coap_hdr_s *coap_header, M2MNsdlInterface::ObjectType lwm2m_object_type); - /** - * @brief Parse bootstrap TLV message. - * @param coap_header, Received CoAP message - * @return True if parsing was succesful else false - */ - bool validate_security_object(); - /** * @brief Handle bootstrap errors. * @param reason, Reason for Bootstrap failure. @@ -585,6 +583,16 @@ class M2MNsdlInterface : public M2MTimerObserver, */ void handle_bootstrap_error(const char *reason, bool wait); + void handle_bootstrap_response(const sn_coap_hdr_s *coap_header); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + + /** + * @brief Parse bootstrap TLV message. + * @param coap_header, Received CoAP message + * @return True if parsing was succesful else false + */ + bool validate_security_object(); + /** * @brief Handle different coap errors. * @param coap_header, CoAP structure. @@ -665,8 +673,6 @@ class M2MNsdlInterface : public M2MTimerObserver, void handle_request_response(const sn_coap_hdr_s *coap_header, struct request_context_s *request_context); - void handle_bootstrap_response(const sn_coap_hdr_s *coap_header); - void handle_message_delivered(M2MBase *base, const M2MBase::MessageType type); void handle_empty_ack(const sn_coap_hdr_s *coap_header, bool is_bootstrap_msg); diff --git a/mbed-client/source/include/m2mnsdlobserver.h b/mbed-client/source/include/m2mnsdlobserver.h index c155dc667..bb4b9377f 100644 --- a/mbed-client/source/include/m2mnsdlobserver.h +++ b/mbed-client/source/include/m2mnsdlobserver.h @@ -69,6 +69,7 @@ public : */ virtual void client_unregistered() = 0; +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Informs that client bootstrapping is done. * @param security_object, M2MSecurity Object which contains information about @@ -101,6 +102,7 @@ public : * @param reason, Error string explaining the failure reason */ virtual void bootstrap_error(const char *reason) = 0; +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * @brief Informs that received data has been processed. diff --git a/mbed-client/source/m2mbase.cpp b/mbed-client/source/m2mbase.cpp index aa518022a..de0f29c36 100644 --- a/mbed-client/source/m2mbase.cpp +++ b/mbed-client/source/m2mbase.cpp @@ -77,13 +77,12 @@ M2MBase::M2MBase(const String& resource_name, sn_nsdl_static_resource_parameters_s *params = const_cast(_sn_resource->dynamic_resource_params->static_resource_parameters); memset(params, 0, sizeof(sn_nsdl_static_resource_parameters_s)); - const size_t len = strlen(resource_type.c_str()); params->free_on_delete = true; +#ifndef DISABLE_RESOURCE_TYPE + const size_t len = strlen(resource_type.c_str()); if (len > 0) { #ifndef RESOURCE_ATTRIBUTES_LIST -#ifndef DISABLE_RESOURCE_TYPE params->resource_type_ptr = (char*)alloc_string_copy((uint8_t*) resource_type.c_str(), len); -#endif #else sn_nsdl_attribute_item_s item; item.attribute_name = ATTR_RESOURCE_TYPE; @@ -91,6 +90,7 @@ M2MBase::M2MBase(const String& resource_name, sn_nsdl_set_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, &item); #endif } +#endif // DISABLE_RESOURCE_TYPE params->path = path; params->mode = (unsigned)mode; params->external_memory_block = external_blockwise_store; diff --git a/mbed-client/source/m2minterfaceimpl.cpp b/mbed-client/source/m2minterfaceimpl.cpp index d0bfebcc1..93195d355 100644 --- a/mbed-client/source/m2minterfaceimpl.cpp +++ b/mbed-client/source/m2minterfaceimpl.cpp @@ -84,7 +84,6 @@ M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer, randLIB_seed_random(); // Range is from 2 to 10 _initial_reconnection_time = randLIB_get_random_in_range(2, 10); - tr_info("M2MInterfaceImpl::M2MInterfaceImpl() initial random time %d\n", _initial_reconnection_time); _reconnection_time = _initial_reconnection_time; @@ -119,9 +118,9 @@ M2MInterfaceImpl::~M2MInterfaceImpl() tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT"); } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::bootstrap(M2MSecurity *security) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::bootstrap - IN - current state %d", _current_state); _retry_timer.stop_timer(); _security = NULL; @@ -165,28 +164,20 @@ void M2MInterfaceImpl::bootstrap(M2MSecurity *security) } _connection_handler.release_mutex(); tr_debug("M2MInterfaceImpl::bootstrap - OUT"); -#else - set_error_description(ERROR_REASON_3); - _observer.error(M2MInterface::NotAllowed); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE - } void M2MInterfaceImpl::cancel_bootstrap() { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE //TODO: Do we need this ? -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::finish_bootstrap() { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::finish_bootstrap"); _security = NULL; bootstrap_done(); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MObjectList &object_list) { @@ -427,9 +418,9 @@ void M2MInterfaceImpl::client_unregistered() //TODO: manage register object in a list. } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::bootstrap_done() { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MInterfaceImpl::bootstrap_done"); _reconnection_time = _initial_reconnection_time; _reconnecting = false; @@ -451,12 +442,10 @@ void M2MInterfaceImpl::bootstrap_done() internal_event(STATE_BOOTSTRAPPED); _observer.bootstrap_done(_nsdl_interface.get_security_object()); } -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::bootstrap_finish() { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MInterfaceImpl::bootstrap_finish"); internal_event(STATE_BOOTSTRAP_WAIT); _observer.bootstrap_data_ready(_nsdl_interface.get_security_object()); @@ -467,29 +456,24 @@ void M2MInterfaceImpl::bootstrap_finish() // callback so we must inform observer now bootstrap_done(); } -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::bootstrap_wait() { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MInterfaceImpl::bootstrap_wait"); internal_event(STATE_BOOTSTRAP_WAIT); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::bootstrap_error_wait(const char *reason) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + tr_error("M2MInterfaceImpl::bootstrap_error_wait"); set_error_description(reason); internal_event(STATE_BOOTSTRAP_ERROR_WAIT); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::bootstrap_error(const char *reason) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_error("M2MInterfaceImpl::bootstrap_error(%s)", reason); _bootstrapped = false; if (_registration_flow_timer) { @@ -514,8 +498,8 @@ void M2MInterfaceImpl::bootstrap_error(const char *reason) if(_reconnection_time >= MAX_RECONNECT_TIMEOUT) { _reconnection_time = MAX_RECONNECT_TIMEOUT; } -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::coap_data_processed() { @@ -546,6 +530,7 @@ void M2MInterfaceImpl::data_available(uint8_t* data, void M2MInterfaceImpl::socket_error(int error_code, bool retry) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE // Bootstrap completed once PEER CLOSE notify received from the server. if (_current_state == STATE_BOOTSTRAP_WAIT && error_code == M2MConnectionHandler::SSL_PEER_CLOSE_NOTIFY) { @@ -553,6 +538,7 @@ void M2MInterfaceImpl::socket_error(int error_code, bool retry) bootstrap_done(); return; } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_error("M2MInterfaceImpl::socket_error: (%d), retry (%d), reconnecting (%d), reconnection_state (%d)", error_code, retry, _reconnecting, (int)_reconnection_state); @@ -665,13 +651,17 @@ void M2MInterfaceImpl::address_ready(const M2MConnectionObserver::SocketAddress ResolvedAddressData data; data._address = &address; data._port = server_port; - if( M2MConnectionObserver::Bootstrap == server_type) { - tr_info("M2MInterfaceImpl::address_ready() Server Type Bootstrap"); - internal_event(STATE_BOOTSTRAP_ADDRESS_RESOLVED, &data); - } else { + if(M2MConnectionObserver::LWM2MServer == server_type) { tr_info("M2MInterfaceImpl::address_ready() Server Type LWM2M"); internal_event(STATE_REGISTER_ADDRESS_RESOLVED, &data); + + } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + else { + tr_info("M2MInterfaceImpl::address_ready() Server Type Bootstrap"); + internal_event(STATE_BOOTSTRAP_ADDRESS_RESOLVED, &data); } +#endif } void M2MInterfaceImpl::data_sent() @@ -685,6 +675,7 @@ void M2MInterfaceImpl::data_sent() } } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE if (_current_state == STATE_BOOTSTRAP_ERROR_WAIT) { // bootstrap_error to be called only after we have sent the last ACK. // Otherwise client will goto reconnection mode before ACK has sent. @@ -692,6 +683,9 @@ void M2MInterfaceImpl::data_sent() } else if (_current_state != STATE_BOOTSTRAP_WAIT) { internal_event(STATE_COAP_DATA_SENT); } +#else + internal_event(STATE_COAP_DATA_SENT); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE // Delay the time when CoAP ping will be send. _nsdl_interface.calculate_new_coap_ping_send_time(); @@ -718,9 +712,13 @@ void M2MInterfaceImpl::timer_expired(M2MTimerObserver::Type type) _retry_timer_expired = true; if (_bootstrapped) { internal_event(STATE_REGISTER); - } else { + } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + else { internal_event(STATE_BOOTSTRAP); } +#endif + } else if (M2MTimerObserver::BootstrapFlowTimer == type) { #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::timer_expired() - bootstrap"); @@ -746,9 +744,9 @@ void M2MInterfaceImpl::state_idle(EventData* /*data*/) _queue_sleep_timer.stop_timer(); } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::state_bootstrap(EventData *data) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap"); // Start with bootstrapping preparation _bootstrapped = false; @@ -818,12 +816,10 @@ void M2MInterfaceImpl::state_bootstrap(EventData *data) M2MConnectionObserver::Bootstrap, _security); } -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved"); if (data) { ResolvedAddressData *event = static_cast (data); @@ -854,22 +850,18 @@ void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data) _observer.error(M2MInterface::InvalidParameters); } } -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrap_resource_created( EventData */*data*/) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrap_resource_created"); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void M2MInterfaceImpl::state_bootstrapped( EventData */*data*/) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MInterfaceImpl::state_bootstrapped"); -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MInterfaceImpl::state_register(EventData *data) { @@ -1068,8 +1060,8 @@ void M2MInterfaceImpl::state_pause() } _connection_handler.unregister_network_handler(); + _connection_handler.force_close(); _nsdl_interface.set_request_context_to_be_resend(NULL, 0); - _connection_handler.stop_listening(); _retry_timer.stop_timer(); _reconnecting = false; _reconnection_time = _initial_reconnection_time; @@ -1220,32 +1212,24 @@ void M2MInterfaceImpl::state_function( uint8_t current_state, EventData* data ) case STATE_IDLE: M2MInterfaceImpl::state_idle(data); break; - case STATE_BOOTSTRAP: #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + case STATE_BOOTSTRAP: M2MInterfaceImpl::state_bootstrap(data); - #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAP_ADDRESS_RESOLVED: - #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrap_address_resolved(data); - #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAP_RESOURCE_CREATED: - #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrap_resource_created(data); - #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAP_WAIT: case STATE_BOOTSTRAP_ERROR_WAIT: - #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE // Do nothing, we're just waiting for data_sent callback - #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE break; case STATE_BOOTSTRAPPED: - #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE M2MInterfaceImpl::state_bootstrapped(data); - #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE break; + #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE case STATE_REGISTER: M2MInterfaceImpl::state_register(data); break; @@ -1424,6 +1408,7 @@ bool M2MInterfaceImpl::set_uri_query_parameters(const char *uri_query_params) void M2MInterfaceImpl::pause() { + _connection_handler.claim_mutex(); tr_debug("M2MInterfaceImpl::pause"); BEGIN_TRANSITION_MAP // - Current State - @@ -1437,14 +1422,14 @@ void M2MInterfaceImpl::pause() TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_registered - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data - TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_update_registration + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_unregister + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_unregistered + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_sending_coap_data + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_coap_data_sent + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_coap_data_received + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_processing_coap_data + TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_coap_data_processed TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_pause TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_resume TRANSITION_MAP_ENTRY (STATE_PAUSE) // state_waiting @@ -1455,10 +1440,12 @@ void M2MInterfaceImpl::pause() set_error_description(ERROR_REASON_29); _observer.error(M2MInterface::NotAllowed); } + _connection_handler.release_mutex(); } void M2MInterfaceImpl::resume(void *iface, const M2MBaseList &list) { + _connection_handler.claim_mutex(); tr_debug("M2MInterfaceImpl::resume"); M2MResumeData data; data._interface = iface; @@ -1493,4 +1480,25 @@ void M2MInterfaceImpl::resume(void *iface, const M2MBaseList &list) set_error_description(ERROR_REASON_29); _observer.error(M2MInterface::NotAllowed); } + _connection_handler.release_mutex(); +} + +void M2MInterfaceImpl::network_interface_status_change(NetworkInterfaceStatus status) +{ + if (status == M2MConnectionObserver::NetworkInterfaceConnected) { + tr_info("M2MInterfaceImpl::network_interface_status_change - connected"); + if (_reconnecting) { + _retry_timer.stop_timer(); + if (_bootstrapped) { + internal_event(STATE_REGISTER); + } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + else { + internal_event(STATE_BOOTSTRAP); + } +#endif + } + } else { + tr_info("M2MInterfaceImpl::network_interface_status_change - disconnected"); + } } diff --git a/mbed-client/source/m2mnsdlinterface.cpp b/mbed-client/source/m2mnsdlinterface.cpp index fa7aea802..69b530951 100644 --- a/mbed-client/source/m2mnsdlinterface.cpp +++ b/mbed-client/source/m2mnsdlinterface.cpp @@ -61,9 +61,12 @@ #define MBED_CLIENT_NSDLINTERFACE_TASKLET_INIT_EVENT 0 // Tasklet init occurs always when generating a tasklet #define MBED_CLIENT_NSDLINTERFACE_EVENT 30 + +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE #define MBED_CLIENT_NSDLINTERFACE_BS_EVENT 31 #define MBED_CLIENT_NSDLINTERFACE_BS_PUT_EVENT 32 #define MBED_CLIENT_NSDLINTERFACE_BS_FINISH_EVENT 33 +#endif #ifdef MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE #define MBED_CLIENT_EVENT_LOOP_SIZE MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE @@ -71,11 +74,15 @@ #define MBED_CLIENT_EVENT_LOOP_SIZE 1024 #endif +#ifdef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT +#define MBED_CLIENT_DISABLE_EST_FEATURE +#endif + #define BUFFER_SIZE 21 #define TRACE_GROUP "mClt" #define MAX_QUERY_COUNT 10 -const char *MCC_VERSION = "mccv=3.0.0"; +const char *MCC_VERSION = "mccv=3.1.0"; int8_t M2MNsdlInterface::_tasklet_id = -1; @@ -101,7 +108,9 @@ extern "C" void nsdlinterface_tasklet_func(arm_event_s *event) M2MNsdlInterface::memory_free(coap_data); eventOS_scheduler_mutex_release(); - } else if (event->event_type == MBED_CLIENT_NSDLINTERFACE_BS_EVENT) { + } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + else if (event->event_type == MBED_CLIENT_NSDLINTERFACE_BS_EVENT) { M2MNsdlInterface::nsdl_coap_data_s *coap_data = (M2MNsdlInterface::nsdl_coap_data_s*)event->data_ptr; M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(coap_data->nsdl_handle); @@ -142,6 +151,7 @@ extern "C" void nsdlinterface_tasklet_func(arm_event_s *event) M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(nsdl_handle); interface->handle_bootstrap_finish_ack(event->event_data); } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } M2MNsdlInterface::M2MNsdlInterface(M2MNsdlObserver &observer, M2MConnectionHandler &connection_handler) @@ -396,9 +406,9 @@ bool M2MNsdlInterface::remove_nsdl_resource(M2MBase *base) return sn_nsdl_pop_resource(_nsdl_handle, resource); } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool M2MNsdlInterface::create_bootstrap_resource(sn_nsdl_addr_s *address) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_debug("M2MNsdlInterface::create_bootstrap_resource()"); _identity_accepted = false; _bootstrap_finish_ack_received = false; @@ -448,11 +458,8 @@ bool M2MNsdlInterface::create_bootstrap_resource(sn_nsdl_addr_s *address) tr_debug("M2MNsdlInterface::create_bootstrap_resource - _bootstrap_id %d", _bootstrap_id); } return success; -#else - (void)address; - return false; -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::set_server_address(uint8_t* address, uint8_t address_length, @@ -690,8 +697,11 @@ uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s *nsdl_hand uint8_t value = 0; request_context_s request_context; if(nsdl_handle && coap_header) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool is_bootstrap_msg = nsdl_handle->is_bs_server; - +#else + bool is_bootstrap_msg = false; +#endif if (coap_header->token_ptr && coap_header->token_len == sizeof(nsdl_handle->register_token) && memcmp(coap_header->token_ptr, &nsdl_handle->register_token, sizeof(nsdl_handle->register_token)) == 0) { @@ -736,6 +746,7 @@ uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s *nsdl_hand if (COAP_MSG_CODE_REQUEST_PUT == coap_header->msg_code) { if (is_bootstrap_msg) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE send_empty_ack(coap_header, address); nsdl_coap_data_s *nsdl_coap_data = create_coap_event_data(coap_header, address, @@ -752,17 +763,20 @@ uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s *nsdl_hand coap_header, COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE); } - +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } else { tr_debug("M2MNsdlInterface::received_from_server_callback - Method not allowed (PUT)."); coap_response = sn_nsdl_build_response(_nsdl_handle, coap_header, COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED); } + } else if (COAP_MSG_CODE_REQUEST_DELETE == coap_header->msg_code) { if (is_bootstrap_msg) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE handle_bootstrap_delete(coap_header, address); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } else { tr_debug("M2MNsdlInterface::received_from_server_callback - Method not allowed (DELETE)."); coap_response = sn_nsdl_build_response(_nsdl_handle, @@ -2045,9 +2059,9 @@ nsdl_s * M2MNsdlInterface::get_nsdl_handle() const return _nsdl_handle; } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::handle_bootstrap_put_message(sn_coap_hdr_s *coap_header, sn_nsdl_addr_s *address) { -#ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MNsdlInterface::handle_bootstrap_put_message"); uint8_t response_code = COAP_MSG_CODE_RESPONSE_CHANGED; sn_coap_hdr_s *coap_response = NULL; @@ -2140,16 +2154,11 @@ void M2MNsdlInterface::handle_bootstrap_put_message(sn_coap_hdr_s *coap_header, } handle_bootstrap_error(buffer, true); } -#else - (void) coap_header; - (void) address; -#endif } bool M2MNsdlInterface::parse_bootstrap_message(sn_coap_hdr_s *coap_header, M2MNsdlInterface::ObjectType lwm2m_object_type) { -#ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MNsdlInterface::parse_bootstrap_message"); bool ret = false; bool is_obj_instance = false; @@ -2237,16 +2246,10 @@ bool M2MNsdlInterface::parse_bootstrap_message(sn_coap_hdr_s *coap_header, tr_error("M2MNsdlInterface::parse_bootstrap_message -- no security object!"); } return ret; -#else - (void) coap_header; - (void) is_security_object; - return false; -#endif } void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address) { -#ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH]; String object_name = coap_to_string(coap_header->uri_path_ptr, @@ -2280,7 +2283,7 @@ void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_n } } } -#endif +#endif //MBED_CLIENT_DISABLE_EST_FEATURE sn_coap_hdr_s *coap_response = NULL; uint8_t msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; @@ -2300,7 +2303,7 @@ void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_n snprintf(buffer, sizeof(buffer), ERROR_REASON_26); msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; } -#endif +#endif //MBED_CLIENT_DISABLE_EST_FEATURE else { // Add short server id to server object if (m2m_id == -1) { @@ -2361,16 +2364,10 @@ void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_n handle_bootstrap_error(buffer, true); } } -#else - (void) coap_header; - (void) address; -#endif } void M2MNsdlInterface::handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address) { - -#ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH]; memset(buffer,0,sizeof(buffer)); sn_coap_hdr_s *coap_response = NULL; @@ -2411,16 +2408,13 @@ void M2MNsdlInterface::handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsd if (!coap_response || COAP_MSG_CODE_RESPONSE_DELETED != msg_code) { handle_bootstrap_error(buffer, true); } -#else - (void) coap_header; - (void) address; -#endif } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool M2MNsdlInterface::validate_security_object() { bool valid = false; -#ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE const M2MObjectInstanceList &instances = _security->instances(); M2MObjectInstanceList::const_iterator it; it = instances.begin(); @@ -2486,11 +2480,11 @@ bool M2MNsdlInterface::validate_security_object() return false; } } -#endif +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE return valid; } - +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::handle_bootstrap_error(const char *reason, bool wait) { tr_error("M2MNsdlInterface::handle_bootstrap_error(%s)",reason); @@ -2502,6 +2496,7 @@ void M2MNsdlInterface::handle_bootstrap_error(const char *reason, bool wait) _observer.bootstrap_error(reason); } } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE const String& M2MNsdlInterface::endpoint_name() const { @@ -2519,7 +2514,7 @@ const String M2MNsdlInterface::internal_endpoint_name() const String temp((const char*)_nsdl_handle->ep_information_ptr->location_ptr, _nsdl_handle->ep_information_ptr->location_len); // Get last part of the location path. - // In mbed Cloud environment full path is /rd/accountid/internal_endpoint + // In Device Management environment full path is /rd/accountid/internal_endpoint int location = temp.find_last_of('/') + 1; iep.append_raw((const char*)_nsdl_handle->ep_information_ptr->location_ptr + location, _nsdl_handle->ep_information_ptr->location_len - location); @@ -2930,11 +2925,13 @@ void M2MNsdlInterface::send_empty_ack(const sn_coap_hdr_s *header, sn_nsdl_addr_ } } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::store_bs_finished_response_id(uint16_t msg_id) { tr_debug("M2MNsdlInterface::store_bs_finished_response_id - id %d", msg_id); _bootstrap_id = msg_id; } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE struct M2MNsdlInterface::nsdl_coap_data_s* M2MNsdlInterface::create_coap_event_data( sn_coap_hdr_s *received_coap_header, @@ -3215,9 +3212,9 @@ void M2MNsdlInterface::handle_request_response(const sn_coap_hdr_s *coap_header, } } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::handle_bootstrap_response(const sn_coap_hdr_s *coap_header) { -#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("M2MNsdlInterface::handle_bootstrap_response"); _bootstrap_id = 0; M2MInterface::Error error_code = interface_error(*coap_header); @@ -3235,10 +3232,8 @@ void M2MNsdlInterface::handle_bootstrap_response(const sn_coap_hdr_s *coap_heade } else { _identity_accepted = true; } -#else - (void)coap_header; -#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool M2MNsdlInterface::handle_post_response(sn_coap_hdr_s* coap_header, sn_nsdl_addr_s* address, @@ -3249,7 +3244,9 @@ bool M2MNsdlInterface::handle_post_response(sn_coap_hdr_s* coap_header, bool execute_value_updated = false; if (is_bootstrap_msg) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE handle_bootstrap_finished(coap_header, address); +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } else if (coap_header->uri_path_ptr) { String resource_name = coap_to_string(coap_header->uri_path_ptr, @@ -3364,6 +3361,7 @@ void M2MNsdlInterface::handle_empty_ack(const sn_coap_hdr_s *coap_header, bool i } } } else if (is_bootstrap_msg) { +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE if (!_bootstrap_finish_ack_received) { // The _bootstrap_finish_ack_received flag is used to avoid sending the finish event // twice incase we get the same ack before the event loop has handled the event. @@ -3383,6 +3381,7 @@ void M2MNsdlInterface::handle_empty_ack(const sn_coap_hdr_s *coap_header, bool i else { tr_debug("M2MNsdlInterface::handle_empty_ack - finish event already in progress"); } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } else { coap_response_s *data = find_response(coap_header->msg_id); if (data) { @@ -3406,6 +3405,7 @@ void M2MNsdlInterface::handle_empty_ack(const sn_coap_hdr_s *coap_header, bool i } } +#ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::handle_bootstrap_finish_ack(uint16_t msg_id) { // EMPTY ACK for BS finished @@ -3421,6 +3421,7 @@ void M2MNsdlInterface::handle_bootstrap_finish_ack(uint16_t msg_id) handle_bootstrap_error(buffer, false); } } +#endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void M2MNsdlInterface::handle_message_delivered(M2MBase *base, const M2MBase::MessageType type) { @@ -3613,9 +3614,7 @@ struct M2MNsdlInterface::coap_response_s* M2MNsdlInterface::find_delayed_respons coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list); while (data) { if (data->uri_path && - strcmp(data->uri_path, uri_path) == 0 && - data->type == type && - ((message_id == UNDEFINED_MSG_ID)) || (data->msg_id == message_id)) { + strcmp(data->uri_path, uri_path) == 0 && data->type == type && ((message_id == UNDEFINED_MSG_ID)) || (data->msg_id == message_id)) { return data; } data = (coap_response_s *)ns_list_get_next(&_response_list, data); diff --git a/source/ConnectorClient.cpp b/source/ConnectorClient.cpp index acf648b77..354ae1ea6 100644 --- a/source/ConnectorClient.cpp +++ b/source/ConnectorClient.cpp @@ -59,14 +59,17 @@ #define CREDENTIAL_ERROR "Failed to read credentials from storage" #define DEVICE_NOT_PROVISIONED "Device not provisioned" #define CONNECTOR_ERROR_NO_MEMORY "Not enough memory to store LWM2M credentials" -#define CONNECTOR_BOOTSTRAP_AGAIN "Re-bootstrapping" + #ifndef MBED_CLIENT_DISABLE_EST_FEATURE #define ERROR_EST_ENROLLMENT_REQUEST_FAILED "EST enrollment request failed" #define LWM2M_CSR_SUBJECT_FORMAT "L=%s,OU=%s,CN=%s" #endif // !MBED_CLIENT_DISABLE_EST_FEATURE +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE #define MAX_REBOOTSTRAP_TIMEOUT 21600 // 6 hours +#define CONNECTOR_BOOTSTRAP_AGAIN "Re-bootstrapping" +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE // XXX: nothing here yet class EventData { @@ -275,7 +278,9 @@ ConnectorClient::ConnectorClient(ConnectorClientCallback* callback) _setup_complete(false), _interface(NULL), _security(NULL), _endpoint_info(M2MSecurity::Certificate), _client_objs(NULL), +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE _rebootstrap_timer(*this), _bootstrap_security_instance(1), +#endif _lwm2m_security_instance(0), _certificate_chain_handle(NULL) #ifndef MBED_CLIENT_DISABLE_EST_FEATURE ,_est_client(*this) @@ -283,8 +288,9 @@ ConnectorClient::ConnectorClient(ConnectorClientCallback* callback) { assert(_callback != NULL); - +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE _rebootstrap_time = randLIB_get_random_in_range(1, 10); +#endif } @@ -338,6 +344,7 @@ const EstClient &ConnectorClient::est_client() } #endif // !MBED_CLIENT_DISABLE_EST_FEATURE +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void ConnectorClient::start_bootstrap() { tr_debug("ConnectorClient::start_bootstrap()"); @@ -359,6 +366,139 @@ void ConnectorClient::start_bootstrap() state_engine(); } +/* +* Creates bootstrap server object with bootstrap server address and other parameters +* required for connecting to bootstrap server. +*/ +bool ConnectorClient::create_bootstrap_object() +{ + tr_debug("ConnectorClient::create_bootstrap_object"); + bool success = false; + + // Check if bootstrap credentials are already stored in KCM + if (bootstrap_credentials_stored_in_kcm() && _security) { + int32_t bs_id = _security->get_security_instance_id(M2MSecurity::Bootstrap); + if (_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate, bs_id)) { + success = true; + } + + tr_info("ConnectorClient::create_bootstrap_object - bs_id = %" PRId32, bs_id); + tr_info("ConnectorClient::create_bootstrap_object - use credentials from storage"); + + // Allocate scratch buffer, this will be used to copy parameters from storage to security object + size_t real_size = 0; + const int max_size = MAX_CERTIFICATE_SIZE; + uint8_t *buffer = NULL; + if (success) { + success = false; + buffer = (uint8_t*)malloc(max_size); + if (buffer != NULL) { + success = true; + } + } + + // Read internal endpoint name if it exists, we need to append + // it to bootstrap uri if device already bootstrapped + uint8_t *iep = NULL; + if (success && ccs_get_string_item(KEY_INTERNAL_ENDPOINT, buffer, max_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { + iep = (uint8_t*)malloc(strlen((const char*)buffer) + strlen(INTERNAL_ENDPOINT_PARAM) + 1); + if (iep != NULL) { + strcpy((char*)iep, INTERNAL_ENDPOINT_PARAM); + strcat((char*)iep, (const char*)buffer); + tr_info("ConnectorClient::create_bootstrap_object - iep: %s", buffer); + } + //TODO: Should handle error if iep exists but allocation fails? + } + + // Bootstrap URI + if (success) { + success = false; + if (ccs_get_string_item(g_fcc_bootstrap_server_uri_name, buffer, max_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { + real_size = strlen((const char*)buffer); + + // Append iep if we 1. have it 2. it doesn't already exist in uri 3. it fits + if (iep && + strstr((const char*)buffer, (const char*)iep) == NULL && + (real_size + strlen((const char*)iep) + 1) <= max_size) { + strcat((char*)buffer, (const char*)iep); + real_size += strlen((const char*)iep) + 1; + } + + tr_info("ConnectorClient::create_bootstrap_object - M2MServerUri %.*s", (int)real_size, buffer); + if (_security->set_resource_value(M2MSecurity::M2MServerUri, buffer, real_size, bs_id)) { + success = true; + } + } + } + + free(iep); + + // Endpoint + if (success) { + success = false; + if (ccs_get_item(g_fcc_endpoint_parameter_name, buffer, max_size, &real_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { + success = true; + _endpoint_info.endpoint_name = String((const char*)buffer, real_size); + tr_info("ConnectorClient::create_bootstrap_object - Endpoint %s", _endpoint_info.endpoint_name.c_str()); + } + } + + // Account ID, not mandatory + if (success) { + if (ccs_get_item(KEY_ACCOUNT_ID, buffer, max_size, &real_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { + _endpoint_info.account_id = String((const char*)buffer, real_size); + tr_info("ConnectorClient::create_bootstrap_object - AccountId %s", _endpoint_info.account_id.c_str()); + } + } + + free(buffer); + + if (!success) { + tr_error("ConnectorClient::create_bootstrap_object - Failed to read credentials"); + _callback->connector_error((M2MInterface::Error)MbedCloudClient::ConnectorFailedToReadCredentials,CREDENTIAL_ERROR); + _security->remove_object_instance(bs_id); + } + } else { + success = true; + tr_info("ConnectorClient::create_bootstrap_object - bootstrap object already done"); + } + + + return success; +} + +void ConnectorClient::state_bootstrap_start() +{ + tr_info("ConnectorClient::state_bootstrap_start()"); + assert(_interface != NULL); + assert(_security != NULL); + + _interface->bootstrap(_security); + + internal_event(State_Bootstrap_Started); +} + +void ConnectorClient::state_bootstrap_started() +{ + // this state may be useful only for verifying the callbacks? +} + +void ConnectorClient::state_bootstrap_success() +{ + assert(_callback != NULL); + // Parse internal endpoint name from mDS cert + _callback->registration_process_result(State_Bootstrap_Success); +} + +void ConnectorClient::state_bootstrap_failure() +{ + assert(_callback != NULL); + // maybe some additional canceling and/or leanup is needed here? + _callback->registration_process_result(State_Bootstrap_Failure); +} + +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + void ConnectorClient::start_registration(M2MBaseList* client_objs) { tr_debug("ConnectorClient::start_registration()"); @@ -438,6 +578,7 @@ void ConnectorClient::state_engine(void) void ConnectorClient::state_function(StartupSubStateRegistration current_state) { switch (current_state) { +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE case State_Bootstrap_Start: state_bootstrap_start(); break; @@ -450,6 +591,8 @@ void ConnectorClient::state_function(StartupSubStateRegistration current_state) case State_Bootstrap_Failure: state_bootstrap_failure(); break; +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + #ifndef MBED_CLIENT_DISABLE_EST_FEATURE case State_EST_Start: state_est_start(); @@ -609,136 +752,7 @@ bool ConnectorClient::create_register_object() return success; } -/* -* Creates bootstrap server object with bootstrap server address and other parameters -* required for connecting to mbed Cloud bootstrap server. -*/ -bool ConnectorClient::create_bootstrap_object() -{ - tr_debug("ConnectorClient::create_bootstrap_object"); - bool success = false; - - // Check if bootstrap credentials are already stored in KCM - if (bootstrap_credentials_stored_in_kcm() && _security) { - int32_t bs_id = _security->get_security_instance_id(M2MSecurity::Bootstrap); - if (_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate, bs_id)) { - success = true; - } - - tr_info("ConnectorClient::create_bootstrap_object - bs_id = %" PRId32, bs_id); - tr_info("ConnectorClient::create_bootstrap_object - use credentials from storage"); - - // Allocate scratch buffer, this will be used to copy parameters from storage to security object - size_t real_size = 0; - const int max_size = MAX_CERTIFICATE_SIZE; - uint8_t *buffer = NULL; - if (success) { - success = false; - buffer = (uint8_t*)malloc(max_size); - if (buffer != NULL) { - success = true; - } - } - - // Read internal endpoint name if it exists, we need to append - // it to bootstrap uri if device already bootstrapped - uint8_t *iep = NULL; - if (success && ccs_get_string_item(KEY_INTERNAL_ENDPOINT, buffer, max_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { - iep = (uint8_t*)malloc(strlen((const char*)buffer) + strlen(INTERNAL_ENDPOINT_PARAM) + 1); - if (iep != NULL) { - strcpy((char*)iep, INTERNAL_ENDPOINT_PARAM); - strcat((char*)iep, (const char*)buffer); - tr_info("ConnectorClient::create_bootstrap_object - iep: %s", buffer); - } - //TODO: Should handle error if iep exists but allocation fails? - } - - // Bootstrap URI - if (success) { - success = false; - if (ccs_get_string_item(g_fcc_bootstrap_server_uri_name, buffer, max_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { - real_size = strlen((const char*)buffer); - - // Append iep if we 1. have it 2. it doesn't already exist in uri 3. it fits - if (iep && - strstr((const char*)buffer, (const char*)iep) == NULL && - (real_size + strlen((const char*)iep) + 1) <= max_size) { - strcat((char*)buffer, (const char*)iep); - real_size += strlen((const char*)iep) + 1; - } - - tr_info("ConnectorClient::create_bootstrap_object - M2MServerUri %.*s", (int)real_size, buffer); - if (_security->set_resource_value(M2MSecurity::M2MServerUri, buffer, real_size, bs_id)) { - success = true; - } - } - } - - free(iep); - - // Endpoint - if (success) { - success = false; - if (ccs_get_item(g_fcc_endpoint_parameter_name, buffer, max_size, &real_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { - success = true; - _endpoint_info.endpoint_name = String((const char*)buffer, real_size); - tr_info("ConnectorClient::create_bootstrap_object - Endpoint %s", _endpoint_info.endpoint_name.c_str()); - } - } - - // Account ID, not mandatory - if (success) { - if (ccs_get_item(KEY_ACCOUNT_ID, buffer, max_size, &real_size, CCS_CONFIG_ITEM) == CCS_STATUS_SUCCESS) { - _endpoint_info.account_id = String((const char*)buffer, real_size); - tr_info("ConnectorClient::create_bootstrap_object - AccountId %s", _endpoint_info.account_id.c_str()); - } - } - - free(buffer); - - if (!success) { - tr_error("ConnectorClient::create_bootstrap_object - Failed to read credentials"); - _callback->connector_error((M2MInterface::Error)MbedCloudClient::ConnectorFailedToReadCredentials,CREDENTIAL_ERROR); - _security->remove_object_instance(bs_id); - } - } else { - success = true; - tr_info("ConnectorClient::create_bootstrap_object - bootstrap object already done"); - } - - - return success; -} - -void ConnectorClient::state_bootstrap_start() -{ - tr_info("ConnectorClient::state_bootstrap_start()"); - assert(_interface != NULL); - assert(_security != NULL); - _interface->bootstrap(_security); - - internal_event(State_Bootstrap_Started); -} - -void ConnectorClient::state_bootstrap_started() -{ - // this state may be useful only for verifying the callbacks? -} - -void ConnectorClient::state_bootstrap_success() -{ - assert(_callback != NULL); - // Parse internal endpoint name from mDS cert - _callback->registration_process_result(State_Bootstrap_Success); -} - -void ConnectorClient::state_bootstrap_failure() -{ - assert(_callback != NULL); - // maybe some additional canceling and/or leanup is needed here? - _callback->registration_process_result(State_Bootstrap_Failure); -} #ifndef MBED_CLIENT_DISABLE_EST_FEATURE void ConnectorClient::state_est_start() @@ -957,8 +971,9 @@ void ConnectorClient::state_registration_success() (size_t)_endpoint_info.internal_endpoint_name.size(), CCS_CONFIG_ITEM); } - +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE _rebootstrap_time = randLIB_get_random_in_range(1, 10); +#endif _callback->registration_process_result(State_Registration_Success); } @@ -977,6 +992,7 @@ void ConnectorClient::state_unregistered() void ConnectorClient::bootstrap_data_ready(M2MSecurity *security_object) { +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE tr_info("ConnectorClient::bootstrap_data_ready"); if(security_object) { // Update bootstrap credentials (we could skip this if we knew whether they were updated) @@ -1028,6 +1044,9 @@ void ConnectorClient::bootstrap_data_ready(M2MSecurity *security_object) tr_info("ConnectorClient::bootstrap_data_ready - set_credentials status %d", status); } } +#else + (void) security_object; +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } void ConnectorClient::bootstrap_done(M2MSecurity *security_object) @@ -1055,7 +1074,7 @@ void ConnectorClient::error(M2MInterface::Error error) { tr_error("ConnectorClient::error() - error: %d", error); assert(_callback != NULL); - +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE if (_current_state >= State_Registration_Start && use_bootstrap() && (error == M2MInterface::SecureConnectionFailed || @@ -1071,8 +1090,11 @@ void ConnectorClient::error(M2MInterface::Error error) bootstrap_again(); } else { +#endif _callback->connector_error(error, _interface->error_description()); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE } +#endif } void ConnectorClient::value_updated(M2MBase *base, M2MBase::BaseType type) @@ -1090,6 +1112,7 @@ bool ConnectorClient::connector_credentials_available() return true; } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool ConnectorClient::use_bootstrap() { tr_debug("ConnectorClient::use_bootstrap"); @@ -1106,7 +1129,7 @@ bool ConnectorClient::use_bootstrap() } return false; } - +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE bool ConnectorClient::get_key(const char *key, const char *endpoint, char *&key_name) { @@ -1209,6 +1232,7 @@ ccs_status_e ConnectorClient::set_connector_credentials(M2MSecurity *security) return status; } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE ccs_status_e ConnectorClient::set_bootstrap_credentials(M2MSecurity *security) { tr_debug("ConnectorClient::set_bootstrap_credentials"); @@ -1246,18 +1270,6 @@ ccs_status_e ConnectorClient::set_bootstrap_credentials(M2MSecurity *security) return status; } -ccs_status_e ConnectorClient::clear_first_to_claim() -{ - tr_debug("ConnectorClient::clear_first_to_claim"); - return ccs_delete_item(KEY_FIRST_TO_CLAIM, CCS_CONFIG_ITEM); -} - - -const ConnectorClientEndpointInfo *ConnectorClient::endpoint_info() const -{ - return &_endpoint_info; -} - bool ConnectorClient::bootstrap_credentials_stored_in_kcm() { size_t real_size = 0; @@ -1269,6 +1281,19 @@ bool ConnectorClient::bootstrap_credentials_stored_in_kcm() return false; } } +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + +ccs_status_e ConnectorClient::clear_first_to_claim() +{ + tr_debug("ConnectorClient::clear_first_to_claim"); + return ccs_delete_item(KEY_FIRST_TO_CLAIM, CCS_CONFIG_ITEM); +} + + +const ConnectorClientEndpointInfo *ConnectorClient::endpoint_info() const +{ + return &_endpoint_info; +} bool ConnectorClient::is_first_to_claim() { @@ -1286,12 +1311,14 @@ bool ConnectorClient::is_first_to_claim() return false; } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void ConnectorClient::timer_expired(M2MTimerObserver::Type type) { if (type == M2MTimerObserver::BootstrapFlowTimer) { start_bootstrap(); } } +#endif #ifndef MBED_CLIENT_DISABLE_EST_FEATURE void ConnectorClient::est_enrollment_result(est_enrollment_result_e result, @@ -1429,6 +1456,7 @@ void ConnectorClient::set_certificate_chain_handle(void *cert_handle) _certificate_chain_handle = cert_handle; } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void ConnectorClient::bootstrap_again() { // delete the old connector credentials @@ -1447,3 +1475,4 @@ void ConnectorClient::bootstrap_again() _callback->connector_error(M2MInterface::SecureConnectionFailed, CONNECTOR_BOOTSTRAP_AGAIN); } +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE diff --git a/source/ServiceClient.cpp b/source/ServiceClient.cpp index 9fc3f6059..594ac23d7 100644 --- a/source/ServiceClient.cpp +++ b/source/ServiceClient.cpp @@ -184,8 +184,11 @@ void ServiceClient::finish_initialization(void) /* Add Device Object to object list. */ _client_objs->push_back(device_object); } - +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE internal_event(State_Bootstrap); +#else + internal_event(State_Register); +#endif } ConnectorClient &ServiceClient::connector_client() @@ -234,9 +237,11 @@ void ServiceClient::state_function(StartupMainState current_state) { switch (current_state) { case State_Init: // -> Goes to bootstrap state +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE case State_Bootstrap: // -> State_Register OR State_Failure state_bootstrap(); break; +#endif case State_Register: // -> State_Succes OR State_Failure state_register(); break; @@ -252,6 +257,7 @@ void ServiceClient::state_function(StartupMainState current_state) } } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void ServiceClient::state_bootstrap() { tr_info("ServiceClient::state_bootstrap()"); @@ -274,7 +280,7 @@ void ServiceClient::state_bootstrap() _connector_client.start_bootstrap(); } } - +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE void ServiceClient::state_register() { tr_info("ServiceClient::state_register()"); @@ -307,9 +313,11 @@ void ServiceClient::connector_error(M2MInterface::Error error, const char *reaso if (_current_state == State_Register) { registration_process_result(ConnectorClient::State_Registration_Failure); } +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE else if (_current_state == State_Bootstrap) { registration_process_result(ConnectorClient::State_Bootstrap_Failure); } +#endif _service_callback.error(int(error),reason); internal_event(State_Failure); } diff --git a/source/include/ConnectorClient.h b/source/include/ConnectorClient.h index ad2f5576d..d70af8ae9 100644 --- a/source/include/ConnectorClient.h +++ b/source/include/ConnectorClient.h @@ -30,6 +30,10 @@ #include "mbed-client/m2mtimer.h" #include "include/CloudClientStorage.h" +#ifdef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT +#define MBED_CLIENT_DISABLE_EST_FEATURE +#endif + #ifndef MBED_CLIENT_DISABLE_EST_FEATURE #include "include/EstClient.h" #endif // !MBED_CLIENT_DISABLE_EST_FEATURE @@ -74,7 +78,11 @@ struct ConnectorClientEndpointInfo { * This class is intended to be used via ServiceClient, not directly. * This class contains also the bootstrap functionality. */ -class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { +class ConnectorClient : public M2MInterfaceObserver +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + , public M2MTimerObserver +#endif +{ public: /** @@ -121,10 +129,12 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { */ bool setup(); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Starts the bootstrap sequence from the Service Client. */ void start_bootstrap(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Starts the registration sequence from the Service Client. @@ -143,11 +153,13 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { */ M2MInterface * m2m_interface(); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Checks whether to use Bootstrap or direct Connector mode. * \return True if bootstrap mode, False if direct Connector flow */ bool use_bootstrap(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Checks whether to go connector registration flow @@ -194,12 +206,14 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { const EstClient &est_client(); #endif /* MBED_CLIENT_DISABLE_EST_FEATURE */ +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief Starts bootstrap sequence again. * This will clean the old LwM2M credentials. * */ void bootstrap_again(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE public: // implementation of M2MInterfaceObserver: @@ -259,9 +273,10 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { */ virtual void value_updated(M2MBase *base, M2MBase::BaseType type); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE protected: // from M2MTimerObserver - virtual void timer_expired(M2MTimerObserver::Type type); +#endif private: /** @@ -283,6 +298,7 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { */ void internal_event(StartupSubStateRegistration new_state); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * When the bootstrap starts. */ @@ -302,6 +318,7 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { * When the bootstrap failed. */ void state_bootstrap_failure(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE #ifndef MBED_CLIENT_DISABLE_EST_FEATURE /** @@ -356,20 +373,13 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { */ bool create_register_object(); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * \brief A utility function to create an M2MSecurity object * for bootstrap. */ bool create_bootstrap_object(); - /** - * \brief A utility function to set the connector credentials - * in storage. This includes endpoint, domain, connector URI - * and certificates. - * \param security, The Connector certificates. - */ - ccs_status_e set_connector_credentials(M2MSecurity *security); - /** * \brief A utility function to set the bootstrap credentials * in storage. This includes Bootstrap URI and certificates. @@ -381,6 +391,15 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { * \brief A utility function to check whether bootstrap credentials are stored in KCM. */ bool bootstrap_credentials_stored_in_kcm(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE + + /** + * \brief A utility function to set the connector credentials + * in storage. This includes endpoint, domain, connector URI + * and certificates. + * \param security, The Connector certificates. + */ + ccs_status_e set_connector_credentials(M2MSecurity *security); /** * \brief A utility function to check whether first to claim feature is configured. @@ -416,10 +435,12 @@ class ConnectorClient : public M2MInterfaceObserver, public M2MTimerObserver { M2MSecurity *_security; ConnectorClientEndpointInfo _endpoint_info; M2MBaseList *_client_objs; +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE M2MTimer _rebootstrap_timer; + uint16_t _rebootstrap_time; uint16_t _bootstrap_security_instance; +#endif uint16_t _lwm2m_security_instance; - uint16_t _rebootstrap_time; void *_certificate_chain_handle; #ifndef MBED_CLIENT_DISABLE_EST_FEATURE EstClient _est_client; diff --git a/source/include/ServiceClient.h b/source/include/ServiceClient.h index 8fde88d00..6b3b4c1e8 100644 --- a/source/include/ServiceClient.h +++ b/source/include/ServiceClient.h @@ -82,8 +82,8 @@ class ServiceClientCallback { * \brief ServiceClient * This class handles all internal interactions between various client * components including connector, identity and update. - * This class maintains the state machine for the use case flow of mbed Cloud - * Client. + * This class maintains the state machine for the use case flow of + * client. */ class ServiceClient : private ConnectorClientCallback @@ -96,7 +96,9 @@ class ServiceClient : private ConnectorClientCallback */ enum StartupMainState { State_Init, +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE State_Bootstrap, +#endif State_Register, State_Success, State_Failure, @@ -246,10 +248,12 @@ protected : */ void internal_event(StartupMainState new_state); +#ifndef MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * When the bootstrap is started. */ void state_bootstrap(); +#endif //MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE /** * When the registration is started.