diff --git a/docs/progress.md b/docs/progress.md index 0f4c6dd..a3c29c7 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -28,7 +28,8 @@ - [ ] Emulator: CloudpilotEmu with a Palm OS 3.5 ROM (the Visor isn't emulated); optionally POSE + a Visor Neo ROM in a Windows VM - [ ] Original Handspring SDK headers (Springboard `HsCard*` APIs; the PalmOne `HsExt.h` lacks them), needed for module-ROM work only - [ ] Port springSD bus routines; SC16IS740 driver -- [ ] Companion protocol framing + minimal command set +- [x] Companion protocol: framing, builders (DEVICE_QUERY, APP_START, GET_CONTACTS, SYNC_NEXT_MESSAGE), parsers (SELF_INFO, contacts, v3 contact/channel messages); host tests from real captures + MeshCore layouts +- [ ] Wire the protocol into the app: connect sequence, contact list, message sync on PUSH_MSG_WAITING - [ ] UI: contacts, DMs, channels - [x] Install method confirmed: HotSync over the USB cradle from the dev Mac (`make install`) @@ -57,3 +58,4 @@ - First on-device run: VisorMesh v0.1 installed with `make install` and launches on the Visor Neo. - Firmware builds and runs. Fixed the patch's I2C override (`-U` flags broke Wire under PlatformIO; now `build_unflags`). Pinned native GCC 14 on Apple Silicon. Enabled the USB companion link alongside the UART. The XIAO answers DEVICE_QUERY over USB, and that real frame is now a Palm host test. - UART path verified: FT232R on D6/D7 at 115200 8N1 gets the same DEVICE_INFO as USB. The radio side of SB-LoRa is proven; what remains is the carrier board (SC16IS740 + bus) and the Palm transport for it. +- Protocol layer for the read-only commands, built against MeshCore's MyMesh.cpp and captured replies (DEVICE_QUERY, APP_START → SELF_INFO, GET_CONTACTS with 0 contacts, SYNC → NO_MORE_MESSAGES). LE fields are assembled byte by byte for the big-endian 68k. The radio is on MeshCore factory defaults (869.618 MHz / 62.5 kHz / SF8, 22 dBm, no name). diff --git a/firmware/tools/companion_query.py b/firmware/tools/companion_query.py index e332ab6..02b0f85 100755 --- a/firmware/tools/companion_query.py +++ b/firmware/tools/companion_query.py @@ -6,6 +6,7 @@ Uses only the standard library (termios), so it runs anywhere with Python 3. companion_query.py /dev/cu.usbmodemXXXX # CMD_DEVICE_QUERY companion_query.py /dev/cu.usbmodemXXXX 16 03 # same, payload given as hex bytes companion_query.py /dev/cu.usbserial-XXXX --baud 115200 # via a USB-TTL adapter on D6/D7 + companion_query.py PORT 01 00 00 00 00 00 00 00 --text VisorMesh # CMD_APP_START Frames: app -> radio '<' len_lo len_hi payload; radio -> app '>' len_lo len_hi payload. """ @@ -61,11 +62,12 @@ def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument('port') ap.add_argument('payload', nargs='*', default=['16', '03'], help='payload bytes in hex (default: 16 03)') + ap.add_argument('--text', default='', help='ASCII text appended to the payload') ap.add_argument('--baud', type=int, default=115200, choices=sorted(BAUDS)) ap.add_argument('--timeout', type=float, default=2.0, help='seconds of silence before stopping') args = ap.parse_args() - payload = bytes(int(b, 16) for b in args.payload) + payload = bytes(int(b, 16) for b in args.payload) + args.text.encode('ascii') frame = b'<' + len(payload).to_bytes(2, 'little') + payload fd = open_port(args.port, args.baud) try: diff --git a/palm-client/src/main.c b/palm-client/src/main.c index 7748838..965b924 100644 --- a/palm-client/src/main.c +++ b/palm-client/src/main.c @@ -33,7 +33,6 @@ static void SetStatus(const Char *text) /* Sends CMD_DEVICE_QUERY and waits for any '>' frame in reply. */ static void QueryRadio(void) { - UInt8 payload[2]; UInt8 frame[VM_HEADER_LEN + 2]; UInt8 rx[32]; UInt16 frameLen, n, i; @@ -49,9 +48,7 @@ static void QueryRadio(void) return; } - payload[0] = VM_CMD_DEVICE_QUERY; - payload[1] = VM_APP_TARGET_VER; - frameLen = VmEncodeFrame(frame, sizeof(frame), payload, sizeof(payload)); + frameLen = VmBuildDeviceQuery(frame, sizeof(frame)); VmDecoderReset(&gDecoder); err = TransportSend(frame, frameLen); diff --git a/palm-client/src/protocol.c b/palm-client/src/protocol.c index e274c64..51dbd25 100644 --- a/palm-client/src/protocol.c +++ b/palm-client/src/protocol.c @@ -2,17 +2,41 @@ enum { ST_SYNC, ST_LEN_LO, ST_LEN_HI, ST_PAYLOAD }; -UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len) +#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 */ + +static UInt32 GetLE32(const UInt8 *p) +{ + return (UInt32)p[0] | ((UInt32)p[1] << 8) | ((UInt32)p[2] << 16) | ((UInt32)p[3] << 24); +} + +static void CopyBytes(UInt8 *dst, const UInt8 *src, UInt16 n) +{ + while (n--) + *dst++ = *src++; +} + +/* Copies up to n bytes of a possibly unterminated string, always NUL-terminating dst (size n + 1 or more). */ +static void CopyName(Char *dst, UInt16 dstSize, const UInt8 *src, UInt16 n) { UInt16 i; + if (n > dstSize - 1) + n = dstSize - 1; + for (i = 0; i < n && src[i]; i++) + dst[i] = (Char)src[i]; + dst[i] = 0; +} + +UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len) +{ if (len > VM_MAX_FRAME || outSize < (UInt16)(len + VM_HEADER_LEN)) return 0; out[0] = VM_FRAME_TO_RADIO; out[1] = (UInt8)(len & 0xFF); out[2] = (UInt8)(len >> 8); - for (i = 0; i < len; i++) - out[VM_HEADER_LEN + i] = payload[i]; + CopyBytes(out + VM_HEADER_LEN, payload, len); return (UInt16)(len + VM_HEADER_LEN); } @@ -58,3 +82,160 @@ Boolean VmDecoderFeed(VmFrameDecoder *d, UInt8 byte) VmDecoderReset(d); return 0; } + +UInt16 VmBuildDeviceQuery(UInt8 *out, UInt16 outSize) +{ + UInt8 p[2]; + + p[0] = VM_CMD_DEVICE_QUERY; + p[1] = VM_APP_TARGET_VER; + return VmEncodeFrame(out, outSize, p, 2); +} + +/* CMD_APP_START: code, 7 reserved bytes, app name (no terminator). */ +UInt16 VmBuildAppStart(UInt8 *out, UInt16 outSize, const Char *appName) +{ + UInt8 p[8 + VM_NAME_SIZE]; + UInt16 n = 0, i; + + p[0] = VM_CMD_APP_START; + for (i = 1; i < 8; i++) + p[i] = 0; + while (appName[n] && n < VM_NAME_SIZE) { + p[8 + n] = (UInt8)appName[n]; + n++; + } + return VmEncodeFrame(out, outSize, p, (UInt16)(8 + n)); +} + +/* CMD_GET_CONTACTS with optional 'since' (lastmod of the newest contact already held; 0 = all). */ +UInt16 VmBuildGetContacts(UInt8 *out, UInt16 outSize, UInt32 since) +{ + UInt8 p[5]; + + p[0] = VM_CMD_GET_CONTACTS; + if (since == 0) + return VmEncodeFrame(out, outSize, p, 1); + p[1] = (UInt8)since; + p[2] = (UInt8)(since >> 8); + p[3] = (UInt8)(since >> 16); + p[4] = (UInt8)(since >> 24); + return VmEncodeFrame(out, outSize, p, 5); +} + +UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize) +{ + UInt8 p[1]; + + p[0] = VM_CMD_SYNC_NEXT_MESSAGE; + return VmEncodeFrame(out, outSize, p, 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, + * freq (kHz), bw (Hz), sf, cr, name (rest of frame, unterminated). + */ +Boolean VmParseSelfInfo(const UInt8 *p, UInt16 len, VmSelfInfo *out) +{ + if (len < SELF_INFO_FIXED_LEN || p[0] != VM_RESP_SELF_INFO) + return 0; + out->advType = p[1]; + out->txPowerDbm = p[2]; + out->maxTxPowerDbm = p[3]; + CopyBytes(out->pubKey, p + 4, VM_PUB_KEY_SIZE); + out->latE6 = (Int32)GetLE32(p + 36); + out->lonE6 = (Int32)GetLE32(p + 40); + /* p[44..47]: multi_acks, advert_loc_policy, telemetry modes, manual_add_contacts */ + out->freqKHz = GetLE32(p + 48); + out->bwHz = GetLE32(p + 52); + out->sf = p[56]; + out->cr = p[57]; + CopyName(out->name, VM_NAME_SIZE, p + SELF_INFO_FIXED_LEN, (UInt16)(len - SELF_INFO_FIXED_LEN)); + return 1; +} + +Boolean VmParseContactsStart(const UInt8 *p, UInt16 len, UInt32 *count) +{ + if (len < 5 || p[0] != VM_RESP_CONTACTS_START) + return 0; + *count = GetLE32(p + 1); + return 1; +} + +/* + * CONTACT: code, pub_key[32], type, flags, out_path_len, out_path[64], + * name[32], last_advert, lat, lon, lastmod. + */ +Boolean VmParseContact(const UInt8 *p, UInt16 len, VmContact *out) +{ + const UInt8 *q; + + if (len < CONTACT_LEN || p[0] != VM_RESP_CONTACT) + return 0; + CopyBytes(out->pubKey, p + 1, VM_PUB_KEY_SIZE); + q = p + 1 + VM_PUB_KEY_SIZE; + out->type = q[0]; + out->flags = q[1]; + out->outPathLen = q[2]; + q += 3 + VM_MAX_PATH_SIZE; + CopyName(out->name, VM_NAME_SIZE, q, VM_NAME_SIZE); + q += VM_NAME_SIZE; + out->lastAdvert = GetLE32(q); + out->latE6 = (Int32)GetLE32(q + 4); + out->lonE6 = (Int32)GetLE32(q + 8); + out->lastMod = GetLE32(q + 12); + return 1; +} + +Boolean VmParseEndOfContacts(const UInt8 *p, UInt16 len, UInt32 *lastMod) +{ + if (len < 5 || p[0] != VM_RESP_END_OF_CONTACTS) + return 0; + *lastMod = GetLE32(p + 1); + return 1; +} + +/* + * v3 message frames (app_target_ver >= 3): + * contact: code, snr, rsv, rsv, sender_prefix[6], path_len, txt_type, timestamp, [sig 4 if signed], text + * channel: code, snr, rsv, rsv, channel_idx, path_len, txt_type, timestamp, text + */ +Boolean VmParseMessage(const UInt8 *p, UInt16 len, VmMessage *out) +{ + UInt16 i; + + if (len < MSG_V3_HEADER_LEN) + return 0; + out->snrQuarterDb = (Int8)p[1]; + i = MSG_V3_HEADER_LEN; + + if (p[0] == VM_RESP_CONTACT_MSG_V3) { + if (len < i + 6 + 6) + return 0; + out->isChannel = 0; + out->channelIdx = 0; + CopyBytes(out->senderPrefix, p + i, 6); + i += 6; + } else if (p[0] == VM_RESP_CHANNEL_MSG_V3) { + if (len < i + 1 + 6) + return 0; + out->isChannel = 1; + out->channelIdx = p[i++]; + } else { + return 0; + } + + out->pathLen = p[i++]; + out->txtType = p[i++]; + out->timestamp = GetLE32(p + i); + i += 4; + if (!out->isChannel && out->txtType == VM_TXT_TYPE_SIGNED_PLAIN) { + if (len < i + 4) + return 0; + i += 4; /* signer's key prefix */ + } + out->text = p + i; + out->textLen = (UInt16)(len - i); + return 1; +} diff --git a/palm-client/src/protocol.h b/palm-client/src/protocol.h index 8dede96..a85e864 100644 --- a/palm-client/src/protocol.h +++ b/palm-client/src/protocol.h @@ -1,9 +1,14 @@ /* - * MeshCore companion protocol framing. + * MeshCore companion protocol: framing, command builders and reply parsers. * * App -> radio: '<' len_lo len_hi payload[len] * Radio -> app: '>' len_lo len_hi payload[len] * + * Layouts follow examples/companion_radio/MyMesh.cpp in MeshCore (the commit + * pinned in firmware/Makefile). Multi-byte fields are little-endian; the + * Visor's 68k is big-endian, so fields are assembled byte by byte, never + * copied with memcpy. + * * Plain C with no Palm OS calls, so it also builds on the host for tests. */ #ifndef VM_PROTOCOL_H @@ -15,8 +20,12 @@ #include typedef uint8_t UInt8; typedef uint16_t UInt16; +typedef uint32_t UInt32; +typedef int8_t Int8; typedef int16_t Int16; +typedef int32_t Int32; typedef uint8_t Boolean; +typedef char Char; #endif #define VM_FRAME_TO_RADIO 0x3C /* '<' */ @@ -24,9 +33,38 @@ typedef uint8_t Boolean; #define VM_MAX_FRAME 172 /* MeshCore MAX_FRAME_SIZE (payload bytes) */ #define VM_HEADER_LEN 3 -/* Commands (app -> radio). Add others as they're verified against MeshCore. */ -#define VM_CMD_DEVICE_QUERY 0x16 -#define VM_APP_TARGET_VER 0x03 +#define VM_APP_TARGET_VER 0x03 /* we understand the v3 message frames */ +#define VM_PUB_KEY_SIZE 32 +#define VM_NAME_SIZE 32 /* contact names: 31 chars + NUL */ +#define VM_MAX_PATH_SIZE 64 + +/* Commands (app -> radio) */ +#define VM_CMD_APP_START 1 +#define VM_CMD_GET_CONTACTS 4 +#define VM_CMD_SYNC_NEXT_MESSAGE 10 +#define VM_CMD_DEVICE_QUERY 22 + +/* Replies (radio -> app) */ +#define VM_RESP_OK 0 +#define VM_RESP_ERR 1 +#define VM_RESP_CONTACTS_START 2 +#define VM_RESP_CONTACT 3 +#define VM_RESP_END_OF_CONTACTS 4 +#define VM_RESP_SELF_INFO 5 +#define VM_RESP_NO_MORE_MESSAGES 10 +#define VM_RESP_DEVICE_INFO 13 +#define VM_RESP_CONTACT_MSG_V3 16 +#define VM_RESP_CHANNEL_MSG_V3 17 + +/* Pushes (radio -> app, unsolicited) */ +#define VM_PUSH_ADVERT 0x80 +#define VM_PUSH_SEND_CONFIRMED 0x82 +#define VM_PUSH_MSG_WAITING 0x83 +#define VM_IS_PUSH(code) ((code) >= 0x80) + +#define VM_TXT_TYPE_PLAIN 0 +#define VM_TXT_TYPE_CLI_DATA 1 +#define VM_TXT_TYPE_SIGNED_PLAIN 2 typedef struct { UInt8 state; @@ -35,16 +73,60 @@ typedef struct { UInt8 payload[VM_MAX_FRAME]; } VmFrameDecoder; -/* Writes a '<' frame into out. Returns total bytes written, or 0 if it won't fit. */ -UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len); +typedef struct { + UInt8 advType; + UInt8 txPowerDbm; + UInt8 maxTxPowerDbm; + UInt8 pubKey[VM_PUB_KEY_SIZE]; + Int32 latE6; /* degrees * 1e6 */ + Int32 lonE6; + UInt32 freqKHz; + UInt32 bwHz; + UInt8 sf; + UInt8 cr; + Char name[VM_NAME_SIZE]; +} VmSelfInfo; -void VmDecoderReset(VmFrameDecoder *d); +typedef struct { + UInt8 pubKey[VM_PUB_KEY_SIZE]; + UInt8 type; + UInt8 flags; + UInt8 outPathLen; /* 0xFF = no known path (flood) */ + Char name[VM_NAME_SIZE]; + UInt32 lastAdvert; + Int32 latE6; + Int32 lonE6; + UInt32 lastMod; +} VmContact; -/* - * Feeds one byte from the radio. Returns 1 when a complete '>' frame is in - * d->payload (length d->len); the next call starts a new frame. Bytes outside - * a frame and frames longer than VM_MAX_FRAME are dropped. - */ +typedef struct { + Boolean isChannel; + Int8 snrQuarterDb; /* SNR * 4 */ + UInt8 channelIdx; /* channel messages */ + UInt8 senderPrefix[6];/* contact messages: first 6 bytes of the sender's key */ + UInt8 pathLen; /* 0xFF = direct */ + UInt8 txtType; + UInt32 timestamp; /* sender's clock, seconds since 1970 */ + const UInt8 *text; /* points into the payload; not NUL-terminated */ + UInt16 textLen; +} VmMessage; + +/* Framing */ +UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len); +void VmDecoderReset(VmFrameDecoder *d); Boolean VmDecoderFeed(VmFrameDecoder *d, UInt8 byte); +/* Command builders: each writes a complete '<' frame and returns its length, or 0 if it won't fit. */ +UInt16 VmBuildDeviceQuery(UInt8 *out, UInt16 outSize); +UInt16 VmBuildAppStart(UInt8 *out, UInt16 outSize, const Char *appName); +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 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); +Boolean VmParseEndOfContacts(const UInt8 *p, UInt16 len, UInt32 *lastMod); +Boolean VmParseMessage(const UInt8 *p, UInt16 len, VmMessage *out); + #endif diff --git a/palm-client/tests/fixtures.h b/palm-client/tests/fixtures.h new file mode 100644 index 0000000..9029597 --- /dev/null +++ b/palm-client/tests/fixtures.h @@ -0,0 +1,79 @@ +/* + * Companion-protocol frames for host tests (complete frames, '>' header included). + * Generated once; see the comment on each for where it came from. + */ + +/* SELF_INFO captured from the SB-LoRa XIAO (MeshCore v1.17.1, factory settings) over + * the D6/D7 UART. Public key replaced with 0x10..0x2f and name with "TestNode" (same length). */ +static const UInt8 kSelfInfo[] = { + 0x3e, 0x42, 0x00, 0x05, 0x01, 0x16, 0x16, 0x10, 0x11, 0x12, 0x13, 0x14, + 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, + 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, + 0x2d, 0x2e, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xf2, 0x44, 0x0d, 0x00, 0x24, 0xf4, 0x00, 0x00, 0x08, + 0x05, 0x54, 0x65, 0x73, 0x74, 0x4e, 0x6f, 0x64, 0x65, +}; + +/* SYNTHETIC: CONTACT laid out per MyMesh::writeContactRespFrame. type 2 (repeater), + * 2-hop path, lat 40.712345, lon -74.006789, last_advert 1793000000, lastmod 1793000123. */ +static const UInt8 kContact[] = { + 0x3e, 0x94, 0x00, 0x03, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, + 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0x02, 0x00, 0x02, 0x5a, 0x3c, 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, 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, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x69, 0x6c, 0x6c, 0x74, + 0x6f, 0x70, 0x20, 0x52, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x72, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x40, 0x02, 0xdf, 0x6a, 0x99, 0x38, 0x6d, 0x02, 0xfb, + 0xbe, 0x96, 0xfb, 0xbb, 0x02, 0xdf, 0x6a, +}; + +/* SYNTHETIC: CONTACTS_START, count 1. */ +static const UInt8 kContactsStart[] = { + 0x3e, 0x05, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, +}; + +/* SYNTHETIC: END_OF_CONTACTS, most recent lastmod 1793000123. */ +static const UInt8 kEndOfContacts[] = { + 0x3e, 0x05, 0x00, 0x04, 0xbb, 0x02, 0xdf, 0x6a, +}; + +/* SYNTHETIC: CONTACT_MSG_RECV_V3 per MyMesh::queueMessage. SNR -2.5 dB (0xf6 = -10/4), + * direct (path 0xff), plain text, timestamp 1793001000, "hello visor". */ +static const UInt8 kContactMsg[] = { + 0x3e, 0x1b, 0x00, 0x10, 0xf6, 0x00, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, + 0xa5, 0xff, 0x00, 0x28, 0x06, 0xdf, 0x6a, 0x68, 0x65, 0x6c, 0x6c, 0x6f, + 0x20, 0x76, 0x69, 0x73, 0x6f, 0x72, +}; + +/* SYNTHETIC: CONTACT_MSG_RECV_V3, signed plain (txt_type 2) with the 4-byte signer prefix + * before the text. SNR +5 dB, 1 hop, "signed hi". */ +static const UInt8 kSignedMsg[] = { + 0x3e, 0x1d, 0x00, 0x10, 0x14, 0x00, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, + 0xa5, 0x01, 0x02, 0x29, 0x06, 0xdf, 0x6a, 0xa0, 0xa1, 0xa2, 0xa3, 0x73, + 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x68, 0x69, +}; + +/* SYNTHETIC: CHANNEL_MSG_RECV_V3 per MyMesh::onChannelMessageRecv. Channel 0, 3 hops, + * SNR +3 dB, "Alice: hi all" (MeshCore puts the sender name in the text). */ +static const UInt8 kChannelMsg[] = { + 0x3e, 0x18, 0x00, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x2a, 0x06, + 0xdf, 0x6a, 0x41, 0x6c, 0x69, 0x63, 0x65, 0x3a, 0x20, 0x68, 0x69, 0x20, + 0x61, 0x6c, 0x6c, +}; + +/* NO_MORE_MESSAGES captured from the XIAO. */ +static const UInt8 kNoMoreMessages[] = { + 0x3e, 0x01, 0x00, 0x0a, +}; + +/* GET_CONTACTS reply captured from the XIAO with no contacts: CONTACTS_START (0), END_OF_CONTACTS (0). */ +static const UInt8 kContactsEmpty[] = { + 0x3e, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x05, 0x00, 0x04, + 0x00, 0x00, 0x00, 0x00, +}; + diff --git a/palm-client/tests/test_protocol.c b/palm-client/tests/test_protocol.c index 71e5cf4..d4345cc 100644 --- a/palm-client/tests/test_protocol.c +++ b/palm-client/tests/test_protocol.c @@ -1,7 +1,8 @@ -/* Host-side tests for the companion framing. Run with `make test`. */ +/* Host-side tests for the companion protocol. Run with `make test`. */ #include #include #include "../src/protocol.h" +#include "fixtures.h" static int failures; @@ -17,6 +18,13 @@ static int feed(VmFrameDecoder *d, const UInt8 *bytes, int n) return frames; } +/* Decodes a single complete frame into d; fails the test if it isn't exactly one frame. */ +static void decode_one(VmFrameDecoder *d, const UInt8 *bytes, int n) +{ + VmDecoderReset(d); + CHECK(feed(d, bytes, n) == 1); +} + static void test_encode_device_query(void) { UInt8 payload[] = { VM_CMD_DEVICE_QUERY, VM_APP_TARGET_VER }; @@ -98,6 +106,126 @@ static void test_decode_real_device_info(void) CHECK(sizeof(in) == VM_HEADER_LEN + 0x52); } +/* Builders must produce exactly the frames companion_query.py sent to the real radio. */ +static void test_builders_match_bench_frames(void) +{ + UInt8 out[64]; + static const UInt8 query[] = { 0x3c, 0x02, 0x00, 0x16, 0x03 }; + static const UInt8 appStart[] = { + 0x3c, 0x11, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 'V', 'i', 's', 'o', 'r', 'M', 'e', 's', 'h' + }; + static const UInt8 contacts[] = { 0x3c, 0x01, 0x00, 0x04 }; + static const UInt8 contactsSince[] = { 0x3c, 0x05, 0x00, 0x04, 0x78, 0x56, 0x34, 0x12 }; + static const UInt8 sync[] = { 0x3c, 0x01, 0x00, 0x0a }; + + CHECK(VmBuildDeviceQuery(out, sizeof(out)) == sizeof(query)); + CHECK(memcmp(out, query, sizeof(query)) == 0); + CHECK(VmBuildAppStart(out, sizeof(out), "VisorMesh") == sizeof(appStart)); + CHECK(memcmp(out, appStart, sizeof(appStart)) == 0); + CHECK(VmBuildGetContacts(out, sizeof(out), 0) == sizeof(contacts)); + CHECK(memcmp(out, contacts, sizeof(contacts)) == 0); + CHECK(VmBuildGetContacts(out, sizeof(out), 0x12345678UL) == sizeof(contactsSince)); + CHECK(memcmp(out, contactsSince, sizeof(contactsSince)) == 0); + CHECK(VmBuildSyncNextMessage(out, sizeof(out)) == sizeof(sync)); + CHECK(memcmp(out, sync, sizeof(sync)) == 0); + CHECK(VmBuildAppStart(out, 10, "VisorMesh") == 0); +} + +static void test_parse_self_info(void) +{ + VmFrameDecoder d; + VmSelfInfo s; + + decode_one(&d, kSelfInfo, sizeof(kSelfInfo)); + CHECK(VmParseSelfInfo(d.payload, d.len, &s)); + CHECK(s.advType == 1); + CHECK(s.txPowerDbm == 22 && s.maxTxPowerDbm == 22); + CHECK(s.pubKey[0] == 0x10 && s.pubKey[31] == 0x2f); + CHECK(s.latE6 == 0 && s.lonE6 == 0); + CHECK(s.freqKHz == 869618UL); + CHECK(s.bwHz == 62500UL); + CHECK(s.sf == 8 && s.cr == 5); + CHECK(strcmp(s.name, "TestNode") == 0); + CHECK(!VmParseSelfInfo(d.payload, 57, &s)); + CHECK(!VmParseContact(d.payload, d.len, (VmContact *)0)); +} + +static void test_parse_contacts(void) +{ + VmFrameDecoder d; + VmContact c; + UInt32 n; + int frames = 0, i; + + decode_one(&d, kContactsStart, sizeof(kContactsStart)); + CHECK(VmParseContactsStart(d.payload, d.len, &n) && n == 1); + + decode_one(&d, kContact, sizeof(kContact)); + CHECK(d.len == 148); + CHECK(VmParseContact(d.payload, d.len, &c)); + CHECK(c.pubKey[0] == 0xa0 && c.pubKey[31] == 0xbf); + CHECK(c.type == 2 && c.flags == 0 && c.outPathLen == 2); + CHECK(strcmp(c.name, "Hilltop Repeater") == 0); + CHECK(c.lastAdvert == 1793000000UL); + CHECK(c.latE6 == 40712345L && c.lonE6 == -74006789L); + CHECK(c.lastMod == 1793000123UL); + CHECK(!VmParseContact(d.payload, 147, &c)); + + decode_one(&d, kEndOfContacts, sizeof(kEndOfContacts)); + CHECK(VmParseEndOfContacts(d.payload, d.len, &n) && n == 1793000123UL); + + /* Real empty-list reply: two back-to-back frames. */ + VmDecoderReset(&d); + for (i = 0; i < (int)sizeof(kContactsEmpty); i++) { + if (!VmDecoderFeed(&d, kContactsEmpty[i])) + continue; + frames++; + if (frames == 1) + CHECK(VmParseContactsStart(d.payload, d.len, &n) && n == 0); + else + CHECK(VmParseEndOfContacts(d.payload, d.len, &n) && n == 0); + } + CHECK(frames == 2); +} + +static void test_parse_messages(void) +{ + VmFrameDecoder d; + VmMessage m; + static const UInt8 sender[6] = { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 }; + + decode_one(&d, kContactMsg, sizeof(kContactMsg)); + CHECK(VmParseMessage(d.payload, d.len, &m)); + CHECK(!m.isChannel); + CHECK(m.snrQuarterDb == -10); + CHECK(memcmp(m.senderPrefix, sender, 6) == 0); + CHECK(m.pathLen == 0xff && m.txtType == VM_TXT_TYPE_PLAIN); + CHECK(m.timestamp == 1793001000UL); + CHECK(m.textLen == 11 && memcmp(m.text, "hello visor", 11) == 0); + + decode_one(&d, kSignedMsg, sizeof(kSignedMsg)); + CHECK(VmParseMessage(d.payload, d.len, &m)); + CHECK(m.txtType == VM_TXT_TYPE_SIGNED_PLAIN && m.snrQuarterDb == 20); + CHECK(m.textLen == 9 && memcmp(m.text, "signed hi", 9) == 0); + + decode_one(&d, kChannelMsg, sizeof(kChannelMsg)); + CHECK(VmParseMessage(d.payload, d.len, &m)); + CHECK(m.isChannel && m.channelIdx == 0 && m.pathLen == 3); + CHECK(m.timestamp == 1793001002UL); + CHECK(m.textLen == 13 && memcmp(m.text, "Alice: hi all", 13) == 0); + + decode_one(&d, kNoMoreMessages, sizeof(kNoMoreMessages)); + CHECK(d.len == 1 && d.payload[0] == VM_RESP_NO_MORE_MESSAGES); + CHECK(!VmParseMessage(d.payload, d.len, &m)); + + /* Truncated frames are rejected, not over-read. */ + decode_one(&d, kChannelMsg, sizeof(kChannelMsg)); + CHECK(!VmParseMessage(d.payload, 10, &m)); + decode_one(&d, kSignedMsg, sizeof(kSignedMsg)); + CHECK(!VmParseMessage(d.payload, 17, &m)); +} + int main(void) { test_encode_device_query(); @@ -106,6 +234,10 @@ int main(void) test_decode_oversize_resyncs(); test_decode_max_frame(); test_decode_real_device_info(); + test_builders_match_bench_frames(); + test_parse_self_info(); + test_parse_contacts(); + test_parse_messages(); if (failures) { printf("%d failure(s)\n", failures); return 1;