The CLI reaches a device over HTTP as well as USB: http_device.py holds the request helpers, driver_ota.py uploads driver firmware, and cli.py and ota.py use them. The README documents the new options. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
"""
|
|
Over-the-air firmware update for the v2 ESP32-S3 controller (Ethernet, plain HTTP).
|
|
|
|
The controller takes the raw app image on POST /ota, writes it to the idle slot and reboots.
|
|
"""
|
|
|
|
import http.client
|
|
import json
|
|
import os
|
|
import time
|
|
from typing import Optional
|
|
|
|
IMAGE_NAME = "led_controller_v2.bin"
|
|
# Searched in order when no image is given (relative to where the tool is run, since the
|
|
# PyInstaller binary has no useful location of its own)
|
|
DEFAULT_IMAGES = [
|
|
os.path.join("v2", "build", IMAGE_NAME),
|
|
os.path.join("build", IMAGE_NAME),
|
|
os.path.join("..", "v2", "build", IMAGE_NAME),
|
|
]
|
|
|
|
|
|
def _default_image() -> Optional[str]:
|
|
return next((p for p in DEFAULT_IMAGES if os.path.isfile(p)), None)
|
|
|
|
|
|
def _info(host: str, port: int) -> Optional[dict]:
|
|
try:
|
|
conn = http.client.HTTPConnection(host, port, timeout=5)
|
|
conn.request("GET", "/ota")
|
|
r = conn.getresponse()
|
|
return json.loads(r.read().decode()) if r.status == 200 else None
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
|
|
def ota_update(host: str, image: Optional[str] = None, port: int = 80) -> None:
|
|
"""Upload image to host and wait for it to come back. Raises RuntimeError on failure."""
|
|
image = image or _default_image()
|
|
if not image:
|
|
raise RuntimeError(f"no image given and {IMAGE_NAME} not found under ./v2/build, ./build or ../v2/build")
|
|
if not os.path.isfile(image):
|
|
raise RuntimeError(f"image not found: {image}")
|
|
size = os.path.getsize(image)
|
|
|
|
before = _info(host, port)
|
|
if before is None:
|
|
raise RuntimeError(f"no OTA endpoint at http://{host}:{port}/ota (is this an LED device?)")
|
|
print(f"Running {before['partition']} (built {before['built']})")
|
|
|
|
print(f"Uploading {image} ({size} bytes)...")
|
|
conn = http.client.HTTPConnection(host, port, timeout=60)
|
|
with open(image, "rb") as f:
|
|
conn.request("POST", "/ota", body=f, headers={
|
|
"Content-Length": str(size),
|
|
"Content-Type": "application/octet-stream",
|
|
})
|
|
r = conn.getresponse()
|
|
body = r.read().decode()
|
|
if r.status != 200:
|
|
try:
|
|
body = json.loads(body).get("error", body)
|
|
except ValueError:
|
|
pass
|
|
raise RuntimeError(f"update rejected ({r.status}): {body}")
|
|
|
|
print("Written, waiting for reboot...")
|
|
time.sleep(3)
|
|
for _ in range(30):
|
|
after = _info(host, port)
|
|
if after:
|
|
print(f"Back up on {after['partition']} (built {after['built']})")
|
|
if after["partition"] == before["partition"]:
|
|
raise RuntimeError("controller came back on the same slot: new image was rejected or rolled back")
|
|
return
|
|
time.sleep(1)
|
|
raise RuntimeError("controller did not come back within 30 s")
|