Nordic Semiconductor 开发平台示例下,提供了两套进行空中 (OTA) 设备固件升级 (DFU)的方式:
1.基于 Matter OTA 更新协议规范下使用 Matter 网络查询和下载新的固件映像,需要 OTBR。
2. nordic 基于低功耗蓝牙(Bluetooth LE)实现的简单管理协议(SMP),与 matter 无关,不需要 OTBR 。
准备一个更高版本的固件,向 light_hulb 工程的 .prj 中添加相关宏启用 SMP(如果使用到SMP)、设置固件版本。
#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
# Enable CHIP
CONFIG_CHIP=y
CONFIG_CHIP_PROJECT_CONFIG="src/chip_project_config.h"
# 32773 == 0x8005 (example lighting-app)
CONFIG_CHIP_DEVICE_PRODUCT_ID=32773
CONFIG_STD_CPP14=y
# Enable CHIP pairing automatically on application start.
CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y
# Add support for LEDs and buttons on Nordic development kits
CONFIG_DK_LIBRARY=y
CONFIG_PWM=y
# Bluetooth Low Energy configuration
CONFIG_BT_DEVICE_NAME="MatterLight"
# Other settings
CONFIG_THREAD_NAME=y
CONFIG_MPU_STACK_GUARD=y
CONFIG_RESET_ON_FATAL_ERROR=n
CONFIG_CHIP_LIB_SHELL=y
# Reduce application size
CONFIG_USE_SEGGER_RTT=n
# Enable Factory Data feature
CONFIG_CHIP_FACTORY_DATA=y
CONFIG_CHIP_FACTORY_DATA_BUILD=y
# ------------user--------------------
CONFIG_CHIP_DFU_OVER_BT_SMP=y
CONFIG_CHIP_DEVICE_SOFTWARE_VERSION=1
通过 Matter 更新
out/provider/chip-ota-provider-app -f ~/light_bulb/build/zephyr/matter.ota
1. 编译 OTA-Provider:
cd ~/connectedhomeip
./scripts/examples/gn_build_example.sh examples/ota-provider-app/linux out/provider chip_config_network_layer_ble=false
2. 使用最新编译的固件运行 OTA-provider 应用程序,保持程序运行:
out/provider/chip-ota-provider-app -f ~/light_bulb/build/zephyr/matter.ota
matter.ota 镜像结构:
字段 | 类型 | 描述 |
FileIdentifier | uint32 | 固定宽度、小端编码、无符号,标识 OTA 映像文件的开头 |
TotalSize | uint64 | 文件的总大小(以字节为单位) |
HeaderSize | uint32 | TLV 编码的 Header 字段的总大小 |
Header | TLV | 特定顺序的 ota-image-header-struct 结构信息,具体查看 Matter Core Specification 的 section 11.20.2.4 |
Payload | n/a | image 数据 |
3. 将 OTA-provider 添加到 matter 网络中,节点 id 为 1:
./out/chiptool/chip-tool pairing onnetwork 1 20202021
4. 将 light_hulb 添加到到 matter 网络,节点 id 为 2:
./connectedhomeip/out/chiptool/chip-tool pairing ble-thread 2 hex:0e08000000000001000035060004001fffe00708fd9e914e6def9df2030f4f70656e5468726561642d323361350410542ccde31a710d5b17af34e12fec0f140c0402a0f7f8051000112233445566778899aabbccddeeff0208111111112222222201021234000300000f 20202021 3840
5. 向 OTA-Requestor(节点2) 写入 default-otaproviders 列表:
./out/chiptool/chip-tool otasoftwareupdaterequestor write default-otaproviders '[{"fabricIndex": 1, "providerNodeID": 1, "endpoint": 0}]' 2 0
查询:
./out/chiptool/chip-tool otasoftwareupdaterequestor read default-otaproviders 2 0
chip-tool 日志:
...
...
[1705416859.589008][93059:93061] CHIP:EM: >>> [E:26842i S:29676 M:94254494 (Ack:104496952)] (S) Msg RX from 1:0000000000000002 [08C0] --- Type 0001:05 (IM:ReportData)
[1705416859.589027][93059:93061] CHIP:EM: Found matching exchange: 26842i, Delegate: 0x7f1cb400a710
[1705416859.589036][93059:93061] CHIP:EM: Rxd Ack; Removing MessageCounter:104496952 from Retrans Table on exchange 26842i
...
...
[1705416859.589204][93059:93061] CHIP:TOO: Endpoint: 0 Cluster: 0x0000_002A Attribute 0x0000_0000 DataVersion: 3719081181
[1705416859.589222][93059:93061] CHIP:TOO: DefaultOTAProviders: 1 entries
[1705416859.589236][93059:93061] CHIP:TOO: [1]: {
[1705416859.589242][93059:93061] CHIP:TOO: ProviderNodeID: 1
[1705416859.589245][93059:93061] CHIP:TOO: Endpoint: 0
[1705416859.589248][93059:93061] CHIP:TOO: FabricIndex: 1
[1705416859.589250][93059:93061] CHIP:TOO: }
[1705416859.589314][93059:93061] CHIP:EM: <<< [E:26842i S:29676 M:104496953 (Ack:94254494)] (S) Msg TX to 1:0000000000000002 [08C0] [UDP:[fdb6:b817:b8cb:1:c1b6:83e3:d019:7c2b%wlp0s20f3]:5540] --- Type 0000:10 (SecureChannel:StandaloneAck)
...
...
6. 设置 OTA-Provider 的 ACL(Access Control List,访问控制列表),为了确保只有经过授权的设备或实体能够执行OTA操作。这是一种安全性和权限管理的做法,旨在防止未经授权的设备或个体对网络中的设备进行固件升级。
./out/chiptool/chip-tool accesscontrol write acl '[{"fabricIndex": 1, "privilege": 5, "authMode": 2, "subjects": [112233], "targets": null}, {"fabricIndex": 1, "privilege": 3, "authMode": 2, "subjects": null, "targets": null}]' 1 0
ACL可以用于定义哪些设备或实体有权向设备发送OTA更新请求,以及在更新过程中有哪些权限。通过ACL,确保固件升级只能由经过授权的实体发起,并只对特定设备或设备组生效。
7. 查询更新请求:
./out/chiptool/chip-tool otasoftwareupdaterequestor announce-otaprovider 1 0 0 0 2 0
或者在 light_hulb 控制台中 发送 matter ota query,当有新版本时自动启动下载。(需要-DCONFIG_CHIP_LIB_SHELL=y,默认已开启此宏)。
OTA-Provider、OTA-Requestor (light-hulb)日志:
固件下载完成后设备自动重启更新固件。再次 matter ota query,日志显示:最新可获取固件版本为 1,当前版本为 1 ,更新忽略。
> matter ota query
matter ota query
I: 265685 [SWU]Triggered immediate OTA query for fabric: 0x1
...
...
...
D: 265840 [SWU] softwareVersion: 1
D: 265843 [SWU] softwareVersionString: prerelease
D: 265847 [SWU] updateToken: 32
D: 265850 [SWU] userConsentNeeded: 0
D: 265854 [SWU] metadataForRequestor: 0
D: 265857 [SWU]Available update version 1 is <= current version 1, update ignored
I: 265865 [SWU]Stopping the watchdog timer
I: 265869 [SWU]Starting the periodic query timer, timeout: 86400 seconds
I: 265876 [EM]<<< [E:41251i S:2372 M:192815727 (Ack:125269414)] (S) Msg TX to 1:0000000000000001 [08C0] --- Type 0000:10 (SecureChannel:StandaloneAck)
I: 265889 [IN](S) Sending msg 192815727 on secure session with LSID: 2372
流程简介
OTA Requestor 节点和 OTA Provider 节点(一般以网关形式存在)处于同一个matter fabric 网络中,OTA Provider 从 Image 服务器中下载新固件,通过 Matter 的 BDX (Bulk Data Exchange Protocol) 协议传输到 OTA Requestor。
在 Image server 之前还有一个 DCL 服务器,即 DCL (Distributed Compliance Ledger) 服务器,是一个分布式数据库,记录该matter 设备的认证状态、产品名称、产品描述固件地址、设备证书。
参考
Nordic FOTA updates
Performing Device Firmware Upgrade in the nRF Connect examples