Implemented a pairing-reset detection strategy

This is a "tricky" workaround due to the lack of physical buttons:

We can distinguish between the two "power up" modes:

- Power on: when the device receives power for the first time. Example:
swapping batteries, prroviding external power. This _DOES NOT_ reset the
pairing info.

- Reset pin: when the device is manually reset (by shorting the RESET
pin to GND), or when a new firmware is flashed. This _RESETS_ the
pairing info.
This commit is contained in:
rbaron 2022-12-10 12:37:29 +01:00
parent 8eb54106d5
commit 7b4e64cec7

View file

@ -1,4 +1,5 @@
#include <dk_buttons_and_leds.h>
#include <hal/nrf_power.h>
#include <math.h>
#include <prstlib/adc.h>
#include <prstlib/button.h>
@ -28,6 +29,18 @@ static struct zb_device_ctx dev_ctx;
static prst_sensors_t sensors;
static void maybe_erase_pairing_info() {
uint32_t reset_reason = nrf_power_resetreas_get(NRF_POWER);
// If we're resetting via the RESET pin (e.g.: reset pin shorting, firmware flashing).
if (reset_reason & 0x1) {
LOG_WRN("Manual reset / re-flashing detected - erasing pairing info");
zigbee_erase_persistent_storage(/*erase=*/true);
// It's a power-on cycle (e.g.: swapping battery, first boot).
} else {
LOG_INF("Power-on cycle - keeping pairing info");
}
}
ZB_ZCL_DECLARE_IDENTIFY_ATTRIB_LIST(
identify_attr_list,
&dev_ctx.identify_attr.identify_time);
@ -168,6 +181,8 @@ int main(void) {
RET_IF_ERR(prst_led_init());
RET_IF_ERR(prst_button_init());
maybe_erase_pairing_info();
register_factory_reset_button(FACTORY_RESET_BUTTON);
prst_zb_attrs_init(&dev_ctx);