From 4b6e3956d6a9dfb1dd2f1aa2fe386afc2fd4a7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 5 May 2022 21:25:19 +0200 Subject: [PATCH] [realtek-ambz] Implement SSL client, cleanup compilation --- README.md | 2 +- arduino/realtek-ambz/cores/arduino/WVariant.h | 14 ++ .../realtek-ambz/cores/arduino/rtl_sys.cpp | 147 ++---------------- .../libraries/WiFi/WiFiClientSecure.h | 8 + builder/frameworks/realtek-ambz-arduino.py | 4 +- builder/frameworks/realtek-ambz-sdk.py | 127 +++++++-------- .../realtek-ambz/memory-management.md | 15 ++ mkdocs.yml | 2 + 8 files changed, 106 insertions(+), 213 deletions(-) create mode 100644 arduino/realtek-ambz/libraries/WiFi/WiFiClientSecure.h create mode 100644 docs/platform/realtek-ambz/memory-management.md diff --git a/README.md b/README.md index 4c91be5..0701ac5 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ SPI | ❌ Wire | ❌ **OTHER LIBRARIES** | Wi-Fi STA/AP/Mixed | ✔️ -Wi-Fi Client (SSL) | ✔️ (❌) +Wi-Fi Client (SSL) | ✔️ (✔️) Wi-Fi Server | ✔️ Wi-Fi Events | ❌ IPv6 | ❌ diff --git a/arduino/realtek-ambz/cores/arduino/WVariant.h b/arduino/realtek-ambz/cores/arduino/WVariant.h index 0b634d4..0d627b8 100644 --- a/arduino/realtek-ambz/cores/arduino/WVariant.h +++ b/arduino/realtek-ambz/cores/arduino/WVariant.h @@ -12,6 +12,8 @@ extern "C" { #include #define boolean boolean_rtl #include "rtl_lib.h" +#include "rand.h" +#include "rt_lib_rom.h" #undef boolean /* moved from Arduino.h */ @@ -33,6 +35,18 @@ extern void wait_us(int us); extern void yield(void); +extern void *pvPortMalloc(size_t xWantedSize); +extern void *pvPortZalloc(size_t size); +extern void *pvPortCalloc(size_t nmemb, size_t size); +extern void *pvPortReAlloc(void *pv, size_t xWantedSize); +extern void vPortFree(void *pv); + +#define malloc pvPortMalloc +#define zalloc pvPortZalloc +#define calloc pvPortCalloc +#define realloc pvPortReAlloc +#define free vPortFree + #ifndef printf #define printf rtl_printf #endif diff --git a/arduino/realtek-ambz/cores/arduino/rtl_sys.cpp b/arduino/realtek-ambz/cores/arduino/rtl_sys.cpp index 6382477..b2b2e2f 100644 --- a/arduino/realtek-ambz/cores/arduino/rtl_sys.cpp +++ b/arduino/realtek-ambz/cores/arduino/rtl_sys.cpp @@ -29,9 +29,6 @@ extern "C" { #include extern void *pvPortMalloc( size_t xWantedSize ); -extern void *os_zalloc( size_t xWantedSize ); -extern void vPortFree( void *pv ); -extern void *pvPortReAlloc( void *pv, size_t xWantedSize ); extern size_t xPortGetFreeHeapSize( void ); extern size_t xPortGetMinimumEverFreeHeapSize( void ); @@ -45,7 +42,7 @@ extern int tcm_heap_freeSpace(void); __attribute__((noreturn)) void __panic_func(const char* file, int line, const char* func) { DiagPrintf("\r\nPanic: %s, line: %d, %s\r\n"); - while(1); + while(1); } #ifdef __cplusplus @@ -56,11 +53,11 @@ void hexdump(void * ptr, int cnt) { char * p = (char *) ptr; int c = cnt; - while(c > 64) { + while(c > 64) { DumpForOneBytes((void *)p, 64); p += 64; c -= 64; - } + } if(c != 0) DumpForOneBytes((void *)p, c); } @@ -79,137 +76,13 @@ void sys_info(void) { HalGetCpuClk(), xPortGetFreeHeapSize(), tcm_heap_freeSpace()); } - -/* void * malloc(size_t size) -{ - void * ret; - if((ret = pvPortMalloc(size)) == NULL) - ret = tcm_heap_malloc(size); - return ret; +void *pvPortZalloc(size_t size) { + void *pvReturn = pvPortMalloc(size); + if (pvReturn) + memset(pvReturn, 0, size); + return pvReturn; } -void * zalloc(size_t size) -{ - void * ret; - if((ret = pvPortMalloc(size)) == NULL) - ret = tcm_heap_calloc(size); - else memset(ret, 0, size); - return ret; +void *pvPortCalloc(size_t nmemb, size_t size) { + return pvPortZalloc(nmemb * size); } - -void *calloc(size_t count, size_t size) -{ - return zalloc(count * size); -} */ - -void free(void *pv) -{ - vPortFree(pv); -} - -void * realloc(void *pv, size_t size) -{ - return pvPortReAlloc(pv, size); -} - -/* void *operator new(size_t size) -{ - void * ret; - if((ret = zalloc(size)) == NULL) { - DiagPrintf("\r\nMEM error!\r\n"); - while(1); - } - return ret; -} - -void *operator new[](size_t size) -{ - void * ret; - if((ret = zalloc(size)) == NULL) { - DiagPrintf("\r\nMEM error!\r\n"); - while(1); - } - return ret; -} - -void operator delete(void * ptr) -{ - free(ptr); -} - -void operator delete[](void * ptr) -{ - free(ptr); -} */ - -/* -extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); -extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); - -void __cxa_pure_virtual(void) -{ - panic(); -} - -void __cxa_deleted_virtual(void) -{ - panic(); -} - -typedef struct { - uint8_t guard; - uint8_t ps; -} guard_t; - -extern "C" int __cxa_guard_acquire(__guard* pg) -{ - uint8_t ps = xt_rsil(15); - if (reinterpret_cast(pg)->guard) { - xt_wsr_ps(ps); - return 0; - } - reinterpret_cast(pg)->ps = ps; - return 1; -} - -extern "C" void __cxa_guard_release(__guard* pg) -{ - reinterpret_cast(pg)->guard = 1; - xt_wsr_ps(reinterpret_cast(pg)->ps); -} - -extern "C" void __cxa_guard_abort(__guard* pg) -{ - xt_wsr_ps(reinterpret_cast(pg)->ps); -} - - -namespace std -{ -void __throw_bad_function_call() -{ - panic(); -} - -void __throw_length_error(char const*) -{ - panic(); -} - -void __throw_bad_alloc() -{ - panic(); -} - -void __throw_logic_error(const char* str) -{ - panic(); -} - -void __throw_out_of_range(const char* str) -{ - panic(); -} -} - -*/ diff --git a/arduino/realtek-ambz/libraries/WiFi/WiFiClientSecure.h b/arduino/realtek-ambz/libraries/WiFi/WiFiClientSecure.h new file mode 100644 index 0000000..a4b1456 --- /dev/null +++ b/arduino/realtek-ambz/libraries/WiFi/WiFiClientSecure.h @@ -0,0 +1,8 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-05-04. */ + +#pragma once + +#include +#include + +typedef MbedTLSClient WiFiClientSecure; diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index 316bfba..2afdb8a 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -6,6 +6,8 @@ from SCons.Script import DefaultEnvironment env = DefaultEnvironment() +env.Replace(AMBZ_NO_POLARSSL=True) + env.SConscript("realtek-ambz-sdk.py", exports="env") env.SConscript("../arduino-common.py", exports="env") @@ -45,8 +47,8 @@ env.Append( # enable LwIPRxBuffer "LT_HAS_LWIP", ("LT_PRINTF_BROKEN", "1"), # printf does not handle %.3f properly - ("zalloc", "os_zalloc"), "MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED", # enable PSK in mbedTLS + # "MBEDTLS_DEBUG_C", ], LINKFLAGS=[ "--specs=nosys.specs", diff --git a/builder/frameworks/realtek-ambz-sdk.py b/builder/frameworks/realtek-ambz-sdk.py index e27a0d7..a95bba3 100644 --- a/builder/frameworks/realtek-ambz-sdk.py +++ b/builder/frameworks/realtek-ambz-sdk.py @@ -35,7 +35,7 @@ env.Replace( ) # Flags -env.Replace( +env.Append( CFLAGS=[ "-std=gnu99", "-mcpu=cortex-m4", @@ -101,7 +101,6 @@ env.AddLibrary( name="ambz_sdk", base_dir="$SDK_DIR", srcs=[ - "+", # NOTE: a fixup is used instead, to remove default main() # "+", "+", @@ -116,22 +115,6 @@ env.AddLibrary( "+", "+", "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", "+", "+", "+", @@ -168,52 +151,8 @@ env.AddLibrary( "+", "+", "+", - "+", "+", "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", - "+", "+", "+", "+", @@ -257,11 +196,7 @@ env.AddLibrary( "+", "+", "+", - "+", - "+", "+", - "+", - "+", "+", ], includes=[ @@ -272,17 +207,12 @@ env.AddLibrary( "+", "+", "+", - "+", "+", "+", "+", "+", "+", "+", - "+", - "+", - "+", - "+", "+", "+", "+", @@ -295,7 +225,6 @@ env.AddLibrary( "+", "+", "+", - "+", "+", "+", "+", @@ -311,11 +240,61 @@ env.AddLibrary( "+", "+", "+", - "+", - "+", ], ) +# Sources - PolarSSL library +if "AMBZ_NO_POLARSSL" not in env or not env["AMBZ_NO_POLARSSL"]: + env.AddLibrary( + name="ambz_polarssl", + base_dir="$SDK_DIR", + srcs=[ + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + "+", + ], + ) + # Sources - platform fixups env.AddLibrary( name="ambz_fixups", diff --git a/docs/platform/realtek-ambz/memory-management.md b/docs/platform/realtek-ambz/memory-management.md new file mode 100644 index 0000000..f65d243 --- /dev/null +++ b/docs/platform/realtek-ambz/memory-management.md @@ -0,0 +1,15 @@ +# Memory management + +Function | Target | #define location | Notes +--------------|------------------------|---------------------------------------------------|------------------------------------------------------------------------------------------------ +__`malloc`__ | __`pvPortMalloc`__ | `component/common/api/platform/platform_stdlib.h` | +`zalloc` | `os_zalloc` (ROM) | | This is **PROBABLY BROKEN**. ROM disassembly shows it only does memset on a fixed memory range. +__`zalloc`__ | __`pvPortZalloc`__ | `arduino/realtek-ambz/cores/WVariant.h` | Custom implementation in `rtl_sys.cpp` +`calloc` | `os_calloc` | ? | This one is not in ROM. I didn't dig any deeper into it. +`calloc` | `calloc_freertos` | `component/os/freertos/cmsis_os.h` | Probably not used +`calloc` | `__rtl_calloc_r` (ROM) | | Not used, as I preferred to use FreeRTOS memory management. +__`calloc`__ | __`pvPortCalloc`__ | `arduino/realtek-ambz/cores/WVariant.h` | Custom implementation in `rtl_sys.cpp` +__`realloc`__ | __`pvPortRealloc`__ | `arduino/realtek-ambz/cores/WVariant.h` | +__`free`__ | __`vPortFree`__ | `component/common/api/platform/platform_stdlib.h` | + +__Underlined__ item means that it is defined and used in code. diff --git a/mkdocs.yml b/mkdocs.yml index caf99cb..8780daa 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -19,5 +19,7 @@ nav: - "Realtek AmebaZ Series": - "Boards": - "WR3": "boards/wr3/README.md" + - "stdlib usage": + - "Memory management": "docs/platform/realtek-ambz/memory-management.md" - "Debugging": "docs/platform/realtek/debugging.md" - "Exception decoder": "docs/platform/realtek/exception-decoder.md"