From 9071e8e4dc552e202ad028184e4dbd566ae7b5d6 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Thu, 3 Aug 2023 14:54:21 +0000 Subject: [PATCH 01/13] freertos-integration-tests: Use FreeRTOS malloc API Instead of directly using malloc, use FreeRTOS provided malloc API. Signed-off-by: Devaraj Ranganna --- .../integration_tests_platform_function.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/aws-iot-example/freertos-integration-tests/integration_tests_platform_function.c b/Projects/aws-iot-example/freertos-integration-tests/integration_tests_platform_function.c index 5ef4137f..948c9f3e 100644 --- a/Projects/aws-iot-example/freertos-integration-tests/integration_tests_platform_function.c +++ b/Projects/aws-iot-example/freertos-integration-tests/integration_tests_platform_function.c @@ -156,7 +156,7 @@ FRTestThreadHandle_t FRTest_ThreadCreate( FRTestThreadFunction_t func, { Task_t * task = NULL; - task = malloc( sizeof( Task_t ) ); + task = pvPortMalloc( sizeof( Task_t ) ); task->mutex = xSemaphoreCreateBinaryStatic( &task->mutex_buf ); task->func = func; task->args = pParam; From f62ffe5a85209120c9dae1bdb26e63e15cfcfaf6 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Fri, 18 Aug 2023 08:55:22 +0000 Subject: [PATCH 02/13] aws-iot-example: Use FreeRTOS `heap_4` instaed of `heap_3` FreeRTOS TCP/IP stack recommends `heap_4` and `BufferAllocation_2`. https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/tree/V4.0.0-appsec#note Since FreeRTOS manages the heap, set Mbed TLS memory APIs to use FreeRTOS heap APIs (mbedtls_platform_set_calloc_free). In addition, reduce allocated heap size from `0x000b0000` to `0x00000400`, since FreeRTOS manages heap when `heap_4` configuration is selected. Ideally, heap size should be 0, however, GNU expects linker symbol `end` to be available. Hence, set it to `0x400`. Also, set the FreeRTOS config `configAPPLICATION_ALLOCATED_HEAP` to 0, indicating FreeRTOS allocates the heap based on `configTOTAL_HEAP_SIZE` (720896 bytes). Signed-off-by: Devaraj Ranganna --- Bsp/an552_ns.ld | 2 +- Config/freertos-config/FreeRTOSConfig.h | 4 ++-- Projects/aws-iot-example/CMakeLists.txt | 2 +- Projects/aws-iot-example/main.c | 4 ++++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Bsp/an552_ns.ld b/Bsp/an552_ns.ld index 1888bb0f..47880487 100644 --- a/Bsp/an552_ns.ld +++ b/Bsp/an552_ns.ld @@ -9,7 +9,7 @@ */ __STACK_SIZE = 0x00002800; -__HEAP_SIZE = 0x000b0000; +__HEAP_SIZE = 0x00000400; MEMORY { diff --git a/Config/freertos-config/FreeRTOSConfig.h b/Config/freertos-config/FreeRTOSConfig.h index 1aadf856..453680c3 100644 --- a/Config/freertos-config/FreeRTOSConfig.h +++ b/Config/freertos-config/FreeRTOSConfig.h @@ -48,8 +48,8 @@ extern uint32_t SystemCoreClock; /* Memory allocation related definitions. */ #define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1 -#define configTOTAL_HEAP_SIZE 512000 /* should be enough */ -#define configAPPLICATION_ALLOCATED_HEAP 1 +#define configTOTAL_HEAP_SIZE 720896 +#define configAPPLICATION_ALLOCATED_HEAP 0 #define configENABLE_MVE 0 #define configENABLE_FPU 1 diff --git a/Projects/aws-iot-example/CMakeLists.txt b/Projects/aws-iot-example/CMakeLists.txt index fff8d9fc..74448fb6 100644 --- a/Projects/aws-iot-example/CMakeLists.txt +++ b/Projects/aws-iot-example/CMakeLists.txt @@ -111,7 +111,7 @@ target_link_libraries(freertos_config app-config ) -set( FREERTOS_HEAP "3" CACHE STRING "" FORCE) +set( FREERTOS_HEAP "4" CACHE STRING "" FORCE) # Select the native compile PORT set( FREERTOS_PORT "GCC_ARM_CM55_TFM" CACHE STRING "" FORCE) diff --git a/Projects/aws-iot-example/main.c b/Projects/aws-iot-example/main.c index 3e3f8921..14122e91 100644 --- a/Projects/aws-iot-example/main.c +++ b/Projects/aws-iot-example/main.c @@ -23,6 +23,7 @@ #include "task.h" #include "mbedtls/threading.h" +#include "mbedtls/platform.h" #include "threading_alt.h" /* Include header that defines log levels. */ @@ -132,6 +133,9 @@ int main() /* Create system events group. */ xSystemEvents = xEventGroupCreateStatic( &xSystemEventsGroup ); + /* Configure Mbed TLS memory APIs to use FreeRTOS heap APIs */ + mbedtls_platform_set_calloc_free( mbedtls_platform_calloc, mbedtls_platform_free ); + xRetVal = vDevModeKeyProvisioning(); if( xRetVal != CKR_OK ) From 3c4a180d74bc38d83b5d53db6ad555c507100b96 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Tue, 3 Oct 2023 11:14:23 +0000 Subject: [PATCH 03/13] iot-vsocket: Remove redundant `socket_startup.c` The socket creation is part of `Middleware/ARM/IoT_VSocket-lib/AVH/interface/vsocket/iot_socket.c` and hence `socket_startup.c` is redundant. In addition, `socket_startup()` always returns 0. Signed-off-by: Devaraj Ranganna --- Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt | 1 - Middleware/ARM/IoT_VSocket-lib/network_startup.c | 8 ++++---- Middleware/ARM/IoT_VSocket-lib/socket_startup.c | 11 ----------- 3 files changed, 4 insertions(+), 16 deletions(-) delete mode 100644 Middleware/ARM/IoT_VSocket-lib/socket_startup.c diff --git a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt index 65abca5e..4c3a5707 100644 --- a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt +++ b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt @@ -11,7 +11,6 @@ execute_process(COMMAND git am --abort add_library(iot-vsocket STATIC AVH/interface/vsocket/iot_socket.c - socket_startup.c network_startup.c transport_tls_iot_socket.c ) diff --git a/Middleware/ARM/IoT_VSocket-lib/network_startup.c b/Middleware/ARM/IoT_VSocket-lib/network_startup.c index 7d156381..c0a0acfc 100644 --- a/Middleware/ARM/IoT_VSocket-lib/network_startup.c +++ b/Middleware/ARM/IoT_VSocket-lib/network_startup.c @@ -5,8 +5,8 @@ #include -extern int32_t socket_startup (void); - -int32_t network_startup (void) { - return socket_startup(); +int32_t network_startup( void ) +{ + /* Assume network interface is already initialized */ + return 0; } diff --git a/Middleware/ARM/IoT_VSocket-lib/socket_startup.c b/Middleware/ARM/IoT_VSocket-lib/socket_startup.c deleted file mode 100644 index 93620407..00000000 --- a/Middleware/ARM/IoT_VSocket-lib/socket_startup.c +++ /dev/null @@ -1,11 +0,0 @@ -/* Copyright 2021-2023 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -int32_t socket_startup (void) { - /* Network interface is already initialized */ - return 0; -} From 08004e0f87de356a7d3f413ff658405410709586 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Fri, 18 Aug 2023 18:55:04 +0000 Subject: [PATCH 04/13] event-helper: Create a new system event helper library The system event helper library provides system event management, so that the tasks can wait on the required system event to happen. Signed-off-by: Devaraj Ranganna --- Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt | 1 + .../ARM/IoT_VSocket-lib/network_startup.c | 21 ++++++- Projects/aws-iot-example/CMakeLists.txt | 4 ++ .../event-helper/CMakeLists.txt | 22 +++++++ .../event-helper/event_helper.c | 62 +++++++++++++++++++ .../event-helper/event_helper.h | 48 ++++++++++++++ Projects/aws-iot-example/main.c | 11 ++-- .../mqtt-agent-wrapper/CMakeLists.txt | 1 + .../mqtt-agent-wrapper/event_helper.h | 16 ----- .../mqtt-agent-wrapper/mqtt_agent_task.c | 31 +--------- 10 files changed, 164 insertions(+), 53 deletions(-) create mode 100644 Projects/aws-iot-example/event-helper/CMakeLists.txt create mode 100644 Projects/aws-iot-example/event-helper/event_helper.c create mode 100644 Projects/aws-iot-example/event-helper/event_helper.h delete mode 100644 Projects/aws-iot-example/mqtt-agent-wrapper/event_helper.h diff --git a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt index 4c3a5707..9612e1dc 100644 --- a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt +++ b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt @@ -24,6 +24,7 @@ target_include_directories(iot-vsocket target_link_libraries(iot-vsocket PUBLIC iot-socket-api + event-helper freertos_kernel fri-bsp awsIoT diff --git a/Middleware/ARM/IoT_VSocket-lib/network_startup.c b/Middleware/ARM/IoT_VSocket-lib/network_startup.c index c0a0acfc..829c67d6 100644 --- a/Middleware/ARM/IoT_VSocket-lib/network_startup.c +++ b/Middleware/ARM/IoT_VSocket-lib/network_startup.c @@ -5,8 +5,27 @@ #include +#include "FreeRTOS.h" +#include "event_helper.h" + int32_t network_startup( void ) { - /* Assume network interface is already initialized */ + EventBits_t uxBits; + + /* The iot-vsocket implementation utilizes the underlying host network + * stack for network operation. It is assumed that the host network is + * already operational by the time the application runs. */ + uxBits = xEventGroupSetBits( xSystemEvents, EVENT_MASK_NETWORK_UP ); + + if( !( uxBits & EVENT_MASK_NETWORK_UP ) ) + { + /* During this stage of application intialisation, there are no other + * tasks active except logging task. Hence the value returned by + * xEventGroupSetBits must have EVENT_MASK_NETWORK_UP bit set. If not, + * something went wrong during initialisation. Trigger an assert to + * catch this. */ + configASSERT( 0 ); + } + return 0; } diff --git a/Projects/aws-iot-example/CMakeLists.txt b/Projects/aws-iot-example/CMakeLists.txt index 74448fb6..e39c3604 100644 --- a/Projects/aws-iot-example/CMakeLists.txt +++ b/Projects/aws-iot-example/CMakeLists.txt @@ -187,6 +187,7 @@ target_link_libraries(integration-test-task add_subdirectory(${MIDDLEWARE_DIR}/AWS ${CMAKE_BINARY_DIR}/Middleware/AWS) +add_subdirectory(event-helper) add_subdirectory(mqtt-agent-wrapper) add_subdirectory(provisioning provisioning_data) @@ -202,6 +203,7 @@ add_dependencies(aws-iot-example provisioning_data_bin) target_include_directories(aws-iot-example PRIVATE + . freertos-integration-tests/include provisioning ) @@ -220,6 +222,8 @@ target_link_libraries(aws-iot-example awsIoT mqtt-agent-task + event-helper + freertos-ota-pal-psa mbedtls diff --git a/Projects/aws-iot-example/event-helper/CMakeLists.txt b/Projects/aws-iot-example/event-helper/CMakeLists.txt new file mode 100644 index 00000000..f103fa34 --- /dev/null +++ b/Projects/aws-iot-example/event-helper/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright 2023 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +# System event helper library + +add_library(event-helper STATIC) + +target_sources(event-helper + PRIVATE + event_helper.c +) + +target_include_directories(event-helper + PUBLIC + . +) + +target_link_libraries(event-helper + PRIVATE + freertos_kernel +) diff --git a/Projects/aws-iot-example/event-helper/event_helper.c b/Projects/aws-iot-example/event-helper/event_helper.c new file mode 100644 index 00000000..a2ff97dc --- /dev/null +++ b/Projects/aws-iot-example/event-helper/event_helper.c @@ -0,0 +1,62 @@ +/* Copyright 2023 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#include "event_helper.h" + +/* System events group. */ +EventGroupHandle_t xSystemEvents = NULL; +static StaticEventGroup_t xSystemEventsGroup; + +void vEventHelperInit( void ) +{ + /* Create a system events group. */ + xSystemEvents = xEventGroupCreateStatic( &xSystemEventsGroup ); +} + +void vWaitUntilNetworkIsUp( void ) +{ + configASSERT( xSystemEvents != NULL ); + + /* There is no need to check the return value of this API, since the task + * is waiting for a particular bit to be set and is waiting forever. */ + ( void ) xEventGroupWaitBits( xSystemEvents, + EVENT_MASK_NETWORK_UP, + pdFALSE, + pdFALSE, + portMAX_DELAY ); +} + +void vWaitUntilMQTTAgentReady( void ) +{ + configASSERT( xSystemEvents != NULL ); + + /* There is no need to check the return value of this API, since the task + * is waiting for a particular bit to be set and is waiting forever. */ + ( void ) xEventGroupWaitBits( xSystemEvents, + EVENT_MASK_MQTT_INIT, + pdFALSE, + pdFALSE, + portMAX_DELAY ); +} + +void vWaitUntilMQTTAgentConnected( void ) +{ + configASSERT( xSystemEvents != NULL ); + + /* There is no need to check the return value of this API, since the task + * is waiting for a particular bit to be set and is waiting forever. */ + ( void ) xEventGroupWaitBits( xSystemEvents, + EVENT_MASK_MQTT_CONNECTED, + pdFALSE, + pdFALSE, + portMAX_DELAY ); +} + +bool xIsMqttAgentConnected( void ) +{ + EventBits_t uxEvents = xEventGroupGetBits( xSystemEvents ); + + return( ( bool ) ( uxEvents & EVENT_MASK_MQTT_CONNECTED ) ); +} diff --git a/Projects/aws-iot-example/event-helper/event_helper.h b/Projects/aws-iot-example/event-helper/event_helper.h new file mode 100644 index 00000000..e4ed2965 --- /dev/null +++ b/Projects/aws-iot-example/event-helper/event_helper.h @@ -0,0 +1,48 @@ +/* Copyright 2023 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#ifndef EVENT_HELPER_H +#define EVENT_HELPER_H + +#include + +/* Kernel includes. */ +#include "FreeRTOS.h" +#include "event_groups.h" + +#define EVENT_MASK_NETWORK_UP 0x01 +#define EVENT_MASK_MQTT_INIT 0x02 +#define EVENT_MASK_MQTT_CONNECTED 0x04 + +extern EventGroupHandle_t xSystemEvents; + +/** + * @brief Create a system events group. + */ +void vEventHelperInit( void ); + +/** + * @brief Wait until network is up and running. + */ +void vWaitUntilNetworkIsUp( void ); + +/** + * @brief Wait until MQTT agent is initialised. + */ +void vWaitUntilMQTTAgentReady( void ); + +/** + * @brief Wait until MQTT agent is connected to an MQTT broker. + */ +void vWaitUntilMQTTAgentConnected( void ); + +/** + * @brief Is MQTT agent connected to an MQTT broker. + * @return true: MQTT agent connected to an MQTT broker + * false: MQTT agent is not connected to an MQTT broker + */ +bool xIsMqttAgentConnected( void ); + +#endif /* EVENT_HELPER_H */ diff --git a/Projects/aws-iot-example/main.c b/Projects/aws-iot-example/main.c index 14122e91..3f676985 100644 --- a/Projects/aws-iot-example/main.c +++ b/Projects/aws-iot-example/main.c @@ -19,13 +19,14 @@ /* Kernel includes. */ #include "FreeRTOS.h" -#include "event_groups.h" #include "task.h" #include "mbedtls/threading.h" #include "mbedtls/platform.h" #include "threading_alt.h" +#include "event_helper.h" + /* Include header that defines log levels. */ #include "logging_levels.h" @@ -40,10 +41,6 @@ psa_key_handle_t xOTACodeVerifyKeyHandle = 0; -/* System events group. */ -EventGroupHandle_t xSystemEvents = NULL; -StaticEventGroup_t xSystemEventsGroup; - #ifdef INTEGRATION_TESTS extern void RunQualificationTest( void ); @@ -130,8 +127,8 @@ int main() LogInfo( ( "PSA Framework version is: %d\n", psa_framework_version() ) ); } - /* Create system events group. */ - xSystemEvents = xEventGroupCreateStatic( &xSystemEventsGroup ); + /* Initialise system events group. */ + vEventHelperInit(); /* Configure Mbed TLS memory APIs to use FreeRTOS heap APIs */ mbedtls_platform_set_calloc_free( mbedtls_platform_calloc, mbedtls_platform_free ); diff --git a/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt b/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt index cdc413ea..c42a9fd4 100644 --- a/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt +++ b/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt @@ -28,4 +28,5 @@ target_link_libraries(mqtt-agent-task iot-vsocket app-config app-logging + event-helper ) diff --git a/Projects/aws-iot-example/mqtt-agent-wrapper/event_helper.h b/Projects/aws-iot-example/mqtt-agent-wrapper/event_helper.h deleted file mode 100644 index af51901f..00000000 --- a/Projects/aws-iot-example/mqtt-agent-wrapper/event_helper.h +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright 2023 Arm Limited and/or its affiliates - * - * SPDX-License-Identifier: MIT - */ - -#ifndef EVENT_HELPER_H -#define EVENT_HELPER_H - -#include "event_groups.h" - -#define EVENT_MASK_MQTT_INIT 0x01 -#define EVENT_MASK_MQTT_CONNECTED 0x02 - -extern EventGroupHandle_t xSystemEvents; - -#endif /* EVENT_HELPER_H */ diff --git a/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c b/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c index a644134b..ff64c769 100644 --- a/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c +++ b/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c @@ -646,6 +646,8 @@ static void prvMQTTAgentTask( void * pParam ) ( void ) pParam; + vWaitUntilNetworkIsUp(); + /* Initialize the MQTT context with the buffer and transport interface. */ xMQTTStatus = prvMQTTInit(); @@ -763,35 +765,6 @@ static void prvMQTTAgentTask( void * pParam ) /*-----------------------------------------------------------*/ -void vWaitUntilMQTTAgentReady( void ) -{ - configASSERT( xSystemEvents != NULL ); - - EventBits_t uxEvents = xEventGroupWaitBits( xSystemEvents, - EVENT_MASK_MQTT_INIT, - pdFALSE, - pdFALSE, - portMAX_DELAY ); -} - -void vWaitUntilMQTTAgentConnected( void ) -{ - configASSERT( xSystemEvents != NULL ); - - EventBits_t uxEvents = xEventGroupWaitBits( xSystemEvents, - EVENT_MASK_MQTT_CONNECTED, - pdFALSE, - pdFALSE, - portMAX_DELAY ); -} - -bool xIsMqttAgentConnected( void ) -{ - EventBits_t uxEvents = xEventGroupGetBits( xSystemEvents ); - - return( ( bool ) ( uxEvents & EVENT_MASK_MQTT_CONNECTED ) ); -} - /* * @brief Create MQTT agent task. */ From 55bd162061a1cebd4a9ee1a060383d1a13d3fd8a Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Mon, 21 Aug 2023 08:14:15 +0000 Subject: [PATCH 05/13] fri: Add network stack selection feature Add a CMake variable `CONNECTIVITY_STACK`, so that the application can select a network stack from the supported network stacks. Signed-off-by: Devaraj Ranganna --- Middleware/ARM/CMakeLists.txt | 8 ++++++-- Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt | 2 ++ Projects/aws-iot-example/CMakeLists.txt | 11 +++++++---- .../aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Middleware/ARM/CMakeLists.txt b/Middleware/ARM/CMakeLists.txt index f5081c6e..02ae458e 100644 --- a/Middleware/ARM/CMakeLists.txt +++ b/Middleware/ARM/CMakeLists.txt @@ -4,7 +4,11 @@ add_subdirectory(TF-M) add_subdirectory(mbedtls-lib) -add_subdirectory(IoT_Socket-lib) -add_subdirectory(IoT_VSocket-lib) +if (DEFINED CONNECTIVITY_STACK) + if (${CONNECTIVITY_STACK} STREQUAL "IOT_VSOCKET") + add_subdirectory(IoT_Socket-lib) + add_subdirectory(IoT_VSocket-lib) + endif() +endif() add_subdirectory(freertos-pkcs11-psa-lib) add_subdirectory(freertos-ota-pal-psa-lib) diff --git a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt index 9612e1dc..ad4485b2 100644 --- a/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt +++ b/Middleware/ARM/IoT_VSocket-lib/CMakeLists.txt @@ -15,6 +15,8 @@ add_library(iot-vsocket STATIC transport_tls_iot_socket.c ) +add_library(connectivity-stack ALIAS iot-vsocket) + target_include_directories(iot-vsocket PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/Projects/aws-iot-example/CMakeLists.txt b/Projects/aws-iot-example/CMakeLists.txt index e39c3604..cf575602 100644 --- a/Projects/aws-iot-example/CMakeLists.txt +++ b/Projects/aws-iot-example/CMakeLists.txt @@ -80,11 +80,15 @@ else() endif() endif() +# Select connectivity stack. +set(CONNECTIVITY_STACK "IOT_VSOCKET" CACHE STRING "Choose the connectivity stack to use. Possible values are `FREERTOS_TCP_IP | IOT_VSOCKET`.") + set(MIDDLEWARE_DIR "../../Middleware") add_subdirectory(${MIDDLEWARE_DIR}/ARM ${CMAKE_BINARY_DIR}/Middleware/ARM) -add_subdirectory(../../Bsp ${CMAKE_BINARY_DIR}/Bsp) add_subdirectory(../../Config ${CMAKE_BINARY_DIR}/Config) +add_subdirectory(../../Bsp ${CMAKE_BINARY_DIR}/Bsp) + target_compile_definitions(arm-corstone-platform-bsp INTERFACE @@ -116,6 +120,7 @@ set( FREERTOS_HEAP "4" CACHE STRING "" FORCE) set( FREERTOS_PORT "GCC_ARM_CM55_TFM" CACHE STRING "" FORCE) add_subdirectory(${MIDDLEWARE_DIR}/FreeRTOS ${CMAKE_BINARY_DIR}/Middleware/FreeRTOS) +add_subdirectory(${MIDDLEWARE_DIR}/AWS ${CMAKE_BINARY_DIR}/Middleware/AWS) # integration tests include(${MIDDLEWARE_DIR}/FreeRTOS/FreeRTOS-Libraries-Integration-Tests/qualification_test.cmake) @@ -180,13 +185,11 @@ target_link_libraries(integration-test-task app-logging unity awsIoT - iot-vsocket + connectivity-stack mbedtls fri-bsp ) -add_subdirectory(${MIDDLEWARE_DIR}/AWS ${CMAKE_BINARY_DIR}/Middleware/AWS) - add_subdirectory(event-helper) add_subdirectory(mqtt-agent-wrapper) add_subdirectory(provisioning provisioning_data) diff --git a/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt b/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt index c42a9fd4..ebc400ee 100644 --- a/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt +++ b/Projects/aws-iot-example/mqtt-agent-wrapper/CMakeLists.txt @@ -25,8 +25,8 @@ target_link_libraries(mqtt-agent-task freertos_kernel freertos-ota-pal-psa awsIoT - iot-vsocket app-config app-logging event-helper + connectivity-stack ) From fa78eafb755c519266b28d97ba11972d42d3b36a Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Sun, 20 Aug 2023 10:44:51 +0000 Subject: [PATCH 06/13] kernel: Update FreeRTOS kernel to V10.6.1 Since we are integrating latest version of FreeRTOS TCP/IP stack which relies on FreeRTOS kernel, ensure that we are using the latest released version of FreeRTOS kernel which is `V10.6.1`. In addtion, fix a typo and remove unused macro from `FreeRTOSConfig.h`. Signed-off-by: Devaraj Ranganna --- Config/freertos-config/FreeRTOSConfig.h | 3 +-- Middleware/FreeRTOS/kernel | 2 +- manifest.yml | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Config/freertos-config/FreeRTOSConfig.h b/Config/freertos-config/FreeRTOSConfig.h index 453680c3..1388a44d 100644 --- a/Config/freertos-config/FreeRTOSConfig.h +++ b/Config/freertos-config/FreeRTOSConfig.h @@ -62,7 +62,7 @@ extern uint32_t SystemCoreClock; #define configTICK_RATE_HZ ( ( uint32_t ) 100 ) /* Scheduler polling rate of 1000 Hz */ #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) xTimeInMs ) -#define TICKS_TO_pdMS( xTics ) ( ( uint32_t ) xTics ) +#define TICKS_TO_pdMS( xTicks ) ( ( uint32_t ) xTicks ) #define configMINIMAL_STACK_SIZE 4096 #define configUSE_16_BIT_TICKS 0 @@ -72,7 +72,6 @@ extern uint32_t SystemCoreClock; #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 #define configUSE_TICKLESS_IDLE 0 #define configCPU_CLOCK_HZ ( ( unsigned long ) SystemCoreClock ) -#define configMS_TO_RTOS_TICK( ms ) ( ms ) /* Tick rate is 1000 Hz, so 1 tick is 1 ms */ #define configMAX_PRIORITIES 56 #define configMAX_TASK_NAME_LEN 16 #define configIDLE_SHOULD_YIELD 1 diff --git a/Middleware/FreeRTOS/kernel b/Middleware/FreeRTOS/kernel index d4d5e432..02642802 160000 --- a/Middleware/FreeRTOS/kernel +++ b/Middleware/FreeRTOS/kernel @@ -1 +1 @@ -Subproject commit d4d5e43292a57df354dcf8f33d938283f2082855 +Subproject commit 0264280230aa6a828247b5f05bf57e33f1994581 diff --git a/manifest.yml b/manifest.yml index 06e4a5a2..cd256fcd 100644 --- a/manifest.yml +++ b/manifest.yml @@ -5,7 +5,7 @@ description: |- dependencies: - name: "FreeRTOS-Kernel" license: "MIT" - version: "d4d5e43292a57df354dcf8f33d938283f2082855" + version: "V10.6.1" repository: type: "git" url: "https://github.com/FreeRTOS/FreeRTOS-Kernel.git" From 686027ceb76c6cc9cb084ca23be19f5dc914eadc Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Tue, 12 Sep 2023 13:25:20 +0000 Subject: [PATCH 07/13] fri: Add FreeRTOS TCP/IP as git submodule Signed-off-by: Devaraj Ranganna --- .gitmodules | 3 +++ .../FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP | 1 + manifest.yml | 7 +++++++ 3 files changed, 11 insertions(+) create mode 160000 Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP diff --git a/.gitmodules b/.gitmodules index aff77a3a..2dfe7c5d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -52,3 +52,6 @@ [submodule "Middleware/Unity"] path = Middleware/Unity url = https://github.com/ThrowTheSwitch/Unity.git +[submodule "Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP"] + path = Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP + url = https://github.com/FreeRTOS/FreeRTOS-Plus-TCP.git diff --git a/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP new file mode 160000 index 00000000..3d5ee0e8 --- /dev/null +++ b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP @@ -0,0 +1 @@ +Subproject commit 3d5ee0e821cab38cb6e6265fcf1ce7552a54519d diff --git a/manifest.yml b/manifest.yml index cd256fcd..ef35671b 100644 --- a/manifest.yml +++ b/manifest.yml @@ -136,3 +136,10 @@ dependencies: type: "git" url: "https://github.com/ThrowTheSwitch/Unity.git" path: "Middleware/Unity" + - name: "FreeRTOS-Plus-TCP" + license: "MIT" + version: "3d5ee0e821cab38cb6e6265fcf1ce7552a54519d" + repository: + type: "git" + url: "https://github.com/FreeRTOS/FreeRTOS-Plus-TCP.git" + path: "Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/FreeRTOS-Plus-TCP" From 8dffe6b4f12a13b9a1b6ec42329af13c2a7438e1 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Mon, 21 Aug 2023 09:53:07 +0000 Subject: [PATCH 08/13] fri: Add support for FreeRTOS TCP/IP stack Added support for FreeRTOS TCP/IP stack and made it the default choice of network stack. Signed-off-by: Devaraj Ranganna --- Config/freertos-config/FreeRTOSIPConfig.h | 316 ++++++++++++++++ Middleware/FreeRTOS/CMakeLists.txt | 6 + .../FreeRTOS-Plus-TCP-lib/CMakeLists.txt | 33 ++ .../src/network_startup.c | 127 +++++++ .../src/transport_interface_api.h | 186 ++++++++++ .../src/transport_mbedtls.c | 342 ++++++++++++++++++ Projects/aws-iot-example/CMakeLists.txt | 2 +- Projects/aws-iot-example/main.c | 41 +++ .../mqtt-agent-wrapper/mqtt_agent_task.c | 8 +- 9 files changed, 1053 insertions(+), 8 deletions(-) create mode 100644 Config/freertos-config/FreeRTOSIPConfig.h create mode 100644 Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/CMakeLists.txt create mode 100644 Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/network_startup.c create mode 100644 Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_interface_api.h create mode 100644 Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_mbedtls.c diff --git a/Config/freertos-config/FreeRTOSIPConfig.h b/Config/freertos-config/FreeRTOSIPConfig.h new file mode 100644 index 00000000..4e1f9238 --- /dev/null +++ b/Config/freertos-config/FreeRTOSIPConfig.h @@ -0,0 +1,316 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + + +/***************************************************************************** +* +* See the following URL for configuration information. +* https://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_IP_Configuration.html +* +*****************************************************************************/ + +#ifndef FREERTOS_IP_CONFIG_H +#define FREERTOS_IP_CONFIG_H + +/* Prototype for the function used to print out. In this case it prints to the + * console before the network is connected then a UDP port after the network has + * connected. */ +extern void vLoggingPrintf( const char * pcFormatString, + ... ); + +#ifdef HEAP3 + #define xPortGetMinimumEverFreeHeapSize( x ) 0 + #define xPortGetFreeHeapSize() 0 +#endif + +/* Set to 1 to print out debug messages. If ipconfigHAS_DEBUG_PRINTF is set to + * 1 then FreeRTOS_debug_printf should be defined to the function used to print + * out the debugging messages. */ +#define ipconfigHAS_DEBUG_PRINTF 0 +#if ( ipconfigHAS_DEBUG_PRINTF == 1 ) + #define FreeRTOS_debug_printf( X ) vLoggingPrintf X +#endif + +/* Set to 1 to print out non debugging messages, for example the output of the + * FreeRTOS_netstat() command, and ping replies. If ipconfigHAS_PRINTF is set to 1 + * then FreeRTOS_printf should be set to the function used to print out the + * messages. */ +#define ipconfigHAS_PRINTF 1 +#if ( ipconfigHAS_PRINTF == 1 ) + #define FreeRTOS_printf( X ) vLoggingPrintf X +#endif + +/* Define the byte order of the target MCU (the MCU FreeRTOS+TCP is executing + * on). Valid options are pdFREERTOS_BIG_ENDIAN and pdFREERTOS_LITTLE_ENDIAN. */ +#define ipconfigBYTE_ORDER pdFREERTOS_LITTLE_ENDIAN + +/* If the network card/driver includes checksum offloading (IP/TCP/UDP checksums) + * then set ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM to 1 to prevent the software + * stack repeating the checksum calculations. */ +#define ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM 1 + +/* Several API's will block until the result is known, or the action has been + * performed, for example FreeRTOS_send() and FreeRTOS_recv(). The timeouts can be + * set per socket, using setsockopt(). If not set, the times below will be + * used as defaults. */ +#define ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME ( 100 ) +#define ipconfigSOCK_DEFAULT_SEND_BLOCK_TIME ( 100 ) + +/* Include support for LLMNR: Link-local Multicast Name Resolution + * (non-Microsoft) */ +#define ipconfigUSE_LLMNR ( 0 ) + +/* Include support for NBNS: NetBIOS Name Service (Microsoft) */ +#define ipconfigUSE_NBNS ( 0 ) + +/* Include support for DNS caching. For TCP, having a small DNS cache is very + * useful. When a cache is present, ipconfigDNS_REQUEST_ATTEMPTS can be kept low + * and also DNS may use small timeouts. If a DNS reply comes in after the DNS + * socket has been destroyed, the result will be stored into the cache. The next + * call to FreeRTOS_gethostbyname() will return immediately, without even creating + * a socket. */ +#define ipconfigUSE_DNS_CACHE ( 1 ) +#define ipconfigDNS_CACHE_NAME_LENGTH ( 254 ) +#define ipconfigDNS_CACHE_ENTRIES ( 4 ) +#define ipconfigDNS_REQUEST_ATTEMPTS ( 2 ) + +/* The IP stack executes it its own task (although any application task can make + * use of its services through the published sockets API). ipconfigUDP_TASK_PRIORITY + * sets the priority of the task that executes the IP stack. The priority is a + * standard FreeRTOS task priority so can take any value from 0 (the lowest + * priority) to (configMAX_PRIORITIES - 1) (the highest priority). + * configMAX_PRIORITIES is a standard FreeRTOS configuration parameter defined in + * FreeRTOSConfig.h, not FreeRTOSIPConfig.h. Consideration needs to be given as to + * the priority assigned to the task executing the IP stack relative to the + * priority assigned to tasks that use the IP stack. */ +#define ipconfigIP_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) + +/* The size, in words (not bytes), of the stack allocated to the FreeRTOS+TCP + * task. This setting is less important when the FreeRTOS Win32 simulator is used + * as the Win32 simulator only stores a fixed amount of information on the task + * stack. FreeRTOS includes optional stack overflow detection, see: + * http://www.freertos.org/Stacks-and-stack-overflow-checking.html */ +#define ipconfigIP_TASK_STACK_SIZE_WORDS ( configMINIMAL_STACK_SIZE * 5 ) + +/* ipconfigRAND32() is called by the IP stack to generate random numbers for + * things such as a DHCP transaction number or initial sequence number. Random + * number generation is performed via this macro to allow applications to use their + * own random number generation method. For example, it might be possible to + * generate a random number by sampling noise on an analogue input. */ +/* #define ipconfigRAND32() uxRand() */ + +/* If ipconfigUSE_NETWORK_EVENT_HOOK is set to 1 then FreeRTOS+TCP will call the + * network event hook at the appropriate times. If ipconfigUSE_NETWORK_EVENT_HOOK + * is not set to 1 then the network event hook will never be called. See + * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/API/vApplicationIPNetworkEventHook.shtml + */ +#define ipconfigUSE_NETWORK_EVENT_HOOK 1 + +/* Sockets have a send block time attribute. If FreeRTOS_sendto() is called but + * a network buffer cannot be obtained then the calling task is held in the Blocked + * state (so other tasks can continue to executed) until either a network buffer + * becomes available or the send block time expires. If the send block time expires + * then the send operation is aborted. The maximum allowable send block time is + * capped to the value set by ipconfigMAX_SEND_BLOCK_TIME_TICKS. Capping the + * maximum allowable send block time prevents prevents a deadlock occurring when + * all the network buffers are in use and the tasks that process (and subsequently + * free) the network buffers are themselves blocked waiting for a network buffer. + * ipconfigMAX_SEND_BLOCK_TIME_TICKS is specified in RTOS ticks. A time in + * milliseconds can be converted to a time in ticks by dividing the time in + * milliseconds by portTICK_PERIOD_MS. */ +#define ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS ( 5000U ) + +/* If ipconfigUSE_DHCP is 1 then FreeRTOS+TCP will attempt to retrieve an IP + * address, netmask, DNS server address and gateway address from a DHCP server. If + * ipconfigUSE_DHCP is 0 then FreeRTOS+TCP will use a static IP address. The + * stack will revert to using the static IP address even when ipconfigUSE_DHCP is + * set to 1 if a valid configuration cannot be obtained from a DHCP server for any + * reason. The static configuration used is that passed into the stack by the + * FreeRTOS_IPInit() function call. */ +#define ipconfigUSE_DHCP 1 +#define ipconfigUSE_DHCP_HOOK 0 + +/* When ipconfigUSE_DHCP is set to 1, DHCP requests will be sent out at + * increasing time intervals until either a reply is received from a DHCP server + * and accepted, or the interval between transmissions reaches + * ipconfigMAXIMUM_DISCOVER_TX_PERIOD. The IP stack will revert to using the + * static IP address passed as a parameter to FreeRTOS_IPInit() if the + * re-transmission time interval reaches ipconfigMAXIMUM_DISCOVER_TX_PERIOD without + * a DHCP reply being received. */ +#define ipconfigMAXIMUM_DISCOVER_TX_PERIOD ( 120000U ) + +/* The ARP cache is a table that maps IP addresses to MAC addresses. The IP + * stack can only send a UDP message to a remove IP address if it knowns the MAC + * address associated with the IP address, or the MAC address of the router used to + * contact the remote IP address. When a UDP message is received from a remote IP + * address the MAC address and IP address are added to the ARP cache. When a UDP + * message is sent to a remote IP address that does not already appear in the ARP + * cache then the UDP message is replaced by a ARP message that solicits the + * required MAC address information. ipconfigARP_CACHE_ENTRIES defines the maximum + * number of entries that can exist in the ARP table at any one time. */ +#define ipconfigARP_CACHE_ENTRIES 6 + +/* ARP requests that do not result in an ARP response will be re-transmitted a + * maximum of ipconfigMAX_ARP_RETRANSMISSIONS times before the ARP request is + * aborted. */ +#define ipconfigMAX_ARP_RETRANSMISSIONS ( 5 ) + +/* ipconfigMAX_ARP_AGE defines the maximum time between an entry in the ARP + * table being created or refreshed and the entry being removed because it is stale. + * New ARP requests are sent for ARP cache entries that are nearing their maximum + * age. ipconfigMAX_ARP_AGE is specified in tens of seconds, so a value of 150 is + * equal to 1500 seconds (or 25 minutes). */ +#define ipconfigMAX_ARP_AGE 150 + +/* Implementing FreeRTOS_inet_addr() necessitates the use of string handling + * routines, which are relatively large. To save code space the full + * FreeRTOS_inet_addr() implementation is made optional, and a smaller and faster + * alternative called FreeRTOS_inet_addr_quick() is provided. FreeRTOS_inet_addr() + * takes an IP in decimal dot format (for example, "192.168.0.1") as its parameter. + * FreeRTOS_inet_addr_quick() takes an IP address as four separate numerical octets + * (for example, 192, 168, 0, 1) as its parameters. If + * ipconfigINCLUDE_FULL_INET_ADDR is set to 1 then both FreeRTOS_inet_addr() and + * FreeRTOS_indet_addr_quick() are available. If ipconfigINCLUDE_FULL_INET_ADDR is + * not set to 1 then only FreeRTOS_indet_addr_quick() is available. */ +#define ipconfigINCLUDE_FULL_INET_ADDR 1 + +/* ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS defines the total number of network buffer that + * are available to the IP stack. The total number of network buffers is limited + * to ensure the total amount of RAM that can be consumed by the IP stack is capped + * to a pre-determinable value. */ +#define ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS 60 + +/* A FreeRTOS queue is used to send events from application tasks to the IP + * stack. ipconfigEVENT_QUEUE_LENGTH sets the maximum number of events that can + * be queued for processing at any one time. The event queue must be a minimum of + * 5 greater than the total number of network buffers. */ +#define ipconfigEVENT_QUEUE_LENGTH ( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS + 5 ) + +/* The address of a socket is the combination of its IP address and its port + * number. FreeRTOS_bind() is used to manually allocate a port number to a socket + * (to 'bind' the socket to a port), but manual binding is not normally necessary + * for client sockets (those sockets that initiate outgoing connections rather than + * wait for incoming connections on a known port number). If + * ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 1 then calling + * FreeRTOS_sendto() on a socket that has not yet been bound will result in the IP + * stack automatically binding the socket to a port number from the range + * socketAUTO_PORT_ALLOCATION_START_NUMBER to 0xffff. If + * ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 0 then calling FreeRTOS_sendto() + * on a socket that has not yet been bound will result in the send operation being + * aborted. */ +#define ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND 1 + +/* Defines the Time To Live (TTL) values used in outgoing UDP packets. */ +#define ipconfigUDP_TIME_TO_LIVE 128 +#define ipconfigTCP_TIME_TO_LIVE 128 /* also defined in FreeRTOSIPConfigDefaults.h */ + +/* USE_TCP: Use TCP and all its features */ +#define ipconfigUSE_TCP ( 1 ) + +/* USE_WIN: Let TCP use windowing mechanism. */ +#define ipconfigUSE_TCP_WIN ( 1 ) + +/* The MTU is the maximum number of bytes the payload of a network frame can + * contain. For normal Ethernet V2 frames the maximum MTU is 1500. Setting a + * lower value can save RAM, depending on the buffer management scheme used. If + * ipconfigCAN_FRAGMENT_OUTGOING_PACKETS is 1 then (ipconfigNETWORK_MTU - 28) must + * be divisible by 8. */ +#define ipconfigNETWORK_MTU 1500U + +/* Set ipconfigUSE_DNS to 1 to include a basic DNS client/resolver. DNS is used + * through the FreeRTOS_gethostbyname() API function. */ +#define ipconfigUSE_DNS 1 + +/* If ipconfigREPLY_TO_INCOMING_PINGS is set to 1 then the IP stack will + * generate replies to incoming ICMP echo (ping) requests. */ +#define ipconfigREPLY_TO_INCOMING_PINGS 1 + +/* If ipconfigSUPPORT_OUTGOING_PINGS is set to 1 then the + * FreeRTOS_SendPingRequest() API function is available. */ +#define ipconfigSUPPORT_OUTGOING_PINGS 0 + +/* If ipconfigSUPPORT_SELECT_FUNCTION is set to 1 then the FreeRTOS_select() + * (and associated) API function is available. */ +#define ipconfigSUPPORT_SELECT_FUNCTION 0 + +/* If ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES is set to 1 then Ethernet frames + * that are not in Ethernet II format will be dropped. This option is included for + * potential future IP stack developments. */ +#define ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES 1 + +/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1 then it is the + * responsibility of the Ethernet interface to filter out packets that are of no + * interest. If the Ethernet interface does not implement this functionality, then + * set ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES to 0 to have the IP stack + * perform the filtering instead (it is much less efficient for the stack to do it + * because the packet will already have been passed into the stack). If the + * Ethernet driver does all the necessary filtering in hardware then software + * filtering can be removed by using a value other than 1 or 0. */ +#define ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES 1 + +/* Advanced only: in order to access 32-bit fields in the IP packets with + * 32-bit memory instructions, all packets will be stored 32-bit-aligned, plus 16-bits. + * This has to do with the contents of the IP-packets: all 32-bit fields are + * 32-bit-aligned, plus 16-bit(!) */ +#define ipconfigPACKET_FILLER_SIZE 2U + +/* Define the size of the pool of TCP window descriptors. On the average, each + * TCP socket will use up to 2 x 6 descriptors, meaning that it can have 2 x 6 + * outstanding packets (for Rx and Tx). When using up to 10 TP sockets + * simultaneously, one could define TCP_WIN_SEG_COUNT as 120. */ +#define ipconfigTCP_WIN_SEG_COUNT 240 + +/* Each TCP socket has a circular buffers for Rx and Tx, which have a fixed + * maximum size. Define the size of Rx buffer for TCP sockets. */ +#define ipconfigTCP_RX_BUFFER_LENGTH ( 10000 ) + +/* Define the size of Tx buffer for TCP sockets. */ +#define ipconfigTCP_TX_BUFFER_LENGTH ( 10000 ) + +/* When using call-back handlers, the driver may check if the handler points to + * real program memory (RAM or flash) or just has a random non-zero value. */ +#define ipconfigIS_VALID_PROG_ADDRESS( x ) ( ( x ) != NULL ) + +/* Include support for TCP hang protection. All sockets in a connecting or + * disconnecting stage will timeout after a period of non-activity. */ +#define ipconfigTCP_HANG_PROTECTION ( 1 ) +#define ipconfigTCP_HANG_PROTECTION_TIME ( 30 ) + +/* Include support for TCP keep-alive messages. */ +#define ipconfigTCP_KEEP_ALIVE ( 1 ) +#define ipconfigTCP_KEEP_ALIVE_INTERVAL ( 20 ) /* in seconds */ + +/* Include all API's and code that is needed for the IPv4 protocol. + * When defined as zero, the application should uses IPv6. */ +#define ipconfigUSE_IPv4 ( 1 ) + +/* + * If defined this macro enables the APIs that are backward compatible + * with single end point IPv4 version of the FreeRTOS+TCP library. + */ +#define ipconfigIPv4_BACKWARD_COMPATIBLE 0 + +#endif /* FREERTOS_IP_CONFIG_H */ diff --git a/Middleware/FreeRTOS/CMakeLists.txt b/Middleware/FreeRTOS/CMakeLists.txt index c7610727..0488e22e 100644 --- a/Middleware/FreeRTOS/CMakeLists.txt +++ b/Middleware/FreeRTOS/CMakeLists.txt @@ -3,3 +3,9 @@ # SPDX-License-Identifier: MIT add_subdirectory(kernel) + +if (DEFINED CONNECTIVITY_STACK) + if (${CONNECTIVITY_STACK} STREQUAL "FREERTOS_TCP_IP") + add_subdirectory(FreeRTOS-Plus-TCP-lib) + endif() +endif() diff --git a/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/CMakeLists.txt b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/CMakeLists.txt new file mode 100644 index 00000000..bc951532 --- /dev/null +++ b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/CMakeLists.txt @@ -0,0 +1,33 @@ +# Copyright 2023 Arm Limited and/or its affiliates +# +# SPDX-License-Identifier: MIT + +set(FREERTOS_PLUS_TCP_NETWORK_IF "MPS3_AN552" CACHE STRING "FreeRTOS Plus TCP Network Interface selection") + +set(FREERTOS_PLUS_TCP_BUFFER_ALLOCATION "2" CACHE STRING "FreeRTOS buffer allocation model number. 1 .. 2.") + +add_subdirectory(FreeRTOS-Plus-TCP/source) +add_subdirectory(FreeRTOS-Plus-TCP/tools) + +add_library(connectivity-stack ALIAS freertos_plus_tcp) + +target_sources( freertos_plus_tcp + PRIVATE + src/network_startup.c + src/transport_mbedtls.c +) + +target_include_directories( freertos_plus_tcp SYSTEM + PUBLIC + src +) + +target_link_libraries(freertos_plus_tcp + PRIVATE + app-logging + freertos_kernel + fri-bsp + awsIoT + mbedtls-helpers + event-helper +) diff --git a/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/network_startup.c b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/network_startup.c new file mode 100644 index 00000000..081664c2 --- /dev/null +++ b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/network_startup.c @@ -0,0 +1,127 @@ +/* Copyright 2023 Arm Limited and/or its affiliates + * + * SPDX-License-Identifier: MIT + */ + +#include + +/* FreeRTOS includes. */ +#include +#include "FreeRTOS_IP.h" + +/* System events helper header. */ +#include "event_helper.h" + +/* Include header that defines log levels. */ +#include "logging_levels.h" + +/* Configure name and log level. */ +#ifndef LIBRARY_LOG_NAME + #define LIBRARY_LOG_NAME "NW_STARTUP" +#endif +#ifndef LIBRARY_LOG_LEVEL + #define LIBRARY_LOG_LEVEL LOG_INFO +#endif +#include "logging_stack.h" + +/* The default IP and MAC address used by the demo. The address configuration + * defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is + * 1 but a DHCP server could not be contacted. See the online documentation for + * more information. */ +static const uint8_t ucIPAddress[ 4 ] = +{ + configIP_ADDR0, + configIP_ADDR1, + configIP_ADDR2, + configIP_ADDR3 +}; +static const uint8_t ucNetMask[ 4 ] = +{ + configNET_MASK0, + configNET_MASK1, + configNET_MASK2, + configNET_MASK3 +}; +static const uint8_t ucGatewayAddress[ 4 ] = +{ + configGATEWAY_ADDR0, + configGATEWAY_ADDR1, + configGATEWAY_ADDR2, + configGATEWAY_ADDR3 +}; +static const uint8_t ucDNSServerAddress[ 4 ] = +{ + configDNS_SERVER_ADDR0, + configDNS_SERVER_ADDR1, + configDNS_SERVER_ADDR2, + configDNS_SERVER_ADDR3 +}; +const uint8_t ucMACAddress[ 6 ] = +{ + configMAC_ADDR0, + configMAC_ADDR1, + configMAC_ADDR2, + configMAC_ADDR3, + configMAC_ADDR4, + configMAC_ADDR5 +}; + +#if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) + +/* In case multiple interfaces are used, define them statically. */ + +/* There is only 1 physical interface. */ + static NetworkInterface_t xInterfaces[ 1 ]; + +/* It will have several end-points. */ + static NetworkEndPoint_t xEndPoints[ 4 ]; + +#endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */ + +int32_t network_startup( void ) +{ + #if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) + /* Initialise the interface descriptor for WinPCap. */ + pxLAN91C111_FillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) ); + + /* === End-point 0 === */ + FreeRTOS_FillEndPoint( &( xInterfaces[ 0 ] ), &( xEndPoints[ 0 ] ), ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress ); + #if ( ipconfigUSE_DHCP != 0 ) + { + /* End-point 0 wants to use DHCPv4. */ + xEndPoints[ 0 ].bits.bWantDHCP = pdTRUE; + } + #endif /* ( ipconfigUSE_DHCP != 0 ) */ + + memcpy( ipLOCAL_MAC_ADDRESS, ucMACAddress, sizeof( ucMACAddress ) ); + + FreeRTOS_IPInit_Multi(); + #else /* if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */ + /* Using the old /single /IPv4 library, or using backward compatible mode of the new /multi library. */ + FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress ); + #endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */ + + return 0; +} + +/* Called by FreeRTOS+TCP when the network connects or disconnects. Disconnect + * events are only received if implemented in the MAC driver. */ +#if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) + void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent, + struct xNetworkEndPoint * pxEndPoint ) +#else + void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent ) +#endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */ +{ + /* If the network has just come up...*/ + if( eNetworkEvent == eNetworkUp ) + { + LogInfo( ( "Network is up" ) ); + ( void ) xEventGroupSetBits( xSystemEvents, EVENT_MASK_NETWORK_UP ); + } + else + { + LogInfo( ( "Network is down" ) ); + ( void ) xEventGroupClearBits( xSystemEvents, EVENT_MASK_NETWORK_UP ); + } +} diff --git a/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_interface_api.h b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_interface_api.h new file mode 100644 index 00000000..a0b8edd7 --- /dev/null +++ b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_interface_api.h @@ -0,0 +1,186 @@ +/* + * FreeRTOS Transport Secure Sockets V1.0.1 + * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://aws.amazon.com/freertos + * http://www.FreeRTOS.org + */ + +/** + * @file transport_sockets.h + * @brief Socket based API for transport interface implementation. + */ + +#ifndef TRANSPORT_INTERFACE_API_H +#define TRANSPORT_INTERFACE_API_H + +#include +#include +#include "transport_interface.h" + +/* FreeRTOS+TCP includes. */ +#include "FreeRTOS_IP.h" +#include "FreeRTOS_Sockets.h" +#include "FreeRTOS_DNS.h" + + +struct NetworkContext +{ + Socket_t socket; + void * pTLSContext; + bool useTLS; +}; + +/** + * @brief TCP, TLS Connect / Disconnect return status. + */ +typedef enum TransportStatus +{ + TRANSPORT_STATUS_SUCCESS = 0, /**< Function successfully completed. */ + TRANSPORT_STATUS_INVALID_PARAMETER, /**< At least one parameter was invalid. */ + TRANSPORT_STATUS_INSUFFICIENT_MEMORY, /**< Insufficient memory required to establish connection. */ + TRANSPORT_STATUS_CREDENTIALS_INVALID, /**< Provided credentials were invalid. */ + TRANSPORT_STATUS_INTERNAL_ERROR, /**< A call to a system API resulted in an internal error. */ + TRANSPORT_STATUS_DNS_FAILURE, /**< Resolving hostname of the server failed. */ + TRANSPORT_STATUS_SOCKET_CREATE_FAILURE, /**< Underlying socket creation failed. */ + TRANSPORT_STATUS_CONNECT_FAILURE, /**< Initial connection to the server failed. */ + TRANSPORT_STATUS_TLS_FAILURE, /**< TLS Handshake for the secure connection failed. */ + TRANSPORT_STATUS_SOCKET_CLOSE_FAILURE /**< Failed to close the underlying socket. */ +} TransportStatus_t; + + +/** + * @brief Information on the remote server for connection setup. + */ +typedef struct ServerInfo +{ + const char * pHostName; /**< @brief Server host name. Must be NULL-terminated. */ + size_t hostNameLength; /**< @brief Length of the server host name. */ + uint16_t port; /**< @brief Server port in host-order. */ +} ServerInfo_t; + + +/** + * @brief Contains the credentials necessary for connection setup. + * + */ +typedef struct TLSParams +{ + /** + * @brief Set this to a non-NULL value to use ALPN. + * + * This string must be NULL-terminated. + * + * See [this link] + * (https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/) + * for more information. + */ + const char * pAlpnProtos; + + /** + * @brief Disable server name indication (SNI) for a TLS session. + */ + bool disableSni; + + /** + * @brief Set this to a non-zero value to use TLS max fragment length + * negotiation (TLS MFLN). + * + * @note The network stack may have a minimum value for this parameter and + * may return an error if this parameter is too small. + */ + size_t maxFragmentLength; + + const char * pRootCa; /**< @brief String representing a trusted server Root CA certificate. */ + size_t rootCaSize; /**< @brief SizeSecureSocketsTransport_Connect associated with #SocketsConfig_t.pRootCa. */ + + char * pPrivateKeyLabel; + char * pClientCertLabel; + char * pLoginPIN; +} TLSParams_t; + + +/** + * @brief Sets up a TCP only connection or a TLS session on top of a TCP connection with Secure Sockets API. + * + * @param[out] pNetworkContext The output parameter to return the created network context. + * @param[in] pServerInfo Server connection info. + * @param[in] pTLSParams socket configs for the connection. + * @param[in] sendTimeoutMs socket send timeout. + * @param[in] recvTimeoutMs socket receive timeout. + * + * @return #TRANSPORT_STATUS_SUCCESS on success; + * #TRANSPORT_STATUS_INVALID_PARAMETER, #TRANSPORT_STATUS_INSUFFICIENT_MEMORY, + * #TRANSPORT_STATUS_CREDENTIALS_INVALID, #TRANSPORT_STATUS_INTERNAL_ERROR, + * #TRANSPORT_STATUS_DNS_FAILURE, #TRANSPORT_STATUS_CONNECT_FAILURE on failure. + */ +TransportStatus_t Transport_Connect( NetworkContext_t * pNetworkContext, + const ServerInfo_t * pServerInfo, + const TLSParams_t * pTLSParams, + uint32_t sendTimeoutMs, + uint32_t recvTimeoutMs ); + +/** + * @brief Closes a TLS session on top of a TCP connection using the Secure Sockets API. + * + * @param[out] pNetworkContext The output parameter to end the TLS session and + * clean the created network context. + * + * @return #TRANSPORT_STATUS_SUCCESS on success; + * #TRANSPORT_STATUS_INVALID_PARAMETER, #TRANSPORT_STATUS_INTERNAL_ERROR on failure. + */ +TransportStatus_t Transport_Disconnect( NetworkContext_t * pNetworkContext ); + + +/** + * @brief Receives data over an established TLS session using the Secure Sockets API. + * + * This can be used as #TransportInterface.recv function for receiving data + * from the network. + * + * @param[in] pNetworkContext The network context created using Socket_Connect API. + * @param[out] pBuffer Buffer to receive network data into. + * @param[in] bytesToRecv Number of bytes requested from the network. + * + * @return Number of bytes (> 0) received if successful; + * 0 if the socket times out without reading any bytes; + * negative value on error. + */ +int32_t Transport_Recv( NetworkContext_t * pNetworkContext, + void * pBuffer, + size_t bytesToRecv ); + +/** + * @brief Sends data over an established TLS session using the Secure Sockets API. + * + * This can be used as the #TransportInterface.send function to send data + * over the network. + * + * @param[in] pNetworkContext The network context created using Secure Sockets API. + * @param[in] pMessage A message to be sent over the network stack. + * @param[in] bytesToSend Number of bytes to send over the network. + * + * @return Number of bytes sent if successful; negative value on error. + */ +int32_t Transport_Send( NetworkContext_t * pNetworkContext, + const void * pMessage, + size_t bytesToSend ); + +#endif /* TRANSPORT_INTERFACE_API_H */ diff --git a/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_mbedtls.c b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_mbedtls.c new file mode 100644 index 00000000..e7763135 --- /dev/null +++ b/Middleware/FreeRTOS/FreeRTOS-Plus-TCP-lib/src/transport_mbedtls.c @@ -0,0 +1,342 @@ +/* + * FreeRTOS V202212.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +/** + * @file tls_freertos.c + * @brief TLS transport interface implementations. This implementation uses + * mbedTLS. + */ + +#include "logging_levels.h" + +#define LIBRARY_LOG_NAME "TRANSPORT TLS" +#define LIBRARY_LOG_LEVEL LOG_INFO + +#include "logging_stack.h" + +/* Standard includes. */ +#include + +/* FreeRTOS includes. */ +#include "FreeRTOS.h" + +/* Transport header. */ +#include "transport_interface_api.h" + +/* TLS helper header. */ +#include "tls_helper.h" + +static int Recv_Cb( void * pvCallerContext, + unsigned char * pucReceiveBuffer, + size_t xReceiveLength ); +static int Send_Cb( void * pvCallerContext, + const unsigned char * pucData, + size_t xDataLength ); + +TransportStatus_t Transport_Connect( NetworkContext_t * pNetworkContext, + const ServerInfo_t * pServerInfo, + const TLSParams_t * pTLSParams, + uint32_t sendTimeoutMs, + uint32_t recvTimeoutMs ) +{ + TransportStatus_t status = TRANSPORT_STATUS_SUCCESS; + int32_t socketStatus = 0; + struct freertos_sockaddr serverAddress = { 0 }; + TLSHelperParams_t tlsHelperParams = { 0 }; + TickType_t transportTimeout = 0; + + if( ( pNetworkContext == NULL ) || ( pServerInfo == NULL ) ) + { + status = TRANSPORT_STATUS_INVALID_PARAMETER; + } + else if( pServerInfo->pHostName == NULL ) + { + status = TRANSPORT_STATUS_INVALID_PARAMETER; + } + else + { + /* Create a TCP socket. */ + pNetworkContext->socket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP ); + + if( pNetworkContext->socket == FREERTOS_INVALID_SOCKET ) + { + LogError( ( "Failed to create new socket." ) ); + status = TRANSPORT_STATUS_SOCKET_CREATE_FAILURE; + } + + /* Resolve the DNS name and get the IP address of the host. */ + if( status == TRANSPORT_STATUS_SUCCESS ) + { + serverAddress.sin_family = FREERTOS_AF_INET; + serverAddress.sin_port = FreeRTOS_htons( pServerInfo->port ); + serverAddress.sin_len = ( uint8_t ) sizeof( serverAddress ); + + LogInfo( ( "Resolving host name: %s.", pServerInfo->pHostName ) ); + #if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) + serverAddress.sin_address.ulIP_IPv4 = ( uint32_t ) FreeRTOS_gethostbyname( pServerInfo->pHostName ); + + /* Check for errors from DNS lookup. */ + if( serverAddress.sin_address.ulIP_IPv4 == 0U ) + #else + serverAddress.sin_addr = ( uint32_t ) FreeRTOS_gethostbyname( pServerInfo->pHostName ); + + /* Check for errors from DNS lookup. */ + if( serverAddress.sin_addr == 0U ) + #endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */ + { + LogError( ( "Failed to connect to server: DNS resolution failed: Hostname=%s.", + pServerInfo->pHostName ) ); + status = TRANSPORT_STATUS_DNS_FAILURE; + } + } + + /* Create a TCP connection to the host. */ + if( status == TRANSPORT_STATUS_SUCCESS ) + { + LogInfo( ( "Initiating TCP connection with host: %s:%d\r\n", pServerInfo->pHostName, pServerInfo->port ) ); + socketStatus = FreeRTOS_connect( pNetworkContext->socket, &serverAddress, sizeof( serverAddress ) ); + + if( socketStatus < 0 ) + { + LogError( ( "Failed to connect to server: FreeRTOS_Connect failed: ReturnCode=%d," + " Hostname=%s, Port=%u.", + socketStatus, + pServerInfo->pHostName, + pServerInfo->port ) ); + status = TRANSPORT_STATUS_CONNECT_FAILURE; + } + } + + /* IF this is a secure TLS connection, perform the TLS handshake. */ + if( ( status == TRANSPORT_STATUS_SUCCESS ) && ( pTLSParams != NULL ) ) + { + /* Initialize TLS */ + tlsHelperParams.pcDestination = pServerInfo->pHostName; + tlsHelperParams.pcServerCertificate = pTLSParams->pRootCa; + tlsHelperParams.ulServerCertificateLength = pTLSParams->rootCaSize; + tlsHelperParams.pvCallerContext = pNetworkContext; + tlsHelperParams.pxNetworkRecv = &Recv_Cb; + tlsHelperParams.pxNetworkSend = &Send_Cb; + tlsHelperParams.pPrivateKeyLabel = pTLSParams->pPrivateKeyLabel; + tlsHelperParams.pClientCertLabel = pTLSParams->pClientCertLabel; + tlsHelperParams.pcLoginPIN = pTLSParams->pLoginPIN; + + + pNetworkContext->pTLSContext = pvPortMalloc( sizeof( TLSContext_t ) ); + + if( pNetworkContext->pTLSContext != NULL ) + { + /* Set socket receive timeout. */ + transportTimeout = pdMS_TO_TICKS( recvTimeoutMs ); + /* Setting the receive block time cannot fail. */ + ( void ) FreeRTOS_setsockopt( pNetworkContext->socket, + 0, + FREERTOS_SO_RCVTIMEO, + &transportTimeout, + sizeof( TickType_t ) ); + + /* Set socket send timeout. */ + transportTimeout = pdMS_TO_TICKS( sendTimeoutMs ); + /* Setting the send block time cannot fail. */ + ( void ) FreeRTOS_setsockopt( pNetworkContext->socket, + 0, + FREERTOS_SO_SNDTIMEO, + &transportTimeout, + sizeof( TickType_t ) ); + + if( TLS_Init( &tlsHelperParams, + ( TLSContext_t * ) ( pNetworkContext->pTLSContext ) ) == pdPASS ) + { + LogInfo( ( "Initiating TLS handshake with host: %s:%d\r\n", pServerInfo->pHostName, pServerInfo->port ) ); + + /* Initiate TLS handshake */ + if( TLS_Connect( ( TLSContext_t * ) ( pNetworkContext->pTLSContext ) ) != 0 ) + { + status = TRANSPORT_STATUS_TLS_FAILURE; + } + } + else + { + status = TRANSPORT_STATUS_TLS_FAILURE; + } + } + else + { + status = TRANSPORT_STATUS_INSUFFICIENT_MEMORY; + } + } + } + + return status; +} + +TransportStatus_t Transport_Disconnect( NetworkContext_t * pNetworkContext ) +{ + TransportStatus_t status = TRANSPORT_STATUS_SUCCESS; + int32_t socketStatus; + BaseType_t waitForShutdownLoopCount = 0; + uint8_t pDummyBuffer[ 2 ]; + + if( ( pNetworkContext == NULL ) || ( pNetworkContext->socket == NULL ) ) + { + status = TRANSPORT_STATUS_INVALID_PARAMETER; + } + else + { + /* Initiate graceful shutdown. */ + ( void ) FreeRTOS_shutdown( pNetworkContext->socket, FREERTOS_SHUT_RDWR ); + + /* Wait for the socket to disconnect gracefully (indicated by FreeRTOS_recv() + * returning a FREERTOS_EINVAL error) before closing the socket. */ + while( FreeRTOS_recv( pNetworkContext->socket, pDummyBuffer, sizeof( pDummyBuffer ), 0 ) >= 0 ) + { + /* We don't need to delay since FreeRTOS_recv should already have a timeout. */ + + if( ++waitForShutdownLoopCount >= 3 ) + { + break; + } + } + + ( void ) FreeRTOS_closesocket( pNetworkContext->socket ); + + if( pNetworkContext->pTLSContext != NULL ) + { + TLS_Cleanup( ( TLSContext_t * ) ( pNetworkContext->pTLSContext ) ); + vPortFree( pNetworkContext->pTLSContext ); + pNetworkContext->pTLSContext = NULL; + } + } + + return status; +} +/*-----------------------------------------------------------*/ + +int32_t Transport_Recv( NetworkContext_t * pNetworkContext, + void * pBuffer, + size_t bytesToRecv ) +{ + int32_t rc = -1; + + if( ( pNetworkContext == NULL ) || ( pBuffer == NULL ) || ( bytesToRecv == 0 ) ) + { + rc = -1; + } + else + { + if( pNetworkContext->pTLSContext != NULL ) + { + rc = TLS_Recv( ( TLSContext_t * ) ( pNetworkContext->pTLSContext ), pBuffer, bytesToRecv ); + } + else + { + LogError( ( "Transport_Recv: TLS context is NULL" ) ); + } + } + + return rc; +} + +int32_t Transport_Send( NetworkContext_t * pNetworkContext, + const void * pMessage, + size_t bytesToSend ) +{ + int32_t rc = -1; + + if( ( pNetworkContext == NULL ) || ( pMessage == NULL ) || ( bytesToSend == 0 ) ) + { + rc = -1; + } + else + { + if( pNetworkContext->pTLSContext != NULL ) + { + rc = TLS_Send( ( TLSContext_t * ) ( pNetworkContext->pTLSContext ), pMessage, bytesToSend ); + } + else + { + LogError( ( "Transport_Send: TLS context is NULL" ) ); + } + } + + return rc; +} + +/** + * @brief Defines callback type for receiving bytes from the network. + * + * @param[in] pvCallerContext Opaque context handle provided by caller. + * @param[out] pucReceiveBuffer Buffer to fill with received data. + * @param[in] xReceiveLength Length of previous parameter in bytes. + * + * @return The number of bytes actually read or appropriate error code. + */ +static int Recv_Cb( void * pvCallerContext, + unsigned char * pucReceiveBuffer, + size_t xReceiveLength ) +{ + int xReturnStatus = pdFREERTOS_ERRNO_NONE; + NetworkContext_t * pNetworkContext = ( NetworkContext_t * ) ( pvCallerContext ); + + if( ( pNetworkContext == NULL ) || ( pucReceiveBuffer == NULL ) ) + { + xReturnStatus = pdFREERTOS_ERRNO_EINVAL; + } + else + { + xReturnStatus = FreeRTOS_recv( pNetworkContext->socket, pucReceiveBuffer, xReceiveLength, 0 ); + } + + return( xReturnStatus ); +} + +/** + * @brief Defines callback type for sending bytes to the network. + * + * @param[in] pvCallerContext Opaque context handle provided by caller. + * @param[out] pucReceiveBuffer Buffer of data to send. + * @param[in] xReceiveLength Length of previous parameter in bytes. + * + * @return The number of bytes actually sent. + */ +static int Send_Cb( void * pvCallerContext, + const unsigned char * pucData, + size_t xDataLength ) +{ + int xReturnStatus = pdFREERTOS_ERRNO_NONE; + NetworkContext_t * pNetworkContext = ( NetworkContext_t * ) ( pvCallerContext ); + + if( ( pNetworkContext == NULL ) || ( pucData == NULL ) ) + { + xReturnStatus = pdFREERTOS_ERRNO_EINVAL; + } + else + { + xReturnStatus = FreeRTOS_send( pNetworkContext->socket, pucData, xDataLength, 0 ); + } + + return( xReturnStatus ); +} diff --git a/Projects/aws-iot-example/CMakeLists.txt b/Projects/aws-iot-example/CMakeLists.txt index cf575602..80115367 100644 --- a/Projects/aws-iot-example/CMakeLists.txt +++ b/Projects/aws-iot-example/CMakeLists.txt @@ -81,7 +81,7 @@ else() endif() # Select connectivity stack. -set(CONNECTIVITY_STACK "IOT_VSOCKET" CACHE STRING "Choose the connectivity stack to use. Possible values are `FREERTOS_TCP_IP | IOT_VSOCKET`.") +set(CONNECTIVITY_STACK "FREERTOS_TCP_IP" CACHE STRING "Choose the connectivity stack to use. Possible values are `FREERTOS_TCP_IP | IOT_VSOCKET`.") set(MIDDLEWARE_DIR "../../Middleware") diff --git a/Projects/aws-iot-example/main.c b/Projects/aws-iot-example/main.c index 3f676985..24c34585 100644 --- a/Projects/aws-iot-example/main.c +++ b/Projects/aws-iot-example/main.c @@ -103,6 +103,47 @@ int mbedtls_hardware_poll( void * data, return 0; } +BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber ) +{ + psa_status_t xPsaStatus = PSA_ERROR_PROGRAMMER_ERROR; + + xPsaStatus = psa_generate_random( ( uint8_t * ) ( pulNumber ), sizeof( uint32_t ) ); + + if( xPsaStatus == PSA_SUCCESS ) + { + return pdTRUE; + } + else + { + return pdFALSE; + } +} + +uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress, + uint16_t usSourcePort, + uint32_t ulDestinationAddress, + uint16_t usDestinationPort ) +{ + psa_status_t xPsaStatus = PSA_ERROR_PROGRAMMER_ERROR; + uint32_t uxRandomValue = 0U; + + /* Unused parameters. */ + ( void ) ulSourceAddress; + ( void ) usSourcePort; + ( void ) ulDestinationAddress; + ( void ) usDestinationPort; + + xPsaStatus = psa_generate_random( ( uint8_t * ) ( &uxRandomValue ), sizeof( uint32_t ) ); + + if( xPsaStatus != PSA_SUCCESS ) + { + LogError( ( "psa_generate_random failed with %d.", xPsaStatus ) ); + configASSERT( 0 ); + } + + return uxRandomValue; +} + int main() { UBaseType_t xRetVal; diff --git a/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c b/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c index ff64c769..c397258a 100644 --- a/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c +++ b/Projects/aws-iot-example/mqtt-agent-wrapper/mqtt_agent_task.c @@ -74,12 +74,6 @@ #endif #include "logging_stack.h" -/** - * @brief Used to convert times to/from ticks and milliseconds. - */ -#define MQTT_AGENT_MILLISECONDS_PER_SECOND ( 1000U ) -#define MQTT_AGENT_MILLISECONDS_PER_TICK TICKS_TO_pdMS( MQTT_AGENT_MILLISECONDS_PER_SECOND ) - /** * @brief Dimensions the buffer used to serialize and deserialize MQTT packets. * @note Specified in bytes. Must be large enough to hold the maximum @@ -255,7 +249,7 @@ static uint32_t prvGetTimeMs( void ) xTickCount = xTaskGetTickCount(); /* Convert the ticks to milliseconds. */ - ulTimeMs = ( uint32_t ) xTickCount * MQTT_AGENT_MILLISECONDS_PER_TICK; + ulTimeMs = ( uint32_t ) TICKS_TO_pdMS( xTickCount ); /* Reduce ulGlobalEntryTimeMs from obtained time so as to always return the * elapsed time in the application. */ From 7c9c2c2d96206115dcb879d9c46a31516a280c15 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Mon, 21 Aug 2023 10:09:47 +0000 Subject: [PATCH 09/13] scripts: Add support for FVP choice Added support for choosing either ecosystem FVP or Arm virtual hardware FVP running in the AMI to the `run.sh` script. The ecosystem FVP is used by default. Signed-off-by: Devaraj Ranganna --- Tools/scripts/run.sh | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Tools/scripts/run.sh b/Tools/scripts/run.sh index 907c6792..21f4a272 100755 --- a/Tools/scripts/run.sh +++ b/Tools/scripts/run.sh @@ -9,8 +9,11 @@ ROOT="$(realpath $HERE/..)" EXAMPLE="" BUILD_PATH="build" TARGET="corstone300" +FVP_TYPE="fvp" FVP_BIN="" +set -e + function show_usage { cat <" + show_usage + exit 2 + fi ;; corstone310 ) - FVP_BIN="VHT_Corstone_SSE-310" + if [ "$FVP_TYPE" == "fvp" ]; then + FVP_BIN="FVP_Corstone_SSE-310" + elif [ "$FVP_TYPE" == "vht" ]; then + FVP_BIN="VHT_Corstone_SSE-310" + else + echo "Invalid FVP type " + show_usage + exit 2 + fi ;; *) echo "Invalid target " From 893be543be2a18e8857e07dbbe3f93af2edc0540 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Wed, 13 Sep 2023 12:50:26 +0000 Subject: [PATCH 10/13] docs: Add instrucitons to install Arm ecosystem FVPs With the addition of support for FreeRTOS TCP/IP stack, in addition to Arm Virtual Hardware using Amazon Machine Images, Arm FRI can now run ecosystem FVPs. Signed-off-by: Devaraj Ranganna --- Docs/Prerequisites.md | 48 +++++++++++++++++++++++++++++++++ Docs/aws-iot-example.md | 16 +++-------- Docs/blinky.md | 16 +++-------- Docs/development-environment.md | 2 +- README.md | 2 +- 5 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 Docs/Prerequisites.md diff --git a/Docs/Prerequisites.md b/Docs/Prerequisites.md new file mode 100644 index 00000000..92b197b4 --- /dev/null +++ b/Docs/Prerequisites.md @@ -0,0 +1,48 @@ +# Prerequisites + +## Setting up the platforms + +This reference integration supports both [Arm ecosystem FVPs](https://developer.arm.com/downloads/-/arm-ecosystem-fvps) +and Arm Virtual Hardware using [Amazon Machine Images](#setting-up-arm-virtual-hardware-using-amazon-machine-images). + +The Corstone-300 ecosystem FVP is aligned with the Arm MPS3 development +platform. It is based on the Cortex-M55 processor and offers a choice of the +Ethos-U55 and Ethos-U65 processors. This FVP is provided free of charge for the +limited development and validation of open-source software on the Corstone-300 +platform while Arm Virtual Hardware is recommended for commercial software. +Follow the [link](https://developer.arm.com/downloads/-/arm-ecosystem-fvps) for +more information. + +### Setting up Arm ecosystem FVPs + +* Download the Corstone-300 FVP from [here](https://developer.arm.com/downloads/-/arm-ecosystem-fvps) +* Run the following commands to install the Corstone-300 FVP and add FVP path + to `PATH` variable. + ```bash + mkdir FVP_Corstone_SSE-300_11.22_20 + tar -xzf /FVP_Corstone_SSE-300_11.22_20_Linux64.tgz -C FVP_Corstone_SSE-300_11.22_20 + cd FVP_Corstone_SSE-300_11.22_20 + ./FVP_Corstone_SSE-300.sh + echo PATH=\"/FVP_Corstone_SSE-300/models/Linux64_GCC-9.3:\$PATH\" >> ~/.bashrc + source ~/.bashrc + ``` + +### Setting up Arm Virtual Hardware using Amazon Machine Images + +Follow the instructions described in [Launch Arm Virtual Hardware Instance](setting-up-arm-virtual-hardware.md) +to setup your development environment. + +If you have successfully followed the instructions, then you should have a +console (either AWS-Web-Console or Local-Console) to an Arm Virtual Hardware +Instance. From now on, any command-line commands described in this document +must be run on the console connected to the Arm Virtual Hardware Instance. + +**Note** +The run example script `Tools/scripts/run.sh` assumes ecosystem FVP by default. +If you are using Arm virtual hardware using Amazon machine images then an +additional argument `--fvp_type vht` must be passed to the run example script. + +## Setting up development environment + +Follow the instructions described in [Setting Up your Development Environment](development-environment.md) +to setup your development environment. diff --git a/Docs/aws-iot-example.md b/Docs/aws-iot-example.md index c58c6799..03edbe37 100644 --- a/Docs/aws-iot-example.md +++ b/Docs/aws-iot-example.md @@ -44,20 +44,10 @@ non-secure (FreeRTOS kernel and the application) images are singed separately, MCUBoot verifies that both image signatures are valid before booting. If either of the verification fails, then MCUBoot stops the booting process. -## Setting up Arm Virtual Hardware using Amazon Machine Images +## Prerequisites -Follow the instructions described in [Launch Arm Virtual Hardware Instance](setting-up-arm-virtual-hardware.md) -to setup your development environment. - -If you have successfully followed the instructions, then you should have a -console (either AWS-Web-Console or Local-Console) to an Arm Virtual Hardware -Instance. From now on, any command-line commands described in this document -must be run on the console connected to the Arm Virtual Hardware Instance. - -## Setting up development environment - -Follow the instructions described in [Setting Up your Development Environment](development-environment.md) -to setup your development environment. +Follow the instructions described in [Prerequisites](Prerequisites.md) and +ensure that all the prerequisites are met before continuing. ## Setting up AWS connectivity diff --git a/Docs/blinky.md b/Docs/blinky.md index 9d9447e8..28f20a47 100644 --- a/Docs/blinky.md +++ b/Docs/blinky.md @@ -9,20 +9,10 @@ on the secure-side and prints it on the console. In addition, to simulate LED blinking, `LED On` and `LED off` are printed onto the console at regular intervals. -## Setting up Arm Virtual Hardware using Amazon Machine Images +## Prerequisites -Follow the instructions described in [Launch Arm Virtual Hardware Instance](setting-up-arm-virtual-hardware.md) -to setup your development environment. - -If you have successfully followed the instructions, then you should have a -console (either AWS-Web-Console or Local-Console) to Arm Virtual Hardware -Instance. From now on, any command-line commands described in this document -must be run on the console connected to Arm Virtual Hardware Instance. - -## Setting up development environment - -Follow the instructions described in [Setting Up your Development Environment](development-environment.md) -to setup your development environment. +Follow the instructions described in [Prerequisites](Prerequisites.md) and +ensure that all the prerequisites are met before continuing. ## Building the application diff --git a/Docs/development-environment.md b/Docs/development-environment.md index daa16a1a..070557ac 100644 --- a/Docs/development-environment.md +++ b/Docs/development-environment.md @@ -16,7 +16,7 @@ argument, you should run: git submodule update --init --recursive ``` -## Prerequisites +## Build requirements * Ubuntu 20.04 or higher. Please note that the following instructions are validated on Ubuntu 20.04. diff --git a/README.md b/README.md index e1e7eab5..1fa78e0b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ This project is based on the [Corstone-300](https://developer.arm.com/Processors platform. Developers and partners can use this integration as a starting point to build -FreeRTOS kernel and libraires based software stack on top of Arm Cortex-M based +FreeRTOS kernel and libraries based software stack on top of Arm Cortex-M based platforms. All the components are put together in a modular manner to make porting of this integration across platforms easy. From d62e8caec2fb281595a1ae461292febb13e742a8 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Wed, 4 Oct 2023 13:19:46 +0000 Subject: [PATCH 11/13] ci: Update spell-check dictionary Signed-off-by: Devaraj Ranganna --- .github/.cSpellWords.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/.cSpellWords.txt b/.github/.cSpellWords.txt index 32533149..3413c569 100644 --- a/.github/.cSpellWords.txt +++ b/.github/.cSpellWords.txt @@ -38,16 +38,20 @@ ecdh ECDH ECKEY fsanitize +FVPs havege hkdf HKDF IDAQAB +indet inkey iotdeviceadvisor istty JITP JITR Jytl +NBNS +LLMNR mcuboot MCUBOOT mbed From f81d8ed538c9104ff8b5c8b0a1d1fd7d64f573c3 Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Wed, 11 Oct 2023 15:34:11 +0000 Subject: [PATCH 12/13] iot-vsocket: Fix/update function headers Signed-off-by: Devaraj Ranganna --- .../IoT_VSocket-lib/transport_interface_api.h | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Middleware/ARM/IoT_VSocket-lib/transport_interface_api.h b/Middleware/ARM/IoT_VSocket-lib/transport_interface_api.h index 65cbb029..a3411306 100644 --- a/Middleware/ARM/IoT_VSocket-lib/transport_interface_api.h +++ b/Middleware/ARM/IoT_VSocket-lib/transport_interface_api.h @@ -56,7 +56,7 @@ typedef enum TransportStatus TRANSPORT_STATUS_DNS_FAILURE, /**< Resolving hostname of the server failed. */ TRANSPORT_STATUS_SOCKET_CREATE_FAILURE, /**< Underlying socket creation failed. */ TRANSPORT_STATUS_CONNECT_FAILURE, /**< Initial connection to the server failed. */ - TRANSPORT_STATUS_TLS_FAILURE, /**< TLS Handshake for the secure connection failed. */ + TRANSPORT_STATUS_TLS_FAILURE, /**< TLS Handshake for the secure connection failed. */ TRANSPORT_STATUS_SOCKET_CLOSE_FAILURE /**< Failed to close the underlying socket. */ } TransportStatus_t; @@ -118,6 +118,8 @@ typedef struct TLSParams * @param[out] pNetworkContext The output parameter to return the created network context. * @param[in] pServerInfo Server connection info. * @param[in] pTLSParams socket configs for the connection. + * @param[in] sendTimeoutMs socket send timeout. + * @param[in] recvTimeoutMs socket receive timeout. * * @return #TRANSPORT_STATUS_SUCCESS on success; * #TRANSPORT_STATUS_INVALID_PARAMETER, #TRANSPORT_STATUS_INSUFFICIENT_MEMORY, @@ -125,9 +127,9 @@ typedef struct TLSParams * #TRANSPORT_STATUS_DNS_FAILURE, #TRANSPORT_STATUS_CONNECT_FAILURE on failure. */ TransportStatus_t Transport_Connect( NetworkContext_t * pNetworkContext, - const ServerInfo_t * pServerInfo, - const TLSParams_t * pTLSParams, - uint32_t sendTimeoutMs, + const ServerInfo_t * pServerInfo, + const TLSParams_t * pTLSParams, + uint32_t sendTimeoutMs, uint32_t recvTimeoutMs ); /** @@ -157,8 +159,8 @@ TransportStatus_t Transport_Disconnect( NetworkContext_t * pNetworkContext ); * negative value on error. */ int32_t Transport_Recv( NetworkContext_t * pNetworkContext, - void * pBuffer, - size_t bytesToRecv ); + void * pBuffer, + size_t bytesToRecv ); /** * @brief Sends data over an established TLS session using the Secure Sockets API. @@ -167,13 +169,13 @@ int32_t Transport_Recv( NetworkContext_t * pNetworkContext, * over the network. * * @param[in] pNetworkContext The network context created using Secure Sockets API. - * @param[in] pBuffer Buffer containing the bytes to send over the network stack. + * @param[in] pMessage A message to be sent over the network stack. * @param[in] bytesToSend Number of bytes to send over the network. * * @return Number of bytes sent if successful; negative value on error. */ int32_t Transport_Send( NetworkContext_t * pNetworkContext, - const void * pMessage, - size_t bytesToSend ); + const void * pMessage, + size_t bytesToSend ); #endif /* TRANSPORT_INTERFACE_API_H */ From 646389838f61fd83a3fbc47677180f4ca826226e Mon Sep 17 00:00:00 2001 From: Devaraj Ranganna Date: Wed, 11 Oct 2023 15:36:29 +0000 Subject: [PATCH 13/13] config: Add description about FVP behaviour Provide justification for setting `configTICK_RATE_HZ` to `100` to simulate scheduler polling rate of `1000 Hz` or 1 tick per second. Signed-off-by: Devaraj Ranganna --- Config/freertos-config/FreeRTOSConfig.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Config/freertos-config/FreeRTOSConfig.h b/Config/freertos-config/FreeRTOSConfig.h index 1388a44d..90fd5dfa 100644 --- a/Config/freertos-config/FreeRTOSConfig.h +++ b/Config/freertos-config/FreeRTOSConfig.h @@ -57,10 +57,26 @@ extern uint32_t SystemCoreClock; #define configENABLE_TRUSTZONE 0 #define configRUN_FREERTOS_SECURE_ONLY 0 -/* somehow 100 tick per second gives similar timing (~85%) as 1000 did on the FPGA, so with this 1 ms to 1 tick can be */ -/* kept... */ -#define configTICK_RATE_HZ ( ( uint32_t ) 100 ) /* Scheduler polling rate of 1000 Hz */ - +/* From the "Fast Models Reference Guide" (https://developer.arm.com/documentation/100964/1123/About-the-models), + * "Programmer's View (PV) models of processors and devices work at a level + * where functional behavior is equivalent to what a programmer would see using + * the hardware. + * + * They sacrifice timing accuracy to achieve fast simulation execution speeds: + * you can use the PV models for confirming software functionality, but you + * must not rely on the accuracy of cycle counts, low-level component + * interactions, or other hardware-specific behavior." + * + * As described above, FVPs sacrifice timing accuracy to achieve fast + * simulation execution speeds. Therefore, we need this work around of setting + * `configTICK_RATE_HZ` to `100` to simulate scheduler polling rate of + * `1000 Hz` or 1 tick per second. + * + * In addition, the macro `pdMS_TO_TICKS` is defined here to match the 1 tick + * per second instead of using the macro defined in + * `FreeRTOS-kernel/include/projdefs.h` + */ +#define configTICK_RATE_HZ ( ( uint32_t ) 100 ) #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) xTimeInMs ) #define TICKS_TO_pdMS( xTicks ) ( ( uint32_t ) xTicks )