diff --git a/.github/workflows/firmware.yml b/.github/workflows/firmware.yml index 0818489..a2a8e8a 100644 --- a/.github/workflows/firmware.yml +++ b/.github/workflows/firmware.yml @@ -24,6 +24,9 @@ jobs: python3 hardware/netlist/build_netlist.py git diff --exit-code -- firmware/src/generated hardware/netlist + - name: Every function is documented + run: python3 firmware/tools/check_docs.py + - name: Core tests (host) run: make -C firmware/test run diff --git a/firmware/README.md b/firmware/README.md index 640b0af..56de460 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -153,8 +153,14 @@ src/generated/ board_pins.h and board_pins_w.h, written by hardware/netlist/build_netlist.py boards/ SDK board headers for the hood and W boards test/ host tests for the core (fake HAL) and the emulator test +tools/ check_docs.py: every function has a comment (CI runs it) ``` +Every function has a comment saying what it does: public ones on their +prototype in the header, static ones above the definition. +`python3 firmware/tools/check_docs.py` lists any that don't, and CI fails +on them. + The GPIO numbers come from the hardware netlist script. Change the board there, and the pin header follows. diff --git a/firmware/src/core/autobaud.c b/firmware/src/core/autobaud.c index 412adf0..7483cde 100644 --- a/firmware/src/core/autobaud.c +++ b/firmware/src/core/autobaud.c @@ -31,7 +31,9 @@ const uint32_t AB_NBAUDS = sizeof AB_BAUDS / sizeof AB_BAUDS[0]; #define ALIGN_TOL16 5u // edge within 5/16 of a bit boundary #define AB_SCORE_MARGIN 16u // 1/16 bit, in 1/256 bit +// A run's length in HAL ticks (bits 30..0). static uint32_t run_len(uint32_t r) { return r & AB_IDLE; } +// A run's level: true for high (bit 31). static bool run_high(uint32_t r) { return (r >> 31) != 0; } void ab_reset(ab_t *a) { a->n = 0; } diff --git a/firmware/src/core/autobaud.h b/firmware/src/core/autobaud.h index e531d03..6ac7409 100644 --- a/firmware/src/core/autobaud.h +++ b/firmware/src/core/autobaud.h @@ -40,6 +40,7 @@ typedef struct { extern const uint32_t AB_BAUDS[]; extern const uint32_t AB_NBAUDS; +// Forget every run, ready to time a new burst of characters. void ab_reset(ab_t *a); // Add a run; merges it into the last if the level didn't change. False if full. bool ab_add(ab_t *a, bool high, uint32_t ticks); @@ -50,7 +51,8 @@ void ab_drop(ab_t *a, uint32_t pairs); // hz = HAL ticks per second. uint32_t ab_detect(const ab_t *a, uint32_t hz, const uart_format_t *f); -// Decode the runs at a baud rate as a UART would, calling fn for each -// character (errors = RX_ERR_* bits). Returns the statistics. +// Called by ab_decode() for each character: the byte and its RX_ERR_* bits. typedef void (*ab_byte_fn)(void *ctx, uint8_t byte, uint8_t errors); +// Decode the runs at a baud rate as a UART would, calling fn for each +// character. Returns the statistics (ab_detect scores rates with them). ab_stats_t ab_decode(const ab_t *a, uint32_t hz, uint32_t baud, const uart_format_t *f, ab_byte_fn fn, void *ctx); diff --git a/firmware/src/core/core.c b/firmware/src/core/core.c index 2bdd033..4b8473d 100644 --- a/firmware/src/core/core.c +++ b/firmware/src/core/core.c @@ -23,13 +23,18 @@ static const uart_format_t DEFAULT_FORMAT = {115200, 8, 1, 0}; #define ROOM_OFF (RING_SIZE * 3u / 4u) #define ROOM_ON (RING_SIZE / 2u) +// At least ms milliseconds from since to now (wraps safely: unsigned difference). static bool elapsed(uint32_t now, uint32_t since, uint32_t ms) { return (uint32_t)(now - since) >= ms; } +// Enter a detection/active state and note when, for the settle and timeout clocks. static void set_state(core_t *c, core_state_t s) { c->state = s; c->t_state = hal_millis(); } +// Turn the UART's hardware CTS on or off to match the flow setting: on always, +// off always, or (auto) once the far end has shown it uses its flow line. +// Only touches the HAL when the answer changes. static void apply_flow(core_t *c) { bool on = false; if (c->role != PS_ROLE_NONE) { @@ -51,6 +56,8 @@ static void apply_format(core_t *c) { if (c->role != PS_ROLE_NONE) hal_uart_format(&c->uart); } +// Whether to learn the far end's baud rate now: LEARN always does while a far +// end is attached; AUTO only in DCE mode, where a computer sets the speed. static bool ab_wanted(const core_t *c) { if (c->role == PS_ROLE_NONE) return false; if (c->settings.baud == PS_BAUD_LEARN) return true; @@ -72,6 +79,8 @@ static void ab_learn(core_t *c) { hal_autobaud(c->role); } +// Stop learning the baud rate and forget a learned one; the caller re-applies +// the host's format. static void ab_stop(core_t *c) { if (c->ab_state != AB_OFF) hal_autobaud(PS_ROLE_NONE); c->ab_state = AB_OFF; @@ -88,6 +97,9 @@ static void ab_update(core_t *c) { } } +// A far end was found (or the mode forces a role): power that role's chip, +// route the UART to it, and start with flow control off and the baud rate +// unlearned. static void activate(core_t *c, uint8_t role) { c->role = role; c->cts_latched = false; @@ -103,6 +115,8 @@ static void activate(core_t *c, uint8_t role) { set_state(c, ST_ACTIVE); } +// The far end went away: drop our output lines, shut both chips down and go +// back to settling before detection starts again. static void deactivate(core_t *c) { ab_stop(c); apply_format(c); // GET_INFO: back to the host's speed @@ -173,6 +187,10 @@ static void detect(core_t *c, uint32_t now) { } } +// The RS-232 lines we should drive now, as PS_LINE_* bits: the host's DTR and +// RTS mapped for the role (or DCD from the link, SPEC-W 4.4), then the +// SET_OUTPUTS override, then our flow line dropped while the buffer to the +// host is filling. static uint8_t outputs_for(core_t *c) { uint8_t dtr = (c->host_lines & PS_LINE_DTR) ? 1 : 0, rts = (c->host_lines & PS_LINE_RTS) ? 1 : 0; uint8_t out = 0, flow_line; @@ -197,6 +215,9 @@ static uint8_t outputs_for(core_t *c) { return out; } +// Once per poll: update the to-host buffer's room (with hysteresis), drive +// the output lines, read the inputs, latch auto flow control, and end a timed +// break. static void lines(core_t *c, uint32_t now) { uint32_t used = ring_count(&c->to_host); if (c->rx_room && used >= ROOM_OFF) c->rx_room = false; @@ -222,6 +243,8 @@ static void lines(core_t *c, uint32_t now) { } } +// Work out the status LEDs from the role and the lines (TR, CD, RI), plus +// short pulses for data each way (SD, RD), and hand them to the HAL. static void leds(core_t *c, uint32_t now) { uint8_t m = CORE_LED_PWR; if (c->role == PS_ROLE_DCE) { @@ -238,13 +261,18 @@ static void leds(core_t *c, uint32_t now) { hal_leds(m); } +// The characters ab_decode() recovers from the learning timings. typedef struct { uint8_t data[AB_MAX_RUNS / 2 + 1]; uint32_t n; uint8_t errors; } ab_out_t; +// ab_decode() callback: keep each decoded character and OR in its errors. static void ab_collect(void *ctx, uint8_t byte, uint8_t errors) { ab_out_t *o = ctx; if (o->n < sizeof o->data) o->data[o->n++] = byte; o->errors |= errors; } +// The baud rate is known: decode the characters it was learned from, switch +// the UART to the rate, and pass those characters on ahead of anything the +// UART receives next (its FIFO is flushed so nothing arrives twice). static void ab_lock(core_t *c, uint32_t hz, uint32_t baud) { static ab_out_t out; out.n = 0; @@ -266,6 +294,10 @@ static void ab_lock(core_t *c, uint32_t hz, uint32_t baud) { hal_irq_restore(irq); } +// Once per poll. Locked: start learning again if framing errors pile up (the +// far end changed speed). Learning: take the new line timings from the +// interrupt, try to fit a rate every CORE_AB_TRY_MS, and lock once the line +// is idle between characters. static void autobaud(core_t *c, uint32_t now) { if (c->ab_state == AB_LOCKED) { // framing errors coming in: the far end changed speed @@ -440,6 +472,8 @@ int core_vendor_in(core_t *c, uint8_t req, uint16_t value, uint8_t *buf, uint16_ } } +// A setting changed: save it to flash after CORE_SAVE_DELAY_MS of no further +// changes (core_poll does the write), to spare the flash. static void mark_dirty(core_t *c) { c->settings_dirty = true; c->t_dirty = hal_millis(); diff --git a/firmware/src/core/core.h b/firmware/src/core/core.h index cd4e2db..e124712 100644 --- a/firmware/src/core/core.h +++ b/firmware/src/core/core.h @@ -112,16 +112,26 @@ typedef struct { extern core_t g_core; +// Reset the core, load the saved settings (or defaults: auto mode, auto flow, +// auto baud) and apply the mode. Call once at startup. void core_init(core_t *c); // Call often from the main loop. void core_poll(core_t *c); // Host link events +// The host's DTR and RTS (mapped to the RS-232 lines in core_poll). void core_host_lines(core_t *c, bool dtr, bool rts); -void core_host_dcd(core_t *c, int8_t dcd); // -1: follow DTR (the default) +// DCD driven by a link in DCE mode instead of following DTR; -1: follow DTR (the default). +void core_host_dcd(core_t *c, int8_t dcd); +// The host's line coding (CDC values). Unsupported parts (1.5 stop bits, +// mark/space parity, odd data bits) are adjusted and flagged in GET_INFO; a +// learned baud rate stays in force. void core_host_format(core_t *c, uint32_t baud, uint8_t stop_bits_cdc, uint8_t parity_cdc, uint8_t data_bits); +// Send a break for duration_ms; 0 ends it, 0xFFFF holds it until 0 is sent. void core_host_break(core_t *c, uint16_t duration_ms); -uint32_t core_host_write(core_t *c, const uint8_t *data, uint32_t len); // returns bytes accepted +// Queue data for the RS-232 port and start the UART; returns bytes accepted. +uint32_t core_host_write(core_t *c, const uint8_t *data, uint32_t len); +// Take up to max bytes received from the RS-232 port; returns how many. uint32_t core_host_read(core_t *c, uint8_t *data, uint32_t max); // SERIAL_STATE to send to the host; true if it changed (or events are pending). bool core_serial_state(core_t *c, uint16_t *state); @@ -131,7 +141,9 @@ int core_vendor_in(core_t *c, uint8_t req, uint16_t value, uint8_t *buf, uint16_ // OUT: false to stall. bool core_vendor_out(core_t *c, uint8_t req, uint16_t value); -// From the UART interrupt +// From the UART interrupt: a received byte and its RX_ERR_* flags. Dropped +// while learning the baud rate (the UART is at the wrong speed); breaks are +// recorded but not passed on as data. static inline void core_isr_rx(core_t *c, uint8_t byte, uint8_t errors) { if (c->ab_state == AB_LEARNING) return; // received at a rate that's about to change if (errors & RX_ERR_FRAMING && c->ab_state == AB_LOCKED) c->ab_fe++; @@ -141,6 +153,7 @@ static inline void core_isr_rx(core_t *c, uint8_t byte, uint8_t errors) { else c->stats.from_rs232++; } } +// The UART can send: hand it the next queued byte, false if there's none. static inline bool core_isr_tx(core_t *c, uint8_t *byte) { if (!ring_get(&c->to_rs232, byte)) return false; c->stats.to_rs232++; @@ -157,5 +170,8 @@ static inline void core_isr_run(core_t *c, bool high, uint32_t ticks) { // Settings blob (for flash), with a checksum. #define CORE_SETTINGS_BLOB 16u +// Settings -> blob: "PSER", version 1, mode, flow, baud mode, CRC-32. void core_settings_pack(const core_settings_t *s, uint8_t out[CORE_SETTINGS_BLOB]); +// Blob -> settings; false (settings untouched) if it isn't a valid version-1 +// blob or a value is out of range. bool core_settings_unpack(core_settings_t *s, const uint8_t in[CORE_SETTINGS_BLOB]); diff --git a/firmware/src/core/hal.h b/firmware/src/core/hal.h index 60078fc..2e1b53c 100644 --- a/firmware/src/core/hal.h +++ b/firmware/src/core/hal.h @@ -19,6 +19,7 @@ typedef struct { uint8_t parity; // 0 none, 1 odd, 2 even } uart_format_t; +// Milliseconds since boot; wraps after about 49 days (callers compare differences). uint32_t hal_millis(void); // Transceivers: PS_ROLE_NONE = both shut down; DCE = chip A on, B off; DTE = B on, A off. @@ -30,29 +31,40 @@ uint8_t hal_detect_bits(void); // PS_DETECT_* // UART: which chip's pins it is on, its format, and flow control. void hal_uart_route(uint8_t role); +// Set the UART's baud rate, data bits, parity and stop bits. void hal_uart_format(const uart_format_t *f); -void hal_uart_hw_cts(bool on); // obey the far end's flow-control line +// Obey the far end's flow-control line in hardware: the UART pauses sending +// while it's deasserted. +void hal_uart_hw_cts(bool on); +// Hold the TX line in the break (space) state, or release it. void hal_uart_break(bool on); -void hal_uart_kick_tx(void); // data was queued for the UART -void hal_uart_rx_flush(void); // drop what the UART has received (call with interrupts off) +// Data was queued for the UART: start sending if it's idle (it pulls more +// with core_isr_tx from its interrupt). +void hal_uart_kick_tx(void); +// Drop what the UART has received (call with interrupts off). +void hal_uart_rx_flush(void); // Baud-rate learning: time the RX line of this role's chip (PS_ROLE_NONE // stops). From the first falling edge after the line is high, report each // run of one level with core_isr_run(), in ticks of hal_autobaud_hz() // (0 = too long to count). void hal_autobaud(uint8_t role); +// The timer rate hal_autobaud reports run lengths in, ticks per second. uint32_t hal_autobaud_hz(void); // Modem lines for the active role. void hal_lines_out(uint8_t role, uint8_t asserted); // DCE: DCD DSR RI CTS; DTE: DTR RTS uint8_t hal_lines_in(uint8_t role); // DCE: DTR RTS; DTE: DCD DSR RI CTS -void hal_leds(uint8_t mask); // CORE_LED_* (boards without LEDs ignore it) +// Show the status LEDs: CORE_LED_* bits, 1 = on (boards without LEDs ignore it). +void hal_leds(uint8_t mask); // Persistent settings (a small blob); load returns false if there is none. bool hal_settings_load(void *buf, uint32_t len); +// Write the blob to flash (rare: the core batches changes). void hal_settings_save(const void *buf, uint32_t len); // Interrupt masking around state shared with the UART interrupt. uint32_t hal_irq_save(void); +// Re-enable interrupts as they were before the matching hal_irq_save. void hal_irq_restore(uint32_t state); diff --git a/firmware/src/core/link.c b/firmware/src/core/link.c index 5950580..03fac5d 100644 --- a/firmware/src/core/link.c +++ b/firmware/src/core/link.c @@ -8,6 +8,8 @@ links_t g_links; +// Who should own the RS-232 port now (link.h's three rules): USB with DTR +// set, else the earliest-connected wireless link, else USB if plugged in. static uint8_t pick_owner(const links_t *s) { const link_t *usb = &s->l[LINK_USB]; if (usb->up && usb->dtr) return LINK_USB; @@ -31,6 +33,8 @@ static void apply(links_t *s, core_t *c, bool coding) { if (coding && l->has_format) core_host_format(c, l->baud, l->stop_cdc, l->parity_cdc, l->data_bits); } +// After any link event: re-pick the owner, count a change (ending the old +// owner's break), and pass the owner's state to the core. static void update(links_t *s, core_t *c) { uint8_t o = pick_owner(s); bool changed = o != s->owner; @@ -112,6 +116,8 @@ static void rc_line(links_t *s, core_t *c, uint8_t id, link_t *l) { } } +// Split the link's outgoing data into lines for rc_line(); lines too long +// to be a result code are skipped. Keeps a partial line between calls. static void rc_scan(links_t *s, core_t *c, uint8_t id, const uint8_t *p, uint32_t n) { link_t *l = &s->l[id]; for (uint32_t i = 0; i < n; i++) { diff --git a/firmware/src/core/link.h b/firmware/src/core/link.h index 70e7473..720ba7c 100644 --- a/firmware/src/core/link.h +++ b/firmware/src/core/link.h @@ -42,18 +42,23 @@ typedef struct { extern links_t g_links; +// All links down, no owner; SPP has DCD-from-result-codes on (SPEC-W 4.4). void links_init(links_t *s, core_t *c); // Connection events. Wireless links come up with DTR and RTS asserted, so a // plain terminal app gets DCD and DSR while connected (SPEC-W 4.3). void link_up(links_t *s, core_t *c, uint8_t id, bool up); +// A link's DTR and RTS changed (USB: SET_CONTROL_LINE_STATE; BLE: Outputs). +// USB's DTR also decides whether it takes the port. void link_lines(links_t *s, core_t *c, uint8_t id, bool dtr, bool rts); // CDC-style line coding (stop: 0 = 1, 1 = 1.5, 2 = 2; parity 0 none 1 odd 2 even ...) void link_format(links_t *s, core_t *c, uint8_t id, uint32_t baud, uint8_t stop_cdc, uint8_t parity_cdc, uint8_t data_bits); -bool link_break(links_t *s, core_t *c, uint8_t id, uint16_t ms); // false: not the owner +// Send a break (as core_host_break) if this link owns the port; false if not. +bool link_break(links_t *s, core_t *c, uint8_t id, uint16_t ms); // DCD from CONNECT / NO CARRIER in this link's data (SPEC-W 4.4). Default: on for SPP. void link_rc_dcd(links_t *s, core_t *c, uint8_t id, bool on); +// Whether this link owns the RS-232 port right now. static inline bool link_owns(const links_t *s, uint8_t id) { return s->owner == id; } // Data from the link toward the RS-232 port: returns bytes accepted. A link // that doesn't own the port has its data dropped (returns len). diff --git a/firmware/src/core/ring.h b/firmware/src/core/ring.h index 00a0fa1..50322be 100644 --- a/firmware/src/core/ring.h +++ b/firmware/src/core/ring.h @@ -18,10 +18,14 @@ typedef struct { uint8_t buf[RING_SIZE]; } ring_t; +// Empty the ring (only while neither side is using it). static inline void ring_init(ring_t *r) { r->head = r->tail = 0; } +// Bytes waiting to be read. static inline uint32_t ring_count(const ring_t *r) { return r->head - r->tail; } +// Bytes that can be written before it's full. static inline uint32_t ring_free(const ring_t *r) { return RING_SIZE - ring_count(r); } +// Producer: add one byte; false (byte dropped) if the ring is full. static inline bool ring_put(ring_t *r, uint8_t b) { uint32_t h = r->head; if (h - r->tail >= RING_SIZE) return false; @@ -30,6 +34,7 @@ static inline bool ring_put(ring_t *r, uint8_t b) { return true; } +// Consumer: take the oldest byte; false if the ring is empty. static inline bool ring_get(ring_t *r, uint8_t *b) { uint32_t t = r->tail; if (r->head == t) return false; @@ -38,12 +43,14 @@ static inline bool ring_get(ring_t *r, uint8_t *b) { return true; } +// Producer: add up to n bytes; returns how many fitted. static inline uint32_t ring_write(ring_t *r, const uint8_t *src, uint32_t n) { uint32_t done = 0; while (done < n && ring_put(r, src[done])) done++; return done; } +// Consumer: take up to n bytes; returns how many there were. static inline uint32_t ring_read(ring_t *r, uint8_t *dst, uint32_t n) { uint32_t done = 0; while (done < n && ring_get(r, &dst[done])) done++; diff --git a/firmware/tools/check_docs.py b/firmware/tools/check_docs.py new file mode 100644 index 0000000..76ac14b --- /dev/null +++ b/firmware/tools/check_docs.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: AGPL-3.0-only +# Copyright (C) 2026 Neo-Desktop +"""Every function in the firmware has a comment saying what it does. + + python3 firmware/tools/check_docs.py # lists what's missing; exit 1 if anything is + +The rule: + - a function's definition has a comment directly above it (attribute or + `static` lines may sit in between), or + - it is public and its prototype in a header has one: the header documents + the interface, the definition needn't repeat it; + - every prototype in a header has one. +Checked: C sources and headers, the C block of .pio files, and the +emulator test (TypeScript). Generated files (src/generated) and the SDK's +own files are skipped. +""" +import os, re, sys + +ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") +SKIP_DIRS = {"generated", "node_modules", ".git"} +SKIP_FILES = {"pico_sdk_import.cmake"} +KEYWORDS = {"if", "for", "while", "switch", "return", "sizeof", "do", "else", "case", "defined", "_Static_assert"} + + +def blank_code(text): + """The text with comments and strings blanked (newlines kept), and the set + of line numbers (1-based) on which a comment ends.""" + out, ends, i, n = [], set(), 0, len(text) + line = 1 + while i < n: + c = text[i] + if text.startswith("//", i): + j = text.find("\n", i) + j = n if j < 0 else j + out.append(" " * (j - i)); ends.add(line); i = j + elif text.startswith("/*", i): + j = text.find("*/", i + 2) + j = n if j < 0 else j + 2 + seg = text[i:j] + out.append(re.sub(r"[^\n]", " ", seg)) + line += seg.count("\n"); ends.add(line); i = j + elif c in "\"'": + j = i + 1 + while j < n and text[j] != c: + j += 2 if text[j] == "\\" else 1 + seg = text[i:j + 1] + out.append(c + re.sub(r"[^\n]", " ", seg[1:-1]) + c); line += seg.count("\n"); i = j + 1 + else: + out.append(c); line += c == "\n"; i += 1 + return "".join(out), ends + + +def documented(lines, ends, first, last=None): + """A comment ends on the line above `first` (1-based), skipping lines that + only carry attributes or qualifiers of the same declaration, or trails the + signature itself (`void f(void); // what it does`).""" + if any(ln in ends for ln in range(first, (last or first) + 1)): + return True + k = first - 1 + while k >= 1 and re.fullmatch(r"\s*(static|inline|__attribute__\(\(.*\)\)|__not_in_flash_func.*|\w+_ISR)?\s*", + lines[k - 1]) and lines[k - 1].strip(): + k -= 1 + return k in ends + + +def c_functions(path): + """(name, first line, is_definition, is_static, documented) for each function + declared or defined at file scope.""" + text = open(path).read() + if path.endswith(".pio"): # only its C block + m = re.search(r"% c-sdk \{(.*?)%\}", text, re.S) + if not m: + return [] + pre = text[:m.start(1)] + text = re.sub(r"[^\n]", " ", pre) + m.group(1) + code, ends = blank_code(text) + lines = text.split("\n") + found, depth, stmt_start, i = [], 0, None, 0 + stmt = [] + for ln, raw in enumerate(code.split("\n"), 1): + s = raw + if s.lstrip().startswith("#"): # preprocessor line (and its continuations, close enough) + continue + for ch in s: + if depth == 0: + if not stmt and ch.isspace(): + continue + if not stmt: + stmt_start = ln + if ch == "{" or ch == ";": + body = "".join(stmt) + m = re.search(r"(\w+)\s*\(([^()]|\([^()]*\))*\)\s*(__attribute__\(\(.*\)\)\s*)?$", body) + if m and m.group(1) not in KEYWORDS and not re.match(r"\s*(typedef|extern\s+\"C\")", body) \ + and "=" not in body.split("(")[0] and not re.match(r"\s*(struct|enum|union)\s+\w+\s*$", body) \ + and re.match(r"\s*[A-Za-z_]", body): + found.append((m.group(1), stmt_start, ch == "{", bool(re.match(r"\s*static\b", body)), + documented(lines, ends, stmt_start, ln))) + stmt = [] + if ch == "{": + depth = 1 + continue + stmt.append(ch) + else: + depth += (ch == "{") - (ch == "}") + if depth == 0 and stmt: + stmt.append(" ") + return found + + +def ts_functions(path): + text = open(path).read() + code, ends = blank_code(text) + lines = text.split("\n") + out = [] + for ln, s in enumerate(code.split("\n"), 1): + m = re.match(r"\s*(export\s+)?(async\s+)?function\s+(\w+)|\s*(export\s+)?const\s+(\w+)\s*=\s*(async\s*)?\(.*\)\s*(:\s*[^=]+)?=>", s) + if m: + out.append((m.group(3) or m.group(5), ln, True, True, documented(lines, ends, ln))) + return out + + +def main(): + missing, total = [], 0 + header_doc = {} + files = [] + for d, dirs, fs in os.walk(ROOT): + dirs[:] = [x for x in dirs if x not in SKIP_DIRS and not x.startswith("build")] + for f in sorted(fs): + if f in SKIP_FILES: + continue + if f.endswith((".c", ".h", ".pio", ".ts")): + files.append(os.path.join(d, f)) + parsed = {p: (ts_functions(p) if p.endswith(".ts") else c_functions(p)) for p in sorted(files)} + for p, fns in parsed.items(): + if p.endswith(".h"): + for name, ln, is_def, st, doc in fns: + if not is_def: + header_doc[name] = header_doc.get(name, False) or doc + for p, fns in parsed.items(): + rel = os.path.relpath(p, ROOT) + for name, ln, is_def, st, doc in fns: + if not is_def and not p.endswith(".h"): + continue # a forward declaration in a .c: the definition carries the comment + total += 1 + if doc or (is_def and not st and header_doc.get(name)): + continue + missing.append(f"{rel}:{ln}: {name}") + for m in missing: + print("undocumented:", m) + print(f"{total - len(missing)} of {total} functions documented") + sys.exit(1 if missing else 0) + + +if __name__ == "__main__": + main()