[realtek-ambz] Implement SSL client, cleanup compilation

This commit is contained in:
Kuba Szczodrzyński
2022-05-05 21:25:19 +02:00
parent 9659ff8afa
commit 4b6e3956d6
8 changed files with 106 additions and 213 deletions

View File

@@ -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 | ❌

View File

@@ -12,6 +12,8 @@ extern "C" {
#include <stdarg.h>
#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

View File

@@ -29,9 +29,6 @@ extern "C" {
#include <diag.h>
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<guard_t*>(pg)->guard) {
xt_wsr_ps(ps);
return 0;
}
reinterpret_cast<guard_t*>(pg)->ps = ps;
return 1;
}
extern "C" void __cxa_guard_release(__guard* pg)
{
reinterpret_cast<guard_t*>(pg)->guard = 1;
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
}
extern "C" void __cxa_guard_abort(__guard* pg)
{
xt_wsr_ps(reinterpret_cast<guard_t*>(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();
}
}
*/

View File

@@ -0,0 +1,8 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-04. */
#pragma once
#include <WiFi.h>
#include <ssl/MbedTLSClient.h>
typedef MbedTLSClient WiFiClientSecure;

View File

@@ -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",

View File

@@ -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=[
"+<component/common/application/uart_adapter/uart_adapter.c>",
# NOTE: a fixup is used instead, to remove default main()
# "+<component/soc/realtek/8711b/cmsis/device/app_start.c>",
"+<component/soc/realtek/8711b/fwlib/ram_lib/startup.c>",
@@ -116,22 +115,6 @@ env.AddLibrary(
"+<component/common/api/wifi/wifi_simple_config.c>",
"+<component/common/api/wifi/wifi_util.c>",
"+<component/common/api/lwip_netconf.c>",
"+<component/common/application/mqtt/MQTTClient/MQTTClient.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTConnectClient.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTConnectServer.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTDeserializePublish.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTFormat.c>",
"+<component/common/application/mqtt/MQTTClient/MQTTFreertos.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTPacket.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTSerializePublish.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTSubscribeClient.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTSubscribeServer.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTUnsubscribeClient.c>",
"+<component/common/application/mqtt/MQTTPacket/MQTTUnsubscribeServer.c>",
"+<component/common/api/network/src/ping_test.c>",
"+<component/common/utilities/ssl_client.c>",
"+<component/common/utilities/tcptest.c>",
"+<component/common/api/network/src/wlan_network.c>",
"+<component/common/network/lwip/lwip_v1.4.1/src/api/api_lib.c>",
"+<component/common/network/lwip/lwip_v1.4.1/src/api/api_msg.c>",
"+<component/common/network/lwip/lwip_v1.4.1/src/api/err.c>",
@@ -168,52 +151,8 @@ env.AddLibrary(
"+<component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/ethernetif.c>",
"+<component/common/drivers/wlan/realtek/src/osdep/lwip_intf.c>",
"+<component/common/network/lwip/lwip_v1.4.1/port/realtek/freertos/sys_arch.c>",
"+<component/common/network/websocket/wsclient_tls.c>",
"+<component/common/network/dhcp/dhcps.c>",
"+<component/common/network/sntp/sntp.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/aesni.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/blowfish.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/camellia.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ccm.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/certs.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/cipher.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/cipher_wrap.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/debug.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ecp_ram.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/entropy.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/entropy_poll.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/error.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/gcm.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/havege.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/md2.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/md4.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/memory_buffer_alloc.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/net.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/padlock.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pbkdf2.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs11.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs12.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs5.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkparse.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/platform.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ripemd160.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_cache.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_ciphersuites.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_cli.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_srv.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_tls.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/threading.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/timing.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/version.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/version_features.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_create.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_crl.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_crt.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_csr.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509write_crt.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509write_csr.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/xtea.c>",
"+<component/common/network/ssl/ssl_ram_map/ssl_ram_map.c>",
"+<component/os/freertos/freertos_v8.1.2/Source/portable/MemMang/heap_5.c>",
"+<component/os/freertos/freertos_v8.1.2/Source/portable/GCC/ARM_CM4F/port.c>",
@@ -257,11 +196,7 @@ env.AddLibrary(
"+<component/soc/realtek/8711b/misc/rtl8710b_ota.c>",
"+<component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_pinmapcfg.c>",
"+<component/soc/realtek/8711b/fwlib/ram_lib/rtl8710b_sleepcfg.c>",
"+<component/common/network/httpc/httpc_tls.c>",
"+<component/common/network/httpd/httpd_tls.c>",
"+<component/common/utilities/cJSON.c>",
"+<component/common/utilities/http_client.c>",
"+<component/common/utilities/uart_socket.c>",
"+<component/common/utilities/xml.c>",
],
includes=[
@@ -272,17 +207,12 @@ env.AddLibrary(
"+<component/os/os_dep/include>",
"+<component/common/api/network/include>",
"+<component/common/api>",
"+<component/common/api/at_cmd>",
"+<component/common/api/platform>",
"+<component/common/api/wifi>",
"+<component/common/api/wifi/rtw_wpa_supplicant/src>",
"+<component/common/api/wifi/rtw_wowlan>",
"+<component/common/api/wifi/rtw_wpa_supplicant/wpa_supplicant>",
"+<component/common/application>",
"+<component/common/application/mqtt/MQTTClient>",
"+<component/common/application/mqtt/MQTTPacket>",
"+<component/common/example>",
"+<component/common/example/wlan_fast_connect>",
"+<component/common/drivers/wlan/realtek/include>",
"+<component/common/drivers/wlan/realtek/src/osdep>",
"+<component/common/drivers/wlan/realtek/wlan_ram_map/rom>",
@@ -295,7 +225,6 @@ env.AddLibrary(
"+<component/common/network/lwip/lwip_v1.4.1/port/realtek>",
"+<component/common/network/ssl/polarssl-1.3.8/include>",
"+<component/common/network/ssl/ssl_ram_map/rom>",
"+<component/common/test>",
"+<component/common/utilities>",
"+<component/soc/realtek/8711b/app/monitor/include>",
"+<component/soc/realtek/8711b/cmsis>",
@@ -311,11 +240,61 @@ env.AddLibrary(
"+<component/common/mbed/hal_ext>",
"+<component/common/mbed/targets/cmsis>",
"+<component/common/mbed/targets/hal/rtl8711b>",
"+<component/common/application/mqtt/MQTTClient>",
"+<component/common/network/websocket>",
],
)
# 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=[
"+<component/common/network/ssl/polarssl-1.3.8/library/aesni.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/blowfish.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/camellia.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ccm.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/certs.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/cipher.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/cipher_wrap.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/debug.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ecp_ram.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/entropy.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/entropy_poll.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/error.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/gcm.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/havege.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/md2.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/md4.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/memory_buffer_alloc.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/net.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/padlock.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pbkdf2.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs11.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs12.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkcs5.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/pkparse.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/platform.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ripemd160.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_cache.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_ciphersuites.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_cli.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_srv.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/ssl_tls.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/threading.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/timing.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/version.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/version_features.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_create.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_crl.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_crt.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509_csr.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509write_crt.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/x509write_csr.c>",
"+<component/common/network/ssl/polarssl-1.3.8/library/xtea.c>",
],
)
# Sources - platform fixups
env.AddLibrary(
name="ambz_fixups",

View File

@@ -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.

View File

@@ -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"