Skip to content

Commit 5b21f3a

Browse files
committed
Handle global TC link keys
1 parent d859823 commit 5b21f3a

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

zigpy_znp/api.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ async def load_network_info(self, *, load_devices=False):
111111
if not is_on_network:
112112
raise NetworkNotFormed("Device is not a part of a network")
113113

114+
ieee = await self.nvram.osal_read(OsalNvIds.EXTADDR, item_type=t.EUI64)
115+
logical_type = await self.nvram.osal_read(
116+
OsalNvIds.LOGICAL_TYPE, item_type=t.DeviceLogicalType
117+
)
118+
119+
node_info = zigpy.state.NodeInfo(
120+
ieee=ieee,
121+
nwk=nib.nwkDevAddress,
122+
logical_type=zdo_t.LogicalType(logical_type),
123+
)
124+
114125
key_desc = await self.nvram.osal_read(
115126
OsalNvIds.NWK_ACTIVE_KEY_INFO, item_type=t.NwkKeyDesc
116127
)
@@ -130,17 +141,28 @@ async def load_network_info(self, *, load_devices=False):
130141
key=key_desc.Key,
131142
seq=key_desc.KeySeqNum,
132143
tx_counter=tc_frame_counter,
133-
rx_counter=None,
144+
rx_counter=0,
145+
partner_ieee=node_info.ieee,
146+
),
147+
tc_link_key=zigpy.state.Key(
148+
key=None,
149+
seq=0,
150+
tx_counter=0,
151+
rx_counter=0,
134152
partner_ieee=None,
135153
),
136-
tc_link_key=None,
137154
children=[],
138155
nwk_addresses={},
139156
key_table=[],
140157
stack_specific=None,
141158
)
142159

143-
if self.version > 1.2:
160+
if self.version == 1.2:
161+
tc_link_key = await self.nvram.osal_read(
162+
OsalNvIds.TCLK_SEED, item_type=t.TCLinkKey
163+
)
164+
network_info.tc_link_key.key = tc_link_key.key
165+
else:
144166
tclk_seed = await self.nvram.osal_read(
145167
OsalNvIds.TCLK_SEED, item_type=t.KeyData
146168
)
@@ -167,17 +189,6 @@ async def load_network_info(self, *, load_devices=False):
167189
if dev.key is not None:
168190
network_info.key_table.append(dev.key)
169191

170-
ieee = await self.nvram.osal_read(OsalNvIds.EXTADDR, item_type=t.EUI64)
171-
logical_type = await self.nvram.osal_read(
172-
OsalNvIds.LOGICAL_TYPE, item_type=t.DeviceLogicalType
173-
)
174-
175-
node_info = zigpy.state.NodeInfo(
176-
ieee=ieee,
177-
nwk=nib.nwkDevAddress,
178-
logical_type=zdo_t.LogicalType(logical_type),
179-
)
180-
181192
self.network_info = network_info
182193
self.node_info = node_info
183194

@@ -357,20 +368,33 @@ async def write_network_info(
357368

358369
tclk_seed = None
359370

360-
if self.version > 1.2:
371+
if self.version == 1.2:
372+
# TCLK_SEED is TCLK_TABLE_START in Z-Stack 1
373+
nvram[OsalNvIds.TCLK_SEED] = t.TCLinkKey(
374+
ExtAddr=t.EUI64.convert("FF:FF:FF:FF:FF:FF:FF:FF"), # global
375+
Key=network_info.tc_link_key.key,
376+
TxFrameCounter=0,
377+
RxFrameCounter=0,
378+
)
379+
else:
380+
if network_info.tc_link_key.key != const.DEFAULT_TC_LINK_KEY:
381+
LOGGER.warning(
382+
"TC link key is configured at build time in Z-Stack 3 and cannot be"
383+
" changed at runtime: %s",
384+
network_info.tc_link_key.key,
385+
)
386+
361387
if (
362388
network_info.stack_specific is not None
363389
and network_info.stack_specific.get("zstack", {}).get("tclk_seed")
364390
):
365-
tclk_seed, _ = t.KeyData.deserialize(
391+
tclk_seed = t.KeyData(
366392
bytes.fromhex(network_info.stack_specific["zstack"]["tclk_seed"])
367393
)
368394
else:
369395
tclk_seed = t.KeyData(os.urandom(16))
370396

371397
nvram[OsalNvIds.TCLK_SEED] = tclk_seed
372-
else:
373-
nvram[OsalNvIds.TCLK_SEED] = const.DEFAULT_TC_LINK_KEY
374398

375399
for key, value in nvram.items():
376400
await self.nvram.osal_write(key, value, create=True)

zigpy_znp/const.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
Z2M_EXT_PAN_ID = t.EUI64.convert("DD:DD:DD:DD:DD:DD:DD:DD")
55
Z2M_NETWORK_KEY = t.KeyData([1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 13])
66

7-
DEFAULT_TC_LINK_KEY = t.TCLinkKey(
8-
ExtAddr=t.EUI64.convert("FF:FF:FF:FF:FF:FF:FF:FF"), # global
9-
Key=t.KeyData(b"ZigBeeAlliance09"),
10-
TxFrameCounter=0,
11-
RxFrameCounter=0,
12-
)
7+
DEFAULT_TC_LINK_KEY = t.KeyData(b"ZigBeeAlliance09")
138
ZSTACK_CONFIGURE_SUCCESS = t.uint8_t(0x55)
149

1510
EMPTY_ADDR_MGR_ENTRY_ZSTACK1 = t.AddrMgrEntry(

0 commit comments

Comments
 (0)