diff --git a/examples/protocols/sockets_non_blocking/tcp_client/CMakeLists.txt b/examples/protocols/sockets/non_blocking/CMakeLists.txt similarity index 93% rename from examples/protocols/sockets_non_blocking/tcp_client/CMakeLists.txt rename to examples/protocols/sockets/non_blocking/CMakeLists.txt index aee70cad5e..ac5ad9362a 100644 --- a/examples/protocols/sockets_non_blocking/tcp_client/CMakeLists.txt +++ b/examples/protocols/sockets/non_blocking/CMakeLists.txt @@ -7,4 +7,4 @@ cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(tcp_client) +project(non_blocking_socket) diff --git a/examples/protocols/sockets_non_blocking/tcp_client/Makefile b/examples/protocols/sockets/non_blocking/Makefile similarity index 87% rename from examples/protocols/sockets_non_blocking/tcp_client/Makefile rename to examples/protocols/sockets/non_blocking/Makefile index 87f4bb92f7..d430b892f6 100644 --- a/examples/protocols/sockets_non_blocking/tcp_client/Makefile +++ b/examples/protocols/sockets/non_blocking/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := tcp_client +PROJECT_NAME := non_blocking_socket EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common diff --git a/examples/protocols/sockets/non_blocking/README.md b/examples/protocols/sockets/non_blocking/README.md new file mode 100644 index 0000000000..8dd55a3d75 --- /dev/null +++ b/examples/protocols/sockets/non_blocking/README.md @@ -0,0 +1,70 @@ + +# TCP non-blocking client and server examples + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +The application aims to demonstrate a simple use of TCP sockets in a nonblocking mode. +It could be configured to run either a TCP server, or a TCP client, or both, in the project configuration settings. + +## How to use example + +The example is configured by default as the TCP client. + +Note that the example uses string representation of IP addresses and ports and thus +could be used on both IPv4 and IPv6 protocols. + +### TCP Client + +In the client mode, the example connects to a configured hostname or address, sends the specified payload data and waits for a response, +then closes the connection. By default, it connects to a public http server and performs a simple http `GET` request. + +### TCP Server + +The server example creates a non-blocking TCP socket with the specified port number and polls till +a connection request from the client arrives. +After accepting a request from the client, a connection between server and client is +established, and the application polls for some data to be received from the client. +Received data are printed as ASCII text and retransmitted back to the client. + +The server could listen on the specified interface (by the configured bound address) and serves multiple clients. +It resumes to listening for new connections when the client's socket gets closed. + +## Hardware Required + +This example can be run on any commonly available ESP32 development board. + +## Configure the project + +``` +idf.py menuconfig +``` + +Set following parameters under Example Configuration Options: + +* Set `EXAMPLE_TCP_SERVER` to use the example as a non-blocking TCP server + * Configure `EXAMPLE_TCP_SERVER_BIND_ADDRESS` to a string representation of the address to bind the server socket to. + * Configure `EXAMPLE_TCP_SERVER_BIND_PORT` to the port number. + +* Set `EXAMPLE_TCP_CLIENT` to use the example as a non-blocking TCP client + * Configure `EXAMPLE_TCP_CLIENT_CONNECT_ADDRESS` to a string representation of the address to connect the client to. + * Configure `EXAMPLE_TCP_CLIENT_CONNECT_PORT` to the port number. + +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. + +## Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + + +## Troubleshooting + +Follow the same troubleshooting instruction as for the standard [TCP sever](../tcp_server/README.md) and the [TCP client](../tcp_client/README.md), +using the host tools and scripts as descibed in the upper level documentation on [BSD socket examples](../README.md). diff --git a/examples/protocols/sockets/non_blocking/example_test.py b/examples/protocols/sockets/non_blocking/example_test.py new file mode 100644 index 0000000000..ecd9522a85 --- /dev/null +++ b/examples/protocols/sockets/non_blocking/example_test.py @@ -0,0 +1,26 @@ +# This example code is in the Public Domain (or CC0 licensed, at your option.) + +# Unless required by applicable law or agreed to in writing, this +# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +# CONDITIONS OF ANY KIND, either express or implied. + +# -*- coding: utf-8 -*- + +from __future__ import print_function, unicode_literals + +import re + +import ttfw_idf + + +@ttfw_idf.idf_example_test(env_tag='Example_GENERIC') +def test_examples_protocol_socket_non_block(env, _): + dut = env.get_dut('non_blocking_socket', 'examples/protocols/sockets/non_blocking', dut_class=ttfw_idf.ESP32DUT) + + # start the test and expect the client to receive back it's original data + dut.start_app() + dut.expect(re.compile(r'nonblocking-socket-client: Received: GET / HTTP/1.1'), timeout=30) + + +if __name__ == '__main__': + test_examples_protocol_socket_non_block() diff --git a/examples/protocols/sockets/non_blocking/main/CMakeLists.txt b/examples/protocols/sockets/non_blocking/main/CMakeLists.txt new file mode 100644 index 0000000000..95920dc0dd --- /dev/null +++ b/examples/protocols/sockets/non_blocking/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "non_blocking_socket_example.c" + INCLUDE_DIRS ".") diff --git a/examples/protocols/sockets/non_blocking/main/Kconfig.projbuild b/examples/protocols/sockets/non_blocking/main/Kconfig.projbuild new file mode 100644 index 0000000000..3c698c3831 --- /dev/null +++ b/examples/protocols/sockets/non_blocking/main/Kconfig.projbuild @@ -0,0 +1,48 @@ +menu "Example Configuration" + + config EXAMPLE_TCP_SERVER + bool "TCP server" + default n + help + This example will setup a tcp server, binds it to the specified address + and starts listening + + if EXAMPLE_TCP_SERVER + config EXAMPLE_TCP_SERVER_BIND_ADDRESS + string "Server bind address" + default "0.0.0.0" + help + Server listener's socket would be bound to this address. This address could be + either IPv4 or IPv6 address + + config EXAMPLE_TCP_SERVER_BIND_PORT + string "Server bind port" + default "3344" + help + Server listener's socket would be bound to this port. + + endif + + config EXAMPLE_TCP_CLIENT + bool "TCP client" + default y + help + This example will setup a tcp client, connects to the specified address + and sends the data. + + if EXAMPLE_TCP_CLIENT + config EXAMPLE_TCP_CLIENT_CONNECT_ADDRESS + string "Client connection address or hostname" + default "www.google.com" + help + Client's socket would connect to this address/host. + + config EXAMPLE_TCP_CLIENT_CONNECT_PORT + string "Client connection port" + default "80" + help + Client connection port. + + endif + +endmenu diff --git a/examples/protocols/sockets_non_blocking/tcp_client/main/component.mk b/examples/protocols/sockets/non_blocking/main/component.mk similarity index 100% rename from examples/protocols/sockets_non_blocking/tcp_client/main/component.mk rename to examples/protocols/sockets/non_blocking/main/component.mk diff --git a/examples/protocols/sockets/non_blocking/main/non_blocking_socket_example.c b/examples/protocols/sockets/non_blocking/main/non_blocking_socket_example.c new file mode 100644 index 0000000000..d429312676 --- /dev/null +++ b/examples/protocols/sockets/non_blocking/main/non_blocking_socket_example.c @@ -0,0 +1,401 @@ +/* BSD non-blocking socket example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "sys/socket.h" +#include "netdb.h" +#include "errno.h" +#include "esp_system.h" +#include "esp_event.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "protocol_examples_common.h" + +/** + * @brief Indicates that the file descriptor represents an invalid (uninitialized or closed) socket + * + * Used in the TCP server structure `sock[]` which holds list of active clients we serve. + */ +#define INVALID_SOCK (-1) + +/** + * @brief Time in ms to yield to all tasks when a non-blocking socket would block + * + * Non-blocking socket operations are typically executed in a separate task validating + * the socket status. Whenever the socket returns `EAGAIN` (idle status, i.e. would block) + * we have to yield to all tasks to prevent lower priority tasks from starving. + */ +#define YIELD_TO_ALL_MS 50 + +/** + * @brief Utility to log socket errors + * + * @param[in] tag Logging tag + * @param[in] sock Socket number + * @param[in] err Socket errno + * @param[in] message Message to print + */ +static void log_socket_error(const char *tag, const int sock, const int err, const char *message) +{ + ESP_LOGE(tag, "[sock=%d]: %s\n" + "error=%d: %s", sock, message, err, strerror(err)); +} + +/** + * @brief Tries to receive data from specified sockets in a non-blocking way, + * i.e. returns immediately if no data. + * + * @param[in] tag Logging tag + * @param[in] sock Socket for reception + * @param[out] data Data pointer to write the received data + * @param[in] max_len Maximum size of the allocated space for receiving data + * @return + * >0 : Size of received data + * =0 : No data available + * -1 : Error occurred during socket read operation + * -2 : Socket is not connected, to distinguish between an actual socket error and active disconnection + */ +static int try_receive(const char *tag, const int sock, char * data, size_t max_len) +{ + int len = recv(sock, data, max_len, 0); + if (len < 0) { + if (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK) { + return 0; // Not an error + } + if (errno == ENOTCONN) { + ESP_LOGW(tag, "[sock=%d]: Connection closed", sock); + return -2; // Socket has been disconnected + } + log_socket_error(tag, sock, errno, "Error occurred during receiving"); + return -1; + } + + return len; +} + +/** + * @brief Sends the specified data to the socket. This function blocks until all bytes got sent. + * + * @param[in] tag Logging tag + * @param[in] sock Socket to write data + * @param[in] data Data to be written + * @param[in] len Length of the data + * @return + * >0 : Size the written data + * -1 : Error occurred during socket write operation + */ +static int socket_send(const char *tag, const int sock, const char * data, const size_t len) +{ + int to_write = len; + while (to_write > 0) { + int written = send(sock, data + (len - to_write), to_write, 0); + if (written < 0 && errno != EINPROGRESS && errno != EAGAIN && errno != EWOULDBLOCK) { + log_socket_error(tag, sock, errno, "Error occurred during sending"); + return -1; + } + to_write -= written; + } + return len; +} + + +#ifdef CONFIG_EXAMPLE_TCP_CLIENT + +static void tcp_client_task(void *pvParameters) +{ + static const char *TAG = "nonblocking-socket-client"; + static const char *payload = "GET / HTTP/1.1\r\n\r\n"; + static char rx_buffer[128]; + + struct addrinfo hints = { .ai_socktype = SOCK_STREAM }; + struct addrinfo *address_info; + int sock = INVALID_SOCK; + + int res = getaddrinfo(CONFIG_EXAMPLE_TCP_CLIENT_CONNECT_ADDRESS, CONFIG_EXAMPLE_TCP_CLIENT_CONNECT_PORT, &hints, &address_info); + if (res != 0 || address_info == NULL) { + ESP_LOGE(TAG, "couldn't get hostname for `%s` " + "getaddrinfo() returns %d, addrinfo=%p", CONFIG_EXAMPLE_TCP_CLIENT_CONNECT_ADDRESS, res, address_info); + goto error; + } + + // Creating client's socket + sock = socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol); + if (sock < 0) { + log_socket_error(TAG, sock, errno, "Unable to create socket"); + goto error; + } + ESP_LOGI(TAG, "Socket created, connecting to %s:%s", CONFIG_EXAMPLE_TCP_CLIENT_CONNECT_ADDRESS, CONFIG_EXAMPLE_TCP_CLIENT_CONNECT_PORT); + + // Marking the socket as non-blocking + int flags = fcntl(sock, F_GETFL); + if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { + log_socket_error(TAG, sock, errno, "Unable to set socket non blocking"); + } + + if (connect(sock, address_info->ai_addr, address_info->ai_addrlen) != 0) { + if (errno == EINPROGRESS) { + ESP_LOGD(TAG, "connection in progress"); + fd_set fdset; + FD_ZERO(&fdset); + FD_SET(sock, &fdset); + + // Connection in progress -> have to wait until the connecting socket is marked as writable, i.e. connection completes + res = select(sock+1, NULL, &fdset, NULL, NULL); + if (res < 0) { + log_socket_error(TAG, sock, errno, "Error during connection: select for socket to be writable"); + goto error; + } else if (res == 0) { + log_socket_error(TAG, sock, errno, "Connection timeout: select for socket to be writable"); + goto error; + } else { + int sockerr; + socklen_t len = (socklen_t)sizeof(int); + + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&sockerr), &len) < 0) { + log_socket_error(TAG, sock, errno, "Error when getting socket error using getsockopt()"); + goto error; + } + if (sockerr) { + log_socket_error(TAG, sock, sockerr, "Connection error"); + goto error; + } + } + } else { + log_socket_error(TAG, sock, errno, "Socket is unable to connect"); + goto error; + } + } + + ESP_LOGI(TAG, "Client sends data to the server..."); + int len = socket_send(TAG, sock, payload, strlen(payload)); + if (len < 0) { + ESP_LOGE(TAG, "Error occurred during socket_send"); + goto error; + } + ESP_LOGI(TAG, "Written: %.*s", len, payload); + + // Keep receiving until we have a reply + do { + len = try_receive(TAG, sock, rx_buffer, sizeof(rx_buffer)); + if (len < 0) { + ESP_LOGE(TAG, "Error occurred during try_receive"); + goto error; + } + vTaskDelay(pdMS_TO_TICKS(YIELD_TO_ALL_MS)); + } while (len == 0); + ESP_LOGI(TAG, "Received: %.*s", len, rx_buffer); + +error: + if (sock != INVALID_SOCK) { + close(sock); + } + free(address_info); + vTaskDelete(NULL); + +} +#endif // CONFIG_EXAMPLE_TCP_CLIENT + + +#ifdef CONFIG_EXAMPLE_TCP_SERVER + +/** + * @brief Returns the string representation of client's address (accepted on this server) + */ +static inline char* get_clients_address(struct sockaddr_storage *source_addr) +{ + static char address_str[128]; + char *res = NULL; + // Convert ip address to string + if (source_addr->ss_family == PF_INET) { + res = inet_ntoa_r(((struct sockaddr_in *)source_addr)->sin_addr, address_str, sizeof(address_str) - 1); + } +#ifdef CONFIG_LWIP_IPV6 + else if (source_addr->ss_family == PF_INET6) { + res = inet6_ntoa_r(((struct sockaddr_in6 *)source_addr)->sin6_addr, address_str, sizeof(address_str) - 1); + } +#endif + if (!res) { + address_str[0] = '\0'; // Returns empty string if conversion didn't succeed + } + return address_str; +} + +static void tcp_server_task(void *pvParameters) +{ + static char rx_buffer[128]; + static const char *TAG = "nonblocking-socket-server"; + xSemaphoreHandle *server_ready = pvParameters; + struct addrinfo hints = { .ai_socktype = SOCK_STREAM }; + struct addrinfo *address_info; + int listen_sock = INVALID_SOCK; + const size_t max_socks = CONFIG_LWIP_MAX_SOCKETS - 1; + static int sock[CONFIG_LWIP_MAX_SOCKETS - 1]; + + // Prepare a list of file descriptors to hold client's sockets, mark all of them as invalid, i.e. available + for (int i=0; iai_family, address_info->ai_socktype, address_info->ai_protocol); + + if (listen_sock < 0) { + log_socket_error(TAG, listen_sock, errno, "Unable to create socket"); + goto error; + } + ESP_LOGI(TAG, "Listener socket created"); + + // Marking the socket as non-blocking + int flags = fcntl(listen_sock, F_GETFL); + if (fcntl(listen_sock, F_SETFL, flags | O_NONBLOCK) == -1) { + log_socket_error(TAG, listen_sock, errno, "Unable to set socket non blocking"); + goto error; + } + ESP_LOGI(TAG, "Socket marked as non blocking"); + + // Binding socket to the given address + int err = bind(listen_sock, address_info->ai_addr, address_info->ai_addrlen); + if (err != 0) { + log_socket_error(TAG, listen_sock, errno, "Socket unable to bind"); + goto error; + } + ESP_LOGI(TAG, "Socket bound on %s:%s", CONFIG_EXAMPLE_TCP_SERVER_BIND_ADDRESS, CONFIG_EXAMPLE_TCP_SERVER_BIND_PORT); + + // Set queue (backlog) of pending connections to one (can be more) + err = listen(listen_sock, 1); + if (err != 0) { + log_socket_error(TAG, listen_sock, errno, "Error occurred during listen"); + goto error; + } + ESP_LOGI(TAG, "Socket listening"); + xSemaphoreGive(*server_ready); + + // Main loop for accepting new connections and serving all connected clients + while (1) { + struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 + socklen_t addr_len = sizeof(source_addr); + + // Find a free socket + int new_sock_index = 0; + for (new_sock_index=0; new_sock_index print it's address + ESP_LOGI(TAG, "[sock=%d]: Connection accepted from IP:%s", sock[new_sock_index], get_clients_address(&source_addr)); + + // ...and set the client's socket non-blocking + flags = fcntl(sock[new_sock_index], F_GETFL); + if (fcntl(sock[new_sock_index], F_SETFL, flags | O_NONBLOCK) == -1) { + log_socket_error(TAG, sock[new_sock_index], errno, "Unable to set socket non blocking"); + goto error; + } + ESP_LOGI(TAG, "[sock=%d]: Socket marked as non blocking", sock[new_sock_index]); + } + } + + // We serve all the connected clients in this loop + for (int i=0; i try to serve it + int len = try_receive(TAG, sock[i], rx_buffer, sizeof(rx_buffer)); + if (len < 0) { + // Error occurred within this client's socket -> close and mark invalid + ESP_LOGI(TAG, "[sock=%d]: try_receive() returned %d -> closing the socket", sock[i], len); + close(sock[i]); + sock[i] = INVALID_SOCK; + } else if (len > 0) { + // Received some data -> echo back + ESP_LOGI(TAG, "[sock=%d]: Received %.*s", sock[i], len, rx_buffer); + + len = socket_send(TAG, sock[i], rx_buffer, len); + if (len < 0) { + // Error occurred on write to this socket -> close it and mark invalid + ESP_LOGI(TAG, "[sock=%d]: socket_send() returned %d -> closing the socket", sock[i], len); + close(sock[i]); + sock[i] = INVALID_SOCK; + } else { + // Successfully echoed to this socket + ESP_LOGI(TAG, "[sock=%d]: Written %.*s", sock[i], len, rx_buffer); + } + } + + } // one client's socket + } // for all sockets + + // Yield to other tasks + vTaskDelay(pdMS_TO_TICKS(YIELD_TO_ALL_MS)); + } + +error: + if (listen_sock != INVALID_SOCK) { + close(listen_sock); + } + + for (int i=0; i - fam_addr = socket.AF_INET6 if sys.argv[1] == 'IPv6' else socket.AF_INET - with TcpServer(PORT, fam_addr, persist=True) as s: - print(input('Press Enter stop the server...')) - else: - test_examples_protocol_socket_tcpclient() diff --git a/examples/protocols/sockets_non_blocking/tcp_client/main/CMakeLists.txt b/examples/protocols/sockets_non_blocking/tcp_client/main/CMakeLists.txt deleted file mode 100644 index 9ff632b1e0..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_client/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "tcp_client.c" - INCLUDE_DIRS ".") diff --git a/examples/protocols/sockets_non_blocking/tcp_client/main/Kconfig.projbuild b/examples/protocols/sockets_non_blocking/tcp_client/main/Kconfig.projbuild deleted file mode 100644 index 765866b523..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_client/main/Kconfig.projbuild +++ /dev/null @@ -1,51 +0,0 @@ -menu "Example Configuration" - - choice EXAMPLE_IP_MODE - prompt "IP Version" - depends on EXAMPLE_SOCKET_IP_INPUT_STRING - help - Example can use either IPV4 or IPV6. - - config EXAMPLE_IPV4 - bool "IPV4" - - config EXAMPLE_IPV6 - bool "IPV6" - - endchoice - - config EXAMPLE_IPV4_ADDR - string "IPV4 Address" - default "192.168.0.165" - depends on EXAMPLE_IPV4 - help - The example will connect to this IPV4 address. - - config EXAMPLE_IPV6_ADDR - string "IPV6 Address" - default "FE80::30AD:E57B:C212:68AD" - depends on EXAMPLE_IPV6 - help - The example will connect to this IPV6 address. - - config EXAMPLE_PORT - int "Port" - range 0 65535 - default 3355 - help - The remote port to which the client example will connect to. - - choice EXAMPLE_SOCKET_IP_INPUT - prompt "Socket example source" - default EXAMPLE_SOCKET_IP_INPUT_STRING - help - Selects the input source of the IP used in the example. - - config EXAMPLE_SOCKET_IP_INPUT_STRING - bool "From string" - - config EXAMPLE_SOCKET_IP_INPUT_STDIN - bool "From stdin" - endchoice - -endmenu diff --git a/examples/protocols/sockets_non_blocking/tcp_client/main/tcp_client.c b/examples/protocols/sockets_non_blocking/tcp_client/main/tcp_client.c deleted file mode 100644 index 26d574cccd..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_client/main/tcp_client.c +++ /dev/null @@ -1,154 +0,0 @@ -/* BSD Socket API Example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "esp_netif.h" -#include "protocol_examples_common.h" -#include "addr_from_stdin.h" -#include "lwip/err.h" -#include "lwip/sockets.h" - - -#if defined(CONFIG_EXAMPLE_IPV4) -#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR -#elif defined(CONFIG_EXAMPLE_IPV6) -#define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR -#else -#define HOST_IP_ADDR "" -#endif - -#define PORT CONFIG_EXAMPLE_PORT -#define TIMEOUT 10 - -static const char *TAG = "example"; -static const char *payload = "Message from ESP32 "; - -static void tcp_client_task(void *pvParameters) -{ - char rx_buffer[128]; - char host_ip[] = HOST_IP_ADDR; - int addr_family = 0; - int ip_protocol = 0; - int flags; - fd_set wrfds; - - FD_ZERO(&wrfds); - while (1) { -#if defined(CONFIG_EXAMPLE_IPV4) - struct sockaddr_in dest_addr; - dest_addr.sin_addr.s_addr = inet_addr(host_ip); - dest_addr.sin_family = AF_INET; - dest_addr.sin_port = htons(PORT); - addr_family = AF_INET; - ip_protocol = IPPROTO_IP; -#elif defined(CONFIG_EXAMPLE_IPV6) - struct sockaddr_in6 dest_addr = { 0 }; - inet6_aton(host_ip, &dest_addr.sin6_addr); - dest_addr.sin6_family = AF_INET6; - dest_addr.sin6_port = htons(PORT); - dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); - addr_family = AF_INET6; - ip_protocol = IPPROTO_IPV6; -#elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN) - struct sockaddr_storage dest_addr = { 0 }; - ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr)); -#endif - - // Creating a client socket - int sock = socket(addr_family, SOCK_STREAM, ip_protocol); - - if (sock < 0) { - ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); - break; - } - ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT); - - // Getting file descriptor flags - flags = fcntl(sock, F_GETFL); - - // Marking the socket as non-blocking - if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { - ESP_LOGE(TAG, "Unable to set socket non blocking: errno %d", errno); - } - if (connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6)) == -1) { - if (errno == EINPROGRESS) { - ESP_LOGI(TAG, "connect EINPROGRESS OK"); - FD_SET(sock, &wrfds); - } else { - ESP_LOGE(TAG, "Socket unable to connect: error %s", strerror(errno)); - } - } else { - ESP_LOGI(TAG, "%s successfully connected on port %d", host_ip, PORT); - } - - // Setting socket back to blocking mode for read/write operations - flags = fcntl(sock, F_GETFL); - - if (fcntl(sock, F_SETFL, flags ^ O_NONBLOCK) == -1) { - ESP_LOGE(TAG, "Unable to set socket non blocking: errno %d", errno); - } - - vTaskDelay(1000 / portTICK_PERIOD_MS); - - while (1) { - int err = send(sock, payload, strlen(payload), 0); - - if(err == -1) { - ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); - vTaskDelay(1000 / portTICK_PERIOD_MS); - continue; - } - - int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0); - - if (len == -1 ) { - ESP_LOGE(TAG, "recv failed, err msg : %s", strerror(errno)); - break; - } - // Data received - else { - rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string - ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip); - ESP_LOGI(TAG, "%s", rx_buffer); - break; - } - - vTaskDelay(1000 / portTICK_PERIOD_MS); - } - if (sock != -1) { - ESP_LOGE(TAG, "Shutting down socket and restarting..."); - shutdown(sock, 0); - close(sock); - } - } - vTaskDelete(NULL); -} - -void app_main(void) -{ - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. - * Read "Establishing Wi-Fi or Ethernet Connection" section in - * examples/protocols/README.md for more information about this function. - */ - ESP_ERROR_CHECK(example_connect()); - - xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL); -} diff --git a/examples/protocols/sockets_non_blocking/tcp_client/sdkconfig.ci b/examples/protocols/sockets_non_blocking/tcp_client/sdkconfig.ci deleted file mode 100644 index 2484db7abb..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_client/sdkconfig.ci +++ /dev/null @@ -1 +0,0 @@ -CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN=y diff --git a/examples/protocols/sockets_non_blocking/tcp_server/CMakeLists.txt b/examples/protocols/sockets_non_blocking/tcp_server/CMakeLists.txt deleted file mode 100644 index 5761720556..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(EXCLUDE_COMPONENTS "coap" "asio") - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(tcp_server) diff --git a/examples/protocols/sockets_non_blocking/tcp_server/Makefile b/examples/protocols/sockets_non_blocking/tcp_server/Makefile deleted file mode 100644 index 344b8aa424..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := tcp_server - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common -EXCLUDE_COMPONENTS = coap asio - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets_non_blocking/tcp_server/README.md b/examples/protocols/sockets_non_blocking/tcp_server/README.md deleted file mode 100644 index 68b5183709..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/README.md +++ /dev/null @@ -1,72 +0,0 @@ - -# TCP Server example - -(See the README.md file in the upper level 'examples' directory for more information about examples.) - -The application creates a non-blocking TCP socket with the specified port number and polls till a connection request from the client arrives. After accepting a request from the client, a connection between server and client is established and the application polls for some data to be received from the client. Received data are printed as ASCII text and retransmitted back to the client. - -## How to use example - -In order to create TCP client that communicates with TCP server example, choose one of the following options. - -There are many host-side tools which can be used to interact with the UDP/TCP server/client. -One command line tool is [netcat](http://netcat.sourceforge.net) which can send and receive many kinds of packets. -Note: please replace `192.168.0.167 3333` with desired IPV4/IPV6 address (displayed in monitor console) and port number in the following command. - -In addition to those tools, simple Python scripts can be found under sockets/scripts directory. Every script is designed to interact with one of the examples. - -### TCP client using netcat -``` -nc 192.168.0.167 3333 -``` - -### Python scripts -Script example_test.py could be used as a counter part to the tcp-server application, -IP address and the message to be send to the server shall be stated as arguments. Example: - -``` -python example_test.py 192.168.0.167 Message -``` -Note that this script is used in automated tests, as well, so the IDF test framework packages need to be imported; -please add `$IDF_PATH/tools/ci/python_packages` to `PYTHONPATH`. - -## Hardware Required - -This example can be run on any commonly available ESP32 development board. - -## Configure the project - -``` -idf.py menuconfig -``` - -Set following parameters under Example Configuration Options: - -* Set `IP version` of the example to be IPV4 or IPV6. - -* Set `Port` number of the socket, that server example will create. - -* Set `TCP keep-alive idle time(s)` value of TCP keep alive idle time. This time is the time between the last data transmission. - -* Set `TCP keep-alive interval time(s)` value of TCP keep alive interval time. This time is the interval time of keepalive probe packets. - -* Set `TCP keep-alive packet retry send counts` value of TCP keep alive packet retry send counts. This is the number of retries of the keepalive probe packet. - -Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. - -## Build and Flash - -Build the project and flash it to the board, then run monitor tool to view serial output: - -``` -idf.py -p PORT flash monitor -``` - -(To exit the serial monitor, type ``Ctrl-]``.) - -See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. - - -## Troubleshooting - -Start server first, to receive data sent from the client (application). diff --git a/examples/protocols/sockets_non_blocking/tcp_server/example_test.py b/examples/protocols/sockets_non_blocking/tcp_server/example_test.py deleted file mode 100644 index 860a0c41e2..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/example_test.py +++ /dev/null @@ -1,89 +0,0 @@ -# This example code is in the Public Domain (or CC0 licensed, at your option.) - -# Unless required by applicable law or agreed to in writing, this -# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -# CONDITIONS OF ANY KIND, either express or implied. - -# -*- coding: utf-8 -*- - -from __future__ import print_function, unicode_literals - -import os -import re -import socket -import sys - -import ttfw_idf - -# ----------- Config ---------- -PORT = 3344 -INTERFACE = 'eth0' -# ------------------------------- - - -def tcp_client(address, payload): - for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC, - socket.SOCK_STREAM, 0, socket.AI_PASSIVE): - family_addr, socktype, proto, canonname, addr = res - try: - sock = socket.socket(family_addr, socket.SOCK_STREAM) - sock.settimeout(60.0) - except socket.error as msg: - print('Could not create socket: ' + msg) - raise - try: - sock.connect(addr) - except socket.error as msg: - print('Could not open socket: ', msg) - sock.close() - raise - sock.sendall(payload.encode()) - data = sock.recv(1024) - if not data: - return '' - print('Reply : ' + data.decode()) - sock.close() - return data.decode() - - -@ttfw_idf.idf_example_test(env_tag='Example_WIFI') -def test_examples_protocol_socket_tcpserver(env, extra_data): - """ - steps: - 1. join AP - 2. have the board connect to the server - 3. send and receive data - """ - msg = 'Data to ESP' - dut1 = env.get_dut('tcp_client', 'examples/protocols/sockets_non_blocking/tcp_server', dut_class=ttfw_idf.ESP32DUT) - # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'tcp_server.bin') - bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('tcp_server_bin_size', '{}KB'.format(bin_size // 1024)) - - # start test - dut1.start_app() - ipv4 = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)[0] - ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form) - ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0] - print('Connected with IPv4={} and IPv6={}'.format(ipv4, ipv6)) - - # test IPv4 - received = tcp_client(ipv4, msg) - if received != msg: - raise - dut1.expect(msg) - - # test IPv6 - received = tcp_client('{}%{}'.format(ipv6, INTERFACE), msg) - if received != msg: - raise - dut1.expect(msg) - - -if __name__ == '__main__': - if sys.argv[2:]: # if two arguments provided: - # Usage: example_test.py - tcp_client(sys.argv[1], sys.argv[2]) - else: # otherwise run standard example test as in the CI - test_examples_protocol_socket_tcpserver() diff --git a/examples/protocols/sockets_non_blocking/tcp_server/main/CMakeLists.txt b/examples/protocols/sockets_non_blocking/tcp_server/main/CMakeLists.txt deleted file mode 100644 index c8a8d16a40..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "tcp_server.c" - INCLUDE_DIRS ".") diff --git a/examples/protocols/sockets_non_blocking/tcp_server/main/Kconfig.projbuild b/examples/protocols/sockets_non_blocking/tcp_server/main/Kconfig.projbuild deleted file mode 100644 index 8d8949cc50..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/main/Kconfig.projbuild +++ /dev/null @@ -1,36 +0,0 @@ -menu "Example Configuration" - - config EXAMPLE_IPV4 - bool "IPV4" - default y - - config EXAMPLE_IPV6 - bool "IPV6" - default n - select EXAMPLE_CONNECT_IPV6 - - config EXAMPLE_PORT - int "Port" - range 0 65535 - default 3344 - help - Local port the example server will listen on. - - config EXAMPLE_KEEPALIVE_IDLE - int "TCP keep-alive idle time(s)" - default 5 - help - Keep-alive idle time. In idle time without receiving any data from peer, will send keep-alive probe packet - - config EXAMPLE_KEEPALIVE_INTERVAL - int "TCP keep-alive interval time(s)" - default 5 - help - Keep-alive probe packet interval time. - - config EXAMPLE_KEEPALIVE_COUNT - int "TCP keep-alive packet retry send counts" - default 3 - help - Keep-alive probe packet retry count. -endmenu diff --git a/examples/protocols/sockets_non_blocking/tcp_server/main/component.mk b/examples/protocols/sockets_non_blocking/tcp_server/main/component.mk deleted file mode 100644 index a98f634eae..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/main/component.mk +++ /dev/null @@ -1,4 +0,0 @@ -# -# "main" pseudo-component makefile. -# -# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/examples/protocols/sockets_non_blocking/tcp_server/main/tcp_server.c b/examples/protocols/sockets_non_blocking/tcp_server/main/tcp_server.c deleted file mode 100644 index 1137133935..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/main/tcp_server.c +++ /dev/null @@ -1,181 +0,0 @@ -/* BSD Socket API Example - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "esp_netif.h" -#include "protocol_examples_common.h" - -#include "lwip/err.h" -#include "lwip/sockets.h" -#include "lwip/sys.h" -#include -#include - - -#define PORT CONFIG_EXAMPLE_PORT -#define KEEPALIVE_IDLE CONFIG_EXAMPLE_KEEPALIVE_IDLE -#define KEEPALIVE_INTERVAL CONFIG_EXAMPLE_KEEPALIVE_INTERVAL -#define KEEPALIVE_COUNT CONFIG_EXAMPLE_KEEPALIVE_COUNT - -static const char *TAG = "example"; - -static void do_retransmit(const int sock) -{ - int len; - char rx_buffer[128]; - - do { - ESP_LOGI(TAG, "Waiting for receiving data"); - len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0); - if (len < 0) { - ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno); - } else if (len == 0) { - ESP_LOGW(TAG, "Connection closed"); - } else { - rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string - ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer); - - // send() can return less bytes than supplied length. - // Walk-around for robust implementation. - int to_write = len; - while (to_write > 0) { - int written = send(sock, rx_buffer + (len - to_write), to_write, 0); - if (written < 0) { - ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); - break; - } - to_write -= written; - } - } - } while (len > 0); -} - -static void tcp_server_task(void *pvParameters) -{ - char addr_str[128]; - int addr_family = (int)pvParameters; - int ip_protocol = 0; - struct sockaddr_storage dest_addr; - - if (addr_family == AF_INET) { - struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr; - dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY); - dest_addr_ip4->sin_family = AF_INET; - dest_addr_ip4->sin_port = htons(PORT); - ip_protocol = IPPROTO_IP; - } -#ifdef CONFIG_EXAMPLE_IPV6 - else if (addr_family == AF_INET6) { - struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&dest_addr; - bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un)); - dest_addr_ip6->sin6_family = AF_INET6; - dest_addr_ip6->sin6_port = htons(PORT); - ip_protocol = IPPROTO_IPV6; - } -#endif - - // Creating a listener socket - int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol); - - if (listen_sock < 0) { - ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); - vTaskDelete(NULL); - return; - } - ESP_LOGI(TAG, "Listenet socket created"); - - // Geting socket file descriptor flags - int flags = fcntl(listen_sock, F_GETFL); - - // Marking the socket as non-blocking - if (fcntl(listen_sock, F_SETFL, flags | O_NONBLOCK) == -1) { - ESP_LOGE(TAG, "Unable to set socket non blocking: errno %d", errno); - } - ESP_LOGI(TAG, "Socket marked as non blocking"); - - // Binding socket to the given interface - int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); - - if (err != 0) { - ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); - ESP_LOGE(TAG, "IPPROTO: %d", addr_family); - goto CLEAN_UP; - } - ESP_LOGI(TAG, "Socket bound on port %d", PORT); - - // Set queue of pending connections to one(can be more) - err = listen(listen_sock, 1); - - if (err != 0) { - ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno); - goto CLEAN_UP; - } - - while (1) { - ESP_LOGI(TAG, "Socket listening"); - struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 - socklen_t addr_len = sizeof(source_addr); - - // Start listening for incoming connections in non-blocking mode - int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len); - - if (sock < 0) { - if (errno == EWOULDBLOCK) { // This is what we expect(Continue looping) - ESP_LOGD(TAG, "No pending connections; sleeping for one second.\n"); - continue; - } else { - ESP_LOGE(TAG, "Error when accepting connection: error %s", strerror(errno)); - break; - } - } - - // Convert ip address to string - if (source_addr.ss_family == PF_INET) { - inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1); - } -#ifdef CONFIG_EXAMPLE_IPV6 - else if (source_addr.ss_family == PF_INET6) { - inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1); - } -#endif - ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str); - do_retransmit(sock); - } - -CLEAN_UP: - close(listen_sock); - vTaskDelete(NULL); -} - -void app_main(void) -{ - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. - * Read "Establishing Wi-Fi or Ethernet Connection" section in - * examples/protocols/README.md for more information about this function. - */ - ESP_ERROR_CHECK(example_connect()); - -#ifdef CONFIG_EXAMPLE_IPV4 - xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL); -#endif -#ifdef CONFIG_EXAMPLE_IPV6 - xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET6, 5, NULL); -#endif -} diff --git a/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci b/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci deleted file mode 100644 index bea22a4eb0..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci +++ /dev/null @@ -1,2 +0,0 @@ -CONFIG_EXAMPLE_IPV4=y -CONFIG_EXAMPLE_IPV6=y diff --git a/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci.1 b/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci.1 deleted file mode 100644 index f4d66cf5ec..0000000000 --- a/examples/protocols/sockets_non_blocking/tcp_server/sdkconfig.ci.1 +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG_EXAMPLE_IPV4=y -CONFIG_EXAMPLE_IPV6=n -CONFIG_EXAMPLE_CONNECT_IPV6=n -CONFIG_LWIP_IPV6=n diff --git a/tools/ci/mypy_ignore_list.txt b/tools/ci/mypy_ignore_list.txt index dd6ee95559..e71d607044 100644 --- a/tools/ci/mypy_ignore_list.txt +++ b/tools/ci/mypy_ignore_list.txt @@ -97,12 +97,11 @@ examples/protocols/openssl_client/example_test.py examples/protocols/openssl_server/example_test.py examples/protocols/pppos_client/example_test.py examples/protocols/sntp/example_test.py +examples/protocols/sockets/non_blocking/example_test.py examples/protocols/sockets/tcp_client/example_test.py examples/protocols/sockets/tcp_server/example_test.py examples/protocols/sockets/udp_client/example_test.py examples/protocols/sockets/udp_server/example_test.py -examples/protocols/sockets_non_blocking/tcp_client/example_test.py -examples/protocols/sockets_non_blocking/tcp_server/example_test.py examples/protocols/websocket/example_test.py examples/provisioning/legacy/ble_prov/ble_prov_test.py examples/provisioning/legacy/custom_config/components/custom_provisioning/python/custom_config_pb2.py