home-assistant-libs / python-matter-server

Python server to interact with Matter
Apache License 2.0
520 stars 80 forks source link

Open commissioning window does not work #205

Closed flatsiedatsie closed 1 year ago

flatsiedatsie commented 1 year ago

I'm trying to share a paired Matter device with a second controller. Both are running the Python Matter Server.

Perhaps my assumptions about this process are wrong, so here is what I'm doing now:

Is this the correct process?

In the output I see this:

matter-adapter: ERROR:chip.SC:Failed to verify peer's MAC. This can happen when setup code is incorrect.

The full log from Pi 1:

2023-02-14 13:22:32.561 ERROR  : matter-adapter: DEBUG:chip.EM:Found matching exchange: 54758i, Delegate: 0x7f68025d20
2023-02-14 13:22:32.562 ERROR  : matter-adapter: DEBUG:chip.EM:CHIP MessageCounter:93762973 not in RetransTable on exchange 54758i
2023-02-14 13:22:32.562 ERROR  : matter-adapter: DEBUG:chip.SC:Received PBKDF param response
2023-02-14 13:22:32.563 ERROR  : matter-adapter: DEBUG:chip.SC:Peer assigned session ID 49245
2023-02-14 13:22:32.564 ERROR  : matter-adapter: DEBUG:chip.SC:Found MRP parameters in the message
2023-02-14 13:22:32.625 ERROR  : matter-adapter: INFO:chip.EM:<<< [E:54758i M:93762974 (Ack:33556571)] (U) Msg TX to 0:0000000000000000 [0000] --- Type 0000:22 (SecureChannel:PASE_Pake1)
2023-02-14 13:22:32.626 ERROR  : matter-adapter: INFO:chip.IN:(U) Sending msg 93762974 to IP address 'UDP:[fe80::32de:4bff:fef6:860%wlan0]:5540'
2023-02-14 13:22:32.626 ERROR  : matter-adapter: DEBUG:chip.SC:Sent spake2p msg1
2023-02-14 13:22:32.630 ERROR  : matter-adapter: INFO:chip.EM:>>> [E:54758i M:33556572 (Ack:93762974)] (U) Msg RX from 0:0000000000000000 [0000] --- Type 0000:10 (SecureChannel:StandaloneAck)
2023-02-14 13:22:32.631 ERROR  : matter-adapter: DEBUG:chip.EM:Found matching exchange: 54758i, Delegate: 0x7f68025d20
2023-02-14 13:22:32.632 ERROR  : matter-adapter: DEBUG:chip.EM:Rxd Ack; Removing MessageCounter:93762974 from Retrans Table on exchange 54758i
2023-02-14 13:22:33.558 ERROR  : matter-adapter: INFO:chip.EM:>>> [E:54758i M:33556573 (Ack:93762974)] (U) Msg RX from 0:0000000000000000 [0000] --- Type 0000:23 (SecureChannel:PASE_Pake2)
2023-02-14 13:22:33.560 ERROR  : matter-adapter: DEBUG:chip.EM:Found matching exchange: 54758i, Delegate: 0x7f68025d20
2023-02-14 13:22:33.561 ERROR  : matter-adapter: DEBUG:chip.EM:CHIP MessageCounter:93762974 not in RetransTable on exchange 54758i
2023-02-14 13:22:33.562 ERROR  : matter-adapter: DEBUG:chip.SC:Received spake2p msg2
2023-02-14 13:22:33.562 ERROR  : matter-adapter: ERROR:chip.SC:Failed to verify peer's MAC. This can happen when setup code is incorrect.
2023-02-14 13:22:33.563 ERROR  : matter-adapter: DEBUG:chip.SC:Sending status report. Protocol code 2, exchange 54758
2023-02-14 13:22:33.563 ERROR  : matter-adapter: INFO:chip.EM:<<< [E:54758i M:93762975 (Ack:33556573)] (U) Msg TX to 0:0000000000000000 [0000] --- Type 0000:40 (SecureChannel:StatusReport)
2023-02-14 13:22:33.564 ERROR  : matter-adapter: INFO:chip.IN:(U) Sending msg 93762975 to IP address 'UDP:[fe80::32de:4bff:fef6:860%wlan0]:5540'
2023-02-14 13:22:33.565 ERROR  : matter-adapter: DEBUG:chip.IN:SecureSession[0x7f6801f240]: Released - Type:1 LSID:11274
2023-02-14 13:22:33.565 ERROR  : matter-adapter: ERROR:chip.SC:Failed during PASE session setup: ../src/crypto/CHIPCryptoPALOpenSSL.cpp:1440: CHIP Error 0x000000AC: Internal error
2023-02-14 13:22:33.566 ERROR  : matter-adapter: ERROR:matter_server.server.client_handler:[547800155136] Error handling message: CommandMessage(message_id='commission_on_network', command='commission_on_network', args={'setup_pin_code': '3840'})
2023-02-14 13:22:33.566 ERROR  : matter-adapter: Traceback (most recent call last):
2023-02-14 13:22:33.567 ERROR  : matter-adapter:   File "/home/pi/.webthings/addons/matter-adapter/lib/matter_server/server/client_handler.py", line 185, in _run_handler
2023-02-14 13:22:33.567 ERROR  : matter-adapter:     result = await result
2023-02-14 13:22:33.567 ERROR  : matter-adapter:   File "/home/pi/.webthings/addons/matter-adapter/lib/matter_server/server/device_controller.py", line 161, in commission_on_network
2023-02-14 13:22:33.568 ERROR  : matter-adapter:     raise NodeCommissionFailed(
2023-02-14 13:22:33.568 ERROR  : matter-adapter: matter_server.common.models.error.NodeCommissionFailed: Commission on network failed for node 1
2023-02-14 13:22:33.688 ERROR  : matter-adapter: INFO:chip.EM:>>> [E:54758i M:33556574 (Ack:93762975)] (U) Msg RX from 0:0000000000000000 [0000] --- Type 0000:10 (SecureChannel:StandaloneAck)
2023-02-14 13:22:33.689 ERROR  : matter-adapter: DEBUG:chip.EM:Found matching exchange: 54758i, Delegate: (nil)
2023-02-14 13:22:33.690 ERROR  : matter-adapter: DEBUG:chip.EM:Rxd Ack; Removing MessageCounter:93762975 from Retrans Table on exchange 54758i
marcelveldt commented 1 year ago

Well, I've seen this too and need to look into it, appears an issue in the python wrapper of the SDK. For me at least just always using the normal commissioning command just works, even for on network pairs.

flatsiedatsie commented 1 year ago

Ah ok. Thanks. I thought perhaps I was mistaking the "discriminator" with the actual pairing code?

To commission the device, you can use either the setup PIN code or the setup PIN code and the discriminator [source](https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/matter/chip_tool_guide.html)

It seems the discriminator is a 4 digit code, and the pairing code is normally longer?

A Passcode SHALL be included as a 27-bit unsigned integer, which serves as proof of possession during commissioning. The 27-bit unsigned integer encodes an 8-digit decimal numeric value, and therefore shall be restricted to the values 0x0000001 to 0x5F5E0FE (00000001 to 99999998 in decimal), excluding the invalid Passcode values. [source](https://www.cookie-daily.life/src/matter/matter_html/Chapter%205.%20Commissioning.html)

And another hint that it should be eight digits here:

I: 254 [DL]Device Configuration:
I: 257 [DL] Serial Number: TEST_SN
I: 260 [DL] Vendor Id: 65521 (0xFFF1)
I: 263 [DL] Product Id: 32768 (0x8000)
I: 267 [DL] Hardware Version: 1
I: 270 [DL] Setup Pin Code: 20202021
I: 273 [DL] Setup Discriminator: 3840 (0xF00)
I: 278 [DL] Manufacturing Date: (not set)
I: 281 [DL] Device Type: 65535 (0xFFFF)
In above printout, the discriminator is 3840 (0xF00) and the setup PIN code is equal to 20202021.
balloob commented 1 year ago

If you run 2 versions of the Matter server, make sure they both have unique IDs and are not identifying as the same controller.

marcelveldt commented 1 year ago

@flatsiedatsie you indeed only need the pin code, the discriminator can be used as filter if multiple devices are advertising as ready to pair but by default the sdk just tries them all so sending the pincode alone is enough.

flatsiedatsie commented 1 year ago

I just tried again, making sure each Raspberry Pi was only running one Matter server. It still resulted in the same error.

@marcelveldt You make it sound as if I should have a choice between two codes? However, when I run the command this is the response I see:

2023-02-15 22:40:38.430 INFO   : matter-adapter: client: on_message: {
2023-02-15 22:40:38.431 INFO   : matter-adapter:   "message_id": "open_commissioning_window",
2023-02-15 22:40:38.431 INFO   : matter-adapter:   "result": 3840,
2023-02-15 22:40:38.432 INFO   : matter-adapter:   "_type": "matter_server.common.models.message.SuccessResultMessage"
2023-02-15 22:40:38.432 INFO   : matter-adapter: }

The result is just one number, which seems to be the discriminator (four numbers) and not the pairing code (eight numbers)?

marcelveldt commented 1 year ago

I'll have a look, I did not actually do anything with this part of the SDK just that. So far our efforts have gone to commission devices into the server instead of the other way around :-)

marcelveldt commented 1 year ago

The "open commission window" command to share an already commissioned device to another controller is broken in the current python wrapper implementation but fixed upstream. Revisit this one 1.1 is available and a new release of the SDK is out.

mrflo97 commented 1 year ago

Interestingly this is working for me. I have added some Eve Devices (3 Energy, 3 Door & Window, 1 Motion) to Home assistant through a compatible USB Dongle and the Silicon Labs Multi Protocol Add On. Then I connected via Websocket to the Matter Server, issued the command, and successfully added the devices via the 8 digit code to Apple Home. (Took this route as I wanted to force the use of Home Assistant being the Border Router instead of my Apple TV, had some stability issues there.)

marcelveldt commented 1 year ago

Yes we fixed this in the code but are still working on the UI within HA.

BTW: At this time its actually the other way around and Apple is the most stable border router. We are working hard on improving the DIY route (and even allow to form one big network) but for now its not our recommended route.

marcelveldt commented 1 year ago

Closing here as the original issue has been resolved. @flatsiedatsie please re-open if you still run into issues with the command.