From a46d94250d19ab676aa803df6e78a4b718a5edbb Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 00:41:45 +0800 Subject: [PATCH] examples/protocols/openssl: use common network component --- .../protocols/openssl_client/CMakeLists.txt | 4 ++ examples/protocols/openssl_client/Makefile | 2 + examples/protocols/openssl_client/README.md | 13 ++-- .../openssl_client/main/Kconfig.projbuild | 12 ---- .../main/openssl_client_example.h | 9 --- .../main/openssl_client_example_main.c | 69 ++++--------------- .../protocols/openssl_server/CMakeLists.txt | 4 ++ examples/protocols/openssl_server/Makefile | 2 + examples/protocols/openssl_server/README.md | 9 +-- .../openssl_server/main/Kconfig.projbuild | 15 ---- .../main/openssl_server_example.h | 9 --- .../main/openssl_server_example_main.c | 67 ++++-------------- 12 files changed, 53 insertions(+), 162 deletions(-) delete mode 100644 examples/protocols/openssl_server/main/Kconfig.projbuild diff --git a/examples/protocols/openssl_client/CMakeLists.txt b/examples/protocols/openssl_client/CMakeLists.txt index 5449461ce7..577d4ae082 100644 --- a/examples/protocols/openssl_client/CMakeLists.txt +++ b/examples/protocols/openssl_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(openssl_client) diff --git a/examples/protocols/openssl_client/Makefile b/examples/protocols/openssl_client/Makefile index 7e2f4fe7f8..1ae39bf11f 100644 --- a/examples/protocols/openssl_client/Makefile +++ b/examples/protocols/openssl_client/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := openssl_client +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/openssl_client/README.md b/examples/protocols/openssl_client/README.md index 2a6ac19881..07be757195 100644 --- a/examples/protocols/openssl_client/README.md +++ b/examples/protocols/openssl_client/README.md @@ -2,12 +2,13 @@ The Example contains of OpenSSL client demo. -First you should config the project by "make menuconfig": - Example Configuration -> - 1. Target Domain : the domain that you want to connect to, and default is "www.baidu.com". - 2. Target port number : the port number of the target domain, and default is 443. - 3. WIFI SSID : your own WIFI, which is connected to the Internet, and default is "myssid". - 4. WIFI Password : WIFI password, and default is "mypassword" +To configure the project, run `make menuconfig` (or `idf.py menuconfig` if using CMake build system). + +* 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. + +* When using Make build system, set `Default serial port` under `Serial flasher config`. + +* Configure target domain and port number under "Example Configuration" If you want to test the OpenSSL client demo: 1. compile the code and load the firmware diff --git a/examples/protocols/openssl_client/main/Kconfig.projbuild b/examples/protocols/openssl_client/main/Kconfig.projbuild index 93fa37b6ea..64b5b37304 100644 --- a/examples/protocols/openssl_client/main/Kconfig.projbuild +++ b/examples/protocols/openssl_client/main/Kconfig.projbuild @@ -13,16 +13,4 @@ menu "Example Configuration" help Target port number for the example to connect to. - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - endmenu diff --git a/examples/protocols/openssl_client/main/openssl_client_example.h b/examples/protocols/openssl_client/main/openssl_client_example.h index fbdecb0d27..8d1645164a 100644 --- a/examples/protocols/openssl_client/main/openssl_client_example.h +++ b/examples/protocols/openssl_client/main/openssl_client_example.h @@ -10,15 +10,6 @@ #ifndef _OPENSSL_EXAMPLE_H_ #define _OPENSSL_EXAMPLE_H_ -/* 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_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - /* The examples use domain of "www.baidu.com" and port number of 433 that you can set via 'make menuconfig'. diff --git a/examples/protocols/openssl_client/main/openssl_client_example_main.c b/examples/protocols/openssl_client/main/openssl_client_example_main.c index 97bcf2abe6..a06ef6cdd4 100644 --- a/examples/protocols/openssl_client/main/openssl_client_example_main.c +++ b/examples/protocols/openssl_client/main/openssl_client_example_main.c @@ -15,24 +15,17 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_log.h" #include "esp_wifi.h" -#include "esp_event_loop.h" - +#include "esp_event.h" #include "nvs_flash.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "lwip/sockets.h" #include "lwip/netdb.h" -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 static int CONNECTED_BIT = BIT0; - const static char *TAG = "openssl_example"; static void openssl_example_task(void *p) @@ -142,7 +135,7 @@ static void openssl_example_task(void *p) ESP_LOGI(TAG, "%s", recv_buf); } while (1); - ESP_LOGI(TAG, "totaly read %d bytes data from %s ......", recv_bytes, OPENSSL_EXAMPLE_TARGET_NAME); + ESP_LOGI(TAG, "totally read %d bytes data from %s ......", recv_bytes, OPENSSL_EXAMPLE_TARGET_NAME); failed5: SSL_shutdown(ssl); @@ -177,50 +170,18 @@ static void openssl_example_client_init(void) } } -static esp_err_t wifi_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: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - openssl_example_client_init(); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_conn_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) ); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_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_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - void app_main(void) { ESP_ERROR_CHECK( nvs_flash_init() ); - wifi_conn_init(); + 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()); + + openssl_example_client_init(); } diff --git a/examples/protocols/openssl_server/CMakeLists.txt b/examples/protocols/openssl_server/CMakeLists.txt index d3e5c5ccbe..9fb42034a5 100644 --- a/examples/protocols/openssl_server/CMakeLists.txt +++ b/examples/protocols/openssl_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(openssl_server) diff --git a/examples/protocols/openssl_server/Makefile b/examples/protocols/openssl_server/Makefile index f65f11a562..24d020d696 100644 --- a/examples/protocols/openssl_server/Makefile +++ b/examples/protocols/openssl_server/Makefile @@ -5,5 +5,7 @@ PROJECT_NAME := openssl_server +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/openssl_server/README.md b/examples/protocols/openssl_server/README.md index 916f619432..6d4506e933 100644 --- a/examples/protocols/openssl_server/README.md +++ b/examples/protocols/openssl_server/README.md @@ -2,10 +2,11 @@ The Example contains of OpenSSL server demo. -First you should configure the project by "make menuconfig": - Example Configuration -> - 1. WIFI SSID: WIFI network to which your PC is also connected to. - 2. WIFI Password: WIFI password +To configure the project, run `make menuconfig` (or `idf.py menuconfig` if using CMake build system). + +* 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. + +* When using Make build system, set `Default serial port` under `Serial flasher config`. IF you want to test the OpenSSL server demo: 1. compile the code and load the firmware diff --git a/examples/protocols/openssl_server/main/Kconfig.projbuild b/examples/protocols/openssl_server/main/Kconfig.projbuild deleted file mode 100644 index ed4e691688..0000000000 --- a/examples/protocols/openssl_server/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - -endmenu diff --git a/examples/protocols/openssl_server/main/openssl_server_example.h b/examples/protocols/openssl_server/main/openssl_server_example.h index 8097b2b741..39276f9368 100644 --- a/examples/protocols/openssl_server/main/openssl_server_example.h +++ b/examples/protocols/openssl_server/main/openssl_server_example.h @@ -12,15 +12,6 @@ #include "sdkconfig.h" -/* 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_WIFI_SSID CONFIG_WIFI_SSID -#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD - #define OPENSSL_EXAMPLE_TASK_NAME "openssl_example" #define OPENSSL_EXAMPLE_TASK_STACK_WORDS 10240 #define OPENSSL_EXAMPLE_TASK_PRIORITY 8 diff --git a/examples/protocols/openssl_server/main/openssl_server_example_main.c b/examples/protocols/openssl_server/main/openssl_server_example_main.c index df3e0f7c0c..9db6fc04f0 100644 --- a/examples/protocols/openssl_server/main/openssl_server_example_main.c +++ b/examples/protocols/openssl_server/main/openssl_server_example_main.c @@ -15,23 +15,17 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_log.h" #include "esp_wifi.h" -#include "esp_event_loop.h" - +#include "esp_event.h" +#include "tcpip_adapter.h" #include "nvs_flash.h" +#include "protocol_examples_common.h" #include "lwip/sockets.h" #include "lwip/netdb.h" -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 static int CONNECTED_BIT = BIT0; const static char *TAG = "Openssl_example"; @@ -211,50 +205,17 @@ static void openssl_server_init(void) } } -static esp_err_t wifi_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: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - openssl_server_init(); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_conn_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) ); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_WIFI_SSID, - .password = EXAMPLE_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_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]\n", EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS); - ESP_ERROR_CHECK( esp_wifi_start() ); -} - void app_main(void) { - ESP_ERROR_CHECK( nvs_flash_init() ); - wifi_conn_init(); + 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()); + + openssl_server_init(); }