diff --git a/code/nrf-connect/samples/zigbee/CMakeLists.txt b/code/nrf-connect/samples/zigbee/CMakeLists.txt index d000625..9fac7f5 100644 --- a/code/nrf-connect/samples/zigbee/CMakeLists.txt +++ b/code/nrf-connect/samples/zigbee/CMakeLists.txt @@ -16,8 +16,9 @@ target_sources(app PRIVATE src/factory_reset.c src/prst_zb_attrs.c src/prst_zb_soil_moisture_defs.c + src/restart_handler.c ) add_subdirectory(../../prstlib prstlib) target_include_directories(app PRIVATE ../../prstlib/include) -target_link_libraries(app PUBLIC prstlib) \ No newline at end of file +target_link_libraries(app PUBLIC prstlib) diff --git a/code/nrf-connect/samples/zigbee/Kconfig b/code/nrf-connect/samples/zigbee/Kconfig index a06d770..5346f11 100644 --- a/code/nrf-connect/samples/zigbee/Kconfig +++ b/code/nrf-connect/samples/zigbee/Kconfig @@ -32,3 +32,7 @@ config PRST_ZB_FACTORY_RESET_VIA_RESET_PIN bool "Resetting via the reset pin will factory reset the device. Power cycling through battery replacement will not." endchoice # PRST_ZB_FACTORY_RESET_METHOD + +config PRST_ZB_RESTART_WATCHDOG_TIMEOUT_SEC + int "Duration after the device will restart the rejoin procedure if a network has not been successfully joined." + default 3600 diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index 12b428d..16ee57b 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -19,6 +19,7 @@ #include "prst_zb_attrs.h" #include "prst_zb_endpoint_defs.h" #include "prst_zb_soil_moisture_defs.h" +#include "restart_handler.h" LOG_MODULE_REGISTER(app, CONFIG_LOG_DEFAULT_LEVEL); @@ -128,9 +129,17 @@ void zboss_signal_handler(zb_bufid_t bufid) { case ZB_BDB_SIGNAL_STEERING: // New network. case ZB_BDB_SIGNAL_DEVICE_REBOOT: { // Previously joined network. LOG_DBG("Steering complete. Status: %d", status); - prst_led_flash(/*times=*/3); if (status == RET_OK) { + LOG_DBG("Steering successful. Status: %d", status); + prst_led_flash(/*times=*/3); k_timer_stop(&led_flashing_timer); + prst_restart_watchdog_stop(); + prst_led_off(); + } else { + LOG_DBG("Steering failed. Status: %d", status); + prst_led_flash(7); + prst_restart_watchdog_start(); + k_timer_stop(&led_flashing_timer); // Power saving prst_led_off(); } } @@ -139,6 +148,7 @@ void zboss_signal_handler(zb_bufid_t bufid) { break; case ZB_ZDO_SIGNAL_LEAVE: if (status == RET_OK) { + k_timer_start(&led_flashing_timer, K_NO_WAIT, K_SECONDS(1)); zb_zdo_signal_leave_params_t *leave_params = ZB_ZDO_SIGNAL_GET_PARAMS(sig_hndler, zb_zdo_signal_leave_params_t); LOG_INF("Network left (leave type: %d)", leave_params->leave_type); @@ -149,7 +159,7 @@ void zboss_signal_handler(zb_bufid_t bufid) { } case ZB_ZDO_SIGNAL_SKIP_STARTUP: { stack_initialised = true; - LOG_DBG("Will restart flashing"); + LOG_DBG("Started zigbee stack and waiting for connection to network."); k_timer_start(&led_flashing_timer, K_NO_WAIT, K_SECONDS(1)); break; } diff --git a/code/nrf-connect/samples/zigbee/src/restart_handler.c b/code/nrf-connect/samples/zigbee/src/restart_handler.c new file mode 100644 index 0000000..b864f69 --- /dev/null +++ b/code/nrf-connect/samples/zigbee/src/restart_handler.c @@ -0,0 +1,23 @@ +#include "restart_handler.h" + +#include +#include +#include + +LOG_MODULE_REGISTER(restart_handler, CONFIG_LOG_DEFAULT_LEVEL); + +static void restart_network_steering_cb(struct k_timer *timer) { + LOG_DBG("Restart handler expired. Restarting network steering."); + // If the device is not commissioned, the rejoin procedure is started. + user_input_indicate(); +} + +K_TIMER_DEFINE(restart_timer, restart_network_steering_cb, NULL); + +void prst_restart_watchdog_start() { + k_timer_start(&restart_timer, K_SECONDS(CONFIG_PRST_ZB_RESTART_WATCHDOG_TIMEOUT_SEC), K_MSEC(0)); +} + +void prst_restart_watchdog_stop() { + k_timer_stop(&restart_timer); +} diff --git a/code/nrf-connect/samples/zigbee/src/restart_handler.h b/code/nrf-connect/samples/zigbee/src/restart_handler.h new file mode 100644 index 0000000..5c3a8ad --- /dev/null +++ b/code/nrf-connect/samples/zigbee/src/restart_handler.h @@ -0,0 +1,7 @@ +#ifndef _PRST_ZB_RESTART_HANDLER_H_ +#define _PRST_ZB_RESTART_HANDLER_H_ + +void prst_restart_watchdog_start(); +void prst_restart_watchdog_stop(); + +#endif // _PRST_ZB_RESTART_HANDLER_H_