palm-client: a fifth code segment, and Node, Settings, Font Preview and Add Channel move from vmcode2 (31.1 KB) to vmcode4, leaving vmcode2 room for the hooks the next features need. protocol2.c (vmcode5, host-tested) has the newer companion commands: trace path, stats, battery, a contact's route, plus meshcore:// share links and #hashtags in text

This commit is contained in:
rich committed 2026-11-05 14:43:50 -05:00
1 parent ee6e4d7a7b
commit 50a5a486db
16 files changed
+759 -51

No files matched your search

+2 -2
View File
@@ -40,12 +40,12 @@ make map # Map basemap PDB (tools/make_map.py); install-map is the user's,
The app is C for GCC 2.95 (prc-tools-remix) and PilRC, against the Palm OS 3.5 SDK, on a 68k with 16 grays at 160×160.
**Code segments.** The code is more than one 32 KB code resource, so modules live in `vmcode2`–`vmcode4`. Read `src/sections.h` before adding code; the rules are there. In short:
**Code segments.** The code is more than one 32 KB code resource, so modules live in `vmcode2`–`vmcode5`. Read `src/sections.h` before adding code; the rules are there. In short:
- Put `VM_SECT2/3/4` on a module's header prototypes, and on its static functions' forward declarations at the top of the file. The function body sits under its declaration.
- Never share an `extern const` table across files. The other file addresses it as data, but it's in code. Shared tables must not be const.
- A `static const` table inside a function in an extra segment reads as garbage on the device. File-level static const is fine. Otherwise compute the value or use a resource.
- `vmcode2` is nearly full (29.3 KB as of 2026-11-03). Put new code in `vmcode3` or `vmcode4`.
- `vmcode2` holds the core screens (23.6 KB as of 2026-11-05, after Node, Settings, Font Preview and Add Channel moved to `vmcode4`). Put new code in `vmcode5` (or `vmcode3`), and keep `vmcode2` for small hooks.
**Launches without globals.** Alarm, display-alarm, reset and sync-notify launch codes run without globals (no A5), and the extra segments aren't loaded. Code on those paths must be in segment 1 and touch no globals and no `VM_SECT*` functions. Check the disassembly when in doubt. A pointer table needs relocations: use a `switch`. `src/bgcheck.c` shows the pattern: it sublaunches the app with `sysAppLaunchFlagNewGlobals` for the real work.
+3 -1
View File
@@ -11,7 +11,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
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
OBJS = $(SRCS:src/%.c=build/%.o)
CC = m68k-palmos-gcc
@@ -83,6 +83,8 @@ test: | build
build/test_map
gcc -std=c99 -Wall -Wextra -o build/test_reactions tests/test_reactions.c src/reaction_fmt.c src/rxpath.c src/sha256.c
build/test_reactions
gcc -std=c99 -Wall -Wextra -o build/test_protocol2 tests/test_protocol2.c src/protocol2.c src/protocol.c
build/test_protocol2
build:
mkdir -p build
+7 -7
View File
@@ -14,13 +14,13 @@
#include "ui/text.h"
#include "channel_form.h"
/* In the second code segment: see sections.h for the rules. */
static void *GetObject(FormType *form, UInt16 id) VM_SECT2;
static const Char *FieldText(FormType *form, UInt16 id) VM_SECT2;
static Boolean IsPrivate(FormType *form) VM_SECT2;
static void ShowKeyField(FormType *form) VM_SECT2;
static void Error(const Char *msg) VM_SECT2;
static Boolean Add(FormType *form) VM_SECT2;
/* In the fourth code segment: see sections.h for the rules. */
static void *GetObject(FormType *form, UInt16 id) VM_SECT4;
static const Char *FieldText(FormType *form, UInt16 id) VM_SECT4;
static Boolean IsPrivate(FormType *form) VM_SECT4;
static void ShowKeyField(FormType *form) VM_SECT4;
static void Error(const Char *msg) VM_SECT4;
static Boolean Add(FormType *form) VM_SECT4;
static void *GetObject(FormType *form, UInt16 id)
{
+4 -4
View File
@@ -2,12 +2,12 @@
#define VM_CHANNEL_FORM_H
#include <PalmOS.h>
#include "sections.h" /* this module is in the second code segment */
#include "sections.h" /* this module is in the fourth code segment */
Boolean ChannelFormHandleEvent(EventType *event) VM_SECT2;
Boolean ChannelFormHandleEvent(EventType *event) VM_SECT4;
/* Options > Leave Channel: asks, then empties the open channel's slot. */
void ChannelLeaveCurrent(void) VM_SECT2;
void ChannelLeaveCurrent(void) VM_SECT4;
/* The same for any channel (Home's tap-and-hold); goes back to Home. */
void ChannelLeave(UInt8 slot) VM_SECT2;
void ChannelLeave(UInt8 slot) VM_SECT4;
#endif
+9 -9
View File
@@ -11,15 +11,15 @@
#include "node_form.h"
#include "ui/draw.h"
/* In the second code segment: see sections.h for the rules. */
static void SetLabel(FormType *form, UInt16 id, const Char *text) VM_SECT2;
static void FormatFreq(Char *out, UInt32 kHz) VM_SECT2;
static void FormatBandwidth(Char *out, UInt32 hz) VM_SECT2;
static void ShowRadioInfo(FormType *form) VM_SECT2;
static void DrawClockLine(void) VM_SECT2;
static void ShowStatus(const Char *text) VM_SECT2;
static void ConnectOrDisconnect(FormType *form) VM_SECT2;
static void SendAdvert(void) VM_SECT2;
/* In the fourth code segment: see sections.h for the rules. */
static void SetLabel(FormType *form, UInt16 id, const Char *text) VM_SECT4;
static void FormatFreq(Char *out, UInt32 kHz) VM_SECT4;
static void FormatBandwidth(Char *out, UInt32 hz) VM_SECT4;
static void ShowRadioInfo(FormType *form) VM_SECT4;
static void DrawClockLine(void) VM_SECT4;
static void ShowStatus(const Char *text) VM_SECT4;
static void ConnectOrDisconnect(FormType *form) VM_SECT4;
static void SendAdvert(void) VM_SECT4;
#define VM_LABEL_CHARS 22 /* the info labels in the .rcp are padded to this width */
+2 -2
View File
@@ -2,8 +2,8 @@
#define VM_NODE_FORM_H
#include <PalmOS.h>
#include "sections.h" /* this module is in the second code segment */
#include "sections.h" /* this module is in the fourth code segment */
Boolean NodeFormHandleEvent(EventType *event) VM_SECT2;
Boolean NodeFormHandleEvent(EventType *event) VM_SECT4;
#endif
+3 -3
View File
@@ -7,9 +7,9 @@
#include "ui/draw.h"
#include "preview_form.h"
/* In the second code segment: see sections.h for the rules. */
static void Row(Coord y, Char mark, const Char *name, const Char *badge, const Char *age, const Char *preview) VM_SECT2;
static void DrawPreview(void) VM_SECT2;
/* In the fourth code segment: see sections.h for the rules. */
static void Row(Coord y, Char mark, const Char *name, const Char *badge, const Char *age, const Char *preview) VM_SECT4;
static void DrawPreview(void) VM_SECT4;
#define TOP 16
+2 -2
View File
@@ -2,8 +2,8 @@
#define VM_PREVIEW_FORM_H
#include <PalmOS.h>
#include "sections.h" /* this module is in the second code segment */
#include "sections.h" /* this module is in the fourth code segment */
Boolean FontPreviewFormHandleEvent(EventType *event) VM_SECT2;
Boolean FontPreviewFormHandleEvent(EventType *event) VM_SECT4;
#endif
+434
View File
@@ -0,0 +1,434 @@
#include "protocol2.h"
static UInt16 Get16(const UInt8 *p) VM_SECT5;
static UInt32 Get32(const UInt8 *p) VM_SECT5;
static void Put32(UInt8 *p, UInt32 v) VM_SECT5;
static Char HexDigit(UInt8 v) VM_SECT5;
static Char HexUpper(UInt8 v) VM_SECT5;
static Int16 HexValue(Char c) VM_SECT5;
static Boolean Unreserved(UInt8 c) VM_SECT5;
static UInt16 PutStr(Char *out, UInt16 size, UInt16 at, const Char *s) VM_SECT5;
static UInt16 PutEscaped(Char *out, UInt16 size, UInt16 at, const UInt8 *s, UInt16 n) VM_SECT5;
static UInt16 PutHex(Char *out, UInt16 size, UInt16 at, const UInt8 *b, UInt16 n) VM_SECT5;
static Boolean Prefix(const Char *s, UInt16 len, const Char *p) VM_SECT5;
static UInt16 Unescape(const Char *s, UInt16 len, UInt8 *out, UInt16 max) VM_SECT5;
static Boolean HexBytes(const Char *s, UInt16 len, UInt8 *out, UInt16 n) VM_SECT5;
static Boolean TagChar(Char c) VM_SECT5;
static UInt16 Get16(const UInt8 *p)
{
return (UInt16)(p[0] | ((UInt16)p[1] << 8));
}
static UInt32 Get32(const UInt8 *p)
{
return (UInt32)p[0] | ((UInt32)p[1] << 8) | ((UInt32)p[2] << 16) | ((UInt32)p[3] << 24);
}
static void Put32(UInt8 *p, UInt32 v)
{
p[0] = (UInt8)v;
p[1] = (UInt8)(v >> 8);
p[2] = (UInt8)(v >> 16);
p[3] = (UInt8)(v >> 24);
}
/* Computed, not a table: a function-level const table reads as garbage in an extra segment. */
static Char HexDigit(UInt8 v)
{
return (Char)(v < 10 ? '0' + v : 'a' + v - 10);
}
static Char HexUpper(UInt8 v)
{
return (Char)(v < 10 ? '0' + v : 'A' + v - 10);
}
static Int16 HexValue(Char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
UInt16 VmBuildCode(UInt8 *out, UInt16 outSize, UInt8 code)
{
return VmEncodeFrame(out, outSize, &code, 1);
}
UInt16 VmBuildGetStats(UInt8 *out, UInt16 outSize, UInt8 type)
{
UInt8 p[2];
p[0] = VM_CMD_GET_STATS;
p[1] = type;
return VmEncodeFrame(out, outSize, p, 2);
}
UInt16 VmBuildGetContactByKey(UInt8 *out, UInt16 outSize, const UInt8 *pubKey)
{
UInt8 p[1 + VM_PUB_KEY_SIZE];
UInt16 i;
p[0] = VM_CMD_GET_CONTACT_BY_KEY;
for (i = 0; i < VM_PUB_KEY_SIZE; i++)
p[1 + i] = pubKey[i];
return VmEncodeFrame(out, outSize, p, sizeof(p));
}
/* code, tag, auth (unused: 0), flags (low 2 bits: log2 of the hash size), path */
UInt16 VmBuildTrace(UInt8 *out, UInt16 outSize, UInt32 tag, UInt8 hashSize, const UInt8 *path, UInt16 pathBytes)
{
UInt8 p[10 + VM_TRACE_MAX];
UInt16 i;
if (pathBytes == 0 || pathBytes > VM_TRACE_MAX || (hashSize != 1 && hashSize != 2 && hashSize != 4) ||
pathBytes % hashSize != 0)
return 0;
p[0] = VM_CMD_SEND_TRACE_PATH;
Put32(p + 1, tag);
Put32(p + 5, 0);
p[9] = hashSize == 1 ? 0 : hashSize == 2 ? 1 : 2;
for (i = 0; i < pathBytes; i++)
p[10 + i] = path[i];
return VmEncodeFrame(out, outSize, p, 10 + pathBytes);
}
/* code, reserved, path bytes, flags, tag, auth, hashes, an SNR per hop, then ours */
Boolean VmParseTrace(const UInt8 *p, UInt16 len, VmTrace *out)
{
UInt16 bytes, hops, i;
if (len < 12 || p[0] != VM_PUSH_TRACE_DATA)
return 0;
bytes = p[2];
out->hashSize = (UInt8)(1 << (p[3] & 3));
hops = bytes / out->hashSize;
if (bytes > sizeof(out->hashes) || bytes % out->hashSize || len < 12 + bytes + hops + 1)
return 0;
out->tag = Get32(p + 4);
out->hops = (UInt8)hops;
for (i = 0; i < bytes; i++)
out->hashes[i] = p[12 + i];
for (i = 0; i <= hops; i++)
out->snrQ[i] = (Int8)p[12 + bytes + i];
return 1;
}
/* code, millivolts, then (newer firmware) storage used and total in KB */
Boolean VmParseBattery(const UInt8 *p, UInt16 len, VmBattery *out)
{
if (len < 3 || p[0] != VM_RESP_BATT_AND_STORAGE)
return 0;
out->battMv = Get16(p + 1);
out->hasStorage = len >= 11;
out->usedKb = out->hasStorage ? Get32(p + 3) : 0;
out->totalKb = out->hasStorage ? Get32(p + 7) : 0;
return 1;
}
Boolean VmParseStats(const UInt8 *p, UInt16 len, VmStats *out)
{
if (len < 2 || p[0] != VM_RESP_STATS)
return 0;
switch (p[1]) {
case VM_STATS_CORE: /* millivolts, uptime, error flags, queue length */
if (len < 11)
return 0;
out->battMv = Get16(p + 2);
out->uptimeSecs = Get32(p + 4);
out->errFlags = Get16(p + 8);
out->queueLen = p[10];
out->hasCore = 1;
return 1;
case VM_STATS_RADIO: /* noise floor, last RSSI, last SNR x 4, TX and RX airtime */
if (len < 14)
return 0;
out->noiseFloor = (Int16)Get16(p + 2);
out->lastRssi = (Int8)p[4];
out->lastSnrQ = (Int8)p[5];
out->txAirSecs = Get32(p + 6);
out->rxAirSecs = Get32(p + 10);
out->hasRadio = 1;
return 1;
case VM_STATS_PACKETS: /* received, sent, sent flood/direct, received flood/direct */
if (len < 26)
return 0;
out->recv = Get32(p + 2);
out->sent = Get32(p + 6);
out->sentFlood = Get32(p + 10);
out->sentDirect = Get32(p + 14);
out->recvFlood = Get32(p + 18);
out->recvDirect = Get32(p + 22);
out->hasPackets = 1;
return 1;
}
return 0;
}
Boolean VmContactPath(const UInt8 *p, UInt16 len, UInt8 *pathLen, UInt8 *path)
{
UInt16 i;
if (len < VM_CONTACT_FRAME_LEN || p[0] != VM_RESP_CONTACT)
return 0;
*pathLen = p[35];
for (i = 0; i < VM_MAX_PATH_SIZE; i++)
path[i] = p[36 + i];
return 1;
}
/*
* Sent without the last-modified field, so the radio stamps the change with
* its clock and the next contact sync picks it up.
*/
UInt16 VmBuildContactRoute(UInt8 *out, UInt16 outSize, const UInt8 *contact, UInt16 len,
UInt8 hashSize, const UInt8 *path, UInt8 hops)
{
UInt8 p[VM_CONTACT_FRAME_LEN - 4];
UInt16 i, bytes = (UInt16)hops * hashSize;
if (len < VM_CONTACT_FRAME_LEN || contact[0] != VM_RESP_CONTACT || hashSize < 1 || hashSize > 3 ||
hops > 63 || bytes > VM_MAX_PATH_SIZE)
return 0;
for (i = 0; i < sizeof(p); i++)
p[i] = contact[i];
p[0] = VM_CMD_ADD_UPDATE_CONTACT;
p[35] = (UInt8)(((hashSize - 1) << 6) | hops);
for (i = 0; i < VM_MAX_PATH_SIZE; i++)
p[36 + i] = i < bytes ? path[i] : 0;
return VmEncodeFrame(out, outSize, p, sizeof(p));
}
Int16 VmParseHopCodes(const Char *s, UInt8 *out, UInt16 max, UInt8 *size)
{
UInt16 i = 0, start, n, hops = 0, b;
*size = 0;
for (;;) {
while (s[i] == ' ' || s[i] == ',' || s[i] == '\n')
i++;
if (!s[i])
return (Int16)hops;
for (start = i; HexValue(s[i]) >= 0; i++)
;
n = i - start;
if (n == 0 || (s[i] && s[i] != ' ' && s[i] != ',' && s[i] != '\n'))
return -1;
if (!*size) {
if (n != 2 && n != 4 && n != 8)
return -1;
*size = (UInt8)(n / 2);
}
if (n != *size * 2U || (hops + 1U) * *size > max)
return -1;
for (b = 0; b < *size; b++)
out[hops * *size + b] = (UInt8)(HexValue(s[start + 2 * b]) << 4 | HexValue(s[start + 2 * b + 1]));
hops++;
}
}
/* URL characters that need no %-escape (RFC 3986) */
static Boolean Unreserved(UInt8 c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~';
}
/* The Put* helpers append at 'at' and return the new length, or size if it ran out (the caller checks). */
static UInt16 PutStr(Char *out, UInt16 size, UInt16 at, const Char *s)
{
while (*s && at < size)
out[at++] = *s++;
return *s ? size : at;
}
static UInt16 PutEscaped(Char *out, UInt16 size, UInt16 at, const UInt8 *s, UInt16 n)
{
UInt16 i;
for (i = 0; i < n && at < size; i++) {
if (Unreserved(s[i])) {
out[at++] = (Char)s[i];
} else if (at + 3 <= size) {
out[at++] = '%';
out[at++] = HexUpper(s[i] >> 4);
out[at++] = HexUpper(s[i] & 15);
} else {
return size;
}
}
return i < n ? size : at;
}
static UInt16 PutHex(Char *out, UInt16 size, UInt16 at, const UInt8 *b, UInt16 n)
{
UInt16 i;
if (at + 2 * n > size)
return size;
for (i = 0; i < n; i++) {
out[at++] = HexDigit(b[i] >> 4);
out[at++] = HexDigit(b[i] & 15);
}
return at;
}
UInt16 VmShareContact(Char *out, UInt16 size, const UInt8 *name, UInt16 nameLen, const UInt8 *key, UInt8 type)
{
UInt16 at;
at = PutStr(out, size, 0, "meshcore://contact/add?name=");
at = PutEscaped(out, size, at, name, nameLen);
at = PutStr(out, size, at, "&public_key=");
at = PutHex(out, size, at, key, VM_PUB_KEY_SIZE);
at = PutStr(out, size, at, "&type=");
if (at + 1 >= size)
return 0;
out[at++] = (Char)('0' + type % 10);
return at;
}
UInt16 VmShareChannel(Char *out, UInt16 size, const UInt8 *name, UInt16 nameLen, const UInt8 *secret)
{
UInt16 at;
at = PutStr(out, size, 0, "meshcore://channel/add?name=");
at = PutEscaped(out, size, at, name, nameLen);
at = PutStr(out, size, at, "&secret=");
at = PutHex(out, size, at, secret, VM_CHANNEL_SECRET_SIZE);
return at >= size ? 0 : at;
}
static Boolean Prefix(const Char *s, UInt16 len, const Char *p)
{
UInt16 i;
for (i = 0; p[i]; i++)
if (i >= len || s[i] != p[i])
return 0;
return 1;
}
/* A query value: '+' is a space, %XX a byte. Returns the length, or max + 1 if it doesn't fit. */
static UInt16 Unescape(const Char *s, UInt16 len, UInt8 *out, UInt16 max)
{
UInt16 i, n = 0;
Int16 hi, lo;
for (i = 0; i < len; i++) {
UInt8 c = (UInt8)s[i];
if (c == '+') {
c = ' ';
} else if (c == '%') {
hi = i + 1 < len ? HexValue(s[i + 1]) : -1;
lo = i + 2 < len ? HexValue(s[i + 2]) : -1;
if (hi < 0 || lo < 0)
return max + 1;
c = (UInt8)(hi << 4 | lo);
i += 2;
}
if (n >= max)
return max + 1;
out[n++] = c;
}
return n;
}
static Boolean HexBytes(const Char *s, UInt16 len, UInt8 *out, UInt16 n)
{
UInt16 i;
Int16 hi, lo;
if (len != 2 * n)
return 0;
for (i = 0; i < n; i++) {
hi = HexValue(s[2 * i]);
lo = HexValue(s[2 * i + 1]);
if (hi < 0 || lo < 0)
return 0;
out[i] = (UInt8)(hi << 4 | lo);
}
return 1;
}
Boolean VmShareParse(const Char *s, UInt16 len, VmShare *out)
{
UInt16 i, start, eq, end;
Boolean hasKey = 0, hasSecret = 0;
for (i = 0; i < sizeof(*out); i++)
((UInt8 *)out)[i] = 0;
if (Prefix(s, len, "meshcore://contact/add?")) {
out->kind = VM_SHARE_CONTACT;
out->type = VM_ADV_TYPE_CHAT;
i = 23;
} else if (Prefix(s, len, "meshcore://channel/add?")) {
out->kind = VM_SHARE_CHANNEL;
i = 23;
} else {
return 0;
}
/* name=value pairs; unknown ones (region_scope) are skipped */
while (i < len) {
start = i;
while (i < len && s[i] != '&' && s[i] > ' ')
i++;
end = i;
for (eq = start; eq < end && s[eq] != '='; eq++)
;
if (eq < end) {
const Char *v = s + eq + 1;
UInt16 vn = end - eq - 1, kn = eq - start;
if (kn == 4 && Prefix(s + start, kn, "name")) {
out->nameLen = Unescape(v, vn, out->name, sizeof(out->name) - 1);
if (out->nameLen >= sizeof(out->name))
return 0;
} else if (kn == 10 && Prefix(s + start, kn, "public_key")) {
hasKey = HexBytes(v, vn, out->key, VM_PUB_KEY_SIZE);
} else if (kn == 6 && Prefix(s + start, kn, "secret")) {
hasSecret = HexBytes(v, vn, out->secret, VM_CHANNEL_SECRET_SIZE);
} else if (kn == 4 && Prefix(s + start, kn, "type") && vn == 1 && v[0] >= '1' && v[0] <= '4') {
out->type = (UInt8)(v[0] - '0');
}
}
if (i >= len || s[i] != '&')
break;
i++;
}
if (!out->nameLen)
return 0;
return out->kind == VM_SHARE_CONTACT ? hasKey : hasSecret;
}
static Boolean TagChar(Char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-';
}
Boolean VmHashtagAt(const Char *text, UInt16 len, UInt16 k, Char *out, UInt16 size)
{
UInt16 start = k, end, i;
if (k >= len)
return 0;
/* back to the '#' */
while (start > 0 && text[start] != '#' && TagChar(text[start]))
start--;
if (text[start] != '#' || start + 1 >= len || text[start + 1] == '-' || !TagChar(text[start + 1]))
return 0;
for (end = start + 1; end < len && TagChar(text[end]); end++)
;
if (k >= end || end - start + 1 > size)
return 0;
for (i = start; i < end; i++)
out[i - start] = (Char)(text[i] >= 'A' && text[i] <= 'Z' ? text[i] + 32 : text[i]);
out[end - start] = 0;
return 1;
}
+112
View File
@@ -0,0 +1,112 @@
/*
* Companion commands added after protocol.c filled its segment: trace paths,
* the radio's stats and battery, a contact's route, plus the meshcore://
* share links (as MeshCore One and the other apps write them) and finding a
* #hashtag in text. Same rules as protocol.h: plain C, little-endian fields
* byte by byte, builders return a '<' frame's length or 0.
*/
#ifndef VM_PROTOCOL2_H
#define VM_PROTOCOL2_H
#include "protocol.h" /* this module is in the fifth code segment */
#define VM_CMD_GET_BATT_AND_STORAGE 20
#define VM_CMD_GET_CONTACT_BY_KEY 30
#define VM_CMD_SEND_TRACE_PATH 36
#define VM_CMD_GET_STATS 56
#define VM_RESP_BATT_AND_STORAGE 12
#define VM_RESP_STATS 24
#define VM_PUSH_TRACE_DATA 0x89
#define VM_STATS_CORE 0
#define VM_STATS_RADIO 1
#define VM_STATS_PACKETS 2
#define VM_CONTACT_FRAME_LEN 148 /* RESP_CONTACT: code, key, type, flags, path len, path[64], name[32], 4 x 4 */
#define VM_TRACE_MAX 32 /* hash bytes in a trace path we send */
/* A command that's only its code (GET_BATT_AND_STORAGE). */
UInt16 VmBuildCode(UInt8 *out, UInt16 outSize, UInt8 code) VM_SECT5;
UInt16 VmBuildGetStats(UInt8 *out, UInt16 outSize, UInt8 type) VM_SECT5;
UInt16 VmBuildGetContactByKey(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) VM_SECT5;
/*
* SEND_TRACE_PATH: hashSize bytes (1, 2 or 4) per hop. The trace is sent
* direct along the path, each hop adding the SNR it heard it at; whoever
* hears it after the last hop reports it (PUSH_TRACE_DATA). For an answer,
* the path must lead back to us: A B A.
*/
UInt16 VmBuildTrace(UInt8 *out, UInt16 outSize, UInt32 tag, UInt8 hashSize, const UInt8 *path, UInt16 pathBytes) VM_SECT5;
typedef struct {
UInt32 tag;
UInt8 hashSize;
UInt8 hops;
UInt8 hashes[64];
Int8 snrQ[65]; /* per hop: the SNR it was heard at, quarter dB; snrQ[hops]: ours */
} VmTrace;
Boolean VmParseTrace(const UInt8 *p, UInt16 len, VmTrace *out) VM_SECT5;
typedef struct {
UInt16 battMv;
Boolean hasStorage;
UInt32 usedKb, totalKb;
} VmBattery;
Boolean VmParseBattery(const UInt8 *p, UInt16 len, VmBattery *out) VM_SECT5;
/* GET_STATS replies: each fills its own part and sets its flag. */
typedef struct {
Boolean hasCore, hasRadio, hasPackets;
UInt16 battMv;
UInt32 uptimeSecs;
UInt16 errFlags;
UInt8 queueLen;
Int16 noiseFloor; /* dBm */
Int8 lastRssi;
Int8 lastSnrQ;
UInt32 txAirSecs, rxAirSecs;
UInt32 recv, sent, sentFlood, sentDirect, recvFlood, recvDirect;
} VmStats;
Boolean VmParseStats(const UInt8 *p, UInt16 len, VmStats *out) VM_SECT5;
/* A RESP_CONTACT's route: the path length byte (VM_PATH_*) and its bytes (up to 64). */
Boolean VmContactPath(const UInt8 *p, UInt16 len, UInt8 *pathLen, UInt8 *path) VM_SECT5;
/*
* ADD_UPDATE_CONTACT from a RESP_CONTACT with a new route: hops of hashSize
* bytes (0 hops: a neighbour, sent direct). Everything else is kept as the
* radio had it.
*/
UInt16 VmBuildContactRoute(UInt8 *out, UInt16 outSize, const UInt8 *contact, UInt16 len,
UInt8 hashSize, const UInt8 *path, UInt8 hops) VM_SECT5;
/*
* Hop codes typed as hex, separated by spaces or commas ("3f2a, 91c0"): all
* the same size (1, 2 or 4 bytes). Fills out (up to max bytes) and returns
* how many hops, with the size in *size; -1 if they don't parse.
*/
Int16 VmParseHopCodes(const Char *s, UInt8 *out, UInt16 max, UInt8 *size) VM_SECT5;
#define VM_SHARE_NONE 0
#define VM_SHARE_CONTACT 1
#define VM_SHARE_CHANNEL 2
typedef struct {
UInt8 kind;
UInt8 type; /* contact: VM_ADV_TYPE_* */
UInt8 key[VM_PUB_KEY_SIZE];
UInt8 secret[VM_CHANNEL_SECRET_SIZE];
UInt8 name[VM_NAME_SIZE]; /* UTF-8, not NUL-terminated */
UInt16 nameLen;
} VmShare;
/* meshcore://contact/add?name=..&public_key=<64 hex>&type=N; returns the length (no NUL), 0 if it won't fit. */
UInt16 VmShareContact(Char *out, UInt16 size, const UInt8 *name, UInt16 nameLen, const UInt8 *key, UInt8 type) VM_SECT5;
/* meshcore://channel/add?name=..&secret=<32 hex> */
UInt16 VmShareChannel(Char *out, UInt16 size, const UInt8 *name, UInt16 nameLen, const UInt8 *secret) VM_SECT5;
Boolean VmShareParse(const Char *s, UInt16 len, VmShare *out) VM_SECT5;
/*
* The #hashtag (MeshCore One's pattern: '#', a letter or digit, then letters,
* digits and '-') that character k of text is in, lowercased with its '#',
* into out (NUL-terminated); false if k isn't in one or it won't fit.
*/
Boolean VmHashtagAt(const Char *text, UInt16 len, UInt16 k, Char *out, UInt16 size) VM_SECT5;
#endif
+3 -1
View File
@@ -1,6 +1,6 @@
/*
* Code segments. The app's code is more than the ~32 KB one 68k code resource
* can reach, so modules also live in vmcode2, vmcode3 and vmcode4 (VM_SECT2-4; see
* can reach, so modules also live in vmcode2 to vmcode5 (VM_SECT2-5; see
* visormesh.def; multigen makes the stub and linker script). Everything below
* applies to any extra section.
*
@@ -34,10 +34,12 @@
#define VM_SECT2 __attribute__ ((section ("vmcode2")))
#define VM_SECT3 __attribute__ ((section ("vmcode3")))
#define VM_SECT4 __attribute__ ((section ("vmcode4")))
#define VM_SECT5 __attribute__ ((section ("vmcode5")))
#else
#define VM_SECT2
#define VM_SECT3
#define VM_SECT4
#define VM_SECT5
#endif
#endif
+16 -16
View File
@@ -11,22 +11,22 @@
#include "settings.h"
#include "app.h"
/* In the second code segment: see sections.h for the rules. */
static void *GetObject(FormType *form, UInt16 id) VM_SECT2;
static void SetFieldText(FormType *form, UInt16 id, const Char *text) VM_SECT2;
static const Char *GetFieldText(FormType *form, UInt16 id) VM_SECT2;
static void SetStatus(FormType *form, const Char *text) VM_SECT2;
static void ShowError(const Char *msg) VM_SECT2;
static void SelectHashSize(FormType *form, UInt8 bytes) VM_SECT2;
static UInt8 SelectedHashSize(FormType *form) VM_SECT2;
static void SetRadioFields(FormType *form, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr) VM_SECT2;
static Int16 FindPreset(UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr) VM_SECT2;
static void FormatUtcOffset(Char *out, Int16 minutes) VM_SECT2;
static Boolean ParseUtcOffset(const Char *s, Int16 *minutes) VM_SECT2;
static void InitForm(FormType *form) VM_SECT2;
static void ApplyPreset(FormType *form, UInt16 index) VM_SECT2;
static Boolean Send(FormType *form, const UInt8 *frame, UInt16 len, const Char *what) VM_SECT2;
static Boolean Save(FormType *form) VM_SECT2;
/* In the fourth code segment: see sections.h for the rules. */
static void *GetObject(FormType *form, UInt16 id) VM_SECT4;
static void SetFieldText(FormType *form, UInt16 id, const Char *text) VM_SECT4;
static const Char *GetFieldText(FormType *form, UInt16 id) VM_SECT4;
static void SetStatus(FormType *form, const Char *text) VM_SECT4;
static void ShowError(const Char *msg) VM_SECT4;
static void SelectHashSize(FormType *form, UInt8 bytes) VM_SECT4;
static UInt8 SelectedHashSize(FormType *form) VM_SECT4;
static void SetRadioFields(FormType *form, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr) VM_SECT4;
static Int16 FindPreset(UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr) VM_SECT4;
static void FormatUtcOffset(Char *out, Int16 minutes) VM_SECT4;
static Boolean ParseUtcOffset(const Char *s, Int16 *minutes) VM_SECT4;
static void InitForm(FormType *form) VM_SECT4;
static void ApplyPreset(FormType *form, UInt16 index) VM_SECT4;
static Boolean Send(FormType *form, const UInt8 *frame, UInt16 len, const Char *what) VM_SECT4;
static Boolean Save(FormType *form) VM_SECT4;
#define CUSTOM_LABEL "Custom"
+2 -2
View File
@@ -2,8 +2,8 @@
#define VM_SETTINGS_H
#include <PalmOS.h>
#include "sections.h" /* this module is in the second code segment */
#include "sections.h" /* this module is in the fourth code segment */
Boolean SettingsFormHandleEvent(EventType *event) VM_SECT2;
Boolean SettingsFormHandleEvent(EventType *event) VM_SECT4;
#endif
+158
View File
@@ -0,0 +1,158 @@
/* Host-side tests for the newer companion commands and share links (src/protocol2.c). Run with `make test`. */
#include <stdio.h>
#include <string.h>
#include "../src/protocol2.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \
} while (0)
static void test_trace(void)
{
UInt8 path[] = { 0x3f, 0x2a, 0x91, 0xc0, 0x3f, 0x2a };
UInt8 f[64];
UInt8 push[] = { 0x89, 0, 6, 1, 0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0,
0x3f, 0x2a, 0x91, 0xc0, 0x3f, 0x2a, /* hashes */
24, 0xF8, 10, /* per hop: 6, -2, 2.5 dB */
20 }; /* ours: 5 dB */
VmTrace t;
UInt16 n;
n = VmBuildTrace(f, sizeof(f), 0x12345678, 2, path, sizeof(path));
CHECK(n == 3 + 10 + 6);
CHECK(f[0] == '<' && f[1] == 16 && f[3] == VM_CMD_SEND_TRACE_PATH);
CHECK(f[4] == 0x78 && f[7] == 0x12 && f[12] == 1 && f[13] == 0x3f && f[18] == 0x2a);
CHECK(VmBuildTrace(f, sizeof(f), 1, 2, path, 5) == 0); /* not whole hops */
CHECK(VmBuildTrace(f, sizeof(f), 1, 3, path, 6) == 0); /* no 3-byte traces */
CHECK(VmBuildTrace(f, sizeof(f), 1, 1, path, 0) == 0);
CHECK(VmParseTrace(push, sizeof(push), &t));
CHECK(t.tag == 0x12345678 && t.hashSize == 2 && t.hops == 3);
CHECK(t.hashes[2] == 0x91 && t.snrQ[0] == 24 && t.snrQ[1] == -8 && t.snrQ[3] == 20);
CHECK(!VmParseTrace(push, sizeof(push) - 1, &t));
}
static void test_stats(void)
{
UInt8 batt[] = { 12, 0xD2, 0x0F, 12, 0, 0, 0, 0xE4, 1, 0, 0 };
UInt8 old[] = { 12, 0xD2, 0x0F };
UInt8 radio[] = { 24, 1, 0x90, 0xFF, 0xA0, 0xEC, 120, 0, 0, 0, 0x10, 0x0E, 0, 0 };
UInt8 core[] = { 24, 0, 0xD2, 0x0F, 0x80, 0x51, 0x01, 0, 0, 0, 2 };
UInt8 pk[26] = { 24, 2, 100 };
VmBattery b;
VmStats s;
CHECK(VmParseBattery(batt, sizeof(batt), &b) && b.battMv == 4050 && b.hasStorage && b.usedKb == 12 && b.totalKb == 484);
CHECK(VmParseBattery(old, sizeof(old), &b) && b.battMv == 4050 && !b.hasStorage);
memset(&s, 0, sizeof(s));
CHECK(VmParseStats(radio, sizeof(radio), &s) && s.hasRadio && !s.hasCore);
CHECK(s.noiseFloor == -112 && s.lastRssi == -96 && s.lastSnrQ == -20 && s.txAirSecs == 120 && s.rxAirSecs == 3600);
CHECK(VmParseStats(core, sizeof(core), &s) && s.hasCore && s.uptimeSecs == 86400 && s.queueLen == 2);
CHECK(VmParseStats(pk, sizeof(pk), &s) && s.hasPackets && s.recv == 100);
CHECK(!VmParseStats(radio, 10, &s));
}
static void test_route(void)
{
UInt8 c[VM_CONTACT_FRAME_LEN], f[200], pathLen, path[64], hops[] = { 0xaa, 0xbb, 0xcc, 0xdd };
UInt16 n, i;
memset(c, 0, sizeof(c));
c[0] = VM_RESP_CONTACT;
c[1] = 0x11;
c[33] = VM_ADV_TYPE_REPEATER;
c[35] = 0x41; /* 2-byte hashes, 1 hop */
c[36] = 0x12; c[37] = 0x34;
memcpy(c + 100, "Rpt", 3);
c[147] = 0x99; /* lastmod: not sent back */
CHECK(VmContactPath(c, sizeof(c), &pathLen, path) && pathLen == 0x41 && path[1] == 0x34);
n = VmBuildContactRoute(f, sizeof(f), c, sizeof(c), 2, hops, 2);
CHECK(n == 3 + 144);
CHECK(f[3] == VM_CMD_ADD_UPDATE_CONTACT && f[4] == 0x11 && f[3 + 33] == VM_ADV_TYPE_REPEATER);
CHECK(f[3 + 35] == 0x42 && f[3 + 36] == 0xaa && f[3 + 39] == 0xdd && f[3 + 40] == 0);
CHECK(memcmp(f + 3 + 100, "Rpt", 3) == 0);
n = VmBuildContactRoute(f, sizeof(f), c, sizeof(c), 2, hops, 0);
CHECK(n && f[3 + 35] == 0x40);
for (i = 0; i < 4; i++)
CHECK(f[3 + 36 + i] == 0);
CHECK(VmBuildContactRoute(f, sizeof(f), c, 100, 2, hops, 1) == 0);
}
static void test_hops(void)
{
UInt8 b[16], size;
CHECK(VmParseHopCodes("3f2a, 91C0 3f2a", b, sizeof(b), &size) == 3 && size == 2 && b[2] == 0x91 && b[3] == 0xc0);
CHECK(VmParseHopCodes("", b, sizeof(b), &size) == 0);
CHECK(VmParseHopCodes("a1 b2c3", b, sizeof(b), &size) == -1); /* mixed sizes */
CHECK(VmParseHopCodes("abc", b, sizeof(b), &size) == -1);
CHECK(VmParseHopCodes("12x4", b, sizeof(b), &size) == -1);
CHECK(VmParseHopCodes("11223344", b, sizeof(b), &size) == 1 && size == 4);
CHECK(VmParseHopCodes("1122 3344 5566", b, 4, &size) == -1); /* too many */
}
static void test_share(void)
{
Char out[200];
UInt8 key[32], secret[16];
VmShare s;
UInt16 n, i;
const char *ch = "meshcore://channel/add?name=%23test+one&secret=000102030405060708090a0b0c0d0e0f&region_scope=pa";
for (i = 0; i < 32; i++)
key[i] = (UInt8)i;
for (i = 0; i < 16; i++)
secret[i] = (UInt8)(0xf0 + i);
n = VmShareContact(out, sizeof(out), (const UInt8 *)"Bob K", 5, key, 2);
out[n] = 0;
CHECK(strcmp(out, "meshcore://contact/add?name=Bob%20K&public_key=000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f&type=2") == 0);
CHECK(VmShareParse(out, n, &s) && s.kind == VM_SHARE_CONTACT && s.type == 2 && s.nameLen == 5 &&
memcmp(s.name, "Bob K", 5) == 0 && s.key[31] == 31);
CHECK(VmShareContact(out, 60, (const UInt8 *)"Bob", 3, key, 1) == 0);
n = VmShareChannel(out, sizeof(out), (const UInt8 *)"#test", 5, secret);
out[n] = 0;
CHECK(strcmp(out, "meshcore://channel/add?name=%23test&secret=f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff") == 0);
CHECK(VmShareParse(ch, (UInt16)strlen(ch), &s) && s.kind == VM_SHARE_CHANNEL && s.nameLen == 9 &&
memcmp(s.name, "#test one", 9) == 0 && s.secret[15] == 15);
CHECK(!VmShareParse("meshcore://channel/add?name=x&secret=00", 39, &s));
CHECK(!VmShareParse("meshcore://contact/add?public_key=00", 36, &s));
CHECK(!VmShareParse("https://example.com", 19, &s));
/* a UTF-8 name round trip */
n = VmShareContact(out, sizeof(out), (const UInt8 *)"Caf\xc3\xa9", 5, key, 1);
CHECK(VmShareParse(out, n, &s) && s.nameLen == 5 && memcmp(s.name, "Caf\xc3\xa9", 5) == 0);
}
static void test_hashtag(void)
{
const char *t = "join #Philly-Mesh now, or #1 #-x #";
Char out[32];
CHECK(VmHashtagAt(t, (UInt16)strlen(t), 5, out, sizeof(out)) && strcmp(out, "#philly-mesh") == 0);
CHECK(VmHashtagAt(t, (UInt16)strlen(t), 12, out, sizeof(out)) && strcmp(out, "#philly-mesh") == 0);
CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 17, out, sizeof(out))); /* the space after it */
CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 2, out, sizeof(out)));
CHECK(VmHashtagAt(t, (UInt16)strlen(t), 26, out, sizeof(out)) && strcmp(out, "#1") == 0);
CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 30, out, sizeof(out))); /* #-x */
CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 33, out, sizeof(out))); /* a lone # */
CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 5, out, 5)); /* too long for out */
}
int main(void)
{
test_trace();
test_stats();
test_route();
test_hops();
test_share();
test_hashtag();
if (failures) {
printf("%d failure(s)\n", failures);
return 1;
}
printf("protocol2: all tests passed\n");
return 0;
}
+1 -1
View File
@@ -16,7 +16,7 @@ import re
import subprocess
import sys
SECTIONS = ('vmcode2', 'vmcode3', 'vmcode4')
SECTIONS = ('vmcode2', 'vmcode3', 'vmcode4', 'vmcode5')
OBJDUMP = 'm68k-palmos-objdump'
+1 -1
View File
@@ -1,2 +1,2 @@
application { "VisorMesh" VMsh backup }
multiple code { "vmcode2" "vmcode3" "vmcode4" }
multiple code { "vmcode2" "vmcode3" "vmcode4" "vmcode5" }