Skip to content

Commit 7be8b6e

Browse files
committed
esp_rmaker_utils: Add helper functions for Wi-Fi and factory reset
Also added events for these.
1 parent 4628715 commit 7be8b6e

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

components/esp_rainmaker/include/esp_rmaker_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ typedef enum {
3939
RMAKER_EVENT_CLAIM_SUCCESSFUL,
4040
/** Self Claiming Failed */
4141
RMAKER_EVENT_CLAIM_FAILED,
42+
/** Node reboot has been triggered. The associated event data is the time in seconds
43+
* (type: uint8_t) after which the node will reboot. Note that this time may not be
44+
* accurate as the events are received asynchronously.*/
45+
RMAKER_EVENT_REBOOT,
46+
/** Wi-Fi credentials reset. Triggered after calling esp_rmaker_wifi_reset() */
47+
RMAKER_EVENT_WIFI_RESET,
48+
/** Node reset to factory defaults. Triggered after calling esp_rmaker_factory_reset() */
49+
RMAKER_EVENT_FACTORY_RESET
4250
} esp_rmaker_event_t;
4351

4452
/** ESP RainMaker Node information */

components/esp_rainmaker/include/esp_rmaker_utils.h

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,51 @@ typedef struct esp_rmaker_time_config {
2727

2828
/** Reboot the chip after a delay
2929
*
30-
* This API just starts an esp_timer and executes a reboot from that.
31-
* Useful if you want to reboot after a delay, to allow other tasks to finish
32-
* their operations (Eg. MQTT publish to indicate OTA success)
30+
* This API just starts a reboot timer and returns immediately.
31+
* The actual reboot is trigerred asynchronously in the timer callback.
32+
* This is useful if you want to reboot after a delay, to allow other tasks to finish
33+
* their operations (Eg. MQTT publish to indicate OTA success). The \ref RMAKER_EVENT_REBOOT
34+
* event is triggered when the reboot timer is started.
3335
*
34-
* @param[in] seconds Time in seconds after which the chip should reboot
36+
* @param[in] seconds Time in seconds after which the chip should reboot.
3537
*
36-
* @return ESP_OK on success
37-
* @return error on failure
38+
* @return ESP_OK on success.
39+
* @return error on failure.
3840
*/
3941
esp_err_t esp_rmaker_reboot(uint8_t seconds);
4042

43+
/** Reset Wi-Fi credentials and reboot
44+
*
45+
* This will reset just the Wi-Fi credentials and trigger a reboot.
46+
* This is useful when you want to keep all the entries in NVS memory
47+
* intact, but just change the Wi-Fi credentials. The \ref RMAKER_EVENT_WIFI_RESET
48+
* event is triggered after the reset.
49+
*
50+
* @note This function internally calls esp_rmaker_reboot() and returns
51+
* immediately. The reboot happens asynchronously.
52+
*
53+
* @param[in] seconds Time in seconds after which the chip should reboot.
54+
*
55+
* @return ESP_OK on success.
56+
* @return error on failure.
57+
*/
58+
esp_err_t esp_rmaker_wifi_reset(uint8_t seconds);
59+
60+
/** Reset to factory defaults and reboot
61+
*
62+
* This will clear entire NVS partition and trigger a reboot.
63+
* The \ref RMAKER_EVENT_FACTORY_RESET event is triggered after the reset.
64+
*
65+
* @note This function internally calls esp_rmaker_reboot() and returns.
66+
* The reboot happens asynchronously.
67+
*
68+
* @param[in] seconds Time in seconds after which the chip should reboot.
69+
*
70+
* @return ESP_OK on success.
71+
* @return error on failure.
72+
*/
73+
esp_err_t esp_rmaker_factory_reset(uint8_t seconds);
74+
4175
/** Initialize time synchronization
4276
*
4377
* This API initializes SNTP for time synchronization.

components/esp_rainmaker/src/core/esp_rmaker_utils.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
#include <stdint.h>
1515
#include <esp_timer.h>
1616
#include <esp_system.h>
17+
#include <esp_wifi.h>
18+
#include <nvs_flash.h>
19+
#include <esp_rmaker_core.h>
1720

21+
#include <esp_rmaker_internal.h>
1822

1923
static esp_timer_handle_t reboot_timer;
2024

@@ -33,8 +37,27 @@ esp_err_t esp_rmaker_reboot(uint8_t seconds)
3337
.dispatch_method = ESP_TIMER_TASK,
3438
.name = "rmaker_reboot_tm"
3539
};
40+
esp_err_t err = ESP_FAIL;
3641
if (esp_timer_create(&reboot_timer_conf, &reboot_timer) == ESP_OK) {
37-
return esp_timer_start_once(reboot_timer, seconds * 1000000U);
42+
err = esp_timer_start_once(reboot_timer, seconds * 1000000U);
3843
}
39-
return ESP_FAIL;
44+
if (err == ESP_OK) {
45+
esp_rmaker_post_event(RMAKER_EVENT_REBOOT, &seconds, sizeof(seconds));
46+
}
47+
return err;
48+
}
49+
50+
esp_err_t esp_rmaker_wifi_reset(uint8_t seconds)
51+
{
52+
esp_wifi_restore();
53+
esp_rmaker_post_event(RMAKER_EVENT_WIFI_RESET, NULL, 0);
54+
return esp_rmaker_reboot(seconds);
55+
}
56+
57+
esp_err_t esp_rmaker_factory_reset(uint8_t seconds)
58+
{
59+
nvs_flash_deinit();
60+
nvs_flash_erase();
61+
esp_rmaker_post_event(RMAKER_EVENT_FACTORY_RESET, NULL, 0);
62+
return esp_rmaker_reboot(seconds);
4063
}

0 commit comments

Comments
 (0)