pyvisionect 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. pyvisionect/__init__.py +120 -0
  2. pyvisionect/devices/__init__.py +92 -0
  3. pyvisionect/devices/enums.py +403 -0
  4. pyvisionect/devices/panels.py +242 -0
  5. pyvisionect/devices/tclv.py +460 -0
  6. pyvisionect/imaging/README.md +175 -0
  7. pyvisionect/imaging/__init__.py +181 -0
  8. pyvisionect/imaging/checksum.py +114 -0
  9. pyvisionect/imaging/constants.py +131 -0
  10. pyvisionect/imaging/data/blue_noise_64.npy +0 -0
  11. pyvisionect/imaging/decode.py +172 -0
  12. pyvisionect/imaging/dither.py +357 -0
  13. pyvisionect/imaging/encoder.py +594 -0
  14. pyvisionect/imaging/geometry.py +129 -0
  15. pyvisionect/imaging/grey.py +104 -0
  16. pyvisionect/imaging/interlace.py +244 -0
  17. pyvisionect/imaging/pack.py +142 -0
  18. pyvisionect/imaging/panel.py +339 -0
  19. pyvisionect/imaging/partial.py +931 -0
  20. pyvisionect/imaging/rects.py +466 -0
  21. pyvisionect/io/__init__.py +33 -0
  22. pyvisionect/io/tcp.py +795 -0
  23. pyvisionect/io/usb/__init__.py +237 -0
  24. pyvisionect/io/usb/cli.py +278 -0
  25. pyvisionect/io/usb/commands.py +781 -0
  26. pyvisionect/io/usb/console.py +730 -0
  27. pyvisionect/io/usb/device.py +914 -0
  28. pyvisionect/io/usb/discovery.py +260 -0
  29. pyvisionect/io/usb/encryption.py +262 -0
  30. pyvisionect/io/usb/errors.py +175 -0
  31. pyvisionect/io/usb/fake.py +292 -0
  32. pyvisionect/io/usb/parsers.py +1464 -0
  33. pyvisionect/io/usb/provisioning.py +502 -0
  34. pyvisionect/packets/__init__.py +135 -0
  35. pyvisionect/packets/control.py +95 -0
  36. pyvisionect/packets/file.py +325 -0
  37. pyvisionect/packets/image.py +234 -0
  38. pyvisionect/packets/misc.py +202 -0
  39. pyvisionect/packets/param.py +284 -0
  40. pyvisionect/packets/status.py +518 -0
  41. pyvisionect/packets/stored.py +217 -0
  42. pyvisionect/py.typed +0 -0
  43. pyvisionect/session/__init__.py +100 -0
  44. pyvisionect/session/connection.py +1043 -0
  45. pyvisionect/session/device.py +587 -0
  46. pyvisionect/session/events.py +196 -0
  47. pyvisionect/session/filetransfer.py +251 -0
  48. pyvisionect/session/pending.py +425 -0
  49. pyvisionect/wire/__init__.py +91 -0
  50. pyvisionect/wire/blocks.py +350 -0
  51. pyvisionect/wire/checksum.py +115 -0
  52. pyvisionect/wire/errors.py +176 -0
  53. pyvisionect/wire/framing.py +334 -0
  54. pyvisionect-0.1.0.dist-info/METADATA +1030 -0
  55. pyvisionect-0.1.0.dist-info/RECORD +58 -0
  56. pyvisionect-0.1.0.dist-info/WHEEL +4 -0
  57. pyvisionect-0.1.0.dist-info/entry_points.txt +3 -0
  58. pyvisionect-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,217 @@
1
+ """Reading a ``.pv2`` file off the device's flash.
2
+
3
+ A ``.pv2`` is a **stored protocol frame**: the same container the wire uses,
4
+ written to the filesystem. Layout, confirmed byte by byte against
5
+ ``/image0.pv2`` .. ``/image5.pv2`` on firmware 7.4.4407::
6
+
7
+ ProtocolHeader 20 B Version=2 | Security=0 | Compression=1 | Length | Checksum
8
+ block chain BlocksMinusOne+1 records of 24 B + payload, 4800 B plaintext each
9
+ +-- DataHeader 36 B Priority | DeviceID[16] | Type=5 | ID | Length | Reserved
10
+ +-- ImageHeader 20 B + RectangleHeaders (:mod:`pyvisionect.packets.image`)
11
+
12
+ So the first 44 bytes -- ``ProtocolHeader`` plus the first block record's
13
+ header -- are enough to learn the whole shape of the file without inflating
14
+ anything: :class:`StoredFrameHeader`.
15
+
16
+ Three differences from a frame on the wire, all of them real:
17
+
18
+ * ``Version`` is **2**, not the 3 that :mod:`pyvisionect.wire.framing`
19
+ requires. :func:`parse_stored_frame` accepts either rather than refusing a
20
+ file the device plainly wrote itself.
21
+ * ``DataHeader.DeviceID`` is 16 zero bytes.
22
+ * ``ImageHeader.Checksum`` is **0**. Nothing we push has a zero checksum (the
23
+ vendor remaps a computed 0 to 1), so this is a reliable tell that a file was
24
+ not written from one of our frames -- and it is why identifying a stored
25
+ frame by its checksum, as ``OPEN-QUESTIONS.md`` B7 proposed, cannot work.
26
+
27
+ What these files are
28
+ --------------------
29
+
30
+ On this firmware they are Visionect's **shipped demo screens**, not
31
+ framebuffers: a wayfinding board, a museum label and four more, each a
32
+ full-canvas 4 bpp frame. Pushing a new image changes none of them -- not their
33
+ size, not their first bytes. See :data:`~pyvisionect.packets.file
34
+ .DEVICE_IMAGE_FILES`.
35
+
36
+ This module is still the right tool for them, and would be the right tool for a
37
+ firmware that *does* cache frames: it turns any ``.pv2`` on a device back into
38
+ an :class:`~pyvisionect.packets.image.ImagePacket`, and
39
+ :func:`pyvisionect.imaging.decode_image_packet` turns that into pixels.
40
+ """
41
+
42
+ from __future__ import annotations
43
+
44
+ import struct
45
+ from dataclasses import dataclass
46
+
47
+ from ..wire.blocks import BLOCK_HEADER_SIZE, BLOCK_PLAINTEXT_SIZE, decode_blocks
48
+ from ..wire.errors import PayloadError
49
+ from .image import ImagePacket
50
+
51
+ __all__ = [
52
+ "STORED_FRAME_HEADER_SIZE",
53
+ "StoredFrame",
54
+ "StoredFrameHeader",
55
+ "parse_stored_frame",
56
+ "parse_stored_frame_header",
57
+ ]
58
+
59
+ STORED_FRAME_HEADER_SIZE = 20 + BLOCK_HEADER_SIZE
60
+ """44 bytes: ``ProtocolHeader`` plus the first block record's header."""
61
+
62
+ _PROTO = struct.Struct("<5I")
63
+ _BLOCK = struct.Struct("<6I")
64
+ _DATA = struct.Struct("<I16sIIII")
65
+ _DATA_SIZE = 36
66
+
67
+ _ACCEPTED_VERSIONS = frozenset({2, 3})
68
+
69
+
70
+ @dataclass(frozen=True, slots=True)
71
+ class StoredFrameHeader:
72
+ """What the first :data:`STORED_FRAME_HEADER_SIZE` bytes of a ``.pv2`` say.
73
+
74
+ Cheap: five 1024-byte reads get you this plus the whole of block 0, which
75
+ is ~2 s on the link. A whole file is minutes.
76
+ """
77
+
78
+ version: int
79
+ security: int
80
+ compression: int
81
+ length: int
82
+ """``ProtocolHeader.Length`` -- the file's size minus the 20-byte header."""
83
+ checksum: int
84
+ """``ProtocolHeader.Checksum``, the device's own frame CRC. Distinct per
85
+ file, but not a value we can predict for a frame we sent, so it identifies
86
+ a file only against a previous reading of the same file."""
87
+ nblocks: int
88
+ """``BlocksMinusOne + 1`` from the first block record."""
89
+ firstblocklen: int
90
+ """``PayloadLength`` of block 0, i.e. its compressed size."""
91
+ blocksize: int
92
+ """``UncompressedLength`` of block 0 -- 4800 on every file seen."""
93
+ stored: int
94
+ """1 if block 0 is verbatim, 0 if it is a raw LZ4 block."""
95
+
96
+ @property
97
+ def total_size(self) -> int:
98
+ """The file's size on flash, as the directory listing reports it."""
99
+ return 20 + self.length
100
+
101
+ @property
102
+ def first_block_end(self) -> int:
103
+ """Byte offset just past block 0 -- how much to read to decode it."""
104
+ return STORED_FRAME_HEADER_SIZE + self.firstblocklen
105
+
106
+ @property
107
+ def plaintext_size(self) -> int:
108
+ """Upper bound on the inflated size (the last block may be shorter)."""
109
+ return self.nblocks * self.blocksize
110
+
111
+
112
+ def parse_stored_frame_header(raw: bytes) -> StoredFrameHeader:
113
+ """Read the 44-byte prefix of a ``.pv2``.
114
+
115
+ Raises:
116
+ PayloadError: if *raw* is shorter than
117
+ :data:`STORED_FRAME_HEADER_SIZE` or does not look like a stored
118
+ frame.
119
+ """
120
+ if len(raw) < STORED_FRAME_HEADER_SIZE:
121
+ raise PayloadError(
122
+ f"a stored frame header is {STORED_FRAME_HEADER_SIZE} bytes, "
123
+ f"got {len(raw)}"
124
+ )
125
+ version, security, compression, length, checksum = _PROTO.unpack_from(raw, 0)
126
+ if version not in _ACCEPTED_VERSIONS:
127
+ raise PayloadError(
128
+ f"stored frame ProtocolHeader.Version is {version}; expected 2 "
129
+ "(what the device writes to flash) or 3 (the wire version)"
130
+ )
131
+ index, blocks_minus_one, payload_length, uncompressed, stored, _reserved = (
132
+ _BLOCK.unpack_from(raw, 20)
133
+ )
134
+ if index != 0:
135
+ raise PayloadError(f"first block record has BlockIndex {index}, expected 0")
136
+ return StoredFrameHeader(
137
+ version=version,
138
+ security=security,
139
+ compression=compression,
140
+ length=length,
141
+ checksum=checksum,
142
+ nblocks=blocks_minus_one + 1,
143
+ firstblocklen=payload_length,
144
+ blocksize=uncompressed or BLOCK_PLAINTEXT_SIZE,
145
+ stored=stored,
146
+ )
147
+
148
+
149
+ @dataclass(frozen=True, slots=True)
150
+ class StoredFrame:
151
+ """A fully decoded ``.pv2``."""
152
+
153
+ header: StoredFrameHeader
154
+ device_id: bytes
155
+ """``DataHeader.DeviceID``. 16 zero bytes in every shipped demo file."""
156
+ packet_type: int
157
+ """``DataHeader.Type``. 5 (image) in every file seen."""
158
+ packet_id: int
159
+ image: ImagePacket
160
+
161
+ @property
162
+ def checksum(self) -> int:
163
+ """``ImageHeader.Checksum``. **0** in every shipped demo file."""
164
+ return self.image.checksum
165
+
166
+ @property
167
+ def looks_like_our_push(self) -> bool:
168
+ """Whether this file could have come from a frame we sent.
169
+
170
+ A frame we built always carries a non-zero ``ImageHeader.Checksum``
171
+ and the device's real UUID. A file that has neither was written by
172
+ something else -- on this firmware, by the factory.
173
+ """
174
+ return self.checksum != 0 and self.device_id != b"\x00" * 16
175
+
176
+
177
+ def parse_stored_frame(raw: bytes, *, strict: bool = False) -> StoredFrame:
178
+ """Decode a whole ``.pv2`` into its :class:`~pyvisionect.packets.image.ImagePacket`.
179
+
180
+ Args:
181
+ raw: the complete file, starting at its ``ProtocolHeader``.
182
+ strict: also validate the block chain's indices and invariants.
183
+
184
+ Raises:
185
+ PayloadError: on a truncated file or a header that is not a stored frame.
186
+ BlockError: on a malformed block chain.
187
+ """
188
+ header = parse_stored_frame_header(raw)
189
+ end = 20 + header.length
190
+ if len(raw) < end:
191
+ raise PayloadError(
192
+ f"stored frame declares {header.length} body bytes "
193
+ f"({end} total) but only {len(raw)} are present; the read was "
194
+ "short -- there is no seek opcode, so restart it from offset 0"
195
+ )
196
+ plain = decode_blocks(raw[20:end], strict=strict)
197
+ if len(plain) < _DATA_SIZE:
198
+ raise PayloadError(
199
+ f"stored frame inflated to {len(plain)} bytes, too short for a "
200
+ f"{_DATA_SIZE}-byte DataHeader"
201
+ )
202
+ _priority, device_id, packet_type, packet_id, length, _reserved = _DATA.unpack_from(
203
+ plain, 0
204
+ )
205
+ body = plain[_DATA_SIZE : _DATA_SIZE + length]
206
+ if len(body) < length:
207
+ raise PayloadError(
208
+ f"DataHeader declares a {length}-byte payload, only {len(body)} "
209
+ "bytes follow it"
210
+ )
211
+ return StoredFrame(
212
+ header=header,
213
+ device_id=device_id,
214
+ packet_type=packet_type,
215
+ packet_id=packet_id,
216
+ image=ImagePacket.decode(body),
217
+ )
pyvisionect/py.typed ADDED
File without changes
@@ -0,0 +1,100 @@
1
+ """Session layer: the sans-io ``DeviceConnection`` and per-UUID device state."""
2
+
3
+ from .connection import (
4
+ DEFAULT_DEVICE_STATE_POLLING,
5
+ DEFAULT_PACKET_TIMEOUT,
6
+ MIN_WATCHDOG_FIRMWARE_REVISION,
7
+ WATCHDOG_FALLBACK,
8
+ WATCHDOG_JITTER_MAX,
9
+ ConnectionConfig,
10
+ DeviceConnection,
11
+ )
12
+ from .connection import SLEEP_MIN_MINUTES
13
+ from .device import (
14
+ CONVERGENCE_CONTACTS,
15
+ DEFAULT_CONTACT_INTERVAL,
16
+ DRAW_ALLOWANCE,
17
+ RECTANGLE_UNSUPPORTED_HARDWARE,
18
+ SCREEN_RECTANGLE_VERIFIED_HARDWARE,
19
+ DeviceState,
20
+ DeviceStateStore,
21
+ SyncStatus,
22
+ )
23
+ from .filetransfer import FileRead, FileReadState
24
+ from .pending import (
25
+ PENDING_ORDER,
26
+ SLOT_FLASH_SAVE,
27
+ SLOT_FRAMEBUFFER_READ,
28
+ SLOT_GHOST_CLEAR,
29
+ SLOT_IMAGE,
30
+ SLOT_PARAM_READS,
31
+ SLOT_PARAM_WRITES,
32
+ SLOT_REBOOT,
33
+ SLOT_SLEEP,
34
+ PendingWork,
35
+ )
36
+ from .events import (
37
+ Acked,
38
+ ButtonReceived,
39
+ CborReceived,
40
+ DeviceConnected,
41
+ Event,
42
+ FileEventReceived,
43
+ GpsReceived,
44
+ HeartbeatOverdue,
45
+ Nacked,
46
+ ParamsReceived,
47
+ ProtocolViolation,
48
+ ReadTimeout,
49
+ StatusReceived,
50
+ TouchReceived,
51
+ UnknownPacketType,
52
+ WatchdogFired,
53
+ )
54
+
55
+ __all__ = [
56
+ "Acked",
57
+ "CONVERGENCE_CONTACTS",
58
+ "DEFAULT_CONTACT_INTERVAL",
59
+ "DRAW_ALLOWANCE",
60
+ "FileRead",
61
+ "FileReadState",
62
+ "SyncStatus",
63
+ "ButtonReceived",
64
+ "CborReceived",
65
+ "ConnectionConfig",
66
+ "DEFAULT_DEVICE_STATE_POLLING",
67
+ "DEFAULT_PACKET_TIMEOUT",
68
+ "DeviceConnected",
69
+ "DeviceConnection",
70
+ "DeviceState",
71
+ "DeviceStateStore",
72
+ "Event",
73
+ "FileEventReceived",
74
+ "GpsReceived",
75
+ "HeartbeatOverdue",
76
+ "MIN_WATCHDOG_FIRMWARE_REVISION",
77
+ "Nacked",
78
+ "PENDING_ORDER",
79
+ "PendingWork",
80
+ "SLEEP_MIN_MINUTES",
81
+ "SLOT_FLASH_SAVE",
82
+ "SLOT_FRAMEBUFFER_READ",
83
+ "SLOT_GHOST_CLEAR",
84
+ "SLOT_IMAGE",
85
+ "SLOT_PARAM_READS",
86
+ "SLOT_PARAM_WRITES",
87
+ "SLOT_REBOOT",
88
+ "SLOT_SLEEP",
89
+ "ParamsReceived",
90
+ "ProtocolViolation",
91
+ "RECTANGLE_UNSUPPORTED_HARDWARE",
92
+ "SCREEN_RECTANGLE_VERIFIED_HARDWARE",
93
+ "ReadTimeout",
94
+ "StatusReceived",
95
+ "TouchReceived",
96
+ "UnknownPacketType",
97
+ "WATCHDOG_FALLBACK",
98
+ "WATCHDOG_JITTER_MAX",
99
+ "WatchdogFired",
100
+ ]