example: update light_wifi_prov to use network_provisioning and add thread support for it

This commit is contained in:
WanqQixiang
2025-04-24 14:43:12 +08:00
parent bd3f00bbd2
commit 850bd16974
23 changed files with 356 additions and 146 deletions
+14
View File
@@ -380,6 +380,20 @@ build_ext_plat_c6_thread_controller_examples:
- *build_esp32c6_wifi_thread_example - *build_esp32c6_wifi_thread_example
- *build_controller_otbr_example - *build_controller_otbr_example
build_zap_light_example:
extends:
- .build_examples_template
needs:
- job: build_image
optional: true
script:
- cd ${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip
- source ./scripts/activate.sh
- cd ${ESP_MATTER_PATH}/examples/zap_light
- idf.py set-target esp32c3 build
- idf.py set-target esp32h2 build
build_esp_matter_examples: build_esp_matter_examples:
extends: extends:
- .build_examples_template - .build_examples_template
+2 -3
View File
@@ -374,7 +374,7 @@ For more advanced BLE usage, you can use the external platform feature.
It also serves as a way to integrate custom BLE usage with Matter. It also serves as a way to integrate custom BLE usage with Matter.
Please refer to the `advance setup`_ section in the programming guide. Please refer to the `advance setup`_ section in the programming guide.
This has been demonstrated in the `blemesh_bridge`_ and `light_wifi_prov`_ examples. This has been demonstrated in the `blemesh_bridge`_ examples.
A1.16 Moving BSS Segments to PSRAM to Reduce Memory Usage A1.16 Moving BSS Segments to PSRAM to Reduce Memory Usage
@@ -411,7 +411,6 @@ Please check `#1123`_ for relevant discussion on Github issue.
.. _blecent_scan(): https://github.com/espressif/esp-idf/blob/b5ac4fbdf9e9fb320bb0a98ee4fbaa18f8566f37/examples/bluetooth/nimble/blecent/main/main.c#L435 .. _blecent_scan(): https://github.com/espressif/esp-idf/blob/b5ac4fbdf9e9fb320bb0a98ee4fbaa18f8566f37/examples/bluetooth/nimble/blecent/main/main.c#L435
.. _advance setup: https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#advanced-setup .. _advance setup: https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#advanced-setup
.. _blemesh_bridge: https://github.com/espressif/esp-matter/tree/main/examples/bridge_apps/blemesh_bridge .. _blemesh_bridge: https://github.com/espressif/esp-matter/tree/main/examples/bridge_apps/blemesh_bridge
.. _light_wifi_prov: https://github.com/espressif/esp-matter/tree/main/examples/light_wifi_prov#4-external-platform
.. _getting the repository: https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#getting-the-repository .. _getting the repository: https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#getting-the-repository
.. _requirements.txt: https://github.com/espressif/esp-matter/blob/main/requirements.txt .. _requirements.txt: https://github.com/espressif/esp-matter/blob/main/requirements.txt
.. _generate_setup_payload.py: https://github.com/project-chip/connectedhomeip/tree/master/src/setup_payload/python .. _generate_setup_payload.py: https://github.com/project-chip/connectedhomeip/tree/master/src/setup_payload/python
@@ -424,4 +423,4 @@ Please check `#1123`_ for relevant discussion on Github issue.
.. _`#1123`: https://github.com/espressif/esp-matter/issues/1123 .. _`#1123`: https://github.com/espressif/esp-matter/issues/1123
.. _`#1126`: https://github.com/espressif/esp-matter/issues/1126 .. _`#1126`: https://github.com/espressif/esp-matter/issues/1126
.. _`#1128`: https://github.com/espressif/esp-matter/issues/1128 .. _`#1128`: https://github.com/espressif/esp-matter/issues/1128
.. _`#13303`: https://github.com/project-chip/connectedhomeip/issues/13303 .. _`#13303`: https://github.com/project-chip/connectedhomeip/issues/13303
+4 -4
View File
@@ -11,9 +11,9 @@ examples/bridge_apps/blemesh_bridge:
examples/zap_light: examples/zap_light:
enable: enable:
- if: IDF_TARGET in ["esp32", "esp32h2"] - if: IDF_TARGET in [""]
temporary: true temporary: true
reason: the other targets are not tested yet reason: this should be compiled in Matter environment, another CI has been added
examples/light_switch: examples/light_switch:
enable: enable:
@@ -111,9 +111,9 @@ examples/ota_provider:
temporary: true temporary: true
reason: the other targets are not tested yet reason: the other targets are not tested yet
examples/light_wifi_prov: examples/light_network_prov:
enable: enable:
- if: IDF_TARGET in ["esp32s3", "esp32c3"] - if: IDF_TARGET in ["esp32s3", "esp32c3", "esp32h2"]
temporary: true temporary: true
reason: the other targets are not tested yet reason: the other targets are not tested yet
@@ -22,7 +22,7 @@ set(EXTRA_COMPONENT_DIRS
"${ESP_MATTER_PATH}/components" "${ESP_MATTER_PATH}/components"
${extra_components_dirs_append}) ${extra_components_dirs_append})
project(light-wifi-prov) project(light-network-prov)
# WARNING: This is just an example for using key for decrypting the encrypted OTA image # WARNING: This is just an example for using key for decrypting the encrypted OTA image
# Please do not use it as is. # Please do not use it as is.
@@ -8,10 +8,9 @@ See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/de
- ESP32-S3-DevKitM / ESP32-C3-DevKitM - ESP32-S3-DevKitM / ESP32-C3-DevKitM
- chip-tool (For Matter commissioning) - chip-tool (For Matter commissioning)
- Android RainMaker APP of version 3.4.1 or iOS RainMaker APP of version 3.2.0 (For RainMaker Provisioning) - Android RainMaker APP of version 3.5.0+ or iOS RainMaker APP of version 3.2.1+ (For RainMaker Network Provisioning)
- [ESP-IDF](https://github.com/espressif/esp-idf) on tag v5.2.2 - [ESP-IDF](https://github.com/espressif/esp-idf) on tag v5.2.2
- [ESP RainMaker](https://github.com/espressif/esp-rainmaker/tree/fd781295) on commit fd78129500aa1a8b2eee9e0dcc0720d7b14cd3dc - [RainMaker CLI](https://github.com/espressif/esp-rainmaker-cli)
- [ESP Secure Cert Manager](https://github.com/espressif/esp_secure_cert_mgr)
## 2. Additional Environment Setup ## 2. Additional Environment Setup
@@ -19,10 +18,16 @@ See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/de
This example will use [host driven claiming](https://rainmaker.espressif.com/docs/claiming/#host-driven-claiming) via the RainMaker CLI. This example will use [host driven claiming](https://rainmaker.espressif.com/docs/claiming/#host-driven-claiming) via the RainMaker CLI.
Install esp-rainmaker-cli and login your RainMaker account:
```
python3 -m pip install esp-rainmaker-cli
esp-rainmaker-cli login
```
Make sure your device is connected to the host machine, login into the CLI and execute this: Make sure your device is connected to the host machine, login into the CLI and execute this:
``` ```
$ cd $RMAKER_PATH/cli $ esp-rainmaker-cli claim --matter <port>
$ ./rainmaker.py claim --matter <port>
``` ```
The CLI will fetch the device certificates and flash them into the secure cert partition of your device. The certificates will be used for both the Matter device attestation verification and RainMaker MQTT connection. The CLI will fetch the device certificates and flash them into the secure cert partition of your device. The certificates will be used for both the Matter device attestation verification and RainMaker MQTT connection.
@@ -62,13 +67,11 @@ $ ./chip-tool pairing ble-wifi <node-id> <ssid> <passcode> <setup-pincode> <disc
**Note**: The `setup-picode` and `discriminator` is generated with the factory partition binary. You can find it in the `out/131b_2/<node-id>/<node-id>-onb_codes.csv`. **Note**: The `setup-picode` and `discriminator` is generated with the factory partition binary. You can find it in the `out/131b_2/<node-id>/<node-id>-onb_codes.csv`.
## 4. External platform ## 4. Additional BLE GATT Service and BLE Advertisements
This example uses [external platform](../common/secondary_ble_adv/). This example uses the BLEManagerImpl::ConfigureExtraServices() to configure the additional BLE GATT service for RainMaker Provisioning. The function should be called before start Matter stack.
In the external platform, `SetSecondaryXX()` APIs are added in the `BLEManagerImpl` class for setting up the secondary BLE advertisement and services. After you call these APIs of `BLEManagerImpl`, there will be an additional BLE advertisement and corresponding service for wifi_provisioning after you initialize the Matter stack. And you can also configure other BLE advertisements with the ble_gap_ext_adv_XX() APIs after the Matter BLE advertisement is started.
There are also some WiFi stack initialization changes to avoid duplicated Wi-Fi stack initialization of wifi_provisioning and Matter stack.
## 5. RainMaker Provisioning ## 5. RainMaker Provisioning
@@ -1,5 +1,5 @@
idf_component_register(SRC_DIRS "." "./wifi_prov_scheme" idf_component_register(SRC_DIRS "." "./network_prov_scheme"
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils" "./wifi_prov_scheme") PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils" "./network_prov_scheme")
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")
@@ -6,13 +6,12 @@
CONDITIONS OF ANY KIND, either express or implied. CONDITIONS OF ANY KIND, either express or implied.
*/ */
#include "esp_matter_attribute_utils.h"
#include "esp_rmaker_user_mapping.h"
#include <esp_err.h> #include <esp_err.h>
#include <esp_log.h> #include <esp_log.h>
#include <nvs_flash.h> #include <nvs_flash.h>
#include <esp_matter.h> #include <esp_matter.h>
#include <esp_matter_attribute_utils.h>
#include <esp_matter_console.h> #include <esp_matter_console.h>
#include <esp_matter_ota.h> #include <esp_matter_ota.h>
@@ -22,17 +21,18 @@
#include <esp_rmaker_schedule.h> #include <esp_rmaker_schedule.h>
#include <esp_rmaker_standard_devices.h> #include <esp_rmaker_standard_devices.h>
#include <esp_rmaker_standard_params.h> #include <esp_rmaker_standard_params.h>
#include <esp_rmaker_user_mapping.h>
#include <app_priv.h> #include <app_priv.h>
#include <app_reset.h> #include <app_reset.h>
#include <app_wifi.h> #include <app_network.h>
#include <common_macros.h> #include <common_macros.h>
#include "app-common/zap-generated/ids/Attributes.h" #include "app-common/zap-generated/ids/Attributes.h"
#include "app-common/zap-generated/ids/Clusters.h" #include "app-common/zap-generated/ids/Clusters.h"
#include "platform/CHIPDeviceEvent.h" #include "network_provisioning/manager.h"
#include "platform/PlatformManager.h" #include "platform/PlatformManager.h"
#include "wifi_provisioning/manager.h"
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/ThreadStackManager.h>
#include <platform/ESP32/OpenthreadLauncher.h> #include <platform/ESP32/OpenthreadLauncher.h>
#endif #endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
@@ -41,7 +41,7 @@
#include <app/server/CommissioningWindowManager.h> #include <app/server/CommissioningWindowManager.h>
#include <app/server/Server.h> #include <app/server/Server.h>
#include <wifi_prov_scheme/protocomm_matter_ble.h> #include <network_prov_scheme/protocomm_matter_ble.h>
static const char *TAG = "app_main"; static const char *TAG = "app_main";
uint16_t light_endpoint_id = 0; uint16_t light_endpoint_id = 0;
@@ -269,7 +269,7 @@ extern "C" void app_main()
ESP_LOGE(TAG, "Error create default event loop"); ESP_LOGE(TAG, "Error create default event loop");
return; return;
} }
app_wifi_init(); app_network_init();
esp_rmaker_config_t rainmaker_cfg = { esp_rmaker_config_t rainmaker_cfg = {
.enable_time_sync = false, .enable_time_sync = false,
}; };
@@ -297,18 +297,31 @@ extern "C" void app_main()
esp_rmaker_schedule_enable(); esp_rmaker_schedule_enable();
err = app_wifi_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_LIGHT, MFG_DATA_DEVICE_SUBTYPE_LIGHT); err = app_network_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_LIGHT, MFG_DATA_DEVICE_SUBTYPE_LIGHT);
err = app_wifi_start(POP_TYPE_MAC); err = app_network_start(POP_TYPE_MAC);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!"); ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!");
abort(); abort();
} }
/* Matter start */ /* Matter start */
err = esp_matter::start(app_event_cb); err = esp_matter::start(app_event_cb);
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
esp_openthread_platform_config_t ot_config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
set_openthread_platform_config(&ot_config);
// This will not really initiaize Thread stack as the thread stack has been initialzed in app_network.
// We call this function to pass the OpenThread instance to GenericThreadStackManagerImpl_OpenThread
// so that it can be used for SRP service registration and network commissioning driver.
chip::DeviceLayer::ThreadStackMgr().InitThreadStack();
#endif
/* Starting driver with default values */ /* Starting driver with default values */
app_driver_light_set_defaults(light_endpoint_id); app_driver_light_set_defaults(light_endpoint_id);
@@ -316,11 +329,14 @@ extern "C" void app_main()
err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len); err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len);
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err)); ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err));
#endif // CONFIG_ENABLE_ENCRYPTED_OTA #endif // CONFIG_ENABLE_ENCRYPTED_OTA
// If Wi-Fi is provisioned and RainMaker user node mapping is done, deinitialize the BLE. bool is_network_provisioned = false;
bool is_wifi_provisioned = false; #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
wifi_prov_mgr_is_provisioned(&is_wifi_provisioned); network_prov_mgr_is_wifi_provisioned(&is_network_provisioned);
if (is_wifi_provisioned && esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) { #else
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t){ chip::DeviceLayer::Internal::BLEMgr().Shutdown(); }); network_prov_mgr_is_thread_provisioned(&is_network_provisioned);
#endif
if (is_network_provisioned && esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) {
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { chip::DeviceLayer::Internal::BLEMgr().Shutdown(); });
} }
#if CONFIG_ENABLE_CHIP_SHELL #if CONFIG_ENABLE_CHIP_SHELL
@@ -10,16 +10,17 @@
#include <esp_log.h> #include <esp_log.h>
#include <esp_rmaker_utils.h> #include <esp_rmaker_utils.h>
#include <esp_wifi.h> #include <esp_wifi.h>
#include <esp_mac.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h> #include <freertos/event_groups.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <string.h> #include <string.h>
#include <esp_netif.h> #include <esp_netif.h>
#include <wifi_prov_scheme_matter_ble.h> #include <network_prov_scheme_matter_ble.h>
#include <wifi_provisioning/manager.h> #include <network_provisioning/manager.h>
#include <app_wifi.h> #include <app_network.h>
#include <esp_rmaker_core.h> #include <esp_rmaker_core.h>
#include <esp_timer.h> #include <esp_timer.h>
#include <nvs.h> #include <nvs.h>
@@ -28,9 +29,24 @@
#include <platform/PlatformManager.h> #include <platform/PlatformManager.h>
#include <qrcode.h> #include <qrcode.h>
ESP_EVENT_DEFINE_BASE(APP_WIFI_EVENT); #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
#include <esp_vfs_eventfd.h>
#include <esp_openthread.h>
#include <esp_openthread_cli.h>
#include <esp_openthread_lock.h>
#include <esp_openthread_netif_glue.h>
#include <esp_openthread_types.h>
static const char *TAG = "app_wifi"; #include <openthread/cli.h>
#include <openthread/instance.h>
#include <openthread/logging.h>
#include <openthread/tasklet.h>
#include <openthread/thread.h>
#endif
ESP_EVENT_DEFINE_BASE(APP_NETWORK_EVENT);
static const char *TAG = "app_network";
#define PROV_QR_VERSION "v1" #define PROV_QR_VERSION "v1"
@@ -43,9 +59,9 @@ static const char *TAG = "app_wifi";
#define POP_STR_SIZE 9 #define POP_STR_SIZE 9
static esp_timer_handle_t prov_stop_timer; static esp_timer_handle_t prov_stop_timer;
/* Timeout period in minutes */ /* Timeout period in minutes */
#define APP_WIFI_PROV_TIMEOUT_PERIOD 30 #define APP_NETWORK_PROV_TIMEOUT_PERIOD 30
/* Autofetch period in micro-seconds */ /* Autofetch period in micro-seconds */
static uint64_t prov_timeout_period = (APP_WIFI_PROV_TIMEOUT_PERIOD * 60 * 1000000LL); static uint64_t prov_timeout_period = (APP_NETWORK_PROV_TIMEOUT_PERIOD * 60 * 1000000LL);
#define APP_PROV_STOP_ON_CREDS_MISMATCH #define APP_PROV_STOP_ON_CREDS_MISMATCH
@@ -63,7 +79,7 @@ char esp_rainmaker_ascii_art[] =
static uint8_t *custom_mfg_data = NULL; static uint8_t *custom_mfg_data = NULL;
static size_t custom_mfg_data_len = 0; static size_t custom_mfg_data_len = 0;
esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype) esp_err_t app_network_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype)
{ {
int8_t mfg_data[] = {(int8_t)MFG_DATA_HEADER, MGF_DATA_APP_ID, MFG_DATA_VERSION, MFG_DATA_CUSTOMER_ID}; int8_t mfg_data[] = {(int8_t)MFG_DATA_HEADER, MGF_DATA_APP_ID, MFG_DATA_VERSION, MFG_DATA_CUSTOMER_ID};
size_t mfg_data_len = sizeof(mfg_data) + 4; // 4 bytes of device type, subtype, and extra-code size_t mfg_data_len = sizeof(mfg_data) + 4; // 4 bytes of device type, subtype, and extra-code
@@ -89,7 +105,7 @@ static esp_err_t qrcode_display(const char *text)
return esp_qrcode_generate(&cfg, text); return esp_qrcode_generate(&cfg, text);
} }
static void app_wifi_print_qr(const char *name, const char *pop, const char *transport) static void app_network_print_qr(const char *name, const char *pop, const char *transport)
{ {
if (!name || !transport) { if (!name || !transport) {
ESP_LOGW(TAG, "Cannot generate QR code payload. Data missing."); ESP_LOGW(TAG, "Cannot generate QR code payload. Data missing.");
@@ -111,7 +127,7 @@ static void app_wifi_print_qr(const char *name, const char *pop, const char *tra
qrcode_display(payload); qrcode_display(payload);
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL,
payload); payload);
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_QR_DISPLAY, payload, strlen(payload) + 1, portMAX_DELAY); esp_event_post(APP_NETWORK_EVENT, APP_NETWORK_EVENT_QR_DISPLAY, payload, strlen(payload) + 1, portMAX_DELAY);
} }
/* Event handler for catching system events */ /* Event handler for catching system events */
@@ -120,12 +136,13 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
static int retries = 0; static int retries = 0;
static int failed_cnt = 0; static int failed_cnt = 0;
if (event_base == WIFI_PROV_EVENT) { if (event_base == NETWORK_PROV_EVENT) {
switch (event_id) { switch (event_id) {
case WIFI_PROV_START: case NETWORK_PROV_START:
ESP_LOGI(TAG, "Provisioning started"); ESP_LOGI(TAG, "Provisioning started");
break; break;
case WIFI_PROV_CRED_RECV: { #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
case NETWORK_PROV_WIFI_CRED_RECV: {
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data; wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
ESP_LOGI(TAG, ESP_LOGI(TAG,
"Received Wi-Fi credentials" "Received Wi-Fi credentials"
@@ -133,35 +150,55 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
(const char *)wifi_sta_cfg->ssid, (const char *)wifi_sta_cfg->password); (const char *)wifi_sta_cfg->ssid, (const char *)wifi_sta_cfg->password);
break; break;
} }
case WIFI_PROV_CRED_FAIL: { case NETWORK_PROV_WIFI_CRED_FAIL: {
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data; network_prov_wifi_sta_fail_reason_t *reason = (network_prov_wifi_sta_fail_reason_t *)event_data;
ESP_LOGE(TAG, ESP_LOGE(TAG,
"Provisioning failed!\n\tReason : %s" "Provisioning failed!\n\tReason : %s"
"\n\tPlease reset to factory and retry provisioning", "\n\tPlease reset to factory and retry provisioning",
(*reason == WIFI_PROV_STA_AUTH_ERROR) ? "Wi-Fi station authentication failed" (*reason == NETWORK_PROV_WIFI_STA_AUTH_ERROR) ? "Wi-Fi station authentication failed"
: "Wi-Fi access-point not found"); : "Wi-Fi access-point not found");
retries++; retries++;
if (retries >= 5) { if (retries >= 5) {
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials"); ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials");
wifi_prov_mgr_reset_sm_state_on_failure(); network_prov_mgr_reset_wifi_sm_state_on_failure();
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY); esp_event_post(APP_NETWORK_EVENT, APP_NETWORK_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY);
ESP_LOGW(TAG, "Failed to connect with provisioned AP, please reset to provisioning manually"); ESP_LOGW(TAG, "Failed to connect with provisioned AP, please reset to provisioning manually");
retries = 0; retries = 0;
} }
break; break;
} }
case WIFI_PROV_CRED_SUCCESS: case NETWORK_PROV_WIFI_CRED_SUCCESS:
ESP_LOGI(TAG, "Provisioning successful"); ESP_LOGI(TAG, "Provisioning successful");
retries = 0; retries = 0;
break; break;
case WIFI_PROV_END: #endif // CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
case NETWORK_PROV_THREAD_DATASET_RECV: {
break;
}
case NETWORK_PROV_THREAD_DATASET_FAIL: {
retries++;
if (retries >= 5) {
ESP_LOGI(TAG, "Failed to connect with provisioned network, reseting provisioned dataset");
network_prov_mgr_reset_thread_sm_state_on_failure();
esp_event_post(APP_NETWORK_EVENT, APP_NETWORK_EVENT_PROV_RESTART, NULL, 0, portMAX_DELAY);
retries = 0;
}
break;
}
case NETWORK_PROV_THREAD_DATASET_SUCCESS:
ESP_LOGI(TAG, "Provisioning successful");
retries = 0;
break;
#endif
case NETWORK_PROV_END:
if (prov_stop_timer) { if (prov_stop_timer) {
esp_timer_stop(prov_stop_timer); esp_timer_stop(prov_stop_timer);
esp_timer_delete(prov_stop_timer); esp_timer_delete(prov_stop_timer);
prov_stop_timer = NULL; prov_stop_timer = NULL;
} }
/* De-initialize manager once provisioning is finished */ /* De-initialize manager once provisioning is finished */
wifi_prov_mgr_deinit(); network_prov_mgr_deinit();
break; break;
default: default:
break; break;
@@ -178,21 +215,61 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
ESP_LOGE(TAG, "Received incorrect PoP or invalid security params! event: %d", (int)event_id); ESP_LOGE(TAG, "Received incorrect PoP or invalid security params! event: %d", (int)event_id);
if (++failed_cnt >= 5) { if (++failed_cnt >= 5) {
/* stop provisioning for security reasons */ /* stop provisioning for security reasons */
wifi_prov_mgr_stop_provisioning(); network_prov_mgr_stop_provisioning();
ESP_LOGW(TAG, ESP_LOGW(TAG,
"Max PoP attempts reached! Provisioning disabled for security reasons. Please reboot device " "Max PoP attempts reached! Provisioning disabled for security reasons. Please reboot device "
"to restart provisioning"); "to restart provisioning");
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_CRED_MISMATCH, NULL, 0, portMAX_DELAY); esp_event_post(APP_NETWORK_EVENT, APP_NETWORK_EVENT_PROV_CRED_MISMATCH, NULL, 0, portMAX_DELAY);
} }
break; break;
default: default:
break; break;
} }
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
esp_wifi_connect(); esp_wifi_connect();
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
} }
} }
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
static esp_netif_t* init_openthread_netif(const esp_openthread_platform_config_t* config)
{
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
esp_netif_t* netif = esp_netif_new(&cfg);
assert(netif != NULL);
ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));
return netif;
}
static void ot_task_worker(void* aContext)
{
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
/* Initialize the OpenThread stack */
ESP_ERROR_CHECK(esp_openthread_init(&config));
#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
/* The OpenThread log level directly matches ESP log level */
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
#endif
esp_netif_t *openthread_netif = init_openthread_netif(&config);
/* Initialize the esp_netif bindings */
esp_netif_set_default_netif(openthread_netif);
/* Run the main loop */
esp_openthread_launch_mainloop();
/* Clean up */
esp_netif_destroy(openthread_netif);
esp_openthread_netif_glue_deinit();
esp_vfs_eventfd_unregister();
vTaskDelete(NULL);
}
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
/* Free random_bytes after use only if function returns ESP_OK */ /* Free random_bytes after use only if function returns ESP_OK */
static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len) static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len)
@@ -225,7 +302,7 @@ static esp_err_t read_random_bytes_from_nvs(uint8_t **random_bytes, size_t *len)
} }
static char *custom_pop; static char *custom_pop;
esp_err_t app_wifi_set_custom_pop(const char *pop) esp_err_t app_network_set_custom_pop(const char *pop)
{ {
/* NULL PoP is not allowed here. Use POP_TYPE_NONE instead. */ /* NULL PoP is not allowed here. Use POP_TYPE_NONE instead. */
if (!pop) { if (!pop) {
@@ -251,9 +328,9 @@ static esp_err_t get_device_service_name(char *service_name, size_t max)
const char *ssid_prefix = "PROV"; const char *ssid_prefix = "PROV";
size_t nvs_random_size = 0; size_t nvs_random_size = 0;
if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 3) { if ((read_random_bytes_from_nvs(&nvs_random, &nvs_random_size) != ESP_OK) || nvs_random_size < 3) {
uint8_t eth_mac[6]; uint8_t mac_addr[6];
esp_wifi_get_mac(WIFI_IF_STA, eth_mac); esp_read_mac(mac_addr, ESP_MAC_BASE);
snprintf(service_name, max, "%s_%02x%02x%02x", ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]); snprintf(service_name, max, "%s_%02x%02x%02x", ssid_prefix, mac_addr[3], mac_addr[4], mac_addr[5]);
} else { } else {
snprintf(service_name, max, "%s_%02x%02x%02x", ssid_prefix, nvs_random[nvs_random_size - 3], snprintf(service_name, max, "%s_%02x%02x%02x", ssid_prefix, nvs_random[nvs_random_size - 3],
nvs_random[nvs_random_size - 2], nvs_random[nvs_random_size - 1]); nvs_random[nvs_random_size - 2], nvs_random[nvs_random_size - 1]);
@@ -264,7 +341,7 @@ static esp_err_t get_device_service_name(char *service_name, size_t max)
return ESP_OK; return ESP_OK;
} }
static char *get_device_pop(app_wifi_pop_type_t pop_type) static char *get_device_pop(app_network_pop_type_t pop_type)
{ {
if (pop_type == POP_TYPE_NONE) { if (pop_type == POP_TYPE_NONE) {
return NULL; return NULL;
@@ -282,10 +359,10 @@ static char *get_device_pop(app_wifi_pop_type_t pop_type)
} }
if (pop_type == POP_TYPE_MAC) { if (pop_type == POP_TYPE_MAC) {
uint8_t eth_mac[6]; uint8_t mac_addr[6];
esp_err_t err = esp_wifi_get_mac(WIFI_IF_STA, eth_mac); esp_err_t err = esp_read_mac(mac_addr, ESP_MAC_BASE);
if (err == ESP_OK) { if (err == ESP_OK) {
snprintf(pop, POP_STR_SIZE, "%02x%02x%02x%02x", eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]); snprintf(pop, POP_STR_SIZE, "%02x%02x%02x%02x", mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
return pop; return pop;
} else { } else {
ESP_LOGE(TAG, "Failed to get MAC address to generate PoP."); ESP_LOGE(TAG, "Failed to get MAC address to generate PoP.");
@@ -311,8 +388,9 @@ pop_err:
return NULL; return NULL;
} }
void app_wifi_init(void) void app_network_init(void)
{ {
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
wifi_init_config_t cfg; wifi_init_config_t cfg;
esp_err_t err = esp_netif_init(); esp_err_t err = esp_netif_init();
if (err != ESP_OK) { if (err != ESP_OK) {
@@ -336,35 +414,47 @@ void app_wifi_init(void)
ESP_LOGE(TAG, "Failed to initialize esp_wifi"); ESP_LOGE(TAG, "Failed to initialize esp_wifi");
return; return;
} }
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
/* Initialize TCP/IP */
esp_netif_init();
esp_vfs_eventfd_config_t eventfd_config = {
.max_fds = 3,
};
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
xTaskCreate(ot_task_worker, "ot_task", 6144, xTaskGetCurrentTaskHandle(), 5, NULL);
#endif
/* Register our event handler for Wi-Fi, IP and Provisioning related events */ /* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(NETWORK_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
#ifdef APP_PROV_STOP_ON_CREDS_MISMATCH #ifdef APP_PROV_STOP_ON_CREDS_MISMATCH
ESP_ERROR_CHECK( ESP_ERROR_CHECK(
esp_event_handler_register(PROTOCOMM_SECURITY_SESSION_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); esp_event_handler_register(PROTOCOMM_SECURITY_SESSION_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
#endif #endif
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); 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_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
} }
static void app_wifi_prov_stop(void *priv) static void app_network_prov_stop(void *priv)
{ {
ESP_LOGW(TAG, "Provisioning timed out. Please reboot device to restart provisioning."); ESP_LOGW(TAG, "Provisioning timed out. Please reboot device to restart provisioning.");
wifi_prov_mgr_stop_provisioning(); network_prov_mgr_stop_provisioning();
esp_event_post(APP_WIFI_EVENT, APP_WIFI_EVENT_PROV_TIMEOUT, NULL, 0, portMAX_DELAY); esp_event_post(APP_NETWORK_EVENT, APP_NETWORK_EVENT_PROV_TIMEOUT, NULL, 0, portMAX_DELAY);
} }
esp_err_t app_wifi_start_timer(void) esp_err_t app_network_start_timer(void)
{ {
if (prov_timeout_period == 0) { if (prov_timeout_period == 0) {
return ESP_OK; return ESP_OK;
} }
esp_timer_create_args_t prov_stop_timer_conf = {.callback = app_wifi_prov_stop, esp_timer_create_args_t prov_stop_timer_conf = {.callback = app_network_prov_stop,
.arg = NULL, .arg = NULL,
.dispatch_method = ESP_TIMER_TASK, .dispatch_method = ESP_TIMER_TASK,
.name = "app_wifi_prov_stop_tm"}; .name = "app_wifi_prov_stop_tm"};
if (esp_timer_create(&prov_stop_timer_conf, &prov_stop_timer) == ESP_OK) { if (esp_timer_create(&prov_stop_timer_conf, &prov_stop_timer) == ESP_OK) {
esp_timer_start_once(prov_stop_timer, prov_timeout_period); esp_timer_start_once(prov_stop_timer, prov_timeout_period);
ESP_LOGI(TAG, "Provisioning will auto stop after %d minute(s).", APP_WIFI_PROV_TIMEOUT_PERIOD); ESP_LOGI(TAG, "Provisioning will auto stop after %d minute(s).", APP_NETWORK_PROV_TIMEOUT_PERIOD);
return ESP_OK; return ESP_OK;
} else { } else {
ESP_LOGE(TAG, "Failed to create Provisioning auto stop timer."); ESP_LOGE(TAG, "Failed to create Provisioning auto stop timer.");
@@ -372,11 +462,11 @@ esp_err_t app_wifi_start_timer(void)
return ESP_FAIL; return ESP_FAIL;
} }
esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type) esp_err_t app_network_start(app_network_pop_type_t pop_type)
{ {
/* Configuration for the provisioning manager */ /* Configuration for the provisioning manager */
wifi_prov_mgr_config_t config = { network_prov_mgr_config_t config = {
.scheme = wifi_prov_scheme_matter_ble, .scheme = network_prov_scheme_matter_ble,
/* Any default scheme specific event handler that you would /* Any default scheme specific event handler that you would
* like to choose. Since our example application requires * like to choose. Since our example application requires
@@ -386,16 +476,21 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
* appropriate scheme specific event handler allows the manager * appropriate scheme specific event handler allows the manager
* to take care of this automatically. This can be set to * to take care of this automatically. This can be set to
* WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/ * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
.scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE, .scheme_event_handler = NETWORK_PROV_EVENT_HANDLER_NONE,
}; };
/* Initialize provisioning manager with the /* Initialize provisioning manager with the
* configuration parameters set above */ * configuration parameters set above */
ESP_ERROR_CHECK(wifi_prov_mgr_init(config)); ESP_ERROR_CHECK(network_prov_mgr_init(config));
bool provisioned = false; bool provisioned = false;
/* Let's find out if the device is provisioned */ /* Let's find out if the device is provisioned */
wifi_prov_mgr_is_provisioned(&provisioned); #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
network_prov_mgr_is_wifi_provisioned(&provisioned);
#endif
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
network_prov_mgr_is_thread_provisioned(&provisioned);
#endif
/* If device is not yet provisioned start provisioning service */ /* If device is not yet provisioned start provisioning service */
if (!provisioned) { if (!provisioned) {
ESP_LOGI(TAG, "Starting provisioning"); ESP_LOGI(TAG, "Starting provisioning");
@@ -420,7 +515,7 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
* using X25519 key exchange and proof of possession (pop) and AES-CTR * using X25519 key exchange and proof of possession (pop) and AES-CTR
* for encryption/decryption of messages. * for encryption/decryption of messages.
*/ */
wifi_prov_security_t security = WIFI_PROV_SECURITY_1; network_prov_security_t security = NETWORK_PROV_SECURITY_1;
/* Do we want a proof-of-possession (ignored if Security 0 is selected): /* Do we want a proof-of-possession (ignored if Security 0 is selected):
* - this should be a string with length > 0 * - this should be a string with length > 0
@@ -445,13 +540,13 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
/* 12th and 13th bit will be replaced by internal bits. */ /* 12th and 13th bit will be replaced by internal bits. */
0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
}; };
esp_err_t err = wifi_prov_scheme_matter_ble_set_service_uuid(custom_service_uuid, sizeof(custom_service_uuid)); esp_err_t err = network_prov_scheme_matter_ble_set_service_uuid(custom_service_uuid, sizeof(custom_service_uuid));
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "wifi_prov_scheme_ble_set_service_uuid failed %d", err); ESP_LOGE(TAG, "wifi_prov_scheme_ble_set_service_uuid failed %d", err);
return err; return err;
} }
if (custom_mfg_data) { if (custom_mfg_data) {
err = wifi_prov_scheme_matter_ble_set_mfg_data(custom_mfg_data, custom_mfg_data_len); err = network_prov_scheme_matter_ble_set_mfg_data(custom_mfg_data, custom_mfg_data_len);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set mfg data, err=0x%x", err); ESP_LOGE(TAG, "Failed to set mfg data, err=0x%x", err);
return err; return err;
@@ -459,22 +554,31 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
} }
/* Start provisioning service */ /* Start provisioning service */
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key)); ESP_ERROR_CHECK(network_prov_mgr_start_provisioning(security, pop, service_name, service_key));
/* Print QR code for provisioning */ /* Print QR code for provisioning */
app_wifi_print_qr(service_name, pop, PROV_TRANSPORT_BLE); app_network_print_qr(service_name, pop, PROV_TRANSPORT_BLE);
ESP_LOGI(TAG, "Provisioning Started. Name : %s, POP : %s", service_name, pop ? pop : "<null>"); ESP_LOGI(TAG, "Provisioning Started. Name : %s, POP : %s", service_name, pop ? pop : "<null>");
if (pop) { if (pop) {
free(pop); free(pop);
} }
app_wifi_start_timer(); app_network_start_timer();
} else { } else {
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA"); ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
/* We don't need the manager as device is already provisioned, /* We don't need the manager as device is already provisioned,
* so let's release it's resources */ * so let's release it's resources */
wifi_prov_mgr_deinit(); network_prov_mgr_deinit();
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_connect(); esp_wifi_connect();
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
#ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
esp_openthread_lock_acquire(portMAX_DELAY);
otInstance* instance = esp_openthread_get_instance();
(void)otIp6SetEnabled(instance, true);
(void)otThreadSetEnabled(instance, true);
esp_openthread_lock_release();
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
esp_rmaker_start(); esp_rmaker_start();
} }
if (custom_mfg_data) { if (custom_mfg_data) {
@@ -24,19 +24,19 @@
#define MFG_DATA_DEVICE_EXTRA_CODE 0x00 #define MFG_DATA_DEVICE_EXTRA_CODE 0x00
/** ESP RainMaker Event Base */ /** ESP RainMaker Event Base */
ESP_EVENT_DECLARE_BASE(APP_WIFI_EVENT); ESP_EVENT_DECLARE_BASE(APP_NETWORK_EVENT);
/** App Wi-Fir Events */ /** App Wi-Fir Events */
typedef enum { typedef enum {
/** QR code available for display. Associated data is the NULL terminated QR payload. */ /** QR code available for display. Associated data is the NULL terminated QR payload. */
APP_WIFI_EVENT_QR_DISPLAY = 1, APP_NETWORK_EVENT_QR_DISPLAY = 1,
/** Provisioning timed out */ /** Provisioning timed out */
APP_WIFI_EVENT_PROV_TIMEOUT, APP_NETWORK_EVENT_PROV_TIMEOUT,
/** Provisioning has restarted due to failures (Invalid SSID/Passphrase) */ /** Provisioning has restarted due to failures (Invalid SSID/Passphrase) */
APP_WIFI_EVENT_PROV_RESTART, APP_NETWORK_EVENT_PROV_RESTART,
/** Provisioning closed due to invalid credentials */ /** Provisioning closed due to invalid credentials */
APP_WIFI_EVENT_PROV_CRED_MISMATCH, APP_NETWORK_EVENT_PROV_CRED_MISMATCH,
} app_wifi_event_t; } app_network_event_t;
/** Types of Proof of Possession */ /** Types of Proof of Possession */
typedef enum { typedef enum {
@@ -52,25 +52,45 @@ typedef enum {
* Set a custom PoP using app_wifi_set_custom_pop() first. * Set a custom PoP using app_wifi_set_custom_pop() first.
*/ */
POP_TYPE_CUSTOM POP_TYPE_CUSTOM
} app_wifi_pop_type_t; } app_network_pop_type_t;
/** Initialize Wi-Fi #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
#include <esp_openthread_types.h>
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_NATIVE, \
}
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_NONE, \
}
#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \
{ \
.storage_partition_name = "nvs", \
.netif_queue_size = 10, \
.task_queue_size = 10, \
}
#endif // CONFIG_ESP_RMAKER_NETWORK_OVER_THREAD
/** Initialize network
* *
* This initializes Wi-Fi and the Wi-Fi provisioning manager * This initializes network and the network provisioning manager
*/ */
void app_wifi_init(); void app_network_init();
/** Start Wi-Fi /** Start network
* *
* This will start provisioning if the node is not provisioned and will connect to Wi-Fi * This will start provisioning if the node is not provisioned and will connect to Wi-Fi/Thread
* if node is provisioned. Function will return successfully only after Wi-Fi is connect * if node is provisioned. Function will return successfully only after Wi-Fi/Thread is connect
* *
* @param[in] pop_type The type for Proof of Possession (PoP) pin * @param[in] pop_type The type for Proof of Possession (PoP) pin
* *
* @return ESP_OK on success (Wi-Fi connected). * @return ESP_OK on success (Wi-Fi/Thread connected).
* @return error in case of failure. * @return error in case of failure.
*/ */
esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type); esp_err_t app_network_start(app_network_pop_type_t pop_type);
/** Set custom manufacturing data /** Set custom manufacturing data
* *
@@ -84,7 +104,7 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type);
* @return ESP_OK on success. * @return ESP_OK on success.
* @return error in case of failure. * @return error in case of failure.
*/ */
esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype); esp_err_t app_network_set_custom_mfg_data(uint16_t device_type, uint8_t device_subtype);
/** Set custom PoP /** Set custom PoP
* *
@@ -96,4 +116,4 @@ esp_err_t app_wifi_set_custom_mfg_data(uint16_t device_type, uint8_t device_subt
* @return ESP_OK on success. * @return ESP_OK on success.
* @return error in case of failure. * @return error in case of failure.
*/ */
esp_err_t app_wifi_set_custom_pop(const char *pop); esp_err_t app_network_set_custom_pop(const char *pop);
@@ -4,6 +4,6 @@ dependencies:
espressif/led_strip: espressif/led_strip:
version: "^2.0.0" version: "^2.0.0"
espressif/esp_rainmaker: espressif/esp_rainmaker:
version: "1.3.0" version: "^1.4.2"
espressif/qrcode: espressif/qrcode:
version: "^0.1.0~2" version: "^0.1.0~2"
@@ -6,7 +6,7 @@
CONDITIONS OF ANY KIND, either express or implied. CONDITIONS OF ANY KIND, either express or implied.
*/ */
#include "wifi_prov_scheme_matter_ble.h" #include "network_prov_scheme_matter_ble.h"
#include "protocomm_matter_ble.h" #include "protocomm_matter_ble.h"
#include <cstdlib> #include <cstdlib>
@@ -17,7 +17,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#define TAG "WIFI_PROV" #define TAG "NETWORK_PROV"
static uint8_t custom_service_uuid[BLE_UUID128_VAL_LENGTH]; static uint8_t custom_service_uuid[BLE_UUID128_VAL_LENGTH];
static bool custom_service_uuid_set = false; static bool custom_service_uuid_set = false;
@@ -62,7 +62,7 @@ static esp_err_t prov_stop(protocomm_t *pc)
return protocomm_matter_ble_stop(pc); return protocomm_matter_ble_stop(pc);
} }
esp_err_t wifi_prov_scheme_matter_ble_set_service_uuid(uint8_t *uuid, size_t uuid_size) esp_err_t network_prov_scheme_matter_ble_set_service_uuid(uint8_t *uuid, size_t uuid_size)
{ {
if (uuid_size != sizeof(custom_service_uuid)) { if (uuid_size != sizeof(custom_service_uuid)) {
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
@@ -72,7 +72,7 @@ esp_err_t wifi_prov_scheme_matter_ble_set_service_uuid(uint8_t *uuid, size_t uui
return ESP_OK; return ESP_OK;
} }
esp_err_t wifi_prov_scheme_matter_ble_set_mfg_data(uint8_t *mfg_data, size_t mfg_data_len) esp_err_t network_prov_scheme_matter_ble_set_mfg_data(uint8_t *mfg_data, size_t mfg_data_len)
{ {
if (custom_manufacturer_data) { if (custom_manufacturer_data) {
free(custom_manufacturer_data); free(custom_manufacturer_data);
@@ -180,10 +180,13 @@ static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, ui
return ESP_OK; return ESP_OK;
} }
const wifi_prov_scheme_t wifi_prov_scheme_matter_ble = {.prov_start = prov_start, const network_prov_scheme_t network_prov_scheme_matter_ble = {.prov_start = prov_start,
.prov_stop = prov_stop, .prov_stop = prov_stop,
.new_config = new_config, .new_config = new_config,
.delete_config = delete_config, .delete_config = delete_config,
.set_config_service = set_config_service, .set_config_service = set_config_service,
.set_config_endpoint = set_config_endpoint, .set_config_endpoint = set_config_endpoint,
.wifi_mode = WIFI_MODE_STA}; #ifdef CONFIG_ESP_RMAKER_NETWORK_OVER_WIFI
.wifi_mode = WIFI_MODE_STA
#endif
};
@@ -0,0 +1,29 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <protocomm.h>
#include <protocomm_ble.h>
#include <stdint.h>
#include <esp_err.h>
#include <network_provisioning/manager.h>
extern const network_prov_scheme_t network_prov_scheme_matter_ble;
#define NETWORK_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM { \
.event_cb = network_prov_scheme_ble_event_cb_free_btdm, \
.user_data = NULL \
}
void network_prov_scheme_matter_ble_event_cb_free_btdm(void *user_data, network_prov_cb_event_t event, void *event_data);
esp_err_t network_prov_scheme_matter_ble_set_service_uuid(uint8_t *uuid, size_t uuid_size);
esp_err_t network_prov_scheme_matter_ble_set_mfg_data(uint8_t *mfg_data, size_t mfg_data_len);
@@ -60,6 +60,9 @@ CONFIG_MBEDTLS_ERROR_STRINGS=n
# Fix for Timer Overflows # Fix for Timer Overflows
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120 CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120
# Decrease Max endpoints
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=2
# Additional configurations # Additional configurations
CONFIG_ESP_RMAKER_USER_ID_CHECK=y CONFIG_ESP_RMAKER_USER_ID_CHECK=y
CONFIG_ESP_RMAKER_NO_CLAIM=y CONFIG_ESP_RMAKER_NO_CLAIM=y
@@ -0,0 +1,48 @@
CONFIG_IDF_TARGET="esp32h2"
# Flash size optimization
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y
CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS=y
# Increase IPv6 address number in netif structure
CONFIG_LWIP_IPV6_NUM_ADDRESSES=8
# Disable Route hook
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=n
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=n
# Enable OpenThread
CONFIG_OPENTHREAD_ENABLED=y
CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n
CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y
CONFIG_OPENTHREAD_DNS64_CLIENT=y
CONFIG_ENABLE_THREAD_TELEMETRY=y
# esp_matter::start() will not initialize Thread stack as we initialize it in app_network.
CONFIG_ESP_MATTER_ENABLE_OPENTHREAD=n
# Use platform mDNS
CONFIG_USE_MINIMAL_MDNS=n
# Disable Matter over Wi-Fi
CONFIG_ENABLE_WIFI_STATION=n
CONFIG_ENABLE_WIFI_TELEMETRY=n
# Disable ECDSA Peripheral
CONFIG_USE_ESP32_ECDSA_PERIPHERAL=n
CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=n
# Memory Optimization
CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE=512
CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE=256
CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE=256
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=2
# BSP Configuration
CONFIG_BUTTON_PERIOD_TIME_MS=20
CONFIG_BUTTON_LONG_PRESS_TIME_MS=5000
CONFIG_BSP_BUTTONS_NUM=1
CONFIG_BSP_BUTTON_1_GPIO=9
CONFIG_BSP_LED_TYPE_RGB=y
CONFIG_BSP_LED_RGB_GPIO=8
@@ -1,29 +0,0 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <protocomm.h>
#include <protocomm_ble.h>
#include <stdint.h>
#include <esp_err.h>
#include <wifi_provisioning/manager.h>
extern const wifi_prov_scheme_t wifi_prov_scheme_matter_ble;
#define WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM { \
.event_cb = wifi_prov_scheme_ble_event_cb_free_btdm, \
.user_data = NULL \
}
void wifi_prov_scheme_matter_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event_t event, void *event_data);
esp_err_t wifi_prov_scheme_matter_ble_set_service_uuid(uint8_t *uuid, size_t uuid_size);
esp_err_t wifi_prov_scheme_matter_ble_set_mfg_data(uint8_t *mfg_data, size_t mfg_data_len);