From 09452ef7ab346cf92587313a995b93be28680de9 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 28 Aug 2021 23:48:01 +0200 Subject: [PATCH] add minimal OTA Update CLI --- README.md | 1 + firmware/otaflash.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100755 firmware/otaflash.py diff --git a/README.md b/README.md index ebf56ee..6ff897e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ git submodule update --init --recursive - ncurses-dev libexpat-dev - python3 python3-serial python-dev + - pip install websocket-client (for otaflash.py, optional) ### Build Steps diff --git a/firmware/otaflash.py b/firmware/otaflash.py new file mode 100755 index 0000000..aeefe3f --- /dev/null +++ b/firmware/otaflash.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import time +import websocket +import argparse +import zlib + +parser = argparse.ArgumentParser(description='Update fiatlux firmware via websocket.') +parser.add_argument("binfile") + +args = parser.parse_args() + +with open(args.binfile, "rb") as f: + try: + ws = websocket.WebSocket() + ws.connect("ws://172.16.0.1") + i = 0 + rolling = 0 + total = 0 + while True: + bytes = f.read(512) + rolling = zlib.crc32(bytes, rolling) + total += len(bytes) + msg = b'F\x00\x00\x00' + msg += i.to_bytes(2, 'big') + msg += len(bytes).to_bytes(2, 'big') + msg += (zlib.crc32(bytes) & 0xffffffff).to_bytes(4, 'big') + msg += bytes + ws.send(msg) + reply = ws.recv() + time.sleep(0.05) + i += 1 + if len(bytes) != 512: + break + msg = b'C\x00\x00\x00' + msg += total.to_bytes(4, 'big') + msg += rolling.to_bytes(4, 'big') + ws.send(msg) + print(ws.recv()) + ws.close() + except ConnectionResetError: + pass + except KeyboardInterrupt: + pass