Skip to content

Commit 661d8a5

Browse files
committed
tools/flash: Pass hub kind instead of metadata.
Each tool only uses the metadata to get the hub kind, so pass it directly. In the BLE tool it was duplicated before, so we need just one.
1 parent 32ced87 commit 661d8a5

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

tools/flash/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def main():
6060
hub_kind = HubKind(metadata["device-id"])
6161

6262
if hub_kind in (HubKind.TECHNIC_SMALL, HubKind.TECHNIC_LARGE):
63-
flash_dfu(firmware, metadata)
63+
flash_dfu(firmware, hub_kind)
6464
elif hub_kind in [HubKind.BOOST, HubKind.CITY, HubKind.TECHNIC]:
65-
asyncio.run(flash_ble(hub_kind, firmware, metadata))
65+
asyncio.run(flash_ble(firmware, hub_kind))
6666
elif hub_kind == HubKind.NXT:
6767
flash_nxt(firmware)
6868
elif hub_kind == HubKind.EV3:

tools/flash/dfu.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from tqdm.contrib.logging import logging_redirect_tqdm
2424

2525
from lwp3.bytecodes import HubKind
26-
from firmware import AnyFirmwareMetadata
2726
from constants import (
2827
LEGO_USB_VID,
2928
MINDSTORMS_INVENTOR_DFU_USB_PID,
@@ -242,7 +241,7 @@ def write_firmware(
242241
print("Done.")
243242

244243

245-
def flash_dfu(firmware_bin: bytes, metadata: AnyFirmwareMetadata) -> None:
244+
def flash_dfu(firmware_bin: bytes, hub_kind: HubKind) -> None:
246245
"""Flashes a firmware image to a connected hub over USB DFU."""
247246
try:
248247
devices = get_dfu_devices(idVendor=LEGO_USB_VID)
@@ -269,7 +268,7 @@ def flash_dfu(firmware_bin: bytes, metadata: AnyFirmwareMetadata) -> None:
269268
print(f"Unknown USB product ID: {product_id:04X}", file=sys.stderr)
270269
exit(1)
271270

272-
if ALL_PIDS[product_id] != metadata["device-id"]:
271+
if ALL_PIDS[product_id] != hub_kind:
273272
print("Incorrect firmware type for this hub", file=sys.stderr)
274273
exit(1)
275274

tools/flash/lwp3/flash.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ async def bootloader_request(self, request, payload=None, timeout=None):
179179
reply = await self.wait_for_reply(timeout)
180180
return request.parse_reply(reply)
181181

182-
async def flash(self, firmware, metadata):
182+
async def flash(self, firmware, hub_kind):
183183
# Firmware information
184184
firmware_io = io.BytesIO(firmware)
185185
firmware_size = len(firmware)
@@ -193,11 +193,11 @@ async def flash(self, firmware, metadata):
193193
hub_name, max_data_size = HUB_INFO[info.type_id]
194194

195195
# Verify hub ID against ID in firmware package
196-
if info.type_id != metadata["device-id"]:
196+
if info.type_id != hub_kind:
197197
await self.disconnect()
198198
raise RuntimeError(
199199
"This firmware {0}, but we are connected to {1}.".format(
200-
HUB_INFO[metadata["device-id"]][0], hub_name
200+
HUB_INFO[hub_kind][0], hub_name
201201
)
202202
)
203203

@@ -308,17 +308,16 @@ def match_hub(hub_kind: HubKind, adv: AdvertisementData) -> bool:
308308
return False
309309

310310

311-
async def flash_ble(hub_kind: HubKind, firmware: bytes, metadata: dict):
311+
async def flash_ble(firmware: bytes, hub_kind: HubKind):
312312
"""
313313
Flashes firmware to the hub using Bluetooth Low Energy.
314314
315315
The hub has to be advertising and can be running official LEGO firmware,
316316
Pybricks firmware or be in bootloader mode.
317317
318318
Args:
319-
hub_kind: The hub type ID. Only hubs matching this ID will be discovered.
320319
firmware: The raw firmware binary blob.
321-
metadata: The firmware metadata from the firmware.zip file.
320+
hub_kind: The hub type ID. Only hubs matching this ID will be discovered.
322321
"""
323322

324323
print(f"Searching for {hub_kind.name} hub...")
@@ -352,4 +351,4 @@ def map_and_match(device: BLEDevice, adv: AdvertisementData):
352351
updater = BootloaderConnection()
353352
await updater.connect(device)
354353
print("Erasing flash and starting update")
355-
await updater.flash(firmware, metadata)
354+
await updater.flash(firmware, hub_kind)

0 commit comments

Comments
 (0)