diff --git a/.gitignore b/.gitignore index c3e96ef..e5c1182 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.pio .config *.o *.pyc @@ -43,4 +44,4 @@ dependencies.lock sdkconfig sdkconfig.old -.local \ No newline at end of file +.local diff --git a/components/wifi-controller/CMakeLists.txt b/components/wifi-controller/CMakeLists.txt index f5bd135..a858a48 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.1.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..8c60824 --- /dev/null +++ b/components/wifi-controller/espidf-v4.0.c @@ -0,0 +1,176 @@ +// +// 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; +} 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..3f4b8f0 100644 --- a/components/wifi-controller/wifi-controller.c +++ b/components/wifi-controller/espidf-v4.1.c @@ -182,4 +182,4 @@ wifi_ap_record_t *wifi_controller_connection(void) return ¤t_wifi_ap; } return NULL; -} \ No newline at end of file +} diff --git a/components/wifi-controller/zephyr.c b/components/wifi-controller/zephyr.c new file mode 100644 index 0000000..e8d1b89 --- /dev/null +++ b/components/wifi-controller/zephyr.c @@ -0,0 +1 @@ +#error WIFI not implemented, yet 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/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})