2021-08-28 21:48:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import time
|
|
|
|
import websocket
|
|
|
|
import argparse
|
|
|
|
import zlib
|
2021-09-12 19:55:17 +00:00
|
|
|
from websocket import WebSocketTimeoutException
|
2021-08-28 21:48:01 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Update fiatlux firmware via websocket.')
|
|
|
|
parser.add_argument("binfile")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-09-12 19:55:17 +00:00
|
|
|
bs = 1024
|
|
|
|
|
|
|
|
|
|
|
|
def parse_reply(bytes):
|
|
|
|
cmd = bytes[0:1].decode("utf-8")
|
|
|
|
ret = int.from_bytes(bytes[1:2], "big")
|
|
|
|
val = int.from_bytes(bytes[2:4], "big")
|
|
|
|
return {'cmd': cmd, 'ret': ret, 'val': val}
|
|
|
|
|
|
|
|
|
2021-08-28 21:48:01 +00:00
|
|
|
with open(args.binfile, "rb") as f:
|
|
|
|
try:
|
|
|
|
ws = websocket.WebSocket()
|
2021-09-12 19:55:17 +00:00
|
|
|
print("send {}".format(args.binfile))
|
2021-08-28 21:48:01 +00:00
|
|
|
ws.connect("ws://172.16.0.1")
|
|
|
|
i = 0
|
2021-09-12 19:55:17 +00:00
|
|
|
bytes = f.read()
|
|
|
|
rolling = zlib.crc32(bytes)
|
|
|
|
total = len(bytes)
|
2021-08-28 21:48:01 +00:00
|
|
|
while True:
|
2021-09-12 19:55:17 +00:00
|
|
|
chunk = bytes[bs * i:bs * i + bs]
|
2021-08-28 21:48:01 +00:00
|
|
|
msg = b'F\x00\x00\x00'
|
|
|
|
msg += i.to_bytes(2, 'big')
|
2021-09-12 19:55:17 +00:00
|
|
|
msg += len(chunk).to_bytes(2, 'big')
|
|
|
|
msg += (zlib.crc32(chunk) & 0xffffffff).to_bytes(4, 'big')
|
|
|
|
msg += chunk
|
2021-08-28 21:48:01 +00:00
|
|
|
ws.send(msg)
|
2021-09-12 19:55:17 +00:00
|
|
|
print("\r{:6.2f}%".format(100 * i * bs / total), end='')
|
|
|
|
reply = parse_reply(ws.recv())
|
|
|
|
if reply['cmd'] == 'F' and reply['ret'] == 0:
|
|
|
|
i += 1
|
|
|
|
elif reply['cmd'] == 'F' and reply['ret'] == 1:
|
|
|
|
print("Error: SEQUENCE_OUT_OF_ORDER")
|
|
|
|
i = reply['val']
|
|
|
|
elif reply['cmd'] == 'F' and reply['ret'] == 2:
|
|
|
|
print("Error: CHECKSUM_MISMATCH")
|
|
|
|
i = reply['val']
|
|
|
|
else:
|
|
|
|
print(reply)
|
|
|
|
|
2021-08-28 21:48:01 +00:00
|
|
|
time.sleep(0.05)
|
2021-09-12 19:55:17 +00:00
|
|
|
if len(chunk) != bs:
|
2021-08-28 21:48:01 +00:00
|
|
|
break
|
2021-09-12 19:55:17 +00:00
|
|
|
|
|
|
|
print("\rdone ")
|
2021-08-28 21:48:01 +00:00
|
|
|
msg = b'C\x00\x00\x00'
|
|
|
|
msg += total.to_bytes(4, 'big')
|
2021-09-12 19:55:17 +00:00
|
|
|
msg += (rolling).to_bytes(4, 'big')
|
|
|
|
ws.settimeout(5)
|
2021-08-28 21:48:01 +00:00
|
|
|
ws.send(msg)
|
2021-09-12 19:55:17 +00:00
|
|
|
reply = parse_reply(ws.recv())
|
|
|
|
print(reply)
|
2021-08-28 21:48:01 +00:00
|
|
|
ws.close()
|
2021-09-12 19:55:17 +00:00
|
|
|
except WebSocketTimeoutException:
|
|
|
|
pass
|
2021-08-28 21:48:01 +00:00
|
|
|
except ConnectionResetError:
|
|
|
|
pass
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|