feat(radxa,android): autonomous Python usb0 DHCP server and Annex-B raw NAL framer for RubyFPV

This commit is contained in:
bleack committed 2026-11-27 22:44:00 +01:00
1 parent 5d4a9a40a4
commit 5b2b40d079
4 files changed
+283 -70

No files matched your search

+162
View File
@@ -0,0 +1,162 @@
#!/usr/bin/env python3
"""Serveur DHCPv4 minimaliste sans dépendance externe pour l'interface usb0 de la Radxa Zero 3W.
Distribue automatiquement 192.168.1.10/24 (sans passerelle par défaut ni DNS) afin que
la Galaxy Tab S8 Ultra (Android One UI) et le Steam Frame (SteamOS/Linux) montent la liaison
USB-C Ethernet en plug-and-play dès le branchement, sans détourner le Wi-Fi/5G.
"""
import logging
import os
import socket
import struct
import sys
import time
IFACE = os.environ.get("DHCP_IFACE", "usb0")
SERVER_IP = os.environ.get("DHCP_SERVER_IP", "192.168.1.1")
OFFER_IP = os.environ.get("DHCP_OFFER_IP", "192.168.1.10")
SUBNET_MASK = "255.255.255.0"
BROADCAST_IP = "192.168.1.255"
LEASE_SECONDS = 43200 # 12 h
DHCP_MAGIC_COOKIE = b"\x63\x82\x53\x63"
DHCPDISCOVER = 1
DHCPOFFER = 2
DHCPREQUEST = 3
DHCPACK = 5
DHCPINFORM = 8
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [usb0-dhcp] %(levelname)s: %(message)s",
)
def parse_options(data: bytes) -> dict:
opts = {}
i = 0
n = len(data)
while i < n:
code = data[i]
i += 1
if code == 0: # Pad
continue
if code == 255: # End
break
if i >= n:
break
length = data[i]
i += 1
if i + length > n:
break
opts[code] = data[i : i + length]
i += length
return opts
def build_dhcp_reply(
req: bytes,
msg_type: int,
offer_ip: str,
server_ip: str,
) -> bytes:
xid = req[4:8]
flags = req[10:12]
ciaddr = req[12:16]
chaddr = req[28:44]
yiaddr = b"\x00\x00\x00\x00" if msg_type == DHCPACK and req[236:240] == DHCP_MAGIC_COOKIE and parse_options(req[240:]).get(53) == bytes([DHCPINFORM]) else socket.inet_aton(offer_ip)
siaddr = socket.inet_aton(server_ip)
header = bytearray(236)
header[0] = 2 # BOOTREPLY
header[1] = 1 # Ethernet
header[2] = 6 # MAC len
header[3] = 0 # Hops
header[4:8] = xid
header[8:10] = b"\x00\x00" # secs
header[10:12] = flags
header[12:16] = ciaddr if msg_type == DHCPACK else b"\x00\x00\x00\x00"
header[16:20] = yiaddr
header[20:24] = siaddr
header[24:28] = b"\x00\x00\x00\x00" # giaddr
header[28:44] = chaddr
# Options DHCP : volontairement SANS option 3 (Router) ni option 6 (DNS)
# pour ne pas casser la route Internet Wi-Fi/5G de la tablette Android ou du Steam Frame.
opts = bytearray()
opts += bytes([53, 1, msg_type]) # DHCP Message Type
opts += bytes([54, 4]) + socket.inet_aton(server_ip) # Server Identifier
opts += bytes([51, 4]) + struct.pack("!I", LEASE_SECONDS) # Lease Time
opts += bytes([58, 4]) + struct.pack("!I", LEASE_SECONDS // 2) # Renewal (T1)
opts += bytes([59, 4]) + struct.pack("!I", (LEASE_SECONDS * 7) // 8) # Rebinding (T2)
opts += bytes([1, 4]) + socket.inet_aton(SUBNET_MASK) # Subnet Mask
opts += bytes([28, 4]) + socket.inet_aton(BROADCAST_IP) # Broadcast Address
opts += bytes([255]) # End
return bytes(header) + DHCP_MAGIC_COOKIE + bytes(opts)
def open_dhcp_socket(iface: str) -> socket.socket:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# SO_BINDTODEVICE = 25 sur Linux (évite de répondre sur wlan0/eth0)
sock.setsockopt(socket.SOL_SOCKET, 25, iface.encode("utf-8") + b"\x00")
sock.bind(("", 67))
sock.settimeout(2.0)
return sock
def main() -> int:
logging.info(
"Démarrage DHCP usb0 : iface=%s server=%s offer=%s (sans passerelle ni DNS)",
IFACE,
SERVER_IP,
OFFER_IP,
)
while True:
sock = None
try:
sock = open_dhcp_socket(IFACE)
logging.info("En écoute UDP :67 sur %s", IFACE)
while True:
try:
data, _addr = sock.recvfrom(1500)
except socket.timeout:
continue
if len(data) < 240 or data[0] != 1 or data[236:240] != DHCP_MAGIC_COOKIE:
continue
opts = parse_options(data[240:])
msg_type_raw = opts.get(53)
if not msg_type_raw:
continue
msg_type = msg_type_raw[0]
mac = ":".join(f"{b:02x}" for b in data[28:34])
if msg_type == DHCPDISCOVER:
reply = build_dhcp_reply(data, DHCPOFFER, OFFER_IP, SERVER_IP)
sock.sendto(reply, ("255.255.255.255", 68))
logging.info("DISCOVER de %s -> OFFER %s", mac, OFFER_IP)
elif msg_type == DHCPREQUEST:
reply = build_dhcp_reply(data, DHCPACK, OFFER_IP, SERVER_IP)
sock.sendto(reply, ("255.255.255.255", 68))
logging.info("REQUEST de %s -> ACK %s", mac, OFFER_IP)
elif msg_type == DHCPINFORM:
reply = build_dhcp_reply(data, DHCPACK, OFFER_IP, SERVER_IP)
sock.sendto(reply, ("255.255.255.255", 68))
logging.info("INFORM de %s -> ACK", mac)
except OSError as exc:
logging.warning("Interface %s indisponible (%s), nouvel essai dans 2s...", IFACE, exc)
time.sleep(2.0)
finally:
if sock is not None:
try:
sock.close()
except OSError:
pass
if __name__ == "__main__":
sys.exit(main())
+27 -40
View File
@@ -1,52 +1,44 @@
#!/usr/bin/env bash
# Configure un serveur DHCP léger (dnsmasq) sur l'interface usb0 (192.168.1.1)
# de la Radxa Zero 3W pour le plug-and-play sur Android (Galaxy Tab S8 Ultra)
# et Linux/SteamOS sans conflit avec les profils IP statiques existants.
# Déploie et active un serveur DHCPv4 autonome (Python 3 stdlib, zéro paquet apt)
# sur l'interface usb0 (192.168.1.1 -> bail 192.168.1.10/24 sans passerelle ni DNS)
# de la Radxa Zero 3W pour le plug-and-play sur Galaxy Tab S8 Ultra (Android)
# et Steam Frame (SteamOS / Linux).
#
# Usage : ./scripts/setup_radxa_dhcp_usb0.sh [root@192.168.1.1]
# Usage : ./scripts/setup_radxa_dhcp_usb0.sh [radxa@192.168.1.1]
set -euo pipefail
RADXA_HOST="${1:-root@192.168.1.1}"
RADXA_HOST="${1:-radxa@192.168.1.1}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "=== Configuration du serveur DHCP usb0 sur ${RADXA_HOST} ==="
echo "=== Déploiement du serveur DHCP autonome usb0 sur ${RADXA_HOST} ==="
scp "${SCRIPT_DIR}/radxa_usb0_dhcp.py" "${RADXA_HOST}:/tmp/radxa_usb0_dhcp.py"
ssh "${RADXA_HOST}" bash -s << 'EOF'
set -euo pipefail
if ! command -v dnsmasq >/dev/null 2>&1; then
echo "[*] dnsmasq absent — tentative d'installation via apt-get..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq && apt-get install -y -qq dnsmasq-base || apt-get install -y -qq dnsmasq
SUDO=""
if [ "$(id -u)" -ne 0 ]; then
SUDO="sudo"
fi
mkdir -p /etc/dnsmasq.d
${SUDO} mv /tmp/radxa_usb0_dhcp.py /usr/local/bin/radxa_usb0_dhcp.py
${SUDO} chmod +x /usr/local/bin/radxa_usb0_dhcp.py
# Configuration dédiée usb0 :
# - port=0 : désactive le serveur DNS (évite tout conflit avec systemd-resolved)
# - dhcp-option=3 (vide) : n'annonce AUCUNE passerelle par défaut (préserve le Wi-Fi/5G sur Android et Steam Frame)
# - dhcp-option=6 (vide) : n'annonce AUCUN DNS
# - plage 192.168.1.10..192.168.1.20 (premier bail = 192.168.1.10, cohérent avec les scripts existants)
cat << 'CONF' > /etc/dnsmasq.d/steamframe-usb0.conf
port=0
interface=usb0
bind-dynamic
dhcp-range=192.168.1.10,192.168.1.20,255.255.255.0,12h
dhcp-option=3
dhcp-option=6
dhcp-authoritative
dhcp-leasefile=/tmp/dnsmasq-usb0.leases
CONF
cat << 'UNIT' > /etc/systemd/system/steamframe-usb0-dhcp.service
${SUDO} tee /etc/systemd/system/steamframe-usb0-dhcp.service >/dev/null << 'UNIT'
[Unit]
Description=SteamFrame FPV DHCP Server on usb0 (Android & SteamOS Plug-and-Play)
Description=SteamFrame FPV Autonomous DHCPv4 Server on usb0 (Android & SteamOS Plug-and-Play)
After=usb0-setup.service network.target
Wants=usb0-setup.service
StartLimitIntervalSec=0
[Service]
Type=simple
ExecStart=/usr/sbin/dnsmasq --keep-in-foreground --no-daemon --conf-file=/etc/dnsmasq.d/steamframe-usb0.conf
Environment=DHCP_IFACE=usb0
Environment=DHCP_SERVER_IP=192.168.1.1
Environment=DHCP_OFFER_IP=192.168.1.10
ExecStart=/usr/bin/python3 /usr/local/bin/radxa_usb0_dhcp.py
Restart=always
RestartSec=2
@@ -54,15 +46,10 @@ RestartSec=2
WantedBy=multi-user.target
UNIT
# Si le service dnsmasq global existe et risque d'occuper le port 67, on évite les conflits
if systemctl is-enabled dnsmasq.service >/dev/null 2>&1; then
systemctl disable --now dnsmasq.service || true
fi
systemctl daemon-reload
systemctl enable steamframe-usb0-dhcp.service
systemctl restart steamframe-usb0-dhcp.service
systemctl status --no-pager steamframe-usb0-dhcp.service || true
${SUDO} systemctl daemon-reload
${SUDO} systemctl enable steamframe-usb0-dhcp.service
${SUDO} systemctl restart steamframe-usb0-dhcp.service
${SUDO} systemctl status --no-pager steamframe-usb0-dhcp.service || true
EOF
echo "[+] DHCP usb0 actif sur ${RADXA_HOST} (plage 192.168.1.10-20, sans passerelle ni DNS) !"
echo "[+] Serveur DHCP autonome usb0 actif sur ${RADXA_HOST} (192.168.1.10/24, sans passerelle ni DNS) !"