palm-client: connect screen (DEVICE_QUERY + APP_START) and session layer

session.c does request/reply over the transport: waits for the wanted
reply code, skips pushes (remembering PUSH_MSG_WAITING), maps RESP_ERR,
and retries sends while the USB bridge attaches. Connect keeps the link
open and the main form shows node name, frequency, BW/SF/CR, TX power
and firmware. Adds a DEVICE_INFO parser.

Fixes the DEVICE_INFO test fixture: it was hand-typed with two bytes
shifted, and an earlier test had been adjusted to match the typo (version
at offset 62). It's now generated from the capture file; the version is
at 60, as in MeshCore's layout.
This commit is contained in:
rich committed 2026-11-01 11:53:30 -05:00
1 parent f4bab463d1
commit c9183dc591
9 files changed
+304 -90

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ APP = visormesh
NAME = VisorMesh
CREATOR = VMsh
SRCS = src/main.c src/protocol.c src/transport_srm.c src/ports.c
SRCS = src/main.c src/protocol.c src/transport_srm.c src/session.c src/ports.c
OBJS = $(SRCS:src/%.c=build/%.o)
CC = m68k-palmos-gcc
+15 -6
View File
@@ -8,13 +8,22 @@ NOFRAME
USABLE
BEGIN
TITLE "VisorMesh"
LABEL "MeshCore client for the Visor" AUTOID AT (5 20)
LABEL "Radio:" AUTOID AT (5 45) FONT 1
LABEL "Not connected " ID MainStatusLabel AT (40 45)
LABEL "Link:" AUTOID AT (5 65) FONT 1
PUSHBUTTON "Serial" ID MainSerialPush AT (40 64 40 12) GROUP MainLinkGroup
LABEL "Link:" AUTOID AT (5 18) FONT 1
PUSHBUTTON "Serial" ID MainSerialPush AT (50 17 40 12) GROUP MainLinkGroup
PUSHBUTTON "USB" ID MainUsbPush AT (PREVRIGHT+1 PREVTOP 40 12) GROUP MainLinkGroup
BUTTON "Query radio" ID MainQueryButton AT (20 140 AUTO AUTO)
LABEL "Node:" AUTOID AT (5 40) FONT 1
LABEL " " ID MainNameLabel AT (50 40)
LABEL "Freq:" AUTOID AT (5 54) FONT 1
LABEL " " ID MainFreqLabel AT (50 54)
LABEL "Mode:" AUTOID AT (5 66) FONT 1
LABEL " " ID MainModeLabel AT (50 66)
LABEL "Power:" AUTOID AT (5 78) FONT 1
LABEL " " ID MainPowerLabel AT (50 78)
LABEL "Firmware:" AUTOID AT (5 90) FONT 1
LABEL " " ID MainFirmwareLabel AT (50 90)
BUTTON "Disconnect" ID MainConnectButton AT (20 140 AUTO AUTO)
BUTTON "Ports" ID MainPortsButton AT (PREVRIGHT+10 PREVTOP AUTO AUTO)
END
+92 -71
View File
@@ -2,13 +2,15 @@
#include "resources.h"
#include "protocol.h"
#include "transport.h"
#include "session.h"
#include "ports.h"
#define VM_MIN_ROM_VERSION sysMakeROMVersion(3, 5, 0, sysROMStageRelease, 0)
#define VM_QUERY_TIMEOUT (2 * SysTicksPerSecond())
#define VM_QUERY_ATTEMPTS 3 /* over USB the host bridge needs a moment to attach after SrmOpen */
#define VM_LABEL_CHARS 22 /* the info labels in the .rcp are padded to this width */
static VmFrameDecoder gDecoder;
/* Radio state shown on the main form; kept here so it survives a trip to the Ports form. */
static VmDeviceInfo gDevice;
static VmSelfInfo gSelf;
static Err RomVersionCompatible(UInt32 required, UInt16 launchFlags)
{
@@ -23,100 +25,119 @@ static Err RomVersionCompatible(UInt32 required, UInt16 launchFlags)
return sysErrRomIncompatible;
}
static void SetStatus(const Char *text)
/* Replaces a label's text, truncated to the width reserved for it in the .rcp. */
static void SetLabel(FormType *form, UInt16 id, const Char *text)
{
FormType *form = FrmGetActiveForm();
Char buf[VM_LABEL_CHARS + 1];
UInt16 index = FrmGetObjectIndex(form, id);
Boolean visible = FrmVisible(form);
FrmHideObject(form, FrmGetObjectIndex(form, MainStatusLabel));
FrmCopyLabel(form, MainStatusLabel, text);
FrmShowObject(form, FrmGetObjectIndex(form, MainStatusLabel));
StrNCopy(buf, text, VM_LABEL_CHARS);
buf[VM_LABEL_CHARS] = 0;
if (visible)
FrmHideObject(form, index);
FrmCopyLabel(form, id, buf);
if (visible)
FrmShowObject(form, index);
}
/* Sends CMD_DEVICE_QUERY and waits for any '>' frame in reply. */
static void QueryRadio(void)
/* Formats e.g. 869618 kHz as "869.618 MHz" and 62500 Hz as "62.5" (kHz), without floating point. */
static void FormatFreq(Char *out, UInt32 kHz)
{
StrPrintF(out, "%lu.%03lu MHz", kHz / 1000, kHz % 1000);
}
static void FormatBandwidth(Char *out, UInt32 hz)
{
if (hz % 1000)
StrPrintF(out, "%lu.%lu", hz / 1000, (hz % 1000) / 100);
else
StrPrintF(out, "%lu", hz / 1000);
}
static void ShowRadioInfo(FormType *form)
{
Char buf[40], bw[12];
if (!SessionIsConnected()) {
SetLabel(form, MainNameLabel, "Not connected");
SetLabel(form, MainFreqLabel, "-");
SetLabel(form, MainModeLabel, "-");
SetLabel(form, MainPowerLabel, "-");
SetLabel(form, MainFirmwareLabel, "-");
CtlSetLabel(FrmGetObjectPtr(form, FrmGetObjectIndex(form, MainConnectButton)), "Connect");
return;
}
SetLabel(form, MainNameLabel, gSelf.name);
FormatFreq(buf, gSelf.freqKHz);
SetLabel(form, MainFreqLabel, buf);
FormatBandwidth(bw, gSelf.bwHz);
StrPrintF(buf, "BW %s SF%u CR%u", bw, gSelf.sf, gSelf.cr);
SetLabel(form, MainModeLabel, buf);
StrPrintF(buf, "%u dBm (max %u)", gSelf.txPowerDbm, gSelf.maxTxPowerDbm);
SetLabel(form, MainPowerLabel, buf);
StrPrintF(buf, "%s (%u)", gDevice.version, gDevice.fwVerCode);
SetLabel(form, MainFirmwareLabel, buf);
CtlSetLabel(FrmGetObjectPtr(form, FrmGetObjectIndex(form, MainConnectButton)), "Disconnect");
}
static void ConnectOrDisconnect(FormType *form)
{
UInt8 frame[VM_HEADER_LEN + 2];
UInt8 rx[32];
UInt16 frameLen, n, i, attempt;
UInt32 deadline;
Char msg[32];
Err err;
SetStatus("Opening port...");
err = TransportOpen();
if (err != errNone) {
StrPrintF(msg, "Open failed (0x%x)", err);
SetStatus(msg);
if (SessionIsConnected()) {
SessionDisconnect();
ShowRadioInfo(form);
return;
}
frameLen = VmBuildDeviceQuery(frame, sizeof(frame));
VmDecoderReset(&gDecoder);
/* Over USB the Visor only appears to the host after SrmOpen; give the bridge time to attach. */
if (TransportGetPort() == VM_PORT_USB)
SysTaskDelay(SysTicksPerSecond());
for (attempt = 1; attempt <= VM_QUERY_ATTEMPTS; attempt++) {
err = TransportSend(frame, frameLen);
if (err == serErrTimeOut) {
/* Nobody reading yet (host still attaching): try again next attempt. */
StrPrintF(msg, "Host not ready (%u)...", attempt);
SetStatus(msg);
continue;
}
if (err != errNone) {
StrPrintF(msg, "Send failed (0x%x)", err);
SetStatus(msg);
TransportClose();
return;
}
StrPrintF(msg, "Waiting for reply (%u)...", attempt);
SetStatus(msg);
deadline = TimGetTicks() + VM_QUERY_TIMEOUT;
while (TimGetTicks() < deadline) {
n = TransportRecv(rx, sizeof(rx), deadline - TimGetTicks(), &err);
for (i = 0; i < n; i++) {
if (VmDecoderFeed(&gDecoder, rx[i])) {
StrPrintF(msg, "Reply 0x%x, %u bytes",
gDecoder.len ? gDecoder.payload[0] : 0, gDecoder.len);
SetStatus(msg);
TransportClose();
return;
}
}
}
SetLabel(form, MainNameLabel, "Connecting...");
err = SessionConnect(&gDevice, &gSelf);
if (err == errNone) {
ShowRadioInfo(form);
return;
}
SetStatus(err == serErrTimeOut ? "Send timed out" : "No reply");
TransportClose();
ShowRadioInfo(form);
if (err == vmErrNoReply)
StrCopy(msg, "No reply from radio");
else if (err == vmErrRadio)
StrCopy(msg, "Radio returned an error");
else if (err == vmErrBadReply)
StrCopy(msg, "Unexpected reply");
else
StrPrintF(msg, "Port error 0x%x", err);
SetLabel(form, MainNameLabel, msg);
}
static Boolean MainFormHandleEvent(EventType *event)
{
FormType *form = FrmGetActiveForm();
switch (event->eType) {
case frmOpenEvent: {
FormType *form = FrmGetActiveForm();
UInt16 link = (TransportGetPort() == VM_PORT_USB) ? MainUsbPush : MainSerialPush;
FrmSetControlGroupSelection(form, MainLinkGroup, link);
ShowRadioInfo(form);
FrmDrawForm(form);
return true;
}
case ctlSelectEvent:
if (event->data.ctlSelect.controlID == MainSerialPush) {
TransportSetPort(VM_PORT_SERIAL);
switch (event->data.ctlSelect.controlID) {
case MainSerialPush:
case MainUsbPush:
/* Changing the link drops any open connection; it applies on the next Connect. */
if (SessionIsConnected()) {
SessionDisconnect();
ShowRadioInfo(form);
}
TransportSetPort(event->data.ctlSelect.controlID == MainUsbPush ? VM_PORT_USB : VM_PORT_SERIAL);
return true;
}
if (event->data.ctlSelect.controlID == MainUsbPush) {
TransportSetPort(VM_PORT_USB);
case MainConnectButton:
ConnectOrDisconnect(form);
return true;
}
if (event->data.ctlSelect.controlID == MainQueryButton) {
QueryRadio();
return true;
}
if (event->data.ctlSelect.controlID == MainPortsButton) {
case MainPortsButton:
FrmGotoForm(PortsForm);
return true;
}
@@ -170,7 +191,7 @@ UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
if (cmd == sysAppLaunchCmdNormalLaunch) {
FrmGotoForm(MainForm);
AppEventLoop();
TransportClose();
SessionDisconnect();
FrmCloseAllForms();
}
return errNone;
+18
View File
@@ -2,6 +2,7 @@
enum { ST_SYNC, ST_LEN_LO, ST_LEN_HI, ST_PAYLOAD };
#define DEVICE_INFO_MIN_LEN 80 /* through the version string */
#define SELF_INFO_FIXED_LEN 58 /* everything before the variable-length name */
#define CONTACT_LEN (1 + VM_PUB_KEY_SIZE + 3 + VM_MAX_PATH_SIZE + VM_NAME_SIZE + 16)
#define MSG_V3_HEADER_LEN 4 /* code, snr, 2 reserved */
@@ -131,6 +132,23 @@ UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize)
return VmEncodeFrame(out, outSize, p, 1);
}
/*
* DEVICE_INFO: code, fw_ver_code, max_contacts / 2, max_channels, ble_pin (4),
* build_date[12], manufacturer[40], version[20], then optional newer fields.
*/
Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out)
{
if (len < DEVICE_INFO_MIN_LEN || p[0] != VM_RESP_DEVICE_INFO)
return 0;
out->fwVerCode = p[1];
out->maxContacts = (UInt16)p[2] * 2;
out->maxChannels = p[3];
CopyName(out->buildDate, sizeof(out->buildDate), p + 8, 12);
CopyName(out->manufacturer, sizeof(out->manufacturer), p + 20, 40);
CopyName(out->version, sizeof(out->version), p + 60, 20);
return 1;
}
/*
* SELF_INFO: code, adv_type, tx_power, max_tx_power, pub_key[32], lat, lon,
* multi_acks, advert_loc_policy, telemetry_modes, manual_add_contacts,
+10
View File
@@ -73,6 +73,15 @@ typedef struct {
UInt8 payload[VM_MAX_FRAME];
} VmFrameDecoder;
typedef struct {
UInt8 fwVerCode; /* companion protocol version the firmware speaks */
UInt16 maxContacts;
UInt8 maxChannels;
Char buildDate[13];
Char manufacturer[41];
Char version[21];
} VmDeviceInfo;
typedef struct {
UInt8 advType;
UInt8 txPowerDbm;
@@ -123,6 +132,7 @@ UInt16 VmBuildGetContacts(UInt8 *out, UInt16 outSize, UInt32 since);
UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize);
/* Reply parsers: take a decoded payload (code byte first). Return true if it has the expected code and length. */
Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out);
Boolean VmParseSelfInfo(const UInt8 *p, UInt16 len, VmSelfInfo *out);
Boolean VmParseContactsStart(const UInt8 *p, UInt16 len, UInt32 *count);
Boolean VmParseContact(const UInt8 *p, UInt16 len, VmContact *out);
+13 -9
View File
@@ -1,16 +1,20 @@
#ifndef VM_RESOURCES_H
#define VM_RESOURCES_H
#define MainForm 1000
#define MainStatusLabel 1001
#define MainQueryButton 1002
#define MainPortsButton 1003
#define MainSerialPush 1004
#define MainUsbPush 1005
#define MainLinkGroup 1
#define MainForm 1000
#define MainNameLabel 1001
#define MainConnectButton 1002
#define MainPortsButton 1003
#define MainSerialPush 1004
#define MainUsbPush 1005
#define MainFreqLabel 1006
#define MainModeLabel 1007
#define MainPowerLabel 1008
#define MainFirmwareLabel 1009
#define MainLinkGroup 1
#define PortsForm 1100
#define PortsDoneButton 1101
#define PortsForm 1100
#define PortsDoneButton 1101
#define RomIncompatibleAlert 2000
+117
View File
@@ -0,0 +1,117 @@
#include <PalmOS.h>
#include "protocol.h"
#include "transport.h"
#include "session.h"
#define VM_APP_NAME "VisorMesh"
#define VM_REPLY_TIMEOUT (2 * SysTicksPerSecond())
#define VM_CONNECT_TRIES 3 /* over USB the host bridge needs a moment to attach */
static VmFrameDecoder gDecoder;
static Boolean gConnected = false;
static Boolean gMsgWaiting = false;
/*
* Reads frames until one with the wanted code arrives. Pushes are noted and
* skipped; RESP_ERR ends the wait. The reply is left in gDecoder.payload.
*/
static Err WaitFor(UInt8 code, Int32 timeout)
{
UInt8 rx[32];
UInt16 n, i;
UInt32 deadline = TimGetTicks() + timeout;
Err err;
while (TimGetTicks() < deadline) {
n = TransportRecv(rx, sizeof(rx), deadline - TimGetTicks(), &err);
for (i = 0; i < n; i++) {
UInt8 got;
if (!VmDecoderFeed(&gDecoder, rx[i]) || gDecoder.len == 0)
continue;
got = gDecoder.payload[0];
if (got == code)
return errNone;
if (got == VM_PUSH_MSG_WAITING)
gMsgWaiting = true;
else if (got == VM_RESP_ERR)
return vmErrRadio;
/* other pushes and stray replies are ignored */
}
}
return vmErrNoReply;
}
/* Sends a request frame and waits for the reply code, retrying sends the host isn't reading yet. */
static Err Request(const UInt8 *frame, UInt16 len, UInt8 code, UInt16 tries)
{
Err err = vmErrNoReply;
UInt16 t;
for (t = 0; t < tries; t++) {
err = TransportSend(frame, len);
if (err == serErrTimeOut)
continue;
if (err != errNone)
return err;
err = WaitFor(code, VM_REPLY_TIMEOUT);
if (err != vmErrNoReply)
return err;
}
return err == serErrTimeOut ? vmErrNoReply : err;
}
Err SessionConnect(VmDeviceInfo *device, VmSelfInfo *self)
{
UInt8 frame[VM_HEADER_LEN + 8 + VM_NAME_SIZE];
UInt16 len;
Err err;
SessionDisconnect();
err = TransportOpen();
if (err != errNone)
return err;
VmDecoderReset(&gDecoder);
gMsgWaiting = false;
/* Over USB the Visor only appears to the host after SrmOpen; give the bridge time to attach. */
if (TransportGetPort() == VM_PORT_USB)
SysTaskDelay(SysTicksPerSecond());
len = VmBuildDeviceQuery(frame, sizeof(frame));
err = Request(frame, len, VM_RESP_DEVICE_INFO, VM_CONNECT_TRIES);
if (err == errNone && !VmParseDeviceInfo(gDecoder.payload, gDecoder.len, device))
err = vmErrBadReply;
if (err != errNone)
goto fail;
len = VmBuildAppStart(frame, sizeof(frame), VM_APP_NAME);
err = Request(frame, len, VM_RESP_SELF_INFO, 1);
if (err == errNone && !VmParseSelfInfo(gDecoder.payload, gDecoder.len, self))
err = vmErrBadReply;
if (err != errNone)
goto fail;
gConnected = true;
return errNone;
fail:
TransportClose();
return err;
}
void SessionDisconnect(void)
{
TransportClose();
gConnected = false;
}
Boolean SessionIsConnected(void)
{
return gConnected;
}
Boolean SessionMessageWaiting(void)
{
return gMsgWaiting;
}
+22
View File
@@ -0,0 +1,22 @@
/*
* A connection to the companion radio: request/reply on top of the transport
* and protocol layers. Pushes that arrive while waiting for a reply are
* skipped; PUSH_MSG_WAITING is remembered for SessionMessageWaiting().
*/
#ifndef VM_SESSION_H
#define VM_SESSION_H
#include <PalmOS.h>
#include "protocol.h"
#define vmErrNoReply (appErrorClass | 1) /* radio didn't answer in time */
#define vmErrRadio (appErrorClass | 2) /* radio replied RESP_ERR */
#define vmErrBadReply (appErrorClass | 3) /* reply had the right code but didn't parse */
/* Opens the link and runs DEVICE_QUERY + APP_START. On error the link is closed. */
Err SessionConnect(VmDeviceInfo *device, VmSelfInfo *self);
void SessionDisconnect(void);
Boolean SessionIsConnected(void);
Boolean SessionMessageWaiting(void);
#endif
+16 -3
View File
@@ -81,7 +81,8 @@ static void test_decode_max_frame(void)
CHECK(frames == 1 && d.len == VM_MAX_FRAME && d.payload[VM_MAX_FRAME - 1] == (VM_MAX_FRAME - 1));
}
/* DEVICE_INFO captured from the SB-LoRa XIAO (MeshCore v1.17.1) with firmware/tools/companion_query.py. */
/* DEVICE_INFO captured from the SB-LoRa XIAO (MeshCore v1.17.1) with firmware/tools/companion_query.py.
* Generated from the capture file, not retyped (an earlier hand-typed copy had two bytes shifted). */
static void test_decode_real_device_info(void)
{
static const UInt8 in[] = {
@@ -90,7 +91,7 @@ static void test_decode_real_device_info(void)
0x65, 0x65, 0x65, 0x64, 0x20, 0x58, 0x69, 0x61, 0x6f, 0x2d, 0x6e, 0x72,
0x66, 0x35, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x31, 0x2e, 0x31, 0x37, 0x2e, 0x31,
0x00, 0x00, 0x00, 0x76, 0x31, 0x2e, 0x31, 0x37, 0x2e, 0x31, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
};
@@ -102,8 +103,20 @@ static void test_decode_real_device_info(void)
CHECK(d.payload[0] == 0x0d);
CHECK(memcmp(&d.payload[8], "14 Aug 2026", 11) == 0);
CHECK(memcmp(&d.payload[20], "Seeed Xiao-nrf52", 16) == 0);
CHECK(memcmp(&d.payload[62], "v1.17.1", 7) == 0);
CHECK(memcmp(&d.payload[60], "v1.17.1", 7) == 0);
CHECK(sizeof(in) == VM_HEADER_LEN + 0x52);
{
VmDeviceInfo info;
CHECK(VmParseDeviceInfo(d.payload, d.len, &info));
CHECK(info.fwVerCode == 0x0d);
CHECK(info.maxContacts == 350 && info.maxChannels == 40);
CHECK(strcmp(info.buildDate, "14 Aug 2026") == 0);
CHECK(strcmp(info.manufacturer, "Seeed Xiao-nrf52") == 0);
CHECK(strcmp(info.version, "v1.17.1") == 0);
CHECK(!VmParseDeviceInfo(d.payload, 79, &info));
}
}
/* Builders must produce exactly the frames companion_query.py sent to the real radio. */