matter 官方代码初体验

入坑

上次edgeX跑起来还算平坦, 没使用源代码编译,docker无疑是一个不错的台阶。 我是熟悉golang的, 所以我是放心的。 但是, matter是c++写的,不是我不熟悉, 而是我知道肯定会被坑。 我太熟悉这些老外了, 他们引用了很多谷歌开源的代码, 这些谷歌代码又引用谷歌工具, 这些工具国内又没人做本地化。

其实我是应该先试用mainflux的, 也是golang实现。 这些更倾向于边缘计算, 而不是智能家居。 我还是觉得智能家居可玩性好一点。以前智能家居不开放, 手机厂商垄断。 有了这个matter的开放协议, 尤其是可以支持个人diy,和usb一样对vid是弱要求。 这个的确赞。

官方代码

架构图 看了一些方案实现, 要么是c语言直接引入官方代码。 要么是python等调用官方代码的库。 这思路也不错。毕竟不需要重复造轮子。该入坑的早晚要入。

github https://github.com/project-chip/connectedhomeip
链接看起来很神奇吧。 chip是一个缩写,Connected Home over IP, 链接家庭设备基于IP协议。 其实我觉得, 太专业了。 智能家居over wifi是最常用, 然后用边缘网关扩展到其他协议, 作为一个扩展标准,学习起来就更简单了。

毕竟咱们也搞过多年c++, 不在乎。

build matter https://project-chip.github.io/connectedhomeip-doc/guides/BUILDING.html
上面链接到了构建部分。 噩梦刚刚开始。
程序员喜欢复制粘贴, 更喜欢git clone, 这里提示一下github 近期禁用了匿名代码拉取。

git clone --recurse-submodules git@github.com:project-chip/connectedhomeip.git

上面这个代码要把子模块也拉下来。 子模块之多, 让我从来没想到。 网速很快,能达到10Mb/s, 但是, 就这速度, 下载完代码一共19G, 19G的视频,我不怕。 代码我还真怕了。 linux 内核有多大? 安卓java代码有多大? 这个小东西要19G真是看不完。

搞嵌入式真是西天取经。 一关又一关。

source scripts/activate.sh

要想编译, 不仅仅是根据文档把属于自己专属os的以来安装一下。 还有一个脚本初始化。 因为不同的库,有不同的依赖和构建。 需要持续兼容。 毕竟用文档写, 超越了用户的忍耐能力。大家都知道,写构建代码的人, 大概率能用就行。 人家在国外, 网速快, 并且稳。 我们在wall内, python代码总是报超时。 配了一下https_proxy这个变量, 又配错了。 注意一定要有http:// 否则这个低概率的bug, 官方代码是不可能给你正确的提示的。 他总是提示我python不支持https, 我都重装好几次了。 时间就这么浪费了。 我开始去查python的代码, 一步一步分析才知道又是一个弱智错误导致时间大量浪费的故事。

后面的编译没有遇到什么问题。

切换到quich_start https://project-chip.github.io/connectedhomeip-doc/QUICK_START.html
因为这里有真正的快速入门的验证。 linux和macos是一样的。 用linux入口能看到编译macos上的虚拟样例。

我是arm的macos选用的下面的脚本

$ ./scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target darwin-arm64-all-clusters-no-ble-asan build"

out/darwin-arm64-all-clusters-no-ble-asan/下面编译出来chip-all-clusters-app

我执行一下精选一点日志看看, 但是, 手机是能发现设备的, 但是无响应。 这个后续再看

cat log2.txt| grep -v DMG | head -n 100

使用上述命令筛选日志

[1702100207789] [21268:1573638] [DL] KVS will be written to: file:///tmp/chip_kvs
[1702100207799] [21268:1573638] [SPT] *** WARNING: Using temporary passcode 20202021 due to no neither --passcode or --spake2p-verifier-base64 given on command line. This is temporary and will disappear. Please update your scripts to explicitly configure onboarding credentials. ***
[1702100207799] [21268:1573638] [SPT] *** WARNING: Using temporary test discriminator 3840 due to --discriminator not given on command line. This is temporary and will disappear. Please update your scripts to explicitly configure discriminator. ***
[1702100207799] [21268:1573638] [SPT] PASE PBKDF iterations set to 1000
[1702100207799] [21268:1573638] [SPT] LinuxCommissionableDataProvider didn't get a PASE salt, generating one.
[1702100207810] [21268:1573638] [DL] Device Configuration:
[1702100207810] [21268:1573638] [DL]   Serial Number: TEST_SN
[1702100207810] [21268:1573638] [DL]   Vendor Id: 65521 (0xFFF1)
[1702100207811] [21268:1573638] [DL]   Product Id: 32769 (0x8001)
[1702100207811] [21268:1573638] [DL]   Product Name: TEST_PRODUCT
[1702100207811] [21268:1573638] [DL]   Hardware Version: 0
[1702100207811] [21268:1573638] [DL]   Setup Pin Code (0 for UNKNOWN/ERROR): 20202021
[1702100207811] [21268:1573638] [DL]   Setup Discriminator (0xFFFF for UNKNOWN/ERROR): 3840 (0xF00)
[1702100207811] [21268:1573638] [DL]   Manufacturing Date: (not set)
[1702100207811] [21268:1573638] [DL]   Device Type: 65535 (0xFFFF)
[1702100207811] [21268:1573638] [-] ==== Onboarding payload for Standard Commissioning Flow ====
[1702100207811] [21268:1573638] [SVR] SetupQRCode: [MT:-24J0AFN00KA0648G00]
[1702100207811] [21268:1573638] [SVR] Copy/paste the below URL in a browser to see the QR Code:
[1702100207811] [21268:1573638] [SVR] https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT%3A-24J0AFN00KA0648G00
[1702100207811] [21268:1573638] [SVR] Manual pairing code: [34970112332]
[1702100207811] [21268:1573638] [SVR] Initializing subscription resumption storage...
[1702100207811] [21268:1573638] [SVR] Server initializing...
[1702100207811] [21268:1573638] [FP] Initializing FabricTable from persistent storage
[1702100207811] [21268:1573638] [TS] Last Known Good Time: 2023-12-08T08:38:17
[1702100207812] [21268:1573638] [FP] Fabric index 0x1 was retrieved from storage. Compressed FabricId 0x90102BE7FFBDEC6A, FabricId 0x00000000568FF855, NodeId 0x00000000974397B2, VendorId 0x1349
[1702100207812] [21268:1573638] [FP] Fabric index 0x2 was retrieved from storage. Compressed FabricId 0xB3B0BEA3B02A7C46, FabricId 0x000000009B1C35D9, NodeId 0x00000000C6C43D51, VendorId 0x1384
[1702100207812] [21268:1573638] [IN] UDP::Init bind&listen port=5540
[1702100207813] [21268:1573638] [IN] UDP::Init bound to port=5540
[1702100207813] [21268:1573638] [IN] UDP::Init bind&listen port=5540
[1702100207813] [21268:1573638] [IN] UDP::Init bound to port=5540
[1702100207813] [21268:1573638] [IN] TransportMgr initialized
[1702100207814] [21268:1573638] [ZCL] Using ZAP configuration...
[1702100207817] [21268:1573638] [ZCL] Initiating Admin Commissioning cluster.
[1702100207817] [21268:1573638] [ZCL] GeneralDiagnostics: OnDeviceReboot
[1702100207817] [21268:1573638] [EVL] LogEvent event number: 0x0000000000030000 priority: 2, endpoint id:  0x0 cluster id: 0x0000_0033 event id: 0x3 Epoch timestamp: 0x0000018C4D1408C9
[1702100207818] [21268:1573638] [EVL] LogEvent event number: 0x0000000000030001 priority: 1, endpoint id:  0x0 cluster id: 0x0000_0038 event id: 0x0 Epoch timestamp: 0x0000018C4D1408CA
[1702100207818] [21268:1573638] [ZCL] Emit DSTTableEmpty event [ep=0]
[1702100207818] [21268:1573638] [ZCL] Endpoint 1 On/off already set to new value
[1702100207818] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207818] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207818] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the CurrentMode from the KVS. Assuming 0
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the StartUpMode from the KVS. Assuming null
[1702100207819] [21268:1573638] [ZCL] ModeBase: Unable to load the OnMode from the KVS.      Assuming null
[1702100207819] [21268:1573638] [ZCL] ResourceMonitoring: Init
[1702100207820] [21268:1573638] [ZCL] ResourceMonitoring: Unable to load the LastChangedTime from the KVS. Assuming null
[1702100207820] [21268:1573638] [ZCL] ResourceMonitoring: calling mDelegate->Init()
[1702100207820] [21268:1573638] [ZCL] HepaFilterMonitoringDelegate::Init()
[1702100207820] [21268:1573638] [ZCL] ResourceMonitoring: Init
[1702100207820] [21268:1573638] [ZCL] ResourceMonitoring: Unable to load the LastChangedTime from the KVS. Assuming null
[1702100207820] [21268:1573638] [ZCL] ResourceMonitoring: calling mDelegate->Init()
[1702100207820] [21268:1573638] [ZCL] ActivatedCarbonFilterMonitoringDelegate::Init()
[1702100207820] [21268:1573638] [ZCL] WC POST ATTRIBUTE=7
[1702100207820] [21268:1573638] [ZCL] ConfigStatus 0x1B Operational=1 OnlineReserved=1
[1702100207820] [21268:1573638] [ZCL] Lift(PA=1 Encoder=0 Reversed=0) Tilt(PA=1 Encoder=0)
[1702100207820] [21268:1573638] [ZCL] Initialize PCC Server Cluster [EP:1]
[1702100207820] [21268:1573638] [-] Setting LowPower default delegate to global manager
[1702100207821] [21268:1573638] [SVR] Fabric already commissioned. Disabling BLE advertisement
[1702100207821] [21268:1573638] [DIS] Updating services using commissioning mode 0
[1702100207821] [21268:1573638] [DL] Using wifi MAC for hostname
[1702100207821] [21268:1573638] [DIS] Advertise operational node 90102BE7FFBDEC6A-00000000974397B2
[1702100207821] [21268:1573638] [DIS] Registering service 90102BE7FFBDEC6A-00000000974397B2 on host BCD07406BDA3.local. with port 5540 and type: _matter._tcp,_I90102BE7FFBDEC6A on interface id: 0
[1702100207822] [21268:1573638] [DL] Using wifi MAC for hostname
[1702100207822] [21268:1573638] [DIS] Advertise operational node B3B0BEA3B02A7C46-00000000C6C43D51
[1702100207822] [21268:1573638] [DIS] Registering service B3B0BEA3B02A7C46-00000000C6C43D51 on host BCD07406BDA3.local. with port 5540 and type: _matter._tcp,_IB3B0BEA3B02A7C46 on interface id: 0
[1702100207822] [21268:1573638] [IN] CASE Server enabling CASE session setups
[1702100207822] [21268:1573638] [IN] SecureSession[0x107e01540]: Allocated Type:2 LSID:10849
[1702100207822] [21268:1573638] [SC] Allocated SecureSession (0x107e01540) - waiting for Sigma1 msg
[1702100207822] [21268:1573638] [SVR] Joining Multicast groups
[1702100207822] [21268:1573638] [ZCL] Emitting StartUp event
[1702100207822] [21268:1573638] [EVL] LogEvent event number: 0x0000000000030002 priority: 2, endpoint id:  0x0 cluster id: 0x0000_0028 event id: 0x0 Epoch timestamp: 0x0000018C4D1408CE
[1702100207822] [21268:1573638] [SVR] Server initialization complete
[1702100207822] [21268:1573638] [SVR] Server Listening...
[1702100207822] [21268:1573638] [DL] Device Configuration:
[1702100207822] [21268:1573638] [DL]   Serial Number: TEST_SN
[1702100207822] [21268:1573638] [DL]   Vendor Id: 65521 (0xFFF1)
[1702100207822] [21268:1573638] [DL]   Product Id: 32769 (0x8001)
[1702100207822] [21268:1573638] [DL]   Product Name: TEST_PRODUCT
[1702100207822] [21268:1573638] [DL]   Hardware Version: 0
[1702100207822] [21268:1573638] [DL]   Setup Pin Code (0 for UNKNOWN/ERROR): 20202021
[1702100207822] [21268:1573638] [DL]   Setup Discriminator (0xFFFF for UNKNOWN/ERROR): 3840 (0xF00)
[1702100207823] [21268:1573638] [DL]   Manufacturing Date: (not set)
[1702100207823] [21268:1573638] [DL]   Device Type: 65535 (0xFFFF)
[1702100207823] [21268:1573638] [SVR] SetupQRCode: [MT:-24J0AFN00KA0648G00]
[1702100207823] [21268:1573638] [SVR] Copy/paste the below URL in a browser to see the QR Code:
[1702100207823] [21268:1573638] [SVR] https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT%3A-24J0AFN00KA0648G00
[1702100207823] [21268:1573638] [SVR] Manual pairing code: [34970112332]
[1702100207823] [21268:1573638] [EVL] LogEvent event number: 0x0000000000030003 priority: 1, endpoint id:  0x1 cluster id: 0x0000_005D event id: 0x0 Epoch timestamp: 0x0000018C4D1408CF

这样就跑起来了。 日志里有二维码的url。每个设备都不一样。

后续需要看一下文档。