]> granicus.if.org Git - esp-idf/commitdiff
examples/protocols/openssl: use common network component
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 20 Nov 2018 16:41:45 +0000 (00:41 +0800)
committerbot <bot@espressif.com>
Mon, 15 Apr 2019 03:32:05 +0000 (03:32 +0000)
12 files changed:
examples/protocols/openssl_client/CMakeLists.txt
examples/protocols/openssl_client/Makefile
examples/protocols/openssl_client/README.md
examples/protocols/openssl_client/main/Kconfig.projbuild
examples/protocols/openssl_client/main/openssl_client_example.h
examples/protocols/openssl_client/main/openssl_client_example_main.c
examples/protocols/openssl_server/CMakeLists.txt
examples/protocols/openssl_server/Makefile
examples/protocols/openssl_server/README.md
examples/protocols/openssl_server/main/Kconfig.projbuild [deleted file]
examples/protocols/openssl_server/main/openssl_server_example.h
examples/protocols/openssl_server/main/openssl_server_example_main.c

index 5449461ce7aa70a91b9d6a22247d0511a62c406c..577d4ae0820a1c38440ad2c206d8ce124b461b1f 100644 (file)
@@ -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)
index 7e2f4fe7f8b241d357a98127343e48b2b99e93e4..1ae39bf11f5a724b5d48f5dbbc9950caed6c9a6c 100644 (file)
@@ -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
 
index 2a6ac19881be1c0a65b339f163e13bc272d90622..07be757195c606f91a8e5e91884f5d8667bfacac 100644 (file)
@@ -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
index 93fa37b6ea25d04919d4718db7b573fed1cd9532..64b5b373044bb8614f817e60a7538e4b1ead0665 100644 (file)
@@ -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
index fbdecb0d277f378986cd0cf531de422935dfb906..8d1645164a2975901fe804a48f87563049df5de5 100644 (file)
 #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'.
 
index 97bcf2abe6de16f5db1df1775eaffd43ef1a462b..a06ef6cdd4083ec03c745202ef14a98956255fbd 100644 (file)
 
 #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();
 }
index d3e5c5ccbea810abbe508cae48ec6e55db8b855b..9fb42034a5437c6eafb5d8d2ddd29b5caa2a114a 100644 (file)
@@ -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)
index f65f11a562599fe65c19d8617e4f511ad025b175..24d020d696efa24d2144eb112308aa1776b56921 100644 (file)
@@ -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
 
index 916f619432daf8364f760f5aa4b9a78804bbc0da..6d4506e933f6be5be26fcd4ee9eb7def6ef89e86 100644 (file)
@@ -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 (file)
index ed4e691..0000000
+++ /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
index 8097b2b7417b9bace5ed85c81e5c71dfdf0a95d5..39276f9368c500eb14b5d6d30180c4cbdff385c0 100644 (file)
 
 #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
index df3e0f7c0cb00da3ba6c64bf5295d8452ee25307..9db6fc04f0e64a8f11feaa68193909611ecc29d0 100644 (file)
 
 #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)
+void app_main(void)
 {
+    ESP_ERROR_CHECK(nvs_flash_init());
     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() );
-}
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
 
-void app_main(void)
-{
-    ESP_ERROR_CHECK( nvs_flash_init() );
-    wifi_conn_init();
+    /* 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();
 }