From fecc7917478eda850a207f174d5574df49dad5dc Mon Sep 17 00:00:00 2001 From: a Date: Fri, 21 Aug 2020 02:25:43 +0200 Subject: [PATCH 1/8] add support for platformio --- .gitignore | 3 +- include/README | 39 ++++++++++++++++ include/mbedtls+HKDF.h | 16 +++++++ lib/README | 46 +++++++++++++++++++ main/CMakeLists.txt | 2 +- main/{wifi.c => wifi/espidf-v4.1.c} | 4 +- main/wifi/zephyr.c | 3 ++ platformio.ini | 70 +++++++++++++++++++++++++++++ test/README | 11 +++++ zephyr/CMakeLists.txt | 6 +++ 10 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 include/README create mode 100644 include/mbedtls+HKDF.h create mode 100644 lib/README rename main/{wifi.c => wifi/espidf-v4.1.c} (99%) create mode 100644 main/wifi/zephyr.c create mode 100644 platformio.ini create mode 100644 test/README create mode 100644 zephyr/CMakeLists.txt diff --git a/.gitignore b/.gitignore index cd45f94..e560edd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.pio .config *.o *.pyc @@ -41,4 +42,4 @@ build dependencies.lock sdkconfig -sdkconfig.old \ No newline at end of file +sdkconfig.old diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/mbedtls+HKDF.h b/include/mbedtls+HKDF.h new file mode 100644 index 0000000..28dfd8b --- /dev/null +++ b/include/mbedtls+HKDF.h @@ -0,0 +1,16 @@ +/** + * \def MBEDTLS_HKDF_C + * + * Disable the HKDF algorithm (RFC 5869). + * + * Module: library/hkdf.c + * Caller: + * + * Requires: MBEDTLS_MD_C + * + * This module adds support for the Hashed Message Authentication Code + * (HMAC)-based key derivation function (HKDF). + */ +#ifndef MBEDTLS_HKDF_C +#define MBEDTLS_HKDF_C +#endif diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 281f413..d625c26 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,6 +2,6 @@ idf_component_register( SRCS "main.c" "display-interface.c" - "wifi.c" + "wifi/espidf-v4.0.c" INCLUDE_DIRS "" ) diff --git a/main/wifi.c b/main/wifi/espidf-v4.1.c similarity index 99% rename from main/wifi.c rename to main/wifi/espidf-v4.1.c index 3a860e5..a6a0a8f 100644 --- a/main/wifi.c +++ b/main/wifi/espidf-v4.1.c @@ -1,3 +1,4 @@ +#ifdef FRAMEWORK_espidf // Copyright 2020 Lukas Haubaum // // Licensed under the GNU Affero General Public License, Version 3; @@ -169,4 +170,5 @@ void wifi_start(void) bool wifi_is_connected(void) { return connected; -} \ No newline at end of file +} +#endif diff --git a/main/wifi/zephyr.c b/main/wifi/zephyr.c new file mode 100644 index 0000000..a560f8d --- /dev/null +++ b/main/wifi/zephyr.c @@ -0,0 +1,3 @@ +#ifdef FRAMEWORK_zephyr +#error WIFI not implemented, yet +#endif diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..6d98398 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,70 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +src_dir = main + +[env] +build_flags = + -Iinclude/tls_config + -DFRAMEWORK_$PIOFRAMEWORK + +[env:esp32dev] +platform = espressif32 +framework = espidf +board = esp32dev +build_flags = + ${env.build_flags} + -Iinclude + -DMBEDTLS_USER_CONFIG_FILE=\"mbedtls+HKDF.h\" +board_build.embed_files = + components/ena/test/export.bin + components/ena-cwa/certs/telekom.pem + + +[env:ID107HR] +platform = nordicnrf51 +board = ng_beacon ; tbd +framework = zephyr +monitor_speed = 115200 +test_ignore = test_desktop +build_flags = + ${env.build_flags} + # For testing: -DUNITY_EXCLUDE_SETJMP_H=1 + -DEN_INCLUDE_ZEPHYR_DEPS=1 + -DEN_INIT_MBEDTLS_ENTROPY=0 +lib_deps = exposure-notification + +[env:nrf52840_dk] +platform = nordicnrf52 +board = nrf52840_dk +framework = zephyr +monitor_speed = 115200 +test_ignore = test_desktop +build_flags = + ${env.build_flags} + # For testing: -DUNITY_EXCLUDE_SETJMP_H=1 + -DEN_INCLUDE_ZEPHYR_DEPS=1 + -DEN_INIT_MBEDTLS_ENTROPY=0 +lib_deps = exposure-notification + + +[env:desktop] +platform = native +test_ignore = test_embedded +lib_compat_mode = off +lib_deps = mbedtls@~2 +build_flags = + ${env.build_flags} + -Iinclude/desktop + -Wno-nullability-completeness + -DMBEDTLS_USER_CONFIG_FILE='"user-tls.conf"' +src_filter = --<.src/> +targets = test diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt new file mode 100644 index 0000000..67128aa --- /dev/null +++ b/zephyr/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(esp-ena) + +FILE(GLOB app_sources ../src/*.c*) +target_sources(app PRIVATE ${app_sources}) From 8c950eb6a135ab1847d8a0c01f9eb95fd14e8f58 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 21 Aug 2020 02:29:19 +0200 Subject: [PATCH 2/8] added 4.0 version of wifi.c --- main/wifi/espidf-v4.0.c | 162 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 main/wifi/espidf-v4.0.c diff --git a/main/wifi/espidf-v4.0.c b/main/wifi/espidf-v4.0.c new file mode 100644 index 0000000..84b21ad --- /dev/null +++ b/main/wifi/espidf-v4.0.c @@ -0,0 +1,162 @@ +#ifdef FRAMEWORK_espidf +// Copyright 2020 Lukas Haubaum +// +// Licensed under the GNU Affero General Public License, Version 3; +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// https://www.gnu.org/licenses/agpl-3.0.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#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 "lwip/err.h" +#include "lwip/sys.h" + +#include "ena-cwa.h" + +#include "wifi.h" + +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static EventGroupHandle_t s_wifi_event_group; + +static int s_retry_num = 0; + +static bool connected = false; + +void wifi_stop(void) +{ + connected = false; + esp_wifi_stop(); + esp_wifi_deinit(); + esp_event_loop_delete_default(); +} + +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_DISCONNECTED: + if (s_retry_num < WIFI_MAXIMUM_RETRY) + { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGD(WIFI_LOG, "retry to connect to the AP"); + } + else + { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGD(WIFI_LOG, "connect to the AP fail"); + connected = false; + break; + case SYSTEM_EVENT_STA_GOT_IP: + s_retry_num = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + connected = true; + + heap_caps_check_integrity_all(true); + break; + default: + ESP_LOGD(WIFI_LOG, "eventbase WIFI, eventid %d", event->event_id); + break; + } + return ESP_OK; +} + +void wifi_start(void) +{ + + // init NVS for WIFI + esp_err_t ret; + ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + ESP_ERROR_CHECK(nvs_flash_erase()); + ESP_ERROR_CHECK(nvs_flash_init()); + } + + s_wifi_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + 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)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + + + wifi_config_t wifi_config = { + .sta = { + .ssid = WIFI_SSID, + .password = WIFI_PASSWORD, + /* Setting a password implies station will connect to all security modes including WEP/WPA. + * However these modes are deprecated and not advisable to be used. Incase your Access point + * doesn't support WPA2, these mode can be enabled by commenting below line */ + .threshold.authmode = WIFI_AUTH_WPA2_PSK, + + .pmf_cfg = { + .capable = true, + .required = false}, + }, + }; + 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()); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + * happened. */ + if (bits & WIFI_CONNECTED_BIT) + { + ESP_LOGV(WIFI_LOG, "connected to ap SSID:%s", + WIFI_SSID); + } + else if (bits & WIFI_FAIL_BIT) + { + ESP_LOGI(WIFI_LOG, "Failed to connect to SSID:%s", + WIFI_SSID); + } + else + { + ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT"); + } + +#if 0 + /* The event will not be processed after unregister */ + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); + vEventGroupDelete(s_wifi_event_group); +#endif +} + +bool wifi_is_connected(void) +{ + return connected; +} +#endif From 903f5afcda5d5b4853e20076ee2f8df1d6a7da3e Mon Sep 17 00:00:00 2001 From: pinkit-cwa <67099188+pinkit-cwa@users.noreply.github.com> Date: Fri, 21 Aug 2020 02:52:18 +0200 Subject: [PATCH 3/8] fixed conflict resolution --- main/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 70512c1..b15b151 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,8 +1,6 @@ idf_component_register( SRCS "main.c" - "display-interface.c" - "wifi/espidf-v4.0.c" INCLUDE_DIRS "" ) From c2bb49b0ba07a036815bde6d1db21afd800ebbd8 Mon Sep 17 00:00:00 2001 From: pinkit-cwa <67099188+pinkit-cwa@users.noreply.github.com> Date: Fri, 21 Aug 2020 03:46:48 +0200 Subject: [PATCH 4/8] reverted conflict resolution --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e884017..e5c1182 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,6 @@ build dependencies.lock sdkconfig -sdkconfig.old \ No newline at end of file +sdkconfig.old + +.local From c981cdbf0d5c33dc79cdfc3ac4f5517f3f84dcd4 Mon Sep 17 00:00:00 2001 From: pinkit-cwa <67099188+pinkit-cwa@users.noreply.github.com> Date: Fri, 21 Aug 2020 03:47:54 +0200 Subject: [PATCH 6/8] Update CMakeLists.txt --- main/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b15b151..af3e167 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,5 @@ idf_component_register( SRCS "main.c" - INCLUDE_DIRS "" ) From 90afad266600288a96a75efa5e53bbbe596e78ab Mon Sep 17 00:00:00 2001 From: a Date: Fri, 21 Aug 2020 03:50:13 +0200 Subject: [PATCH 7/8] adjusted to modified locations; updated to latest content --- components/wifi-controller/CMakeLists.txt | 4 +- components/wifi-controller/espidf-v4.0.c | 179 ++++++++++++++++++ .../{wifi-controller.c => espidf-v4.1.c} | 4 +- .../wifi-controller}/zephyr.c | 0 main/wifi/espidf-v4.0.c | 162 ---------------- main/wifi/espidf-v4.1.c | 174 ----------------- 6 files changed, 184 insertions(+), 339 deletions(-) create mode 100644 components/wifi-controller/espidf-v4.0.c rename components/wifi-controller/{wifi-controller.c => espidf-v4.1.c} (99%) rename {main/wifi => components/wifi-controller}/zephyr.c (100%) delete mode 100644 main/wifi/espidf-v4.0.c delete mode 100644 main/wifi/espidf-v4.1.c diff --git a/components/wifi-controller/CMakeLists.txt b/components/wifi-controller/CMakeLists.txt index f5bd135..549afa6 100644 --- a/components/wifi-controller/CMakeLists.txt +++ b/components/wifi-controller/CMakeLists.txt @@ -1,8 +1,8 @@ idf_component_register( SRCS - "wifi-controller.c" + "espidf-v4.0.c" INCLUDE_DIRS "." PRIV_REQUIRES esp_wifi nvs_flash -) \ No newline at end of file +) diff --git a/components/wifi-controller/espidf-v4.0.c b/components/wifi-controller/espidf-v4.0.c new file mode 100644 index 0000000..de32e3a --- /dev/null +++ b/components/wifi-controller/espidf-v4.0.c @@ -0,0 +1,179 @@ +#ifdef FRAMEWORK_espidf +// Copyright 2020 Lukas Haubaum +// +// Licensed under the GNU Affero General Public License, Version 3; +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// https://www.gnu.org/licenses/agpl-3.0.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#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 "lwip/err.h" +#include "lwip/sys.h" + +#include "wifi-controller.h" + +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static EventGroupHandle_t s_wifi_event_group; + +static bool initialized = false; + +static wifi_ap_record_t current_wifi_ap; + +void wifi_controller_init(void) +{ + // init NVS for WIFI + esp_err_t ret; + ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + ESP_ERROR_CHECK(nvs_flash_erase()); + ESP_ERROR_CHECK(nvs_flash_init()); + } + + tcpip_adapter_init(); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + initialized = true; +} + +void wifi_controller_scan(wifi_ap_record_t *ap_info, uint16_t *ap_count) +{ + if (!initialized) + { + wifi_controller_init(); + } + + uint16_t number = 10; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info)); + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(ap_count)); + ESP_ERROR_CHECK(esp_wifi_stop()); +} + +static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) + { + esp_wifi_connect(); + } + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) + { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) + { + ESP_LOGD(WIFI_LOG, "got IP"); + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + heap_caps_check_integrity_all(true); + } +} + +esp_err_t wifi_controller_connect(wifi_config_t wifi_config) +{ + if (!initialized) + { + wifi_controller_init(); + } + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + + 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()); + + s_wifi_event_group = xEventGroupCreate(); + + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler)); + vEventGroupDelete(s_wifi_event_group); + + if (bits & WIFI_CONNECTED_BIT) + { + ESP_LOGV(WIFI_LOG, "connected to ap SSID:%s", wifi_config.sta.ssid); + return ESP_OK; + } + else if (bits & WIFI_FAIL_BIT) + { + ESP_LOGI(WIFI_LOG, "Failed to connect to SSID:%s with PASSWORD:%s", wifi_config.sta.ssid, wifi_config.sta.password); + return ESP_ERR_INVALID_ARG; + } + else + { + ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT"); + return ESP_FAIL; + } +} + +esp_err_t wifi_controller_reconnect(void) +{ + if (!initialized) + { + wifi_controller_init(); + } + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); + + s_wifi_event_group = xEventGroupCreate(); + + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler)); + vEventGroupDelete(s_wifi_event_group); + + if (bits & WIFI_CONNECTED_BIT) + { + ESP_LOGV(WIFI_LOG, "reconnected to last ap"); + return ESP_OK; + } + else if (bits & WIFI_FAIL_BIT) + { + ESP_LOGI(WIFI_LOG, "Failed to reconnect to last ap"); + return ESP_ERR_INVALID_ARG; + } + else + { + ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT"); + return ESP_FAIL; + } +} + +wifi_ap_record_t *wifi_controller_connection(void) +{ + if (esp_wifi_sta_get_ap_info(¤t_wifi_ap) == ESP_OK) + { + ESP_LOGD(WIFI_LOG, "Current AP: %s", current_wifi_ap.ssid); + return ¤t_wifi_ap; + } + return NULL; +} +#endif diff --git a/components/wifi-controller/wifi-controller.c b/components/wifi-controller/espidf-v4.1.c similarity index 99% rename from components/wifi-controller/wifi-controller.c rename to components/wifi-controller/espidf-v4.1.c index 7395e63..d041872 100644 --- a/components/wifi-controller/wifi-controller.c +++ b/components/wifi-controller/espidf-v4.1.c @@ -1,3 +1,4 @@ +#ifdef FRAMEWORK_espidf // Copyright 2020 Lukas Haubaum // // Licensed under the GNU Affero General Public License, Version 3; @@ -182,4 +183,5 @@ wifi_ap_record_t *wifi_controller_connection(void) return ¤t_wifi_ap; } return NULL; -} \ No newline at end of file +} +#endif diff --git a/main/wifi/zephyr.c b/components/wifi-controller/zephyr.c similarity index 100% rename from main/wifi/zephyr.c rename to components/wifi-controller/zephyr.c diff --git a/main/wifi/espidf-v4.0.c b/main/wifi/espidf-v4.0.c deleted file mode 100644 index 84b21ad..0000000 --- a/main/wifi/espidf-v4.0.c +++ /dev/null @@ -1,162 +0,0 @@ -#ifdef FRAMEWORK_espidf -// Copyright 2020 Lukas Haubaum -// -// Licensed under the GNU Affero General Public License, Version 3; -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// https://www.gnu.org/licenses/agpl-3.0.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#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 "lwip/err.h" -#include "lwip/sys.h" - -#include "ena-cwa.h" - -#include "wifi.h" - -#define WIFI_CONNECTED_BIT BIT0 -#define WIFI_FAIL_BIT BIT1 - -static EventGroupHandle_t s_wifi_event_group; - -static int s_retry_num = 0; - -static bool connected = false; - -void wifi_stop(void) -{ - connected = false; - esp_wifi_stop(); - esp_wifi_deinit(); - esp_event_loop_delete_default(); -} - -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_DISCONNECTED: - if (s_retry_num < WIFI_MAXIMUM_RETRY) - { - esp_wifi_connect(); - s_retry_num++; - ESP_LOGD(WIFI_LOG, "retry to connect to the AP"); - } - else - { - xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); - } - ESP_LOGD(WIFI_LOG, "connect to the AP fail"); - connected = false; - break; - case SYSTEM_EVENT_STA_GOT_IP: - s_retry_num = 0; - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - connected = true; - - heap_caps_check_integrity_all(true); - break; - default: - ESP_LOGD(WIFI_LOG, "eventbase WIFI, eventid %d", event->event_id); - break; - } - return ESP_OK; -} - -void wifi_start(void) -{ - - // init NVS for WIFI - esp_err_t ret; - ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) - { - ESP_ERROR_CHECK(nvs_flash_erase()); - ESP_ERROR_CHECK(nvs_flash_init()); - } - - s_wifi_event_group = xEventGroupCreate(); - - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - 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)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - - - wifi_config_t wifi_config = { - .sta = { - .ssid = WIFI_SSID, - .password = WIFI_PASSWORD, - /* Setting a password implies station will connect to all security modes including WEP/WPA. - * However these modes are deprecated and not advisable to be used. Incase your Access point - * doesn't support WPA2, these mode can be enabled by commenting below line */ - .threshold.authmode = WIFI_AUTH_WPA2_PSK, - - .pmf_cfg = { - .capable = true, - .required = false}, - }, - }; - 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()); - - /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum - * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ - EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, - WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & WIFI_CONNECTED_BIT) - { - ESP_LOGV(WIFI_LOG, "connected to ap SSID:%s", - WIFI_SSID); - } - else if (bits & WIFI_FAIL_BIT) - { - ESP_LOGI(WIFI_LOG, "Failed to connect to SSID:%s", - WIFI_SSID); - } - else - { - ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT"); - } - -#if 0 - /* The event will not be processed after unregister */ - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); - vEventGroupDelete(s_wifi_event_group); -#endif -} - -bool wifi_is_connected(void) -{ - return connected; -} -#endif diff --git a/main/wifi/espidf-v4.1.c b/main/wifi/espidf-v4.1.c deleted file mode 100644 index a6a0a8f..0000000 --- a/main/wifi/espidf-v4.1.c +++ /dev/null @@ -1,174 +0,0 @@ -#ifdef FRAMEWORK_espidf -// Copyright 2020 Lukas Haubaum -// -// Licensed under the GNU Affero General Public License, Version 3; -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// https://www.gnu.org/licenses/agpl-3.0.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#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 "lwip/err.h" -#include "lwip/sys.h" - -#include "ena-cwa.h" - -#include "wifi.h" - -#define WIFI_CONNECTED_BIT BIT0 -#define WIFI_FAIL_BIT BIT1 - -static EventGroupHandle_t s_wifi_event_group; - -static int s_retry_num = 0; - -static bool connected = false; - -void wifi_stop(void) -{ - connected = false; - esp_wifi_stop(); - esp_wifi_deinit(); - esp_event_loop_delete_default(); -} - -static void event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) - { - esp_wifi_connect(); - } - else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) - { - if (s_retry_num < WIFI_MAXIMUM_RETRY) - { - esp_wifi_connect(); - s_retry_num++; - ESP_LOGD(WIFI_LOG, "retry to connect to the AP"); - } - else - { - xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); - } - ESP_LOGD(WIFI_LOG, "connect to the AP fail"); - connected = false; - } - else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) - { - s_retry_num = 0; - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - connected = true; - - heap_caps_check_integrity_all(true); - } - else - { - ESP_LOGD(WIFI_LOG, "eventbase %s, eventid %d", event_base, event_id); - } -} - -void wifi_start(void) -{ - - // init NVS for WIFI - esp_err_t ret; - ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) - { - ESP_ERROR_CHECK(nvs_flash_erase()); - ESP_ERROR_CHECK(nvs_flash_init()); - } - - s_wifi_event_group = xEventGroupCreate(); - - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - ESP_ERROR_CHECK(esp_netif_init()); - esp_netif_create_default_wifi_sta(); - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - - esp_event_handler_instance_t instance_any_id; - esp_event_handler_instance_t instance_got_ip; - ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, - ESP_EVENT_ANY_ID, - &event_handler, - NULL, - &instance_any_id)); - ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, - IP_EVENT_STA_GOT_IP, - &event_handler, - NULL, - &instance_got_ip)); - - wifi_config_t wifi_config = { - .sta = { - .ssid = WIFI_SSID, - .password = WIFI_PASSWORD, - /* Setting a password implies station will connect to all security modes including WEP/WPA. - * However these modes are deprecated and not advisable to be used. Incase your Access point - * doesn't support WPA2, these mode can be enabled by commenting below line */ - .threshold.authmode = WIFI_AUTH_WPA2_PSK, - - .pmf_cfg = { - .capable = true, - .required = false}, - }, - }; - 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()); - - /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum - * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ - EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, - WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & WIFI_CONNECTED_BIT) - { - ESP_LOGV(WIFI_LOG, "connected to ap SSID:%s", - WIFI_SSID); - } - else if (bits & WIFI_FAIL_BIT) - { - ESP_LOGI(WIFI_LOG, "Failed to connect to SSID:%s", - WIFI_SSID); - } - else - { - ESP_LOGE(WIFI_LOG, "UNEXPECTED EVENT"); - } - - /* The event will not be processed after unregister */ - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); - ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); - vEventGroupDelete(s_wifi_event_group); -} - -bool wifi_is_connected(void) -{ - return connected; -} -#endif From a9290385abf47afdf868024492504b2983450468 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 21 Aug 2020 04:28:11 +0200 Subject: [PATCH 8/8] use components/wifi-controller/espidf-v4.1.c --- components/wifi-controller/CMakeLists.txt | 2 +- components/wifi-controller/espidf-v4.0.c | 3 --- components/wifi-controller/espidf-v4.1.c | 2 -- components/wifi-controller/zephyr.c | 2 -- 4 files changed, 1 insertion(+), 8 deletions(-) diff --git a/components/wifi-controller/CMakeLists.txt b/components/wifi-controller/CMakeLists.txt index 549afa6..a858a48 100644 --- a/components/wifi-controller/CMakeLists.txt +++ b/components/wifi-controller/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( SRCS - "espidf-v4.0.c" + "espidf-v4.1.c" INCLUDE_DIRS "." PRIV_REQUIRES esp_wifi diff --git a/components/wifi-controller/espidf-v4.0.c b/components/wifi-controller/espidf-v4.0.c index de32e3a..8c60824 100644 --- a/components/wifi-controller/espidf-v4.0.c +++ b/components/wifi-controller/espidf-v4.0.c @@ -1,5 +1,3 @@ -#ifdef FRAMEWORK_espidf -// Copyright 2020 Lukas Haubaum // // Licensed under the GNU Affero General Public License, Version 3; // you may not use this file except in compliance with the License. @@ -176,4 +174,3 @@ wifi_ap_record_t *wifi_controller_connection(void) } return NULL; } -#endif diff --git a/components/wifi-controller/espidf-v4.1.c b/components/wifi-controller/espidf-v4.1.c index d041872..3f4b8f0 100644 --- a/components/wifi-controller/espidf-v4.1.c +++ b/components/wifi-controller/espidf-v4.1.c @@ -1,4 +1,3 @@ -#ifdef FRAMEWORK_espidf // Copyright 2020 Lukas Haubaum // // Licensed under the GNU Affero General Public License, Version 3; @@ -184,4 +183,3 @@ wifi_ap_record_t *wifi_controller_connection(void) } return NULL; } -#endif diff --git a/components/wifi-controller/zephyr.c b/components/wifi-controller/zephyr.c index a560f8d..e8d1b89 100644 --- a/components/wifi-controller/zephyr.c +++ b/components/wifi-controller/zephyr.c @@ -1,3 +1 @@ -#ifdef FRAMEWORK_zephyr #error WIFI not implemented, yet -#endif