From a15bd37a1a86100e27856daa9079f50a27521955 Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Sun, 28 Apr 2024 14:43:38 +0200 Subject: [PATCH 01/10] Added zigbee ota configuration to main. All relevant sections are behind a build toggle which can be activated in Kconfig. --- code/nrf-connect/samples/zigbee/src/main.c | 93 +++++++++++++++++++++- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index b123080..379fd29 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -16,6 +16,12 @@ #include #include +#if CONFIG_ZIGBEE_FOTA +#include +#include +#include +#endif + #include "debug_counters.h" #include "factory_reset.h" #include "flash_fs.h" @@ -115,10 +121,67 @@ PRST_ZB_DECLARE_ENDPOINT( PRST_ZIGBEE_ENDPOINT, app_template_clusters); +#ifndef CONFIG_ZIGBEE_FOTA ZBOSS_DECLARE_DEVICE_CTX_1_EP( + app_template_ctx, + app_template_ep); +#else + +extern zb_af_endpoint_desc_t zigbee_fota_client_ep; + +ZBOSS_DECLARE_DEVICE_CTX_2_EP( app_template_ctx, + zigbee_fota_client_ep, app_template_ep); +static void confirm_image(void) +{ + if (!boot_is_img_confirmed()) { + int ret = boot_write_img_confirmed(); + + if (ret) { + LOG_ERR("Couldn't confirm image: %d", ret); + } else { + LOG_INF("Marked image as OK"); + } + } +} + +static void ota_evt_handler(const struct zigbee_fota_evt *evt) +{ + switch (evt->id) { + case ZIGBEE_FOTA_EVT_PROGRESS: + // dk_set_led(OTA_ACTIVITY_LED, evt->dl.progress % 2); + prst_led_flash(1); + break; + case ZIGBEE_FOTA_EVT_FINISHED: + LOG_INF("Reboot application."); + /* Power on unused sections of RAM to allow MCUboot to use it. */ + if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) { + power_up_unused_ram(); + } + sys_reboot(SYS_REBOOT_COLD); + break; + + case ZIGBEE_FOTA_EVT_ERROR: + LOG_ERR("OTA image transfer failed."); + break; + } +} + +static void zcl_device_cb(zb_bufid_t bufid) +{ + zb_zcl_device_callback_param_t *device_cb_param = + ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t); + + if (device_cb_param->device_cb_id == ZB_ZCL_OTA_UPGRADE_VALUE_CB_ID) { + zigbee_fota_zcl_cb(bufid); + } else { + device_cb_param->status = RET_NOT_IMPLEMENTED; + } +} +#endif /* CONFIG_ZIGBEE_FOTA */ + void identify_cb(zb_bufid_t bufid) { LOG_DBG("Remote identify command called"); prst_led_flash(15); @@ -185,6 +248,12 @@ void update_sensors_cb(zb_uint8_t arg) { void zboss_signal_handler(zb_bufid_t bufid) { // See zigbee_default_signal_handler() for all available signals. + +#ifdef CONFIG_ZIGBEE_FOTA + /* Pass signal to the OTA client implementation. */ + zigbee_fota_signal_handler(bufid); +#endif /* CONFIG_ZIGBEE_FOTA */ + zb_zdo_app_signal_hdr_t *sig_hndler = NULL; zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, /*sg_p=*/&sig_hndler); zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid); @@ -314,15 +383,31 @@ int main(void) { prst_zb_attrs_init(&dev_ctx); - ZB_AF_REGISTER_DEVICE_CTX(&app_template_ctx); - prst_led_flash(2); k_msleep(100); - ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(PRST_ZIGBEE_ENDPOINT, identify_cb); - zigbee_configure_sleepy_behavior(/*enable=*/true); power_down_unused_ram(); + +#ifdef CONFIG_ZIGBEE_FOTA + /* Initialize Zigbee FOTA download service. */ + zigbee_fota_init(ota_evt_handler); + + /* Mark the current firmware as valid. */ + confirm_image(); + + /* Register callback for handling ZCL commands. */ + ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb); +#endif /* CONFIG_ZIGBEE_FOTA */ + + ZB_AF_REGISTER_DEVICE_CTX(&app_template_ctx); + + ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(PRST_ZIGBEE_ENDPOINT, identify_cb); + +#ifdef CONFIG_ZIGBEE_FOTA + ZB_AF_SET_IDENTIFY_NOTIFICATION_HANDLER(CONFIG_ZIGBEE_FOTA_ENDPOINT, identify_cb); +#endif + zigbee_enable(); prst_debug_counters_increment("main_finish"); From 61533da1b6a40f947dc7cdb40d6b63affe2f16c7 Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Sun, 28 Apr 2024 14:49:23 +0200 Subject: [PATCH 02/10] Added separate ota config file based on the prj file. Parameters are based on the zigbee light switch example from nrf-connect toolkit. clang-format with vs code. --- code/nrf-connect/samples/zigbee/prj_ota.conf | 89 ++++++++++++++++++++ code/nrf-connect/samples/zigbee/src/main.c | 89 ++++++++++---------- 2 files changed, 132 insertions(+), 46 deletions(-) create mode 100644 code/nrf-connect/samples/zigbee/prj_ota.conf diff --git a/code/nrf-connect/samples/zigbee/prj_ota.conf b/code/nrf-connect/samples/zigbee/prj_ota.conf new file mode 100644 index 0000000..4a10f2f --- /dev/null +++ b/code/nrf-connect/samples/zigbee/prj_ota.conf @@ -0,0 +1,89 @@ + +CONFIG_LOG=y +CONFIG_PWM=y +CONFIG_I2C=y +CONFIG_ADC=y +CONFIG_GPIO=y + +CONFIG_USE_SEGGER_RTT=y +CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096 + +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y + +CONFIG_SERIAL=n + +CONFIG_HEAP_MEM_POOL_SIZE=2048 +CONFIG_MAIN_THREAD_PRIORITY=7 + +CONFIG_ZIGBEE=y +CONFIG_ZIGBEE_APP_UTILS=y +CONFIG_ZIGBEE_ROLE_END_DEVICE=y + +# This example requires more workqueue stack +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 + +# Enable nRF ECB driver +CONFIG_CRYPTO=y +CONFIG_CRYPTO_NRF_ECB=y +CONFIG_CRYPTO_INIT_PRIORITY=80 + +# Networking +CONFIG_NET_IPV6=n +CONFIG_NET_IPV6_MLD=n +CONFIG_NET_IPV6_NBR_CACHE=n +CONFIG_NET_IPV6_RA_RDNSS=n +CONFIG_NET_IP_ADDR_CHECK=n +CONFIG_NET_UDP=n + +## +## ZigBee Channel Selection +## +# Get Zigbee to scan every channel. +CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_MULTI=y +# By default only scans channel 11 (ZigBee2MQTT default) and 15 (ZHA default). +# Comment to scan all channels - this will make pairing consume more energy. +# CONFIG_ZIGBEE_CHANNEL_MASK=0x8800 + +# Uncomment to set a specific channel - this will make pairing more energy efficient. +# CONFIG_ZIGBEE_CHANNEL=15 + +# Enable API for powering down unused RAM parts. +# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/nrf/ug_zigbee_configuring.html#power-saving-during-sleep +CONFIG_RAM_POWER_DOWN_LIBRARY=y + +# LittleFS. +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_FILE_SYSTEM=y +CONFIG_FILE_SYSTEM_LITTLEFS=y + +CONFIG_WATCHDOG=y +CONFIG_HWINFO=y +CONFIG_ASSERT=y + +# Uncomment for debug log level. +# CONFIG_LOG_DEFAULT_LEVEL=4 + +# Factory reset method selection. Only hardware revision 2.0.0+ has button SW1. Earlier +# revisions must select a different method. See Kconfig for options. +# CONFIG_PRST_ZB_FACTORY_RESET_VIA_SW1=y +# Disable factory reset. + +# OTA update configuration. This is based on the zigbee light switch example from the nrf-connect toolkit. +CONFIG_ZIGBEE_FOTA=y +CONFIG_ZIGBEE_FOTA_ENDPOINT=5 +CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" # Increase this version for generating an update for the devices. +CONFIG_ZIGBEE_FOTA_COMMENT="b_parasite" + +# Ensure an MCUboot-compatible binary is generated. +CONFIG_IMG_MANAGER=y +CONFIG_STREAM_FLASH=y +CONFIG_DFU_TARGET_MCUBOOT=y +CONFIG_IMG_ERASE_PROGRESSIVELY=y +CONFIG_ZIGBEE_FOTA_PROGRESS_EVT=y + +# Increase the number of RX buffers +CONFIG_NRF_802154_RX_BUFFERS=32 diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index 379fd29..68c9d13 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -17,9 +17,9 @@ #include #if CONFIG_ZIGBEE_FOTA -#include -#include #include +#include +#include #endif #include "debug_counters.h" @@ -123,8 +123,8 @@ PRST_ZB_DECLARE_ENDPOINT( #ifndef CONFIG_ZIGBEE_FOTA ZBOSS_DECLARE_DEVICE_CTX_1_EP( - app_template_ctx, - app_template_ep); + app_template_ctx, + app_template_ep); #else extern zb_af_endpoint_desc_t zigbee_fota_client_ep; @@ -134,51 +134,48 @@ ZBOSS_DECLARE_DEVICE_CTX_2_EP( zigbee_fota_client_ep, app_template_ep); -static void confirm_image(void) -{ - if (!boot_is_img_confirmed()) { - int ret = boot_write_img_confirmed(); +static void confirm_image(void) { + if (!boot_is_img_confirmed()) { + int ret = boot_write_img_confirmed(); - if (ret) { - LOG_ERR("Couldn't confirm image: %d", ret); - } else { - LOG_INF("Marked image as OK"); - } - } + if (ret) { + LOG_ERR("Couldn't confirm image: %d", ret); + } else { + LOG_INF("Marked image as OK"); + } + } } -static void ota_evt_handler(const struct zigbee_fota_evt *evt) -{ - switch (evt->id) { +static void ota_evt_handler(const struct zigbee_fota_evt *evt) { + switch (evt->id) { case ZIGBEE_FOTA_EVT_PROGRESS: - // dk_set_led(OTA_ACTIVITY_LED, evt->dl.progress % 2); + // dk_set_led(OTA_ACTIVITY_LED, evt->dl.progress % 2); prst_led_flash(1); - break; + break; case ZIGBEE_FOTA_EVT_FINISHED: - LOG_INF("Reboot application."); - /* Power on unused sections of RAM to allow MCUboot to use it. */ - if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) { - power_up_unused_ram(); - } - sys_reboot(SYS_REBOOT_COLD); - break; + LOG_INF("Reboot application."); + /* Power on unused sections of RAM to allow MCUboot to use it. */ + if (IS_ENABLED(CONFIG_RAM_POWER_DOWN_LIBRARY)) { + power_up_unused_ram(); + } + sys_reboot(SYS_REBOOT_COLD); + break; case ZIGBEE_FOTA_EVT_ERROR: - LOG_ERR("OTA image transfer failed."); - break; - } + LOG_ERR("OTA image transfer failed."); + break; + } } -static void zcl_device_cb(zb_bufid_t bufid) -{ - zb_zcl_device_callback_param_t *device_cb_param = - ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t); +static void zcl_device_cb(zb_bufid_t bufid) { + zb_zcl_device_callback_param_t *device_cb_param = + ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t); - if (device_cb_param->device_cb_id == ZB_ZCL_OTA_UPGRADE_VALUE_CB_ID) { - zigbee_fota_zcl_cb(bufid); - } else { - device_cb_param->status = RET_NOT_IMPLEMENTED; - } + if (device_cb_param->device_cb_id == ZB_ZCL_OTA_UPGRADE_VALUE_CB_ID) { + zigbee_fota_zcl_cb(bufid); + } else { + device_cb_param->status = RET_NOT_IMPLEMENTED; + } } #endif /* CONFIG_ZIGBEE_FOTA */ @@ -250,8 +247,8 @@ void zboss_signal_handler(zb_bufid_t bufid) { // See zigbee_default_signal_handler() for all available signals. #ifdef CONFIG_ZIGBEE_FOTA - /* Pass signal to the OTA client implementation. */ - zigbee_fota_signal_handler(bufid); + /* Pass signal to the OTA client implementation. */ + zigbee_fota_signal_handler(bufid); #endif /* CONFIG_ZIGBEE_FOTA */ zb_zdo_app_signal_hdr_t *sig_hndler = NULL; @@ -390,14 +387,14 @@ int main(void) { power_down_unused_ram(); #ifdef CONFIG_ZIGBEE_FOTA - /* Initialize Zigbee FOTA download service. */ - zigbee_fota_init(ota_evt_handler); + /* Initialize Zigbee FOTA download service. */ + zigbee_fota_init(ota_evt_handler); - /* Mark the current firmware as valid. */ - confirm_image(); + /* Mark the current firmware as valid. */ + confirm_image(); - /* Register callback for handling ZCL commands. */ - ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb); + /* Register callback for handling ZCL commands. */ + ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb); #endif /* CONFIG_ZIGBEE_FOTA */ ZB_AF_REGISTER_DEVICE_CTX(&app_template_ctx); From bb2f3cc3c2c3f64a86b2e08101e27c7f87e17592 Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Wed, 1 May 2024 10:53:07 +0200 Subject: [PATCH 03/10] Removed obsolete leftover comment from ota sample. --- code/nrf-connect/samples/zigbee/src/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index 68c9d13..0efad06 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -149,7 +149,6 @@ static void confirm_image(void) { static void ota_evt_handler(const struct zigbee_fota_evt *evt) { switch (evt->id) { case ZIGBEE_FOTA_EVT_PROGRESS: - // dk_set_led(OTA_ACTIVITY_LED, evt->dl.progress % 2); prst_led_flash(1); break; case ZIGBEE_FOTA_EVT_FINISHED: From 76a76de52c9f3f3238f9e0d4e5b6263acd89ccbf Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Wed, 1 May 2024 16:53:23 +0200 Subject: [PATCH 04/10] Added basic information to zigbee readme file for ota updates. --- code/nrf-connect/samples/zigbee/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/nrf-connect/samples/zigbee/README.md b/code/nrf-connect/samples/zigbee/README.md index 939fdd4..c663ecc 100644 --- a/code/nrf-connect/samples/zigbee/README.md +++ b/code/nrf-connect/samples/zigbee/README.md @@ -39,6 +39,13 @@ Available options in `Kconfig`. Notable options: * `CONFIG_PRST_ZB_SLEEP_DURATION_SEC`: amount of time (in seconds) the device sleeps between reading all sensors and updating its clusters * `CONFIG_PRST_ZB_PARENT_POLL_INTERVAL_SEC`: amount of time (in seconds) the device waits between polling its parent for data +### Over the air (OTA) updates +The firmware is prepared for accepting over the air updates. You need to set the toggle `CONFIG_ZIGBEE_OTA` in the `prj.conf` to `y` to compile OTA functionality in the firmware. + +Once the toggle is set, during compilation there will be a *.zigbee file generated as well, which is the firmware update file. This needs to be provided to your zigbee coordinator, so that it can be discovered by your deployed and paired parasites. The version `CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION` needs to be increased to allow the device to recognize the firmware file as an actual update. + +Some helpful information on how to provide an firmware update via Home Assistant and ZHA integration can be found in the [Home Assistant forums](https://community.home-assistant.io/t/zha-ota-firmware-update-upgrade/488960). + ## Home Assistant Integration This firmware sample has only been tested with Home Assistant, using one of the following integrations. From 4bb9a97daaa991e39e368956cc04d98a36d7bee0 Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Wed, 1 May 2024 16:57:24 +0200 Subject: [PATCH 05/10] Added ota config parameters to main prj.conf. Toggle default is set to `n` for now. Removed obsolete prj_ota.conf file. --- code/nrf-connect/samples/zigbee/prj.conf | 17 ++++ code/nrf-connect/samples/zigbee/prj_ota.conf | 89 -------------------- 2 files changed, 17 insertions(+), 89 deletions(-) delete mode 100644 code/nrf-connect/samples/zigbee/prj_ota.conf diff --git a/code/nrf-connect/samples/zigbee/prj.conf b/code/nrf-connect/samples/zigbee/prj.conf index 9e20278..321ca6f 100644 --- a/code/nrf-connect/samples/zigbee/prj.conf +++ b/code/nrf-connect/samples/zigbee/prj.conf @@ -70,3 +70,20 @@ CONFIG_ASSERT=y # Factory reset method selection. Only hardware revision 2.0.0+ has button SW1. Earlier # revisions must select a different method. See Kconfig for options. # CONFIG_PRST_ZB_FACTORY_RESET_VIA_SW1=y +## OTA section + +# OTA update configuration. This is based on the zigbee light switch example from the nrf-connect toolkit. +CONFIG_ZIGBEE_FOTA=n +CONFIG_ZIGBEE_FOTA_ENDPOINT=5 +CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" # Increase this version for generating an update for the devices. +CONFIG_ZIGBEE_FOTA_COMMENT="b_parasite" + +# Ensure an MCUboot-compatible binary for ota update is generated. +CONFIG_IMG_MANAGER=y +CONFIG_STREAM_FLASH=y +CONFIG_DFU_TARGET_MCUBOOT=y +CONFIG_IMG_ERASE_PROGRESSIVELY=y +CONFIG_ZIGBEE_FOTA_PROGRESS_EVT=y + +# Increase the number of RX buffers, as suggested by zigbee ota sample. +CONFIG_NRF_802154_RX_BUFFERS=32 diff --git a/code/nrf-connect/samples/zigbee/prj_ota.conf b/code/nrf-connect/samples/zigbee/prj_ota.conf deleted file mode 100644 index 4a10f2f..0000000 --- a/code/nrf-connect/samples/zigbee/prj_ota.conf +++ /dev/null @@ -1,89 +0,0 @@ - -CONFIG_LOG=y -CONFIG_PWM=y -CONFIG_I2C=y -CONFIG_ADC=y -CONFIG_GPIO=y - -CONFIG_USE_SEGGER_RTT=y -CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096 - -CONFIG_NEWLIB_LIBC=y -CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y - -CONFIG_SERIAL=n - -CONFIG_HEAP_MEM_POOL_SIZE=2048 -CONFIG_MAIN_THREAD_PRIORITY=7 - -CONFIG_ZIGBEE=y -CONFIG_ZIGBEE_APP_UTILS=y -CONFIG_ZIGBEE_ROLE_END_DEVICE=y - -# This example requires more workqueue stack -CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 - -# Enable nRF ECB driver -CONFIG_CRYPTO=y -CONFIG_CRYPTO_NRF_ECB=y -CONFIG_CRYPTO_INIT_PRIORITY=80 - -# Networking -CONFIG_NET_IPV6=n -CONFIG_NET_IPV6_MLD=n -CONFIG_NET_IPV6_NBR_CACHE=n -CONFIG_NET_IPV6_RA_RDNSS=n -CONFIG_NET_IP_ADDR_CHECK=n -CONFIG_NET_UDP=n - -## -## ZigBee Channel Selection -## -# Get Zigbee to scan every channel. -CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_MULTI=y -# By default only scans channel 11 (ZigBee2MQTT default) and 15 (ZHA default). -# Comment to scan all channels - this will make pairing consume more energy. -# CONFIG_ZIGBEE_CHANNEL_MASK=0x8800 - -# Uncomment to set a specific channel - this will make pairing more energy efficient. -# CONFIG_ZIGBEE_CHANNEL=15 - -# Enable API for powering down unused RAM parts. -# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/nrf/ug_zigbee_configuring.html#power-saving-during-sleep -CONFIG_RAM_POWER_DOWN_LIBRARY=y - -# LittleFS. -CONFIG_MAIN_STACK_SIZE=2048 -CONFIG_FLASH=y -CONFIG_FLASH_MAP=y -CONFIG_FLASH_PAGE_LAYOUT=y -CONFIG_FILE_SYSTEM=y -CONFIG_FILE_SYSTEM_LITTLEFS=y - -CONFIG_WATCHDOG=y -CONFIG_HWINFO=y -CONFIG_ASSERT=y - -# Uncomment for debug log level. -# CONFIG_LOG_DEFAULT_LEVEL=4 - -# Factory reset method selection. Only hardware revision 2.0.0+ has button SW1. Earlier -# revisions must select a different method. See Kconfig for options. -# CONFIG_PRST_ZB_FACTORY_RESET_VIA_SW1=y -# Disable factory reset. - -# OTA update configuration. This is based on the zigbee light switch example from the nrf-connect toolkit. -CONFIG_ZIGBEE_FOTA=y -CONFIG_ZIGBEE_FOTA_ENDPOINT=5 -CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" # Increase this version for generating an update for the devices. -CONFIG_ZIGBEE_FOTA_COMMENT="b_parasite" - -# Ensure an MCUboot-compatible binary is generated. -CONFIG_IMG_MANAGER=y -CONFIG_STREAM_FLASH=y -CONFIG_DFU_TARGET_MCUBOOT=y -CONFIG_IMG_ERASE_PROGRESSIVELY=y -CONFIG_ZIGBEE_FOTA_PROGRESS_EVT=y - -# Increase the number of RX buffers -CONFIG_NRF_802154_RX_BUFFERS=32 From 7dcd4bfe53f62076ec3119826fad65f1db86be1f Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Wed, 1 May 2024 17:28:41 +0200 Subject: [PATCH 06/10] Moving toggled settings to Kconfig for ota updates. --- code/nrf-connect/samples/zigbee/Kconfig | 36 ++++++++++++++++++++++++ code/nrf-connect/samples/zigbee/prj.conf | 12 -------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/code/nrf-connect/samples/zigbee/Kconfig b/code/nrf-connect/samples/zigbee/Kconfig index 6fbb42a..237488f 100644 --- a/code/nrf-connect/samples/zigbee/Kconfig +++ b/code/nrf-connect/samples/zigbee/Kconfig @@ -42,3 +42,39 @@ 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 + +if ZIGBEE_FOTA + +config ZIGBEE_FOTA_ENDPOINT + int "Endpoint number for ota zigbee endpoint. Must not be used by another application." + default 5 + +config ZIGBEE_FOTA_COMMENT + string + default "b-parasite" + +config IMG_MANAGER + bool "Ensure an MCUboot-compatible binary for ota update is generated." + default y + +config STREAM_FLASH + bool + default y + +config DFU_TARGET_MCUBOOT + bool + default y + +config IMG_ERASE_PROGRESSIVELY + bool + default y + +config ZIGBEE_FOTA_PROGRESS_EVT + bool + default y + +config NRF_802154_RX_BUFFERS + int "Increase the number of RX buffers for ota functionality." + default 32 + +endif diff --git a/code/nrf-connect/samples/zigbee/prj.conf b/code/nrf-connect/samples/zigbee/prj.conf index 321ca6f..9dbefe1 100644 --- a/code/nrf-connect/samples/zigbee/prj.conf +++ b/code/nrf-connect/samples/zigbee/prj.conf @@ -74,16 +74,4 @@ CONFIG_ASSERT=y # OTA update configuration. This is based on the zigbee light switch example from the nrf-connect toolkit. CONFIG_ZIGBEE_FOTA=n -CONFIG_ZIGBEE_FOTA_ENDPOINT=5 CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" # Increase this version for generating an update for the devices. -CONFIG_ZIGBEE_FOTA_COMMENT="b_parasite" - -# Ensure an MCUboot-compatible binary for ota update is generated. -CONFIG_IMG_MANAGER=y -CONFIG_STREAM_FLASH=y -CONFIG_DFU_TARGET_MCUBOOT=y -CONFIG_IMG_ERASE_PROGRESSIVELY=y -CONFIG_ZIGBEE_FOTA_PROGRESS_EVT=y - -# Increase the number of RX buffers, as suggested by zigbee ota sample. -CONFIG_NRF_802154_RX_BUFFERS=32 From b72b97091c8dd41f6bcb216fa8220ed46d1c59fd Mon Sep 17 00:00:00 2001 From: rbaron Date: Sat, 11 May 2024 09:27:07 +0200 Subject: [PATCH 07/10] Define and set sw_build_id in the basic attr cluster --- code/nrf-connect/samples/zigbee/Kconfig | 4 ++++ code/nrf-connect/samples/zigbee/src/prst_zb_attrs.c | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/code/nrf-connect/samples/zigbee/Kconfig b/code/nrf-connect/samples/zigbee/Kconfig index 237488f..f34acc6 100644 --- a/code/nrf-connect/samples/zigbee/Kconfig +++ b/code/nrf-connect/samples/zigbee/Kconfig @@ -13,6 +13,10 @@ config PRST_ZB_BUILD_DATE string "Zigbee basic cluster build date attribute. Max 16 bytes." default "" +config PRST_ZB_SOFTWARE_VERSION + string "Zigbee basic cluster software version attribute. Max 16 bytes." + default "" + config PRST_ZB_MODEL_ID string "Zigbee basic cluster model id attribute. Max 32 bytes." default "b-parasite" diff --git a/code/nrf-connect/samples/zigbee/src/prst_zb_attrs.c b/code/nrf-connect/samples/zigbee/src/prst_zb_attrs.c index 8307369..9590c55 100644 --- a/code/nrf-connect/samples/zigbee/src/prst_zb_attrs.c +++ b/code/nrf-connect/samples/zigbee/src/prst_zb_attrs.c @@ -22,6 +22,11 @@ void prst_zb_attrs_init(struct zb_device_ctx *dev_ctx) { CONFIG_PRST_ZB_BUILD_DATE, ZB_ZCL_STRING_CONST_SIZE(CONFIG_PRST_ZB_BUILD_DATE)); + ZB_ZCL_SET_STRING_VAL( + dev_ctx->basic_attr.sw_ver, + CONFIG_PRST_ZB_SOFTWARE_VERSION, + ZB_ZCL_STRING_CONST_SIZE(CONFIG_PRST_ZB_SOFTWARE_VERSION)); + dev_ctx->basic_attr.hw_version = CONFIG_PRST_ZB_HARDWARE_VERSION; dev_ctx->batt_attrs.quantity = 1; From b2376a4050224201fc872cdc1e60bb756319b2b8 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sat, 11 May 2024 10:38:27 +0200 Subject: [PATCH 08/10] Re-create separate prj_fota.conf --- code/nrf-connect/samples/zigbee/Kconfig | 36 ------- code/nrf-connect/samples/zigbee/prj.conf | 5 - code/nrf-connect/samples/zigbee/prj_fota.conf | 96 +++++++++++++++++++ 3 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 code/nrf-connect/samples/zigbee/prj_fota.conf diff --git a/code/nrf-connect/samples/zigbee/Kconfig b/code/nrf-connect/samples/zigbee/Kconfig index f34acc6..4ce621a 100644 --- a/code/nrf-connect/samples/zigbee/Kconfig +++ b/code/nrf-connect/samples/zigbee/Kconfig @@ -46,39 +46,3 @@ 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 - -if ZIGBEE_FOTA - -config ZIGBEE_FOTA_ENDPOINT - int "Endpoint number for ota zigbee endpoint. Must not be used by another application." - default 5 - -config ZIGBEE_FOTA_COMMENT - string - default "b-parasite" - -config IMG_MANAGER - bool "Ensure an MCUboot-compatible binary for ota update is generated." - default y - -config STREAM_FLASH - bool - default y - -config DFU_TARGET_MCUBOOT - bool - default y - -config IMG_ERASE_PROGRESSIVELY - bool - default y - -config ZIGBEE_FOTA_PROGRESS_EVT - bool - default y - -config NRF_802154_RX_BUFFERS - int "Increase the number of RX buffers for ota functionality." - default 32 - -endif diff --git a/code/nrf-connect/samples/zigbee/prj.conf b/code/nrf-connect/samples/zigbee/prj.conf index 9dbefe1..9e20278 100644 --- a/code/nrf-connect/samples/zigbee/prj.conf +++ b/code/nrf-connect/samples/zigbee/prj.conf @@ -70,8 +70,3 @@ CONFIG_ASSERT=y # Factory reset method selection. Only hardware revision 2.0.0+ has button SW1. Earlier # revisions must select a different method. See Kconfig for options. # CONFIG_PRST_ZB_FACTORY_RESET_VIA_SW1=y -## OTA section - -# OTA update configuration. This is based on the zigbee light switch example from the nrf-connect toolkit. -CONFIG_ZIGBEE_FOTA=n -CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" # Increase this version for generating an update for the devices. diff --git a/code/nrf-connect/samples/zigbee/prj_fota.conf b/code/nrf-connect/samples/zigbee/prj_fota.conf new file mode 100644 index 0000000..e9d769a --- /dev/null +++ b/code/nrf-connect/samples/zigbee/prj_fota.conf @@ -0,0 +1,96 @@ + +CONFIG_LOG=y +CONFIG_PWM=y +CONFIG_I2C=y +CONFIG_ADC=y +CONFIG_GPIO=y + +CONFIG_USE_SEGGER_RTT=y +CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096 + +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y + +CONFIG_SERIAL=n + +CONFIG_HEAP_MEM_POOL_SIZE=2048 +CONFIG_MAIN_THREAD_PRIORITY=7 + +CONFIG_ZIGBEE=y +CONFIG_ZIGBEE_APP_UTILS=y +CONFIG_ZIGBEE_ROLE_END_DEVICE=y + +# This example requires more workqueue stack +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 + +# Enable nRF ECB driver +CONFIG_CRYPTO=y +CONFIG_CRYPTO_NRF_ECB=y +CONFIG_CRYPTO_INIT_PRIORITY=80 + +# Networking +CONFIG_NET_IPV6=n +CONFIG_NET_IPV6_MLD=n +CONFIG_NET_IPV6_NBR_CACHE=n +CONFIG_NET_IPV6_RA_RDNSS=n +CONFIG_NET_IP_ADDR_CHECK=n +CONFIG_NET_UDP=n + +## +## ZigBee Channel Selection +## +# Get Zigbee to scan every channel. +CONFIG_ZIGBEE_CHANNEL_SELECTION_MODE_MULTI=y +# By default only scans channel 11 (ZigBee2MQTT default) and 15 (ZHA default). +# Comment to scan all channels - this will make pairing consume more energy. +# CONFIG_ZIGBEE_CHANNEL_MASK=0x8800 + +# Uncomment to set a specific channel - this will make pairing more energy efficient. +# CONFIG_ZIGBEE_CHANNEL=11 + +# Enable API for powering down unused RAM parts. +# https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.7.1/nrf/ug_zigbee_configuring.html#power-saving-during-sleep +CONFIG_RAM_POWER_DOWN_LIBRARY=y + +# LittleFS. +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_FILE_SYSTEM=y +CONFIG_FILE_SYSTEM_LITTLEFS=y + +CONFIG_WATCHDOG=y +CONFIG_HWINFO=y +CONFIG_ASSERT=y + +# Uncomment for debug log level. +# CONFIG_LOG_DEFAULT_LEVEL=4 + +# Factory reset method selection. Only hardware revision 2.0.0+ has button SW1. Earlier +# revisions must select a different method. See Kconfig for options. +# CONFIG_PRST_ZB_FACTORY_RESET_VIA_SW1=y + +## +## OTA section. +## + +# Software build version in the basic_attrs cluster. +CONFIG_PRST_ZB_SOFTWARE_VERSION="test-sw-1" + +# Enable Zigbee FOTA library +CONFIG_ZIGBEE_FOTA=y +CONFIG_ZIGBEE_FOTA_HW_VERSION=52 +CONFIG_ZIGBEE_FOTA_ENDPOINT=5 +CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" +CONFIG_ZIGBEE_FOTA_COMMENT="b-parasite" + +# Ensure an MCUboot-compatible binary is generated. +CONFIG_IMG_MANAGER=y +CONFIG_STREAM_FLASH=y +CONFIG_DFU_TARGET_MCUBOOT=y +CONFIG_IMG_ERASE_PROGRESSIVELY=y +CONFIG_ZIGBEE_FOTA_PROGRESS_EVT=y + +# Increase the number of RX buffers +CONFIG_NRF_802154_RX_BUFFERS=32 From 18f4914f37cff45b13a01fdc7df280ee292add51 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 12 May 2024 14:50:51 +0200 Subject: [PATCH 09/10] FOTA configs for safer firmware matching --- code/nrf-connect/samples/zigbee/prj_fota.conf | 22 ++++++++++++++++--- code/nrf-connect/samples/zigbee/src/main.c | 4 +++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/code/nrf-connect/samples/zigbee/prj_fota.conf b/code/nrf-connect/samples/zigbee/prj_fota.conf index e9d769a..7c16e20 100644 --- a/code/nrf-connect/samples/zigbee/prj_fota.conf +++ b/code/nrf-connect/samples/zigbee/prj_fota.conf @@ -76,13 +76,29 @@ CONFIG_ASSERT=y ## # Software build version in the basic_attrs cluster. -CONFIG_PRST_ZB_SOFTWARE_VERSION="test-sw-1" +CONFIG_PRST_ZB_SOFTWARE_VERSION="dev" # Enable Zigbee FOTA library CONFIG_ZIGBEE_FOTA=y -CONFIG_ZIGBEE_FOTA_HW_VERSION=52 + CONFIG_ZIGBEE_FOTA_ENDPOINT=5 -CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="1.0.0" +CONFIG_ZIGBEE_FOTA_LOG_LEVEL_DBG=y + +# DIY Zigbee FOTA manufacturer ID. +CONFIG_ZIGBEE_FOTA_MANUFACTURER_ID=0xDB15 + +# This must be BOARD_REVISION_CODE. +# TODO: move this to Kconfig. +CONFIG_ZIGBEE_FOTA_HW_VERSION=4 + +# Set for which hardware versions this FOTA image is valid. +CONFIG_ENABLE_ZIGBEE_FOTA_MIN_HW_VERSION=y +CONFIG_ZIGBEE_FOTA_MIN_HW_VERSION=0x00 +CONFIG_ENABLE_ZIGBEE_FOTA_MAX_HW_VERSION=y +CONFIG_ZIGBEE_FOTA_MAX_HW_VERSION=0xff + +# 0.0.0 is a safe default for development. +CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION="0.0.0" CONFIG_ZIGBEE_FOTA_COMMENT="b-parasite" # Ensure an MCUboot-compatible binary is generated. diff --git a/code/nrf-connect/samples/zigbee/src/main.c b/code/nrf-connect/samples/zigbee/src/main.c index 0efad06..2d794e3 100644 --- a/code/nrf-connect/samples/zigbee/src/main.c +++ b/code/nrf-connect/samples/zigbee/src/main.c @@ -149,7 +149,7 @@ static void confirm_image(void) { static void ota_evt_handler(const struct zigbee_fota_evt *evt) { switch (evt->id) { case ZIGBEE_FOTA_EVT_PROGRESS: - prst_led_flash(1); + LOG_INF("OTA progress: %d%%", evt->dl.progress); break; case ZIGBEE_FOTA_EVT_FINISHED: LOG_INF("Reboot application."); @@ -386,6 +386,8 @@ int main(void) { power_down_unused_ram(); #ifdef CONFIG_ZIGBEE_FOTA + LOG_INF("Firmware version: %s", CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION); + /* Initialize Zigbee FOTA download service. */ zigbee_fota_init(ota_evt_handler); From 67b3dd8482134abf6dab59683ba730d6888152a4 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 12 May 2024 14:55:45 +0200 Subject: [PATCH 10/10] Leave manufacturer ID unset for now --- code/nrf-connect/samples/zigbee/prj_fota.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/nrf-connect/samples/zigbee/prj_fota.conf b/code/nrf-connect/samples/zigbee/prj_fota.conf index 7c16e20..d76af58 100644 --- a/code/nrf-connect/samples/zigbee/prj_fota.conf +++ b/code/nrf-connect/samples/zigbee/prj_fota.conf @@ -84,8 +84,8 @@ CONFIG_ZIGBEE_FOTA=y CONFIG_ZIGBEE_FOTA_ENDPOINT=5 CONFIG_ZIGBEE_FOTA_LOG_LEVEL_DBG=y -# DIY Zigbee FOTA manufacturer ID. -CONFIG_ZIGBEE_FOTA_MANUFACTURER_ID=0xDB15 +# FOTA manufacturer ID. Defaults to 0x127f (Nordic). +# CONFIG_ZIGBEE_FOTA_MANUFACTURER_ID=0x0000 # This must be BOARD_REVISION_CODE. # TODO: move this to Kconfig.