Skip to content

Commit

Permalink
zigbee: Factory Reset fix - allows setting new PAN_ID randomly
Browse files Browse the repository at this point in the history
Clearing the value in pibcache is needed to unlock the picking
of a new PAN_ID.

Signed-off-by: Adam Zelik <[email protected]>
  • Loading branch information
AdamZelikNS authored and edmont committed Jun 5, 2024
1 parent 7bf524d commit 7e773f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions subsys/zigbee/lib/zigbee_app_utils/zigbee_app_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ extern struct zb_aps_device_key_pair_set_s *zb_secur_get_link_key_by_address(
struct factory_reset_context_t {
uint32_t button;
bool reset_done;
bool pibcache_pan_id_needs_reset;
struct k_timer timer;
};
static struct factory_reset_context_t factory_reset_context;
Expand Down Expand Up @@ -447,6 +448,10 @@ zb_ret_t zigbee_default_signal_handler(zb_bufid_t bufid)

if (zb_get_network_role() ==
ZB_NWK_DEVICE_TYPE_COORDINATOR) {
if (factory_reset_context.pibcache_pan_id_needs_reset) {
zigbee_pibcache_pan_id_clear();
factory_reset_context.pibcache_pan_id_needs_reset = false;
}
/* For coordinator node,
* start network formation.
*/
Expand Down Expand Up @@ -984,6 +989,7 @@ static void factory_reset_timer_expired(struct k_timer *timer_id)
* from ZBOSS scheduler context
*/
LOG_DBG("Schedule Factory Reset; stop timer; set factory_reset_done flag");
factory_reset_context.pibcache_pan_id_needs_reset = true;
ZB_SCHEDULE_APP_CALLBACK(zb_bdb_reset_via_local_action, 0);
k_timer_stop(timer_id);
factory_reset_context.reset_done = true;
Expand All @@ -998,6 +1004,7 @@ void register_factory_reset_button(uint32_t button)
{
factory_reset_context.button = button;
factory_reset_context.reset_done = false;
factory_reset_context.pibcache_pan_id_needs_reset = false;

k_timer_init(&factory_reset_context.timer, factory_reset_timer_expired, NULL);
}
Expand All @@ -1009,6 +1016,7 @@ void check_factory_reset_button(uint32_t button_state, uint32_t has_changed)

/* Reset flag indicating that Factory Reset was initiated */
factory_reset_context.reset_done = false;
factory_reset_context.pibcache_pan_id_needs_reset = false;

/* Start timer checking button press time */
k_timer_start(&factory_reset_context.timer,
Expand Down
11 changes: 11 additions & 0 deletions subsys/zigbee/osif/zb_nrf_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

/* Value that is returned while reading a single byte from the erased flash page .*/
#define FLASH_EMPTY_BYTE 0xFF
/* Broadcast Pan ID value */
#define ZB_BROADCAST_PAN_ID 0xFFFFU
/* The number of bytes to be checked before concluding that the ZBOSS NVRAM is not initialized. */
#define ZB_PAGE_INIT_CHECK_LEN 32

Expand Down Expand Up @@ -592,6 +594,15 @@ void zb_osif_abort(void)
}
}

uint32_t zigbee_pibcache_pan_id_clear(void)
{
/* For consistency with zb_nwk_nib_init(), the 0xFFFFU is used,
* i.e. ZB_BROADCAST_PAN_ID.
*/
ZB_PIBCACHE_PAN_ID() = ZB_BROADCAST_PAN_ID;
return ZB_BROADCAST_PAN_ID;
}

void zb_reset(zb_uint8_t param)
{
uint8_t reas = (uint8_t)SYS_REBOOT_COLD;
Expand Down
13 changes: 13 additions & 0 deletions subsys/zigbee/osif/zb_nrf_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ uint32_t zigbee_event_poll(uint32_t timeout_us);
*/
zb_bool_t zigbee_is_nvram_initialised(void);

/**@brief Clears the PAN_ID value held in the Zigbee PIB cache.
*
* @details The value set is consistent with the behavior of
* @c zb_nwk_nib_init() from Zigbee stack NWK layer.
*
* Function can be used to ensure that the PIB cache does not store any valid
* PAN_ID value in scenarios where the device is in "absent from the network"
* phase (not yet joined or has already left).
*
* @return The newly set PAN_ID value.
*/
uint32_t zigbee_pibcache_pan_id_clear(void);

#endif /* ZB_NRF_PLATFORM_H__ */

0 comments on commit 7e773f7

Please sign in to comment.