feat(espnow): app-file OTA over the reliable link
New espnow_ota module, loaded on the first OTA packet (types 0x07-0x0C, status reply 0x26). Files are written by chunk index to <path>.new, checked against their sha256, and moved into place together on commit, followed by a reset. Unchanged files are skipped, and a repeated commit after the reset still gets OK. - receive ESP-NOW frames from esp.irq() so frames are not lost while presets.tick() runs; polling remains as a fallback - raise rxbuf to 2048 and the link inbox to 24 frames - file_hashes: MicroPython's sha256 has no hexdigest() - bench stats line includes esp.stats() Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
940d1d0a77
commit
495da4e366
8 files changed
+577
-15
No files matched your search
@@ -0,0 +1,231 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Ota: staging, chunk bitmap, hash checks, commit and sessions. Runs in a temp directory."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
import tempfile
|
||||
|
||||
import espnow_ota as o
|
||||
import file_hashes
|
||||
|
||||
M = b"\x01\x02\x03\x04\x05\x06"
|
||||
|
||||
|
||||
def begin(sess, path, body):
|
||||
p = path.encode()
|
||||
return struct.pack("<BBHI", 0x4C, o.OP_BEGIN, sess, len(body)) + hashlib.sha256(body).digest() + bytes([len(p)]) + p
|
||||
|
||||
|
||||
def chunk(sess, body, idx):
|
||||
return struct.pack("<BBHH", 0x4C, o.OP_DATA, sess, idx) + body[idx * o.CHUNK : (idx + 1) * o.CHUNK]
|
||||
|
||||
|
||||
def cmd(op, sess):
|
||||
return struct.pack("<BBH", 0x4C, op, sess)
|
||||
|
||||
|
||||
def nchunks(body):
|
||||
return (len(body) + o.CHUNK - 1) // o.CHUNK
|
||||
|
||||
|
||||
class Rig:
|
||||
def __init__(self):
|
||||
self.sent, self.resets = [], 0
|
||||
self.ota = o.Ota(lambda mac, f: self.sent.append(f), self._reset)
|
||||
|
||||
def _reset(self):
|
||||
self.resets += 1
|
||||
|
||||
def __call__(self, pkt):
|
||||
self.ota.handle(M, pkt)
|
||||
if self.sent:
|
||||
f = self.sent.pop()
|
||||
_, t, sess, op, code = struct.unpack_from("<BBHBB", f)
|
||||
assert t == o.MSG_STATUS
|
||||
return op, code, f[6:]
|
||||
return None
|
||||
|
||||
def send_file(self, sess, path, body, skip=()):
|
||||
assert self(begin(sess, path, body))[1] == o.READY
|
||||
for i in range(nchunks(body)):
|
||||
if i not in skip:
|
||||
assert self(chunk(sess, body, i)) is None # data is acked by the link, not here
|
||||
return self(cmd(o.OP_END, sess))[1]
|
||||
|
||||
|
||||
def in_tmp(fn):
|
||||
def run():
|
||||
old = os.getcwd()
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
os.chdir(d)
|
||||
try:
|
||||
fn()
|
||||
finally:
|
||||
os.chdir(old)
|
||||
|
||||
run.__name__ = fn.__name__
|
||||
return run
|
||||
|
||||
|
||||
BODY = bytes(range(256)) * 10 # 2560 bytes, 3 chunks
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_write_commit_and_manifest():
|
||||
r = Rig()
|
||||
assert r.send_file(5, "patterns/wave.py", BODY) == o.OK
|
||||
assert not os.path.exists("patterns/wave.py") # staged only
|
||||
op, code, arg = r(cmd(o.OP_COMMIT, 5))
|
||||
assert (op, code, struct.unpack("<I", arg)[0]) == (o.OP_COMMIT, o.OK, 1)
|
||||
assert open("patterns/wave.py", "rb").read() == BODY and not os.path.exists("patterns/wave.py.new")
|
||||
assert file_hashes.load()["patterns/wave.py"] == hashlib.sha256(BODY).hexdigest()
|
||||
assert r.resets == 1
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_same_file_is_skipped():
|
||||
with open("main.py", "wb") as f:
|
||||
f.write(BODY)
|
||||
r = Rig()
|
||||
assert r(begin(1, "main.py", BODY))[1] == o.SAME
|
||||
op, code, _ = r(cmd(o.OP_COMMIT, 1))
|
||||
assert code == o.OK and r.resets == 0 # nothing moved, no reset
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_out_of_order_and_duplicate_chunks():
|
||||
r = Rig()
|
||||
assert r(begin(2, "a.py", BODY))[1] == o.READY
|
||||
for i in (2, 0, 2, 1, 0):
|
||||
r(chunk(2, BODY, i))
|
||||
assert r(cmd(o.OP_END, 2))[1] == o.OK
|
||||
r(cmd(o.OP_COMMIT, 2))
|
||||
assert open("a.py", "rb").read() == BODY
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_incomplete_then_missing_then_repair():
|
||||
r = Rig()
|
||||
assert r.send_file(3, "a.py", BODY, skip=(0, 2)) == o.INCOMPLETE
|
||||
op, code, arg = r(cmd(o.OP_MISSING, 3))
|
||||
cnt = struct.unpack_from("<H", arg)[0]
|
||||
assert (op, cnt, arg[2]) == (o.OP_MISSING, 3, 0b101)
|
||||
r(chunk(3, BODY, 0))
|
||||
r(chunk(3, BODY, 2))
|
||||
assert r(cmd(o.OP_MISSING, 3))[2][2] == 0
|
||||
assert r(cmd(o.OP_END, 3))[1] == o.OK
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_bad_hash_is_not_staged():
|
||||
r = Rig()
|
||||
bad = bytearray(begin(4, "a.py", BODY))
|
||||
bad[8] ^= 0xFF
|
||||
assert r(bytes(bad))[1] == o.READY
|
||||
for i in range(nchunks(BODY)):
|
||||
r(chunk(4, BODY, i))
|
||||
assert r(cmd(o.OP_END, 4))[1] == o.BADHASH
|
||||
r(cmd(o.OP_COMMIT, 4))
|
||||
assert not os.path.exists("a.py") and not os.path.exists("a.py.new") and r.resets == 0
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_bad_paths():
|
||||
r = Rig()
|
||||
for p in ("../x.py", "/main.py", "a/b.py", "lib//x.py", "file_hashes.json", "patterns\\x.py", "lib/../main.py"):
|
||||
assert r(begin(6, p, b"x"))[1] == o.BADPATH, p
|
||||
for p in ("main.py", "patterns/x.py", "lib/microdot/websocket.py"):
|
||||
assert r(begin(6, p, b"x"))[1] == o.READY, p
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_new_session_discards_staged():
|
||||
r = Rig()
|
||||
assert r.send_file(7, "a.py", BODY) == o.OK
|
||||
assert os.path.exists("a.py.new")
|
||||
assert r(begin(8, "b.py", b"bb"))[1] == o.READY
|
||||
assert not os.path.exists("a.py.new")
|
||||
assert r(cmd(o.OP_COMMIT, 7))[1] == o.NOSESS # old session is gone
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_repeated_begin_and_end_are_idempotent():
|
||||
r = Rig()
|
||||
assert r(begin(9, "a.py", BODY))[1] == o.READY
|
||||
r(chunk(9, BODY, 0))
|
||||
assert r(begin(9, "a.py", BODY))[1] == o.READY # reply was lost, controller asks again
|
||||
r(chunk(9, BODY, 1))
|
||||
r(chunk(9, BODY, 2))
|
||||
assert r(cmd(o.OP_END, 9))[1] == o.OK
|
||||
assert r(cmd(o.OP_END, 9))[1] == o.OK
|
||||
assert r(begin(9, "a.py", BODY))[1] == o.READY # already staged with that hash
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_abort_removes_staged():
|
||||
r = Rig()
|
||||
r.send_file(10, "a.py", BODY)
|
||||
assert r(cmd(o.OP_ABORT, 10))[1] == o.OK
|
||||
assert not os.path.exists("a.py.new")
|
||||
assert r(cmd(o.OP_COMMIT, 10))[2] == struct.pack("<I", 0)
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_broadcast_with_loss_recovers():
|
||||
# three drivers share one broadcast; each drops different chunks, then gets repaired by unicast
|
||||
body = os.urandom(o.CHUNK * 7 + 100)
|
||||
rigs = [Rig() for _ in range(3)]
|
||||
for k, r in enumerate(rigs):
|
||||
os.makedirs(str(k))
|
||||
old = os.getcwd()
|
||||
for k, r in enumerate(rigs):
|
||||
os.chdir(str(k))
|
||||
assert r(begin(11, "lib/x.py", body))[1] == o.READY
|
||||
for i in range(nchunks(body)):
|
||||
if (i + k) % 3:
|
||||
r(chunk(11, body, i))
|
||||
assert r(cmd(o.OP_END, 11))[1] == o.INCOMPLETE
|
||||
arg = r(cmd(o.OP_MISSING, 11))[2]
|
||||
cnt = struct.unpack_from("<H", arg)[0]
|
||||
for i in range(cnt):
|
||||
if arg[2 + (i >> 3)] & (1 << (i & 7)):
|
||||
r(chunk(11, body, i))
|
||||
assert r(cmd(o.OP_END, 11))[1] == o.OK
|
||||
r(cmd(o.OP_COMMIT, 11))
|
||||
assert open("lib/x.py", "rb").read() == body
|
||||
os.chdir(old)
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_commit_repeated_after_reset():
|
||||
r = Rig()
|
||||
r.send_file(14, "a.py", BODY)
|
||||
assert r(cmd(o.OP_COMMIT, 14))[1] == o.OK
|
||||
fresh = Rig() # the driver reset before its reply arrived
|
||||
op, code, arg = fresh(cmd(o.OP_COMMIT, 14))
|
||||
assert code == o.OK and fresh.resets == 0
|
||||
assert fresh(cmd(o.OP_COMMIT, 15))[1] == o.NOSESS
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_empty_file():
|
||||
r = Rig()
|
||||
assert r.send_file(13, "lib/pkg/__init__.py", b"") == o.OK
|
||||
r(cmd(o.OP_COMMIT, 13))
|
||||
assert open("lib/pkg/__init__.py", "rb").read() == b""
|
||||
|
||||
|
||||
@in_tmp
|
||||
def test_too_big_rejected():
|
||||
r = Rig()
|
||||
pkt = bytearray(begin(12, "a.py", b"x"))
|
||||
struct.pack_into("<I", pkt, 4, o.MAX_SIZE + 1)
|
||||
assert r(bytes(pkt))[1] == o.TOOBIG
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for name, fn in list(globals().items()):
|
||||
if name.startswith("test_"):
|
||||
fn()
|
||||
print("ok")
|
||||
Reference in new issue
Block a user