|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""WS-on-MicroPython diagnostic — sequential, no threads. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + MPY_TEST_PORT=/dev/cu.usbmodem* python tools/diag_mpy_ws.py |
| 6 | +
|
| 7 | +What it does: |
| 8 | +1. Cold-mounts uhttp on ESP32, connects WiFi. |
| 9 | +2. Boots a verbose echo server (try/except + sys.print_exception + |
| 10 | + gc.mem_free) using fire-and-forget exec. |
| 11 | +3. PC opens a WS connection, sends a binary frame, waits for echo — |
| 12 | + short timeout (3 s) so we don't block forever. |
| 13 | +4. Sends Ctrl-C to the device REPL, then DRAINS serial buffer so we |
| 14 | + can see any traceback the server printed. |
| 15 | +""" |
| 16 | +import json |
| 17 | +import os |
| 18 | +import socket |
| 19 | +import sys |
| 20 | +import time |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +PORT = os.environ.get('MPY_TEST_PORT', '/dev/cu.usbmodem101') |
| 24 | +ESP32_SERVER_PORT = 8081 |
| 25 | +wifi_cfg = json.loads( |
| 26 | + (Path.home() / '.config/uhttp/wifi.json').read_text()) |
| 27 | +WIFI_SSID = wifi_cfg['ssid'] |
| 28 | +WIFI_PASSWORD = wifi_cfg.get('password', '') |
| 29 | + |
| 30 | +import mpytool |
| 31 | +from mpytool.mpy_cross import MpyCross |
| 32 | + |
| 33 | + |
| 34 | +def log(msg): |
| 35 | + print(f"[diag] {msg}", flush=True) |
| 36 | + |
| 37 | + |
| 38 | +def drain_serial(conn, label, max_total_s=3.0): |
| 39 | + """Drain whatever the device emitted to the serial bus and print it. |
| 40 | +
|
| 41 | + conn.read(timeout=0.1) is non-blocking-ish (returns None when no |
| 42 | + data). We loop until N consecutive empty reads or max_total_s. |
| 43 | + """ |
| 44 | + deadline = time.time() + max_total_s |
| 45 | + buf = bytearray() |
| 46 | + empty_streak = 0 |
| 47 | + while time.time() < deadline: |
| 48 | + try: |
| 49 | + chunk = conn.read(timeout=0.1) |
| 50 | + except Exception as e: |
| 51 | + log(f"drain {label}: read err {e!r}") |
| 52 | + break |
| 53 | + if chunk: |
| 54 | + buf.extend(chunk) |
| 55 | + empty_streak = 0 |
| 56 | + else: |
| 57 | + empty_streak += 1 |
| 58 | + if empty_streak >= 5: |
| 59 | + break |
| 60 | + if buf: |
| 61 | + text = bytes(buf).decode('utf-8', errors='replace') |
| 62 | + print(f"--- ESP32 stdout ({label}) ---") |
| 63 | + print(text) |
| 64 | + print(f"--- end ESP32 stdout ({label}) ---") |
| 65 | + else: |
| 66 | + log(f"drain {label}: (no output)") |
| 67 | + |
| 68 | + |
| 69 | +# 1. Connect to device, soft reset |
| 70 | +log(f"Connecting to ESP32 on {PORT}…") |
| 71 | +conn = mpytool.ConnSerial(port=PORT, baudrate=115200) |
| 72 | +mpy = mpytool.Mpy(conn) |
| 73 | +mpy.stop() |
| 74 | +try: |
| 75 | + conn.write(b'\x03\x03\x04') # Ctrl-C Ctrl-C Ctrl-D |
| 76 | + time.sleep(2) |
| 77 | + conn.read_all() |
| 78 | +except Exception: |
| 79 | + pass |
| 80 | +mpy.stop() |
| 81 | + |
| 82 | +# 2. Mount uhttp |
| 83 | +server_dir = Path(__file__).parent.parent / 'uhttp' |
| 84 | +mpy_cross = MpyCross() |
| 85 | +mpy_cross.init(mpy.platform()) |
| 86 | +mount = mpy.mount( |
| 87 | + str(server_dir), mount_point='/lib/uhttp', mpy_cross=mpy_cross) |
| 88 | + |
| 89 | +# 3. WiFi |
| 90 | +wifi = f""" |
| 91 | +import network, time |
| 92 | +wlan = network.WLAN(network.STA_IF) |
| 93 | +wlan.active(True) |
| 94 | +if not wlan.isconnected(): |
| 95 | + wlan.connect({WIFI_SSID!r}, {WIFI_PASSWORD!r}) |
| 96 | + for _ in range(30): |
| 97 | + if wlan.isconnected(): break |
| 98 | + time.sleep(0.5) |
| 99 | +print('IP:', wlan.ifconfig()[0] if wlan.isconnected() else 'FAIL') |
| 100 | +""" |
| 101 | +out = mpy.comm.exec(wifi, timeout=20).decode('utf-8') |
| 102 | +ip = None |
| 103 | +for line in out.strip().split('\n'): |
| 104 | + if line.startswith('IP:'): |
| 105 | + ip = line.split(':', 1)[1].strip() |
| 106 | +if not ip or ip == 'FAIL': |
| 107 | + log(f"WiFi failed: {out}") |
| 108 | + sys.exit(1) |
| 109 | +log(f"ESP32 IP: {ip}") |
| 110 | + |
| 111 | +# 4. Verbose server, fire-and-forget |
| 112 | +server_code = f""" |
| 113 | +import sys, gc |
| 114 | +sys.path.insert(0, '/lib') |
| 115 | +from uhttp.server import ( |
| 116 | + HttpServer, EVENT_REQUEST, EVENT_WS_REQUEST, EVENT_WS_MESSAGE, |
| 117 | + EVENT_WS_CLOSE, EVENT_WS_PING) |
| 118 | +gc.collect() |
| 119 | +server = HttpServer(port={ESP32_SERVER_PORT}, event_mode=True) |
| 120 | +print('READY mem=', gc.mem_free()) |
| 121 | +while True: |
| 122 | + ev = -1 |
| 123 | + client = None |
| 124 | + try: |
| 125 | + client = server.wait(timeout=1) |
| 126 | + except Exception as e: |
| 127 | + print('ERR_WAIT:') |
| 128 | + sys.print_exception(e) |
| 129 | + continue |
| 130 | + if not client: |
| 131 | + continue |
| 132 | + try: |
| 133 | + ev = client.event |
| 134 | + if ev == EVENT_WS_REQUEST: |
| 135 | + print('WS_REQ path=', client.path, 'mem=', gc.mem_free()) |
| 136 | + client.accept_websocket() |
| 137 | + print('WS_ACC_OK mem=', gc.mem_free()) |
| 138 | + elif ev == EVENT_REQUEST: |
| 139 | + print('HTTP_REQ', client.path) |
| 140 | + if client.path == '/health': |
| 141 | + client.respond({{'status': 'ok'}}) |
| 142 | + else: |
| 143 | + client.respond({{'path': client.path}}) |
| 144 | + elif ev == EVENT_WS_MESSAGE: |
| 145 | + ml = len(client.ws_message) if client.ws_message else 0 |
| 146 | + print('WS_MSG len=', ml, 'mem=', gc.mem_free()) |
| 147 | + client.ws_send(client.ws_message) |
| 148 | + print('WS_SEND_OK mem=', gc.mem_free()) |
| 149 | + elif ev == EVENT_WS_PING: |
| 150 | + print('WS_PING') |
| 151 | + elif ev == EVENT_WS_CLOSE: |
| 152 | + print('WS_CLOSE') |
| 153 | + else: |
| 154 | + print('EV?', ev) |
| 155 | + except Exception as e: |
| 156 | + print('ERR_EV', ev, ':') |
| 157 | + sys.print_exception(e) |
| 158 | + try: |
| 159 | + client.close() |
| 160 | + except Exception: |
| 161 | + pass |
| 162 | +""" |
| 163 | +mpy.comm.exec(server_code, timeout=0) |
| 164 | +log("Server boot fired, waiting 2s for READY…") |
| 165 | +time.sleep(2) |
| 166 | + |
| 167 | +# 5. Wait for TCP listening |
| 168 | +log("Health-checking server…") |
| 169 | +ready = False |
| 170 | +for _ in range(20): |
| 171 | + try: |
| 172 | + s = socket.socket() |
| 173 | + s.settimeout(2) |
| 174 | + s.connect((ip, ESP32_SERVER_PORT)) |
| 175 | + s.sendall(b'GET /health HTTP/1.0\r\nHost: t\r\n\r\n') |
| 176 | + if b'200' in s.recv(1024): |
| 177 | + s.close() |
| 178 | + ready = True |
| 179 | + break |
| 180 | + s.close() |
| 181 | + except OSError: |
| 182 | + pass |
| 183 | + time.sleep(1) |
| 184 | +if not ready: |
| 185 | + log("Server did not become ready") |
| 186 | + drain_serial(conn, 'no-ready') |
| 187 | + conn.write(b'\x03') |
| 188 | + sys.exit(1) |
| 189 | +log("Server ready.") |
| 190 | + |
| 191 | +# 6. Reproduce test_binary_echo |
| 192 | +WS_OPCODE_BINARY = 0x2 |
| 193 | + |
| 194 | + |
| 195 | +def build_masked_frame(opcode, payload, mask=b'\x37\xfa\x21\x3d'): |
| 196 | + if isinstance(payload, str): |
| 197 | + payload = payload.encode('utf-8') |
| 198 | + frame = bytearray() |
| 199 | + frame.append(0x80 | opcode) |
| 200 | + length = len(payload) |
| 201 | + if length < 126: |
| 202 | + frame.append(0x80 | length) |
| 203 | + elif length < 65536: |
| 204 | + frame.append(0x80 | 126) |
| 205 | + frame.append((length >> 8) & 0xFF) |
| 206 | + frame.append(length & 0xFF) |
| 207 | + else: |
| 208 | + frame.append(0x80 | 127) |
| 209 | + for i in range(8): |
| 210 | + frame.append((length >> (56 - 8 * i)) & 0xFF) |
| 211 | + frame.extend(mask) |
| 212 | + masked = bytearray(payload) |
| 213 | + for i in range(len(masked)): |
| 214 | + masked[i] ^= mask[i % 4] |
| 215 | + frame.extend(masked) |
| 216 | + return bytes(frame) |
| 217 | + |
| 218 | + |
| 219 | +def ws_upgrade(sock, host, path='/ws'): |
| 220 | + key = b'x3JJHMbDL1EzLkh9GBhXDw==' |
| 221 | + sock.sendall( |
| 222 | + f"GET {path} HTTP/1.1\r\n" |
| 223 | + f"Host: {host}\r\n" |
| 224 | + f"Upgrade: websocket\r\n" |
| 225 | + f"Connection: Upgrade\r\n" |
| 226 | + f"Sec-WebSocket-Key: {key.decode()}\r\n" |
| 227 | + f"Sec-WebSocket-Version: 13\r\n\r\n".encode()) |
| 228 | + sock.settimeout(5) |
| 229 | + response = b'' |
| 230 | + while b'\r\n\r\n' not in response: |
| 231 | + chunk = sock.recv(1024) |
| 232 | + if not chunk: |
| 233 | + raise RuntimeError(f"upgrade EOF: {response!r}") |
| 234 | + response += chunk |
| 235 | + return response |
| 236 | + |
| 237 | + |
| 238 | +def recv_frame(sock, timeout=3): |
| 239 | + sock.settimeout(timeout) |
| 240 | + header = b'' |
| 241 | + while len(header) < 2: |
| 242 | + b = sock.recv(2 - len(header)) |
| 243 | + if not b: |
| 244 | + raise RuntimeError("EOF during header") |
| 245 | + header += b |
| 246 | + fin = bool(header[0] & 0x80) |
| 247 | + opcode = header[0] & 0x0F |
| 248 | + length = header[1] & 0x7F |
| 249 | + if length == 126: |
| 250 | + ext = b'' |
| 251 | + while len(ext) < 2: |
| 252 | + ext += sock.recv(2 - len(ext)) |
| 253 | + length = (ext[0] << 8) | ext[1] |
| 254 | + elif length == 127: |
| 255 | + ext = b'' |
| 256 | + while len(ext) < 8: |
| 257 | + ext += sock.recv(8 - len(ext)) |
| 258 | + length = int.from_bytes(ext, 'big') |
| 259 | + payload = b'' |
| 260 | + while len(payload) < length: |
| 261 | + chunk = sock.recv(length - len(payload)) |
| 262 | + if not chunk: |
| 263 | + raise RuntimeError("EOF during payload") |
| 264 | + payload += chunk |
| 265 | + return fin, opcode, payload |
| 266 | + |
| 267 | + |
| 268 | +log("=== test_binary_echo repro ===") |
| 269 | +sock = socket.socket() |
| 270 | +sock.settimeout(10) |
| 271 | +try: |
| 272 | + sock.connect((ip, ESP32_SERVER_PORT)) |
| 273 | + log("PC: TCP connected") |
| 274 | + resp = ws_upgrade(sock, ip, '/ws') |
| 275 | + log(f"PC: upgrade OK ({resp[:30]!r}…)") |
| 276 | + payload = bytes(range(256)) |
| 277 | + log(f"PC: sending binary frame, len={len(payload)}") |
| 278 | + sock.sendall(build_masked_frame(WS_OPCODE_BINARY, payload)) |
| 279 | + log("PC: frame sent, waiting up to 3s for echo…") |
| 280 | + try: |
| 281 | + fin, op, echo = recv_frame(sock, timeout=3) |
| 282 | + log(f"PC: ECHO OK fin={fin} op={op:#x} len={len(echo)} " |
| 283 | + f"match={echo == payload}") |
| 284 | + except Exception as e: |
| 285 | + log(f"PC: NO ECHO — {type(e).__name__}: {e}") |
| 286 | +finally: |
| 287 | + try: |
| 288 | + sock.close() |
| 289 | + except Exception: |
| 290 | + pass |
| 291 | + |
| 292 | +# 7. Stop server, drain serial output |
| 293 | +log("Sending Ctrl-C to ESP32 to stop server loop…") |
| 294 | +conn.write(b'\x03') |
| 295 | +time.sleep(1) |
| 296 | +drain_serial(conn, 'after-test') |
| 297 | +mpy.stop() |
| 298 | +log("Done.") |
0 commit comments