From 7b4e64cec748224131df5e79c05bc13ae591de02 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sat, 10 Dec 2022 12:37:29 +0100 Subject: [PATCH] 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. --- code/nrf-connect/samples/zigbee/src/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index cab3057..2709b43 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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);