Skip to content

Commit 4d8430d

Browse files
committed
fix: throw propper exceptions and cleanup code
1 parent bf580c3 commit 4d8430d

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

meshtastic/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ def onConnected(interface):
466466

467467
print(f"Triggering OTA update on {interface.hostname}...")
468468
interface.getNode(args.dest, False, **getNode_kwargs).startOTA(
469-
mode=admin_pb2.OTA_WIFI,
470-
hash=ota.hash_bytes()
469+
ota_mode=admin_pb2.OTAMode.OTA_WIFI,
470+
ota_file_hash=ota.hash_bytes()
471471
)
472472

473473
print("Waiting for device to reboot into OTA mode...")
@@ -487,7 +487,7 @@ def onConnected(interface):
487487
time.sleep(2)
488488

489489
print("\nOTA update completed successfully!")
490-
490+
491491
if args.enter_dfu:
492492
closeNow = True
493493
waitForAckNak = True
@@ -1946,7 +1946,8 @@ def addRemoteAdminArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPars
19461946

19471947
group.add_argument(
19481948
"--ota-update",
1949-
help="Perform an OTA update on the destination node (ESP32, firmware version >=2.7.18, WiFi/TCP only for now). Specify the path to the firmware file.",
1949+
help="Perform an OTA update on the local node (ESP32, firmware version >=2.7.18, WiFi/TCP only for now). "
1950+
"Specify the path to the firmware file.",
19501951
metavar="FIRMWARE_FILE",
19511952
action="store",
19521953
)

meshtastic/node.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,17 +669,17 @@ def rebootOTA(self, secs: int = 10):
669669

670670
def startOTA(
671671
self,
672-
mode: admin_pb2.OTAMode.ValueType,
673-
hash: bytes,
672+
ota_mode: admin_pb2.OTAMode.ValueType,
673+
ota_file_hash: bytes,
674674
):
675675
"""Tell the node to start OTA mode (firmware >= 2.7.18)."""
676676
if self != self.iface.localNode:
677-
raise Exception("startOTA only possible in local node")
677+
raise ValueError("startOTA only possible in local node")
678678

679679
self.ensureSessionKey()
680680
p = admin_pb2.AdminMessage()
681-
p.ota_request.reboot_ota_mode=mode
682-
p.ota_request.ota_hash=hash
681+
p.ota_request.reboot_ota_mode = ota_mode
682+
p.ota_request.ota_hash = ota_file_hash
683683

684684
return self._sendAdmin(p)
685685

meshtastic/ota.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import os
1+
"""Meshtastic ESP32 Unified OTA
2+
"""
3+
import os
24
import hashlib
35
import socket
46
import logging
@@ -11,13 +13,18 @@
1113
def _file_sha256(filename: str):
1214
"""Calculate SHA256 hash of a file."""
1315
sha256_hash = hashlib.sha256()
14-
16+
1517
with open(filename, "rb") as f:
1618
for byte_block in iter(lambda: f.read(4096), b""):
1719
sha256_hash.update(byte_block)
1820

1921
return sha256_hash
2022

23+
24+
class OTAError(Exception):
25+
"""Exception for OTA errors."""
26+
27+
2128
class ESP32WiFiOTA:
2229
"""ESP32 WiFi Unified OTA updates."""
2330

@@ -28,21 +35,21 @@ def __init__(self, filename: str, hostname: str, port: int = 3232):
2835
self._socket: Optional[socket.socket] = None
2936

3037
if not os.path.exists(self._filename):
31-
raise Exception(f"File {self._filename} does not exist")
38+
raise FileNotFoundError(f"File {self._filename} does not exist")
3239

3340
self._file_hash = _file_sha256(self._filename)
3441

3542
def _read_line(self) -> str:
3643
"""Read a line from the socket."""
3744
if not self._socket:
38-
raise Exception("Socket not connected")
45+
raise ConnectionError("Socket not connected")
3946

4047
line = b""
4148
while not line.endswith(b"\n"):
4249
char = self._socket.recv(1)
43-
50+
4451
if not char:
45-
raise Exception("Connection closed while waiting for response")
52+
raise ConnectionError("Connection closed while waiting for response")
4653

4754
line += char
4855

@@ -78,10 +85,11 @@ def update(self, progress_callback: Optional[Callable[[int, int], None]] = None)
7885
response = self._read_line()
7986
if response == "OK":
8087
break
81-
elif response == "ERASING":
88+
89+
if response == "ERASING":
8290
logger.info("Device is erasing flash...")
8391
elif response.startswith("ERR "):
84-
raise Exception(f"Device reported error: {response}")
92+
raise OTAError(f"Device reported error: {response}")
8593
else:
8694
logger.warning(f"Unexpected response: {response}")
8795

@@ -105,18 +113,16 @@ def update(self, progress_callback: Optional[Callable[[int, int], None]] = None)
105113
logger.info("Firmware sent, waiting for verification...")
106114
while True:
107115
response = self._read_line()
108-
109116
if response == "OK":
110117
logger.info("OTA update completed successfully!")
111118
break
112-
elif response == "ACK":
113-
continue
114-
elif response.startswith("ERR "):
115-
raise Exception(f"OTA update failed: {response}")
116-
else:
119+
120+
if response.startswith("ERR "):
121+
raise OTAError(f"OTA update failed: {response}")
122+
elif response != "ACK":
117123
logger.warning(f"Unexpected final response: {response}")
118124

119125
finally:
120126
if self._socket:
121127
self._socket.close()
122-
self._socket = None
128+
self._socket = None

0 commit comments

Comments
 (0)