diff --git a/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h b/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h index 83b4fff0a2..9dc370539c 100644 --- a/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h +++ b/common_components/linux_compat/freertos/include/freertos/FreeRTOS.h @@ -21,7 +21,7 @@ typedef void *EventGroupHandle_t; typedef uint32_t TickType_t; typedef TickType_t EventBits_t; -typedef void (*TaskFunction_t)( void * ); +typedef void (*TaskFunction_t)(void *); typedef unsigned int UBaseType_t; typedef int BaseType_t; diff --git a/components/eppp_link/examples/host/main/register_iperf.c b/components/eppp_link/examples/host/main/register_iperf.c index 63fded10c5..b8a6348c12 100644 --- a/components/eppp_link/examples/host/main/register_iperf.c +++ b/components/eppp_link/examples/host/main/register_iperf.c @@ -139,9 +139,9 @@ static int ppp_cmd_iperf(int argc, char **argv) cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", (uint16_t) cfg.source_ip4 & 0xFF, - (uint16_t) (cfg.source_ip4 >> 8) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 16) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 24) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 8) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 16) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 24) & 0xFF, cfg.sport, cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport, diff --git a/components/esp_modem/examples/modem_console/main/console_helper.hpp b/components/esp_modem/examples/modem_console/main/console_helper.hpp index c6012a7e02..ceae2a7755 100644 --- a/components/esp_modem/examples/modem_console/main/console_helper.hpp +++ b/components/esp_modem/examples/modem_console/main/console_helper.hpp @@ -80,7 +80,7 @@ class ConsoleCommand { * @param f Function callback for the command */ template explicit ConsoleCommand(const char *command, const char *help, const T *arg_struct, size_t srg_struct_size, - std::function f): func(std::move(f)) + std::function f): func(std::move(f)) { size_t args_plain_size = srg_struct_size / sizeof(CommandArgs); auto first_arg = reinterpret_cast(arg_struct); diff --git a/components/esp_modem/examples/modem_console/main/ping_handle.c b/components/esp_modem/examples/modem_console/main/ping_handle.c index 7163ebfb21..2839d899f7 100644 --- a/components/esp_modem/examples/modem_console/main/ping_handle.c +++ b/components/esp_modem/examples/modem_console/main/ping_handle.c @@ -101,10 +101,10 @@ static int do_ping_cmd(int argc, char **argv) return 1; } if (res->ai_family == AF_INET) { - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); } else { - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; + struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); } freeaddrinfo(res); diff --git a/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp b/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp index 5e1343ceb0..30b82f2954 100644 --- a/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp +++ b/components/esp_modem/examples/modem_tcp_client/main/sock_commands_sim7600.cpp @@ -18,13 +18,13 @@ using namespace esp_modem; command_result net_open(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string response; auto ret = dce_commands::generic_get_string(term, "AT+NETOPEN?\r", response, 1000); if (ret != command_result::OK) { return ret; } - ESP_LOGV(TAG, "%s", response.data() ); + ESP_LOGV(TAG, "%s", response.data()); if (response.find("+NETOPEN: 1") != std::string::npos) { ESP_LOGD(TAG, "Already there"); ret = command_result::OK; @@ -42,23 +42,23 @@ command_result net_open(CommandableIf *term) command_result net_close(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); return dce_commands::generic_command(term, "AT+NETCLOSE\r", "+NETCLOSE:", "ERROR", 30000); } command_result tcp_open(CommandableIf *term, const std::string &host, int port, int timeout) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); auto ret = dce_commands::generic_command(term, "AT+CIPRXGET=1\r", "OK", "ERROR", 50000); if (ret != command_result::OK) { ESP_LOGE(TAG, "Setting Rx mode failed!"); return ret; } - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string ip_open = R"(AT+CIPOPEN=0,"TCP",")" + host + "\"," + std::to_string(port) + "\r"; ret = dce_commands::generic_command(term, ip_open, "+CIPOPEN: 0,0", "ERROR", timeout); if (ret != command_result::OK) { - ESP_LOGE(TAG, "%s Failed", __func__ ); + ESP_LOGE(TAG, "%s Failed", __func__); return ret; } return command_result::OK; @@ -66,13 +66,13 @@ command_result tcp_open(CommandableIf *term, const std::string &host, int port, command_result tcp_close(CommandableIf *term) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); return dce_commands::generic_command(term, "AT+CIPCLOSE=0\r", "+CIPCLOSE:", "ERROR", 10000); } command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string send = "AT+CIPSEND=0," + std::to_string(len) + "\r"; auto ret = term->command(send, [&](uint8_t *data, size_t len) { std::string_view response((char *)data, len); @@ -107,7 +107,7 @@ command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) uint8_t ctrl_z = '\x1A'; term->write(&ctrl_z, 1); int count = 0; - while (ret == command_result::TIMEOUT && count++ < 1000 ) { + while (ret == command_result::TIMEOUT && count++ < 1000) { vTaskDelay(pdMS_TO_TICKS(1000)); } term->on_read(nullptr); @@ -116,7 +116,7 @@ command_result tcp_send(CommandableIf *term, uint8_t *data, size_t len) command_result tcp_recv(CommandableIf *term, uint8_t *data, size_t len, size_t &out_len) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string out; auto ret = dce_commands::generic_get_string(term, "AT+CIPRXGET=4,0\r", out); if (ret != command_result::OK) { diff --git a/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp b/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp index 981b3ce44b..c5082390e4 100644 --- a/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp +++ b/components/esp_modem/examples/simple_cmux_client/components/SIM7070_gnss/SIM7070_gnss.cpp @@ -43,8 +43,8 @@ class LocalFactory: public Factory { * @return unique pointer of the resultant DCE */ std::unique_ptr create_SIM7070_GNSS_dce(const esp_modem::dce_config *config, - std::shared_ptr dte, - esp_netif_t *netif) + std::shared_ptr dte, + esp_netif_t *netif) { return gnss_factory::LocalFactory::create(config, std::move(dte), netif); } @@ -52,7 +52,7 @@ std::unique_ptr create_SIM7070_GNSS_dce(const esp_modem::dce_config *c esp_modem::command_result get_gnss_information_sim70xx_lib(esp_modem::CommandableIf *t, sim70xx_gps_t &gps) { - ESP_LOGV(TAG, "%s", __func__ ); + ESP_LOGV(TAG, "%s", __func__); std::string str_out; auto ret = esp_modem::dce_commands::generic_get_string(t, "AT+CGNSINF\r", str_out); if (ret != esp_modem::command_result::OK) { diff --git a/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp b/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp index a26ee8e017..41111941d2 100644 --- a/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp +++ b/components/esp_modem/include/cxx_include/esp_modem_buffer.hpp @@ -14,7 +14,7 @@ namespace esp_modem { */ struct unique_buffer { explicit unique_buffer(size_t size); - unique_buffer (unique_buffer const &) = delete; + unique_buffer(unique_buffer const &) = delete; unique_buffer &operator=(unique_buffer const &) = delete; unique_buffer(unique_buffer &&other) noexcept { diff --git a/components/esp_modem/src/esp_modem_uart_linux.cpp b/components/esp_modem/src/esp_modem_uart_linux.cpp index e6aa222a4f..e4e67fcc90 100644 --- a/components/esp_modem/src/esp_modem_uart_linux.cpp +++ b/components/esp_modem/src/esp_modem_uart_linux.cpp @@ -17,7 +17,7 @@ constexpr const char *TAG = "uart_resource"; uart_resource::uart_resource(const esp_modem_uart_term_config *config, QueueHandle_t *event_queue, int fd): port(-1) { - ESP_LOGD(TAG, "Creating uart resource" ); + ESP_LOGD(TAG, "Creating uart resource"); struct termios tty = {}; ESP_MODEM_THROW_IF_FALSE(tcgetattr(fd, &tty) == 0, "Failed to tcgetattr()"); diff --git a/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp b/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp index 7571d2dfe2..7a80d01e94 100644 --- a/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp +++ b/components/esp_modem/src/esp_modem_vfs_socket_creator.cpp @@ -52,7 +52,7 @@ static esp_err_t hostname_to_fd(const char *host, int port, int *fd) auto *p = reinterpret_cast(address_info->ai_addr); p->sin_port = htons(port); ESP_LOGI(TAG, "[sock=%d] Resolved IPv4 address: %s", *fd, inet_ntoa(p->sin_addr)); - memcpy(&address, p, sizeof(struct sockaddr )); + memcpy(&address, p, sizeof(struct sockaddr)); } else { ESP_LOGE(TAG, "Unsupported protocol family %d", address_info->ai_family); close(*fd); diff --git a/components/esp_modem/test/host_test/main/LoopbackTerm.cpp b/components/esp_modem/test/host_test/main/LoopbackTerm.cpp index 935c634fa8..0dea11e109 100644 --- a/components/esp_modem/test/host_test/main/LoopbackTerm.cpp +++ b/components/esp_modem/test/host_test/main/LoopbackTerm.cpp @@ -26,7 +26,7 @@ int LoopbackTerm::write(uint8_t *data, size_t len) async_results.push_back(std::move(ret)); return len; } - if (len > 2 && (data[len - 1] == '\r' || data[len - 1] == '+') ) { // Simple AT responder + if (len > 2 && (data[len - 1] == '\r' || data[len - 1] == '+')) { // Simple AT responder std::string command((char *)data, len); std::string response; if (command == "+++") { diff --git a/components/esp_modem/test/target/main/pppd_test.cpp b/components/esp_modem/test/target/main/pppd_test.cpp index cd9bfb8bd9..302eba544f 100644 --- a/components/esp_modem/test/target/main/pppd_test.cpp +++ b/components/esp_modem/test/target/main/pppd_test.cpp @@ -131,7 +131,7 @@ extern "C" { ESP_LOGE(TAG, "Signal handler %d", nr); } - _sig_func_ptr signal (int nr, _sig_func_ptr) + _sig_func_ptr signal(int nr, _sig_func_ptr) { return handle; } diff --git a/components/esp_modem/test/target_iperf/main/cmd_pppclient.c b/components/esp_modem/test/target_iperf/main/cmd_pppclient.c index 0bb6a89699..13cbce44ce 100644 --- a/components/esp_modem/test/target_iperf/main/cmd_pppclient.c +++ b/components/esp_modem/test/target_iperf/main/cmd_pppclient.c @@ -223,9 +223,9 @@ static int ppp_cmd_iperf(int argc, char **argv) cfg.flag & IPERF_FLAG_TCP ? "tcp" : "udp", cfg.flag & IPERF_FLAG_SERVER ? "server" : "client", (uint16_t) cfg.source_ip4 & 0xFF, - (uint16_t) (cfg.source_ip4 >> 8) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 16) & 0xFF, - (uint16_t) (cfg.source_ip4 >> 24) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 8) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 16) & 0xFF, + (uint16_t)(cfg.source_ip4 >> 24) & 0xFF, cfg.sport, cfg.destination_ip4 & 0xFF, (cfg.destination_ip4 >> 8) & 0xFF, (cfg.destination_ip4 >> 16) & 0xFF, (cfg.destination_ip4 >> 24) & 0xFF, cfg.dport, diff --git a/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp b/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp index 160172b27f..c7a98be6f5 100644 --- a/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp +++ b/components/esp_modem/test/target_ota/components/manual_ota/transport_batch_tls.cpp @@ -226,7 +226,7 @@ int TlsTransport::preread(size_t len, int timeout_ms) while (len != read_len) { int l = esp_transport_read(transport_, buf.data() + read_len, len - read_len, timeout_ms); ESP_LOGD(TAG, "need %d read %d already %d", len, l, read_len); - if ((l == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN || l == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT ) && read_len > 0) { + if ((l == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN || l == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) && read_len > 0) { return read_len; } if (l <= 0) { diff --git a/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp b/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp index 3bdf35640d..c60f4e8c58 100644 --- a/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp +++ b/components/esp_mqtt_cxx/examples/ssl/main/mqtt_ssl_example.cpp @@ -85,6 +85,6 @@ extern "C" void app_main(void) MyClient client{broker, credentials, config}; while (true) { constexpr TickType_t xDelay = 500 / portTICK_PERIOD_MS; - vTaskDelay( xDelay ); + vTaskDelay(xDelay); } } diff --git a/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp b/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp index c71e6b0b4a..1e21a9b4cd 100644 --- a/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp +++ b/components/mbedtls_cxx/examples/udp_mutual_auth/main/udp_mutual.cpp @@ -39,11 +39,11 @@ class SecureLink: public Tls { int recv_timeout(unsigned char *buf, size_t len, int timeout) override { struct timeval tv { - timeout / 1000, (timeout % 1000 ) * 1000 + timeout / 1000, (timeout % 1000) * 1000 }; fd_set read_fds; - FD_ZERO( &read_fds ); - FD_SET( sock, &read_fds ); + FD_ZERO(&read_fds); + FD_SET(sock, &read_fds); int ret = select(sock + 1, &read_fds, nullptr, nullptr, timeout == 0 ? nullptr : &tv); if (ret == 0) { diff --git a/components/mbedtls_cxx/mbedtls_wrap.cpp b/components/mbedtls_cxx/mbedtls_wrap.cpp index c486191592..ce5f4b27a7 100644 --- a/components/mbedtls_cxx/mbedtls_wrap.cpp +++ b/components/mbedtls_cxx/mbedtls_wrap.cpp @@ -93,8 +93,8 @@ int Tls::handshake() int ret = 0; mbedtls_ssl_set_bio(&ssl_, this, bio_write, bio_read, is_dtls_ ? bio_read_tout : nullptr); - while ( ( ret = mbedtls_ssl_handshake( &ssl_ ) ) != 0 ) { - if ( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { + while ((ret = mbedtls_ssl_handshake(&ssl_)) != 0) { + if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { #if CONFIG_MBEDTLS_SSL_PROTO_DTLS if (is_server_ && is_dtls_ && ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { // hello verification requested -> restart the session with this client_id @@ -104,7 +104,7 @@ int Tls::handshake() continue; } #endif // MBEDTLS_SSL_PROTO_DTLS - print_error( "mbedtls_ssl_handshake returned", ret ); + print_error("mbedtls_ssl_handshake returned", ret); return -1; } delay(); @@ -132,12 +132,12 @@ int Tls::bio_read_tout(void *ctx, unsigned char *buf, size_t len, uint32_t timeo int Tls::write(const unsigned char *buf, size_t len) { - return mbedtls_ssl_write( &ssl_, buf, len ); + return mbedtls_ssl_write(&ssl_, buf, len); } int Tls::read(unsigned char *buf, size_t len) { - return mbedtls_ssl_read( &ssl_, buf, len ); + return mbedtls_ssl_read(&ssl_, buf, len); } bool Tls::set_own_cert(const_buf crt, const_buf key) diff --git a/components/mdns/tests/test_afl_fuzz_host/mdns_di.h b/components/mdns/tests/test_afl_fuzz_host/mdns_di.h index ed56960f47..2e9c848b05 100644 --- a/components/mdns/tests/test_afl_fuzz_host/mdns_di.h +++ b/components/mdns/tests/test_afl_fuzz_host/mdns_di.h @@ -11,18 +11,18 @@ #include "mdns.h" #include "mdns_private.h" -void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL; +void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL; mdns_srv_item_t *(*mdns_test_static_mdns_get_service_item)(const char *service, const char *proto, const char *hostname) = NULL; mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, - uint32_t timeout, uint8_t max_results, - mdns_query_notify_t notifier) = NULL; -esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t *search) = NULL; -void (*mdns_test_static_search_free)(mdns_search_once_t *search) = NULL; + uint32_t timeout, uint8_t max_results, + mdns_query_notify_t notifier) = NULL; +esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t *search) = NULL; +void (*mdns_test_static_search_free)(mdns_search_once_t *search) = NULL; static void _mdns_execute_action(mdns_action_t *action); static mdns_srv_item_t *_mdns_get_service_item(const char *service, const char *proto, const char *hostname); static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast, - uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier); + uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier); static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t *search); static void _mdns_search_free(mdns_search_once_t *search); diff --git a/components/mosquitto/port/signals.c b/components/mosquitto/port/signals.c index f1559b51aa..c7999aad13 100644 --- a/components/mosquitto/port/signals.c +++ b/components/mosquitto/port/signals.c @@ -7,7 +7,7 @@ */ #include "signal.h" -int sigprocmask (int, const sigset_t *, sigset_t *) +int sigprocmask(int, const sigset_t *, sigset_t *) { return 0; } diff --git a/components/net_connect/CMakeLists.txt b/components/net_connect/CMakeLists.txt new file mode 100644 index 0000000000..9258de8b52 --- /dev/null +++ b/components/net_connect/CMakeLists.txt @@ -0,0 +1,52 @@ +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + # Header only library for linux + + idf_component_register(INCLUDE_DIRS include + SRCS protocol_examples_utils.c) + return() +endif() + +set(srcs "stdin_out.c" + "addr_from_stdin.c" + "connect.c" + "wifi_connect.c" + "protocol_examples_utils.c") + +if(CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD) + list(APPEND srcs "console_cmd.c") +endif() + +if(CONFIG_NET_CONNECT_CONNECT_ETHERNET) + list(APPEND srcs "eth_connect.c") +endif() + +if(CONFIG_NET_CONNECT_CONNECT_THREAD) + list(APPEND srcs "thread_connect.c") +endif() + +if(CONFIG_NET_CONNECT_CONNECT_PPP) + list(APPEND srcs "ppp_connect.c") +endif() + + +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS "include" + PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console esp_eth openthread) + +if(CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD) + idf_component_optional_requires(PRIVATE console) +endif() + +if(CONFIG_NET_CONNECT_CONNECT_ETHERNET) + idf_component_optional_requires(PUBLIC esp_eth) +endif() + +if(CONFIG_NET_CONNECT_CONNECT_THREAD) + idf_component_optional_requires(PRIVATE openthread) +endif() + +if(CONFIG_NET_CONNECT_CONNECT_PPP) + idf_component_optional_requires(PRIVATE esp_tinyusb espressif__esp_tinyusb) +endif() diff --git a/components/net_connect/Kconfig.projbuild b/components/net_connect/Kconfig.projbuild new file mode 100644 index 0000000000..a571465c0d --- /dev/null +++ b/components/net_connect/Kconfig.projbuild @@ -0,0 +1,403 @@ +menu "Net Connect Configuration" + + orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps" + + config NET_CONNECT_CONNECT_WIFI + bool "connect using WiFi interface" + depends on !IDF_TARGET_LINUX && (SOC_WIFI_SUPPORTED || ESP_WIFI_REMOTE_ENABLED || ESP_HOST_WIFI_ENABLED) + default y if SOC_WIFI_SUPPORTED + help + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. + Choose this option to connect with WiFi + + if NET_CONNECT_CONNECT_WIFI + config NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + bool "Get ssid and password from stdin" + default n + help + Give the WiFi SSID and password from stdin. + + config NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + bool "Provide wifi connect commands" + default y + help + Provide wifi connect commands for esp_console. + Please use `example_register_wifi_connect_commands` to register them. + + config NET_CONNECT_WIFI_SSID + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config NET_CONNECT_WIFI_PASSWORD + depends on !NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + Can be left blank if the network has no security set. + + config NET_CONNECT_WIFI_CONN_MAX_RETRY + int "Maximum retry" + default 6 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited, + in case the AP is really inexistent. + + choice NET_CONNECT_WIFI_SCAN_METHOD + prompt "WiFi Scan Method" + default NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL + help + WiFi scan method: + + If "Fast" is selected, scan will end after find SSID match AP. + + If "All Channel" is selected, scan will end after scan all the channel. + + config NET_CONNECT_WIFI_SCAN_METHOD_FAST + bool "Fast" + config NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL + bool "All Channel" + endchoice + + menu "WiFi Scan threshold" + config NET_CONNECT_WIFI_SCAN_RSSI_THRESHOLD + int "WiFi minimum rssi" + range -127 0 + + default -127 + help + The minimum rssi to accept in the scan mode. + + choice NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD + prompt "WiFi Scan auth mode threshold" + default NET_CONNECT_WIFI_AUTH_OPEN + help + The weakest authmode to accept in the scan mode. + + config NET_CONNECT_WIFI_AUTH_OPEN + bool "OPEN" + config NET_CONNECT_WIFI_AUTH_WEP + bool "WEP" + config NET_CONNECT_WIFI_AUTH_WPA_PSK + bool "WPA PSK" + config NET_CONNECT_WIFI_AUTH_WPA2_PSK + bool "WPA2 PSK" + config NET_CONNECT_WIFI_AUTH_WPA_WPA2_PSK + bool "WPA WPA2 PSK" + config NET_CONNECT_WIFI_AUTH_WPA2_ENTERPRISE + bool "WPA2 ENTERPRISE" + config NET_CONNECT_WIFI_AUTH_WPA3_PSK + bool "WPA3 PSK" + config NET_CONNECT_WIFI_AUTH_WPA2_WPA3_PSK + bool "WPA2 WPA3 PSK" + config NET_CONNECT_WIFI_AUTH_WAPI_PSK + bool "WAPI PSK" + endchoice + endmenu + + choice NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD + prompt "WiFi Connect AP Sort Method" + default NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL + help + WiFi connect AP sort method: + + If "Signal" is selected, Sort matched APs in scan list by RSSI. + + If "Security" is selected, Sort matched APs in scan list by security mode. + + config NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL + bool "Signal" + config NET_CONNECT_WIFI_CONNECT_AP_BY_SECURITY + bool "Security" + endchoice + endif + + config NET_CONNECT_CONNECT_ETHERNET + bool "connect using Ethernet interface" + depends on !IDF_TARGET_LINUX + default y if !NET_CONNECT_CONNECT_WIFI && !NET_CONNECT_CONNECT_THREAD + help + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. + Choose this option to connect with Ethernet + + if NET_CONNECT_CONNECT_ETHERNET + config NET_CONNECT_ETHERNET_EMAC_TASK_STACK_SIZE + int "emac_rx task stack size" + default 2048 + help + This set stack size for emac_rx task + + config NET_CONNECT_USE_SPI_ETHERNET + bool + + choice NET_CONNECT_ETHERNET_TYPE + prompt "Ethernet Type" + default NET_CONNECT_USE_INTERNAL_ETHERNET if SOC_EMAC_SUPPORTED + default NET_CONNECT_USE_DUMMY + help + Select which kind of Ethernet will be used in the example. + + config NET_CONNECT_USE_INTERNAL_ETHERNET + depends on SOC_EMAC_SUPPORTED + select ETH_USE_ESP32_EMAC + bool "Internal EMAC" + help + Select internal Ethernet MAC controller. + + config NET_CONNECT_USE_DUMMY + bool "DUMMY Module" + select ETH_USE_SPI_ETHERNET + help + Dummy option to just to pass builds, will be fixed by IDF-14059 + + config NET_CONNECT_USE_OPENETH + bool "OpenCores Ethernet MAC (EXPERIMENTAL)" + select ETH_USE_OPENETH + help + When this option is enabled, the example is built with support for + OpenCores Ethernet MAC, which allows testing the example in QEMU. + Note that this option is used for internal testing purposes, and + not officially supported. Examples built with this option enabled + will not run on a real ESP32 chip. + + endchoice # NET_CONNECT_ETHERNET_TYPE + + if NET_CONNECT_USE_INTERNAL_ETHERNET + choice NET_CONNECT_ETH_PHY_MODEL + prompt "Ethernet PHY Device" + default NET_CONNECT_ETH_PHY_GENERIC + help + Select the Ethernet PHY device to use in the example. + + config NET_CONNECT_ETH_PHY_GENERIC + bool "Generic 802.3 PHY" + help + Any Ethernet PHY chip compliant with IEEE 802.3 can be used. However, while + basic functionality should always work, some specific features might be limited, + even if the PHY meets IEEE 802.3 standard. A typical example is loopback + functionality, where certain PHYs may require setting a specific speed mode to + operate correctly. + endchoice + + config NET_CONNECT_ETH_MDC_GPIO + int "SMI MDC GPIO number" + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX + default 23 if IDF_TARGET_ESP32 + default 31 if IDF_TARGET_ESP32P4 + help + Set the GPIO number used by SMI MDC. + + config NET_CONNECT_ETH_MDIO_GPIO + int "SMI MDIO GPIO number" + range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX + default 18 if IDF_TARGET_ESP32 + default 52 if IDF_TARGET_ESP32P4 + help + Set the GPIO number used by SMI MDIO. + endif + + config NET_CONNECT_ETH_PHY_RST_GPIO + int "PHY Reset GPIO number" + range -1 ENV_GPIO_OUT_RANGE_MAX + default 51 if IDF_TARGET_ESP32P4 + default 5 + help + Set the GPIO number used to reset PHY chip. + Set to -1 to disable PHY chip hardware reset. + + config NET_CONNECT_ETH_PHY_ADDR + int "PHY Address" + range 0 31 if NET_CONNECT_USE_INTERNAL_ETHERNET + default 1 + help + Set PHY address according your board schematic. + endif # NET_CONNECT_CONNECT_ETHERNET + + config NET_CONNECT_CONNECT_PPP + bool "connect using Point to Point interface" + select LWIP_PPP_SUPPORT + help + Protocol examples can use PPP connection over serial line. + Choose this option to connect to the ppp server running + on your laptop over a serial line (either UART or USB ACM) + + if NET_CONNECT_CONNECT_PPP + choice NET_CONNECT_CONNECT_PPP_DEVICE + prompt "Choose PPP device" + default NET_CONNECT_CONNECT_PPP_DEVICE_USB + help + Select which peripheral to use to connect to the PPP server. + + config NET_CONNECT_CONNECT_PPP_DEVICE_USB + bool "USB" + depends on SOC_USB_OTG_SUPPORTED + select TINYUSB_CDC_ENABLED + help + Use USB ACM device. + + config NET_CONNECT_CONNECT_PPP_DEVICE_UART + bool "UART" + help + Use UART. + + endchoice + + menu "UART Configuration" + depends on NET_CONNECT_CONNECT_PPP_DEVICE_UART + config NET_CONNECT_CONNECT_UART_TX_PIN + int "TXD Pin Number" + default 4 + range 0 31 + help + Pin number of UART TX. + + config NET_CONNECT_CONNECT_UART_RX_PIN + int "RXD Pin Number" + default 5 + range 0 31 + help + Pin number of UART RX. + + config NET_CONNECT_CONNECT_UART_BAUDRATE + int "UART Baudrate" + default 115200 + range 9600 3000000 + help + Baudrate of the UART device + + endmenu + + config NET_CONNECT_PPP_CONN_MAX_RETRY + int "Maximum retry" + default 6 + help + Set the Maximum retry to avoid station reconnecting if the pppd + is not available + + endif # NET_CONNECT_CONNECT_PPP + + config NET_CONNECT_CONNECT_THREAD + bool "Connect using Thread interface" + depends on !IDF_TARGET_LINUX && OPENTHREAD_ENABLED + default y if SOC_IEEE802154_SUPPORTED + select NET_CONNECT_CONNECT_IPV6 + help + Protocol examples can use Wi-Fi, Ethernet and/or Thread to connect to the network. + Choose this option to connect with Thread. + The operational active dataset of the Thread network can be configured in openthread + component at '->Components->OpenThread->Thread Core Features->Thread Operational Dataset' + + if NET_CONNECT_CONNECT_THREAD + config NET_CONNECT_THREAD_TASK_STACK_SIZE + int "Example Thread task stack size" + default 8192 + help + Thread task stack size + + menu "Radio Spinel Options" + depends on OPENTHREAD_RADIO_SPINEL_UART || OPENTHREAD_RADIO_SPINEL_SPI + + config NET_CONNECT_THREAD_UART_RX_PIN + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart Rx Pin" + default 17 + + config NET_CONNECT_THREAD_UART_TX_PIN + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart Tx pin" + default 18 + + config NET_CONNECT_THREAD_UART_BAUD + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart baud rate" + default 460800 + + config NET_CONNECT_THREAD_UART_PORT + depends on OPENTHREAD_RADIO_SPINEL_UART + int "Uart port" + default 1 + + config NET_CONNECT_THREAD_SPI_CS_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI CS Pin" + default 10 + + config NET_CONNECT_THREAD_SPI_SCLK_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI SCLK Pin" + default 12 + + config NET_CONNECT_THREAD_SPI_MISO_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI MISO Pin" + default 13 + + config NET_CONNECT_THREAD_SPI_MOSI_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI MOSI Pin" + default 11 + + config NET_CONNECT_THREAD_SPI_INTR_PIN + depends on OPENTHREAD_RADIO_SPINEL_SPI + int "SPI Interrupt Pin" + default 8 + endmenu + + endif + + config NET_CONNECT_CONNECT_IPV4 + bool + depends on LWIP_IPV4 + default n if NET_CONNECT_CONNECT_THREAD + default y + + config NET_CONNECT_CONNECT_IPV6 + depends on NET_CONNECT_CONNECT_WIFI || NET_CONNECT_CONNECT_ETHERNET || NET_CONNECT_CONNECT_PPP || NET_CONNECT_CONNECT_THREAD + bool "Obtain IPv6 address" + default y + select LWIP_IPV6 + select LWIP_PPP_ENABLE_IPV6 if NET_CONNECT_CONNECT_PPP + help + By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. + Disable this option if the network does not support IPv6. + Choose the preferred IPv6 address type if the connection code should wait until other than + the local link address gets assigned. + Consider enabling IPv6 stateless address autoconfiguration (SLAAC) in the LWIP component. + + if NET_CONNECT_CONNECT_IPV6 + choice NET_CONNECT_CONNECT_PREFERRED_IPV6 + prompt "Preferred IPv6 Type" + default NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK + help + Select which kind of IPv6 address the connect logic waits for. + + config NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK + bool "Local Link Address" + help + Blocks until Local link address assigned. + + config NET_CONNECT_CONNECT_IPV6_PREF_GLOBAL + bool "Global Address" + help + Blocks until Global address assigned. + + config NET_CONNECT_CONNECT_IPV6_PREF_SITE_LOCAL + bool "Site Local Address" + help + Blocks until Site link address assigned. + + config NET_CONNECT_CONNECT_IPV6_PREF_UNIQUE_LOCAL + bool "Unique Local Link Address" + help + Blocks until Unique local address assigned. + + endchoice + + endif + + +endmenu diff --git a/components/net_connect/README.md b/components/net_connect/README.md new file mode 100644 index 0000000000..91d1305bbb --- /dev/null +++ b/components/net_connect/README.md @@ -0,0 +1,66 @@ +# net_connect + +This component implements the most common connection methods for ESP32 boards. It provides WiFi, Ethernet, Thread, and PPP connection functionality for network-enabled applications. + +## How to use this component + +Choose the preferred interface (WiFi, Ethernet, Thread, PPPoS) to connect to the network and configure the interface. + +It is possible to enable multiple interfaces simultaneously making the connection phase to block until all the chosen interfaces acquire IP addresses. +It is also possible to disable all interfaces, skipping the connection phase altogether. + +### WiFi + +Choose WiFi connection method (for chipsets that support it) and configure basic WiFi connection properties: +* WiFi SSID +* WiFI password +* Maximum connection retry (connection would be aborted if it doesn't succeed after specified number of retries) +* WiFi scan method (including RSSI and authorization mode threshold) + + + +### Ethernet + +Choose Ethernet connection if your board supports it. The most common settings is using Espressif Ethernet Kit, which is also the recommended HW for this selection. You can also select an SPI ethernet device (if your chipset doesn't support internal EMAC or if you prefer). It is also possible to use OpenCores Ethernet MAC if you're running the example under QEMU. + +### Thread + +Choose Thread connection if your board supports IEEE802.15.4 native radio or works with [OpenThread RCP](../../openthread/ot_rcp/README.md). You can configure the Thread network at menuconfig '->Components->OpenThread->Thread Core Features->Thread Operational Dataset'. + +If the Thread end-device joins a Thread network with a Thread Border Router that has the NAT64 feature enabled, the end-device can access the Internet with the standard DNS APIs after configuring the following properties: +* Enable DNS64 client ('->Components->OpenThread->Thread Core Features->Enable DNS64 client') +* Enable custom DNS external resolve Hook ('->Components->LWIP->Hooks->DNS external resolve Hook->Custom implementation') + +### PPP + +Point to point connection method creates a simple IP tunnel to the counterpart device (running PPP server), typically a Linux machine with pppd service. We currently support only PPP over Serial (using UART or USB CDC). This is useful for simple testing of networking layers, but with some additional configuration on the server side, we could simulate standard model of internet connectivity. The PPP server could be also represented by a cellular modem device with pre-configured connectivity and already switched to PPP mode (this setup is not very flexible though, so we suggest using a standard modem library implementing commands and modes, e.g. [esp_modem](https://components.espressif.com/component/espressif/esp_modem) ). + +> [!Note] +> Note that if you choose USB device, you have to manually add a dependency on `esp_tinyusb` component. This step is necessary to keep the `net_connect` component simple and dependency free. Please run this command from your project location to add the dependency: +> ```bash +> idf.py add-dependency espressif/esp_tinyusb^1 +> ``` + +#### Setup a PPP server + +Connect the board using UART or USB and note the device name, which would be typically: +* `/dev/ttyACMx` for USB devices +* `/dev/ttyUSBx` for UART devices + +Run the pppd server: + +```bash +sudo pppd /dev/ttyACM0 115200 192.168.11.1:192.168.11.2 ms-dns 8.8.8.8 modem local noauth debug nocrtscts nodetach +ipv6 +``` + +Please update the parameters with the correct serial device, baud rate, IP addresses, DNS server, use `+ipv6` if `CONFIG_NET_CONNECT_IPV6=y`. + +#### Connection to outside + +In order to access other network endpoints, we have to configure some IP/translation rules. The easiest method is to setup a masquerade of the PPPD created interface (`ppp0`) to your default networking interface (`${ETH0}`). Here is an example of such rule: + +```bash +sudo iptables -t nat -A POSTROUTING -o ${ETH0} -j MASQUERADE +sudo iptables -A FORWARD -i ${ETH0} -o ppp0 -m state --state RELATED,ESTABLISHED -j ACCEPT +sudo iptables -A FORWARD -i ppp0 -o ${ETH0} -j ACCEPT +``` diff --git a/components/net_connect/addr_from_stdin.c b/components/net_connect/addr_from_stdin.c new file mode 100644 index 0000000000..9aac72d7b5 --- /dev/null +++ b/components/net_connect/addr_from_stdin.c @@ -0,0 +1,75 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "net_connect.h" + +#include "lwip/sockets.h" +#include +#include + +#define HOST_IP_SIZE 128 + +esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_storage *dest_addr) +{ + char host_ip[HOST_IP_SIZE]; + int len; + static bool already_init = false; + + // this function could be called multiple times -> make sure UART init runs only once + if (!already_init) { + net_configure_stdin_stdout(); + already_init = true; + } + + // ignore empty or LF only string (could receive from DUT class) + do { + fgets(host_ip, HOST_IP_SIZE, stdin); + len = strlen(host_ip); + } while (len <= 1 && host_ip[0] == '\n'); + host_ip[len - 1] = '\0'; + + struct addrinfo hints, *addr_list, *cur; + memset(&hints, 0, sizeof(hints)); + + // run getaddrinfo() to decide on the IP protocol + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = sock_type; + hints.ai_protocol = IPPROTO_TCP; + if (getaddrinfo(host_ip, NULL, &hints, &addr_list) != 0) { + return ESP_FAIL; + } + for (cur = addr_list; cur != NULL; cur = cur->ai_next) { + memcpy(dest_addr, cur->ai_addr, sizeof(*dest_addr)); +#if CONFIG_NET_CONNECT_CONNECT_IPV4 + if (cur->ai_family == AF_INET) { + *ip_protocol = IPPROTO_IP; + *addr_family = AF_INET; + // add port number and return on first IPv4 match + ((struct sockaddr_in*)dest_addr)->sin_port = htons(port); + freeaddrinfo(addr_list); + return ESP_OK; + } +#endif // IPV4 +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + if (cur->ai_family == AF_INET6) { + *ip_protocol = IPPROTO_IPV6; + *addr_family = AF_INET6; + // add port and interface number and return on first IPv6 match + ((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port); + ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(NET_CONNECT_INTERFACE); + freeaddrinfo(addr_list); + return ESP_OK; + } +#endif // IPV6 + } + // no match found + freeaddrinfo(addr_list); + return ESP_FAIL; +} diff --git a/components/net_connect/connect.c b/components/net_connect/connect.c new file mode 100644 index 0000000000..1f2507e790 --- /dev/null +++ b/components/net_connect/connect.c @@ -0,0 +1,148 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "net_connect.h" +#include "net_connect_private.h" +#include "sdkconfig.h" +#include "esp_event.h" +#include "esp_wifi.h" +#include "esp_wifi_default.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "lwip/err.h" +#include "lwip/sys.h" + +static const char *TAG = "net_connect"; + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +/* types of ipv6 addresses to be displayed on ipv6 events */ +const char *net_connect_ipv6_addr_types_to_str[6] = { + "ESP_IP6_ADDR_IS_UNKNOWN", + "ESP_IP6_ADDR_IS_GLOBAL", + "ESP_IP6_ADDR_IS_LINK_LOCAL", + "ESP_IP6_ADDR_IS_SITE_LOCAL", + "ESP_IP6_ADDR_IS_UNIQUE_LOCAL", + "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6" +}; +#endif + +/** + * @brief Checks the netif description if it contains specified prefix. + * All netifs created within common connect component are prefixed with the module TAG, + * so it returns true if the specified netif is owned by this module + */ +bool net_connect_is_our_netif(const char *prefix, esp_netif_t *netif) +{ + return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; +} + +static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx) +{ + return strcmp(ctx, esp_netif_get_desc(netif)) == 0; +} + +esp_netif_t *net_get_netif_from_desc(const char *desc) +{ + return esp_netif_find_if(netif_desc_matches_with, (void*)desc); +} + +static esp_err_t print_all_ips_tcpip(void* ctx) +{ + const char *prefix = ctx; + // iterate over active interfaces, and print out IPs of "our" netifs + esp_netif_t *netif = NULL; + while ((netif = esp_netif_next_unsafe(netif)) != NULL) { + if (net_connect_is_our_netif(prefix, netif)) { + ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); +#if CONFIG_NET_CONNECT_CONNECT_IPV4 + esp_netif_ip_info_t ip; + ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); + + ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip)); +#endif +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; + int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); + for (int j = 0; j < ip6_addrs; ++j) { + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j])); + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), net_connect_ipv6_addr_types_to_str[ipv6_type]); + } +#endif + } + } + return ESP_OK; +} + +void net_connect_print_all_netif_ips(const char *prefix) +{ + // Print all IPs in TCPIP context to avoid potential races of removing/adding netifs when iterating over the list + esp_netif_tcpip_exec(print_all_ips_tcpip, (void*) prefix); +} + + +esp_err_t net_connect(void) +{ +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + if (net_connect_ethernet_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_ethernet_shutdown)); +#endif +#if CONFIG_NET_CONNECT_CONNECT_WIFI + if (net_connect_wifi_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_wifi_shutdown)); +#endif +#if CONFIG_NET_CONNECT_CONNECT_THREAD + if (net_connect_thread_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_thread_shutdown)); +#endif +#if CONFIG_NET_CONNECT_CONNECT_PPP + if (net_connect_ppp_connect() != ESP_OK) { + return ESP_FAIL; + } + ESP_ERROR_CHECK(esp_register_shutdown_handler(&net_connect_ppp_shutdown)); +#endif + +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_ETH); +#endif + +#if CONFIG_NET_CONNECT_CONNECT_WIFI + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_STA); +#endif + +#if CONFIG_NET_CONNECT_CONNECT_THREAD + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_THREAD); +#endif + +#if CONFIG_NET_CONNECT_CONNECT_PPP + net_connect_print_all_netif_ips(NET_CONNECT_NETIF_DESC_PPP); +#endif + + return ESP_OK; +} + + +esp_err_t net_disconnect(void) +{ +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET + net_connect_ethernet_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&net_connect_ethernet_shutdown)); +#endif +#if CONFIG_NET_CONNECT_CONNECT_WIFI + net_connect_wifi_shutdown(); + ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&net_connect_wifi_shutdown)); +#endif + return ESP_OK; +} diff --git a/components/net_connect/console_cmd.c b/components/net_connect/console_cmd.c new file mode 100644 index 0000000000..094121b978 --- /dev/null +++ b/components/net_connect/console_cmd.c @@ -0,0 +1,87 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#include +#include "net_connect.h" +#include "net_connect_private.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" + + +static const char *TAG = "example_console"; + +typedef struct { + struct arg_str *ssid; + struct arg_str *password; + struct arg_int *channel; + struct arg_end *end; +} wifi_connect_args_t; +static wifi_connect_args_t connect_args; + +static int cmd_do_wifi_connect(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &connect_args); + + if (nerrors != 0) { + arg_print_errors(stderr, connect_args.end, argv[0]); + return 1; + } + + wifi_config_t wifi_config = { + .sta = { + .scan_method = WIFI_ALL_CHANNEL_SCAN, + .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, + }, + }; + if (connect_args.channel->count > 0) { + wifi_config.sta.channel = (uint8_t)(connect_args.channel->ival[0]); + } + const char *ssid = connect_args.ssid->sval[0]; + const char *pass = connect_args.password->sval[0]; + strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid)); + if (pass) { + strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password)); + } + net_connect_wifi_sta_do_connect(wifi_config, false); + return 0; +} + +static int cmd_do_wifi_disconnect(int argc, char **argv) +{ + net_connect_wifi_sta_do_disconnect(); + return 0; +} + +void net_register_wifi_connect_commands(void) +{ + ESP_LOGI(TAG, "Registering WiFi connect commands."); + net_connect_wifi_start(); + + connect_args.ssid = arg_str1(NULL, NULL, "", "SSID of AP"); + connect_args.password = arg_str0(NULL, NULL, "", "password of AP"); + connect_args.channel = arg_int0("n", "channel", "", "channel of AP"); + connect_args.end = arg_end(2); + const esp_console_cmd_t wifi_connect_cmd = { + .command = "wifi_connect", + .help = "WiFi is station mode, join specified soft-AP", + .hint = NULL, + .func = &cmd_do_wifi_connect, + .argtable = &connect_args + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&wifi_connect_cmd)); + + + const esp_console_cmd_t wifi_disconnect_cmd = { + .command = "wifi_disconnect", + .help = "Do wifi disconnect", + .hint = NULL, + .func = &cmd_do_wifi_disconnect, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&wifi_disconnect_cmd)); +} diff --git a/components/net_connect/eth_connect.c b/components/net_connect/eth_connect.c new file mode 100644 index 0000000000..026ea555d7 --- /dev/null +++ b/components/net_connect/eth_connect.c @@ -0,0 +1,198 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "net_connect.h" +#include "net_connect_private.h" +#include "esp_event.h" +#include "esp_eth.h" +#include "esp_log.h" +#include "esp_mac.h" +#include "driver/gpio.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" + + +static const char *TAG = "ethernet_connect"; +static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; +#endif + +static esp_netif_t *eth_start(void); +static void eth_stop(void); + + +/** Event handler for Ethernet events */ + +static void eth_on_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { + return; + } + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + xSemaphoreGive(s_semph_get_ip_addrs); +} + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + +static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_ETH, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { + xSemaphoreGive(s_semph_get_ip6_addrs); + } +} + +static void on_eth_event(void *esp_netif, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + ESP_LOGI(TAG, "Ethernet Link Up"); + ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif)); + break; + default: + break; + } +} + +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 + +static esp_eth_handle_t s_eth_handle = NULL; +static esp_eth_mac_t *s_mac = NULL; +static esp_eth_phy_t *s_phy = NULL; +static esp_eth_netif_glue_handle_t s_eth_glue = NULL; + +static esp_netif_t *eth_start(void) +{ +// TODO just to pass builds, will be fixed by IDF-14059 +#if CONFIG_NET_CONNECT_USE_DUMMY + return NULL; +#else + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH(); + // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_ETH; + esp_netif_config.route_prio = 64; + esp_netif_config_t netif_config = { + .base = &esp_netif_config, + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH + }; + esp_netif_t *netif = esp_netif_new(&netif_config); + assert(netif); + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + mac_config.rx_task_stack_size = CONFIG_NET_CONNECT_ETHERNET_EMAC_TASK_STACK_SIZE; + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = CONFIG_NET_CONNECT_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_NET_CONNECT_ETH_PHY_RST_GPIO; +#if CONFIG_NET_CONNECT_USE_INTERNAL_ETHERNET + eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + esp32_emac_config.smi_gpio.mdc_num = CONFIG_NET_CONNECT_ETH_MDC_GPIO; + esp32_emac_config.smi_gpio.mdio_num = CONFIG_NET_CONNECT_ETH_MDIO_GPIO; + s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); +#if CONFIG_NET_CONNECT_ETH_PHY_GENERIC + s_phy = esp_eth_phy_new_generic(&phy_config); +#endif // CONFIG_NET_CONNECT_ETH_PHY_GENERIC +#elif CONFIG_NET_CONNECT_USE_OPENETH + phy_config.autonego_timeout_ms = 100; + s_mac = esp_eth_mac_new_openeth(&mac_config); + s_phy = esp_eth_phy_new_generic(&phy_config); +#endif // CONFIG_NET_CONNECT_USE_INTERNAL_ETHERNET + + // Install Ethernet driver + esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); + ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); + + // combine driver with netif + s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); + esp_netif_attach(netif, s_eth_glue); + + // Register user defined event handlers + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip, NULL)); +#ifdef CONFIG_NET_CONNECT_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6, NULL)); +#endif + + esp_eth_start(s_eth_handle); + return netif; +#endif // CONFIG_NET_CONNECT_USE_DUMMY +} + +static void eth_stop(void) +{ + esp_netif_t *eth_netif = net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, ð_on_got_ip)); +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, ð_on_got_ipv6)); + ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); +#endif + ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle)); + ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue)); + ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); + s_eth_handle = NULL; + ESP_ERROR_CHECK(s_phy->del(s_phy)); + ESP_ERROR_CHECK(s_mac->del(s_mac)); + + esp_netif_destroy(eth_netif); +} + +esp_eth_handle_t net_get_eth_handle(void) +{ + return s_eth_handle; +} + +/* tear down connection, release resources */ +void net_connect_ethernet_shutdown(void) +{ + if (s_semph_get_ip_addrs == NULL) { + return; + } + vSemaphoreDelete(s_semph_get_ip_addrs); + s_semph_get_ip_addrs = NULL; +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + vSemaphoreDelete(s_semph_get_ip6_addrs); + s_semph_get_ip6_addrs = NULL; +#endif + eth_stop(); +} + +esp_err_t net_connect_ethernet_connect(void) +{ +#if CONFIG_NET_CONNECT_CONNECT_IPV4 + s_semph_get_ip_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip_addrs == NULL) { + return ESP_ERR_NO_MEM; + } +#endif +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip6_addrs == NULL) { + vSemaphoreDelete(s_semph_get_ip_addrs); + return ESP_ERR_NO_MEM; + } +#endif + eth_start(); + ESP_LOGI(TAG, "Waiting for IP(s)."); +#if CONFIG_NET_CONNECT_CONNECT_IPV4 + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); +#endif +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); +#endif + return ESP_OK; +} diff --git a/components/net_connect/idf_component.yml b/components/net_connect/idf_component.yml new file mode 100644 index 0000000000..69fbd24b5e --- /dev/null +++ b/components/net_connect/idf_component.yml @@ -0,0 +1,6 @@ +version: 1.0.0 +url: https://github.com/espressif/esp-protocols/tree/master/components/net_connect +description: Network connection component providing WiFi, Ethernet, Thread, and PPP connection methods for ESP32 boards. +dependencies: + idf: + version: '>=5.0' diff --git a/components/net_connect/include/addr_from_stdin.h b/components/net_connect/include/addr_from_stdin.h new file mode 100644 index 0000000000..4e1a10cf82 --- /dev/null +++ b/components/net_connect/include/addr_from_stdin.h @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "lwip/sys.h" +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Read and evaluate IP address from stdin + * + * This API reads stdin and parses the input address using getaddrinfo() + * to fill in struct sockaddr_storage (for both IPv4 and IPv6) used to open + * a socket. IP protocol is guessed from the IP address string. + * + * @param[in] port port number of expected connection + * @param[in] sock_type expected protocol: SOCK_STREAM or SOCK_DGRAM + * @param[out] ip_protocol resultant IP protocol: IPPROTO_IP or IPPROTO_IP6 + * @param[out] addr_family resultant address family: AF_INET or AF_INET6 + * @param[out] dest_addr sockaddr_storage structure (for both IPv4 and IPv6) + * @return ESP_OK on success, ESP_FAIL otherwise + */ +esp_err_t get_addr_from_stdin(int port, int sock_type, + int *ip_protocol, + int *addr_family, + struct sockaddr_storage *dest_addr); + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/include/net_connect.h b/components/net_connect/include/net_connect.h new file mode 100644 index 0000000000..f11087dc60 --- /dev/null +++ b/components/net_connect/include/net_connect.h @@ -0,0 +1,154 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "sdkconfig.h" +#include "esp_err.h" +#if !CONFIG_IDF_TARGET_LINUX +#include "esp_netif.h" +#if CONFIG_NET_CONNECT_ETHERNET +#include "esp_eth.h" +#endif +#endif // !CONFIG_IDF_TARGET_LINUX + +#ifdef __cplusplus +extern "C" { +#endif + +#if !CONFIG_IDF_TARGET_LINUX +#if CONFIG_NET_CONNECT_CONNECT_WIFI +#define NET_CONNECT_NETIF_DESC_STA "net_connect_netif_sta" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET +#define NET_CONNECT_NETIF_DESC_ETH "net_connect_netif_eth" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_THREAD +#define NET_CONNECT_NETIF_DESC_THREAD "net_connect_netif_thread" +#endif + +#if CONFIG_NET_CONNECT_CONNECT_PPP +#define NET_CONNECT_NETIF_DESC_PPP "net_connect_netif_ppp" +#endif + +#if CONFIG_NET_CONNECT_WIFI_SCAN_METHOD_FAST +#define NET_CONNECT_WIFI_SCAN_METHOD WIFI_FAST_SCAN +#elif CONFIG_NET_CONNECT_WIFI_SCAN_METHOD_ALL_CHANNEL +#define NET_CONNECT_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#endif + +#if CONFIG_NET_CONNECT_WIFI_CONNECT_AP_BY_SIGNAL +#define NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_NET_CONNECT_WIFI_CONNECT_AP_BY_SECURITY +#define NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#endif + +#if CONFIG_NET_CONNECT_WIFI_AUTH_OPEN +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WEP +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA_WPA2_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_ENTERPRISE +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA3_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WPA2_WPA3_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_NET_CONNECT_WIFI_AUTH_WAPI_PSK +#define NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + +/* Network connect default interface, prefer the ethernet one if running in example-test (CI) configuration */ +#if CONFIG_NET_CONNECT_CONNECT_ETHERNET +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_ETH) +#elif CONFIG_NET_CONNECT_CONNECT_WIFI +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_STA) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_STA) +#elif CONFIG_NET_CONNECT_CONNECT_THREAD +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_THREAD) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_THREAD) +#elif CONFIG_NET_CONNECT_CONNECT_PPP +#define NET_CONNECT_INTERFACE net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_PPP) +#define net_get_netif() net_get_netif_from_desc(NET_CONNECT_NETIF_DESC_PPP) +#endif + +/** + * @brief Configure Wi-Fi or Ethernet, connect, wait for IP + * + * This all-in-one helper function is used in protocols examples to + * reduce the amount of boilerplate in the example. + * + * It is not intended to be used in real world applications. + * See examples under examples/wifi/getting_started/ and examples/ethernet/ + * for more complete Wi-Fi or Ethernet initialization code. + * + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + * + * @return ESP_OK on successful connection + */ +esp_err_t net_connect(void); + +/** + * Counterpart to net_connect, de-initializes Wi-Fi or Ethernet + */ +esp_err_t net_disconnect(void); + +/** + * @brief Configure stdin and stdout to use blocking I/O + * + * This helper function is used in ASIO examples. It wraps installing the + * UART driver and configuring VFS layer to use UART driver for console I/O. + */ +esp_err_t net_configure_stdin_stdout(void); + +/** + * @brief Returns esp-netif pointer created by net_connect() described by + * the supplied desc field + * + * @param desc Textual interface of created network interface, for example "sta" + * indicate default WiFi station, "eth" default Ethernet interface. + * + */ +esp_netif_t *net_get_netif_from_desc(const char *desc); + +#if CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD +/** + * @brief Register wifi connect commands + * + * Provide a simple wifi_connect command in esp_console. + * This function can be used after esp_console is initialized. + */ +void net_register_wifi_connect_commands(void); +#endif + +#if CONFIG_NET_CONNECT_ETHERNET +/** + * @brief Get the network connect Ethernet driver handle + * + * @return esp_eth_handle_t + */ +esp_eth_handle_t net_get_eth_handle(void); +#endif // CONFIG_NET_CONNECT_ETHERNET + +#else +static inline esp_err_t net_connect(void) +{ + return ESP_OK; +} +#endif // !CONFIG_IDF_TARGET_LINUX + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/include/net_connect_private.h b/components/net_connect/include/net_connect_private.h new file mode 100644 index 0000000000..cb14635d26 --- /dev/null +++ b/components/net_connect/include/net_connect_private.h @@ -0,0 +1,58 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/* Private Functions of protocol example common */ + +#pragma once + +#include "esp_err.h" +#include "esp_wifi.h" +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +#define MAX_IP6_ADDRS_PER_NETIF (5) + +#if defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_LOCAL_LINK) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_GLOBAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_SITE_LOCAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL +#elif defined(CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_UNIQUE_LOCAL) +#define NET_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL +#endif // if-elif CONFIG_NET_CONNECT_CONNECT_IPV6_PREF_... + +#endif + + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +extern const char *net_connect_ipv6_addr_types_to_str[6]; +#endif + +void net_connect_wifi_start(void); +void net_connect_wifi_stop(void); +esp_err_t net_connect_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait); +esp_err_t net_connect_wifi_sta_do_disconnect(void); +bool net_connect_is_our_netif(const char *prefix, esp_netif_t *netif); +void net_connect_print_all_netif_ips(const char *prefix); +void net_connect_wifi_shutdown(void); +esp_err_t net_connect_wifi_connect(void); +void net_connect_ethernet_shutdown(void); +esp_err_t net_connect_ethernet_connect(void); +void net_connect_thread_shutdown(void); +esp_err_t net_connect_thread_connect(void); +esp_err_t net_connect_ppp_connect(void); +void net_connect_ppp_start(void); +void net_connect_ppp_shutdown(void); + + + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/include/protocol_examples_thread_config.h b/components/net_connect/include/protocol_examples_thread_config.h new file mode 100644 index 0000000000..cdd269599c --- /dev/null +++ b/components/net_connect/include/protocol_examples_thread_config.h @@ -0,0 +1,115 @@ +/* + * Thread configurations for protocol examples + * + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include + +#include + +#ifdef CONFIG_OPENTHREAD_RADIO_NATIVE +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_NATIVE, \ + } + +#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_UART) +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_UART_RCP, \ + .radio_uart_config = \ + { \ + .port = CONFIG_NET_CONNECT_THREAD_UART_PORT, \ + .uart_config = \ + { \ + .baud_rate = CONFIG_NET_CONNECT_THREAD_UART_BAUD, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_DEFAULT, \ + }, \ + .rx_pin = CONFIG_NET_CONNECT_THREAD_UART_RX_PIN, \ + .tx_pin = CONFIG_NET_CONNECT_THREAD_UART_TX_PIN, \ + }, \ + } +#elif defined(CONFIG_OPENTHREAD_RADIO_SPINEL_SPI) +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_SPI_RCP, \ + .radio_spi_config = \ + { \ + .host_device = SPI2_HOST, \ + .dma_channel = 2, \ + .spi_interface = \ + { \ + .mosi_io_num = CONFIG_NET_CONNECT_THREAD_SPI_MOSI_PIN, \ + .miso_io_num = CONFIG_NET_CONNECT_THREAD_SPI_MISO_PIN, \ + .sclk_io_num = CONFIG_NET_CONNECT_THREAD_SPI_SCLK_PIN, \ + .quadwp_io_num = -1, \ + .quadhd_io_num = -1, \ + }, \ + .spi_device = \ + { \ + .cs_ena_pretrans = 2, \ + .input_delay_ns = 100, \ + .mode = 0, \ + .clock_speed_hz = 2500 * 1000, \ + .spics_io_num = CONFIG_NET_CONNECT_THREAD_SPI_CS_PIN, \ + .queue_size = 5, \ + }, \ + .intr_pin = CONFIG_NET_CONNECT_THREAD_SPI_INTR_PIN, \ + }, \ + } +#else +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_TREL, \ + } +#endif + +#if CONFIG_OPENTHREAD_CONSOLE_TYPE_UART +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_CLI_UART, \ + .host_uart_config = \ + { \ + .port = 0, \ + .uart_config = \ + { \ + .baud_rate = 115200, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_DEFAULT, \ + }, \ + .rx_pin = UART_PIN_NO_CHANGE, \ + .tx_pin = UART_PIN_NO_CHANGE, \ + }, \ + } +#elif CONFIG_OPENTHREAD_CONSOLE_TYPE_USB_SERIAL_JTAG +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_CLI_USB, \ + .host_usb_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(), \ + } +#else +#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_NONE, \ + } +#endif + +#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \ + { \ + .storage_partition_name = "nvs", \ + .netif_queue_size = 10, \ + .task_queue_size = 10, \ + } diff --git a/components/net_connect/include/protocol_examples_utils.h b/components/net_connect/include/protocol_examples_utils.h new file mode 100644 index 0000000000..dd39aa2582 --- /dev/null +++ b/components/net_connect/include/protocol_examples_utils.h @@ -0,0 +1,49 @@ +/* + * Utility functions for protocol examples + * + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @brief Encode an URI + * + * @param dest a destination memory location + * @param src the source string + * @param len the length of the source string + * @return uint32_t the count of escaped characters + * + * @note Please allocate the destination buffer keeping in mind that encoding a + * special character will take up 3 bytes (for '%' and two hex digits). + * In the worst-case scenario, the destination buffer will have to be 3 times + * that of the source string. + */ +uint32_t example_uri_encode(char *dest, const char *src, size_t len); + +/** + * @brief Decode an URI + * + * @param dest a destination memory location + * @param src the source string + * @param len the length of the source string + * + * @note Please allocate the destination buffer keeping in mind that a decoded + * special character will take up 2 less bytes than its encoded form. + * In the worst-case scenario, the destination buffer will have to be + * the same size that of the source string. + */ +void example_uri_decode(char *dest, const char *src, size_t len); + +#ifdef __cplusplus +} +#endif diff --git a/components/net_connect/ppp_connect.c b/components/net_connect/ppp_connect.c new file mode 100644 index 0000000000..5b68ecb1e9 --- /dev/null +++ b/components/net_connect/ppp_connect.c @@ -0,0 +1,260 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "sdkconfig.h" +#include "net_connect.h" +#include "net_connect_private.h" + +#if CONFIG_NET_CONNECT_CONNECT_PPP +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_netif_ppp.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB +#include "tinyusb.h" +#include "tusb_cdc_acm.h" + +static int s_itf; +static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE]; + +#else // DEVICE is UART + +#include "driver/uart.h" +#define BUF_SIZE (1024) +static bool s_stop_task = false; + +#endif // CONNECT_PPP_DEVICE + + +static const char *TAG = "example_connect_ppp"; +static int s_retry_num = 0; +static EventGroupHandle_t s_event_group = NULL; +static esp_netif_t *s_netif; +static const int GOT_IPV4 = BIT0; +static const int CONNECTION_FAILED = BIT1; +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +static const int GOT_IPV6 = BIT2; +#define CONNECT_BITS (GOT_IPV4|GOT_IPV6|CONNECTION_FAILED) +#else +#define CONNECT_BITS (GOT_IPV4|CONNECTION_FAILED) +#endif + +static esp_err_t transmit(void *h, void *buffer, size_t len) +{ + ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE); +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB + tinyusb_cdcacm_write_queue(s_itf, buffer, len); + tinyusb_cdcacm_write_flush(s_itf, 0); +#else // DEVICE_UART + uart_write_bytes(UART_NUM_1, buffer, len); +#endif // CONNECT_PPP_DEVICE + return ESP_OK; +} + +static esp_netif_driver_ifconfig_t driver_cfg = { + .handle = (void *)1, // singleton driver, just to != NULL + .transmit = transmit, +}; +const esp_netif_driver_ifconfig_t *ppp_driver_cfg = &driver_cfg; + +static void on_ip_event(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + + if (event_id == IP_EVENT_PPP_GOT_IP) { + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_PPP, event->esp_netif)) { + return; + } + esp_netif_t *netif = event->esp_netif; + esp_netif_dns_info_t dns_info; + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_info); + ESP_LOGI(TAG, "Main DNS server : " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); + xEventGroupSetBits(s_event_group, GOT_IPV4); +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + } else if (event_id == IP_EVENT_GOT_IP6) { + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_PPP, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { + xEventGroupSetBits(s_event_group, GOT_IPV6); + } +#endif + } else if (event_id == IP_EVENT_PPP_LOST_IP) { + ESP_LOGI(TAG, "Disconnect from PPP Server"); + s_retry_num++; + if (s_retry_num > CONFIG_NET_CONNECT_PPP_CONN_MAX_RETRY) { + ESP_LOGE(TAG, "PPP Connection failed %d times, stop reconnecting.", s_retry_num); + xEventGroupSetBits(s_event_group, CONNECTION_FAILED); + } else { + ESP_LOGI(TAG, "PPP Connection failed %d times, try to reconnect.", s_retry_num); + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); + } + + } +} + +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB +static void cdc_rx_callback(int itf, cdcacm_event_t *event) +{ + size_t rx_size = 0; + if (itf != s_itf) { + // Not our channel + return; + } + esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); + if (ret == ESP_OK) { + ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_VERBOSE); + // pass the received data to the network interface + esp_netif_receive(s_netif, buf, rx_size, NULL); + } else { + ESP_LOGE(TAG, "Read error"); + } +} + +static void line_state_changed(int itf, cdcacm_event_t *event) +{ + s_itf = itf; // use this channel for the netif communication + ESP_LOGI(TAG, "Line state changed on channel %d", itf); +} +#else // DEVICE is UART + +static void ppp_task(void *args) +{ + uart_config_t uart_config = {}; + uart_config.baud_rate = CONFIG_NET_CONNECT_CONNECT_UART_BAUDRATE; + uart_config.data_bits = UART_DATA_8_BITS; + uart_config.parity = UART_PARITY_DISABLE; + uart_config.stop_bits = UART_STOP_BITS_1; + uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; + uart_config.source_clk = UART_SCLK_DEFAULT; + + QueueHandle_t event_queue; + ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE, 0, 16, &event_queue, 0)); + ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config)); + ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, CONFIG_NET_CONNECT_CONNECT_UART_TX_PIN, CONFIG_NET_CONNECT_CONNECT_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1)); + + char *buffer = (char*)malloc(BUF_SIZE); + uart_event_t event; + esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, s_netif); + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); + while (!s_stop_task) { + xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000)); + if (event.type == UART_DATA) { + size_t len; + uart_get_buffered_data_len(UART_NUM_1, &len); + if (len) { + len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0); + ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE); + esp_netif_receive(s_netif, buffer, len, NULL); + } + } else { + ESP_LOGW(TAG, "Received UART event: %d", event.type); + } + } + free(buffer); + vTaskDelete(NULL); +} + +#endif // CONNECT_PPP_DEVICE + +esp_err_t net_connect_ppp_connect(void) +{ + ESP_LOGI(TAG, "Start example_connect."); + +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB + ESP_LOGI(TAG, "USB initialization"); + const tinyusb_config_t tusb_cfg = { + .device_descriptor = NULL, + .string_descriptor = NULL, + .external_phy = false, + .configuration_descriptor = NULL, + }; + + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + tinyusb_config_cdcacm_t acm_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .callback_rx = &cdc_rx_callback, + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL + }; + + ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + /* the second way to register a callback */ + ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( + TINYUSB_CDC_ACM_0, + CDC_EVENT_LINE_STATE_CHANGED, + &line_state_changed)); +#endif // CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB + + s_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event, NULL)); + + esp_netif_inherent_config_t base_netif_cfg = ESP_NETIF_INHERENT_DEFAULT_PPP(); + base_netif_cfg.if_desc = NET_CONNECT_NETIF_DESC_PPP; + esp_netif_config_t netif_ppp_config = { .base = &base_netif_cfg, + .driver = ppp_driver_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP + }; + + s_netif = esp_netif_new(&netif_ppp_config); + assert(s_netif); +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_USB + esp_netif_action_start(s_netif, 0, 0, 0); + esp_netif_action_connected(s_netif, 0, 0, 0); +#else // DEVICE is UART + s_stop_task = false; + if (xTaskCreate(ppp_task, "ppp connect", 4096, NULL, 5, NULL) != pdTRUE) { + ESP_LOGE(TAG, "Failed to create a ppp connection task"); + return ESP_FAIL; + } +#endif // CONNECT_PPP_DEVICE + + ESP_LOGI(TAG, "Waiting for IP address"); + EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS, pdFALSE, pdFALSE, portMAX_DELAY); + if (bits & CONNECTION_FAILED) { + ESP_LOGE(TAG, "Connection failed!"); + return ESP_FAIL; + } + ESP_LOGI(TAG, "Connected!"); + + return ESP_OK; +} + +void net_connect_ppp_shutdown(void) +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event)); +#if CONFIG_NET_CONNECT_CONNECT_PPP_DEVICE_UART + s_stop_task = true; + vTaskDelay(pdMS_TO_TICKS(1000)); // wait for the ppp task to stop +#endif + + esp_netif_action_disconnected(s_netif, 0, 0, 0); + + vEventGroupDelete(s_event_group); + esp_netif_action_stop(s_netif, 0, 0, 0); + esp_netif_destroy(s_netif); + s_netif = NULL; + s_event_group = NULL; +} + +#endif // CONFIG_NET_CONNECT_CONNECT_PPP diff --git a/components/net_connect/protocol_examples_utils.c b/components/net_connect/protocol_examples_utils.c new file mode 100644 index 0000000000..09296fc131 --- /dev/null +++ b/components/net_connect/protocol_examples_utils.c @@ -0,0 +1,387 @@ +/* + * Utility functions for protocol examples + * + * SPDX-FileCopyrightText: 2002-2021 Igor Sysoev + * 2011-2022 Nginx, Inc. + * 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/* + * Copyright (C) 2002-2021 Igor Sysoev + * Copyright (C) 2011-2022 Nginx, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "protocol_examples_utils.h" + +/* Type of Escape algorithms to be used */ +#define NGX_ESCAPE_URI (0) +#define NGX_ESCAPE_ARGS (1) +#define NGX_ESCAPE_URI_COMPONENT (2) +#define NGX_ESCAPE_HTML (3) +#define NGX_ESCAPE_REFRESH (4) +#define NGX_ESCAPE_MEMCACHED (5) +#define NGX_ESCAPE_MAIL_AUTH (6) + +/* Type of Unescape algorithms to be used */ +#define NGX_UNESCAPE_URI (1) +#define NGX_UNESCAPE_REDIRECT (2) + + +uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, unsigned int type) +{ + unsigned int n; + uint32_t *escape; + static u_char hex[] = "0123456789ABCDEF"; + + /* + * Per RFC 3986 only the following chars are allowed in URIs unescaped: + * + * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" + * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" + * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" + * / "*" / "+" / "," / ";" / "=" + * + * And "%" can appear as a part of escaping itself. The following + * characters are not allowed and need to be escaped: %00-%1F, %7F-%FF, + * " ", """, "<", ">", "\", "^", "`", "{", "|", "}". + */ + + /* " ", "#", "%", "?", not allowed */ + + static uint32_t uri[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xd000002d, /* 1101 0000 0000 0000 0000 0000 0010 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "#", "%", "&", "+", ";", "?", not allowed */ + + static uint32_t args[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xd800086d, /* 1101 1000 0000 0000 0000 1000 0110 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* not ALPHA, DIGIT, "-", ".", "_", "~" */ + + static uint32_t uri_component[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "#", """, "%", "'", not allowed */ + + static uint32_t html[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x500000ad, /* 0101 0000 0000 0000 0000 0000 1010 1101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", """, "'", not allowed */ + + static uint32_t refresh[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x50000085, /* 0101 0000 0000 0000 0000 0000 1000 0101 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0xd8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */ + + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + }; + + /* " ", "%", %00-%1F */ + + static uint32_t memcached[] = { + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */ + + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ + }; + + /* mail_auth is the same as memcached */ + + static uint32_t *map[] = + { uri, args, uri_component, html, refresh, memcached, memcached }; + + + escape = map[type]; + + if (dst == NULL) { + + /* find the number of the characters to be escaped */ + + n = 0; + + while (size) { + if (escape[*src >> 5] & (1U << (*src & 0x1f))) { + n++; + } + src++; + size--; + } + + return (uintptr_t) n; + } + + while (size) { + if (escape[*src >> 5] & (1U << (*src & 0x1f))) { + *dst++ = '%'; + *dst++ = hex[*src >> 4]; + *dst++ = hex[*src & 0xf]; + src++; + + } else { + *dst++ = *src++; + } + size--; + } + + return (uintptr_t) dst; +} + + +void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type) +{ + u_char *d, *s, ch, c, decoded; + enum { + sw_usual = 0, + sw_quoted, + sw_quoted_second + } state; + + d = *dst; + s = *src; + + state = 0; + decoded = 0; + + while (size--) { + + ch = *s++; + + switch (state) { + case sw_usual: + if (ch == '?' + && (type & (NGX_UNESCAPE_URI | NGX_UNESCAPE_REDIRECT))) { + *d++ = ch; + goto done; + } + + if (ch == '%') { + state = sw_quoted; + break; + } + + *d++ = ch; + break; + + case sw_quoted: + + if (ch >= '0' && ch <= '9') { + decoded = (u_char)(ch - '0'); + state = sw_quoted_second; + break; + } + + c = (u_char)(ch | 0x20); + if (c >= 'a' && c <= 'f') { + decoded = (u_char)(c - 'a' + 10); + state = sw_quoted_second; + break; + } + + /* the invalid quoted character */ + + state = sw_usual; + + *d++ = ch; + + break; + + case sw_quoted_second: + + state = sw_usual; + + if (ch >= '0' && ch <= '9') { + ch = (u_char)((decoded << 4) + (ch - '0')); + + if (type & NGX_UNESCAPE_REDIRECT) { + if (ch > '%' && ch < 0x7f) { + *d++ = ch; + break; + } + + *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); + + break; + } + + *d++ = ch; + + break; + } + + c = (u_char)(ch | 0x20); + if (c >= 'a' && c <= 'f') { + ch = (u_char)((decoded << 4) + (c - 'a') + 10); + + if (type & NGX_UNESCAPE_URI) { + if (ch == '?') { + *d++ = ch; + goto done; + } + + *d++ = ch; + break; + } + + if (type & NGX_UNESCAPE_REDIRECT) { + if (ch == '?') { + *d++ = ch; + goto done; + } + + if (ch > '%' && ch < 0x7f) { + *d++ = ch; + break; + } + + *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); + break; + } + + *d++ = ch; + + break; + } + + /* the invalid quoted character */ + + break; + } + } + +done: + + *dst = d; + *src = s; +} + + +uint32_t example_uri_encode(char *dest, const char *src, size_t len) +{ + if (!src || !dest) { + return 0; + } + + uintptr_t ret = ngx_escape_uri((unsigned char *)dest, (unsigned char *)src, len, NGX_ESCAPE_URI_COMPONENT); + return (uint32_t)(ret - (uintptr_t)dest); +} + + +void example_uri_decode(char *dest, const char *src, size_t len) +{ + if (!src || !dest) { + return; + } + + unsigned char *src_ptr = (unsigned char *)src; + unsigned char *dst_ptr = (unsigned char *)dest; + ngx_unescape_uri(&dst_ptr, &src_ptr, len, NGX_UNESCAPE_URI); +} diff --git a/components/net_connect/sdkconfig.rename b/components/net_connect/sdkconfig.rename new file mode 100644 index 0000000000..fccfd425c2 --- /dev/null +++ b/components/net_connect/sdkconfig.rename @@ -0,0 +1 @@ +CONFIG_EXAMPLE_ETH_PHY_IP101 CONFIG_EXAMPLE_ETH_PHY_GENERIC diff --git a/components/net_connect/stdin_out.c b/components/net_connect/stdin_out.c new file mode 100644 index 0000000000..9049802404 --- /dev/null +++ b/components/net_connect/stdin_out.c @@ -0,0 +1,29 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "net_connect.h" +#include "esp_err.h" +#include "driver/uart_vfs.h" +#include "driver/uart.h" +#include "sdkconfig.h" + +esp_err_t example_configure_stdin_stdout(void) +{ + if (uart_is_driver_installed((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM)) { + return ESP_OK; + } + // Initialize VFS & UART so we can use std::cout/cin + setvbuf(stdin, NULL, _IONBF, 0); + /* Install UART driver for interrupt-driven reads and writes */ + ESP_ERROR_CHECK(uart_driver_install((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0)); + /* Tell VFS to use UART driver */ + uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + uart_vfs_dev_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + /* Move the caret to the beginning of the next line on '\n' */ + uart_vfs_dev_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + return ESP_OK; +} diff --git a/components/net_connect/thread_connect.c b/components/net_connect/thread_connect.c new file mode 100644 index 0000000000..3899d3c4ba --- /dev/null +++ b/components/net_connect/thread_connect.c @@ -0,0 +1,130 @@ +/* + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_err.h" +#include "esp_event.h" +#include "esp_event_base.h" +#include "esp_vfs_eventfd.h" +#include "net_connect_private.h" +#include "net_connect.h" +#include "protocol_examples_thread_config.h" +#include "esp_log.h" +#include + +#include +#include +#include +#include +#include +#include +#include + +static TaskHandle_t s_ot_task_handle = NULL; +static esp_netif_t *s_openthread_netif = NULL; +static SemaphoreHandle_t s_semph_thread_attached = NULL; +static SemaphoreHandle_t s_semph_thread_set_dns_server = NULL; +static const char *TAG = "example_connect"; + +static void thread_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, + void* event_data) +{ + if (event_base == OPENTHREAD_EVENT) { + if (event_id == OPENTHREAD_EVENT_ATTACHED) { + xSemaphoreGive(s_semph_thread_attached); + } else if (event_id == OPENTHREAD_EVENT_SET_DNS_SERVER) { + xSemaphoreGive(s_semph_thread_set_dns_server); + } + } +} + +static void ot_task_worker(void *aContext) +{ + esp_openthread_platform_config_t config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }; + + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_OPENTHREAD(); + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_THREAD; + esp_netif_config_t cfg = { + .base = &esp_netif_config, + .stack = &g_esp_netif_netstack_default_openthread, + }; + s_openthread_netif = esp_netif_new(&cfg); + assert(s_openthread_netif != NULL); + + // Initialize the OpenThread stack + ESP_ERROR_CHECK(esp_openthread_init(&config)); + ESP_ERROR_CHECK(esp_netif_attach(s_openthread_netif, esp_openthread_netif_glue_init(&config))); + esp_openthread_lock_acquire(portMAX_DELAY); + (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL); + esp_openthread_cli_init(); + esp_openthread_cli_create_task(); + otOperationalDatasetTlvs dataset; + otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset); + if (error != OT_ERROR_NONE) { + ESP_ERROR_CHECK(esp_openthread_auto_start(NULL)); + } else { + ESP_ERROR_CHECK(esp_openthread_auto_start(&dataset)); + } + esp_openthread_lock_release(); + + // Run the main loop + esp_openthread_launch_mainloop(); + + // Clean up + esp_openthread_netif_glue_deinit(); + esp_netif_destroy(s_openthread_netif); + esp_vfs_eventfd_unregister(); + vTaskDelete(NULL); +} + +/* tear down connection, release resources */ +void net_connect_thread_shutdown(void) +{ + vTaskDelete(s_ot_task_handle); + esp_openthread_netif_glue_deinit(); + esp_netif_destroy(s_openthread_netif); + esp_vfs_eventfd_unregister(); + vSemaphoreDelete(s_semph_thread_set_dns_server); + vSemaphoreDelete(s_semph_thread_attached); +} + +esp_err_t net_connect_thread_connect(void) +{ + s_semph_thread_attached = xSemaphoreCreateBinary(); + if (s_semph_thread_attached == NULL) { + return ESP_ERR_NO_MEM; + } + s_semph_thread_set_dns_server = xSemaphoreCreateBinary(); + if (s_semph_thread_set_dns_server == NULL) { + vSemaphoreDelete(s_semph_thread_attached); + return ESP_ERR_NO_MEM; + } + // 4 eventfds might be used for Thread + // * netif + // * ot task queue + // * radio driver + // * border router + esp_vfs_eventfd_config_t eventfd_config = { + .max_fds = 4, + }; + esp_vfs_eventfd_register(&eventfd_config); + ESP_ERROR_CHECK(esp_event_handler_register(OPENTHREAD_EVENT, ESP_EVENT_ANY_ID, thread_event_handler, NULL)); + if (xTaskCreate(ot_task_worker, "ot_br_main", CONFIG_NET_CONNECT_THREAD_TASK_STACK_SIZE, NULL, 5, &s_ot_task_handle) != pdPASS) { + vSemaphoreDelete(s_semph_thread_attached); + vSemaphoreDelete(s_semph_thread_set_dns_server); + ESP_LOGE(TAG, "Failed to create openthread task"); + return ESP_FAIL; + } + xSemaphoreTake(s_semph_thread_attached, portMAX_DELAY); + // Wait 1s for the Thread device to set its DNS server with the NAT64 prefix. + if (xSemaphoreTake(s_semph_thread_set_dns_server, 1000 / portTICK_PERIOD_MS) != pdPASS) { + ESP_LOGW(TAG, "DNS server is not set for the Thread device, might be unable to access the Internet"); + } + return ESP_OK; +} diff --git a/components/net_connect/wifi_connect.c b/components/net_connect/wifi_connect.c new file mode 100644 index 0000000000..16cf486f4f --- /dev/null +++ b/components/net_connect/wifi_connect.c @@ -0,0 +1,239 @@ +/* + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "net_connect.h" +#include "net_connect_private.h" +#include "esp_log.h" + +#if CONFIG_NET_CONNECT_CONNECT_WIFI + +static const char *TAG = "example_connect"; +static esp_netif_t *s_example_sta_netif = NULL; +static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; +#endif + +static int s_retry_num = 0; + +static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + s_retry_num++; + if (s_retry_num > CONFIG_NET_CONNECT_WIFI_CONN_MAX_RETRY) { + ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num); + /* let net_connect_wifi_sta_do_connect() return */ + if (s_semph_get_ip_addrs) { + xSemaphoreGive(s_semph_get_ip_addrs); + } +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + if (s_semph_get_ip6_addrs) { + xSemaphoreGive(s_semph_get_ip6_addrs); + } +#endif + net_connect_wifi_sta_do_disconnect(); + return; + } + wifi_event_sta_disconnected_t *disconn = event_data; + if (disconn->reason == WIFI_REASON_ROAMING) { + ESP_LOGD(TAG, "station roaming, do nothing"); + return; + } + ESP_LOGI(TAG, "Wi-Fi disconnected %d, trying to reconnect...", disconn->reason); + esp_err_t err = esp_wifi_connect(); + if (err == ESP_ERR_WIFI_NOT_STARTED) { + return; + } + ESP_ERROR_CHECK(err); +} + +static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + esp_netif_create_ip6_linklocal(esp_netif); +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 +} + +static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + s_retry_num = 0; + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_STA, event->esp_netif)) { + return; + } + ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); + if (s_semph_get_ip_addrs) { + xSemaphoreGive(s_semph_get_ip_addrs); + } else { + ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip)); + } +} + +#if CONFIG_NET_CONNECT_CONNECT_IPV6 +static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; + if (!net_connect_is_our_netif(NET_CONNECT_NETIF_DESC_STA, event->esp_netif)) { + return; + } + esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); + ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), + IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + + if (ipv6_type == NET_CONNECT_PREFERRED_IPV6_TYPE) { + if (s_semph_get_ip6_addrs) { + xSemaphoreGive(s_semph_get_ip6_addrs); + } else { + ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), net_connect_ipv6_addr_types_to_str[ipv6_type]); + } + } +} +#endif // CONFIG_NET_CONNECT_CONNECT_IPV6 + + +void net_connect_wifi_start(void) +{ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); + // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) + esp_netif_config.if_desc = NET_CONNECT_NETIF_DESC_STA; + esp_netif_config.route_prio = 128; + s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config); + esp_wifi_set_default_wifi_sta_handlers(); + + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + + +void net_connect_wifi_stop(void) +{ + esp_err_t err = esp_wifi_stop(); + if (err == ESP_ERR_WIFI_NOT_INIT) { + return; + } + ESP_ERROR_CHECK(err); + ESP_ERROR_CHECK(esp_wifi_deinit()); + ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif)); + esp_netif_destroy(s_example_sta_netif); + s_example_sta_netif = NULL; +} + + +esp_err_t net_connect_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) +{ + if (wait) { + s_semph_get_ip_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip_addrs == NULL) { + return ESP_ERR_NO_MEM; + } +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); + if (s_semph_get_ip6_addrs == NULL) { + vSemaphoreDelete(s_semph_get_ip_addrs); + return ESP_ERR_NO_MEM; + } +#endif + } + s_retry_num = 0; + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL)); +#endif + + ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + esp_err_t ret = esp_wifi_connect(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret); + return ret; + } + if (wait) { + ESP_LOGI(TAG, "Waiting for IP(s)"); +#if CONFIG_NET_CONNECT_CONNECT_IPV4 + xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); + vSemaphoreDelete(s_semph_get_ip_addrs); + s_semph_get_ip_addrs = NULL; +#endif +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); + vSemaphoreDelete(s_semph_get_ip6_addrs); + s_semph_get_ip6_addrs = NULL; +#endif + if (s_retry_num > CONFIG_NET_CONNECT_WIFI_CONN_MAX_RETRY) { + return ESP_FAIL; + } + } + return ESP_OK; +} + +esp_err_t net_connect_wifi_sta_do_disconnect(void) +{ + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect)); + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect)); +#if CONFIG_NET_CONNECT_CONNECT_IPV6 + ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6)); +#endif + return esp_wifi_disconnect(); +} + +void net_connect_wifi_shutdown(void) +{ + net_connect_wifi_sta_do_disconnect(); + net_connect_wifi_stop(); +} + +esp_err_t net_connect_wifi_connect(void) +{ + ESP_LOGI(TAG, "Start example_connect."); + net_connect_wifi_start(); + wifi_config_t wifi_config = { + .sta = { +#if !CONFIG_NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + .ssid = CONFIG_NET_CONNECT_WIFI_SSID, + .password = CONFIG_NET_CONNECT_WIFI_PASSWORD, +#endif + .scan_method = NET_CONNECT_WIFI_SCAN_METHOD, + .sort_method = NET_CONNECT_WIFI_CONNECT_AP_SORT_METHOD, + .threshold.rssi = CONFIG_NET_CONNECT_WIFI_SCAN_RSSI_THRESHOLD, + .threshold.authmode = NET_CONNECT_WIFI_SCAN_AUTH_MODE_THRESHOLD, + }, + }; +#if CONFIG_NET_CONNECT_WIFI_SSID_PWD_FROM_STDIN + net_configure_stdin_stdout(); + char buf[sizeof(wifi_config.sta.ssid) + sizeof(wifi_config.sta.password) + 2] = {0}; + ESP_LOGI(TAG, "Please input ssid password:"); + fgets(buf, sizeof(buf), stdin); + int len = strlen(buf); + buf[len - 1] = '\0'; /* removes '\n' */ + memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid)); + + char *rest = NULL; + char *temp = strtok_r(buf, " ", &rest); + strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid)); + memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password)); + temp = strtok_r(NULL, " ", &rest); + if (temp) { + strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password)); + } else { + wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; + } +#endif + return net_connect_wifi_sta_do_connect(wifi_config, true); +} + + +#endif /* CONFIG_NET_CONNECT_CONNECT_WIFI */ diff --git a/examples/esp_netif/multiple_netifs/main/check_connection.c b/examples/esp_netif/multiple_netifs/main/check_connection.c index 64c2e21cac..a020b9dd9b 100644 --- a/examples/esp_netif/multiple_netifs/main/check_connection.c +++ b/examples/esp_netif/multiple_netifs/main/check_connection.c @@ -83,10 +83,10 @@ esp_err_t check_connectivity(const char *host) return ESP_ERR_NOT_FOUND; } if (res->ai_family == AF_INET) { - struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); } else { - struct in6_addr addr6 = ((struct sockaddr_in6 *) (res->ai_addr))->sin6_addr; + struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr; inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6); } freeaddrinfo(res); diff --git a/examples/esp_netif/slip_custom_netif/main/slip_client_main.c b/examples/esp_netif/slip_custom_netif/main/slip_client_main.c index 56c2a87ce1..010a3309d3 100644 --- a/examples/esp_netif/slip_custom_netif/main/slip_client_main.c +++ b/examples/esp_netif/slip_custom_netif/main/slip_client_main.c @@ -169,7 +169,7 @@ static bool slip_rx_filter(slip_modem_handle slip, uint8_t *data, uint32_t len) #if CONFIG_EXAMPLE_IPV4 static const esp_netif_ip_info_t s_slip_ip4 = { - .ip = { .addr = ESP_IP4TOADDR( 10, 0, 0, 2) }, + .ip = { .addr = ESP_IP4TOADDR(10, 0, 0, 2) }, }; #endif