From 20e526dd4aae4eabeba405b651a565aed36d9ec1 Mon Sep 17 00:00:00 2001 From: Luca Arato Date: Wed, 7 Aug 2024 12:43:02 +0200 Subject: [PATCH] Update sdk version and example Signed-off-by: Luca Arato --- .github/workflows/build.yaml | 12 +- .gitignore | 6 + CHANGELOG.md | 2 +- examples/edgehog_app/CMakeLists.txt | 15 ++ examples/edgehog_app/Makefile | 27 ---- .../edgehog_app/edgehog_partition_example.csv | 2 + examples/edgehog_app/main/CMakeLists.txt | 2 +- examples/edgehog_app/main/Kconfig.projbuild | 93 ++++++++++- examples/edgehog_app/main/component.mk | 26 --- .../edgehog_app/main/include/example_task.h | 3 + examples/edgehog_app/main/include/wifi.h | 12 ++ examples/edgehog_app/main/include/wifi_cfg.h | 29 ---- examples/edgehog_app/main/main.c | 11 +- examples/edgehog_app/main/src/example_task.c | 131 ++++++++++----- examples/edgehog_app/main/src/wifi.c | 149 ++++++++++++++++++ examples/edgehog_app/main/src/wifi_cfg.c | 118 -------------- examples/edgehog_app/sdkconfig.defaults | 5 +- idf_component.yml | 2 +- 18 files changed, 381 insertions(+), 264 deletions(-) delete mode 100644 examples/edgehog_app/Makefile delete mode 100644 examples/edgehog_app/main/component.mk create mode 100644 examples/edgehog_app/main/include/wifi.h delete mode 100644 examples/edgehog_app/main/include/wifi_cfg.h create mode 100644 examples/edgehog_app/main/src/wifi.c delete mode 100644 examples/edgehog_app/main/src/wifi_cfg.c diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 05ad5e9..84c51a7 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -31,12 +31,15 @@ jobs: strategy: matrix: idf-version: - - "4.4.4" - - "5.0.1" + - "4.4" + - "5.0" + - "5.1" + - "5.2" + - "5.3" build-system: - idf runs-on: ubuntu-latest - container: espressif/idf:v${{ matrix.idf-version }} + container: espressif/idf:release-v${{ matrix.idf-version }} steps: - name: Check out repository uses: actions/checkout@v2 @@ -48,8 +51,7 @@ jobs: shell: bash - name: Build with idf.py run: | - . $IDF_PATH/export.sh - pip install idf-component-manager --upgrade + source $IDF_PATH/export.sh idf.py reconfigure idf.py build # Print component size info diff --git a/.gitignore b/.gitignore index 7865c7c..c058c8c 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,9 @@ doc/out # VS code config .vscode/ + +# LSP cache +.cache + +# Private files +*.priv diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ada302..dd59817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed -- Bump Astarte Device SDK to v1.2.0. +- Bump Astarte Device SDK to v1.3.1. ## [0.7.1] - 2023-09-19 ### Changed diff --git a/examples/edgehog_app/CMakeLists.txt b/examples/edgehog_app/CMakeLists.txt index f9c7f79..ae1b7ef 100644 --- a/examples/edgehog_app/CMakeLists.txt +++ b/examples/edgehog_app/CMakeLists.txt @@ -22,6 +22,21 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# Get the absolute path of the current directory +get_filename_component(CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE) +# Construct the absolute path to sdkconfig.priv +set(SDKCONFIG_PRIV_PATH "${CURRENT_DIR}/sdkconfig.priv") +# Construct the absolute path to sdkconfig.defaults +set(SDKCONFIG_DEFAULTS_PATH "${CURRENT_DIR}/sdkconfig.defaults") +# Check if sdkconfig.priv exists and add it to SDKCONFIG_DEFAULTS +if(EXISTS "${SDKCONFIG_PRIV_PATH}") + if(EXISTS "${SDKCONFIG_DEFAULTS_PATH}") + set(SDKCONFIG_DEFAULTS "sdkconfig.defaults;sdkconfig.priv") + else() + set(SDKCONFIG_DEFAULTS "sdkconfig.priv") + endif() +endif() + include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(PROJECT_VER "0.1.0") diff --git a/examples/edgehog_app/Makefile b/examples/edgehog_app/Makefile deleted file mode 100644 index 0e8a4bc..0000000 --- a/examples/edgehog_app/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# -# This file is part of Edgehog. -# -# Copyright 2021 SECO Mind Srl -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# 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. -# -# SPDX-License-Identifier: Apache-2.0 -# - -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := edgehog-app -include $(IDF_PATH)/make/project.mk diff --git a/examples/edgehog_app/edgehog_partition_example.csv b/examples/edgehog_app/edgehog_partition_example.csv index 74643cb..16924c9 100644 --- a/examples/edgehog_app/edgehog_partition_example.csv +++ b/examples/edgehog_app/edgehog_partition_example.csv @@ -27,3 +27,5 @@ phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, , 0x12c000, ota_0, app, ota_0, , 0x12c000, ota_1, app, ota_1, , 0x12c000, +astarte, data, nvs, , 200K, +edgehog, data, nvs, , 200K, diff --git a/examples/edgehog_app/main/CMakeLists.txt b/examples/edgehog_app/main/CMakeLists.txt index 77027d1..98d51b8 100644 --- a/examples/edgehog_app/main/CMakeLists.txt +++ b/examples/edgehog_app/main/CMakeLists.txt @@ -18,6 +18,6 @@ # SPDX-License-Identifier: Apache-2.0 # -idf_component_register(SRCS "main.c" "./src/wifi_cfg.c" "./src/example_task.c" +idf_component_register(SRCS "main.c" "./src/wifi.c" "./src/example_task.c" INCLUDE_DIRS "./include" REQUIRES edgehog-esp32-device nvs_flash) diff --git a/examples/edgehog_app/main/Kconfig.projbuild b/examples/edgehog_app/main/Kconfig.projbuild index 8cb963d..e3e76ce 100644 --- a/examples/edgehog_app/main/Kconfig.projbuild +++ b/examples/edgehog_app/main/Kconfig.projbuild @@ -20,15 +20,92 @@ menu "Edgehog example" -config WIFI_SSID - string "WiFi SSID" - default "myssid" +menu "Astarte device configuration" + +config CREDENTIALS_SECRET + string "Credentials secret" + default "" help - SSID (network name) for the example to connect to. + Astarte device credential secret, generated when the device is added on an Astarte + instance. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" +config DEVICE_ID + string "Device hardware ID" + default "" help - WiFi password (WPA or WPA2) for the example to use. + Astarte device hardware id, generated when the device is added on an Astarte instance. + +endmenu + +menu "WiFi configuration" + + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the demo to connect to. + + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the demo to use. + + choice WIFI_SAE_MODE + prompt "WPA3 SAE mode selection" + default ESP_WPA3_SAE_PWE_BOTH + help + Select mode for SAE as Hunt and Peck, H2E or both. + config ESP_WPA3_SAE_PWE_HUNT_AND_PECK + bool "HUNT AND PECK" + config ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT + bool "H2E" + config ESP_WPA3_SAE_PWE_BOTH + bool "BOTH" + endchoice + + config WIFI_PW_ID + string "PASSWORD IDENTIFIER" + depends on ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT|| ESP_WPA3_SAE_PWE_BOTH + default "" + help + password identifier for SAE H2E + + config ESP_MAXIMUM_RETRY + int "Maximum retry" + default 5 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP + is really inexistent. + + choice WIFI_SCAN_AUTH_MODE_THRESHOLD + prompt "WiFi Scan auth mode threshold" + default WIFI_AUTH_WPA2_PSK + help + The weakest authmode to accept in the scan mode. + This value defaults to WIFI_AUTH_WPA2_PSK incase password is present and + WIFI_AUTH_OPEN is used. + Please select WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK incase AP is operating in WEP/WPA + mode. + + config WIFI_AUTH_OPEN + bool "OPEN" + config WIFI_AUTH_WEP + bool "WEP" + config WIFI_AUTH_WPA_PSK + bool "WPA PSK" + config WIFI_AUTH_WPA2_PSK + bool "WPA2 PSK" + config WIFI_AUTH_WPA_WPA2_PSK + bool "WPA/WPA2 PSK" + config WIFI_AUTH_WPA3_PSK + bool "WPA3 PSK" + config WIFI_AUTH_WPA2_WPA3_PSK + bool "WPA2/WPA3 PSK" + config WIFI_AUTH_WAPI_PSK + bool "WAPI PSK" + endchoice + +endmenu + endmenu diff --git a/examples/edgehog_app/main/component.mk b/examples/edgehog_app/main/component.mk deleted file mode 100644 index 0012a3c..0000000 --- a/examples/edgehog_app/main/component.mk +++ /dev/null @@ -1,26 +0,0 @@ -# -# This file is part of Edgehog. -# -# Copyright 2021,2022 SECO Mind Srl -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# 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. -# -# SPDX-License-Identifier: Apache-2.0 -# - -# -# "main" pseudo-component makefile. -# -# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) -COMPONENT_ADD_INCLUDEDIRS += include -COMPONENT_SRCDIRS += src diff --git a/examples/edgehog_app/main/include/example_task.h b/examples/edgehog_app/main/include/example_task.h index 8de3cb3..11afdc2 100644 --- a/examples/edgehog_app/main/include/example_task.h +++ b/examples/edgehog_app/main/include/example_task.h @@ -21,6 +21,9 @@ #ifndef EXAMPLE_TASK_H #define EXAMPLE_TASK_H +#define ASTARTE_PARTITION_NAME "astarte" +#define EDGEHOG_PARTITION_NAME "edgehog" + /** * @brief Edgehog configuration task. * diff --git a/examples/edgehog_app/main/include/wifi.h b/examples/edgehog_app/main/include/wifi.h new file mode 100644 index 0000000..1ff791b --- /dev/null +++ b/examples/edgehog_app/main/include/wifi.h @@ -0,0 +1,12 @@ +/* + * (C) Copyright 2024, SECO Mind Srl + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef WIFI_H +#define WIFI_H + +void wifi_init_sta(void); + +#endif /* WIFI_H */ diff --git a/examples/edgehog_app/main/include/wifi_cfg.h b/examples/edgehog_app/main/include/wifi_cfg.h deleted file mode 100644 index 846cb37..0000000 --- a/examples/edgehog_app/main/include/wifi_cfg.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of Edgehog. - * - * Copyright 2023 SECO Mind Srl - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef WIFI_CFG_H -#define WIFI_CFG_H - -/** - * @brief Wifi module initialization function. - */ -void wifi_init(void); - -#endif /* WIFI_CFG_H */ diff --git a/examples/edgehog_app/main/main.c b/examples/edgehog_app/main/main.c index 7c210a2..c223447 100644 --- a/examples/edgehog_app/main/main.c +++ b/examples/edgehog_app/main/main.c @@ -24,13 +24,13 @@ #include #include "example_task.h" -#include "wifi_cfg.h" +#include "wifi.h" /************************************************ * Constants/Defines ***********************************************/ -static const char *TAG = "EDGEHOG_EXAMPLE_MAIN"; +static const char *TAG = "EXAMPLE_MAIN"; /************************************************ * Main function definition @@ -43,8 +43,13 @@ void app_main(void) ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } + ESP_ERROR_CHECK(ret); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init_partition(ASTARTE_PARTITION_NAME)); + ESP_ERROR_CHECK(nvs_flash_init_partition(EDGEHOG_PARTITION_NAME)); + + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); + wifi_init_sta(); ESP_LOGI(TAG, "NVS and WIFI initialization completed."); diff --git a/examples/edgehog_app/main/src/example_task.c b/examples/edgehog_app/main/src/example_task.c index bfec9f1..60c0bdc 100644 --- a/examples/edgehog_app/main/src/example_task.c +++ b/examples/edgehog_app/main/src/example_task.c @@ -23,6 +23,8 @@ #include #include +#include "astarte.h" +#include "astarte_device.h" #include "edgehog_device.h" #include "edgehog_event.h" @@ -30,8 +32,7 @@ * Constants and defines ***********************************************/ -#define NVS_PARTITION "nvs" -#define TAG "EDGEHOG_EXAMPLE_TASK" +#define TAG "EXAMPLE_TASK" /************************************************ * Static variables declarations @@ -46,9 +47,18 @@ static edgehog_device_handle_t edgehog_device; /** * @brief Initialize an Astarte device. * + * @param notify_handle Task to be notified when the device gets connected. * @return The Astarte device instance handle. */ -static astarte_device_handle_t astarte_device_sdk_init(void); +static astarte_device_handle_t astarte_device_sdk_init(TaskHandle_t notify_handle); +/** + * @brief Initialize an Edgehog device and starts the associated astarte device. + * + * @param arg Extra args. + * + * @return The Edgehog device instance handle. + */ +static edgehog_device_handle_t edgehog_device_init(astarte_device_handle_t astarte_handle); /** * @brief Handler for edgehog events. * @@ -84,49 +94,31 @@ static void astarte_disconnection_events_handler(astarte_device_disconnection_ev void edgehog_example_task(void *ctx) { - astarte_device_handle_t astarte_device = astarte_device_sdk_init(); - if (astarte_device == NULL) { - ESP_LOGE(TAG, "Failed to initialize the Astarte device"); - vTaskDelete(NULL); - } - astarte_err_t astarte_err = astarte_device_start(astarte_device); - if (astarte_err != ASTARTE_OK) { - ESP_LOGE(TAG, "Failed to start the Astarte device"); - vTaskDelete(NULL); - } - ESP_LOGI(TAG, "Astarte device started"); - - ESP_ERROR_CHECK(esp_event_handler_instance_register( - EDGEHOG_EVENTS, ESP_EVENT_ANY_ID, edgehog_event_handler, NULL, NULL)); - - edgehog_device_telemetry_config_t telemetry_config = { - .type = EDGEHOG_TELEMETRY_SYSTEM_STATUS, - .period_seconds = 3600, - }; - edgehog_device_config_t edgehog_conf = { - .astarte_device = astarte_device, - .partition_label = NVS_PARTITION, - .telemetry_config = &telemetry_config, - .telemetry_config_len = 1, - }; - edgehog_device = edgehog_device_new(&edgehog_conf); - if (edgehog_device == NULL) { - ESP_LOGE(TAG, "Failed to create the Edgehog device"); + TaskHandle_t current_task_handle = xTaskGetCurrentTaskHandle(); + astarte_device_handle_t astarte_handle = astarte_device_sdk_init(current_task_handle); + assert(astarte_handle); + edgehog_device_handle_t edgehog_device = edgehog_device_init(astarte_handle); + assert(edgehog_device); + + // Start the Astarte device + if (astarte_device_start(astarte_handle) != ASTARTE_OK) { + ESP_LOGE(TAG, "Failed starting the Astarte device."); vTaskDelete(NULL); } - ESP_ERROR_CHECK(edgehog_device_set_system_serial_number(edgehog_device, "serial_number_1")); - ESP_ERROR_CHECK(edgehog_device_set_system_part_number(edgehog_device, "part_number_1")); + // Wait until the Astarte device is connected and the current task gets notified + (void) ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + ESP_LOGI(TAG, "Astarte device started and connected, starting edgehog device..."); + // Start the Edgehog device edgehog_err_t edgehog_err = edgehog_device_start(edgehog_device); - if (edgehog_err != EDGEHOG_OK || edgehog_device == NULL) { - ESP_LOGE(TAG, "Failed to create the Edgehog device"); + if (edgehog_err != EDGEHOG_OK) { + ESP_LOGE(TAG, "Failed starting the Edgehog device"); vTaskDelete(NULL); } - ESP_LOGI(TAG, "Edgehog device started"); - while (1) { + while (true) { // In this example this task is running with the lower priority and does not starve // the mcu thanks to time slicing. // If in your code you want to run this task with a higher priority make sure not to @@ -138,15 +130,27 @@ void edgehog_example_task(void *ctx) * Static functions definitions ***********************************************/ -static astarte_device_handle_t astarte_device_sdk_init(void) +static astarte_device_handle_t astarte_device_sdk_init(TaskHandle_t notify_handle) { - astarte_credentials_use_nvs_storage(NVS_PARTITION); - astarte_credentials_init(); + // Set up storage for the Astarte device + if (astarte_credentials_use_nvs_storage(ASTARTE_PARTITION_NAME) != ASTARTE_OK) { + ESP_LOGE(TAG, "Failed setting NVS as storage defaults."); + return NULL; + } + + // Initialize the credentials for the Astarte device + if (astarte_credentials_init() != ASTARTE_OK) { + ESP_LOGE(TAG, "Failed initializing the Astarte credentials."); + return NULL; + } astarte_device_config_t astarte_device_cfg = { .connection_event_callback = astarte_connection_events_handler, .disconnection_event_callback = astarte_disconnection_events_handler, .data_event_callback = astarte_data_events_handler, + .callbacks_user_data = notify_handle, + .credentials_secret = CONFIG_CREDENTIALS_SECRET, + .hwid = CONFIG_DEVICE_ID, }; astarte_device_handle_t astarte_device = astarte_device_init(&astarte_device_cfg); @@ -155,12 +159,51 @@ static astarte_device_handle_t astarte_device_sdk_init(void) return NULL; } - char *astarte_device_encoded_id = astarte_device_get_encoded_id(astarte_device); - ESP_LOGI(TAG, "[APP] Encoded device ID: %s", astarte_device_encoded_id); - return astarte_device; } +static edgehog_device_handle_t edgehog_device_init(astarte_device_handle_t astarte_handle) +{ + // Setup the event handler for the Edgehog device + esp_err_t esp_err = esp_event_handler_instance_register( + EDGEHOG_EVENTS, ESP_EVENT_ANY_ID, edgehog_event_handler, NULL, NULL); + if (esp_err != ESP_OK) { + ESP_LOGE(TAG, "Failed registering the Edgehog event handler."); + return NULL; + } + + // Initialize the Edgehog device + edgehog_device_telemetry_config_t telemetry_config = { + .type = EDGEHOG_TELEMETRY_SYSTEM_STATUS, + .period_seconds = 3600, + }; + edgehog_device_config_t edgehog_conf = { + .astarte_device = astarte_handle, + .partition_label = EDGEHOG_PARTITION_NAME, + .telemetry_config = &telemetry_config, + .telemetry_config_len = 1, + }; + edgehog_device_handle_t edgehog_device = edgehog_device_new(&edgehog_conf); + if (!edgehog_device) { + ESP_LOGE(TAG, "Failed initializing the Edgehog device."); + return NULL; + } + + // Declare serial and part number for the example Edgehog device + esp_err = edgehog_device_set_system_serial_number(edgehog_device, "edgehog_example_sn_1"); + if (esp_err != ESP_OK) { + ESP_LOGE(TAG, "Failed setting the system serial number for Edgehog."); + return NULL; + } + esp_err + = edgehog_device_set_system_part_number(edgehog_device, "edgehog_example_part_number_1"); + if (esp_err != ESP_OK) { + ESP_LOGE(TAG, "Failed setting the system part number for Edgehog."); + return NULL; + } + return edgehog_device; +} + static void edgehog_event_handler( void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -188,6 +231,8 @@ static void edgehog_event_handler( static void astarte_connection_events_handler(astarte_device_connection_event_t *event) { ESP_LOGI(TAG, "Astarte device connected, session_present: %d", event->session_present); + // Unlocking the example task + xTaskNotifyGive(event->user_data); } static void astarte_data_events_handler(astarte_device_data_event_t *event) diff --git a/examples/edgehog_app/main/src/wifi.c b/examples/edgehog_app/main/src/wifi.c new file mode 100644 index 0000000..142d9e1 --- /dev/null +++ b/examples/edgehog_app/main/src/wifi.c @@ -0,0 +1,149 @@ +/* + * (C) Copyright 2024, SECO Mind Srl + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "wifi.h" + +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "freertos/task.h" + +#include "esp_event.h" +#include "esp_log.h" +#include "esp_system.h" +#include "esp_wifi.h" + +#include "lwip/err.h" +#include "lwip/sys.h" + +#define EXAMPLE_ESP_WIFI_SSID CONFIG_WIFI_SSID +#define EXAMPLE_ESP_WIFI_PASS CONFIG_WIFI_PASSWORD +#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY + +#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK +#define EXAMPLE_H2E_IDENTIFIER "" +#elif CONFIG_ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HASH_TO_ELEMENT +#define EXAMPLE_H2E_IDENTIFIER CONFIG_WIFI_PW_ID +#elif CONFIG_ESP_WPA3_SAE_PWE_BOTH +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH +#define EXAMPLE_H2E_IDENTIFIER CONFIG_WIFI_PW_ID +#endif +#if CONFIG_WIFI_AUTH_OPEN +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_WIFI_AUTH_WEP +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_WIFI_AUTH_WPA_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_WIFI_AUTH_WPA2_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_WIFI_AUTH_WPA_WPA2_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_WIFI_AUTH_WPA3_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_WIFI_AUTH_WPA2_WPA3_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_WIFI_AUTH_WAPI_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + +/* FreeRTOS event group to signal when we are connected*/ +static EventGroupHandle_t s_wifi_event_group; + +/* The event group allows multiple bits for each event, but we only care about two events: + * - we are connected to the AP with an IP + * - we failed to connect after the maximum amount of retries + */ +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static const char *TAG = "WIFI_STATION"; + +static int s_retry_num = 0; + +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 < EXAMPLE_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(TAG, "connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } +} + +void wifi_init_sta(void) +{ + s_wifi_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_netif_init()); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + 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 = EXAMPLE_ESP_WIFI_SSID, + .password = EXAMPLE_ESP_WIFI_PASS, + /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8). + * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value + * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to + * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. + */ + .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, + .sae_pwe_h2e = ESP_WIFI_SAE_MODE, +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER, +#endif + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + + /* 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_LOGI(TAG, "connected to ap SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID, + EXAMPLE_ESP_WIFI_PASS); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", EXAMPLE_ESP_WIFI_SSID, + EXAMPLE_ESP_WIFI_PASS); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } +} diff --git a/examples/edgehog_app/main/src/wifi_cfg.c b/examples/edgehog_app/main/src/wifi_cfg.c deleted file mode 100644 index a97da31..0000000 --- a/examples/edgehog_app/main/src/wifi_cfg.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * This file is part of Edgehog. - * - * Copyright 2023 SECO Mind Srl - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "wifi_cfg.h" - -#include -#include -#include -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) -#include -#endif - -#include "freertos/event_groups.h" -#include "freertos/task.h" - -/************************************************ - * Constants/Defines - ***********************************************/ - -#define TAG "EDGEHOG_EXAMPLE_WIFI_CFG" - -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - -/************************************************ - * Static functions declaration - ***********************************************/ - -/** - * @brief Wifi event handler for the ESP32 wifi. - * - * @details To be installed in an event loop. - * @param arg Remaining handler arguments. - * @param event_base event base. - * @param event_id event identifier. - * @param event_data event data. - * @return The status code, ASTARTE_OK if successful, otherwise an error code is returned. - */ -static void wifi_event_handler( - void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); - -/************************************************ - * Global functions definition - ***********************************************/ - -void wifi_init(void) -{ - wifi_event_group = xEventGroupCreate(); - - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - 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, &wifi_event_handler, NULL, &instance_any_id)); - ESP_ERROR_CHECK(esp_event_handler_instance_register( - IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip)); - - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - 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]", CONFIG_WIFI_SSID, "******"); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); - - 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(wifi_event_group); -} - -/************************************************ - * Static functions definitions - ***********************************************/ - -static void wifi_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) { - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { - ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; - ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - } -} diff --git a/examples/edgehog_app/sdkconfig.defaults b/examples/edgehog_app/sdkconfig.defaults index c1f6c1e..9505029 100644 --- a/examples/edgehog_app/sdkconfig.defaults +++ b/examples/edgehog_app/sdkconfig.defaults @@ -1,6 +1,7 @@ +# Do not include SoftAP in the build process. As we do not need to work as an Access Point. +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n + CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="edgehog_partition_example.csv" -CONFIG_ESP_TLS_INSECURE=y CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y -CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP=y diff --git a/idf_component.yml b/idf_component.yml index 80d2aa2..764497d 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -25,5 +25,5 @@ dependencies: # Required IDF version idf: ">=4.1" astarte-device-sdk-esp32: - version: "v1.2.0" + version: "v1.3.1" git: https://github.com/astarte-platform/astarte-device-sdk-esp32.git