Packet en/de-coding and fragmentation/reassembly¶
Generators for encoding and decoding Simple Management Protocol (SMP) packets.
encode(request: bytes, line_length: int = 8192) -> Generator[bytes, None, None]
¶
Iteratively pack an SMP bytes to packets of line_length
size.
Note: only the USB/serial transport uses this encoding and fragmentation. The BLE and UDP transports can simply send the SMP frames as they are.
Params: - request: The bytes to be encoded. - line_length: The maximum length of each packet.
Example:
Encode arbitrary bytes to SMP packets:
prints:Set a smaller packet size to fragment:
prints:b'\x06\tAA9I\n'
b'\x04\x14ZWxs\n'
b'\x04\x14bywg\n'
b'\x04\x14d29y\n'
b'\x04\x14bGQh\n'
b'\x04\x14et4=\n'
The packet size should be set according to the buffer sizes of the SMP server, and larger buffers will be more efficient. It is not necessary to use multiple buffers.
Source code in smp/packet.py
decode() -> Generator[None, bytes, bytes]
¶
Iteratively unpack a series of SMP packets to SMP frame bytes.
Example:
from smp.packet import decode
from smp.image_management import ImageUploadWriteRequest
# Example encoded SMP packets
packets = (
bytes.fromhex("06094148344b41414230414147610a"),
bytes.fromhex("041441615a6a6247567544574e760a"),
bytes.fromhex("04145a6d59594b6d4e7a6147464d0a"),
bytes.fromhex("041464484a3163335167625755670a"),
bytes.fromhex("0414596e4a765a475268644746590a"),
bytes.fromhex("04145141414241674d45425159480a"),
bytes.fromhex("041443416b4b4377774e446738510a"),
bytes.fromhex("041445524954464255574678675a0a"),
bytes.fromhex("04144768736348523466494345690a"),
bytes.fromhex("04144979516c4a69636f4b536f720a"),
bytes.fromhex("04144c4330754c7a41784d6a4d300a"),
bytes.fromhex("04144e5459334f446b364f7a77390a"),
bytes.fromhex("0414506a396c615731685a3255410a"),
bytes.fromhex("04145a3356775a334a685a4758300a"),
bytes.fromhex("04147134673d0a"),
)
decoder = decode()
next(decoder)
for packet in packets:
try:
decoder.send(packet)
except StopIteration as e:
frame = e.value
break
print(frame)
print(ImageUploadWriteRequest.loads(frame))
bytearray(b'\n\x00\x00t\x00\x01\x9a\x01\xa6clen\rcoff\x18*cshaLtrust me broddataX@\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?eimage\x00gupgrade\xf4') # noqa: E501
header=Header(op=<OP.WRITE: 2>, version=<Version.V2: 1>, flags=<Flag: 0>,
length=116, group_id=1, sequence=154, command_id=1) version=<Version.V2: 1>
sequence=154 smp_data=b'\n\x00\x00t\x00\x01\x9a\x01\xa6clen\rcoff\x18*cshaL
trust me broddataX@\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e
\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&
\'()*+,-./0123456789:;<=>?eimage\x00gupgrade\xf4' off=42 data=b'\x00\x01\x02
\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16
\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?'
image=0 len=13 sha=b'trust me bro' upgrade=False
Source code in smp/packet.py
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 |
|