diff --git a/RPi-Pico/CMakeLists.txt b/RPi-Pico/CMakeLists.txt index 54b2e6c64..17c3722ff 100644 --- a/RPi-Pico/CMakeLists.txt +++ b/RPi-Pico/CMakeLists.txt @@ -3,11 +3,11 @@ cmake_minimum_required(VERSION 3.13) # Pull in Pico and FreeRTOS include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake) #include(pico_extras_import_optional.cmake) -#include($ENV{FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) +include($ENV{FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) if(PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0") - message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}") + message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0(or later) required. Your version is ${PICO_SDK_VERSION_STRING}") endif() project(wolf_pico_examples C CXX ASM) @@ -31,214 +31,27 @@ endif() include_directories(config) include_directories(include) include_directories(${PICO_SDK_PATH}/src/rp2_common/pico_lwip/include) + include_directories(${PICO_SDK_PATH}/src/rp2_common/hardware_rtc/include) include_directories(${PICO_SDK_PATH}/lib/lwip/contrib/ports/freertos/include) include_directories(${PICO_SDK_PATH}/lib/lwip/src/include) include_directories(${PICO_SDK_PATH}/src/rp2_common/pico_async_context/include) + #include_directories(${PICO_SDK_PATH}/lib/btstack/test/embedded/) set(WOLFSSL_ROOT $ENV{WOLFSSL_ROOT}) + set(FREERTOS_KERNEL_PATH $ENV{FREERTOS_KERNEL_PATH}) + include_directories(${FREERTOS_KERNEL_PATH}/include/) + include_directories(${FREERTOS_KERNEL_PATH}/portable/ThirdParty/GCC/RP2040/include) include_directories(${WOLFSSL_ROOT}) ### End of Global Include Path + include(wolfssl_import.cmake) -### wolfSSL/wolfCrypt library - file(GLOB WOLFSSL_SRC - "${WOLFSSL_ROOT}/src/*.c" - "${WOLFSSL_ROOT}/wolfcrypt/src/*.c" - "${WOLFSSL_ROOT}/wolfcrypt/src/port/rpi_pico/*" - ) - list(REMOVE_ITEM WOLFSSL_SRC EXCLUDE REGEX - "${WOLFSSL_ROOT}/src/bio.c" - "${WOLFSSL_ROOT}/src/conf.c" - "${WOLFSSL_ROOT}/src/pk.c" - "${WOLFSSL_ROOT}/src/ssl_asn1.c" - "${WOLFSSL_ROOT}/src/ssl_bn.c" - "${WOLFSSL_ROOT}/src/ssl_misc.c" - "${WOLFSSL_ROOT}/src/x509.c" - "${WOLFSSL_ROOT}/src/x509_str.c" - "${WOLFSSL_ROOT}/wolfcrypt/src/evp.c" - "${WOLFSSL_ROOT}/wolfcrypt/src/misc.c" - ) - - add_library(wolfssl STATIC - ${WOLFSSL_SRC} - ) - - target_compile_definitions(wolfssl PUBLIC - WOLFSSL_USER_SETTINGS - ) - if (${PICO_PLATFORM} STREQUAL "rp2350-arm-s") - add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM) - elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv") - add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32) - else() - add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM) - endif() - - target_link_libraries(wolfssl - pico_stdlib - pico_rand - ) -### End of wolfSSL/wolfCrypt library - - -### Test wolfCrypt algorithms - add_executable(testwolfcrypt - src/test_main.c - ${WOLFSSL_ROOT}/wolfcrypt/test/test.c - ) - - target_link_libraries(testwolfcrypt - wolfssl - pico_stdlib - pico_rand - ) - - if (USE_UART) - pico_enable_stdio_usb(testwolfcrypt 0) - pico_enable_stdio_uart(testwolfcrypt 1) - else() - pico_enable_stdio_usb(testwolfcrypt 1) - pico_enable_stdio_uart(testwolfcrypt 0) - endif() - - pico_add_extra_outputs(testwolfcrypt) -### End of Test wolfCrypt algorithms - - -### Benchmark wolfCrypt algorithms - add_executable(benchmark - src/bench_main.c - ${WOLFSSL_ROOT}/wolfcrypt/benchmark/benchmark.c - ) - - target_link_libraries(benchmark - wolfssl - pico_stdlib - pico_rand - ) - - if (USE_UART) - pico_enable_stdio_usb(benchmark 0) - pico_enable_stdio_uart(benchmark 1) - else() - pico_enable_stdio_usb(benchmark 1) - pico_enable_stdio_uart(benchmark 0) - endif() - - - pico_add_extra_outputs(benchmark) -### End of Benchmark wolfCrypt algorithms - - -if (USE_WIFI) -### Wifi connection - add_executable(Wifi - src/blink.c - src/wifi.c - src/wifi_main.c - ) - - target_compile_definitions(Wifi PRIVATE - WIFI_SSID=\"${WIFI_SSID}\" - WIFI_PASSWORD=\"${WIFI_PASSWORD}\" - PICO_CYW43_ARCH_POLL - NO_SYS=1 - ) - - target_link_libraries(Wifi - pico_stdlib - pico_rand - pico_lwip - pico_cyw43_arch - pico_lwip_nosys - pico_async_context_poll - ) - - if (USE_UART) - pico_enable_stdio_usb(Wifi 0) - pico_enable_stdio_uart(Wifi 1) - else() - pico_enable_stdio_usb(Wifi 1) - pico_enable_stdio_uart(Wifi 0) - endif() - - pico_add_extra_outputs(Wifi) -### End of Wifi connection -endif() - -if (USE_WIFI) -### TCP Client - add_executable(tcp_Client - src/blink.c - src/wifi.c - src/tcp.c - src/tcpClient_main.c - ) - - target_compile_definitions(tcp_Client PRIVATE - WIFI_SSID=\"${WIFI_SSID}\" - WIFI_PASSWORD=\"${WIFI_PASSWORD}\" - TEST_TCP_SERVER_IP=\"${TEST_TCP_SERVER_IP}\" - PICO_CYW43_ARCH_POLL - NO_SYS=1 - ) - - target_link_libraries(tcp_Client - pico_stdlib - pico_rand - pico_lwip - pico_cyw43_arch - pico_lwip_nosys - pico_async_context_poll - ) - - if (USE_UART) - pico_enable_stdio_usb(tcp_Client 0) - pico_enable_stdio_uart(tcp_Client 1) - else() - pico_enable_stdio_usb(tcp_Client 1) - pico_enable_stdio_uart(tcp_Client 0) + add_subdirectory(testwolfcrypt) + add_subdirectory(benchmark) + if (USE_WIFI) + add_subdirectory(wifi) + add_subdirectory(tcp_client) + add_subdirectory(tcp_server) + add_subdirectory(tls_client) + add_subdirectory(tls_server) endif() - - pico_add_extra_outputs(tcp_Client) -### End of TCP Client -endif() - -if (USE_WIFI) -### TLS Client - add_executable(tls_Client - src/blink.c - src/wifi.c - src/tcp.c - src/tlsClient_main.c - ) - - target_compile_definitions(tls_Client PRIVATE - WIFI_SSID=\"${WIFI_SSID}\" - WIFI_PASSWORD=\"${WIFI_PASSWORD}\" - TEST_TCP_SERVER_IP=\"${TEST_TCP_SERVER_IP}\" - PICO_CYW43_ARCH_POLL - NO_SYS=1 - ) - - target_link_libraries(tls_Client - pico_stdlib - pico_rand - pico_lwip - pico_cyw43_arch - pico_lwip_nosys - pico_async_context_poll - wolfssl - ) - - if (USE_UART) - pico_enable_stdio_usb(tls_Client 0) - pico_enable_stdio_uart(tls_Client 1) - else() - pico_enable_stdio_usb(tls_Client 1) - pico_enable_stdio_uart(tls_Client 0) - endif() - - pico_add_extra_outputs(tls_Client) -### End of TLS Client -endif() diff --git a/RPi-Pico/README.md b/RPi-Pico/README.md index 70cdd46be..4b4b63d13 100644 --- a/RPi-Pico/README.md +++ b/RPi-Pico/README.md @@ -38,7 +38,13 @@ export WOLFSSL_ROOT=/path/to/wolfssl/source export PICO_SDK_PATH=/path/to/pico-sdk ``` -### 3. cmake and make +### 3. Set `FREERTOS_KERNEL_PATH` +If you want to use TCP/TLS server or client, You also need to have the [FreeRTOS-Kernel GitHub repository](https://github.com/FreeRTOS/FreeRTOS-Kernel). +``` +export FREERTOS_KERNEL_PATH=/path/to/FreeRTOS-Kernel +``` + +### 4. cmake and make The following CMAKE options are available: @@ -53,13 +59,27 @@ To use the RP2350 in RISC-V mode, add `-DPICO_PLATFORM=rp2350-riscv`. ``` $ cd RPi-Pico -$ mkdir build -$ cd build $ cmake -DPICO_BOARD=pico_w .. $ make ``` -### 4. Upload to the Pico +The build produces the following UF2 images: + +- testwolfcrypt.uf2 + +- benchmark.uf2 + +- Wifi.uf2 + +- tcp_Client.uf2 + +- tls_Client.uf2 + +- tcp_Server.uf2 + +- tls_Server.uf2 + +### 5. Upload to the Pico Hold the boot button and plug the Pico into your computer, you can then drag/drop a `.uf2` to the Pico. It will stop becoming a USB mass storage device @@ -71,7 +91,7 @@ sudo picotool load benchmark.uf2 sudo picotool reboot ``` -### 5. Serial output +### 6. Serial output If you have not set `USE_UART`, once rebooted the USB port will turn into an "Abstract Control Module" serial port. On Linux this will likely be diff --git a/RPi-Pico/benchmark/CMakeLists.txt b/RPi-Pico/benchmark/CMakeLists.txt new file mode 100644 index 000000000..191f5dfa7 --- /dev/null +++ b/RPi-Pico/benchmark/CMakeLists.txt @@ -0,0 +1,24 @@ +add_executable(benchmark + ../src/bench_main.c + ${WOLFSSL_ROOT}/wolfcrypt/benchmark/benchmark.c +) + +target_link_libraries(benchmark + wolfssl + pico_stdlib + pico_rand +) + +target_compile_definitions(benchmark PRIVATE + WOLFSSL_RPI_PICO +) + +if (USE_UART) +pico_enable_stdio_usb(benchmark 0) +pico_enable_stdio_uart(benchmark 1) +else() +pico_enable_stdio_usb(benchmark 1) +pico_enable_stdio_uart(benchmark 0) +endif() + +pico_add_extra_outputs(benchmark) diff --git a/RPi-Pico/config/FreeRTOSConfig.h b/RPi-Pico/config/FreeRTOSConfig.h new file mode 100644 index 000000000..4ce18aa54 --- /dev/null +++ b/RPi-Pico/config/FreeRTOSConfig.h @@ -0,0 +1,108 @@ +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ((TickType_t)1000) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE (configSTACK_DEPTH_TYPE)(256*3) +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +// todo need this for lwip FreeRTOS sys_arch to compile +#define configENABLE_BACKWARD_COMPATIBILITY 1 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 1 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128 * 1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 1 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 0 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +#if FREE_RTOS_KERNEL_SMP // set by the RP2040 SMP port of FreeRTOS +/* SMP port only */ +#define configNUM_CORES 2 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 1 +#define configUSE_CORE_AFFINITY 1 +#endif + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 + +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ diff --git a/RPi-Pico/config/lwipopts.h b/RPi-Pico/config/lwipopts.h index 7c032ae2c..aff4ddd57 100644 --- a/RPi-Pico/config/lwipopts.h +++ b/RPi-Pico/config/lwipopts.h @@ -7,28 +7,27 @@ // This example uses a common include to avoid repetition #include "lwipopts_examples_common.h" -#if !NO_SYS -#define TCPIP_THREAD_STACKSIZE 1024 -#define DEFAULT_THREAD_STACKSIZE 1024 +#define TCPIP_THREAD_STACKSIZE (1024*10) +#define DEFAULT_THREAD_STACKSIZE 1024*10 #define DEFAULT_RAW_RECVMBOX_SIZE 8 #define TCPIP_MBOX_SIZE 8 +#define DEFAULT_ACCEPTMBOX_SIZE 8 #define LWIP_TIMEVAL_PRIVATE 0 -// not necessary, can be done either way -#define LWIP_TCPIP_CORE_LOCKING_INPUT 1 - // ping_thread sets socket receive timeout, so enable this feature #define LWIP_SO_RCVTIMEO 1 -#endif - - #define LWIP_TIMEVAL_PRIVATE 0 #define LWIP_MPU_COMPATIBLE 0 -#define LWIP_PROVIDE_ERRNO + +#ifdef PICO_CYW43_ARCH_POLL #define LWIP_FREERTOS_SYS_ARCH_PROTECT_USES_MUTEX 1 +#endif + +#define MEMP_NUM_SYS_TIMEOUT 10 +#define DEFAULT_TCP_RECVMBOX_SIZE 10 -/* wolfTCP config */ -#define WOLF_SOCKET 1 +#undef MEM_SIZE +#define MEM_SIZE 4000 * 4 #endif diff --git a/RPi-Pico/config/lwipopts_examples_common.h b/RPi-Pico/config/lwipopts_examples_common.h index 891a5e382..217cb1357 100644 --- a/RPi-Pico/config/lwipopts_examples_common.h +++ b/RPi-Pico/config/lwipopts_examples_common.h @@ -1,89 +1,90 @@ #ifndef _LWIPOPTS_EXAMPLE_COMMONH_H #define _LWIPOPTS_EXAMPLE_COMMONH_H + // Common settings used in most of the pico_w examples // (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) // allow override in some examples #ifndef NO_SYS -#define NO_SYS 0 +#define NO_SYS 1 #endif // allow override in some examples #ifndef LWIP_SOCKET -#define LWIP_SOCKET 0 +#define LWIP_SOCKET 0 #endif #if PICO_CYW43_ARCH_POLL -#define MEM_LIBC_MALLOC 1 +#define MEM_LIBC_MALLOC 1 #else // MEM_LIBC_MALLOC is incompatible with non polling versions -#define MEM_LIBC_MALLOC 0 +#define MEM_LIBC_MALLOC 0 #endif -#define MEM_ALIGNMENT 4 -#define MEM_SIZE 4000 -#define MEMP_NUM_TCP_SEG 32 -#define MEMP_NUM_ARP_QUEUE 10 -#define PBUF_POOL_SIZE 24 -#define LWIP_ARP 1 -#define LWIP_ETHERNET 1 -#define LWIP_ICMP 1 -#define LWIP_RAW 1 -#define TCP_WND (8 * TCP_MSS) -#define TCP_MSS 1460 -#define TCP_SND_BUF (8 * TCP_MSS) -#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) -#define LWIP_NETIF_STATUS_CALLBACK 1 -#define LWIP_NETIF_LINK_CALLBACK 1 -#define LWIP_NETIF_HOSTNAME 1 -#define LWIP_NETCONN 0 -#define MEM_STATS 0 -#define SYS_STATS 0 -#define MEMP_STATS 0 -#define LINK_STATS 0 +#define MEM_ALIGNMENT 4 +#define MEM_SIZE 4000 +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 // #define ETH_PAD_SIZE 2 -#define LWIP_CHKSUM_ALGORITHM 3 -#define LWIP_DHCP 1 -#define LWIP_IPV4 1 -#define LWIP_TCP 1 -#define LWIP_UDP 1 -#define LWIP_DNS 1 -#define LWIP_TCP_KEEPALIVE 1 -#define LWIP_NETIF_TX_SINGLE_PBUF 1 -#define DHCP_DOES_ARP_CHECK 0 -#define LWIP_DHCP_DOES_ACD_CHECK 0 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 #ifndef NDEBUG -#define LWIP_DEBUG 1 -#define LWIP_STATS 1 -#define LWIP_STATS_DISPLAY 1 +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 #endif -#define ETHARP_DEBUG LWIP_DBG_OFF -#define NETIF_DEBUG LWIP_DBG_OFF -#define PBUF_DEBUG LWIP_DBG_OFF -#define API_LIB_DEBUG LWIP_DBG_OFF -#define API_MSG_DEBUG LWIP_DBG_OFF -#define SOCKETS_DEBUG LWIP_DBG_OFF -#define ICMP_DEBUG LWIP_DBG_OFF -#define INET_DEBUG LWIP_DBG_OFF -#define IP_DEBUG LWIP_DBG_OFF -#define IP_REASS_DEBUG LWIP_DBG_OFF -#define RAW_DEBUG LWIP_DBG_OFF -#define MEM_DEBUG LWIP_DBG_OFF -#define MEMP_DEBUG LWIP_DBG_OFF -#define SYS_DEBUG LWIP_DBG_OFF -#define TCP_DEBUG LWIP_DBG_OFF -#define TCP_INPUT_DEBUG LWIP_DBG_OFF -#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF -#define TCP_RTO_DEBUG LWIP_DBG_OFF -#define TCP_CWND_DEBUG LWIP_DBG_OFF -#define TCP_WND_DEBUG LWIP_DBG_OFF -#define TCP_FR_DEBUG LWIP_DBG_OFF -#define TCP_QLEN_DEBUG LWIP_DBG_OFF -#define TCP_RST_DEBUG LWIP_DBG_OFF -#define UDP_DEBUG LWIP_DBG_OFF -#define TCPIP_DEBUG LWIP_DBG_OFF -#define PPP_DEBUG LWIP_DBG_OFF -#define SLIP_DEBUG LWIP_DBG_OFF -#define DHCP_DEBUG LWIP_DBG_OFF +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF #endif /* __LWIPOPTS_H__ */ diff --git a/RPi-Pico/include/wolf/blink.h b/RPi-Pico/include/wolf/blink.h index 0139dba2c..7ea7f282c 100644 --- a/RPi-Pico/include/wolf/blink.h +++ b/RPi-Pico/include/wolf/blink.h @@ -1,6 +1,6 @@ /* blink.h * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,6 +19,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ +#ifndef BLINK_H +#define BLINK_H + void blink(int n, int init); -#define WOLF_BLINK_INIT 1 \ No newline at end of file +#define WOLF_BLINK_INIT 1 + +#endif diff --git a/RPi-Pico/include/wolf/common.h b/RPi-Pico/include/wolf/common.h index 61520e411..76620394f 100644 --- a/RPi-Pico/include/wolf/common.h +++ b/RPi-Pico/include/wolf/common.h @@ -1,6 +1,6 @@ -/* wolf_common.h +/* common.h * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,5 +19,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ +#ifndef COMMON_H +#define COMMON_H + #define WOLF_SUCCESS 0 -#define WOLF_FAIL -1 \ No newline at end of file +#define WOLF_FAIL -1 + +#endif diff --git a/RPi-Pico/include/wolf/tcp.h b/RPi-Pico/include/wolf/tcp.h index 25b87b152..d788995ec 100644 --- a/RPi-Pico/include/wolf/tcp.h +++ b/RPi-Pico/include/wolf/tcp.h @@ -1,6 +1,6 @@ -/* tcp.c +/* tcp.h * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,63 +19,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -#include -#include "pico/cyw43_arch.h" -#include "pico/stdlib.h" +#ifndef TCP_H +#define TCP_H -#include "lwip/ip_addr.h" - -#include "wolf/blink.h" -#include "wolf/common.h" - -/* This is just for, inet_pton, sockaddr_in */ -#undef LWIP_SOCKET -#define LWIP_SOCKET 1 -#define LWIP_IPV4 1 - -#include "lwip/sockets.h" -#include "lwip/inet.h" - -#if WOLF_SOCKET - -#undef SOCKET_T -#undef socket -#undef close -#undef inet_pton -#undef connect -#undef recv -#undef send -#define SOCKET_T WOLF_SOCKET_T -#define socket wolf_TCPsocket -#define close wolf_TCPclose -#define inet_pton wolf_inet_pton -#define connect wolf_TCPconnect -#define recv wolf_TCPread -#define send wolf_TCPwrite +void tcp_initThread(void); #endif -#define DEBUG_printf printf -#define BUF_SIZE (4096*2) - -typedef u32_t socklen_t; - -typedef struct { - struct tcp_pcb *tcp_pcb; - ip_addr_t remote_addr; - uint8_t buffer[BUF_SIZE]; - int buffer_len; - int sent_len; - bool complete; - int run_count; - bool connected; -} *WOLF_SOCKET_T; - - - -WOLF_SOCKET_T wolf_TCPsocket(void); -int wolf_inet_pton(int af, const char *ip_str, void *ip_dst); -int wolf_TCPclose(WOLF_SOCKET_T sock); -int wolf_TCPconnect(WOLF_SOCKET_T, const struct sockaddr *addr, socklen_t addrlen); -int wolf_TCPread (WOLF_SOCKET_T, unsigned char *, unsigned long); -int wolf_TCPwrite(WOLF_SOCKET_T, const unsigned char *, unsigned long); \ No newline at end of file diff --git a/RPi-Pico/wolf/blink.h b/RPi-Pico/include/wolf/time.h similarity index 87% rename from RPi-Pico/wolf/blink.h rename to RPi-Pico/include/wolf/time.h index 0139dba2c..352ed051c 100644 --- a/RPi-Pico/wolf/blink.h +++ b/RPi-Pico/include/wolf/time.h @@ -1,6 +1,6 @@ -/* blink.h +/* time.h * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,6 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -void blink(int n, int init); +#ifndef TIME_H +#define TIME_H -#define WOLF_BLINK_INIT 1 \ No newline at end of file +int time_init(void); + +#endif diff --git a/RPi-Pico/include/wolf/tls.h b/RPi-Pico/include/wolf/tls.h new file mode 100644 index 000000000..994efdc8f --- /dev/null +++ b/RPi-Pico/include/wolf/tls.h @@ -0,0 +1,28 @@ +/* tls.h + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifndef TLS_H +#define TLS_H + +int my_IORecv(WOLFSSL *ssl, char *buff, int sz, void *ctx); +int my_IOSend(WOLFSSL *ssl, char *buff, int sz, void *ctx); + +#endif diff --git a/RPi-Pico/include/wolf/wifi.h b/RPi-Pico/include/wolf/wifi.h index cabf5c5ab..9659597fc 100644 --- a/RPi-Pico/include/wolf/wifi.h +++ b/RPi-Pico/include/wolf/wifi.h @@ -1,6 +1,6 @@ /* wifi.h * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,5 +19,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ +#ifndef WIFI_H +#define WIFI_H + int wolf_wifiConnect(const char *ssid, const char *pw, uint32_t auth, uint32_t timeout); int wolf_wifiDisconnect(void); + +#endif diff --git a/RPi-Pico/src/bench_main.c b/RPi-Pico/src/bench_main.c index bc5d2eb31..3580b5a61 100644 --- a/RPi-Pico/src/bench_main.c +++ b/RPi-Pico/src/bench_main.c @@ -1,6 +1,6 @@ /* bench_main.c * - * Copyright (C) 2006-2022 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * diff --git a/RPi-Pico/src/blink.c b/RPi-Pico/src/blink.c index bb4d0ae9a..97d2e9c22 100644 --- a/RPi-Pico/src/blink.c +++ b/RPi-Pico/src/blink.c @@ -1,6 +1,6 @@ /* blink.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -22,6 +22,14 @@ #include "pico/stdlib.h" #include "pico/cyw43_arch.h" +#ifdef PICO_CYW43_ARCH_FREERTOS +#include "FreeRTOS.h" +#include "task.h" +#define SLEEP_MS vTaskDelay +#else +#define SLEEP_MS sleep_ms +#endif + void blink(int n, int init) { int i; @@ -34,17 +42,17 @@ void blink(int n, int init) while (1) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - sleep_ms(25); + SLEEP_MS(25); cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); - sleep_ms(25); + SLEEP_MS(25); } } } for (i = 0; i < n; i++) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); - sleep_ms(250); + SLEEP_MS(250); cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); - sleep_ms(250); + SLEEP_MS(250); } -} \ No newline at end of file +} diff --git a/RPi-Pico/src/idleMemory.c b/RPi-Pico/src/idleMemory.c new file mode 100644 index 000000000..615db56ea --- /dev/null +++ b/RPi-Pico/src/idleMemory.c @@ -0,0 +1,74 @@ +#include "FreeRTOS.h" +#include +#include +/* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an +implementation of vApplicationGetIdleTaskMemory() to provide the memory that is +used by the Idle task. */ +void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, + StackType_t **ppxIdleTaskStackBuffer, + uint32_t *pulIdleTaskStackSize) +{ + /* If the buffers to be provided to the Idle task are declared inside this + function then they must be declared static – otherwise they will be allocated on + the stack and so not exists after this function exits. */ + static StaticTask_t xIdleTaskTCB; + static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE]; + + /* Pass out a pointer to the StaticTask_t structure in which the Idle task’s + state will be stored. */ + *ppxIdleTaskTCBBuffer = &xIdleTaskTCB; + + /* Pass out the array that will be used as the Idle task’s stack. */ + *ppxIdleTaskStackBuffer = uxIdleTaskStack; + + /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. + Note that, as the array is necessarily of type StackType_t, + configMINIMAL_STACK_SIZE is specified in words, not bytes. */ + *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; +} +/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the +application must provide an implementation of vApplicationGetTimerTaskMemory() +to provide the memory that is used by the Timer service task. */ +void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, + StackType_t **ppxTimerTaskStackBuffer, + uint32_t *pulTimerTaskStackSize) +{ + /* If the buffers to be provided to the Timer task are declared inside this + function then they must be declared static – otherwise they will be allocated on + the stack and so not exists after this function exits. */ + static StaticTask_t xTimerTaskTCB; + static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH]; + + /* Pass out a pointer to the StaticTask_t structure in which the Timer + task’s state will be stored. */ + *ppxTimerTaskTCBBuffer = &xTimerTaskTCB; + + /* Pass out the array that will be used as the Timer task’s stack. */ + *ppxTimerTaskStackBuffer = uxTimerTaskStack; + + /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer. + Note that, as the array is necessarily of type StackType_t, + configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */ + *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; +} + +void vApplicationStackOverflowHook(TaskHandle_t xTask, + char *pcTaskName) +{ + printf("----------------------------------------------\n"); + printf("STACK OVERFLOW on %s\n", pcTaskName); + printf("----------------------------------------------\n"); + taskDISABLE_INTERRUPTS(); + for (;;) + ; +} + +void vAssertCalled(const char *pcFile, uint32_t ulLine) +{ + printf("----------------------------------------------\n"); + printf("ASSERT FAILED %s line: %d\n", pcFile, ulLine); + printf("----------------------------------------------\n"); + taskDISABLE_INTERRUPTS(); + for (;;) + ; +} diff --git a/RPi-Pico/src/tcp.c b/RPi-Pico/src/tcp.c index 148546457..5807189ed 100644 --- a/RPi-Pico/src/tcp.c +++ b/RPi-Pico/src/tcp.c @@ -1,6 +1,6 @@ /* tcp.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -19,228 +19,62 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -#include -#include "pico/cyw43_arch.h" -#include "pico/stdlib.h" +#include "lwip/init.h" +#include "lwip/tcpip.h" +#include "lwip/netif.h" +#include "lwip/ip_addr.h" +#include "lwip/ip4_addr.h" -#include "wolf/tcp.h" +#include "lwip/init.h" +#include "lwip/sockets.h" -#include "lwip/pbuf.h" -#include "lwip/tcp.h" +#include "FreeRTOS.h" +#include "task.h" -#define TEST_ITERATIONS 10 -#define POLL_TIME_S 5 +static int tcpip_initialized = 0; -static void dump_bytes(const uint8_t *p, uint32_t len) +static void my_tcpip_init_done(void *arg) { - for (; len; len--, p++) { - if (((unsigned long)p & 0x07) == 0) { - DEBUG_printf("\n"); - } - DEBUG_printf("%02x ", *p); - } - DEBUG_printf("\n"); -} - - -int wolf_TCPclose(WOLF_SOCKET_T sock) -{ - err_t err = ERR_OK; - if (sock->tcp_pcb != NULL) - { - tcp_arg(sock->tcp_pcb, NULL); - tcp_poll(sock->tcp_pcb, NULL, 0); - tcp_sent(sock->tcp_pcb, NULL); - tcp_recv(sock->tcp_pcb, NULL); - tcp_err(sock->tcp_pcb, NULL); - err = tcp_close(sock->tcp_pcb); - if (err != ERR_OK) { - DEBUG_printf("close failed %d, calling abort\n", err); - tcp_abort(sock->tcp_pcb); - err = ERR_ABRT; - } - sock->tcp_pcb = NULL; - } - free(sock); - return err; -} - -static err_t tcp_result(WOLF_SOCKET_T sock, int status) -{ - if (status == 0) { - DEBUG_printf("test success\n"); - } else { - DEBUG_printf("test failed %d\n", status); - } - sock->complete = true; - return 0; -} - -static err_t lwip_cb_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) -{ - WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; - - sock->sent_len += len; - - if (sock->sent_len >= BUF_SIZE) - { - - sock->run_count++; - if (sock->run_count >= TEST_ITERATIONS) - { - tcp_result(arg, 0); - return ERR_OK; - } - - // We should receive a new buffer from the server - sock->buffer_len = 0; - sock->sent_len = 0; - } - - return ERR_OK; -} - -static err_t lwip_cb_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) -{ - WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; - - if (err != ERR_OK) - { - DEBUG_printf("connect failed %d\n", err); - return tcp_result(arg, err); - } - sock->connected = true; - return ERR_OK; -} - -static err_t lwip_cb_client_poll(void *arg, struct tcp_pcb *tpcb) -{ - return tcp_result(arg, 0); + tcpip_initialized = 1; + printf("\ntcpip_thread initialized!\n"); } -static void lwip_cb_client_err(void *arg, err_t err) +void print_netinfo(void) { - if (err != ERR_ABRT) { - DEBUG_printf("tcp_client_err %d\n", err); - tcp_result(arg, err); + struct netif *netif = netif_list; + if (netif != NULL) { + ip4_addr_t ip = netif->ip_addr; + ip4_addr_t netmask = netif->netmask; + ip4_addr_t gw = netif->gw; + + printf("IP Addr: %s\n", ip4addr_ntoa(&ip)); + printf("Netmask: %s\n", ip4addr_ntoa(&netmask)); + printf("Gateway: %s\n", ip4addr_ntoa(&gw)); } -} - -static err_t lwip_cb_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) -{ - WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; - if (!p) { - return ERR_OK; + else { + printf("Network interface not found.\n"); } - - cyw43_arch_lwip_check(); - if (p->tot_len > 0) { - const uint16_t buffer_left = BUF_SIZE - sock->buffer_len; - sock->buffer_len += pbuf_copy_partial(p, sock->buffer + sock->buffer_len, - p->tot_len > buffer_left ? buffer_left : p->tot_len, 0); - tcp_recved(tpcb, p->tot_len); - } - pbuf_free(p); - return ERR_OK; -} - -int wolf_inet_pton(int af, const char *ip_str, void *ip_dst) -{ - (void)af; - struct sockaddr_in *addr = (struct sockaddr_in *)ip_dst; - - return ip4addr_aton(ip_str, ip_dst); } -static u32_t swapBytes(u16_t a) +void tcp_initThread(void) { - u8_t *p = (u8_t *)&a; - return p[0] << 8 | p[1]; -} + static struct netif netif; + static ip4_addr_t ipaddr, netmask, gw; -int wolf_TCPconnect(WOLF_SOCKET_T sock, const struct sockaddr *addr, socklen_t addrlen) -{ - int err; - struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; + tcpip_init(my_tcpip_init_done, NULL); - sock->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&addr_in->sin_addr)); - if (!sock->tcp_pcb) { - DEBUG_printf("failed to create pcb\n"); - return false; + while (tcpip_initialized == 0) { + printf("."); + vTaskDelay(1000); /* need to wait for initializing TCPIP */ } - tcp_arg(sock->tcp_pcb, sock); - tcp_poll(sock->tcp_pcb, lwip_cb_client_poll, POLL_TIME_S * 2); - tcp_sent(sock->tcp_pcb, lwip_cb_client_sent); - tcp_recv(sock->tcp_pcb, lwip_cb_client_recv); - tcp_err(sock->tcp_pcb, lwip_cb_client_err); - - sock->buffer_len = 0; - - cyw43_arch_lwip_begin(); - err = tcp_connect(sock->tcp_pcb, (const ip_addr_t *)&addr_in->sin_addr, - swapBytes(addr_in->sin_port), lwip_cb_client_connected); - cyw43_arch_lwip_end(); - if (err != ERR_OK) { - DEBUG_printf("wolf_TCPconnect: Failed"); - return WOLF_FAIL; - } - sock->connected = false; + IP4_ADDR(&ipaddr, 192, 168, 10, 79); + IP4_ADDR(&netmask, 255, 255, 255, 0); + IP4_ADDR(&gw, 192, 168, 10, 1); - while (sock->connected != true) - { - cyw43_arch_poll(); - cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000)); - } - return WOLF_SUCCESS; -} + netif_add(&netif, &ipaddr, &netmask, &gw, NULL, NULL, tcpip_input); + netif_set_default(&netif); + netif_set_up(&netif); -// get a new TCP client -WOLF_SOCKET_T wolf_TCPsocket() -{ - WOLF_SOCKET_T sock = calloc(1, sizeof(*sock)); - if (!sock) { - DEBUG_printf("failed to allocate state\n"); - return NULL; - } - return sock; + print_netinfo(); } - -int wolf_TCPwrite(WOLF_SOCKET_T sock, const unsigned char *buff, long unsigned int len) -{ - int ret; - int i; - - sock->sent_len = 0; - ret = tcp_write(sock->tcp_pcb, buff, len, TCP_WRITE_FLAG_COPY); - - if (ret == ERR_OK) { - tcp_output(sock->tcp_pcb); - } - sock->sent_len = 0; - return (int)len; -} - -int wolf_TCPread(WOLF_SOCKET_T sock, unsigned char *buff, long unsigned int len) -{ - int recv_len; - int remained; - int i; - - while (1) { /* no timeout for now */ - if(sock->buffer_len > 0) { - recv_len = len <= sock->buffer_len ? len : sock->buffer_len; - memcpy(buff, sock->buffer, recv_len); - if(recv_len >= len) { - remained = sock->buffer_len - recv_len; - sock->buffer_len = remained; - for(i=0; ibuffer[i] = sock->buffer[i+recv_len]; - } else - sock->buffer_len = 0; - return recv_len; - } - cyw43_arch_poll(); - cyw43_arch_wait_for_work_until(make_timeout_time_ms(10)); - } -} \ No newline at end of file diff --git a/RPi-Pico/src/tcpClient_main.c b/RPi-Pico/src/tcpClient_main.c index 7fc142432..39c983ffe 100644 --- a/RPi-Pico/src/tcpClient_main.c +++ b/RPi-Pico/src/tcpClient_main.c @@ -1,6 +1,6 @@ /* tcpClient_main.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -23,59 +23,92 @@ #include "pico/stdlib.h" #include "pico/cyw43_arch.h" -#include "wolf/common.h" -#include "wolf/tcp.h" +#include "FreeRTOS.h" +#include "task.h" + +#include "lwip/init.h" +#include "lwip/sockets.h" + #include "wolf/wifi.h" #include "wolf/blink.h" +#include "wolf/tcp.h" -#define TCP_PORT 1111 +#define TCP_PORT 11111 -void tcpClient_test(void) +void tcpClient_test(void *arg) { + (void)arg; int i; int ret; #define BUFF_SIZE 2048 char buffer[BUFF_SIZE]; - #define SIZE_OF_CLIENT_HELLO 815 - char msg[SIZE_OF_CLIENT_HELLO] = "\026\003\003\003\052\001\000\003\046 Fake Client Hello"; + #define SIZE_OF_CLIENT_HELLO 16 + char msg[SIZE_OF_CLIENT_HELLO] = "Client Hello"; - SOCKET_T sock; + int sock; struct sockaddr_in servAddr; - sock = socket(); - if (!sock) - { - printf("ERROR:wolf_TCPsocke()\n"); + cyw43_arch_init(); + + printf("Connecting to Wi-Fi...\n"); + if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 5000)) { + printf("failed to connect.\n"); return; } + else { + printf("Wifi connected.\n"); + } - memset(&servAddr, 0, sizeof(servAddr)); - servAddr.sin_family = AF_INET; /* using IPv4 */ - servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */ + lwip_init(); + tcp_initThread(); - if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) { - fprintf(stderr, "ERROR: invalid address\n"); - goto exit; - } + printf("Starting TCP client\n"); - if (connect(sock,(struct sockaddr*) &servAddr, sizeof(servAddr)) != WOLF_SUCCESS) { - printf("ERROR:wolf_TCPconnect()\n"); - goto exit; + for(i=0; i< 3; i++) { + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + printf("ERROR:socke()\n"); + return; + } + + memset(&servAddr, 0, sizeof(servAddr)); + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */ + + printf("Connecting to the server(%s)\n", TEST_TCP_SERVER_IP); + if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) { + fprintf(stderr, "ERROR: invalid address\n"); + goto exit; + } + + if ((ret = connect(sock,(struct sockaddr*) &servAddr, + sizeof(servAddr))) != EXIT_SUCCESS) { + printf("ERROR:connect(%d)\n", ret); + goto exit; + } + + printf("Writing to server: %s\n", msg); + ret = write(sock, msg, sizeof(msg)); + if (ret < 0) { + printf("Failed to write data. err=%d\n", ret); + goto exit; + } + + ret = read(sock, buffer, BUFF_SIZE); + if (ret < 0) { + printf("Failed to read data. err=%d\n", ret); + goto exit; + } + buffer[ret] = '\0'; + printf("Received message[%d]: %s\n", i, buffer); + close(sock); } - printf("Writing to server: %s\n", msg); - ret = send(sock, msg, sizeof(msg)); - if (ret < 0) { - DEBUG_printf("Failed to write data. err=%d\n", ret); - goto exit; - } - - ret = recv(sock, buffer, BUFF_SIZE); - if (ret < 0) { - DEBUG_printf("Failed to read data. err=%d\n", ret); - goto exit; - } - printf("Message: %s\n", buffer); + printf("End of TCP client\n"); + + cyw43_arch_deinit(); + + printf("Wifi disconnected\n"); exit: close(sock); @@ -83,32 +116,20 @@ void tcpClient_test(void) void main(void) { - blink(20, 1); - - cyw43_arch_enable_sta_mode(); - printf("Connecting to Wi-Fi...\n"); - printf("WIFI_SSID=%s, WIFI_PASSWORD=%s\n", WIFI_SSID, WIFI_PASSWORD); - if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { - printf("failed to connect.\n"); - return; - } else { - printf("Wifi connected.\n"); - } - cyw43_arch_lwip_begin(); + TaskHandle_t task_tcpClient; +#define STACK_SIZE (1024 * 16) - printf("Starting TCP client\n"); - tcpClient_test(); - printf("End of TCP client\n"); + int i; - cyw43_arch_lwip_end(); - cyw43_arch_deinit(); + stdio_init_all(); + for(i=0; i<10; i++) { + printf("Starting in %dSec.\n", 10 - i); + sleep_ms(1000); + } - printf("Wifi disconnected\n"); -} + printf("Creating tlsClient task, stack = %d\n", STACK_SIZE); + xTaskCreate(tcpClient_test, "WifiMainThread", STACK_SIZE, NULL, + CYW43_TASK_PRIORITY+1, &task_tcpClient); + vTaskStartScheduler(); -void lwip_example_app_platform_assert(const char *msg, int line, const char *file) -{ - printf("Assertion \"%s\" failed at line %d in %s\n", msg, line, file); - fflush(NULL); } - diff --git a/RPi-Pico/src/tcpServer_main.c b/RPi-Pico/src/tcpServer_main.c new file mode 100644 index 000000000..e9108017d --- /dev/null +++ b/RPi-Pico/src/tcpServer_main.c @@ -0,0 +1,179 @@ +/* server-tcp.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include "pico/stdlib.h" +#include "pico/cyw43_arch.h" + +#include "FreeRTOS.h" +#include "task.h" + +#include "lwip/init.h" +#include "lwip/sockets.h" + +#include "wolf/wifi.h" +#include "wolf/blink.h" +#include "wolf/tcp.h" + +#define DEFAULT_PORT 11111 + +void tcpServer_test(void *arg) +{ + (void)arg; + int ret; + int sockfd; + int connd; + struct sockaddr_in servAddr; + struct sockaddr_in clientAddr; + socklen_t size = sizeof(clientAddr); + char buff[256]; + size_t len; + int shutdown = 0; + const char *reply = "I hear ya fa shizzle!\n"; + + cyw43_arch_init(); + + printf("Connecting to Wi-Fi...\n"); + if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 5000)) { + printf("failed to connect.\n"); + return; + } + else { + printf("Wifi connected.\n"); + } + + lwip_init(); + tcp_initThread(); + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + fprintf(stderr, "ERROR: failed to create the socket\n"); + ret = -1; + goto end; + } + printf("sockfd = %d\n", sockfd); + /* Initialize the server address struct with zeros */ + memset(&servAddr, 0, sizeof(servAddr)); + + /* Fill in the server address */ + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ + servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */ + printf("binding\n"); + /* Bind the server socket to our port */ + if (bind(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) + { + fprintf(stderr, "ERROR: failed to bind\n"); + ret = -1; + goto servsocket_cleanup; + } + printf("listening\n"); + /* Listen for a new connection, allow 5 pending connections */ + if (listen(sockfd, 5) < 0) + { + fprintf(stderr, "ERROR: failed to listen\n"); + ret = -1; + goto servsocket_cleanup; + } + + /* Continue to accept clients until shutdown is issued */ + while (!shutdown) + { + printf("Waiting for a connection...\n"); + + /* Accept client connections */ + if ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size)) < 0) + { + fprintf(stderr, "ERROR: failed to accept the connection\n\n"); + ret = -1; + goto servsocket_cleanup; + } + + printf("Client connected successfully\n"); + + /* Read the client data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = read(connd, buff, sizeof(buff) - 1)) < 0) + { + fprintf(stderr, "ERROR: failed to read\n"); + goto clientsocket_cleanup; + } + + /* Print to stdout any data the client sends */ + printf("Client: %s\n", buff); + + /* Check for server shutdown command */ + if (strncmp(buff, "shutdown", 8) == 0) + { + printf("Shutdown command issued!\n"); + shutdown = 1; + } + + /* Write our reply into buff */ + memset(buff, 0, sizeof(buff)); + memcpy(buff, reply, strlen(reply)); + len = strnlen(buff, sizeof(buff)); + + /* Reply back to the client */ + if ((ret = write(connd, buff, len)) != len) + { + fprintf(stderr, "ERROR: failed to write\n"); + goto clientsocket_cleanup; + } + + /* Cleanup after this connection */ + close(connd); /* Close the connection to the client */ + } + + printf("Shutdown complete\n"); + + /* Cleanup and return */ +clientsocket_cleanup: + close(connd); /* Close the connection to the client */ +servsocket_cleanup: + close(sockfd); /* Close the socket listening for clients */ +end: + return; /* Return reporting a success */ + +} + +void main(void) +{ + TaskHandle_t task_tcpServer; +#define STACK_SIZE (1024 * 16) + + int i; + + stdio_init_all(); + for (i = 0; i < 10; i++) + { + printf("Starting in %dSec.\n", 10 - i); + sleep_ms(1000); + } + + printf("Creating tcpServer task, stack = %d\n", STACK_SIZE); + xTaskCreate(tcpServer_test, "WifiMainThread", STACK_SIZE, NULL, + CYW43_TASK_PRIORITY + 1, &task_tcpServer); + vTaskStartScheduler(); +} diff --git a/RPi-Pico/src/test_main.c b/RPi-Pico/src/test_main.c index de7d27f36..853a0598f 100644 --- a/RPi-Pico/src/test_main.c +++ b/RPi-Pico/src/test_main.c @@ -1,6 +1,6 @@ /* bench_main.c * - * Copyright (C) 2006-2022 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * diff --git a/RPi-Pico/src/time.c b/RPi-Pico/src/time.c new file mode 100644 index 000000000..6866b9e86 --- /dev/null +++ b/RPi-Pico/src/time.c @@ -0,0 +1,44 @@ + +#include +#include +#include "pico/stdlib.h" + +#include +#include "FreeRTOS.h" +#include "task.h" + +static long epoch_base; + +time_t myTime(time_t *tt) +{ + return epoch_base + (xTaskGetTickCount() / configTICK_RATE_HZ); +} + +int time_init() +{ + char datetime[] = DATETIME; + struct tm t; + + printf("Date/Time = %s\n", datetime); + + if(strlen(datetime) != 12) { + printf("Invalid Date/Time length(%s)\n", datetime); + return -1; + } + if (sscanf(datetime, "%02d%02d%02d%02d%02d%02d", + &t.tm_year, &t.tm_mon, &t.tm_mday, + &t.tm_hour, &t.tm_min, &t.tm_sec) != 6) { + printf("Invalid Date/Time(%s)\n", datetime); + return -1; + } + printf("%d, %d, %d, %d, %d, %d\n", + t.tm_year, t.tm_mon, t.tm_mday, + t.tm_hour, t.tm_min, &t.tm_sec); + if (t.tm_year < 70) + t.tm_year += 100; /* base year of 1900 */ + t.tm_mon--; + epoch_base = mktime(&t); + printf("epoch base = %d\n", epoch_base); + return 0; + +} diff --git a/RPi-Pico/src/tls.c b/RPi-Pico/src/tls.c new file mode 100644 index 000000000..69fed1a4d --- /dev/null +++ b/RPi-Pico/src/tls.c @@ -0,0 +1,135 @@ +/* tls.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include "lwip/init.h" +#include "lwip/sockets.h" + +#include "wolfssl/wolfcrypt/settings.h" +#include "wolfssl/ssl.h" + +int my_IORecv(WOLFSSL *ssl, char *buff, int sz, void *ctx) +{ + /* By default, ctx will be a pointer to the file descriptor to read from. + * This can be changed by calling wolfSSL_SetIOReadCtx(). */ + int sockfd = *(int *)ctx; + int recvd; + + /* Receive message from socket */ + if ((recvd = recv(sockfd, buff, sz, 0)) == -1) + { + /* error encountered. Be responsible and report it in wolfSSL terms */ + + fprintf(stderr, "IO RECEIVE ERROR: "); + switch (errno) + { +#if EAGAIN != EWOULDBLOCK + case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */ +#endif + case EWOULDBLOCK: + if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) + { + fprintf(stderr, "would block\n"); + return WOLFSSL_CBIO_ERR_WANT_READ; + } + else + { + fprintf(stderr, "socket timeout\n"); + return WOLFSSL_CBIO_ERR_TIMEOUT; + } + case ECONNRESET: + fprintf(stderr, "connection reset\n"); + return WOLFSSL_CBIO_ERR_CONN_RST; + case EINTR: + fprintf(stderr, "socket interrupted\n"); + return WOLFSSL_CBIO_ERR_ISR; + case ECONNREFUSED: + fprintf(stderr, "connection refused\n"); + return WOLFSSL_CBIO_ERR_WANT_READ; + case ECONNABORTED: + fprintf(stderr, "connection aborted\n"); + return WOLFSSL_CBIO_ERR_CONN_CLOSE; + default: + fprintf(stderr, "general error\n"); + return WOLFSSL_CBIO_ERR_GENERAL; + } + } + else if (recvd == 0) + { + printf("Connection closed\n"); + return WOLFSSL_CBIO_ERR_CONN_CLOSE; + } + + /* successful receive */ + #ifdef WOLFSSL_DEBUG + printf("my_IORecv: received %d bytes from %d\n", sz, sockfd); + #endif + + return recvd; +} + +int my_IOSend(WOLFSSL *ssl, char *buff, int sz, void *ctx) +{ + /* By default, ctx will be a pointer to the file descriptor to write to. + * This can be changed by calling wolfSSL_SetIOWriteCtx(). */ + int sockfd = *(int *)ctx; + int sent; + + /* Receive message from socket */ + if ((sent = send(sockfd, buff, sz, 0)) == -1) + { + /* error encountered. Be responsible and report it in wolfSSL terms */ + + fprintf(stderr, "IO SEND ERROR: "); + switch (errno) + { +#if EAGAIN != EWOULDBLOCK + case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */ +#endif + case EWOULDBLOCK: + fprintf(stderr, "would block\n"); + return WOLFSSL_CBIO_ERR_WANT_WRITE; + case ECONNRESET: + fprintf(stderr, "connection reset\n"); + return WOLFSSL_CBIO_ERR_CONN_RST; + case EINTR: + fprintf(stderr, "socket interrupted\n"); + return WOLFSSL_CBIO_ERR_ISR; + case EPIPE: + fprintf(stderr, "socket EPIPE\n"); + return WOLFSSL_CBIO_ERR_CONN_CLOSE; + default: + fprintf(stderr, "general error\n"); + return WOLFSSL_CBIO_ERR_GENERAL; + } + } + else if (sent == 0) + { + printf("Connection closed\n"); + return 0; + } + + /* successful send */ + #ifdef WOLFSSL_DEBUG + printf("my_IOSend: sent %d bytes to %d\n", sz, sockfd); + #endif + + return sent; +} diff --git a/RPi-Pico/src/tlsClient_main.c b/RPi-Pico/src/tlsClient_main.c index 7b384ea4d..dfa7eb42b 100644 --- a/RPi-Pico/src/tlsClient_main.c +++ b/RPi-Pico/src/tlsClient_main.c @@ -1,6 +1,6 @@ /* tcpClient_main.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -23,173 +23,182 @@ #include "pico/stdlib.h" #include "pico/cyw43_arch.h" +#include "FreeRTOS.h" +#include "task.h" + +#include "lwip/init.h" +#include "lwip/sockets.h" + #include "wolfssl/wolfcrypt/settings.h" #include "wolfssl/ssl.h" -#include "wolf/common.h" -#include "wolf/tcp.h" #include "wolf/wifi.h" #include "wolf/blink.h" -#include "lwip/tcp.h" - -#include "wolfssl/wolfcrypt/settings.h" -#include "wolfssl/ssl.h" +#include "wolf/tcp.h" +#include "wolf/tls.h" +#include "wolf/time.h" #define USE_CERT_BUFFERS_256 #define USE_CERT_BUFFERS_2048 #include -#define TCP_PORT 1111 +#define TCP_PORT 11111 +#define WIFI_TASK_PRIORITY (tskIDLE_PRIORITY + 2UL) -int wolf_cb_TCPwrite(WOLFSSL *ssl, const unsigned char *buff, long unsigned int len, void *ctx) -{ - (void)ssl; - unsigned long ret; - SOCKET_T sock = (SOCKET_T)ctx; - ret = send(sock, buff, len); - return ret; -} - -int wolf_cb_TCPread(WOLFSSL *ssl, unsigned char *buff, long unsigned int len, void *ctx) -{ - (void)ssl; - SOCKET_T sock = (SOCKET_T)ctx; - int ret; - - ret = recv(sock, buff, len); - return ret; -} - -void tlsClient_test(void) +void tlsClient_test(void *arg) { + (void)arg; int i; int ret; #define BUFF_SIZE 2048 static char buffer[BUFF_SIZE]; char msg[] = "Hello Server"; - SOCKET_T sock; + int sock; struct sockaddr_in servAddr; WOLFSSL_CTX *ctx = NULL; WOLFSSL *ssl = NULL; - /* Initialize wolfSSL */ - wolfSSL_Init(); - wolfSSL_Debugging_ON(); + cyw43_arch_init(); - if ((ctx = wolfSSL_CTX_new((wolfTLSv1_2_client_method()))) == NULL) { - printf("ERROR:wolfSSL_CTX_new()\n"); + printf("Connecting to Wi-Fi...\n"); + if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, + CYW43_AUTH_WPA2_AES_PSK, 5000)) { + printf("failed to connect.\n"); return; } - /* Load client certificates into WOLFSSL_CTX */ - if ((ret = wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048, - sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1)) != WOLFSSL_SUCCESS) { - printf("ERROR: failed to load CA cert. %d\n", ret); - goto exit; + else { + printf("Wifi connected.\n"); } - wolfSSL_SetIORecv(ctx, (CallbackIORecv)wolf_cb_TCPread); - wolfSSL_SetIOSend(ctx, (CallbackIOSend)wolf_cb_TCPwrite); - - sock = socket(); - if (!sock) - { - printf("ERROR:wolf_TCPsocke()\n"); - return; - } + lwip_init(); + tcp_initThread(); - memset(&servAddr, 0, sizeof(servAddr)); - servAddr.sin_family = AF_INET; /* using IPv4 */ - servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */ + /* Initialize wolfSSL */ + wolfSSL_Init(); + wolfSSL_Debugging_ON(); - if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) { - fprintf(stderr, "ERROR: invalid address\n"); - goto exit; - } + printf("\nStarting tlsClient_test\n"); - if (connect(sock,(struct sockaddr*) &servAddr, sizeof(servAddr)) != WOLF_SUCCESS) { - printf("ERROR:wolf_TCPconnect()\n"); - goto exit; + if(time_init() < 0) { + printf("ERROR:time_init()\n"); + return; } - - if ((ssl = wolfSSL_new(ctx)) == NULL) { - fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); - ret = -1; - goto exit; + if ((ctx = wolfSSL_CTX_new((wolfTLSv1_2_client_method()))) == NULL) { + printf("ERROR:wolfSSL_CTX_new()\n"); + return; } - wolfSSL_SetIOReadCtx(ssl, sock); - wolfSSL_SetIOWriteCtx(ssl, sock); + printf("wolfSSL_CTX_new\n"); + /* Register callbacks */ + wolfSSL_CTX_SetIORecv(ctx, my_IORecv); + wolfSSL_CTX_SetIOSend(ctx, my_IOSend); - printf("TLS Connecting\n"); - if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) { - fprintf(stderr, "ERROR: failed to connect to wolfSSL(%d)\n", - wolfSSL_get_error(ssl, ret)); + /* Load client certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048, + sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1)) != WOLFSSL_SUCCESS) { + printf("ERROR: failed to load CA cert. %d\n", ret); goto exit; } - printf("Writing to server: %s\n", msg); - ret = wolfSSL_write(ssl, msg, strlen(msg)); - if (ret < 0) { - DEBUG_printf("Failed to write data. err=%d\n", ret); - goto exit; + printf("wolfSSL_CTX_load_verify_buffer\n"); + + for(i=0; i<10; i++) { + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + printf("ERROR:wolf_TCPsocke()\n"); + return; + } + + printf("Retured from socket\n"); + + memset(&servAddr, 0, sizeof(servAddr)); + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(TCP_PORT); /* on DEFAULT_PORT */ + + printf("Connecting to the server(%s)\n", TEST_TCP_SERVER_IP); + if (inet_pton(AF_INET, TEST_TCP_SERVER_IP, &servAddr.sin_addr) != 1) { + fprintf(stderr, "ERROR: invalid address\n"); + goto exit; + } + + if ((ret = connect(sock, (struct sockaddr *)&servAddr, + sizeof(servAddr))) != EXIT_SUCCESS) { + printf("ERROR:connect(%d)\n", ret); + goto exit; + } + + printf("TCP connected\n"); + + if ((ssl = wolfSSL_new(ctx)) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); + ret = -1; + goto exit; + } + + if ((ret = wolfSSL_set_fd(ssl, sock)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: Failed to set the file descriptor\n"); + goto exit; + } + + printf("TLS Connecting\n"); + if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to connect to wolfSSL(%d)\n", + wolfSSL_get_error(ssl, ret)); + goto exit; + } + + printf("Writing to server: %s\n", msg); + ret = wolfSSL_write(ssl, msg, strlen(msg)); + if (ret < 0) { + printf("Failed to write data. err=%d\n", ret); + goto exit; + } + + ret = wolfSSL_read(ssl, buffer, BUFF_SIZE); + if (ret < 0) { + printf("Failed to read data. err=%d\n", ret); + goto exit; + } + printf("Message: %s\n", buffer); + + wolfSSL_free(ssl); + close(sock); } - - ret = wolfSSL_read(ssl, buffer, BUFF_SIZE); - if (ret < 0) { - DEBUG_printf("Failed to read data. err=%d\n", ret); - goto exit; - } - printf("Message: %s\n", buffer); - + printf("End of TLS Client\n"); exit: if (ssl) - wolfSSL_free(ssl); /* Free the wolfSSL object */ - if (sock) - free(sock); /* Close the connection to the server */ + wolfSSL_free(ssl); + if (sock >= 0) + close(sock); if (ctx) - wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ - wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ + wolfSSL_CTX_free(ctx); + wolfSSL_Cleanup(); + while(1) + vTaskDelay(1000); } void main(void) { - blink(20, 1); - - cyw43_arch_enable_sta_mode(); - printf("Connecting to Wi-Fi...\n"); - printf("WIFI_SSID=%s, WIFI_PASSWORD=%s\n", WIFI_SSID, WIFI_PASSWORD); - if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { - printf("failed to connect.\n"); - return; - } else { - printf("Wifi connected.\n"); - } - cyw43_arch_lwip_begin(); + TaskHandle_t task_tlsClient; +#define STACK_SIZE (1024 * 16) - printf("Starting TLS client\n"); - tlsClient_test(); - printf("End of TLS client\n"); - - cyw43_arch_lwip_end(); - cyw43_arch_deinit(); + int i; - printf("Wifi disconnected\n"); -} + stdio_init_all(); + for (i = 0; i < 10; i++) + { + printf("Starting in %dSec.\n", 10 - i); + sleep_ms(1000); + } -void lwip_example_app_platform_assert(const char *msg, int line, const char *file) -{ - printf("Assertion \"%s\" failed at line %d in %s\n", msg, line, file); - fflush(NULL); + printf("Creating tlsClient task, stack = %d\n", STACK_SIZE); + xTaskCreate(tlsClient_test, "TLS_MainThread", STACK_SIZE, NULL, + CYW43_TASK_PRIORITY + 1, &task_tlsClient); + vTaskStartScheduler(); } - -#include -time_t myTime(time_t *t) -{ - *t = (((2023 - 1970) * 365 + (8 * 30)) * 24 * 60 * 60); - return *t; -} \ No newline at end of file diff --git a/RPi-Pico/src/tlsServer_main.c b/RPi-Pico/src/tlsServer_main.c new file mode 100644 index 000000000..b179f68e2 --- /dev/null +++ b/RPi-Pico/src/tlsServer_main.c @@ -0,0 +1,262 @@ +/* server-tls.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. (formerly known as CyaSSL) + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include "pico/stdlib.h" +#include "pico/cyw43_arch.h" + +#include "FreeRTOS.h" +#include "task.h" + +#include "lwip/init.h" +#include "lwip/sockets.h" + +#include "wolfssl/wolfcrypt/settings.h" +#include "wolfssl/ssl.h" + +#include "wolf/wifi.h" +#include "wolf/blink.h" +#include "wolf/tcp.h" +#include "wolf/tls.h" +#include "wolf/time.h" + +#define USE_CERT_BUFFERS_256 +#define USE_CERT_BUFFERS_2048 +#include + +#define DEFAULT_PORT 11111 + +void tlsServer_test(void *arg) +{ + (void) arg; + int sockfd = SOCKET_INVALID; + int connd = SOCKET_INVALID; + struct sockaddr_in servAddr; + struct sockaddr_in clientAddr; + socklen_t size = sizeof(clientAddr); + char buff[256]; + size_t len; + int shutdown = 0; + int ret; + const char *reply = "I hear ya fa shizzle!\n"; + + /* declare wolfSSL objects */ + WOLFSSL_CTX *ctx = NULL; + WOLFSSL *ssl = NULL; + WOLFSSL_CIPHER *cipher; + + cyw43_arch_init(); + + printf("Connecting to Wi-Fi...\n"); + if (wolf_wifiConnect(WIFI_SSID, WIFI_PASSWORD, + CYW43_AUTH_WPA2_AES_PSK, 5000)) { + printf("failed to connect.\n"); + return; + } + else { + printf("Wifi connected.\n"); + } + + lwip_init(); + tcp_initThread(); + + /* Initialize wolfSSL */ + wolfSSL_Init(); + wolfSSL_Debugging_ON(); + if(time_init() < 0) { + printf("ERROR:time_init()\n"); + return; + } + + printf("\nStarting tlsClient_test\n"); +#if 0 + wolfSSL_Debugging_ON(); +#endif + + /* Create a socket that uses an internet IPv4 address, + * Sets the socket to be stream based (TCP), + * 0 means choose the default protocol. */ + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + fprintf(stderr, "ERROR: failed to create the socket\n"); + ret = -1; + goto exit; + } + + /* Create and initialize WOLFSSL_CTX */ +#ifdef USE_TLSV13 + ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()); +#else + ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()); +#endif + if (ctx == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); + ret = -1; + goto exit; + } + + /* Register callbacks */ + wolfSSL_CTX_SetIORecv(ctx, my_IORecv); + wolfSSL_CTX_SetIOSend(ctx, my_IOSend); + + /* Load server certificates into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048, + sizeof_server_cert_der_2048, SSL_FILETYPE_ASN1)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to loadserver_cert_der_2048.\n"); + goto exit; + } + + /* Load server key into WOLFSSL_CTX */ + if ((ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048, + sizeof_server_key_der_2048, SSL_FILETYPE_ASN1)) != WOLFSSL_SUCCESS) { + fprintf(stderr, "ERROR: failed to load server_key_der_2048\n"); + goto exit; + } + + /* Initialize the server address struct with zeros */ + memset(&servAddr, 0, sizeof(servAddr)); + + /* Fill in the server address */ + servAddr.sin_family = AF_INET; /* using IPv4 */ + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ + servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */ + + /* Bind the server socket to our port */ + if (bind(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) { + fprintf(stderr, "ERROR: failed to bind\n"); + ret = -1; + goto exit; + } + + /* Listen for a new connection, allow 5 pending connections */ + if (listen(sockfd, 5) == -1) { + fprintf(stderr, "ERROR: failed to listen\n"); + ret = -1; + goto exit; + } + + /* Continue to accept clients until shutdown is issued */ + while (!shutdown) { + printf("Waiting for a connection...\n"); + + /* Accept client connections */ + if ((connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size)) == -1) { + fprintf(stderr, "ERROR: failed to accept the connection\n\n"); + ret = -1; + goto exit; + } + + /* Create a WOLFSSL object */ + if ((ssl = wolfSSL_new(ctx)) == NULL) { + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); + ret = -1; + goto exit; + } + + /* Attach wolfSSL to the socket */ + wolfSSL_set_fd(ssl, connd); + + /* Establish TLS connection */ + ret = wolfSSL_accept(ssl); + if (ret != WOLFSSL_SUCCESS) { + fprintf(stderr, "wolfSSL_accept error = %d\n", + wolfSSL_get_error(ssl, ret)); + goto exit; + } + + printf("Client connected successfully\n"); + + cipher = wolfSSL_get_current_cipher(ssl); + printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher)); + + /* Read the client data into our buff array */ + memset(buff, 0, sizeof(buff)); + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) == -1) { + fprintf(stderr, "ERROR: failed to read\n"); + goto exit; + } + + /* Print to stdout any data the client sends */ + printf("Client: %s\n", buff); + + /* Check for server shutdown command */ + if (strncmp(buff, "shutdown", 8) == 0) { + printf("Shutdown command issued!\n"); + shutdown = 1; + } + + /* Write our reply into buff */ + memset(buff, 0, sizeof(buff)); + memcpy(buff, reply, strlen(reply)); + len = strnlen(buff, sizeof(buff)); + + /* Reply back to the client */ + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { + fprintf(stderr, "ERROR: failed to write\n"); + goto exit; + } + + /* Notify the client that the connection is ending */ + wolfSSL_shutdown(ssl); + printf("Shutdown complete\n"); + + /* Cleanup after this connection */ + wolfSSL_free(ssl); /* Free the wolfSSL object */ + ssl = NULL; + close(connd); /* Close the connection to the client */ + } + + ret = 0; + +exit: + /* Cleanup and return */ + if (ssl) + wolfSSL_free(ssl); /* Free the wolfSSL object */ + if (connd != SOCKET_INVALID) + close(connd); /* Close the connection to the client */ + if (sockfd != SOCKET_INVALID) + close(sockfd); /* Close the socket listening for clients */ + if (ctx) + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ + + return; /* Return reporting a success */ +} + +void main(void) +{ + TaskHandle_t task_tlsServer; +#define STACK_SIZE (1024 * 16) + + int i; + + stdio_init_all(); + for (i = 0; i < 10; i++) + { + printf("Starting in %dSec.\n", 10 - i); + sleep_ms(1000); + } + + printf("Creating tlsServer task, stack = %d\n", STACK_SIZE); + xTaskCreate(tlsServer_test, "TLS_MainThread", STACK_SIZE, NULL, + CYW43_TASK_PRIORITY + 1, &task_tlsServer); + vTaskStartScheduler(); +} diff --git a/RPi-Pico/src/wifi.c b/RPi-Pico/src/wifi.c index c1642433d..c100f014d 100644 --- a/RPi-Pico/src/wifi.c +++ b/RPi-Pico/src/wifi.c @@ -1,6 +1,6 @@ /* wifi.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * diff --git a/RPi-Pico/src/wifi_main.c b/RPi-Pico/src/wifi_main.c index c5bc0c62f..d478a2263 100644 --- a/RPi-Pico/src/wifi_main.c +++ b/RPi-Pico/src/wifi_main.c @@ -1,6 +1,6 @@ /* wifi_main.c * - * Copyright (C) 2006-2023 wolfSSL Inc. + * Copyright (C) 2006-2025 wolfSSL Inc. * * This file is part of wolfSSL. * @@ -45,4 +45,4 @@ int main(int argc, char **argv) wolf_wifiDisconnect(); return 0; -} \ No newline at end of file +} diff --git a/RPi-Pico/src/wolfTcp.c b/RPi-Pico/src/wolfTcp.c new file mode 100644 index 000000000..8a44d4aba --- /dev/null +++ b/RPi-Pico/src/wolfTcp.c @@ -0,0 +1,246 @@ +/* tcp.c + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include "pico/cyw43_arch.h" +#include "pico/stdlib.h" + +#include "wolf/tcp.h" + +#include "lwip/pbuf.h" +#include "lwip/tcp.h" + +#define TEST_ITERATIONS 10 +#define POLL_TIME_S 5 + +static void dump_bytes(const uint8_t *p, uint32_t len) +{ + for (; len; len--, p++) { + if (((unsigned long)p & 0x07) == 0) { + DEBUG_printf("\n"); + } + DEBUG_printf("%02x ", *p); + } + DEBUG_printf("\n"); +} + + +int wolf_TCPclose(WOLF_SOCKET_T sock) +{ + err_t err = ERR_OK; + if (sock->tcp_pcb != NULL) + { + tcp_arg(sock->tcp_pcb, NULL); + tcp_poll(sock->tcp_pcb, NULL, 0); + tcp_sent(sock->tcp_pcb, NULL); + tcp_recv(sock->tcp_pcb, NULL); + tcp_err(sock->tcp_pcb, NULL); + err = tcp_close(sock->tcp_pcb); + if (err != ERR_OK) { + DEBUG_printf("close failed %d, calling abort\n", err); + tcp_abort(sock->tcp_pcb); + err = ERR_ABRT; + } + sock->tcp_pcb = NULL; + } + free(sock); + return err; +} + +static err_t tcp_result(WOLF_SOCKET_T sock, int status) +{ + if (status == 0) { + DEBUG_printf("test success\n"); + } else { + DEBUG_printf("test failed %d\n", status); + } + sock->complete = true; + return 0; +} + +static err_t lwip_cb_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) +{ + WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; + + sock->sent_len += len; + + if (sock->sent_len >= BUF_SIZE) + { + + sock->run_count++; + if (sock->run_count >= TEST_ITERATIONS) + { + tcp_result(arg, 0); + return ERR_OK; + } + + // We should receive a new buffer from the server + sock->buffer_len = 0; + sock->sent_len = 0; + } + + return ERR_OK; +} + +static err_t lwip_cb_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) +{ + WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; + + if (err != ERR_OK) + { + DEBUG_printf("connect failed %d\n", err); + return tcp_result(arg, err); + } + sock->connected = true; + return ERR_OK; +} + +static err_t lwip_cb_client_poll(void *arg, struct tcp_pcb *tpcb) +{ + return tcp_result(arg, 0); +} + +static void lwip_cb_client_err(void *arg, err_t err) +{ + if (err != ERR_ABRT) { + DEBUG_printf("tcp_client_err %d\n", err); + tcp_result(arg, err); + } +} + +static err_t lwip_cb_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) +{ + WOLF_SOCKET_T sock = (WOLF_SOCKET_T)arg; + if (!p) { + return ERR_OK; + } + + cyw43_arch_lwip_check(); + if (p->tot_len > 0) { + const uint16_t buffer_left = BUF_SIZE - sock->buffer_len; + sock->buffer_len += pbuf_copy_partial(p, sock->buffer + sock->buffer_len, + p->tot_len > buffer_left ? buffer_left : p->tot_len, 0); + tcp_recved(tpcb, p->tot_len); + } + pbuf_free(p); + return ERR_OK; +} + +int wolf_inet_pton(int af, const char *ip_str, void *ip_dst) +{ + (void)af; + struct sockaddr_in *addr = (struct sockaddr_in *)ip_dst; + + return ip4addr_aton(ip_str, ip_dst); +} + +static u32_t swapBytes(u16_t a) +{ + u8_t *p = (u8_t *)&a; + return p[0] << 8 | p[1]; +} + +int wolf_TCPconnect(WOLF_SOCKET_T sock, const struct sockaddr *addr, socklen_t addrlen) +{ + int err; + struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; + + sock->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&addr_in->sin_addr)); + if (!sock->tcp_pcb) { + DEBUG_printf("failed to create pcb\n"); + return false; + } + + tcp_arg(sock->tcp_pcb, sock); + tcp_poll(sock->tcp_pcb, lwip_cb_client_poll, POLL_TIME_S * 2); + tcp_sent(sock->tcp_pcb, lwip_cb_client_sent); + tcp_recv(sock->tcp_pcb, lwip_cb_client_recv); + tcp_err(sock->tcp_pcb, lwip_cb_client_err); + + sock->buffer_len = 0; + + cyw43_arch_lwip_begin(); + err = tcp_connect(sock->tcp_pcb, (const ip_addr_t *)&addr_in->sin_addr, + swapBytes(addr_in->sin_port), lwip_cb_client_connected); + cyw43_arch_lwip_end(); + if (err != ERR_OK) { + DEBUG_printf("wolf_TCPconnect: Failed"); + return WOLF_FAIL; + } + sock->connected = false; + + while (sock->connected != true) + { + cyw43_arch_poll(); + cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000)); + } + return WOLF_SUCCESS; +} + +// get a new TCP client +WOLF_SOCKET_T wolf_TCPsocket() +{ + WOLF_SOCKET_T sock = calloc(1, sizeof(*sock)); + if (!sock) { + DEBUG_printf("failed to allocate state\n"); + return NULL; + } + return sock; +} + +int wolf_TCPwrite(WOLF_SOCKET_T sock, const unsigned char *buff, long unsigned int len) +{ + int ret; + int i; + + sock->sent_len = 0; + ret = tcp_write(sock->tcp_pcb, buff, len, TCP_WRITE_FLAG_COPY); + + if (ret == ERR_OK) { + tcp_output(sock->tcp_pcb); + } + sock->sent_len = 0; + return (int)len; +} + +int wolf_TCPread(WOLF_SOCKET_T sock, unsigned char *buff, long unsigned int len) +{ + int recv_len; + int remained; + int i; + + while (1) { /* no timeout for now */ + if(sock->buffer_len > 0) { + recv_len = len <= sock->buffer_len ? len : sock->buffer_len; + memcpy(buff, sock->buffer, recv_len); + if(recv_len >= len) { + remained = sock->buffer_len - recv_len; + sock->buffer_len = remained; + for(i=0; ibuffer[i] = sock->buffer[i+recv_len]; + } else + sock->buffer_len = 0; + return recv_len; + } + cyw43_arch_poll(); + cyw43_arch_wait_for_work_until(make_timeout_time_ms(10)); + } +} diff --git a/RPi-Pico/tcp_client/CMakeLists.txt b/RPi-Pico/tcp_client/CMakeLists.txt new file mode 100644 index 000000000..c74f199cf --- /dev/null +++ b/RPi-Pico/tcp_client/CMakeLists.txt @@ -0,0 +1,34 @@ +add_executable(tcp_Client + ../src/blink.c + ../src/wifi.c + ../src/tcp.c + ../src/tcpClient_main.c + ../src/idleMemory.c +) + +target_compile_definitions(tcp_Client PRIVATE + WIFI_SSID=\"${WIFI_SSID}\" + WIFI_PASSWORD=\"${WIFI_PASSWORD}\" + TEST_TCP_SERVER_IP=\"${TEST_TCP_SERVER_IP}\" + NO_SYS=0 + LWIP_SOCKET + LWIP_COMPAT_SOCKETS + PING_USE_SOCKETS=1 +) + +target_link_libraries(tcp_Client + pico_stdlib + pico_rand + pico_cyw43_arch_lwip_sys_freertos + FreeRTOS-Kernel-Heap4 +) + +if (USE_UART) +pico_enable_stdio_usb(tcp_Client 0) +pico_enable_stdio_uart(tcp_Client 1) +else() +pico_enable_stdio_usb(tcp_Client 1) +pico_enable_stdio_uart(tcp_Client 0) +endif() + +pico_add_extra_outputs(tcp_Client) diff --git a/RPi-Pico/tcp_server/CMakeLists.txt b/RPi-Pico/tcp_server/CMakeLists.txt new file mode 100644 index 000000000..ba0431fe6 --- /dev/null +++ b/RPi-Pico/tcp_server/CMakeLists.txt @@ -0,0 +1,33 @@ +add_executable(tcp_Server + ../src/blink.c + ../src/wifi.c + ../src/tcp.c + ../src/tcpServer_main.c + ../src/idleMemory.c + ) + +target_compile_definitions(tcp_Server PRIVATE + WIFI_SSID=\"${WIFI_SSID}\" + WIFI_PASSWORD=\"${WIFI_PASSWORD}\" + NO_SYS=0 + LWIP_SOCKET + LWIP_COMPAT_SOCKETS + PING_USE_SOCKETS=1 +) + +target_link_libraries(tcp_Server + pico_stdlib + pico_rand + pico_cyw43_arch_lwip_sys_freertos + FreeRTOS-Kernel-Heap4 +) + +if (USE_UART) +pico_enable_stdio_utsb(tcp_Server 0) +pico_enable_stdio_uart(tcp_Server 1) +else() +pico_enable_stdio_usb(tcp_Server 1) +pico_enable_stdio_uart(tcp_Server 0) +endif() + +pico_add_extra_outputs(tcp_Server) diff --git a/RPi-Pico/testwolfcrypt/CMakeLists.txt b/RPi-Pico/testwolfcrypt/CMakeLists.txt new file mode 100644 index 000000000..ab7054498 --- /dev/null +++ b/RPi-Pico/testwolfcrypt/CMakeLists.txt @@ -0,0 +1,24 @@ +add_executable(testwolfcrypt + ../src/test_main.c + ${WOLFSSL_ROOT}/wolfcrypt/test/test.c +) + +target_compile_definitions(testwolfcrypt PRIVATE + WOLFSSL_USER_SETTINGS +) + +target_link_libraries(testwolfcrypt + wolfssl + pico_stdlib + pico_rand +) + +if (USE_UART) +pico_enable_stdio_usb(testwolfcrypt 0) +pico_enable_stdio_uart(testwolfcrypt 1) +else() +pico_enable_stdio_usb(testwolfcrypt 1) +pico_enable_stdio_uart(testwolfcrypt 0) +endif() + +pico_add_extra_outputs(testwolfcrypt) diff --git a/RPi-Pico/tls_client/CMakeLists.txt b/RPi-Pico/tls_client/CMakeLists.txt new file mode 100644 index 000000000..7a9a31f0a --- /dev/null +++ b/RPi-Pico/tls_client/CMakeLists.txt @@ -0,0 +1,39 @@ +add_executable(tls_Client + ../src/blink.c + ../src/wifi.c + ../src/tcp.c + ../src/tls.c + ../src/time.c + ../src/idleMemory.c + ../src/tlsClient_main.c +) + +target_compile_definitions(tls_Client PRIVATE + WIFI_SSID=\"${WIFI_SSID}\" + WIFI_PASSWORD=\"${WIFI_PASSWORD}\" + TEST_TCP_SERVER_IP=\"${TEST_TCP_SERVER_IP}\" + NO_SYS=0 + LWIP_SOCKET=1 + LWIP_COMPAT_SOCKETS=1 + PING_USE_SOCKETS=1 + DATETIME=\"${DATETIME}\" + WOLFSSL_USER_SETTINGS +) + +target_link_libraries(tls_Client + pico_stdlib + pico_rand + pico_cyw43_arch_lwip_sys_freertos + FreeRTOS-Kernel-Heap4 + wolfssl +) + +if (USE_UART) +pico_enable_stdio_usb(tls_Client 0) +pico_enable_stdio_uart(tls_Client 1) +else() +pico_enable_stdio_usb(tls_Client 1) +pico_enable_stdio_uart(tls_Client 0) +endif() + +pico_add_extra_outputs(tls_Client) diff --git a/RPi-Pico/tls_server/CMakeLists.txt b/RPi-Pico/tls_server/CMakeLists.txt new file mode 100644 index 000000000..364f9515f --- /dev/null +++ b/RPi-Pico/tls_server/CMakeLists.txt @@ -0,0 +1,38 @@ +add_executable(tls_Server + ../src/blink.c + ../src/wifi.c + ../src/tcp.c + ../src/tls.c + ../src/time.c + ../src/idleMemory.c + ../src/tlsServer_main.c +) + +target_compile_definitions(tls_Server PRIVATE + WIFI_SSID=\"${WIFI_SSID}\" + WIFI_PASSWORD=\"${WIFI_PASSWORD}\" + NO_SYS=0 + LWIP_SOCKET=1 + LWIP_COMPAT_SOCKETS=1 + PING_USE_SOCKETS=1 + DATETIME=\"${DATETIME}\" + WOLFSSL_USER_SETTINGS +) + +target_link_libraries(tls_Server + pico_stdlib + pico_rand + pico_cyw43_arch_lwip_sys_freertos + FreeRTOS-Kernel-Heap4 + wolfssl +) + +if (USE_UART) +pico_enable_stdio_usb(tls_Server 0) +pico_enable_stdio_uart(tls_Server 1) +else() +pico_enable_stdio_usb(tls_Server 1) +pico_enable_stdio_uart(tls_Server 0) +endif() + +pico_add_extra_outputs(tls_Server) diff --git a/RPi-Pico/wifi/CMakeLists.txt b/RPi-Pico/wifi/CMakeLists.txt new file mode 100644 index 000000000..eca0e72be --- /dev/null +++ b/RPi-Pico/wifi/CMakeLists.txt @@ -0,0 +1,31 @@ +add_executable(Wifi + ../src/blink.c + ../src/wifi.c + ../src/wifi_main.c +) + +target_compile_definitions(Wifi PRIVATE + WIFI_SSID=\"${WIFI_SSID}\" + WIFI_PASSWORD=\"${WIFI_PASSWORD}\" + PICO_CYW43_ARCH_POLL + NO_SYS=1 +) + +target_link_libraries(Wifi + pico_stdlib + pico_rand + pico_lwip + pico_cyw43_arch + pico_lwip_nosys + pico_async_context_poll +) + +if (USE_UART) + pico_enable_stdio_usb(Wifi 0) + pico_enable_stdio_uart(Wifi 1) +else() + pico_enable_stdio_usb(Wifi 1) + pico_enable_stdio_uart(Wifi 0) +endif() + +pico_add_extra_outputs(Wifi) diff --git a/RPi-Pico/wolfssl_import.cmake b/RPi-Pico/wolfssl_import.cmake new file mode 100644 index 000000000..20e6c6faa --- /dev/null +++ b/RPi-Pico/wolfssl_import.cmake @@ -0,0 +1,46 @@ + +set(WOLFSSL_ROOT $ENV{WOLFSSL_ROOT}) + +# ## wolfSSL/wolfCrypt library +file(GLOB WOLFSSL_SRC + "${WOLFSSL_ROOT}/src/*.c" + "${WOLFSSL_ROOT}/wolfcrypt/src/*.c" + "${WOLFSSL_ROOT}/wolfcrypt/src/port/rpi_pico/*" +) + +list(REMOVE_ITEM WOLFSSL_SRC EXCLUDE REGEX + "${WOLFSSL_ROOT}/src/bio.c" + "${WOLFSSL_ROOT}/src/conf.c" + "${WOLFSSL_ROOT}/src/pk.c" + "${WOLFSSL_ROOT}/src/ssl_asn1.c" + "${WOLFSSL_ROOT}/src/ssl_bn.c" + "${WOLFSSL_ROOT}/src/ssl_misc.c" + "${WOLFSSL_ROOT}/src/x509.c" + "${WOLFSSL_ROOT}/src/x509_str.c" + "${WOLFSSL_ROOT}/wolfcrypt/src/evp.c" + "${WOLFSSL_ROOT}/wolfcrypt/src/misc.c" +) + +add_library(wolfssl STATIC + ${WOLFSSL_SRC} +) + +include_directories(${WOLFSSL_ROOT}) + +target_compile_definitions(wolfssl PUBLIC + WOLFSSL_USER_SETTINGS +) + +if (${PICO_PLATFORM} STREQUAL "rp2350-arm-s") +add_compile_definitions(wolfssl WOLFSSL_SP_ARM_CORTEX_M_ASM) +elseif (${PICO_PLATFORM} STREQUAL "rp2350-riscv") +add_compile_definitions(wolfSSL WOLFSSL_SP_RISCV32) +else() +add_compile_definitions(wolfssl WOLFSSL_SP_ARM_THUMB_ASM) +endif() + +target_link_libraries(wolfssl +pico_stdlib +pico_rand +) +### End of wolfSSL/wolfCrypt library