From cfc0a08806fd779a750e1ff496758f5d28a7dec7 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Wed, 18 Sep 2024 11:26:10 +0800 Subject: [PATCH 1/2] example: Add RainMaker factoryreset for the factoryreset button in light_wifi_prov example --- examples/light_wifi_prov/main/app_driver.cpp | 26 +++++++++++++++++++- examples/light_wifi_prov/main/app_main.cpp | 9 ++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/examples/light_wifi_prov/main/app_driver.cpp b/examples/light_wifi_prov/main/app_driver.cpp index cc356b06a..f66b37819 100644 --- a/examples/light_wifi_prov/main/app_driver.cpp +++ b/examples/light_wifi_prov/main/app_driver.cpp @@ -11,6 +11,7 @@ #include #include +#include #include "bsp/esp-bsp.h" #include @@ -195,12 +196,35 @@ app_driver_handle_t app_driver_light_init() #endif } +static bool perform_factory_reset = false; + +static void button_factory_reset_pressed_cb(void *arg, void *data) +{ + if (!perform_factory_reset) { + ESP_LOGI(TAG, "Factory reset triggered. Release the button to start factory reset."); + perform_factory_reset = true; + } +} + +static void button_factory_reset_released_cb(void *arg, void *data) +{ + if (perform_factory_reset) { + ESP_LOGI(TAG, "Starting factory reset"); + // Do RainMaker factory reset immediately and wait for 10 seconds + // so that the device can finish Matter factory reset. + esp_rmaker_factory_reset(0, 10); + esp_matter::factory_reset(); + perform_factory_reset = false; + } +} + app_driver_handle_t app_driver_button_init() { /* Initialize button */ button_handle_t btns[BSP_BUTTON_NUM]; ESP_ERROR_CHECK(bsp_iot_button_create(btns, NULL, BSP_BUTTON_NUM)); ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_PRESS_DOWN, app_driver_button_toggle_cb, NULL)); - + ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_LONG_PRESS_HOLD, button_factory_reset_pressed_cb, NULL)); + ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_PRESS_UP, button_factory_reset_released_cb, NULL)); return (app_driver_handle_t)btns[0]; } diff --git a/examples/light_wifi_prov/main/app_main.cpp b/examples/light_wifi_prov/main/app_main.cpp index 9d5bbf21e..130bf6891 100644 --- a/examples/light_wifi_prov/main/app_main.cpp +++ b/examples/light_wifi_prov/main/app_main.cpp @@ -29,6 +29,7 @@ #include #include "app-common/zap-generated/ids/Attributes.h" #include "app-common/zap-generated/ids/Clusters.h" +#include "wifi_provisioning/manager.h" #if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include #endif @@ -212,8 +213,7 @@ extern "C" void app_main() /* Initialize driver */ app_driver_handle_t light_handle = app_driver_light_init(); - app_driver_handle_t button_handle = app_driver_button_init(); - app_reset_button_register(button_handle); + app_driver_button_init(); /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ node::config_t node_config; @@ -307,7 +307,10 @@ extern "C" void app_main() 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)); #endif // CONFIG_ENABLE_ENCRYPTED_OTA - if (esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) { + // If Wi-Fi is provisioned and RainMaker user node mapping is done, deinitialize the BLE. + bool is_wifi_provisioned = false; + wifi_prov_mgr_is_provisioned(&is_wifi_provisioned); + if (is_wifi_provisioned && esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) { chip::DeviceLayer::Internal::BLEMgr().Shutdown(); } From 9f93041173baa278dccbf568f53a9d7030e3ee5d Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Wed, 18 Sep 2024 17:40:45 +0800 Subject: [PATCH 2/2] example: Disable CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE for light_wifi_prov example --- examples/light_wifi_prov/sdkconfig.defaults | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/light_wifi_prov/sdkconfig.defaults b/examples/light_wifi_prov/sdkconfig.defaults index e40a3dc6c..707dc404b 100644 --- a/examples/light_wifi_prov/sdkconfig.defaults +++ b/examples/light_wifi_prov/sdkconfig.defaults @@ -54,6 +54,7 @@ CONFIG_MBEDTLS_DYNAMIC_BUFFER=y CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y +CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n # Fix for Timer Overflows CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120