diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index 8b8d1f1..fe76d62 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -352,6 +352,7 @@ BEGIN 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" + "region" "region get..." "region allowf..." "region denyf..." "region save" "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..." diff --git a/palm-client/src/map_form.c b/palm-client/src/map_form.c index f73a6a4..54f01c0 100644 --- a/palm-client/src/map_form.c +++ b/palm-client/src/map_form.c @@ -5,6 +5,7 @@ #include "contact_form.h" #include "session.h" #include "map_geo.h" +#include "neigh.h" #include "map_form.h" #include "ui/draw.h" #include "ui/text.h" @@ -16,6 +17,8 @@ static BitmapType *Tile(UInt16 rec) VM_SECT4; static void ClampCenter(void) VM_SECT4; static void Draw(void) VM_SECT4; static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy) VM_SECT4; +static Int16 NodeByKey(const UInt8 *key) VM_SECT4; +static void DrawNeighbours(const RectangleType *r, Int32 vx, Int32 vy) VM_SECT4; static void DrawInfo(const RectangleType *r) VM_SECT4; static void DrawFooter(void) VM_SECT4; static void Halo(const Char *s, UInt16 len, Coord x, Coord y) VM_SECT4; @@ -40,6 +43,7 @@ typedef struct { UInt8 type; Boolean direct; /* a 0-hop neighbour */ UInt32 lastAdvert; + UInt8 key[4]; /* for the neighbour lines (neigh.h) */ Char name[NAME_LEN + 1]; } MapNode; @@ -179,6 +183,7 @@ static void LoadNodes(void) n->type = c.type; n->direct = c.outPathLen != 0xFF && (c.outPathLen & 0x3F) == 0; n->lastAdvert = c.lastAdvert; + MemMove(n->key, c.pubKey, 4); StrNCopy(n->name, c.name, NAME_LEN); n->name[NAME_LEN] = 0; gNodeCount++; @@ -243,6 +248,50 @@ static void Halo(const Char *s, UInt16 len, Coord x, Coord y) WinPaintChars(s, len, x, y); } +static Int16 NodeByKey(const UInt8 *key) +{ + UInt16 i; + + for (i = 0; i < gNodeCount; i++) + if (MemCmp(gNodes[i].key, key, 4) == 0) + return (Int16)i; + return -1; +} + +/* + * The last neighbour list a repeater's console showed: a line to each + * neighbour with a position, solid if the repeater hears it well (SNR 0 dB + * or better), grey if not. Draw has set the clip to the map. + */ +static void DrawNeighbours(const RectangleType *r, Int32 vx, Int32 vy) +{ + VmNeighbour list[VM_NEIGH_MAX]; + UInt8 rep[6]; + UInt16 n = NeighGet(rep, list), i; + Int16 from, to; + Int32 x0, y0, x1, y1; + + if (n == 0 || (from = NodeByKey(rep)) < 0) + return; + VmMapToZoom(&gH, gZi, gNodes[from].wx, gNodes[from].wy, &x0, &y0); + x0 += r->topLeft.x - vx; + y0 += r->topLeft.y - vy; + for (i = 0; i < n; i++) { + if ((to = NodeByKey(list[i].prefix)) < 0) + continue; + VmMapToZoom(&gH, gZi, gNodes[to].wx, gNodes[to].wy, &x1, &y1); + x1 += r->topLeft.x - vx; + y1 += r->topLeft.y - vy; + /* far off-screen ends would overflow Coord */ + if (x1 < -2000 || x1 > 2000 || y1 < -2000 || y1 > 2000 || x0 < -2000 || x0 > 2000 || y0 < -2000 || y0 > 2000) + continue; + if (list[i].snrQ >= 0) + WinDrawLine((Coord)x0, (Coord)y0, (Coord)x1, (Coord)y1); + else + WinDrawGrayLine((Coord)x0, (Coord)y0, (Coord)x1, (Coord)y1); + } +} + static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy) { RectangleType labels[MAX_NODES / 4]; @@ -366,6 +415,7 @@ static void Draw(void) WinEraseRectangle(&t, 0); } } + DrawNeighbours(&r, vx, vy); DrawNodes(&r, vx, vy); DrawInfo(&r); WinResetClip(); diff --git a/palm-client/src/neigh.c b/palm-client/src/neigh.c new file mode 100644 index 0000000..5b3591b --- /dev/null +++ b/palm-client/src/neigh.c @@ -0,0 +1,43 @@ +#include +#include "session.h" +#include "protocol2.h" +#include "neigh.h" + +static UInt8 gRepeater[6]; +static VmNeighbour gList[VM_NEIGH_MAX]; +static UInt16 gCount = 0; +static UInt32 gAt = 0; + +Boolean NeighNote(const UInt8 *repeater6, const Char *text, UInt16 len) +{ + VmNeighbour list[VM_NEIGH_MAX]; + UInt16 start = 0, end, n = 0; + UInt32 secs; + + while (start < len) { + for (end = start; end < len && text[end] != '\n'; end++) + ; + if (n < VM_NEIGH_MAX) { + if (!VmParseNeighbour(text + start, end - start, list[n].prefix, &secs, &list[n].snrQ)) + return false; /* any other line: not a neighbour list */ + n++; + } + start = end + 1; + } + if (n == 0) + return false; + MemMove(gRepeater, repeater6, 6); + MemMove(gList, list, n * sizeof(VmNeighbour)); + gCount = n; + gAt = SessionNow(); + return true; +} + +UInt16 NeighGet(UInt8 *repeater6, VmNeighbour *out) +{ + if (!gCount || SessionNow() - gAt > VM_NEIGH_SECS) + return 0; + MemMove(repeater6, gRepeater, 6); + MemMove(out, gList, gCount * sizeof(VmNeighbour)); + return gCount; +} diff --git a/palm-client/src/neigh.h b/palm-client/src/neigh.h new file mode 100644 index 0000000..6ffff3b --- /dev/null +++ b/palm-client/src/neigh.h @@ -0,0 +1,25 @@ +/* + * A repeater's neighbours, from the last "neighbors" reply in its admin + * console, for the Map: lines from the repeater to each neighbour that has a + * position, shaded by SNR. Kept in memory for VM_NEIGH_SECS. + */ +#ifndef VM_NEIGH_H +#define VM_NEIGH_H + +#include +#include "sections.h" /* this module is in the fifth code segment */ + +#define VM_NEIGH_MAX 16 +#define VM_NEIGH_SECS (30UL * 60UL) + +typedef struct { + UInt8 prefix[4]; + Int16 snrQ; +} VmNeighbour; + +/* A CLI reply from the repeater with this key prefix: true if it was a neighbour list (stored). */ +Boolean NeighNote(const UInt8 *repeater6, const Char *text, UInt16 len) VM_SECT5; +/* The list if one is recent: fills repeater6 and up to VM_NEIGH_MAX entries, returns how many. */ +UInt16 NeighGet(UInt8 *repeater6, VmNeighbour *out) VM_SECT5; + +#endif diff --git a/palm-client/src/protocol2.c b/palm-client/src/protocol2.c index a008f99..4f10be8 100644 --- a/palm-client/src/protocol2.c +++ b/palm-client/src/protocol2.c @@ -14,6 +14,7 @@ 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 Boolean Number(const Char *s, UInt16 len, UInt16 *i, Int32 *out) VM_SECT5; static UInt16 Get16(const UInt8 *p) { @@ -432,3 +433,39 @@ Boolean VmHashtagAt(const Char *text, UInt16 len, UInt16 k, Char *out, UInt16 si out[end - start] = 0; return 1; } + +/* Optional '-', digits, from *i; leaves *i after them. */ +static Boolean Number(const Char *s, UInt16 len, UInt16 *i, Int32 *out) +{ + Boolean neg = 0, any = 0; + Int32 v = 0; + + if (*i < len && s[*i] == '-') { + neg = 1; + (*i)++; + } + while (*i < len && s[*i] >= '0' && s[*i] <= '9') { + v = v * 10 + (s[*i] - '0'); + (*i)++; + any = 1; + } + *out = neg ? -v : v; + return any; +} + +Boolean VmParseNeighbour(const Char *s, UInt16 len, UInt8 *prefix4, UInt32 *secsAgo, Int16 *snrQ) +{ + UInt16 i = 9; + Int32 secs, snr; + + if (len < 13 || s[8] != ':' || !HexBytes(s, 8, prefix4, 4)) + return 0; + if (!Number(s, len, &i, &secs) || secs < 0 || i >= len || s[i] != ':') + return 0; + i++; + if (!Number(s, len, &i, &snr) || i != len) + return 0; + *secsAgo = (UInt32)secs; + *snrQ = (Int16)snr; + return 1; +} diff --git a/palm-client/src/protocol2.h b/palm-client/src/protocol2.h index 2e53974..1341b56 100644 --- a/palm-client/src/protocol2.h +++ b/palm-client/src/protocol2.h @@ -109,4 +109,11 @@ Boolean VmShareParse(const Char *s, UInt16 len, VmShare *out) VM_SECT5; */ Boolean VmHashtagAt(const Char *text, UInt16 len, UInt16 k, Char *out, UInt16 size) VM_SECT5; +/* + * One line of a repeater's "neighbors" reply, "a1b2c3d4:120:-14": a + * neighbour's first 4 key bytes, seconds since heard, SNR in quarter dB. + * len stops at the line's end; returns false if it isn't one. + */ +Boolean VmParseNeighbour(const Char *s, UInt16 len, UInt8 *prefix4, UInt32 *secsAgo, Int16 *snrQ) VM_SECT5; + #endif diff --git a/palm-client/src/remote.c b/palm-client/src/remote.c index 8c73335..0eb5518 100644 --- a/palm-client/src/remote.c +++ b/palm-client/src/remote.c @@ -5,6 +5,7 @@ #include "ui/fontmetrics.h" #include "app.h" #include "remote.h" +#include "neigh.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; @@ -235,6 +236,8 @@ Boolean RemoteOnCli(const UInt8 *prefix6, const UInt8 *utf8, UInt16 len) return false; n = TxtUtf8ToPalm(utf8, len, text, sizeof(text)); AddText(VM_RL_REPLY, text, n); + if (NeighNote(prefix6, text, n)) + AddText(VM_RL_NOTE, "(on the Map for 30 min)", 23); if (gPending == REQ_CLI) Done(); return true; diff --git a/palm-client/tests/test_protocol2.c b/palm-client/tests/test_protocol2.c index 03ffdeb..95c2c59 100644 --- a/palm-client/tests/test_protocol2.c +++ b/palm-client/tests/test_protocol2.c @@ -141,8 +141,22 @@ static void test_hashtag(void) CHECK(!VmHashtagAt(t, (UInt16)strlen(t), 5, out, 5)); /* too long for out */ } +static void test_neighbour(void) +{ + UInt8 k[4]; + UInt32 secs; + Int16 snr; + + CHECK(VmParseNeighbour("a1B2c3d4:120:-14", 16, k, &secs, &snr) && k[1] == 0xb2 && secs == 120 && snr == -14); + CHECK(VmParseNeighbour("a1b2c3d4:5:22\nffff", 13, k, &secs, &snr) && snr == 22); + CHECK(!VmParseNeighbour("-none-", 6, k, &secs, &snr)); + CHECK(!VmParseNeighbour("a1b2c3d4:x:1", 12, k, &secs, &snr)); + CHECK(!VmParseNeighbour("a1b2c3d4:12:1z", 14, k, &secs, &snr)); +} + int main(void) { + test_neighbour(); test_trace(); test_stats(); test_route();