Skip to content

Commit

Permalink
Merge branch 'example/factoryreset_light_wifi_prov' into 'main'
Browse files Browse the repository at this point in the history
example: Add RainMaker factoryreset for the factoryreset button in light_wifi_prov example

See merge request app-frameworks/esp-matter!884
  • Loading branch information
chshu committed Sep 19, 2024
2 parents e255773 + 9f93041 commit b3da7b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
26 changes: 25 additions & 1 deletion examples/light_wifi_prov/main/app_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string.h>

#include <esp_matter.h>
#include <esp_rmaker_utils.h>
#include "bsp/esp-bsp.h"

#include <app_priv.h>
Expand Down Expand Up @@ -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];
}
9 changes: 6 additions & 3 deletions examples/light_wifi_prov/main/app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <common_macros.h>
#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 <platform/ESP32/OpenthreadLauncher.h>
#endif
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions examples/light_wifi_prov/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3da7b0

Please sign in to comment.