diff --git a/components/mbedtls/CMakeLists.txt b/components/mbedtls/CMakeLists.txt index 41ef73f6418..00164929054 100644 --- a/components/mbedtls/CMakeLists.txt +++ b/components/mbedtls/CMakeLists.txt @@ -495,8 +495,11 @@ endif() target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${mbedtls_targets}) -# Ensure PSA crypto initialization is included in the build -if(NOT ${IDF_TARGET} STREQUAL "linux") +# Ensure PSA crypto initialization is included in the build. +# On macOS (Mach-O), C symbols have a leading underscore prefix. +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND ${IDF_TARGET} STREQUAL "linux") + target_link_libraries(${COMPONENT_LIB} ${linkage_type} "-u _mbedtls_psa_crypto_init_include_impl") +else() target_link_libraries(${COMPONENT_LIB} ${linkage_type} "-u mbedtls_psa_crypto_init_include_impl") endif() diff --git a/examples/protocols/https_server/simple/main/CMakeLists.txt b/examples/protocols/https_server/simple/main/CMakeLists.txt index 5f1c94e3a64..3fe83b30a22 100644 --- a/examples/protocols/https_server/simple/main/CMakeLists.txt +++ b/examples/protocols/https_server/simple/main/CMakeLists.txt @@ -1,6 +1,15 @@ +set(requires esp_https_server nvs_flash) +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + list(APPEND requires esp_stubs protocol_examples_common) +else() + list(APPEND requires esp_wifi esp_eth) +endif() + idf_component_register(SRCS "main.c" INCLUDE_DIRS "." - PRIV_REQUIRES esp_https_server esp_wifi nvs_flash esp_eth + PRIV_REQUIRES ${requires} EMBED_TXTFILES "certs/servercert.pem" "certs/prvtkey.pem" "certs/cacert.pem" diff --git a/examples/protocols/https_server/simple/main/idf_component.yml b/examples/protocols/https_server/simple/main/idf_component.yml index 718194867b7..c17c116c4bd 100644 --- a/examples/protocols/https_server/simple/main/idf_component.yml +++ b/examples/protocols/https_server/simple/main/idf_component.yml @@ -1,3 +1,7 @@ dependencies: protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common + esp_stubs: + path: ${IDF_PATH}/examples/protocols/linux_stubs/esp_stubs + rules: + - if: "target in [linux]" diff --git a/examples/protocols/https_server/simple/main/main.c b/examples/protocols/https_server/simple/main/main.c index 6fe6aa23fdf..30878614d17 100644 --- a/examples/protocols/https_server/simple/main/main.c +++ b/examples/protocols/https_server/simple/main/main.c @@ -7,18 +7,21 @@ CONDITIONS OF ANY KIND, either express or implied. */ -#include +#include #include #include -#include #include #include #include #include #include #include "esp_netif.h" -#include "esp_eth.h" #include "protocol_examples_common.h" +#if !CONFIG_IDF_TARGET_LINUX +#include +#include +#include "esp_eth.h" +#endif // !CONFIG_IDF_TARGET_LINUX #include #include "esp_tls.h" @@ -181,6 +184,11 @@ static httpd_handle_t start_webserver(void) httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT(); +#if CONFIG_IDF_TARGET_LINUX + /* Use non-privileged port on Linux since port 443 requires root */ + conf.port_secure = 8443; +#endif + extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start"); extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end"); @@ -237,6 +245,7 @@ static httpd_handle_t start_webserver(void) return server; } +#if !CONFIG_IDF_TARGET_LINUX static esp_err_t stop_webserver(httpd_handle_t server) { // Stop the httpd server @@ -264,6 +273,7 @@ static void connect_handler(void* arg, esp_event_base_t event_base, *server = start_webserver(); } } +#endif // !CONFIG_IDF_TARGET_LINUX void app_main(void) { @@ -277,6 +287,7 @@ void app_main(void) * and stop server when disconnection happens. */ +#if !CONFIG_IDF_TARGET_LINUX #ifdef CONFIG_EXAMPLE_CONNECT_WIFI ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); @@ -285,6 +296,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server)); #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET +#endif // !CONFIG_IDF_TARGET_LINUX #ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_SERVER_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); #endif // CONFIG_ESP_HTTPS_SERVER_EVENTS @@ -294,4 +306,12 @@ void app_main(void) * examples/protocols/README.md for more information about this function. */ ESP_ERROR_CHECK(example_connect()); + +#if CONFIG_IDF_TARGET_LINUX + /* On Linux, start the server directly since there are no WiFi/Ethernet events */ + server = start_webserver(); + while (server) { + sleep(5); + } +#endif // CONFIG_IDF_TARGET_LINUX } diff --git a/examples/protocols/https_server/wss_server/main/CMakeLists.txt b/examples/protocols/https_server/wss_server/main/CMakeLists.txt index eb67257ab63..e6340d879cc 100644 --- a/examples/protocols/https_server/wss_server/main/CMakeLists.txt +++ b/examples/protocols/https_server/wss_server/main/CMakeLists.txt @@ -1,5 +1,14 @@ +set(requires esp_https_server nvs_flash esp_timer esp_netif) +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + list(APPEND requires esp_stubs protocol_examples_common) +else() + list(APPEND requires esp_wifi esp_eth) +endif() + idf_component_register(SRCS "wss_server_example.c" "keep_alive.c" INCLUDE_DIRS "." - PRIV_REQUIRES esp_https_server nvs_flash esp_timer esp_netif esp_eth esp_wifi + PRIV_REQUIRES ${requires} EMBED_TXTFILES "certs/servercert.pem" "certs/prvtkey.pem") diff --git a/examples/protocols/https_server/wss_server/main/idf_component.yml b/examples/protocols/https_server/wss_server/main/idf_component.yml index 718194867b7..c17c116c4bd 100644 --- a/examples/protocols/https_server/wss_server/main/idf_component.yml +++ b/examples/protocols/https_server/wss_server/main/idf_component.yml @@ -1,3 +1,7 @@ dependencies: protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common + esp_stubs: + path: ${IDF_PATH}/examples/protocols/linux_stubs/esp_stubs + rules: + - if: "target in [linux]" diff --git a/examples/protocols/https_server/wss_server/main/keep_alive.c b/examples/protocols/https_server/wss_server/main/keep_alive.c index 92ae0f6126a..a3e2f7c6826 100644 --- a/examples/protocols/https_server/wss_server/main/keep_alive.c +++ b/examples/protocols/https_server/wss_server/main/keep_alive.c @@ -8,12 +8,16 @@ */ #include -#include #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/task.h" #include "keep_alive.h" +#if !CONFIG_IDF_TARGET_LINUX +#include #include "esp_timer.h" +#else +#include +#endif // !CONFIG_IDF_TARGET_LINUX typedef enum { NO_CLIENT = 0, @@ -47,7 +51,13 @@ static const char *TAG = "wss_keep_alive"; static uint64_t _tick_get_ms(void) { - return esp_timer_get_time()/1000; +#if CONFIG_IDF_TARGET_LINUX + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +#else + return esp_timer_get_time() / 1000; +#endif } // Goes over active clients to find out how long we could sleep before checking who's alive diff --git a/examples/protocols/https_server/wss_server/main/wss_server_example.c b/examples/protocols/https_server/wss_server/main/wss_server_example.c index cc27539f8a5..7588c839749 100644 --- a/examples/protocols/https_server/wss_server/main/wss_server_example.c +++ b/examples/protocols/https_server/wss_server/main/wss_server_example.c @@ -9,14 +9,18 @@ #include #include -#include #include #include #include "esp_netif.h" +#include "protocol_examples_common.h" +#if !CONFIG_IDF_TARGET_LINUX +#include #include "esp_eth.h" #include "esp_wifi.h" -#include "protocol_examples_common.h" #include "lwip/sockets.h" +#else +#include +#endif // !CONFIG_IDF_TARGET_LINUX #include #include "keep_alive.h" #include "sdkconfig.h" @@ -191,6 +195,10 @@ static httpd_handle_t start_wss_echo_server(void) ESP_LOGI(TAG, "Starting server"); httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT(); +#if CONFIG_IDF_TARGET_LINUX + /* Use non-privileged port on Linux since port 443 requires root */ + conf.port_secure = 8443; +#endif conf.httpd.max_open_sockets = max_clients; conf.httpd.global_user_ctx = keep_alive; conf.httpd.open_fn = wss_open_fd; @@ -220,6 +228,7 @@ static httpd_handle_t start_wss_echo_server(void) return server; } +#if !CONFIG_IDF_TARGET_LINUX static esp_err_t stop_wss_echo_server(httpd_handle_t server) { // Stop keep alive thread @@ -249,6 +258,7 @@ static void connect_handler(void* arg, esp_event_base_t event_base, *server = start_wss_echo_server(); } } +#endif // !CONFIG_IDF_TARGET_LINUX // Get all clients and send async message static void wss_server_send_messages(httpd_handle_t* server) @@ -299,6 +309,7 @@ void app_main(void) * and stop server when disconnection happens. */ +#if !CONFIG_IDF_TARGET_LINUX #ifdef CONFIG_EXAMPLE_CONNECT_WIFI ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); @@ -307,6 +318,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server)); #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET +#endif // !CONFIG_IDF_TARGET_LINUX /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. * Read "Establishing Wi-Fi or Ethernet Connection" section in @@ -314,6 +326,11 @@ void app_main(void) */ ESP_ERROR_CHECK(example_connect()); +#if CONFIG_IDF_TARGET_LINUX + /* On Linux, start the server directly since there are no WiFi/Ethernet events */ + server = start_wss_echo_server(); +#endif // CONFIG_IDF_TARGET_LINUX + /* This function demonstrates periodic sending Websocket messages * to all connected clients to this server */