Skip to content

Commit 97e4c24

Browse files
committed
Write network settings directly to NVRAM
1 parent 1585a0c commit 97e4c24

File tree

5 files changed

+260
-190
lines changed

5 files changed

+260
-190
lines changed

tests/conftest.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,7 @@ def connection_lost(self, exc):
393393

394394
return super().connection_lost(exc)
395395

396-
def _startup(self):
397-
startup_option = self.nvram_deserialize(
398-
self._nvram[ExNvIds.LEGACY].get(OsalNvIds.STARTUP_OPTION, b"\x00"),
399-
t.StartupOptions,
400-
)
401-
402-
if not startup_option & t.StartupOptions.ClearState:
403-
return
404-
396+
def _create_network_nvram(self):
405397
self.nib = self._default_nib()
406398

407399
empty_key = t.NwkActiveKeyItems(
@@ -663,10 +655,7 @@ def nib(self, nib):
663655
self._nvram[ExNvIds.LEGACY][OsalNvIds.NIB] = self.nvram_serialize(nib)
664656

665657
@reply_to(c.SYS.ResetReq.Req(Type=t.ResetType.Soft))
666-
def reset_req(self, request, *, startup=True):
667-
if startup:
668-
self._startup()
669-
658+
def reset_req(self, request):
670659
version = self.version_replier(None)
671660

672661
return c.SYS.ResetInd.Callback(
@@ -787,6 +776,8 @@ def startup_from_app(self, req):
787776
self.update_device_state(t.DeviceState.StartedAsCoordinator),
788777
]
789778
else:
779+
self._create_network_nvram()
780+
790781
return [
791782
c.ZDO.StartupFromApp.Rsp(State=c.zdo.StartupState.NewNetworkState),
792783
self.update_device_state(t.DeviceState.StartingAsCoordinator),
@@ -913,6 +904,8 @@ def handle_bdb_start_commissioning(self, request):
913904
),
914905
]
915906
else:
907+
self._create_network_nvram()
908+
916909
return [
917910
c.AppConfig.BDBStartCommissioning.Rsp(Status=t.Status.SUCCESS),
918911
self.update_device_state(t.DeviceState.StartingAsCoordinator),
@@ -956,9 +949,7 @@ def connection_made(self):
956949
self._first_connection = False
957950

958951
# Z-Stack 3 devices send a callback when they're first used
959-
asyncio.get_running_loop().call_soon(
960-
self.send, self.reset_req(None, startup=False)
961-
)
952+
asyncio.get_running_loop().call_soon(self.send, self.reset_req(None))
962953

963954

964955
class BaseLaunchpadCC26X2R1(BaseZStack3Device):
@@ -1114,6 +1105,21 @@ def version_replier(self, request):
11141105
def led_responder(self, req):
11151106
return req.Rsp(Status=t.Status.SUCCESS)
11161107

1108+
@reply_to(
1109+
c.AppConfig.BDBStartCommissioning.Req(
1110+
Mode=c.app_config.BDBCommissioningMode.NwkFormation
1111+
)
1112+
)
1113+
def handle_bdb_start_commissioning(self, request):
1114+
result = super().handle_bdb_start_commissioning(request)
1115+
1116+
# This item is only created after a network is formed
1117+
self._nvram[ExNvIds.LEGACY][OsalNvIds.ADDRMGR] = 124 * self.nvram_serialize(
1118+
t.EMPTY_ADDR_MGR_ENTRY
1119+
)
1120+
1121+
return result
1122+
11171123

11181124
class FormedLaunchpadCC26X2R1(BaseLaunchpadCC26X2R1):
11191125
def __init__(self, *args, **kwargs):

zigpy_znp/api.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ async def load_network_info(self, *, load_keys=False):
8686

8787
try:
8888
nib = await self.nvram.osal_read(OsalNvIds.NIB, item_type=t.NIB)
89-
is_on_network = nib.nwkLogicalChannel != 0 and nib.nwkKeyLoaded
9089
except KeyError:
9190
is_on_network = False
9291
else:
92+
is_on_network = nib.nwkLogicalChannel != 0 and nib.nwkKeyLoaded
93+
9394
if is_on_network and self.version >= 3.0:
9495
# This NVRAM item is the very first thing initialized in `zgInit`
9596
is_on_network = (
@@ -133,11 +134,16 @@ async def load_network_info(self, *, load_keys=False):
133134
),
134135
tc_link_key=None,
135136
key_table=[],
136-
stack_specific={
137-
"TCLK_SEED": tclk_seed,
138-
},
137+
stack_specific=None,
139138
)
140139

140+
if tclk_seed is not None:
141+
network_info.stack_specific = {
142+
"zstack": {
143+
"tclk_seed": tclk_seed.serialize().hex(),
144+
}
145+
}
146+
141147
# This takes a few seconds
142148
if load_keys:
143149
for device in await security.read_devices(self):

zigpy_znp/tools/network_backup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ async def backup_network(znp: ZNP) -> t.JSONType:
7070
"devices": devices,
7171
}
7272

73-
if znp.network_info.stack_specific:
74-
obj["stack_specific"] = znp.network_info.stack_specific
73+
if znp.network_info.stack_specific.get("zstack", {}).get("tclk_seed"):
74+
obj["stack_specific"] = {
75+
"zstack": {
76+
"tclk_seed": znp.network_info.stack_specific["zstack"]["tclk_seed"]
77+
}
78+
}
7579

7680
# Ensure our generated backup is valid
7781
validate_backup_json(obj)

0 commit comments

Comments
 (0)