From 4068473ad133681cfbc6038d239d18e58ba1f0fe Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Mon, 2 Nov 2026 07:17:03 -0500 Subject: [PATCH] palm-client: admin console for repeaters and rooms (CLI, status, telemetry) Login on a repeater opens the console (rooms: More > Admin Console). A command line sends CLI commands (SEND_TXT_MSG with txt_type CLI); replies, which the radio delivers as CLI-type DMs, go to the console instead of the conversation. Status (SEND_STATUS_REQ -> 0x87) shows the repeater's RepeaterStats: battery, uptime, queue, noise, last RSSI/SNR, packet counts, airtime, dups and errors. Telem (SEND_TELEMETRY_REQ -> 0x8B) decodes the Cayenne LPP readings. Cmds lists common commands and Read settings (ten gets); items ending in ... go into the command line to finish. One request at a time, the rest queued; no answer is reported after the radio's estimate plus 8 s. remote_fmt.c (builders, parsers, text) is pure C and host-tested; the console and screen are in segment 3 (12.4 KB). --- palm-client/Makefile | 4 +- palm-client/resources/visormesh.rcp | 26 ++- palm-client/src/contact_form.c | 27 ++- palm-client/src/main.c | 6 + palm-client/src/remote.c | 288 ++++++++++++++++++++++++ palm-client/src/remote.h | 49 +++++ palm-client/src/remote_fmt.c | 325 ++++++++++++++++++++++++++++ palm-client/src/remote_fmt.h | 53 +++++ palm-client/src/remote_form.c | 172 +++++++++++++++ palm-client/src/remote_form.h | 9 + palm-client/src/resources.h | 11 + palm-client/src/session.c | 49 ++++- palm-client/src/session.h | 4 + palm-client/tests/test_protocol.c | 56 +++++ 14 files changed, 1066 insertions(+), 13 deletions(-) create mode 100644 palm-client/src/remote.c create mode 100644 palm-client/src/remote.h create mode 100644 palm-client/src/remote_fmt.c create mode 100644 palm-client/src/remote_fmt.h create mode 100644 palm-client/src/remote_form.c create mode 100644 palm-client/src/remote_form.h diff --git a/palm-client/Makefile b/palm-client/Makefile index b3fbb2c..7e09b6c 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -10,7 +10,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/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.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/fontmetrics.c src/ports.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/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/fontmetrics.c src/ports.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -70,7 +70,7 @@ build/$(APP).prc: build/$(APP) build/res.stamp build-prc -o $@ $(APP).def build/$(APP) build/res/*.bin test: | build - gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c src/sha256.c + gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c src/sha256.c src/remote_fmt.c build/test_protocol gcc -std=c99 -Wall -Wextra -o build/test_text tests/test_text.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c build/test_text diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index 89cdd71..239e1ee 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -106,7 +106,7 @@ BEGIN BUTTON "Message" ID ContactMessageButton AT (PREVRIGHT+4 PREVTOP AUTO AUTO) BUTTON "Login" ID ContactLoginButton AT (PREVRIGHT+4 PREVTOP AUTO AUTO) BUTTON "More" ID ContactMoreButton AT (PREVRIGHT+4 PREVTOP AUTO AUTO) - LIST "Reset Path" "Remove" ID ContactMoreList AT (100 122 58 AUTO) NONUSABLE VISIBLEITEMS 2 + LIST "Admin Console" "Reset Path" "Remove" ID ContactMoreList AT (90 111 68 AUTO) NONUSABLE VISIBLEITEMS 3 END FORM ID ContactAddForm AT (0 0 160 160) @@ -129,6 +129,30 @@ BEGIN GRAFFITISTATEINDICATOR AT (145 147) END +/* Remote admin console for a repeater or room */ +FORM ID RemoteForm AT (0 0 160 160) +NOFRAME +USABLE +MENUID MainMenuBar +BEGIN + TITLE " " + GADGET ID RemoteGadget AT (0 16 153 114) + SCROLLBAR ID RemoteScroll AT (153 16 7 114) VALUE 0 MIN 0 MAX 0 PAGESIZE 1 + FIELD ID RemoteField AT (1 132 148 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 140 + GRAFFITISTATEINDICATOR AT (151 133) + BUTTON "Send" ID RemoteSendButton AT (1 147 25 12) + BUTTON "Status" ID RemoteStatusButton AT (PREVRIGHT+3 PREVTOP 31 12) + BUTTON "Telem" ID RemoteTelemButton AT (PREVRIGHT+3 PREVTOP 29 12) + BUTTON "Cmds" ID RemoteCmdsButton AT (PREVRIGHT+3 PREVTOP 28 12) + BUTTON "Done" ID RemoteDoneButton AT (PREVRIGHT+3 PREVTOP 27 12) + LIST "Read settings" "ver" "clock" "clock sync" "neighbors" "advert" "advert.zerohop" + "stats-core" "stats-radio" "stats-packets" "get acl" "clear stats" + "set name..." "set tx..." "set radio..." "set repeat..." "set advert.interval..." + "set flood.advert.interval..." "set flood.max..." "set guest.password..." "password..." + "reboot..." + ID RemoteCmdList AT (30 16 128 AUTO) NONUSABLE VISIBLEITEMS 12 +END + ALERT ID PasswordAlert CONFIRMATION DEFAULTBUTTON 1 diff --git a/palm-client/src/contact_form.c b/palm-client/src/contact_form.c index 198715c..2f63ed0 100644 --- a/palm-client/src/contact_form.c +++ b/palm-client/src/contact_form.c @@ -5,8 +5,8 @@ * Detail: type, route, last heard, the full key, then every time the radio * heard the node directly (its own zero-hop adverts, or relaying packets for a * repeater) with SNR and RSSI. Message opens the DM; for a room it logs in - * first if needed. Login asks for the password; More has Reset Path and - * Remove. + * first if needed. Login asks for the password and opens a room's posts or + * a repeater's admin console; More has Admin Console, Reset Path and Remove. */ #include #include "resources.h" @@ -20,6 +20,7 @@ #include "ui/text.h" #include "ui/rowlist.h" #include "contact_form.h" +#include "remote.h" /* In an extra code segment (VM_SECT3): see sections.h for the rules. */ static void *GetObject(FormType *form, UInt16 id) VM_SECT3; @@ -198,8 +199,8 @@ static Boolean Login(FormType *form) ErrorFor(err); return false; } - if (gContact.type != VM_ADV_TYPE_ROOM) { - StrPrintF(msg, "Logged in to %s%s.", gContact.name, admin ? " as admin" : ""); + if (!admin && gContact.type == VM_ADV_TYPE_REPEATER) { + StrPrintF(msg, "Logged in to %s as a guest: status and telemetry only, no CLI.", gContact.name); FrmCustomAlert(ContactInfoAlert, "Login", msg, ""); } return true; @@ -233,7 +234,14 @@ static void More(FormType *form) Error("Connect to the radio first (Node)."); return; } - if (pick == 0) { /* Reset Path */ + if (pick == 0) { /* Admin Console */ + if (gContact.type != VM_ADV_TYPE_ROOM && gContact.type != VM_ADV_TYPE_REPEATER) { + Error("Only repeaters and rooms have an admin console."); + return; + } + if (SessionIsLoggedIn(gContact.pubKey) || Login(form)) + RemoteOpen(&gContact); + } else if (pick == 1) { /* Reset Path */ err = SessionKeyCommand(VM_CMD_RESET_PATH, gContact.pubKey); if (err == errNone) err = ContactsRefresh(NULL); @@ -241,7 +249,7 @@ static void More(FormType *form) ErrorFor(err); ReloadContact(); RowListDraw(&gList); - } else if (pick == 1) { /* Remove */ + } else if (pick == 2) { /* Remove */ if (FrmCustomAlert(RemoveContactAlert, gContact.name, "", "") != 0) return; err = SessionKeyCommand(VM_CMD_REMOVE_CONTACT, gContact.pubKey); @@ -309,10 +317,13 @@ Boolean ContactFormHandleEvent(EventType *event) OpenConversation(); return true; case ContactLoginButton: - if (Login(form) && gContact.type == VM_ADV_TYPE_ROOM) + /* a room opens to its posts, a repeater to its admin console */ + if (!Login(form)) + RowListDraw(&gList); + else if (gContact.type == VM_ADV_TYPE_ROOM) OpenConversation(); else - RowListDraw(&gList); + RemoteOpen(&gContact); return true; case ContactMoreButton: More(form); diff --git a/palm-client/src/main.c b/palm-client/src/main.c index 6adebf9..f8839e6 100644 --- a/palm-client/src/main.c +++ b/palm-client/src/main.c @@ -10,6 +10,8 @@ #include "contacts_form.h" #include "contact_form.h" #include "heard.h" +#include "remote.h" +#include "remote_form.h" #include "node_form.h" #include "settings.h" #include "ports.h" @@ -72,6 +74,7 @@ static Boolean AppHandleEvent(EventType *event) case ChannelForm: FrmSetEventHandler(form, ChannelFormHandleEvent); break; case ContactForm: FrmSetEventHandler(form, ContactFormHandleEvent); break; case ContactAddForm: FrmSetEventHandler(form, ContactAddFormHandleEvent); break; + case RemoteForm: FrmSetEventHandler(form, RemoteFormHandleEvent); break; } return true; } @@ -115,6 +118,9 @@ static void Poll(void) UInt16 formId = FrmGetActiveFormID(); UInt16 changed = SessionPoll(); + if (RemoteTick()) + changed |= VM_CHANGED_REMOTE; + /* a short beep for a new DM or mention, unless that conversation is on screen */ if ((changed & VM_CHANGED_ALERT) && !(formId == ConvoForm && ConvEqual(SessionAlertConv(), &gAppConv))) SndPlaySystemSound(sndConfirmation); diff --git a/palm-client/src/remote.c b/palm-client/src/remote.c new file mode 100644 index 0000000..8c73335 --- /dev/null +++ b/palm-client/src/remote.c @@ -0,0 +1,288 @@ +#include +#include "resources.h" +#include "session.h" +#include "ui/text.h" +#include "ui/fontmetrics.h" +#include "app.h" +#include "remote.h" + +/* In an extra code segment (VM_SECT3): see sections.h for the rules. */ +static void AddLine(UInt8 kind, const Char *text, UInt16 len) VM_SECT3; +static void AddText(UInt8 kind, const Char *text, UInt16 len) VM_SECT3; +static void Note(const Char *text) VM_SECT3; +static Boolean Mine(const UInt8 *prefix6) VM_SECT3; +static void Start(UInt8 kind, const Char *cmd, UInt16 cmdLen) VM_SECT3; +static void Next(void) VM_SECT3; +static void Done(void) VM_SECT3; +static void Enqueue(UInt8 kind, const Char *text, UInt16 len) VM_SECT3; + +#define MAX_LINES 120 +#define WRAP_W 148 +#define SLACK_TICKS (8 * SysTicksPerSecond()) /* past the radio's estimate, before "no answer" */ +#define MAX_QUEUE 12 + +#define REQ_NONE 0 +#define REQ_CLI 1 +#define REQ_STATUS 2 +#define REQ_TELEM 3 + +static VmContact gTarget; +static VmRLine *gLines = NULL; +static UInt16 gCount = 0; +static UInt8 gPending = REQ_NONE; +static UInt32 gDeadline; +/* queued commands: pointers into strings that outlive the queue, with lengths */ +static const Char *gQueue[MAX_QUEUE]; +static UInt16 gQueueLen[MAX_QUEUE]; +static UInt8 gQueueKind[MAX_QUEUE]; +static UInt16 gQHead = 0, gQCount = 0; +static Char gTyped[MAX_QUEUE][VM_RLINE_CHARS * 3]; /* copies of typed commands */ + +void RemoteOpen(const VmContact *c) +{ + if (MemCmp(gTarget.pubKey, c->pubKey, VM_PUB_KEY_SIZE) != 0) { + gCount = 0; + gPending = REQ_NONE; + gQCount = 0; + } + gTarget = *c; + if (!gLines) + gLines = MemPtrNew(sizeof(VmRLine) * MAX_LINES); + FrmGotoForm(RemoteForm); +} + +const VmContact *RemoteTarget(void) +{ + return &gTarget; +} + +UInt16 RemoteLineCount(void) +{ + return gLines ? gCount : 0; +} + +const VmRLine *RemoteLine(UInt16 i) +{ + return &gLines[i]; +} + +Boolean RemoteBusy(void) +{ + return gPending != REQ_NONE; +} + +/* One display line; the oldest scrolls away when full. */ +static void AddLine(UInt8 kind, const Char *text, UInt16 len) +{ + VmRLine *l; + + if (!gLines) + return; + if (gCount == MAX_LINES) { + MemMove(gLines, gLines + 1, sizeof(VmRLine) * (MAX_LINES - 1)); + gCount--; + } + l = &gLines[gCount++]; + l->kind = kind; + if (len > VM_RLINE_CHARS - 1) + len = VM_RLINE_CHARS - 1; + MemMove(l->text, text, len); + l->text[len] = 0; +} + +/* Wrapped to the console's width, breaking at line breaks too. */ +static void AddText(UInt8 kind, const Char *text, UInt16 len) +{ + UInt16 off = 0, n, used; + + if (len == 0) { + AddLine(kind, "", 0); + return; + } + while (off < len) { + n = TxtWrap(text + off, (UInt16)(len - off), WRAP_W, TxtSmallWidth, &used); + if (used == 0) + break; + AddLine(kind, text + off, n); + off += used; + } +} + +static void Note(const Char *text) +{ + AddText(VM_RL_NOTE, text, StrLen(text)); +} + +static Boolean Mine(const UInt8 *prefix6) +{ + return gLines && MemCmp(gTarget.pubKey, prefix6, 6) == 0; +} + +/* Sends one request now. */ +static void Start(UInt8 kind, const Char *cmd, UInt16 cmdLen) +{ + UInt8 frame[VM_HEADER_LEN + 13 + VM_MAX_DM_TEXT], utf8[VM_MAX_DM_TEXT]; + Char msg[48]; + UInt16 len = 0, n; + VmSent sent; + Err err; + + if (kind == REQ_CLI) { + AddText(VM_RL_CMD, cmd, cmdLen); + n = TxtPalmToUtf8(cmd, cmdLen, utf8, sizeof(utf8)); + if (n == 0) { + Note("Too long to send."); + return; + } + len = VmBuildSendCli(frame, sizeof(frame), gTarget.pubKey, 0, utf8, n); + } else if (kind == REQ_STATUS) { + Note("Asking for status..."); + len = VmBuildStatusReq(frame, sizeof(frame), gTarget.pubKey); + } else { + Note("Asking for telemetry..."); + len = VmBuildTelemetryReq(frame, sizeof(frame), gTarget.pubKey); + } + err = SessionRequestSent(frame, len, &sent); + if (err != errNone) { + AppErrorText(err, msg); + Note(msg); + return; + } + gPending = kind; + gDeadline = TimGetTicks() + (sent.timeoutMs / 1000UL + 2) * SysTicksPerSecond() + SLACK_TICKS; +} + +/* Starts queued requests until one is outstanding. */ +static void Next(void) +{ + while (gPending == REQ_NONE && gQCount > 0) { + UInt16 i = gQHead; + + gQHead = (gQHead + 1) % MAX_QUEUE; + gQCount--; + Start(gQueueKind[i], gQueue[i], gQueueLen[i]); + } +} + +static void Done(void) +{ + gPending = REQ_NONE; + Next(); +} + +static void Enqueue(UInt8 kind, const Char *text, UInt16 len) +{ + UInt16 i; + + if (!SessionIsConnected()) { + Note("Not connected to the radio."); + return; + } + if (gQCount == MAX_QUEUE) { + Note("Busy: wait for the answers."); + return; + } + i = (gQHead + gQCount) % MAX_QUEUE; + gQueueKind[i] = kind; + gQueue[i] = text; + gQueueLen[i] = len; + gQCount++; + Next(); +} + +void RemoteCommand(const Char *text) +{ + UInt16 i = (gQHead + gQCount) % MAX_QUEUE, len = StrLen(text); + + /* typed text goes away with the field: keep a copy in the queue slot */ + if (len > sizeof(gTyped[0]) - 1) + len = sizeof(gTyped[0]) - 1; + MemMove(gTyped[i], text, len); + gTyped[i][len] = 0; + Enqueue(REQ_CLI, gTyped[i], len); +} + +void RemoteBatch(const Char *cmds) +{ + const Char *p = cmds; + + while (*p) { + const Char *e = p; + + while (*e && *e != '|') + e++; + Enqueue(REQ_CLI, p, (UInt16)(e - p)); + p = *e ? e + 1 : e; + } +} + +void RemoteStatus(void) +{ + Enqueue(REQ_STATUS, "", 0); +} + +void RemoteTelemetry(void) +{ + Enqueue(REQ_TELEM, "", 0); +} + +Boolean RemoteOnCli(const UInt8 *prefix6, const UInt8 *utf8, UInt16 len) +{ + Char text[VM_MAX_FRAME]; + UInt16 n; + + if (!Mine(prefix6)) + return false; + n = TxtUtf8ToPalm(utf8, len, text, sizeof(text)); + AddText(VM_RL_REPLY, text, n); + if (gPending == REQ_CLI) + Done(); + return true; +} + +Boolean RemoteOnStatus(const UInt8 *frame, UInt16 len) +{ + VmRepeaterStats s; + UInt8 prefix[6]; + Char line[64]; + UInt16 i; + + if (!VmParseStatus(frame, len, prefix, &s) || !Mine(prefix)) + return false; + for (i = 0; i < VM_STATUS_LINES; i++) { + VmFormatStatusLine(&s, i, line); + AddText(VM_RL_INFO, line, StrLen(line)); + } + if (gPending == REQ_STATUS) + Done(); + return true; +} + +Boolean RemoteOnTelemetry(const UInt8 *frame, UInt16 len) +{ + const UInt8 *data; + UInt8 prefix[6]; + UInt16 dlen, pos = 0, shown = 0; + Char line[64]; + + if (!VmParseTelemetry(frame, len, prefix, &data, &dlen) || !Mine(prefix)) + return false; + while (VmLppNext(data, dlen, &pos, line)) { + AddText(VM_RL_INFO, line, StrLen(line)); + shown++; + } + if (!shown) + Note("No telemetry readings (or not permitted)."); + if (gPending == REQ_TELEM) + Done(); + return true; +} + +Boolean RemoteTick(void) +{ + if (gPending == REQ_NONE || TimGetTicks() < gDeadline) + return false; + Note(gPending == REQ_CLI ? "(no answer: not admin, or out of range?)" : "(no answer)"); + Done(); + return true; +} diff --git a/palm-client/src/remote.h b/palm-client/src/remote.h new file mode 100644 index 0000000..400250d --- /dev/null +++ b/palm-client/src/remote.h @@ -0,0 +1,49 @@ +/* + * The remote admin console for one repeater or room: CLI commands and their + * replies, status and telemetry reports, as wrapped lines for the screen. + * One request is outstanding at a time; "Read settings" queues several gets + * and sends each when the previous one is answered or times out. + */ +#ifndef VM_REMOTE_H +#define VM_REMOTE_H + +#include +#include "sections.h" /* this module is in the third code segment */ +#include "protocol.h" +#include "remote_fmt.h" + +#define VM_RLINE_CHARS 48 + +#define VM_RL_CMD 0 /* a command we sent */ +#define VM_RL_REPLY 1 /* the node's answer */ +#define VM_RL_INFO 2 /* a status / telemetry line */ +#define VM_RL_NOTE 3 /* ours: "no answer", errors */ + +typedef struct { + UInt8 kind; + Char text[VM_RLINE_CHARS]; +} VmRLine; + +/* Opens the console screen for a node (clears it if it was another node's). */ +void RemoteOpen(const VmContact *c) VM_SECT3; +const VmContact *RemoteTarget(void) VM_SECT3; + +UInt16 RemoteLineCount(void) VM_SECT3; +const VmRLine *RemoteLine(UInt16 i) VM_SECT3; +Boolean RemoteBusy(void) VM_SECT3; + +/* Sends a command (Palm text) now, or queues it behind the outstanding request. */ +void RemoteCommand(const Char *text) VM_SECT3; +/* Queues a '|'-separated list of commands (the string must stay valid: a literal). */ +void RemoteBatch(const Char *cmds) VM_SECT3; +void RemoteStatus(void) VM_SECT3; +void RemoteTelemetry(void) VM_SECT3; + +/* From the session, for frames addressed to us. True if the console took it. */ +Boolean RemoteOnCli(const UInt8 *prefix6, const UInt8 *utf8, UInt16 len) VM_SECT3; +Boolean RemoteOnStatus(const UInt8 *frame, UInt16 len) VM_SECT3; +Boolean RemoteOnTelemetry(const UInt8 *frame, UInt16 len) VM_SECT3; +/* Timeouts and the next queued command; true if lines changed. Called from the poll loop. */ +Boolean RemoteTick(void) VM_SECT3; + +#endif diff --git a/palm-client/src/remote_fmt.c b/palm-client/src/remote_fmt.c new file mode 100644 index 0000000..74afac3 --- /dev/null +++ b/palm-client/src/remote_fmt.c @@ -0,0 +1,325 @@ +#include "remote_fmt.h" + +/* In an extra code segment (VM_SECT3): see sections.h for the rules. */ +static UInt16 LE16(const UInt8 *p) VM_SECT3; +static UInt32 LE32(const UInt8 *p) VM_SECT3; +static Int32 BE(const UInt8 *p, UInt16 n, Boolean isSigned) VM_SECT3; +static Char *Put(Char *o, const Char *s) VM_SECT3; +static Char *PutU(Char *o, UInt32 v) VM_SECT3; +static Char *PutI(Char *o, Int32 v) VM_SECT3; +static Char *PutFixed(Char *o, Int32 v, UInt16 decimals) VM_SECT3; +static Char *PutDuration(Char *o, UInt32 secs) VM_SECT3; + +static UInt16 LE16(const UInt8 *p) +{ + return (UInt16)(p[0] | (p[1] << 8)); +} + +static UInt32 LE32(const UInt8 *p) +{ + return (UInt32)p[0] | ((UInt32)p[1] << 8) | ((UInt32)p[2] << 16) | ((UInt32)p[3] << 24); +} + +/* Cayenne LPP values are big-endian. */ +static Int32 BE(const UInt8 *p, UInt16 n, Boolean isSigned) +{ + UInt32 v = 0; + UInt16 i; + + for (i = 0; i < n; i++) + v = (v << 8) | p[i]; + if (isSigned && n < 4 && (p[0] & 0x80)) + v |= ~(UInt32)0 << (8 * n); + return (Int32)v; +} + +static Char *Put(Char *o, const Char *s) +{ + while (*s) + *o++ = *s++; + *o = 0; + return o; +} + +static Char *PutU(Char *o, UInt32 v) +{ + Char tmp[11]; + UInt16 n = 0; + + do { + tmp[n++] = (Char)('0' + v % 10); + v /= 10; + } while (v); + while (n) + *o++ = tmp[--n]; + *o = 0; + return o; +} + +static Char *PutI(Char *o, Int32 v) +{ + if (v < 0) { + *o++ = '-'; + return PutU(o, (UInt32)-v); + } + return PutU(o, (UInt32)v); +} + +/* v / 10^decimals with that many decimals: (412, 2) -> "4.12". */ +static Char *PutFixed(Char *o, Int32 v, UInt16 decimals) +{ + UInt32 div = 1, a; + UInt16 i; + + for (i = 0; i < decimals; i++) + div *= 10; + if (v < 0) { + *o++ = '-'; + a = (UInt32)-v; + } else { + a = (UInt32)v; + } + o = PutU(o, a / div); + if (decimals) { + UInt32 frac = a % div; + + *o++ = '.'; + for (i = decimals; i > 0; i--) { + UInt32 d = div / 10, digit; + UInt16 k; + + for (k = 1, d = 1; k < i; k++) + d *= 10; + digit = (frac / d) % 10; + *o++ = (Char)('0' + digit); + } + *o = 0; + } + return o; +} + +/* "3d 4h", "5h 2m", "12m", "40s". */ +static Char *PutDuration(Char *o, UInt32 secs) +{ + if (secs >= 86400UL) { + o = PutU(o, secs / 86400UL); + o = Put(o, "d "); + o = PutU(o, (secs % 86400UL) / 3600); + return Put(o, "h"); + } + if (secs >= 3600) { + o = PutU(o, secs / 3600); + o = Put(o, "h "); + o = PutU(o, (secs % 3600) / 60); + return Put(o, "m"); + } + if (secs >= 60) { + o = PutU(o, secs / 60); + return Put(o, "m"); + } + o = PutU(o, secs); + return Put(o, "s"); +} + +UInt16 VmBuildSendCli(UInt8 *out, UInt16 outSize, const UInt8 *prefix6, UInt32 timestamp, + const UInt8 *text, UInt16 textLen) +{ + UInt16 n = VmBuildSendTxt(out, outSize, prefix6, timestamp, 0, text, textLen); + + if (n) + out[VM_HEADER_LEN + 1] = VM_TXT_TYPE_CLI_DATA; /* the radio uses its own clock for CLI */ + return n; +} + +UInt16 VmBuildStatusReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) +{ + return VmBuildKeyCommand(out, outSize, VM_CMD_SEND_STATUS_REQ, pubKey); +} + +/* CMD_SEND_TELEMETRY_REQ: code, 3 reserved bytes, pub_key[32]. */ +UInt16 VmBuildTelemetryReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) +{ + UInt8 p[4 + VM_PUB_KEY_SIZE]; + UInt16 i; + + p[0] = VM_CMD_SEND_TELEMETRY_REQ; + p[1] = p[2] = p[3] = 0; + for (i = 0; i < VM_PUB_KEY_SIZE; i++) + p[4 + i] = pubKey[i]; + return VmEncodeFrame(out, outSize, p, sizeof(p)); +} + +Boolean VmParseStatus(const UInt8 *p, UInt16 len, UInt8 *prefix6, VmRepeaterStats *s) +{ + UInt16 i; + + if (len < 8 + 48 || p[0] != VM_PUSH_STATUS_RESPONSE) + return 0; + for (i = 0; i < 6; i++) + prefix6[i] = p[2 + i]; + p += 8; + s->battMv = LE16(p); + s->txQueue = LE16(p + 2); + s->noiseFloor = (Int16)LE16(p + 4); + s->lastRssi = (Int16)LE16(p + 6); + s->recv = LE32(p + 8); + s->sent = LE32(p + 12); + s->txAirSecs = LE32(p + 16); + s->upSecs = LE32(p + 20); + s->sentFlood = LE32(p + 24); + s->sentDirect = LE32(p + 28); + s->recvFlood = LE32(p + 32); + s->recvDirect = LE32(p + 36); + s->errEvents = LE16(p + 40); + s->lastSnrQ = (Int16)LE16(p + 42); + s->directDups = LE16(p + 44); + s->floodDups = LE16(p + 46); + /* later firmware adds these */ + s->rxAirSecs = len >= 8 + 52 ? LE32(p + 48) : 0; + s->recvErrors = len >= 8 + 56 ? LE32(p + 52) : 0; + return 1; +} + +Boolean VmParseTelemetry(const UInt8 *p, UInt16 len, UInt8 *prefix6, const UInt8 **data, UInt16 *dataLen) +{ + UInt16 i; + + if (len < 8 || p[0] != VM_PUSH_TELEMETRY_RESPONSE) + return 0; + for (i = 0; i < 6; i++) + prefix6[i] = p[2 + i]; + *data = p + 8; + *dataLen = (UInt16)(len - 8); + return 1; +} + +void VmFormatStatusLine(const VmRepeaterStats *s, UInt16 n, Char *o) +{ + *o = 0; + switch (n) { + case 0: + o = Put(o, "Battery "); + o = PutFixed(o, s->battMv, 3); + o = Put(o, "V, up "); + o = PutDuration(o, s->upSecs); + o = Put(o, ", queue "); + PutU(o, s->txQueue); + break; + case 1: + o = Put(o, "Noise "); + o = PutI(o, s->noiseFloor); + o = Put(o, "dBm, last "); + o = PutI(o, s->lastRssi); + o = Put(o, "dBm "); + o = PutFixed(o, s->lastSnrQ * 25L, 2); + Put(o, "dB"); + break; + case 2: + o = Put(o, "Recv "); + o = PutU(o, s->recv); + o = Put(o, " (flood "); + o = PutU(o, s->recvFlood); + o = Put(o, ", direct "); + o = PutU(o, s->recvDirect); + Put(o, ")"); + break; + case 3: + o = Put(o, "Sent "); + o = PutU(o, s->sent); + o = Put(o, " (flood "); + o = PutU(o, s->sentFlood); + o = Put(o, ", direct "); + o = PutU(o, s->sentDirect); + Put(o, ")"); + break; + case 4: + o = Put(o, "Airtime tx "); + o = PutDuration(o, s->txAirSecs); + o = Put(o, ", rx "); + PutDuration(o, s->rxAirSecs); + break; + case 5: + o = Put(o, "Dups flood "); + o = PutU(o, s->floodDups); + o = Put(o, ", direct "); + o = PutU(o, s->directDups); + o = Put(o, "; errors "); + o = PutU(o, s->recvErrors); + o = Put(o, ", events "); + PutU(o, s->errEvents); + break; + } +} + +/* Cayenne LPP: channel, type, value. Sizes and scales from MeshCore's LPPDataHelpers.h. */ +Boolean VmLppNext(const UInt8 *d, UInt16 len, UInt16 *pos, Char *o) +{ + UInt16 i = *pos, size; + UInt8 ch, type; + const UInt8 *v; + + if (i + 2 > len) + return 0; + ch = d[i]; + type = d[i + 1]; + if (ch == 0 && type == 0 && i + 2 >= len) + return 0; /* padding */ + switch (type) { + case 0: case 1: case 102: case 104: case 120: case 142: size = 1; break; + case 2: case 3: case 101: case 103: case 115: case 116: case 117: case 121: case 125: case 128: case 132: size = 2; break; + case 135: size = 3; break; + case 100: case 118: case 130: case 131: case 133: size = 4; break; + case 113: case 134: size = 6; break; + case 136: size = 9; break; + default: + o = Put(o, "Ch"); + o = PutU(o, ch); + o = Put(o, " type "); + o = PutU(o, type); + Put(o, ": not understood"); + *pos = len; + return 1; + } + if (i + 2 + size > len) + return 0; + v = d + i + 2; + o = Put(o, "Ch"); + o = PutU(o, ch); + o = Put(o, " "); + switch (type) { + case 0: o = Put(o, "Input "); PutU(o, v[0]); break; + case 1: o = Put(o, "Output "); PutU(o, v[0]); break; + case 2: o = Put(o, "Analog in "); PutFixed(o, BE(v, 2, 1), 2); break; + case 3: o = Put(o, "Analog out "); PutFixed(o, BE(v, 2, 1), 2); break; + case 100: o = Put(o, "Value "); PutU(o, (UInt32)BE(v, 4, 0)); break; + case 101: o = Put(o, "Light "); o = PutU(o, (UInt32)BE(v, 2, 0)); Put(o, " lux"); break; + case 102: o = Put(o, "Presence "); Put(o, v[0] ? "yes" : "no"); break; + case 103: o = Put(o, "Temperature "); o = PutFixed(o, BE(v, 2, 1), 1); Put(o, "\xb0" "C"); break; + case 104: o = Put(o, "Humidity "); o = PutFixed(o, v[0] * 5L, 1); Put(o, "%"); break; + case 113: o = Put(o, "Accel "); o = PutFixed(o, BE(v, 2, 1), 3); o = Put(o, " "); + o = PutFixed(o, BE(v + 2, 2, 1), 3); o = Put(o, " "); o = PutFixed(o, BE(v + 4, 2, 1), 3); + Put(o, "G"); break; + case 115: o = Put(o, "Pressure "); o = PutFixed(o, BE(v, 2, 0), 1); Put(o, "hPa"); break; + case 116: o = Put(o, "Voltage "); o = PutFixed(o, BE(v, 2, 0), 2); Put(o, "V"); break; + case 117: o = Put(o, "Current "); o = PutFixed(o, BE(v, 2, 0), 3); Put(o, "A"); break; + case 118: o = Put(o, "Frequency "); o = PutU(o, (UInt32)BE(v, 4, 0)); Put(o, "Hz"); break; + case 120: o = Put(o, "Level "); o = PutU(o, v[0]); Put(o, "%"); break; + case 121: o = Put(o, "Altitude "); o = PutI(o, BE(v, 2, 1)); Put(o, "m"); break; + case 125: o = Put(o, "Concentration "); o = PutU(o, (UInt32)BE(v, 2, 0)); Put(o, "ppm"); break; + case 128: o = Put(o, "Power "); o = PutU(o, (UInt32)BE(v, 2, 0)); Put(o, "W"); break; + case 130: o = Put(o, "Distance "); o = PutFixed(o, BE(v, 4, 0), 3); Put(o, "m"); break; + case 131: o = Put(o, "Energy "); o = PutFixed(o, BE(v, 4, 0), 3); Put(o, "kWh"); break; + case 132: o = Put(o, "Direction "); o = PutU(o, (UInt32)BE(v, 2, 0)); Put(o, "\xb0"); break; + case 133: o = Put(o, "Time "); PutU(o, (UInt32)BE(v, 4, 0)); break; + case 134: o = Put(o, "Gyro "); o = PutFixed(o, BE(v, 2, 1), 2); o = Put(o, " "); + o = PutFixed(o, BE(v + 2, 2, 1), 2); o = Put(o, " "); PutFixed(o, BE(v + 4, 2, 1), 2); break; + case 135: o = Put(o, "Colour "); o = PutU(o, v[0]); o = Put(o, ","); o = PutU(o, v[1]); + o = Put(o, ","); PutU(o, v[2]); break; + case 136: o = Put(o, "GPS "); o = PutFixed(o, BE(v, 3, 1), 4); o = Put(o, ", "); + o = PutFixed(o, BE(v + 3, 3, 1), 4); o = Put(o, " "); + o = PutFixed(o, BE(v + 6, 3, 1), 2); Put(o, "m"); break; + case 142: o = Put(o, "Switch "); Put(o, v[0] ? "on" : "off"); break; + } + *pos = (UInt16)(i + 2 + size); + return 1; +} diff --git a/palm-client/src/remote_fmt.h b/palm-client/src/remote_fmt.h new file mode 100644 index 0000000..584232d --- /dev/null +++ b/palm-client/src/remote_fmt.h @@ -0,0 +1,53 @@ +/* + * Remote administration of repeaters and rooms: request builders, the status + * and telemetry replies, and their text. Pure C (no Palm calls) so the host + * tests cover it. + */ +#ifndef VM_REMOTE_FMT_H +#define VM_REMOTE_FMT_H + +#include "sections.h" /* this module is in the third code segment */ +#include "protocol.h" + +#define VM_CMD_SEND_STATUS_REQ 27 +#define VM_CMD_SEND_TELEMETRY_REQ 39 +#define VM_PUSH_STATUS_RESPONSE 0x87 +#define VM_PUSH_TELEMETRY_RESPONSE 0x8B + +/* A repeater's RepeaterStats (simple_repeater/MyMesh.h), as REQ_TYPE_GET_STATUS returns it. */ +typedef struct { + UInt16 battMv; + UInt16 txQueue; + Int16 noiseFloor; + Int16 lastRssi; + UInt32 recv, sent, txAirSecs, upSecs; + UInt32 sentFlood, sentDirect, recvFlood, recvDirect; + UInt16 errEvents; + Int16 lastSnrQ; + UInt16 directDups, floodDups; + UInt32 rxAirSecs; + UInt32 recvErrors; +} VmRepeaterStats; +#define VM_STATUS_LINES 6 + +/* SEND_TXT_MSG with txt_type CLI: a command for a node we're logged into as admin. */ +UInt16 VmBuildSendCli(UInt8 *out, UInt16 outSize, const UInt8 *prefix6, UInt32 timestamp, + const UInt8 *text, UInt16 textLen) VM_SECT3; +UInt16 VmBuildStatusReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) VM_SECT3; +UInt16 VmBuildTelemetryReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) VM_SECT3; + +/* PUSH_STATUS_RESPONSE: code, 0, prefix[6], stats. */ +Boolean VmParseStatus(const UInt8 *p, UInt16 len, UInt8 *prefix6, VmRepeaterStats *out) VM_SECT3; +/* PUSH_TELEMETRY_RESPONSE: code, 0, prefix[6], Cayenne LPP data. */ +Boolean VmParseTelemetry(const UInt8 *p, UInt16 len, UInt8 *prefix6, const UInt8 **data, UInt16 *dataLen) VM_SECT3; + +/* Line n (0 .. VM_STATUS_LINES-1) of a status report. */ +void VmFormatStatusLine(const VmRepeaterStats *s, UInt16 n, Char *out) VM_SECT3; +/* + * The next Cayenne LPP reading at *pos as text ("1 Voltage 4.12V"); advances + * *pos. False at the end, or for a type it doesn't know (whose size is + * unknown, so nothing after it can be read). + */ +Boolean VmLppNext(const UInt8 *d, UInt16 len, UInt16 *pos, Char *out) VM_SECT3; + +#endif diff --git a/palm-client/src/remote_form.c b/palm-client/src/remote_form.c new file mode 100644 index 0000000..51dd7d0 --- /dev/null +++ b/palm-client/src/remote_form.c @@ -0,0 +1,172 @@ +/* + * The admin console for a repeater or room we're logged into: a transcript of + * CLI commands and replies, a command line, Status and Telemetry requests, + * and Cmds, a list of common commands (those ending in "..." go into the + * command line to finish; the others are sent at once). + */ +#include +#include "resources.h" +#include "session.h" +#include "contact_form.h" +#include "remote.h" +#include "ui/draw.h" +#include "ui/rowlist.h" +#include "remote_form.h" + +/* In an extra code segment (VM_SECT3): see sections.h for the rules. */ +static void *GetObject(FormType *form, UInt16 id) VM_SECT3; +static void DrawRow(UInt16 row, const RectangleType *b, void *ctx) VM_SECT3; +static void Reload(Boolean draw) VM_SECT3; +static void SendTyped(FormType *form) VM_SECT3; +static void PickCommand(FormType *form) VM_SECT3; + +/* the settings "Read settings" asks for */ +#define READ_SETTINGS "get name|get radio|get tx|get repeat|get advert.interval|get flood.advert.interval|get flood.max|get path.hash.mode|ver|clock" + +static RowList gList; +static Char gTitle[VM_NAME_SIZE + 2]; + +static void *GetObject(FormType *form, UInt16 id) +{ + return FrmGetObjectPtr(form, FrmGetObjectIndex(form, id)); +} + +static void DrawRow(UInt16 row, const RectangleType *b, void *ctx) +{ + const VmRLine *l = RemoteLine(row); + FontID old = FntSetFont(vmSmallFont); + Coord x = b->topLeft.x + 1, y = b->topLeft.y; + Char mark; + + switch (l->kind) { + case VM_RL_CMD: + mark = GLYPH_FORWARD; + WinDrawChars(&mark, 1, x, y); + WinDrawChars(l->text, StrLen(l->text), x + 6, y); + /* underline our commands so they stand apart from the answers */ + WinDrawGrayLine(x + 6, y + VM_ROW_H - 1, x + 6 + FntCharsWidth(l->text, StrLen(l->text)), y + VM_ROW_H - 1); + break; + case VM_RL_NOTE: + WinDrawGrayLine(x, y + VM_ROW_H / 2, x + 3, y + VM_ROW_H / 2); + WinDrawChars(l->text, StrLen(l->text), x + 6, y); + break; + default: + WinDrawChars(l->text, StrLen(l->text), x + 6, y); + break; + } + FntSetFont(old); +} + +static void Reload(Boolean draw) +{ + RowListSetCount(&gList, RemoteLineCount()); + RowListScrollToEnd(&gList); + if (draw) + RowListDraw(&gList); +} + +static void SendTyped(FormType *form) +{ + FieldType *f = GetObject(form, RemoteField); + const Char *t = FldGetTextPtr(f); + + if (!t || !*t) + return; + RemoteCommand(t); + FldDelete(f, 0, FldGetTextLength(f)); + Reload(true); +} + +static void PickCommand(FormType *form) +{ + ListType *list = GetObject(form, RemoteCmdList); + Int16 pick = LstPopupList(list); + const Char *item; + UInt16 len; + + if (pick < 0) + return; + item = LstGetSelectionText(list, pick); + len = StrLen(item); + if (pick == 0) { + RemoteBatch(READ_SETTINGS); + } else if (len > 3 && StrCompare(item + len - 3, "...") == 0) { + /* to finish typing: "set tx..." -> "set tx " */ + FieldType *f = GetObject(form, RemoteField); + + FldDelete(f, 0, FldGetTextLength(f)); + FldInsert(f, item, len - 3); + if (item[len - 4] != ' ') + FldInsert(f, " ", 1); + FrmSetFocus(form, FrmGetObjectIndex(form, RemoteField)); + return; + } else { + RemoteCommand(item); + } + Reload(true); +} + +Boolean RemoteFormHandleEvent(EventType *event) +{ + FormType *form = FrmGetActiveForm(); + Int16 tapped = -1; + + switch (event->eType) { + case frmOpenEvent: + StrNCopy(gTitle, RemoteTarget()->name, VM_NAME_SIZE - 1); + gTitle[VM_NAME_SIZE - 1] = 0; + FrmCopyTitle(form, gTitle); + RowListInit(&gList, RemoteGadget, RemoteScroll, VM_ROW_H, DrawRow, NULL, NULL); + Reload(false); + FrmDrawForm(form); + RowListDraw(&gList); + FrmSetFocus(form, FrmGetObjectIndex(form, RemoteField)); + if (RemoteLineCount() == 0) { + if (!SessionIsAdmin(RemoteTarget()->pubKey)) + RemoteBatch("ver"); /* a guest gets no answer: the note says so */ + else + RemoteBatch(READ_SETTINGS); + Reload(true); + } + return true; + + case frmUpdateEvent: + if (event->data.frmUpdate.updateCode == frmRedrawUpdateCode) + FrmDrawForm(form); + Reload(true); + return true; + + case keyDownEvent: + if (event->data.keyDown.chr == linefeedChr || event->data.keyDown.chr == crChr) { + SendTyped(form); + return true; + } + break; + + case ctlSelectEvent: + switch (event->data.ctlSelect.controlID) { + case RemoteSendButton: + SendTyped(form); + return true; + case RemoteStatusButton: + RemoteStatus(); + Reload(true); + return true; + case RemoteTelemButton: + RemoteTelemetry(); + Reload(true); + return true; + case RemoteCmdsButton: + PickCommand(form); + return true; + case RemoteDoneButton: + ContactFormOpen(RemoteTarget()); + return true; + } + break; + + default: + break; + } + return RowListHandleEvent(&gList, event, &tapped); +} diff --git a/palm-client/src/remote_form.h b/palm-client/src/remote_form.h new file mode 100644 index 0000000..4a38745 --- /dev/null +++ b/palm-client/src/remote_form.h @@ -0,0 +1,9 @@ +#ifndef VM_REMOTE_FORM_H +#define VM_REMOTE_FORM_H + +#include +#include "sections.h" /* this module is in the third code segment */ + +Boolean RemoteFormHandleEvent(EventType *event) VM_SECT3; + +#endif diff --git a/palm-client/src/resources.h b/palm-client/src/resources.h index 160fd78..e91ec46 100644 --- a/palm-client/src/resources.h +++ b/palm-client/src/resources.h @@ -71,6 +71,17 @@ #define ContactAddCancelButton 1907 #define ContactAddTypeGroup 4 +#define RemoteForm 2100 +#define RemoteGadget 2101 +#define RemoteScroll 2102 +#define RemoteField 2103 +#define RemoteSendButton 2104 +#define RemoteStatusButton 2105 +#define RemoteTelemButton 2106 +#define RemoteCmdsButton 2107 +#define RemoteDoneButton 2108 +#define RemoteCmdList 2109 + #define PasswordAlert 2007 #define RemoveContactAlert 2008 diff --git a/palm-client/src/session.c b/palm-client/src/session.c index 94d029e..ca8d0ec 100644 --- a/palm-client/src/session.c +++ b/palm-client/src/session.c @@ -7,6 +7,7 @@ #include "ui/text.h" #include "session.h" #include "heard.h" +#include "remote.h" #define VM_APP_NAME "VisorMesh" #define VM_REPLY_TIMEOUT (2 * SysTicksPerSecond()) @@ -70,6 +71,12 @@ static void HandlePush(void) } else if (code == VM_PUSH_SEND_CONFIRMED) { if (VmParseSendConfirmed(gDecoder.payload, gDecoder.len, &ack, &trip) && MessagesAcked(ack)) gChanged |= VM_CHANGED_MESSAGES; + } else if (code == VM_PUSH_STATUS_RESPONSE) { + if (RemoteOnStatus(gDecoder.payload, gDecoder.len)) + gChanged |= VM_CHANGED_REMOTE; + } else if (code == VM_PUSH_TELEMETRY_RESPONSE) { + if (RemoteOnTelemetry(gDecoder.payload, gDecoder.len)) + gChanged |= VM_CHANGED_REMOTE; } else if (code == VM_PUSH_LOG_RX) { VmRxLog rx; @@ -386,7 +393,15 @@ Err SessionSyncMessages(void) return err; if (gDecoder.payload[0] == VM_RESP_NO_MORE_MESSAGES) return errNone; - if (VmParseMessage(gDecoder.payload, gDecoder.len, &m) && MessagesAddIncoming(&m, SessionNow()) == errNone) { + if (!VmParseMessage(gDecoder.payload, gDecoder.len, &m)) + continue; + if (!m.isChannel && m.txtType == VM_TXT_TYPE_CLI_DATA) { + /* a CLI reply: for the admin console, not the conversation */ + RemoteOnCli(m.senderPrefix, m.text, m.textLen); + gChanged |= VM_CHANGED_REMOTE; + continue; + } + if (MessagesAddIncoming(&m, SessionNow()) == errNone) { gChanged |= VM_CHANGED_MESSAGES; if (!m.isChannel || MentionsUs(&m)) NoteAlert(&m); @@ -496,8 +511,19 @@ UInt16 SessionPoll(void) /* Rooms logged into this session (key prefixes); the firmware keeps them alive. */ #define VM_MAX_LOGINS 4 static UInt8 gLogins[VM_MAX_LOGINS][6]; +static Boolean gLoginAdmin[VM_MAX_LOGINS]; static UInt16 gLoginCount = 0; +Boolean SessionIsAdmin(const UInt8 *prefix6) +{ + UInt16 i; + + for (i = 0; i < gLoginCount; i++) + if (MemCmp(gLogins[i], prefix6, 6) == 0) + return gLoginAdmin[i]; + return false; +} + Boolean SessionIsLoggedIn(const UInt8 *prefix6) { UInt16 i; @@ -540,11 +566,30 @@ Err SessionLogin(const UInt8 *pubKey, const Char *password, Boolean *isAdmin) return vmErrBadReply; if (!ok) return vmErrLoginFailed; - if (!SessionIsLoggedIn(prefix) && gLoginCount < VM_MAX_LOGINS) + for (n = 0; n < gLoginCount && MemCmp(gLogins[n], prefix, 6) != 0; n++) + ; + if (n == gLoginCount && gLoginCount < VM_MAX_LOGINS) MemMove(gLogins[gLoginCount++], prefix, 6); + if (n < gLoginCount) + gLoginAdmin[n] = *isAdmin; return errNone; } +Err SessionRequestSent(const UInt8 *frame, UInt16 len, VmSent *sent) +{ + static const UInt8 sentCode[] = { VM_RESP_SENT }; + Err err; + + if (!gConnected) + return serErrNotOpen; + if (len == 0) + return vmErrTooLong; + err = RequestAny(frame, len, sentCode, 1, 1); + if (err == errNone && !VmParseSent(gDecoder.payload, gDecoder.len, sent)) + err = vmErrBadReply; + return err; +} + Err SessionKeyCommand(UInt8 code, const UInt8 *pubKey) { UInt8 frame[VM_HEADER_LEN + 1 + VM_PUB_KEY_SIZE]; diff --git a/palm-client/src/session.h b/palm-client/src/session.h index 1cf3c5f..bbb240e 100644 --- a/palm-client/src/session.h +++ b/palm-client/src/session.h @@ -20,6 +20,7 @@ #define VM_CHANGED_MESSAGES 0x0001 #define VM_CHANGED_CONTACTS 0x0002 #define VM_CHANGED_ALERT 0x0004 /* a new DM or mention of us arrived: see SessionAlertConv */ +#define VM_CHANGED_REMOTE 0x0008 /* the admin console has new lines */ /* Opens the link and runs DEVICE_QUERY + APP_START. On error the link is closed. */ @@ -92,6 +93,9 @@ Err SessionSendAdvert(Boolean flood); */ Err SessionLogin(const UInt8 *pubKey, const Char *password, Boolean *isAdmin); Boolean SessionIsLoggedIn(const UInt8 *prefix6); +Boolean SessionIsAdmin(const UInt8 *prefix6); +/* Sends a request whose first reply is RESP_SENT (CLI, status, telemetry); the answer comes later as a push. */ +Err SessionRequestSent(const UInt8 *frame, UInt16 len, VmSent *sent); /* VM_CMD_RESET_PATH, VM_CMD_REMOVE_CONTACT, VM_CMD_LOGOUT for a full key; needs RESP_OK. */ Err SessionKeyCommand(UInt8 code, const UInt8 *pubKey); /* Adds a contact the radio hasn't heard yet, by its full key (unknown path). */ diff --git a/palm-client/tests/test_protocol.c b/palm-client/tests/test_protocol.c index cb29a47..d2d4c06 100644 --- a/palm-client/tests/test_protocol.c +++ b/palm-client/tests/test_protocol.c @@ -2,6 +2,7 @@ #include #include #include "../src/protocol.h" +#include "../src/remote_fmt.h" #include "fixtures.h" #include "../src/sha256.h" @@ -303,6 +304,60 @@ static void test_rx_log(void) CHECK(!VmParseRxLog(advert2, 7, &rx)); /* path cut off */ } +static void test_remote(void) +{ + UInt8 out[200], key[32], prefix[6], st[8 + 56]; + VmRepeaterStats s; + char line[64]; + const UInt8 *data; + UInt16 dlen, pos, i; + VmFrameDecoder d; + /* ch1 voltage 4.12 V, ch2 temp -3.5 C, ch3 humidity 41.5%, ch4 GPS 39.9526,-75.1652 12.34m */ + static const UInt8 lpp[] = { 1, 116, 0x01, 0x9c, 2, 103, 0xff, 0xdd, 3, 104, 83, + 4, 136, 0x06, 0x18, 0xa6, 0xf4, 0x87, 0xdc, 0x00, 0x04, 0xd2, 5, 77, 1 }; + + for (i = 0; i < 32; i++) + key[i] = (UInt8)(0x40 + i); + CHECK(VmBuildSendCli(out, sizeof(out), key, 0x12345678UL, (const UInt8 *)"get tx", 6) == 3 + 13 + 6); + CHECK(out[3] == 2 && out[4] == 1 && out[5] == 0 && out[10] == 0x40 && memcmp(out + 16, "get tx", 6) == 0); + CHECK(VmBuildStatusReq(out, sizeof(out), key) == 3 + 33 && out[3] == 27 && out[4] == 0x40); + CHECK(VmBuildTelemetryReq(out, sizeof(out), key) == 3 + 36 && out[3] == 39 && out[4] == 0 && out[7] == 0x40); + + memset(st, 0, sizeof(st)); + st[0] = 0x87; st[2] = 0xaa; st[7] = 0xff; + st[8] = 0x1c; st[9] = 0x10; /* 4124 mV */ + st[12] = 0x92; st[13] = 0xff; /* noise -110 */ + st[28] = 0x10; st[29] = 0x0e; st[30] = 0x04; /* up 265744 s = 3d 1h */ + st[50] = 0xe8; st[51] = 0xff; /* snr -24/4 = -6.00 */ + st[16] = 0xd2; st[17] = 0x04; /* recv 1234 */ + CHECK(VmParseStatus(st, sizeof(st), prefix, &s) && prefix[0] == 0xaa && prefix[5] == 0xff); + CHECK(s.battMv == 4124 && s.noiseFloor == -110 && s.upSecs == 265744UL && s.lastSnrQ == -24 && s.recv == 1234); + VmFormatStatusLine(&s, 0, line); + CHECK(strcmp(line, "Battery 4.124V, up 3d 1h, queue 0") == 0); + VmFormatStatusLine(&s, 1, line); + CHECK(strcmp(line, "Noise -110dBm, last 0dBm -6.00dB") == 0); + VmFormatStatusLine(&s, 2, line); + CHECK(strcmp(line, "Recv 1234 (flood 0, direct 0)") == 0); + CHECK(!VmParseStatus(st, 40, prefix, &s)); + + { + UInt8 t[8 + sizeof(lpp)]; + + t[0] = 0x8b; t[1] = 0; + memcpy(t + 2, key, 6); + memcpy(t + 8, lpp, sizeof(lpp)); + CHECK(VmParseTelemetry(t, sizeof(t), prefix, &data, &dlen) && dlen == sizeof(lpp) && prefix[0] == 0x40); + } + pos = 0; + CHECK(VmLppNext(lpp, sizeof(lpp), &pos, line) && strcmp(line, "Ch1 Voltage 4.12V") == 0); + CHECK(VmLppNext(lpp, sizeof(lpp), &pos, line) && strcmp(line, "Ch2 Temperature -3.5\xb0" "C") == 0); + CHECK(VmLppNext(lpp, sizeof(lpp), &pos, line) && strcmp(line, "Ch3 Humidity 41.5%") == 0); + CHECK(VmLppNext(lpp, sizeof(lpp), &pos, line) && strcmp(line, "Ch4 GPS 39.9526, -75.1652 12.34m") == 0); + CHECK(VmLppNext(lpp, sizeof(lpp), &pos, line) && strcmp(line, "Ch5 type 77: not understood") == 0); + CHECK(!VmLppNext(lpp, sizeof(lpp), &pos, line)); + (void)d; +} + static void test_path_len(void) { CHECK(VM_PATH_HOPS(0x00) == 0 && VM_PATH_HASH_BYTES(0x00) == 1); @@ -501,6 +556,7 @@ int main(void) test_sha256_and_channels(); test_contacts_and_rooms(); test_rx_log(); + test_remote(); test_messaging(); test_parse_self_info(); test_parse_contacts();