From 3e7591e92a58a3471ad8450035e7e6302553352a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 00:44:31 +0800 Subject: [PATCH] examples/protocols/asio: use common network component * Original commit: espressif/esp-idf@aa4a7804ec5bd1f8a1da046dfe1fd7368ded2205 --- .../protocols/asio/chat_client/CMakeLists.txt | 4 + examples/protocols/asio/chat_client/Makefile | 2 + examples/protocols/asio/chat_client/README.md | 19 +-- .../chat_client/components/CMakeLists.txt | 5 - .../asio/chat_client/components/component.mk | 10 -- .../asio/chat_client/components/wifi_asio.cpp | 136 ------------------ .../asio/chat_client/main/Kconfig.projbuild | 22 +-- .../asio/chat_client/main/chat_client.cpp | 23 ++- .../protocols/asio/chat_server/CMakeLists.txt | 4 + examples/protocols/asio/chat_server/Makefile | 2 + examples/protocols/asio/chat_server/README.md | 19 +-- .../asio/chat_server/asio_chat_server_test.py | 2 +- .../chat_server/components/CMakeLists.txt | 5 - .../asio/chat_server/components/component.mk | 10 -- .../asio/chat_server/components/wifi_asio.cpp | 136 ------------------ .../asio/chat_server/main/Kconfig.projbuild | 18 +-- .../asio/chat_server/main/chat_server.cpp | 22 ++- .../asio/tcp_echo_server/CMakeLists.txt | 4 + .../protocols/asio/tcp_echo_server/Makefile | 2 + .../protocols/asio/tcp_echo_server/README.md | 19 +-- .../tcp_echo_server/asio_tcp_server_test.py | 4 +- .../tcp_echo_server/components/CMakeLists.txt | 5 - .../tcp_echo_server/components/component.mk | 10 -- .../tcp_echo_server/components/wifi_asio.cpp | 136 ------------------ .../tcp_echo_server/main/Kconfig.projbuild | 18 +-- .../asio/tcp_echo_server/main/echo_server.cpp | 20 ++- .../asio/udp_echo_server/CMakeLists.txt | 4 + .../protocols/asio/udp_echo_server/Makefile | 2 + .../protocols/asio/udp_echo_server/README.md | 17 +-- .../udp_echo_server/asio_udp_server_test.py | 4 +- .../udp_echo_server/components/CMakeLists.txt | 5 - .../udp_echo_server/components/component.mk | 10 -- .../udp_echo_server/components/wifi_asio.cpp | 136 ------------------ .../udp_echo_server/main/Kconfig.projbuild | 18 +-- .../udp_echo_server/main/udp_echo_server.cpp | 22 ++- .../protocols/asio/wifi_init/wifi_asio.cpp | 136 ------------------ 36 files changed, 161 insertions(+), 850 deletions(-) delete mode 100644 examples/protocols/asio/chat_client/components/CMakeLists.txt delete mode 100644 examples/protocols/asio/chat_client/components/component.mk delete mode 100644 examples/protocols/asio/chat_client/components/wifi_asio.cpp delete mode 100644 examples/protocols/asio/chat_server/components/CMakeLists.txt delete mode 100644 examples/protocols/asio/chat_server/components/component.mk delete mode 100644 examples/protocols/asio/chat_server/components/wifi_asio.cpp delete mode 100644 examples/protocols/asio/tcp_echo_server/components/CMakeLists.txt delete mode 100644 examples/protocols/asio/tcp_echo_server/components/component.mk delete mode 100644 examples/protocols/asio/tcp_echo_server/components/wifi_asio.cpp delete mode 100644 examples/protocols/asio/udp_echo_server/components/CMakeLists.txt delete mode 100644 examples/protocols/asio/udp_echo_server/components/component.mk delete mode 100644 examples/protocols/asio/udp_echo_server/components/wifi_asio.cpp delete mode 100644 examples/protocols/asio/wifi_init/wifi_asio.cpp diff --git a/examples/protocols/asio/chat_client/CMakeLists.txt b/examples/protocols/asio/chat_client/CMakeLists.txt index e0480c277..ddcc068ba 100644 --- a/examples/protocols/asio/chat_client/CMakeLists.txt +++ b/examples/protocols/asio/chat_client/CMakeLists.txt @@ -2,5 +2,9 @@ # 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) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(asio_chat_client) diff --git a/examples/protocols/asio/chat_client/Makefile b/examples/protocols/asio/chat_client/Makefile index 924891a67..90197676a 100644 --- a/examples/protocols/asio/chat_client/Makefile +++ b/examples/protocols/asio/chat_client/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := asio_chat_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/asio/chat_client/README.md b/examples/protocols/asio/chat_client/README.md index 0ab16f578..951afad53 100644 --- a/examples/protocols/asio/chat_client/README.md +++ b/examples/protocols/asio/chat_client/README.md @@ -1,19 +1,20 @@ -# ASIO chat server example +# Asio chat client example -Simple asio chat client using WiFi STA +Simple Asio chat client using WiFi STA or Ethernet. ## Example workflow -- WiFi STA is started and trying to connect to the access point defined through `make menuconfig` -- Once connected and acquired IP address ASIO chat client connects to a corresponding server whose port number and ip are defined through `make menuconfig` -- Chat client receives all messages from other chat clients, also it sends message received from stdin using `make monitor` +- Wi-Fi or Ethernet connection is established, and IP address is obtained. +- Asio chat client connects to the corresponding server whose port number and IP are defined through `make menuconfig`. +- Chat client receives all messages from other chat clients, also it sends message received from stdin using `make monitor`. ## Running the example -- Run `make menuconfig` to configure the access point's SSID and Password and server ip address and port number -- Start chat server either on host machine or as another ESP device running chat_server example -- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal -- Wait for WiFi to connect to your access point +- Run `make menuconfig` to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +- Set server IP address and port number in menuconfig, "Example configuration". +- Start chat server either on host machine or as another ESP device running chat_server example. +- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal. +- Wait for the board to connect to WiFi or Ethernet. - Receive and send messages to/from other clients on stdin/stdout via serial terminal. See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/examples/protocols/asio/chat_client/components/CMakeLists.txt b/examples/protocols/asio/chat_client/components/CMakeLists.txt deleted file mode 100644 index e51766724..000000000 --- a/examples/protocols/asio/chat_client/components/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(COMPONENT_SRCS "wifi_asio.cpp") - -set(COMPONENT_PRIV_REQUIRES asio nvs_flash console) - -register_component() diff --git a/examples/protocols/asio/chat_client/components/component.mk b/examples/protocols/asio/chat_client/components/component.mk deleted file mode 100644 index b23b0cb71..000000000 --- a/examples/protocols/asio/chat_client/components/component.mk +++ /dev/null @@ -1,10 +0,0 @@ -# -# Component Makefile -# -# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default, -# this will take the sources in the src/ directory, compile them and link them into -# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, -# please read the SDK documents if you need to do this. -# - -COMPONENT_SRCDIRS = . diff --git a/examples/protocols/asio/chat_client/components/wifi_asio.cpp b/examples/protocols/asio/chat_client/components/wifi_asio.cpp deleted file mode 100644 index b90ce26ae..000000000 --- a/examples/protocols/asio/chat_client/components/wifi_asio.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* Common WiFi Init as STA for ASIO examples - - 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 "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "driver/uart.h" -#include "esp_console.h" -#include "esp_vfs_dev.h" - -#include "lwip/err.h" -#include "lwip/sys.h" - -#include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "asio example wifi init"; - -/** - * Definition of ASIO main method, which is called after network initialized - */ -void asio_main(); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config; - // zero out the config struct to ensure defaults are setup - memset(&wifi_config, 0, sizeof(wifi_sta_config_t)); - // only copy ssid&password from example config - strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID); - strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -extern "C" void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_sta(); - - // Initialize VFS & UART so we can use std::cout/cin - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); - /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); - - // wait till we receive IP, so asio realated code can be started - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY); - - // network is ready, let's proceed with ASIO example - asio_main(); -} diff --git a/examples/protocols/asio/chat_client/main/Kconfig.projbuild b/examples/protocols/asio/chat_client/main/Kconfig.projbuild index 4185e2424..34d9e93cf 100644 --- a/examples/protocols/asio/chat_client/main/Kconfig.projbuild +++ b/examples/protocols/asio/chat_client/main/Kconfig.projbuild @@ -1,28 +1,16 @@ menu "Example Configuration" - config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config EXAMPLE_PORT - string "asio example port number" + string "Asio example server port number" default "2222" help - Port number used by ASIO example + Port number used by Asio example. config EXAMPLE_SERVER_IP - string "asio example server ip" + string "Asio example server ip" default "FROM_STDIN" help - asio example server ip for this client to connect to (leave default=FROM_STDIN to enter the server address - via serial terminal). + Asio example server ip for this client to connect to. + Leave default "FROM_STDIN" to enter the server address via serial terminal. endmenu diff --git a/examples/protocols/asio/chat_client/main/chat_client.cpp b/examples/protocols/asio/chat_client/main/chat_client.cpp index e18d8d4e3..d4a111057 100644 --- a/examples/protocols/asio/chat_client/main/chat_client.cpp +++ b/examples/protocols/asio/chat_client/main/chat_client.cpp @@ -14,6 +14,10 @@ #include #include "asio.hpp" #include "chat_message.hpp" +#include "protocol_examples_common.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "nvs_flash.h" using asio::ip::tcp; @@ -130,8 +134,21 @@ private: void read_line(char * line, int max_chars); -void asio_main() +extern "C" void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_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()); + + /* This helper function configures blocking UART I/O */ + ESP_ERROR_CHECK(example_configure_stdin_stdout()); + std::string name(CONFIG_EXAMPLE_SERVER_IP); std::string port(CONFIG_EXAMPLE_PORT); char line[chat_message::max_body_length + 1] = { 0 }; @@ -152,7 +169,7 @@ void asio_main() std::thread t([&io_context](){ io_context.run(); }); - while (std::cin.getline(line, chat_message::max_body_length + 1) && std::string(line) != "exit\n") { + while (std::cin.getline(line, chat_message::max_body_length + 1) && std::string(line) != "exit") { chat_message msg; msg.body_length(std::strlen(line)); std::memcpy(msg.body(), line, msg.body_length()); @@ -162,4 +179,6 @@ void asio_main() c.close(); t.join(); + + ESP_ERROR_CHECK(example_disconnect()); } diff --git a/examples/protocols/asio/chat_server/CMakeLists.txt b/examples/protocols/asio/chat_server/CMakeLists.txt index c8c629856..182f8d402 100644 --- a/examples/protocols/asio/chat_server/CMakeLists.txt +++ b/examples/protocols/asio/chat_server/CMakeLists.txt @@ -2,5 +2,9 @@ # 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) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(asio_chat_server) diff --git a/examples/protocols/asio/chat_server/Makefile b/examples/protocols/asio/chat_server/Makefile index 0b0ed1fc5..a7656889f 100644 --- a/examples/protocols/asio/chat_server/Makefile +++ b/examples/protocols/asio/chat_server/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := asio_chat_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/asio/chat_server/README.md b/examples/protocols/asio/chat_server/README.md index 591efd977..e043cba32 100644 --- a/examples/protocols/asio/chat_server/README.md +++ b/examples/protocols/asio/chat_server/README.md @@ -1,19 +1,20 @@ -# ASIO chat server example +# Asio chat server example -Simple asio chat server using WiFi STA +Simple Asio chat server using WiFi STA or Ethernet. ## Example workflow -- WiFi STA is started and trying to connect to the access point defined through `make menuconfig` -- Once connected and acquired IP address, ASIO chat server is started on port number defined through `make menuconfig` -- Chat server echoes a message (received from any client) to all connected clients +- Wi-Fi or Ethernet connection is established, and IP address is obtained. +- Asio chat server is started on port number defined through `make menuconfig`. +- Chat server echoes a message (received from any client) to all connected clients. ## Running the example -- Run `make menuconfig` to configure the access point's SSID and Password and port number -- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal -- Wait for WiFi to connect to your access point (note the IP address) -- Connect to the server using multiple clients, for example using any option below +- Run `make menuconfig` to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +- Set server port number in menuconfig, "Example configuration". +- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal. +- Wait for the board to connect to WiFi or Ethernet (note the IP address). +- Connect to the server using multiple clients, for example using any option below. - build and run asi chat client on your host machine - run chat_client asio example on ESP platform - since chat message consist of ascii size and message, it is possible to diff --git a/examples/protocols/asio/chat_server/asio_chat_server_test.py b/examples/protocols/asio/chat_server/asio_chat_server_test.py index 4f28116aa..ea4702c4e 100644 --- a/examples/protocols/asio/chat_server/asio_chat_server_test.py +++ b/examples/protocols/asio/chat_server/asio_chat_server_test.py @@ -40,7 +40,7 @@ def test_examples_protocol_asio_chat_server(env, extra_data): # 3. create tcp client and connect to server cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli.settimeout(30) - cli.connect((data[0],80)) + cli.connect((data[0], 2222)) cli.send(test_msg) data = cli.recv(1024) # 4. check the message received back from the server diff --git a/examples/protocols/asio/chat_server/components/CMakeLists.txt b/examples/protocols/asio/chat_server/components/CMakeLists.txt deleted file mode 100644 index e51766724..000000000 --- a/examples/protocols/asio/chat_server/components/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(COMPONENT_SRCS "wifi_asio.cpp") - -set(COMPONENT_PRIV_REQUIRES asio nvs_flash console) - -register_component() diff --git a/examples/protocols/asio/chat_server/components/component.mk b/examples/protocols/asio/chat_server/components/component.mk deleted file mode 100644 index b23b0cb71..000000000 --- a/examples/protocols/asio/chat_server/components/component.mk +++ /dev/null @@ -1,10 +0,0 @@ -# -# Component Makefile -# -# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default, -# this will take the sources in the src/ directory, compile them and link them into -# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, -# please read the SDK documents if you need to do this. -# - -COMPONENT_SRCDIRS = . diff --git a/examples/protocols/asio/chat_server/components/wifi_asio.cpp b/examples/protocols/asio/chat_server/components/wifi_asio.cpp deleted file mode 100644 index b90ce26ae..000000000 --- a/examples/protocols/asio/chat_server/components/wifi_asio.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* Common WiFi Init as STA for ASIO examples - - 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 "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "driver/uart.h" -#include "esp_console.h" -#include "esp_vfs_dev.h" - -#include "lwip/err.h" -#include "lwip/sys.h" - -#include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "asio example wifi init"; - -/** - * Definition of ASIO main method, which is called after network initialized - */ -void asio_main(); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config; - // zero out the config struct to ensure defaults are setup - memset(&wifi_config, 0, sizeof(wifi_sta_config_t)); - // only copy ssid&password from example config - strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID); - strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -extern "C" void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_sta(); - - // Initialize VFS & UART so we can use std::cout/cin - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); - /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); - - // wait till we receive IP, so asio realated code can be started - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY); - - // network is ready, let's proceed with ASIO example - asio_main(); -} diff --git a/examples/protocols/asio/chat_server/main/Kconfig.projbuild b/examples/protocols/asio/chat_server/main/Kconfig.projbuild index ecbbf8ae9..c1f397b9c 100644 --- a/examples/protocols/asio/chat_server/main/Kconfig.projbuild +++ b/examples/protocols/asio/chat_server/main/Kconfig.projbuild @@ -1,21 +1,9 @@ menu "Example Configuration" - config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config EXAMPLE_PORT - string "asio example port number" - default "80" + string "Asio example server port number" + default "2222" help - Port number used by ASIO example + Port number used by Asio example endmenu diff --git a/examples/protocols/asio/chat_server/main/chat_server.cpp b/examples/protocols/asio/chat_server/main/chat_server.cpp index 05740f479..56a447e05 100644 --- a/examples/protocols/asio/chat_server/main/chat_server.cpp +++ b/examples/protocols/asio/chat_server/main/chat_server.cpp @@ -17,6 +17,11 @@ #include #include "asio.hpp" #include "chat_message.hpp" +#include "protocol_examples_common.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "nvs_flash.h" + using asio::ip::tcp; @@ -197,8 +202,21 @@ private: //---------------------------------------------------------------------- -int asio_main() +extern "C" void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_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()); + + /* This helper function configures blocking UART I/O */ + ESP_ERROR_CHECK(example_configure_stdin_stdout()); + asio::io_context io_context; std::list servers; @@ -209,6 +227,4 @@ int asio_main() } io_context.run(); - - return 0; } diff --git a/examples/protocols/asio/tcp_echo_server/CMakeLists.txt b/examples/protocols/asio/tcp_echo_server/CMakeLists.txt index b8d5638f7..6bf8f4682 100644 --- a/examples/protocols/asio/tcp_echo_server/CMakeLists.txt +++ b/examples/protocols/asio/tcp_echo_server/CMakeLists.txt @@ -2,5 +2,9 @@ # 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) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(asio_tcp_echo_server) diff --git a/examples/protocols/asio/tcp_echo_server/Makefile b/examples/protocols/asio/tcp_echo_server/Makefile index ef5b79670..ed9cfb8d8 100644 --- a/examples/protocols/asio/tcp_echo_server/Makefile +++ b/examples/protocols/asio/tcp_echo_server/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := asio_tcp_echo_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/asio/tcp_echo_server/README.md b/examples/protocols/asio/tcp_echo_server/README.md index a32fd93ea..f672027ef 100644 --- a/examples/protocols/asio/tcp_echo_server/README.md +++ b/examples/protocols/asio/tcp_echo_server/README.md @@ -1,18 +1,19 @@ -# ASIO tcp echo server example +# Asio TCP echo server example -Simple asio echo server using WiFi STA +Simple Asio TCP echo server using WiFi STA or Ethernet. ## Example workflow -- WiFi STA is started and trying to connect to the access point defined through `make menuconfig` -- Once connected and acquired IP address, ASIO tcp server is started on port number defined through `make menuconfig` -- Server receives and echoes back messages transmitted from client +- Wi-Fi or Ethernet connection is established, and IP address is obtained. +- Asio TCP server is started on port number defined through `make menuconfig`. +- Server receives and echoes back messages transmitted from client. ## Running the example -- Run `make menuconfig` to configure the access point's SSID and Password and port number -- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal -- Wait for WiFi to connect to your access point (note the IP address) -- You can now send a tcp message and check it is repeated, for example using netcat `nc IP PORT` +- Run `make menuconfig` to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +- Set server port number in menuconfig, "Example configuration". +- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal. +- Wait for the board to connect to WiFi or Ethernet (note the IP address). +- You can now send a TCP message and check it is repeated, for example using netcat `nc IP PORT`. See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/examples/protocols/asio/tcp_echo_server/asio_tcp_server_test.py b/examples/protocols/asio/tcp_echo_server/asio_tcp_server_test.py index c29b8ce7b..2cdfd45ae 100644 --- a/examples/protocols/asio/tcp_echo_server/asio_tcp_server_test.py +++ b/examples/protocols/asio/tcp_echo_server/asio_tcp_server_test.py @@ -42,7 +42,7 @@ def test_examples_protocol_asio_tcp_server(env, extra_data): # 3. create tcp client and connect to server cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli.settimeout(30) - cli.connect((data[0],80)) + cli.connect((data[0], 2222)) cli.send(test_msg) data = cli.recv(1024) # 4. check the message received back from the server @@ -51,7 +51,7 @@ def test_examples_protocol_asio_tcp_server(env, extra_data): pass else: print("Failure!") - raise ValueError('Wrong data received from asi tcp server: {} (expoected:{})'.format(data, test_msg)) + raise ValueError('Wrong data received from asi tcp server: {} (expected:{})'.format(data, test_msg)) # 5. check the client message appears also on server terminal dut1.expect(test_msg.decode()) diff --git a/examples/protocols/asio/tcp_echo_server/components/CMakeLists.txt b/examples/protocols/asio/tcp_echo_server/components/CMakeLists.txt deleted file mode 100644 index e51766724..000000000 --- a/examples/protocols/asio/tcp_echo_server/components/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(COMPONENT_SRCS "wifi_asio.cpp") - -set(COMPONENT_PRIV_REQUIRES asio nvs_flash console) - -register_component() diff --git a/examples/protocols/asio/tcp_echo_server/components/component.mk b/examples/protocols/asio/tcp_echo_server/components/component.mk deleted file mode 100644 index b23b0cb71..000000000 --- a/examples/protocols/asio/tcp_echo_server/components/component.mk +++ /dev/null @@ -1,10 +0,0 @@ -# -# Component Makefile -# -# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default, -# this will take the sources in the src/ directory, compile them and link them into -# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, -# please read the SDK documents if you need to do this. -# - -COMPONENT_SRCDIRS = . diff --git a/examples/protocols/asio/tcp_echo_server/components/wifi_asio.cpp b/examples/protocols/asio/tcp_echo_server/components/wifi_asio.cpp deleted file mode 100644 index b90ce26ae..000000000 --- a/examples/protocols/asio/tcp_echo_server/components/wifi_asio.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* Common WiFi Init as STA for ASIO examples - - 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 "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "driver/uart.h" -#include "esp_console.h" -#include "esp_vfs_dev.h" - -#include "lwip/err.h" -#include "lwip/sys.h" - -#include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "asio example wifi init"; - -/** - * Definition of ASIO main method, which is called after network initialized - */ -void asio_main(); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config; - // zero out the config struct to ensure defaults are setup - memset(&wifi_config, 0, sizeof(wifi_sta_config_t)); - // only copy ssid&password from example config - strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID); - strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -extern "C" void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_sta(); - - // Initialize VFS & UART so we can use std::cout/cin - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); - /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); - - // wait till we receive IP, so asio realated code can be started - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY); - - // network is ready, let's proceed with ASIO example - asio_main(); -} diff --git a/examples/protocols/asio/tcp_echo_server/main/Kconfig.projbuild b/examples/protocols/asio/tcp_echo_server/main/Kconfig.projbuild index ecbbf8ae9..795326db9 100644 --- a/examples/protocols/asio/tcp_echo_server/main/Kconfig.projbuild +++ b/examples/protocols/asio/tcp_echo_server/main/Kconfig.projbuild @@ -1,21 +1,9 @@ menu "Example Configuration" - config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config EXAMPLE_PORT - string "asio example port number" - default "80" + string "Asio example port number" + default "2222" help - Port number used by ASIO example + Port number used by Asio example. endmenu diff --git a/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp b/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp index 22f171f53..008ec6ed7 100644 --- a/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp +++ b/examples/protocols/asio/tcp_echo_server/main/echo_server.cpp @@ -1,6 +1,10 @@ #include "asio.hpp" #include #include +#include "protocol_examples_common.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "nvs_flash.h" using asio::ip::tcp; @@ -27,6 +31,7 @@ private: { if (!ec) { + data_[length] = 0; std::cout << data_ << std::endl; do_write(length); } @@ -79,13 +84,24 @@ private: }; -void asio_main() +extern "C" void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_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()); + + /* This helper function configures blocking UART I/O */ + ESP_ERROR_CHECK(example_configure_stdin_stdout()); asio::io_context io_context; server s(io_context, std::atoi(CONFIG_EXAMPLE_PORT)); io_context.run(); - } \ No newline at end of file diff --git a/examples/protocols/asio/udp_echo_server/CMakeLists.txt b/examples/protocols/asio/udp_echo_server/CMakeLists.txt index 8d5c3a692..59493277c 100644 --- a/examples/protocols/asio/udp_echo_server/CMakeLists.txt +++ b/examples/protocols/asio/udp_echo_server/CMakeLists.txt @@ -2,5 +2,9 @@ # 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) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(asio_udp_echo_server) diff --git a/examples/protocols/asio/udp_echo_server/Makefile b/examples/protocols/asio/udp_echo_server/Makefile index a23a5c9ac..f28a9d96a 100644 --- a/examples/protocols/asio/udp_echo_server/Makefile +++ b/examples/protocols/asio/udp_echo_server/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := asio_udp_echo_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/asio/udp_echo_server/README.md b/examples/protocols/asio/udp_echo_server/README.md index 6cf2c2ba3..cc69fee08 100644 --- a/examples/protocols/asio/udp_echo_server/README.md +++ b/examples/protocols/asio/udp_echo_server/README.md @@ -1,18 +1,19 @@ -# ASIO udp echo server example +# Asio UDP echo server example -Simple asio echo server using WiFi STA +Simple Asio UDP echo server using WiFi STA or Ethernet. ## Example workflow -- WiFi STA is started and trying to connect to the access point defined through `make menuconfig` -- Once connected and acquired IP address, ASIO udp server is started on port number defined through `make menuconfig` +- Wi-Fi or Ethernet connection is established, and IP address is obtained. +- Asio UDP server is started on port number defined through `make menuconfig` - Server receives and echoes back messages transmitted from client ## Running the example -- Run `make menuconfig` to configure the access point's SSID and Password and port number -- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal -- Wait for WiFi to connect to your access point (note the IP address) -- You can now send a udp message and check it is repeated, for example using netcat `nc -u IP PORT` +- Run `make menuconfig` to configure Wi-Fi or Ethernet. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +- Set server port number in menuconfig, "Example configuration". +- Run `make flash monitor` to build and upload the example to your board and connect to it's serial terminal. +- Wait for the board to connect to WiFi or Ethernet (note the IP address). +- You can now send a UDP message and check it is repeated, for example using netcat `nc -u IP PORT`. See the README.md file in the upper level 'examples' directory for more information about examples. diff --git a/examples/protocols/asio/udp_echo_server/asio_udp_server_test.py b/examples/protocols/asio/udp_echo_server/asio_udp_server_test.py index d3a01563c..dc2cc78a8 100644 --- a/examples/protocols/asio/udp_echo_server/asio_udp_server_test.py +++ b/examples/protocols/asio/udp_echo_server/asio_udp_server_test.py @@ -42,7 +42,7 @@ def test_examples_protocol_asio_udp_server(env, extra_data): # 3. create tcp client and connect to server cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) cli.settimeout(30) - cli.connect((data[0], 80)) + cli.connect((data[0], 2222)) cli.send(test_msg) data = cli.recv(1024) # 4. check the message received back from the server @@ -51,7 +51,7 @@ def test_examples_protocol_asio_udp_server(env, extra_data): pass else: print("Failure!") - raise ValueError('Wrong data received from asi udp server: {} (expoected:{})'.format(data, test_msg)) + raise ValueError('Wrong data received from asio udp server: {} (expected:{})'.format(data, test_msg)) # 5. check the client message appears also on server terminal dut1.expect(test_msg.decode()) diff --git a/examples/protocols/asio/udp_echo_server/components/CMakeLists.txt b/examples/protocols/asio/udp_echo_server/components/CMakeLists.txt deleted file mode 100644 index e51766724..000000000 --- a/examples/protocols/asio/udp_echo_server/components/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set(COMPONENT_SRCS "wifi_asio.cpp") - -set(COMPONENT_PRIV_REQUIRES asio nvs_flash console) - -register_component() diff --git a/examples/protocols/asio/udp_echo_server/components/component.mk b/examples/protocols/asio/udp_echo_server/components/component.mk deleted file mode 100644 index b23b0cb71..000000000 --- a/examples/protocols/asio/udp_echo_server/components/component.mk +++ /dev/null @@ -1,10 +0,0 @@ -# -# Component Makefile -# -# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default, -# this will take the sources in the src/ directory, compile them and link them into -# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, -# please read the SDK documents if you need to do this. -# - -COMPONENT_SRCDIRS = . diff --git a/examples/protocols/asio/udp_echo_server/components/wifi_asio.cpp b/examples/protocols/asio/udp_echo_server/components/wifi_asio.cpp deleted file mode 100644 index b90ce26ae..000000000 --- a/examples/protocols/asio/udp_echo_server/components/wifi_asio.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* Common WiFi Init as STA for ASIO examples - - 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 "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "driver/uart.h" -#include "esp_console.h" -#include "esp_vfs_dev.h" - -#include "lwip/err.h" -#include "lwip/sys.h" - -#include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "asio example wifi init"; - -/** - * Definition of ASIO main method, which is called after network initialized - */ -void asio_main(); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config; - // zero out the config struct to ensure defaults are setup - memset(&wifi_config, 0, sizeof(wifi_sta_config_t)); - // only copy ssid&password from example config - strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID); - strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -extern "C" void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_sta(); - - // Initialize VFS & UART so we can use std::cout/cin - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); - /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); - - // wait till we receive IP, so asio realated code can be started - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY); - - // network is ready, let's proceed with ASIO example - asio_main(); -} diff --git a/examples/protocols/asio/udp_echo_server/main/Kconfig.projbuild b/examples/protocols/asio/udp_echo_server/main/Kconfig.projbuild index ecbbf8ae9..795326db9 100644 --- a/examples/protocols/asio/udp_echo_server/main/Kconfig.projbuild +++ b/examples/protocols/asio/udp_echo_server/main/Kconfig.projbuild @@ -1,21 +1,9 @@ menu "Example Configuration" - config ESP_WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config ESP_WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config EXAMPLE_PORT - string "asio example port number" - default "80" + string "Asio example port number" + default "2222" help - Port number used by ASIO example + Port number used by Asio example. endmenu diff --git a/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp b/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp index 5b6607d73..06ac3fec9 100644 --- a/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp +++ b/examples/protocols/asio/udp_echo_server/main/udp_echo_server.cpp @@ -13,6 +13,12 @@ #include "asio.hpp" +#include "protocol_examples_common.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "nvs_flash.h" + + using asio::ip::udp; class server @@ -32,6 +38,7 @@ public: { if (!ec && bytes_recvd > 0) { + data_[bytes_recvd] = 0; std::cout << data_ << std::endl; do_send(bytes_recvd); } @@ -59,8 +66,21 @@ private: char data_[max_length]; }; -void asio_main() +extern "C" void app_main() { + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_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()); + + /* This helper function configures blocking UART I/O */ + ESP_ERROR_CHECK(example_configure_stdin_stdout()); + asio::io_context io_context; server s(io_context, std::atoi(CONFIG_EXAMPLE_PORT)); diff --git a/examples/protocols/asio/wifi_init/wifi_asio.cpp b/examples/protocols/asio/wifi_init/wifi_asio.cpp deleted file mode 100644 index b90ce26ae..000000000 --- a/examples/protocols/asio/wifi_init/wifi_asio.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* Common WiFi Init as STA for ASIO examples - - 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 "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" -#include "esp_event_loop.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "driver/uart.h" -#include "esp_console.h" -#include "esp_vfs_dev.h" - -#include "lwip/err.h" -#include "lwip/sys.h" - -#include - -/* The examples use simple WiFi configuration that you can set via - 'make menuconfig'. - - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID -#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t wifi_event_group; - -/* The event group allows multiple bits for each event, - but we only care about one event - are we connected - to the AP with an IP? */ -const int WIFI_CONNECTED_BIT = BIT0; - -static const char *TAG = "asio example wifi init"; - -/** - * Definition of ASIO main method, which is called after network initialized - */ -void asio_main(); - -static esp_err_t event_handler(void *ctx, system_event_t *event) -{ - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI(TAG, "got ip:%s", - ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d", - MAC2STR(event->event_info.sta_connected.mac), - event->event_info.sta_connected.aid); - break; - case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "station:" MACSTR "leave, AID=%d", - MAC2STR(event->event_info.sta_disconnected.mac), - event->event_info.sta_disconnected.aid); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -void wifi_init_sta() -{ - wifi_event_group = xEventGroupCreate(); - - tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) ); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - wifi_config_t wifi_config; - // zero out the config struct to ensure defaults are setup - memset(&wifi_config, 0, sizeof(wifi_sta_config_t)); - // only copy ssid&password from example config - strcpy((char*)(wifi_config.sta.ssid), EXAMPLE_ESP_WIFI_SSID); - strcpy((char*)(wifi_config.sta.password), EXAMPLE_ESP_WIFI_PASS); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); - ESP_ERROR_CHECK(esp_wifi_start() ); - - ESP_LOGI(TAG, "wifi_init_sta finished."); - ESP_LOGI(TAG, "connect to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); -} - -extern "C" void app_main() -{ - //Initialize NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } - ESP_ERROR_CHECK(ret); - - ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); - wifi_init_sta(); - - // Initialize VFS & UART so we can use std::cout/cin - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - /* Install UART driver for interrupt-driven reads and writes */ - ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_CONSOLE_UART_NUM, - 256, 0, 0, NULL, 0) ); - /* Tell VFS to use UART driver */ - esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); - esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); - /* Move the caret to the beginning of the next line on '\n' */ - esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); - - // wait till we receive IP, so asio realated code can be started - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, 1, 1, portMAX_DELAY); - - // network is ready, let's proceed with ASIO example - asio_main(); -}