Skip to content

Commit 9e9437d

Browse files
committed
Completely deserialize zigpy's ZDO request when proxying
1 parent 8614a21 commit 9e9437d

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

tests/test_application.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import zigpy_znp.config as conf
1111

1212
import zigpy.device
13-
from zigpy.zdo.types import ZDOCmd
13+
from zigpy.zdo.types import ZDOCmd, SizePrefixedSimpleDescriptor
1414

1515
from zigpy_znp.uart import ZnpMtProtocol
1616

@@ -511,32 +511,41 @@ async def test_zdo_request_interception(application, mocker):
511511
app, znp_server = application
512512
await app.startup(auto_form=False)
513513

514-
device = app.add_device(ieee=t.EUI64(range(8)), nwk=0x0011)
514+
device = app.add_device(ieee=t.EUI64(range(8)), nwk=0xFA9E)
515515

516516
# Send back a request response
517517
active_ep_req = znp_server.reply_once_to(
518-
request=c.ZDOCommands.ActiveEpReq.Req(
519-
DstAddr=device.nwk, NWKAddrOfInterest=device.nwk
518+
request=c.ZDOCommands.SimpleDescReq.Req(
519+
DstAddr=device.nwk, NWKAddrOfInterest=device.nwk, Endpoint=1
520520
),
521521
responses=[
522-
c.ZDOCommands.ActiveEpReq.Rsp(Status=t.Status.Success),
523-
c.ZDOCommands.ActiveEpRsp.Callback(
522+
c.ZDOCommands.SimpleDescReq.Rsp(Status=t.Status.Success),
523+
c.ZDOCommands.SimpleDescRsp.Callback(
524524
Src=device.nwk,
525525
Status=t.ZDOStatus.SUCCESS,
526-
ActiveEndpoints=[0, 1, 2],
527526
NWK=device.nwk,
527+
SimpleDescriptor=SizePrefixedSimpleDescriptor(
528+
*dict(
529+
endpoint=1,
530+
profile=49246,
531+
device_type=256,
532+
device_version=2,
533+
input_clusters=[0, 3, 4, 5, 6, 8, 2821, 4096],
534+
output_clusters=[5, 25, 32, 4096],
535+
).values()
536+
),
528537
),
529538
],
530539
)
531540

532541
status, message = await app.request(
533542
device=device,
534543
profile=260,
535-
cluster=ZDOCmd.Active_EP_req,
544+
cluster=ZDOCmd.Simple_Desc_req,
536545
src_ep=0,
537546
dst_ep=0,
538-
sequence=0,
539-
data=b"test",
547+
sequence=1,
548+
data=b"\x01\x9e\xfa\x01",
540549
use_ieee=False,
541550
)
542551

zigpy_znp/zigbee/application.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import zigpy.profiles
1313
import zigpy.zcl.foundation
1414

15-
from zigpy.zdo.types import ZDOCmd
15+
from zigpy.zdo.types import ZDOCmd, ZDOHeader, CLUSTERS as ZDO_CLUSTERS
1616

17-
from zigpy.types import ExtendedPanId
17+
from zigpy.types import ExtendedPanId, deserialize as list_deserialize
1818
from zigpy.zcl.clusters.security import IasZone
1919

2020
import zigpy_znp.config as conf
@@ -35,8 +35,8 @@
3535
ZDOCmd.Node_Desc_req: (
3636
ZDOCmd.Node_Desc_rsp,
3737
(
38-
lambda addr, ep: c.ZDOCommands.NodeDescReq.Req(
39-
DstAddr=addr, NWKAddrOfInterest=addr
38+
lambda addr, NWKAddrOfInterest: c.ZDOCommands.NodeDescReq.Req(
39+
DstAddr=addr, NWKAddrOfInterest=NWKAddrOfInterest
4040
)
4141
),
4242
(
@@ -49,8 +49,8 @@
4949
ZDOCmd.Active_EP_req: (
5050
ZDOCmd.Active_EP_rsp,
5151
(
52-
lambda addr, ep: c.ZDOCommands.ActiveEpReq.Req(
53-
DstAddr=addr, NWKAddrOfInterest=addr
52+
lambda addr, NWKAddrOfInterest: c.ZDOCommands.ActiveEpReq.Req(
53+
DstAddr=addr, NWKAddrOfInterest=NWKAddrOfInterest
5454
)
5555
),
5656
(
@@ -63,8 +63,8 @@
6363
ZDOCmd.Simple_Desc_req: (
6464
ZDOCmd.Simple_Desc_rsp,
6565
(
66-
lambda addr, ep: c.ZDOCommands.SimpleDescReq.Req(
67-
DstAddr=addr, NWKAddrOfInterest=addr, Endpoint=ep
66+
lambda addr, NWKAddrOfInterest, EndPoint: c.ZDOCommands.SimpleDescReq.Req(
67+
DstAddr=addr, NWKAddrOfInterest=NWKAddrOfInterest, Endpoint=EndPoint
6868
)
6969
),
7070
(
@@ -474,8 +474,15 @@ async def _send_zdo_request(
474474

475475
assert dst_ep == ZDO_ENDPOINT
476476

477+
# Deserialize the ZDO request
478+
zdo_hdr, data = ZDOHeader.deserialize(cluster, data)
479+
field_names, field_types = ZDO_CLUSTERS[cluster]
480+
zdo_args, _ = list_deserialize(data, field_types)
481+
zdo_kwargs = dict(zip(field_names, zdo_args))
482+
483+
# Call the converter with the ZDO request's kwargs
477484
rsp_cluster, req_factory, callback_factory, converter = ZDO_CONVERTERS[cluster]
478-
request = req_factory(dst_addr.address, ep=src_ep)
485+
request = req_factory(dst_addr.address, **zdo_kwargs)
479486
callback = callback_factory(dst_addr.address)
480487

481488
LOGGER.debug(

0 commit comments

Comments
 (0)