From 6c17e25a767b7695a63d153a85cefaa6e7cb0c6f Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Sun, 8 Nov 2026 19:34:16 -0500 Subject: [PATCH] palm-client: the request retry rules are in request.c (session.c supplies the link), host-tested with a scripted link in tests/test_request.c: a frame that went out is sent again only while tries allows, one that didn't go is resent up to 4 times without using up tries, one cut off part way never is. check_source.py checks that the functions that send over the air or take a queued message (messages, reactions, adverts, syncs, logins, traces) only ever use tries 1 --- palm-client/Makefile | 4 +- palm-client/src/request.c | 27 +++++++ palm-client/src/request.h | 56 +++++++++++++ palm-client/src/session.c | 76 +++++++++++------ palm-client/tests/test_request.c | 130 ++++++++++++++++++++++++++++++ palm-client/tools/check_source.py | 65 +++++++++++++++ 6 files changed, 332 insertions(+), 26 deletions(-) create mode 100644 palm-client/src/request.c create mode 100644 palm-client/src/request.h create mode 100644 palm-client/tests/test_request.c diff --git a/palm-client/Makefile b/palm-client/Makefile index 32a0c75..167e39c 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -12,7 +12,7 @@ APP = visormesh NAME = VisorMesh CREATOR = VMsh -SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/screen_fmt.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c +SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/request.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/screen_fmt.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -88,6 +88,8 @@ test: | build build/test_text gcc -std=c99 -Wall -Wextra -o build/test_sblora tests/test_sblora.c src/sb_spi.c build/test_sblora + gcc -std=c99 -Wall -Wextra -o build/test_request tests/test_request.c src/request.c + build/test_request gcc -std=c99 -Wall -Wextra -Isrc -o build/test_screens tests/test_screens.c src/screen_fmt.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c build/test_screens gcc -std=c99 -Wall -Wextra -o build/test_scopes tests/test_scopes.c src/scope_proto.c src/protocol.c src/sha256.c diff --git a/palm-client/src/request.c b/palm-client/src/request.c new file mode 100644 index 0000000..6ca5b8e --- /dev/null +++ b/palm-client/src/request.c @@ -0,0 +1,27 @@ +#include "request.h" + +Err VmRequestRun(const VmRequestLink *l, UInt16 tries, Boolean *linkGone) +{ + Err err = vmErrNoReply; + UInt16 t = 0, unsent = 0; + + *linkGone = 0; + while (t < tries) { + if (l->cancelled(l->ctx)) + return vmErrCancelled; + l->drain(l->ctx); + err = l->send(l->ctx); + if (err == serErrTimeOut && ++unsent < VM_UNSENT_TRIES) + continue; + if (err != errNone) + break; + err = l->wait(l->ctx); + if (err != vmErrNoReply) + return err; + t++; + } + if (err == serErrTimeOut || err == vmErrPartSent) + err = vmErrNoReply; + *linkGone = 1; + return err; +} diff --git a/palm-client/src/request.h b/palm-client/src/request.h new file mode 100644 index 0000000..e000498 --- /dev/null +++ b/palm-client/src/request.h @@ -0,0 +1,56 @@ +/* + * The retry rules for a request to the radio, apart from the transport so + * the host tests (tests/test_request.c) can drive them with a fake link: + * + * - a frame that didn't go at all (serErrTimeOut from the transport: the + * USB host re-attaching) is sent again, up to VM_UNSENT_TRIES times, + * whatever tries says; + * - a frame that went out but got no reply is sent again only while tries + * lasts, so tries > 1 only for commands that are safe to repeat; + * - a frame cut off part way (vmErrPartSent) is never sent again. + * + * Pure C: session.c supplies the link. + */ +#ifndef VM_REQUEST_H +#define VM_REQUEST_H + +#include "protocol.h" + +#ifdef __palmos__ +#include +#else +typedef UInt16 Err; +#define errNone 0 +#define serErrTimeOut 0x0305 +#define appErrorClass 0x8000 +#endif + +/* session.h and transport.h define these on the Palm; the host tests get them here */ +#ifndef vmErrNoReply +#define vmErrNoReply (appErrorClass | 1) +#endif +#ifndef vmErrCancelled +#define vmErrCancelled (appErrorClass | 7) +#endif +#ifndef vmErrPartSent +#define vmErrPartSent (appErrorClass | 9) +#endif + +#define VM_UNSENT_TRIES 4 /* sends that didn't go, about a second each (transport_srm.c) */ + +typedef struct { + Boolean (*cancelled)(void *ctx); /* the user wants out */ + void (*drain)(void *ctx); /* drops stale input before a send */ + Err (*send)(void *ctx); /* the frame: errNone, serErrTimeOut (nothing went) or another error */ + Err (*wait)(void *ctx); /* the reply: errNone, vmErrNoReply or another error */ + void *ctx; +} VmRequestLink; + +/* + * Runs one request. *linkGone is set when it ended without an answer from + * the radio (which always answers at once), so the caller can go offline. + * serErrTimeOut and vmErrPartSent come back as vmErrNoReply. + */ +Err VmRequestRun(const VmRequestLink *l, UInt16 tries, Boolean *linkGone); + +#endif diff --git a/palm-client/src/session.c b/palm-client/src/session.c index 6de66da..dd3ad02 100644 --- a/palm-client/src/session.c +++ b/palm-client/src/session.c @@ -11,6 +11,7 @@ #include "contacts.h" #include "ui/text.h" #include "session.h" +#include "request.h" #include "heard.h" #include "remote.h" #include "sounds.h" @@ -25,7 +26,6 @@ #define VM_APP_NAME "VisorMesh" #define VM_REPLY_TIMEOUT (2 * SysTicksPerSecond()) -#define VM_UNSENT_TRIES 4 /* sends that didn't go, about a second each (transport_srm.c) */ #define VM_CONNECT_TRIES 3 /* over USB the host bridge needs a moment to attach */ #define VM_SYNC_MAX 50 /* messages pulled per sync, so the UI isn't blocked forever */ #define VM_ACK_MIN_SECS 10 /* shortest wait for a DM ack, whatever the radio estimates */ @@ -203,40 +203,66 @@ static void DrainInput(void) VmDecoderReset(&gDecoder); } +/* The request being made, for the VmRequestRun link. */ +typedef struct { + const UInt8 *frame, *codes; + UInt16 len, n; +} ReqCtx; + +static Boolean ReqCancelled(void *ctx) +{ + (void)ctx; + return UserWaiting(); +} + +static void ReqDrain(void *ctx) +{ + (void)ctx; + DrainInput(); +} + +static Err ReqSend(void *ctx) +{ + ReqCtx *c = (ReqCtx *)ctx; + + return TransportSend(c->frame, c->len); +} + +static Err ReqWait(void *ctx) +{ + ReqCtx *c = (ReqCtx *)ctx; + + return WaitForAny(c->codes, c->n, VM_REPLY_TIMEOUT); +} + /* - * Sends a request frame and waits for one of the reply codes. A frame that - * didn't go at all (the USB host re-attaching) is sent again whatever tries - * says, up to VM_UNSENT_TRIES times. A try that went out but got no reply is - * only repeated while tries lasts, so only use tries > 1 for commands that - * are safe to repeat. + * Sends a request frame and waits for one of the reply codes, by the rules + * in request.h: only use tries > 1 for commands that are safe to repeat + * (tools/check_source.py checks the sends that must only try once). */ static Err RequestAny(const UInt8 *frame, UInt16 len, const UInt8 *codes, UInt16 n, UInt16 tries) { - Err err = vmErrNoReply; - UInt16 t = 0, unsent = 0; + ReqCtx c; + VmRequestLink link; + Boolean gone; + Err err; - while (t < tries) { - if (UserWaiting()) - return vmErrCancelled; - DrainInput(); - err = TransportSend(frame, len); - if (err == serErrTimeOut && ++unsent < VM_UNSENT_TRIES) - continue; - if (err != errNone) - break; - err = WaitForAny(codes, n, VM_REPLY_TIMEOUT); - if (err != vmErrNoReply) - return err; - t++; - } - if (err == serErrTimeOut || err == vmErrPartSent) - err = vmErrNoReply; + c.frame = frame; + c.len = len; + c.codes = codes; + c.n = n; + link.cancelled = ReqCancelled; + link.drain = ReqDrain; + link.send = ReqSend; + link.wait = ReqWait; + link.ctx = &c; + err = VmRequestRun(&link, tries, &gone); /* * The radio itself always answers at once, so the link is gone (the bridge * stopped, a cable pulled): offline, rather than "online" with every * command failing. gConnected first, so the disconnect sends nothing. */ - if (gConnected) { + if (gone && gConnected) { gConnected = false; gLost = true; SessionDisconnect(); diff --git a/palm-client/tests/test_request.c b/palm-client/tests/test_request.c new file mode 100644 index 0000000..90a16a0 --- /dev/null +++ b/palm-client/tests/test_request.c @@ -0,0 +1,130 @@ +/* + * Host tests for the request retry rules (src/request.c), with a scripted + * link: what each send and each wait returns, and how many frames went out. + * The point: a frame that went out is only sent again when tries allows it, + * so messages, adverts and syncs (tries 1) are never sent twice. + * Run with `make test`. + */ +#include +#include "../src/request.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \ +} while (0) + +#define vmErrRadio (appErrorClass | 2) + +typedef struct { + Err sends[8], waits[8]; + int nSends, nWaits; /* script positions */ + int delivered; /* sends that went out whole */ + int attempts; /* every call to send */ + int cancelAt; /* cancelled from this attempt on; 0 = never */ +} Script; + +static Boolean Cancelled(void *ctx) +{ + Script *s = (Script *)ctx; + + return s->cancelAt && s->attempts + 1 >= s->cancelAt; +} + +static void Drain(void *ctx) +{ + (void)ctx; +} + +static Err Send(void *ctx) +{ + Script *s = (Script *)ctx; + Err e = s->sends[s->nSends++]; + + s->attempts++; + if (e == errNone) + s->delivered++; + return e; +} + +static Err Wait(void *ctx) +{ + Script *s = (Script *)ctx; + + return s->waits[s->nWaits++]; +} + +static Err Run(Script *s, UInt16 tries, Boolean *gone) +{ + VmRequestLink l; + + l.cancelled = Cancelled; + l.drain = Drain; + l.send = Send; + l.wait = Wait; + l.ctx = s; + return VmRequestRun(&l, tries, gone); +} + +int main(void) +{ + Boolean gone; + Err e; + + /* answered first time */ + { Script s = { { errNone }, { errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 1, &gone); + CHECK(e == errNone && !gone && s.delivered == 1); } + + /* tries 1, sent but no answer: never sent again, and the link is reported gone */ + { Script s = { { errNone, errNone }, { vmErrNoReply, errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 1, &gone); + CHECK(e == vmErrNoReply && gone && s.delivered == 1); } + + /* tries 2, safe to repeat: the second try answers */ + { Script s = { { errNone, errNone }, { vmErrNoReply, errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 2, &gone); + CHECK(e == errNone && !gone && s.delivered == 2); } + + /* tries 2, no answer either time */ + { Script s = { { errNone, errNone }, { vmErrNoReply, vmErrNoReply }, 0, 0, 0, 0, 0 }; + e = Run(&s, 2, &gone); + CHECK(e == vmErrNoReply && gone && s.delivered == 2); } + + /* tries 1, the frame didn't go twice (USB re-attaching), then did: delivered once */ + { Script s = { { serErrTimeOut, serErrTimeOut, errNone }, { errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 1, &gone); + CHECK(e == errNone && !gone && s.attempts == 3 && s.delivered == 1); } + + /* frames that don't go stop after VM_UNSENT_TRIES, nothing delivered */ + { Script s = { { serErrTimeOut, serErrTimeOut, serErrTimeOut, serErrTimeOut, errNone }, { errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 1, &gone); + CHECK(e == vmErrNoReply && gone && s.attempts == VM_UNSENT_TRIES && s.delivered == 0); } + + /* unsent sends don't use up tries: one didn't go, one went unanswered, tries 1 stops there */ + { Script s = { { serErrTimeOut, errNone, errNone }, { vmErrNoReply, errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 1, &gone); + CHECK(e == vmErrNoReply && s.attempts == 2 && s.delivered == 1); } + + /* cut off part way: never resent, even with tries 2 */ + { Script s = { { vmErrPartSent, errNone }, { errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 2, &gone); + CHECK(e == vmErrNoReply && gone && s.attempts == 1); } + + /* the radio said no: that's an answer, not a lost link, and not retried */ + { Script s = { { errNone, errNone }, { vmErrRadio, errNone }, 0, 0, 0, 0, 0 }; + e = Run(&s, 2, &gone); + CHECK(e == vmErrRadio && !gone && s.delivered == 1); } + + /* the user taps first: nothing sent */ + { Script s = { { errNone }, { errNone }, 0, 0, 0, 0, 1 }; + e = Run(&s, 2, &gone); + CHECK(e == vmErrCancelled && !gone && s.attempts == 0); } + + if (failures) { + printf("%d failure(s)\n", failures); + return 1; + } + printf("request: all tests passed\n"); + return 0; +} diff --git a/palm-client/tools/check_source.py b/palm-client/tools/check_source.py index d0efa65..bbf42ed 100644 --- a/palm-client/tools/check_source.py +++ b/palm-client/tools/check_source.py @@ -10,6 +10,9 @@ check_sections.py and check_ui.py): menu's U X C P S K G forms every form gets an event handler in main.c's frmLoadEvent switch, or is opened as a dialog with FrmInitForm + once the functions that send over the air or take a queued message + only ever make requests with tries 1 (ONCE): a repeat would + transmit twice or skip a message (src/request.h) ids no number used twice within one form, one menu bar or one resource type (forms, alerts, menu bars); numbers may repeat across those, which Palm OS keeps apart @@ -20,6 +23,10 @@ import re import sys EDIT_KEYS = set('UXCPSKG') + +# session.c functions whose requests must only try once. +ONCE = ('SendRecord', 'SessionSendReaction', 'SessionSyncMessages', 'SessionSendAdvert', + 'SessionRequestSent', 'SessionTrace', 'SessionLogin', 'SessionAskRegions') EXTRA = re.compile(r'\bVM_SECT[2-5]\b') @@ -66,6 +73,63 @@ def check_tables(c_files, extra): return problems +def function_bodies(text): + """{name: (first line, body)} for K&R functions ({ and } at column 0).""" + bodies, lines, name, start, body = {}, text.split('\n'), None, 0, [] + for no, line in enumerate(lines, 1): + if name is None: + m = re.match(r'^[A-Za-z_][\w \t\*]*?\b(\w+)\s*\(', line) + if m and not line.rstrip().endswith(';'): + head = m.group(1) + if line.startswith('{'): + name, start, body = head, no, [] + continue + if line.startswith('}'): + bodies[name] = (start, '\n'.join(body)) + name = None + continue + body.append(line) + return bodies + + +def call_args(text, at): + """The top-level arguments of the call whose '(' is at text[at].""" + depth, args, cur = 0, [], '' + for ch in text[at:]: + if ch == '(': + depth += 1 + if depth == 1: + continue + elif ch == ')': + depth -= 1 + if depth == 0: + args.append(cur.strip()) + return args + elif ch == ',' and depth == 1: + args.append(cur.strip()) + cur = '' + continue + cur += ch + return args + + +def check_once(path): + problems = [] + bodies = function_bodies(strip_comments(open(path, encoding='latin-1').read())) + for name in ONCE: + if name not in bodies: + problems.append('%s: %s() not found: update ONCE in tools/check_source.py' % (path, name)) + continue + start, body = bodies[name] + for m in re.finditer(r'\b(Request|RequestAny)\s*\(', body): + args = call_args(body, m.end() - 1) + if args and args[-1] != '1': + no = start + body.count('\n', 0, m.start()) + 1 + problems.append('%s:%d: %s() makes a request with tries %s: it sends over the air or takes ' + 'a queued message, so a repeat would do it twice' % (path, no, name, args[-1])) + return problems + + def read_menus(rcp): """[(bar, [(label, id, key)])] from the MENU blocks.""" bars, bar = [], None @@ -163,6 +227,7 @@ def main(argv): problems += check_menus(rcp, code) problems += check_forms(rcp, code, main_c) problems += check_ids(rcp, header) + problems += check_once([p for p in c_files if p.endswith('src/session.c')][0]) for p in problems: print('check_source: ' + p) if problems: