Skip to content

Commit b3da7b0

Browse files
committed
Merge branch 'example/factoryreset_light_wifi_prov' into 'main'
example: Add RainMaker factoryreset for the factoryreset button in light_wifi_prov example See merge request app-frameworks/esp-matter!884
2 parents e255773 + 9f93041 commit b3da7b0

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

examples/light_wifi_prov/main/app_driver.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212

1313
#include <esp_matter.h>
14+
#include <esp_rmaker_utils.h>
1415
#include "bsp/esp-bsp.h"
1516

1617
#include <app_priv.h>
@@ -195,12 +196,35 @@ app_driver_handle_t app_driver_light_init()
195196
#endif
196197
}
197198

199+
static bool perform_factory_reset = false;
200+
201+
static void button_factory_reset_pressed_cb(void *arg, void *data)
202+
{
203+
if (!perform_factory_reset) {
204+
ESP_LOGI(TAG, "Factory reset triggered. Release the button to start factory reset.");
205+
perform_factory_reset = true;
206+
}
207+
}
208+
209+
static void button_factory_reset_released_cb(void *arg, void *data)
210+
{
211+
if (perform_factory_reset) {
212+
ESP_LOGI(TAG, "Starting factory reset");
213+
// Do RainMaker factory reset immediately and wait for 10 seconds
214+
// so that the device can finish Matter factory reset.
215+
esp_rmaker_factory_reset(0, 10);
216+
esp_matter::factory_reset();
217+
perform_factory_reset = false;
218+
}
219+
}
220+
198221
app_driver_handle_t app_driver_button_init()
199222
{
200223
/* Initialize button */
201224
button_handle_t btns[BSP_BUTTON_NUM];
202225
ESP_ERROR_CHECK(bsp_iot_button_create(btns, NULL, BSP_BUTTON_NUM));
203226
ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_PRESS_DOWN, app_driver_button_toggle_cb, NULL));
204-
227+
ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_LONG_PRESS_HOLD, button_factory_reset_pressed_cb, NULL));
228+
ESP_ERROR_CHECK(iot_button_register_cb(btns[0], BUTTON_PRESS_UP, button_factory_reset_released_cb, NULL));
205229
return (app_driver_handle_t)btns[0];
206230
}

examples/light_wifi_prov/main/app_main.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <common_macros.h>
3030
#include "app-common/zap-generated/ids/Attributes.h"
3131
#include "app-common/zap-generated/ids/Clusters.h"
32+
#include "wifi_provisioning/manager.h"
3233
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
3334
#include <platform/ESP32/OpenthreadLauncher.h>
3435
#endif
@@ -212,8 +213,7 @@ extern "C" void app_main()
212213

213214
/* Initialize driver */
214215
app_driver_handle_t light_handle = app_driver_light_init();
215-
app_driver_handle_t button_handle = app_driver_button_init();
216-
app_reset_button_register(button_handle);
216+
app_driver_button_init();
217217

218218
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
219219
node::config_t node_config;
@@ -307,7 +307,10 @@ extern "C" void app_main()
307307
err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len);
308308
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialized the encrypted OTA, err: %d", err));
309309
#endif // CONFIG_ENABLE_ENCRYPTED_OTA
310-
if (esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) {
310+
// If Wi-Fi is provisioned and RainMaker user node mapping is done, deinitialize the BLE.
311+
bool is_wifi_provisioned = false;
312+
wifi_prov_mgr_is_provisioned(&is_wifi_provisioned);
313+
if (is_wifi_provisioned && esp_rmaker_user_node_mapping_get_state() == ESP_RMAKER_USER_MAPPING_DONE) {
311314
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
312315
}
313316

examples/light_wifi_prov/sdkconfig.defaults

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
5454
CONFIG_MBEDTLS_DYNAMIC_FREE_PEER_CERT=y
5555
CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y
5656
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y
57+
CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n
5758

5859
# Fix for Timer Overflows
5960
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=3120

0 commit comments

Comments
 (0)