File: nvram.py

package info (click to toggle)
zigpy-znp 0.14.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,112 kB
  • sloc: python: 14,241; makefile: 6
file content (420 lines) | stat: -rw-r--r-- 13,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import logging
import itertools

import zigpy_znp.types as t
import zigpy_znp.commands as c
from zigpy_znp.types import nvids
from zigpy_znp.exceptions import SecurityError, InvalidCommandResponse

LOGGER = logging.getLogger(__name__)


# Some NVIDs don't really exist and Z-Stack doesn't behave consistently when operations
# are performed on them.
PROXIED_NVIDS = {nvids.OsalNvIds.POLL_RATE_OLD16}


class NVRAMHelper:
    def __init__(self, znp):
        self.znp = znp
        self.align_structs = None

    async def determine_alignment(self) -> None:
        """
        Automatically determine struct memory alignment. Must be called before any
        structs are read/written.
        """

        # This is the only known MT command to respond with a struct's in-memory
        # representation over serial.
        LOGGER.debug("Detecting struct alignment")
        rsp = await self.znp.request(c.UTIL.AssocFindDevice.Req(Index=0))

        if len(rsp.Device) == 28:
            self.align_structs = False
        elif len(rsp.Device) == 36:
            # `AssociatedDevice` has an extra member at the end in Z-Stack 3.30 but
            # the struct does not change in size due to padding.
            self.align_structs = True
        else:
            raise ValueError(f"Cannot determine alignment from struct: {rsp!r}")

        LOGGER.debug("Detected struct alignment: %s", self.align_structs)

    def serialize(self, value) -> bytes:
        """
        Serialize an object, automatically computing struct padding based on the target
        platform.
        """

        if hasattr(value, "serialize"):
            if isinstance(value, (t.CStruct, t.BaseListType)):
                assert self.align_structs is not None
                value = value.serialize(align=self.align_structs)
            else:
                value = value.serialize()
        elif not isinstance(value, (bytes, bytearray)):
            raise TypeError(
                f"Only bytes or serializable types can be written to NVRAM."
                f" Got {value!r} (type {type(value)})"
            )

        if not value:
            raise ValueError("NVRAM value cannot be empty")

        return value

    def deserialize(self, data: bytes, item_type, *, allow_trailing=False):
        """
        Serialize raw bytes, automatically computing struct padding based on the target
        platform.
        """

        if issubclass(item_type, (t.CStruct, t.BaseListType)):
            assert self.align_structs is not None
            value, remaining = item_type.deserialize(data, align=self.align_structs)
        else:
            value, remaining = item_type.deserialize(data)

        if remaining and not allow_trailing:
            raise ValueError(f"Data left after deserialization: {remaining!r}")

        return value

    async def osal_delete(self, nv_id: t.uint16_t) -> bool:
        """
        Deletes an item from NVRAM. Returns whether or not the item existed.
        """

        length = (await self.znp.request(c.SYS.OSALNVLength.Req(Id=nv_id))).ItemLen

        if length == 0:
            return False

        delete_rsp = await self.znp.request(
            c.SYS.OSALNVDelete.Req(Id=nv_id, ItemLen=length)
        )

        return delete_rsp.Status == t.Status.SUCCESS

    async def osal_write(self, nv_id: t.uint16_t, value, *, create: bool = False):
        """
        Writes a complete value to NVRAM, optionally resizing and creating the item if
        necessary.

        Serializes all serializable values and passes bytes directly.
        """

        value = self.serialize(value)
        length = (await self.znp.request(c.SYS.OSALNVLength.Req(Id=nv_id))).ItemLen

        # Recreate the item if the length is not correct
        if length != len(value) and nv_id not in PROXIED_NVIDS:
            if not create:
                if length == 0:
                    raise KeyError(f"NV item does not exist: {nv_id!r}")
                else:
                    raise ValueError(
                        f"Stored length and actual length differ:"
                        f" {length} != {len(value)}"
                    )

            if length != 0:
                await self.znp.request(
                    c.SYS.OSALNVDelete.Req(Id=nv_id, ItemLen=length),
                    RspStatus=t.Status.SUCCESS,
                )

            await self.znp.request(
                c.SYS.OSALNVItemInit.Req(
                    Id=nv_id,
                    ItemLen=len(value),
                    Value=t.ShortBytes(value[:244]),
                ),
                RspStatus=t.Status.NV_ITEM_UNINIT,
            )

        # 244 bytes is the most you can fit in a single `SYS.OSALNVWriteExt` command
        for offset in range(0, len(value), 244):
            await self.znp.request(
                c.SYS.OSALNVWriteExt.Req(
                    Id=nv_id,
                    Offset=offset,
                    Value=t.ShortBytes(value[offset : offset + 244]),
                ),
                RspStatus=t.Status.SUCCESS,
            )

    async def osal_read(self, nv_id: t.uint16_t, *, item_type):
        """
        Reads a complete value from NVRAM.

        Raises an `KeyError` error if the NVID doesn't exist.
        """

        # XXX: Some NVIDs don't really exist and Z-Stack behaves strangely with them
        if nv_id in PROXIED_NVIDS:
            read_rsp = await self.znp.request(
                c.SYS.OSALNVRead.Req(Id=nv_id, Offset=0),
                RspStatus=t.Status.SUCCESS,
            )

            value = self.deserialize(read_rsp.Value, item_type)
            LOGGER.debug('Read NVRAM["LEGACY"][0x%04x] = %r', nv_id, value)

            return value

        # Every item has a length, even missing ones
        length = (await self.znp.request(c.SYS.OSALNVLength.Req(Id=nv_id))).ItemLen

        if length == 0:
            raise KeyError(f"NV item does not exist: {nv_id!r}")

        data = b""

        try:
            while len(data) < length:
                read_rsp = await self.znp.request(
                    c.SYS.OSALNVReadExt.Req(Id=nv_id, Offset=len(data)),
                    RspStatus=t.Status.SUCCESS,
                )

                data += read_rsp.Value
        except InvalidCommandResponse as e:
            # Only expected status code is INVALID_PARAMETER
            assert e.response.Status == t.Status.INVALID_PARAMETER

            # Not all items can be read out due to security policies, though this can
            # easily be bypassed for some. The SAPI "ConfigId" is only 8 bits which
            # means some nvids are not able to read this way.
            if not self.znp.capabilities & t.MTCapabilities.SAPI or nv_id > 0xFF:
                raise SecurityError(
                    f"NV item cannot be read due to security constraints: {nv_id!r}"
                )

            read_rsp = await self.znp.request(
                c.SAPI.ZBReadConfiguration.Req(ConfigId=nv_id),
                RspStatus=t.Status.SUCCESS,
                RspConfigId=nv_id,
            )

            data = read_rsp.Value

        assert len(data) == length

        value = self.deserialize(data, item_type)
        LOGGER.debug('Read NVRAM["LEGACY"][0x%04x] = %r', nv_id, value)

        return value

    async def delete(
        self,
        *,
        sys_id: t.uint8_t = nvids.NvSysIds.ZSTACK,
        item_id: t.uint16_t,
        sub_id: t.uint16_t,
    ) -> bool:
        """
        Deletes a subitem from NVRAM. Returns whether or not the item existed.
        """

        delete_rsp = await self.znp.request(
            c.SYS.NVDelete.Req(SysId=sys_id, ItemId=item_id, SubId=sub_id)
        )

        return delete_rsp.Status == t.Status.SUCCESS

    async def write(
        self,
        *,
        sys_id: t.uint8_t = nvids.NvSysIds.ZSTACK,
        item_id: t.uint16_t,
        sub_id: t.uint16_t,
        value,
        create: bool = True,
    ) -> None:
        """
        Writes a value to NVRAM for the specified subsystem, item, and subitem.

        Calls to OSALNVWrite(sub_id=1) in newer Z-Stack releases are really calls to
        NVWrite(sys_id=ZSTACK, item_id=LEGACY, sub_id=1) in the background.
        """

        value = self.serialize(value)
        length = (
            await self.znp.request(
                c.SYS.NVLength.Req(SysId=sys_id, ItemId=item_id, SubId=sub_id)
            )
        ).Length

        if length != len(value) and not (
            sys_id == nvids.NvSysIds.ZSTACK
            and item_id in PROXIED_NVIDS
            and sub_id == 0x0000
        ):
            if not create:
                if length == 0:
                    raise KeyError(
                        f"NV item does not exist:"
                        f" sys_id={sys_id!r} item_id={item_id!r} sub_id={sub_id!r}"
                    )
                else:
                    raise ValueError(
                        f"Stored length and actual length differ:"
                        f" {length} != {len(value)}"
                    )

            if length != 0:
                await self.znp.request(
                    c.SYS.NVDelete.Req(SysId=sys_id, ItemId=item_id, SubId=sub_id),
                    RspStatus=t.Status.SUCCESS,
                )

            create_rsp = await self.znp.request(
                c.SYS.NVCreate.Req(
                    SysId=sys_id,
                    ItemId=item_id,
                    SubId=sub_id,
                    Length=len(value),
                )
            )

            if create_rsp.Status not in (t.Status.SUCCESS, t.Status.NV_ITEM_UNINIT):
                raise InvalidCommandResponse("Bad create status", create_rsp)

        # 244 bytes is the most you can fit in a single `SYS.NVWrite` command
        for offset in range(0, len(value), 244):
            await self.znp.request(
                c.SYS.NVWrite.Req(
                    SysId=sys_id,
                    ItemId=item_id,
                    SubId=sub_id,
                    Value=t.ShortBytes(value[offset : offset + 244]),
                    Offset=0,
                ),
                RspStatus=t.Status.SUCCESS,
            )

    async def read(
        self,
        *,
        sys_id: t.uint8_t = nvids.NvSysIds.ZSTACK,
        item_id: t.uint16_t,
        sub_id: t.uint16_t,
        item_type,
    ) -> bytes:
        """
        Reads a value from NVRAM for the specified subsystem, item, and subitem.

        Calls to OSALNVRead(sub_id=1) in newer Z-Stack releases are really calls to
        NVRead(sys_id=ZSTACK, item_id=LEGACY, sub_id=1) in the background.

        Raises an `KeyError` error if the NVID doesn't exist.
        """

        length_rsp = await self.znp.request(
            c.SYS.NVLength.Req(SysId=sys_id, ItemId=item_id, SubId=sub_id)
        )
        length = length_rsp.Length

        if length == 0:
            raise KeyError(
                f"NV item does not exist:"
                f" sys_id={sys_id!r} item_id={item_id!r} sub_id={sub_id!r}"
            )

        data = b""

        while len(data) < length:
            read_rsp = await self.znp.request(
                c.SYS.NVRead.Req(
                    SysId=sys_id,
                    ItemId=item_id,
                    SubId=sub_id,
                    Offset=len(data),
                    Length=length,
                ),
                RspStatus=t.Status.SUCCESS,
            )

            data += read_rsp.Value

        assert len(data) == length

        value = self.deserialize(data, item_type)
        LOGGER.debug("Read NVRAM[%s][%s][0x%04x] = %r", sys_id, item_id, sub_id, value)

        return value

    async def write_table(
        self,
        *,
        sys_id: t.uint8_t = nvids.NvSysIds.ZSTACK,
        item_id: t.uint16_t,
        values,
        fill_value,
    ) -> None:
        for sub_id, value in itertools.zip_longest(
            range(0x0000, 0xFFFF + 1), values, fillvalue=fill_value
        ):
            value = self.serialize(value)

            try:
                await self.write(
                    sys_id=sys_id,
                    item_id=item_id,
                    sub_id=sub_id,
                    value=value,
                    create=False,
                )
            except KeyError:
                break

    async def osal_write_table(
        self, start_nvid: t.uint16_t, end_nvid: t.uint16_t, values, *, fill_value
    ) -> None:
        values = list(values)

        for nvid, value in itertools.zip_longest(
            range(start_nvid, end_nvid + 1), values, fillvalue=fill_value
        ):
            value = self.serialize(value)

            try:
                await self.osal_write(
                    nv_id=nvid,
                    value=value,
                    create=False,
                )
            except KeyError:
                break

    async def read_table(
        self,
        *,
        sys_id: t.uint8_t = nvids.NvSysIds.ZSTACK,
        item_id: t.uint16_t,
        item_type=t.Bytes,
    ):
        for sub_id in range(0x0000, 0xFFFF + 1):
            try:
                yield await self.read(
                    sys_id=sys_id,
                    item_id=item_id,
                    sub_id=sub_id,
                    item_type=item_type,
                )
            except KeyError:
                break

    async def osal_read_table(
        self,
        start_nvid: t.uint16_t,
        end_nvid: t.uint16_t,
        item_type=t.Bytes,
    ):
        for nvid in range(start_nvid, end_nvid + 1):
            try:
                yield await self.osal_read(nvid, item_type=item_type)
            except KeyError:
                break