palm-client: flood scopes (list, radio default, per-channel scope, discover from neighbouring repeaters)
This commit is contained in:
1 parent
1c3c9c3b25
commit
7c4473bcb5
13 files changed
+1000
-2
No files matched your search
+3
-1
@@ -39,7 +39,8 @@
|
||||
- [ ] UI: contacts, DMs, channels
|
||||
- [x] Install method confirmed: HotSync over the USB cradle from the dev Mac (`make install`)
|
||||
- [x] Notification sounds per event kind, mute channels, larger text option (5x8 font)
|
||||
- [ ] Flood scopes (regions): manage the list of scopes, set the default scope, and set a scope per channel. MeshCore v8+ has CMD_SET_FLOOD_SCOPE_KEY (54), CMD_SET_DEFAULT_FLOOD_SCOPE (63) / CMD_GET_DEFAULT_FLOOD_SCOPE (64)
|
||||
- [x] Flood scopes (regions): manage the list of scopes, set the default scope, and set a scope per channel; discover from neighbouring repeaters. MeshCore v8+ has CMD_SET_FLOOD_SCOPE_KEY (54), CMD_SET_DEFAULT_FLOOD_SCOPE (63) / CMD_GET_DEFAULT_FLOOD_SCOPE (64)
|
||||
- [ ] Region discovery beyond neighbours: keep each contact's out_path and send the reversed path as the reply path
|
||||
|
||||
## Open questions
|
||||
- Which CF adapter is it, and does its ROM include an app installer?
|
||||
@@ -85,3 +86,4 @@
|
||||
- 2026-11-02: Notification sounds (Options > Sounds): separate tunes for direct messages, mentions, room posts, private / hashtag / Public channels and new contacts (PUSH_NEW_ADVERT), each on or off, at the alarm volume. One sound per poll, by priority; none for the conversation on screen. Verified on the Neo. Segment 3 is 13.8 KB.
|
||||
- 2026-11-02: SB-LoRa receive is interrupt-driven: a slot IRQ* handler (hsCardAttrIntHandler) drains the SC16IS740 RX FIFO (IER RHR, trigger 8 or timeout) into a 1 KB ring passed as cardParam. The handler path (SbDrainRx and the bus core) is in segment 1 with no A5 references, checked in the disassembly, so it's safe in interrupt context and while the Visor sleeps. The app masks the slot interrupt around its own SPI transactions, and falls back to polling if the handler won't install. Host tests cover draining, ring wrap and overflow; untested on hardware.
|
||||
- 2026-11-02: Mute Channel (Options menu; by channel key; mentions still sound; open-circle unread badge, left out of the title total) and Text size Small/Large on the new Preferences screen (5x8 misc-fixed via tools/pcf2bdf.py from XQuartz's PCF; gen_font.py builds both fonts). Verified on the Neo. Segments: 11.1 / 28.0 / 14.7 KB.
|
||||
- 2026-11-02: Flood scopes: a list of scope names (Options > Flood Scopes), the radio's default (Set Default / No Default), a per-channel choice (Options > Channel Scope: radio default, none, or a named scope), and Discover, which asks neighbouring repeaters for their regions (SEND_ANON_REQ type 0x01). The session sets the scope override before each flood send and resets it on disconnect. Keys and frames are host-tested (tests/test_scopes.c; the key for "test" matches the radio's #test channel key). Verified on the Neo. Discover only reaches 0-hop repeaters for now.
|
||||
@@ -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/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/fontmetrics.c src/ports.c src/sounds.c src/mutes.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/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c
|
||||
OBJS = $(SRCS:src/%.c=build/%.o)
|
||||
|
||||
CC = m68k-palmos-gcc
|
||||
@@ -76,6 +76,8 @@ test: | build
|
||||
build/test_text
|
||||
gcc -std=c99 -Wall -Wextra -o build/test_sblora tests/test_sblora.c src/sb_spi.c
|
||||
build/test_sblora
|
||||
gcc -std=c99 -Wall -Wextra -o build/test_scopes tests/test_scopes.c src/scope_proto.c src/protocol.c src/sha256.c
|
||||
build/test_scopes
|
||||
|
||||
build:
|
||||
mkdir -p build
|
||||
|
||||
@@ -21,6 +21,8 @@ BEGIN
|
||||
MENUITEM "Add Channel..." ID MainMenuAddChannel "A"
|
||||
MENUITEM "Leave Channel" ID MainMenuLeaveChannel "L"
|
||||
MENUITEM "Mute Channel" ID MainMenuMuteChannel "M"
|
||||
MENUITEM "Channel Scope..." ID MainMenuChannelScope "E"
|
||||
MENUITEM "Flood Scopes..." ID MainMenuScopes "O"
|
||||
MENUITEM "-" AUTOID
|
||||
MENUITEM "Preferences..." ID MainMenuSounds "R"
|
||||
MENUITEM "-" AUTOID
|
||||
@@ -285,6 +287,45 @@ BEGIN
|
||||
BUTTON "Done" ID SoundsDoneButton AT (CENTER 145 AUTO AUTO)
|
||||
END
|
||||
|
||||
/* Flood scopes: the list, and the radio's default */
|
||||
FORM ID ScopesForm AT (0 0 160 160)
|
||||
NOFRAME
|
||||
USABLE
|
||||
BEGIN
|
||||
TITLE "Flood Scopes"
|
||||
LABEL "Radio default:" AUTOID AT (5 17) FONT 1
|
||||
LABEL " " ID ScopesDefaultLabel AT (75 17)
|
||||
LIST "" ID ScopesList AT (5 31 150 AUTO) VISIBLEITEMS 6
|
||||
FIELD ID ScopesField AT (5 100 68 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 31
|
||||
BUTTON "Add" ID ScopesAddButton AT (78 99 30 12)
|
||||
BUTTON "Discover" ID ScopesDiscoverButton AT (PREVRIGHT+4 PREVTOP 43 12)
|
||||
BUTTON "Delete" ID ScopesDeleteButton AT (5 116 36 12)
|
||||
BUTTON "Set Default" ID ScopesSetDefaultButton AT (PREVRIGHT+4 PREVTOP 52 12)
|
||||
BUTTON "No Default" ID ScopesClearDefaultButton AT (PREVRIGHT+4 PREVTOP 52 12)
|
||||
BUTTON "Done" ID ScopesDoneButton AT (CENTER 145 AUTO AUTO)
|
||||
GRAFFITISTATEINDICATOR AT (150 148)
|
||||
END
|
||||
|
||||
ALERT ID ScopeAlert
|
||||
INFORMATION
|
||||
BEGIN
|
||||
TITLE "Flood Scopes"
|
||||
MESSAGE "^1"
|
||||
BUTTONS "OK"
|
||||
END
|
||||
|
||||
/* Channel Scope: which scope this channel's messages flood in */
|
||||
FORM ID ChanScopeForm AT (2 28 156 130)
|
||||
MODAL
|
||||
DEFAULTBTNID ChanScopeCancelButton
|
||||
BEGIN
|
||||
TITLE "Channel Scope"
|
||||
LABEL "Flood this channel in:" AUTOID AT (5 15)
|
||||
LIST "" ID ChanScopeList AT (5 28 146 AUTO) VISIBLEITEMS 7
|
||||
BUTTON "OK" ID ChanScopeOKButton AT (5 113 36 12)
|
||||
BUTTON "Cancel" ID ChanScopeCancelButton AT (PREVRIGHT+5 PREVTOP 36 12)
|
||||
END
|
||||
|
||||
ALERT ID MuteAlert
|
||||
INFORMATION
|
||||
BEGIN
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "app.h"
|
||||
#include "sounds.h"
|
||||
#include "mutes.h"
|
||||
#include "scopes.h"
|
||||
|
||||
VmConv gAppConv;
|
||||
Boolean gAppAutoConnectTried = false;
|
||||
@@ -29,6 +30,7 @@ void AppLoadPrefs(void)
|
||||
|
||||
SoundsLoad();
|
||||
MutesLoad();
|
||||
ScopesLoad();
|
||||
if (PrefGetAppPreferences(VM_CREATOR, VM_PREFS_ID, &prefs, &size, true) != noPreferenceFound &&
|
||||
size == sizeof(prefs) && prefs.version == VM_PREFS_VERSION) {
|
||||
/*
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "remote_form.h"
|
||||
#include "sounds.h"
|
||||
#include "mutes.h"
|
||||
#include "scopes.h"
|
||||
#include "node_form.h"
|
||||
#include "settings.h"
|
||||
#include "ports.h"
|
||||
@@ -51,6 +52,8 @@ static Boolean HandleMenu(UInt16 itemID)
|
||||
case MainMenuAddChannel: FrmGotoForm(ChannelForm); return true;
|
||||
case MainMenuLeaveChannel: ChannelLeaveCurrent(); return true;
|
||||
case MainMenuMuteChannel: ChannelMuteToggleCurrent(); return true;
|
||||
case MainMenuChannelScope: ChannelScopeChooseCurrent(); return true;
|
||||
case MainMenuScopes: ScopesFormOpen(FrmGetActiveFormID()); return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -80,6 +83,7 @@ static Boolean AppHandleEvent(EventType *event)
|
||||
case ContactAddForm: FrmSetEventHandler(form, ContactAddFormHandleEvent); break;
|
||||
case RemoteForm: FrmSetEventHandler(form, RemoteFormHandleEvent); break;
|
||||
case SoundsForm: FrmSetEventHandler(form, SoundsFormHandleEvent); break;
|
||||
case ScopesForm: FrmSetEventHandler(form, ScopesFormHandleEvent); break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#define MainMenuLeaveChannel 1007
|
||||
#define MainMenuSounds 1008
|
||||
#define MainMenuMuteChannel 1009
|
||||
#define MainMenuChannelScope 1010
|
||||
#define MainMenuScopes 1011
|
||||
#define MainLinkGroup 1
|
||||
|
||||
#define SettingsForm 1200
|
||||
@@ -128,6 +130,23 @@
|
||||
#define SoundsLargePush 2213
|
||||
#define SoundsTextGroup 5
|
||||
|
||||
#define ScopesForm 2300
|
||||
#define ScopesDefaultLabel 2301
|
||||
#define ScopesList 2302
|
||||
#define ScopesField 2303
|
||||
#define ScopesAddButton 2304
|
||||
#define ScopesDeleteButton 2305
|
||||
#define ScopesSetDefaultButton 2306
|
||||
#define ScopesClearDefaultButton 2307
|
||||
#define ScopesDoneButton 2308
|
||||
#define ScopeAlert 2309
|
||||
#define ScopesDiscoverButton 2310
|
||||
|
||||
#define ChanScopeForm 2400
|
||||
#define ChanScopeList 2401
|
||||
#define ChanScopeOKButton 2402
|
||||
#define ChanScopeCancelButton 2403
|
||||
|
||||
#define SmallFontRes 1000
|
||||
#define LargeFontRes 1001
|
||||
#define FontPreviewForm 1400
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
#include "scope_proto.h"
|
||||
#include "sha256.h"
|
||||
|
||||
const Char *VmScopeBareName(const Char *name)
|
||||
{
|
||||
return name[0] == '#' ? name + 1 : name;
|
||||
}
|
||||
|
||||
void VmScopeKey(const Char *name, UInt8 key[VM_SCOPE_KEY_SIZE])
|
||||
{
|
||||
UInt8 tag[VM_SCOPE_NAME_SIZE + 1];
|
||||
UInt8 digest[32];
|
||||
UInt16 n = 0;
|
||||
UInt16 i;
|
||||
|
||||
name = VmScopeBareName(name);
|
||||
tag[n++] = '#';
|
||||
while (name[n - 1] && n < sizeof(tag)) {
|
||||
tag[n] = (UInt8)name[n - 1];
|
||||
n++;
|
||||
}
|
||||
VmSha256(tag, n, digest);
|
||||
for (i = 0; i < VM_SCOPE_KEY_SIZE; i++)
|
||||
key[i] = digest[i];
|
||||
}
|
||||
|
||||
Boolean VmScopeNameValid(const Char *name)
|
||||
{
|
||||
UInt16 n = 0;
|
||||
|
||||
name = VmScopeBareName(name);
|
||||
if (name[0] == '$')
|
||||
return 0;
|
||||
for (; name[n]; n++)
|
||||
if ((UInt8)name[n] <= ' ' || (UInt8)name[n] > '~')
|
||||
return 0;
|
||||
return n > 0 && n < VM_SCOPE_NAME_SIZE;
|
||||
}
|
||||
|
||||
UInt16 VmBuildSetScopeKey(UInt8 *out, UInt16 outSize, const UInt8 *key)
|
||||
{
|
||||
UInt8 p[2 + VM_SCOPE_KEY_SIZE];
|
||||
UInt16 i;
|
||||
|
||||
p[0] = VM_CMD_SET_FLOOD_SCOPE_KEY;
|
||||
p[1] = 0;
|
||||
if (!key)
|
||||
return VmEncodeFrame(out, outSize, p, 2);
|
||||
for (i = 0; i < VM_SCOPE_KEY_SIZE; i++)
|
||||
p[2 + i] = key[i];
|
||||
return VmEncodeFrame(out, outSize, p, sizeof(p));
|
||||
}
|
||||
|
||||
UInt16 VmBuildSetUnscoped(UInt8 *out, UInt16 outSize)
|
||||
{
|
||||
UInt8 p[2];
|
||||
|
||||
p[0] = VM_CMD_SET_FLOOD_SCOPE_KEY;
|
||||
p[1] = 1;
|
||||
return VmEncodeFrame(out, outSize, p, 2);
|
||||
}
|
||||
|
||||
UInt16 VmBuildSetDefaultScope(UInt8 *out, UInt16 outSize, const Char *name)
|
||||
{
|
||||
UInt8 p[1 + VM_SCOPE_NAME_SIZE + VM_SCOPE_KEY_SIZE];
|
||||
UInt16 i;
|
||||
|
||||
p[0] = VM_CMD_SET_DEFAULT_FLOOD_SCOPE;
|
||||
if (!name)
|
||||
return VmEncodeFrame(out, outSize, p, 1);
|
||||
if (!VmScopeNameValid(name))
|
||||
return 0;
|
||||
name = VmScopeBareName(name);
|
||||
for (i = 0; i < VM_SCOPE_NAME_SIZE; i++)
|
||||
p[1 + i] = 0;
|
||||
for (i = 0; name[i]; i++)
|
||||
p[1 + i] = (UInt8)name[i];
|
||||
VmScopeKey(name, p + 1 + VM_SCOPE_NAME_SIZE);
|
||||
return VmEncodeFrame(out, outSize, p, sizeof(p));
|
||||
}
|
||||
|
||||
UInt16 VmBuildGetDefaultScope(UInt8 *out, UInt16 outSize)
|
||||
{
|
||||
UInt8 p[1];
|
||||
|
||||
p[0] = VM_CMD_GET_DEFAULT_FLOOD_SCOPE;
|
||||
return VmEncodeFrame(out, outSize, p, 1);
|
||||
}
|
||||
|
||||
Boolean VmParseDefaultScope(const UInt8 *p, UInt16 len, Char *name, UInt8 key[VM_SCOPE_KEY_SIZE])
|
||||
{
|
||||
UInt16 i;
|
||||
|
||||
if (len < 1 || p[0] != VM_RESP_DEFAULT_FLOOD_SCOPE)
|
||||
return 0;
|
||||
name[0] = 0;
|
||||
for (i = 0; i < VM_SCOPE_KEY_SIZE; i++)
|
||||
key[i] = 0;
|
||||
if (len == 1)
|
||||
return 1; /* no default */
|
||||
if (len < 1 + VM_SCOPE_NAME_SIZE + VM_SCOPE_KEY_SIZE)
|
||||
return 0;
|
||||
for (i = 0; i < VM_SCOPE_NAME_SIZE - 1 && p[1 + i]; i++)
|
||||
name[i] = (Char)p[1 + i];
|
||||
name[i] = 0;
|
||||
for (i = 0; i < VM_SCOPE_KEY_SIZE; i++)
|
||||
key[i] = p[1 + VM_SCOPE_NAME_SIZE + i];
|
||||
return 1;
|
||||
}
|
||||
|
||||
UInt16 VmBuildRegionsReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey)
|
||||
{
|
||||
UInt8 p[1 + VM_PUB_KEY_SIZE + 2];
|
||||
UInt16 i;
|
||||
|
||||
p[0] = VM_CMD_SEND_ANON_REQ;
|
||||
for (i = 0; i < VM_PUB_KEY_SIZE; i++)
|
||||
p[1 + i] = pubKey[i];
|
||||
p[1 + VM_PUB_KEY_SIZE] = VM_ANON_REQ_REGIONS;
|
||||
p[2 + VM_PUB_KEY_SIZE] = 0; /* reply path: zero-hop */
|
||||
return VmEncodeFrame(out, outSize, p, sizeof(p));
|
||||
}
|
||||
|
||||
Boolean VmParseRegionsReply(const UInt8 *p, UInt16 len, UInt32 *tag,
|
||||
const UInt8 **names, UInt16 *namesLen)
|
||||
{
|
||||
UInt16 n;
|
||||
|
||||
if (len < 10 || p[0] != VM_PUSH_BINARY_RESPONSE)
|
||||
return 0;
|
||||
*tag = (UInt32)p[2] | ((UInt32)p[3] << 8) | ((UInt32)p[4] << 16) | ((UInt32)p[5] << 24);
|
||||
*names = p + 10; /* after the repeater's clock */
|
||||
for (n = 0; 10 + n < len && p[10 + n]; n++)
|
||||
;
|
||||
*namesLen = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Boolean VmNextRegionName(const UInt8 *names, UInt16 len, UInt16 *pos, Char *out)
|
||||
{
|
||||
UInt16 i = *pos, n = 0;
|
||||
|
||||
while (i < len && names[i] == ' ')
|
||||
i++;
|
||||
if (i >= len)
|
||||
return 0;
|
||||
while (i < len && names[i] != ',') {
|
||||
if (n < VM_SCOPE_NAME_SIZE - 1)
|
||||
out[n++] = (Char)names[i];
|
||||
i++;
|
||||
}
|
||||
while (n && out[n - 1] == ' ')
|
||||
n--;
|
||||
out[n] = 0;
|
||||
*pos = i < len ? i + 1 : i;
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Flood scopes (MeshCore "regions"): commands and key derivation.
|
||||
*
|
||||
* A flood packet can carry a transport code derived from a scope key, and
|
||||
* repeaters set up for that region forward it (region allowf / denyf). A
|
||||
* public scope's key is the first 16 bytes of SHA-256 of "#" + its name,
|
||||
* the same scheme as hashtag channels (TransportKeyStore::getAutoKeyFor).
|
||||
*
|
||||
* SET_FLOOD_SCOPE_KEY (54) 0, key[16]: override the scope for sends from now on
|
||||
* 0: clear the override (back to the default)
|
||||
* 1: send unscoped (firmware v12+)
|
||||
* SET_DEFAULT_FLOOD_SCOPE (63) name[31], key[16]: the radio's saved default; just
|
||||
* the code clears it
|
||||
* GET_DEFAULT_FLOOD_SCOPE (64) -> DEFAULT_FLOOD_SCOPE (28) name[31], key[16], or
|
||||
* just the code for none
|
||||
*
|
||||
* Discovering a repeater's regions (simple_repeater handleAnonRegionsReq):
|
||||
* SEND_ANON_REQ (57) pub_key[32], 0x01, reply_path_len, reply_path
|
||||
* -> SENT (tag), later PUSH_BINARY_RESPONSE (0x8C) 0, tag, its clock[4],
|
||||
* names: comma-separated, '#' dropped, "*" for the wildcard
|
||||
* The repeater ignores it unless it arrives direct, so it only works for
|
||||
* repeaters we have a direct path to; with reply_path_len 0 the answer comes
|
||||
* back zero-hop, so only neighbours (firmware v13+ for non-contacts).
|
||||
*
|
||||
* The override lasts until the radio reboots, across app connections, so the
|
||||
* session sets it before every flood send (session.c, ApplyScope).
|
||||
* Plain C with no Palm OS calls, so it also builds on the host for tests.
|
||||
*/
|
||||
#ifndef VM_SCOPE_PROTO_H
|
||||
#define VM_SCOPE_PROTO_H
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#define VM_CMD_SET_FLOOD_SCOPE_KEY 54
|
||||
#define VM_CMD_SET_DEFAULT_FLOOD_SCOPE 63
|
||||
#define VM_CMD_GET_DEFAULT_FLOOD_SCOPE 64
|
||||
#define VM_RESP_DEFAULT_FLOOD_SCOPE 28
|
||||
|
||||
#define VM_CMD_SEND_ANON_REQ 57
|
||||
#define VM_PUSH_BINARY_RESPONSE 0x8C
|
||||
#define VM_ANON_REQ_REGIONS 0x01
|
||||
|
||||
#define VM_SCOPE_KEY_SIZE 16
|
||||
#define VM_SCOPE_NAME_SIZE 31 /* including the NUL, as the firmware stores it */
|
||||
|
||||
#define VM_FW_SCOPES 8 /* SET_FLOOD_SCOPE_KEY */
|
||||
#define VM_FW_UNSCOPED 12 /* SET_FLOOD_SCOPE_KEY 1 */
|
||||
|
||||
/* Name without a leading '#', as stored; key from "#" + that. */
|
||||
const Char *VmScopeBareName(const Char *name) VM_SECT3;
|
||||
void VmScopeKey(const Char *name, UInt8 key[VM_SCOPE_KEY_SIZE]) VM_SECT3;
|
||||
|
||||
/*
|
||||
* A name we can derive a key for: 1-30 printable ASCII characters after any
|
||||
* '#', not starting with '$' (private regions have keys only the repeaters
|
||||
* that set them up know).
|
||||
*/
|
||||
Boolean VmScopeNameValid(const Char *name) VM_SECT3;
|
||||
|
||||
/* key NULL: clear the override, so sends use the radio's default. */
|
||||
UInt16 VmBuildSetScopeKey(UInt8 *out, UInt16 outSize, const UInt8 *key) VM_SECT3;
|
||||
UInt16 VmBuildSetUnscoped(UInt8 *out, UInt16 outSize) VM_SECT3;
|
||||
/* name NULL: no default. */
|
||||
UInt16 VmBuildSetDefaultScope(UInt8 *out, UInt16 outSize, const Char *name) VM_SECT3;
|
||||
UInt16 VmBuildGetDefaultScope(UInt8 *out, UInt16 outSize) VM_SECT3;
|
||||
|
||||
/* A zero-hop regions request to the repeater with this key. */
|
||||
UInt16 VmBuildRegionsReq(UInt8 *out, UInt16 outSize, const UInt8 *pubKey) VM_SECT3;
|
||||
|
||||
/* PUSH_BINARY_RESPONSE: the request's tag, and the names (not NUL-terminated). */
|
||||
Boolean VmParseRegionsReply(const UInt8 *p, UInt16 len, UInt32 *tag,
|
||||
const UInt8 **names, UInt16 *namesLen) VM_SECT3;
|
||||
|
||||
/*
|
||||
* The next name in a comma list from *pos (advanced past it), trimmed, into
|
||||
* out[VM_SCOPE_NAME_SIZE] (cut if longer). Returns false at the end.
|
||||
*/
|
||||
Boolean VmNextRegionName(const UInt8 *names, UInt16 len, UInt16 *pos, Char *out) VM_SECT3;
|
||||
|
||||
/* DEFAULT_FLOOD_SCOPE: name[VM_SCOPE_NAME_SIZE] is "" when the radio has none. */
|
||||
Boolean VmParseDefaultScope(const UInt8 *p, UInt16 len, Char *name, UInt8 key[VM_SCOPE_KEY_SIZE]) VM_SECT3;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,371 @@
|
||||
#include <PalmOS.h>
|
||||
#include "resources.h"
|
||||
#include "app.h"
|
||||
#include "channels.h"
|
||||
#include "session.h"
|
||||
#include "contacts.h"
|
||||
#include "scopes.h"
|
||||
|
||||
static Int16 FindChannel(const VmChannel *c) VM_SECT3;
|
||||
static void SavePrefs(void) VM_SECT3;
|
||||
static void Error(const Char *msg) VM_SECT3;
|
||||
static void FillList(FormType *form) VM_SECT3;
|
||||
static void ShowDefault(FormType *form) VM_SECT3;
|
||||
static void AddTyped(FormType *form) VM_SECT3;
|
||||
static void DeleteSelected(FormType *form) VM_SECT3;
|
||||
static void SetRadioDefault(FormType *form, Boolean clear) VM_SECT3;
|
||||
static void Discover(FormType *form) VM_SECT3;
|
||||
static Boolean AddName(const Char *name) VM_SECT3;
|
||||
static void SetDefaultLabel(FormType *form, const Char *text) VM_SECT3;
|
||||
|
||||
#define VM_SCOPES_PREFS_ID 4
|
||||
#define VM_SCOPES_PREFS_VERSION 1
|
||||
#define VM_SCOPES_MAX 12
|
||||
#define VM_SCOPE_CHANS 16
|
||||
#define VM_CHAN_ID_SIZE 4 /* leading bytes of the channel key */
|
||||
|
||||
typedef struct {
|
||||
UInt8 id[VM_CHAN_ID_SIZE];
|
||||
UInt8 mode; /* VM_SCOPE_* */
|
||||
Char name[VM_SCOPE_NAME_SIZE]; /* VM_SCOPE_NAMED: kept even if it leaves the list */
|
||||
} ChanScope;
|
||||
|
||||
typedef struct {
|
||||
UInt16 version;
|
||||
UInt16 count;
|
||||
UInt16 chanCount;
|
||||
Char names[VM_SCOPES_MAX][VM_SCOPE_NAME_SIZE];
|
||||
ChanScope chans[VM_SCOPE_CHANS];
|
||||
} ScopesPrefs;
|
||||
|
||||
static ScopesPrefs gScopes;
|
||||
static UInt16 gReturnForm = HomeForm;
|
||||
|
||||
/* List text for the forms: "#name", and the pointers LstSetListChoices keeps. */
|
||||
static Char gItems[VM_SCOPES_MAX + 2][VM_SCOPE_NAME_SIZE + 1];
|
||||
static Char *gItemPtrs[VM_SCOPES_MAX + 2];
|
||||
static Char gDefaultText[VM_SCOPE_NAME_SIZE + 2];
|
||||
|
||||
void ScopesLoad(void)
|
||||
{
|
||||
UInt16 size = sizeof(gScopes);
|
||||
|
||||
if (PrefGetAppPreferences(VM_CREATOR, VM_SCOPES_PREFS_ID, &gScopes, &size, true) == noPreferenceFound ||
|
||||
size != sizeof(gScopes) || gScopes.version != VM_SCOPES_PREFS_VERSION ||
|
||||
gScopes.count > VM_SCOPES_MAX || gScopes.chanCount > VM_SCOPE_CHANS) {
|
||||
MemSet(&gScopes, sizeof(gScopes), 0);
|
||||
}
|
||||
gScopes.version = VM_SCOPES_PREFS_VERSION;
|
||||
}
|
||||
|
||||
static void SavePrefs(void)
|
||||
{
|
||||
PrefSetAppPreferences(VM_CREATOR, VM_SCOPES_PREFS_ID, VM_SCOPES_PREFS_VERSION, &gScopes, sizeof(gScopes), true);
|
||||
}
|
||||
|
||||
static void Error(const Char *msg)
|
||||
{
|
||||
FrmCustomAlert(ScopeAlert, msg, "", "");
|
||||
}
|
||||
|
||||
static Int16 FindChannel(const VmChannel *c)
|
||||
{
|
||||
UInt16 i;
|
||||
|
||||
for (i = 0; i < gScopes.chanCount; i++)
|
||||
if (MemCmp(gScopes.chans[i].id, c->secret, VM_CHAN_ID_SIZE) == 0)
|
||||
return (Int16)i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
UInt8 ScopesForChannel(UInt8 slot, UInt8 key[VM_SCOPE_KEY_SIZE])
|
||||
{
|
||||
VmChannel c;
|
||||
Int16 i;
|
||||
|
||||
if (!ChannelsFindSlot(slot, &c) || (i = FindChannel(&c)) < 0)
|
||||
return VM_SCOPE_DEFAULT;
|
||||
if (gScopes.chans[i].mode == VM_SCOPE_NAMED)
|
||||
VmScopeKey(gScopes.chans[i].name, key);
|
||||
return gScopes.chans[i].mode;
|
||||
}
|
||||
|
||||
/* ---- Channel Scope dialog ---- */
|
||||
|
||||
void ChannelScopeChooseCurrent(void)
|
||||
{
|
||||
VmChannel c;
|
||||
FormType *form, *prev;
|
||||
ListType *list;
|
||||
Int16 i, sel = 0;
|
||||
UInt16 n, k;
|
||||
ChanScope *cs;
|
||||
|
||||
if (FrmGetActiveFormID() != ConvoForm || gAppConv.type != VM_CONV_CHANNEL ||
|
||||
!ChannelsFindSlot(gAppConv.channel, &c)) {
|
||||
Error("Open the channel first.");
|
||||
return;
|
||||
}
|
||||
i = FindChannel(&c);
|
||||
StrCopy(gItems[0], "Radio default");
|
||||
StrCopy(gItems[1], "No scope");
|
||||
n = 2;
|
||||
for (k = 0; k < gScopes.count; k++, n++)
|
||||
StrPrintF(gItems[n], "#%s", gScopes.names[k]);
|
||||
if (i >= 0 && gScopes.chans[i].mode == VM_SCOPE_NONE)
|
||||
sel = 1;
|
||||
if (i >= 0 && gScopes.chans[i].mode == VM_SCOPE_NAMED) {
|
||||
sel = -1;
|
||||
for (k = 0; k < gScopes.count; k++)
|
||||
if (StrCompare(gScopes.names[k], gScopes.chans[i].name) == 0)
|
||||
sel = (Int16)(2 + k);
|
||||
if (sel < 0 && n < VM_SCOPES_MAX + 2) { /* no longer in the list: still offer it */
|
||||
StrPrintF(gItems[n], "#%s", gScopes.chans[i].name);
|
||||
sel = (Int16)n++;
|
||||
}
|
||||
}
|
||||
for (k = 0; k < n; k++)
|
||||
gItemPtrs[k] = gItems[k];
|
||||
|
||||
prev = FrmGetActiveForm();
|
||||
form = FrmInitForm(ChanScopeForm);
|
||||
FrmSetActiveForm(form);
|
||||
list = (ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ChanScopeList));
|
||||
LstSetListChoices(list, gItemPtrs, n);
|
||||
LstSetSelection(list, sel);
|
||||
LstMakeItemVisible(list, sel);
|
||||
if (FrmDoDialog(form) == ChanScopeOKButton) {
|
||||
sel = LstGetSelection(list);
|
||||
if (i < 0 && sel > 0) {
|
||||
if (gScopes.chanCount >= VM_SCOPE_CHANS) {
|
||||
FrmDeleteForm(form);
|
||||
FrmSetActiveForm(prev);
|
||||
Error("Too many channels with their own scope: set one back to Radio default first.");
|
||||
return;
|
||||
}
|
||||
i = (Int16)gScopes.chanCount++;
|
||||
MemMove(gScopes.chans[i].id, c.secret, VM_CHAN_ID_SIZE);
|
||||
}
|
||||
if (i >= 0) {
|
||||
cs = &gScopes.chans[i];
|
||||
if (sel <= 0) { /* default: drop the entry */
|
||||
MemMove(cs, cs + 1, (gScopes.chanCount - i - 1) * sizeof(ChanScope));
|
||||
gScopes.chanCount--;
|
||||
} else if (sel == 1) {
|
||||
cs->mode = VM_SCOPE_NONE;
|
||||
} else {
|
||||
cs->mode = VM_SCOPE_NAMED;
|
||||
StrCopy(cs->name, gItems[sel] + 1); /* skip the '#' */
|
||||
}
|
||||
SavePrefs();
|
||||
}
|
||||
}
|
||||
FrmDeleteForm(form);
|
||||
FrmSetActiveForm(prev);
|
||||
}
|
||||
|
||||
/* ---- Flood Scopes form ---- */
|
||||
|
||||
static void FillList(FormType *form)
|
||||
{
|
||||
ListType *list = (ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList));
|
||||
UInt16 k;
|
||||
|
||||
for (k = 0; k < gScopes.count; k++) {
|
||||
StrPrintF(gItems[k], "#%s", gScopes.names[k]);
|
||||
gItemPtrs[k] = gItems[k];
|
||||
}
|
||||
LstSetListChoices(list, gItemPtrs, gScopes.count);
|
||||
LstSetSelection(list, gScopes.count ? 0 : noListSelection);
|
||||
}
|
||||
|
||||
/* The label reserves 30 characters in the .rcp. */
|
||||
static void SetDefaultLabel(FormType *form, const Char *text)
|
||||
{
|
||||
UInt16 index = FrmGetObjectIndex(form, ScopesDefaultLabel);
|
||||
|
||||
StrNCopy(gDefaultText, text, 30);
|
||||
gDefaultText[30] = 0;
|
||||
if (FrmVisible(form))
|
||||
FrmHideObject(form, index);
|
||||
FrmCopyLabel(form, ScopesDefaultLabel, gDefaultText);
|
||||
if (FrmVisible(form))
|
||||
FrmShowObject(form, index);
|
||||
}
|
||||
|
||||
static void ShowDefault(FormType *form)
|
||||
{
|
||||
Char name[VM_SCOPE_NAME_SIZE], text[VM_SCOPE_NAME_SIZE + 2];
|
||||
|
||||
if (!SessionIsConnected())
|
||||
StrCopy(text, "(connect to see)");
|
||||
else if (SessionGetDefaultScope(name) != errNone)
|
||||
StrCopy(text, "(no reply)");
|
||||
else if (!name[0])
|
||||
StrCopy(text, "none");
|
||||
else
|
||||
StrPrintF(text, "#%s", name);
|
||||
SetDefaultLabel(form, text);
|
||||
}
|
||||
|
||||
/* Adds a name to the list if it's new and there's room; true if added. */
|
||||
static Boolean AddName(const Char *name)
|
||||
{
|
||||
UInt16 k;
|
||||
|
||||
name = VmScopeBareName(name);
|
||||
for (k = 0; k < gScopes.count; k++)
|
||||
if (StrCompare(gScopes.names[k], name) == 0)
|
||||
return false;
|
||||
if (gScopes.count >= VM_SCOPES_MAX)
|
||||
return false;
|
||||
StrCopy(gScopes.names[gScopes.count++], name);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Asks each neighbouring repeater (a contact with a 0-hop path) for its
|
||||
* regions and adds the public ones. Repeaters only answer direct requests,
|
||||
* and the reply comes back zero-hop, so farther ones can't be asked yet.
|
||||
*/
|
||||
static void Discover(FormType *form)
|
||||
{
|
||||
Char names[160], name[VM_SCOPE_NAME_SIZE], msg[120], status[VM_NAME_SIZE + 16];
|
||||
UInt16 count, i, pos, asked = 0, answered = 0, added = 0, skipped = 0;
|
||||
VmContact ct;
|
||||
|
||||
if (!SessionIsConnected()) {
|
||||
Error("Connect to the radio first.");
|
||||
return;
|
||||
}
|
||||
count = ContactsCount();
|
||||
for (i = 0; i < count; i++) {
|
||||
if (!ContactsGet(i, &ct) || ct.type != VM_ADV_TYPE_REPEATER)
|
||||
continue;
|
||||
if (ct.outPathLen == 0xFF || (ct.outPathLen & 0x3F) != 0)
|
||||
continue; /* not a neighbour */
|
||||
asked++;
|
||||
StrPrintF(status, "asking %s...", ct.name);
|
||||
SetDefaultLabel(form, status);
|
||||
if (SessionAskRegions(ct.pubKey, names, sizeof(names)) != errNone)
|
||||
continue;
|
||||
answered++;
|
||||
pos = 0;
|
||||
while (VmNextRegionName((const UInt8 *)names, StrLen(names), &pos, name)) {
|
||||
if (!VmScopeNameValid(name)) {
|
||||
if (name[0] == '$')
|
||||
skipped++; /* private: no key from the name */
|
||||
continue; /* "*", the wildcard, isn't a scope */
|
||||
}
|
||||
if (AddName(name))
|
||||
added++;
|
||||
}
|
||||
}
|
||||
if (added) {
|
||||
SavePrefs();
|
||||
FillList(form);
|
||||
LstDrawList((ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList)));
|
||||
}
|
||||
ShowDefault(form);
|
||||
if (asked == 0)
|
||||
StrCopy(msg, "No neighbouring repeaters in your contacts. Repeaters only answer when they're one hop away.");
|
||||
else
|
||||
StrPrintF(msg, "%u of %u nearby repeaters answered. %u new scope%s added%s.", answered, asked, added,
|
||||
added == 1 ? "" : "s", skipped ? "; private ($) ones skipped" : "");
|
||||
Error(msg);
|
||||
}
|
||||
|
||||
static void AddTyped(FormType *form)
|
||||
{
|
||||
FieldType *field = (FieldType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesField));
|
||||
Char *text = FldGetTextPtr(field);
|
||||
if (!text || !text[0])
|
||||
return;
|
||||
if (!VmScopeNameValid(text)) {
|
||||
Error(VmScopeBareName(text)[0] == '$'
|
||||
? "Private ($) scopes need a key from the repeater's owner; only public names work here."
|
||||
: "A scope name is 1-30 letters, digits or symbols, no spaces.");
|
||||
return;
|
||||
}
|
||||
if (!AddName(text)) {
|
||||
if (gScopes.count >= VM_SCOPES_MAX)
|
||||
Error("The list is full: delete a scope first.");
|
||||
return;
|
||||
}
|
||||
SavePrefs();
|
||||
FldDelete(field, 0, FldGetTextLength(field));
|
||||
FillList(form);
|
||||
LstSetSelection((ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList)), gScopes.count - 1);
|
||||
LstDrawList((ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList)));
|
||||
}
|
||||
|
||||
static void DeleteSelected(FormType *form)
|
||||
{
|
||||
ListType *list = (ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList));
|
||||
Int16 sel = LstGetSelection(list);
|
||||
|
||||
if (sel < 0 || sel >= (Int16)gScopes.count)
|
||||
return;
|
||||
MemMove(gScopes.names[sel], gScopes.names[sel + 1], (gScopes.count - sel - 1) * VM_SCOPE_NAME_SIZE);
|
||||
gScopes.count--;
|
||||
SavePrefs();
|
||||
FillList(form);
|
||||
LstDrawList(list);
|
||||
}
|
||||
|
||||
static void SetRadioDefault(FormType *form, Boolean clear)
|
||||
{
|
||||
ListType *list = (ListType *)FrmGetObjectPtr(form, FrmGetObjectIndex(form, ScopesList));
|
||||
Int16 sel = LstGetSelection(list);
|
||||
Char msg[40];
|
||||
Err err;
|
||||
|
||||
if (!SessionIsConnected()) {
|
||||
Error("Connect to the radio first.");
|
||||
return;
|
||||
}
|
||||
if (!clear && (sel < 0 || sel >= (Int16)gScopes.count)) {
|
||||
Error("Select a scope in the list first.");
|
||||
return;
|
||||
}
|
||||
err = SessionSetDefaultScope(clear ? NULL : gScopes.names[sel]);
|
||||
if (err != errNone) {
|
||||
AppErrorText(err, msg);
|
||||
Error(msg);
|
||||
}
|
||||
ShowDefault(form);
|
||||
}
|
||||
|
||||
void ScopesFormOpen(UInt16 returnForm)
|
||||
{
|
||||
if (returnForm != ScopesForm)
|
||||
gReturnForm = returnForm;
|
||||
FrmGotoForm(ScopesForm);
|
||||
}
|
||||
|
||||
Boolean ScopesFormHandleEvent(EventType *event)
|
||||
{
|
||||
FormType *form = FrmGetActiveForm();
|
||||
|
||||
switch (event->eType) {
|
||||
case frmOpenEvent:
|
||||
FillList(form);
|
||||
ShowDefault(form);
|
||||
FrmDrawForm(form);
|
||||
FrmSetFocus(form, FrmGetObjectIndex(form, ScopesField));
|
||||
return true;
|
||||
case ctlSelectEvent:
|
||||
switch (event->data.ctlSelect.controlID) {
|
||||
case ScopesAddButton: AddTyped(form); return true;
|
||||
case ScopesDiscoverButton: Discover(form); return true;
|
||||
case ScopesDeleteButton: DeleteSelected(form); return true;
|
||||
case ScopesSetDefaultButton: SetRadioDefault(form, false); return true;
|
||||
case ScopesClearDefaultButton: SetRadioDefault(form, true); return true;
|
||||
case ScopesDoneButton: FrmGotoForm(gReturnForm); return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Flood scopes on the Visor side (protocol in scope_proto.h): the list of
|
||||
* scope names to pick from, and each channel's choice of scope. The radio's
|
||||
* default scope is kept on the radio; per-channel scopes are kept here, by
|
||||
* channel key like mutes, and the session applies them before each send.
|
||||
*/
|
||||
#ifndef VM_SCOPES_H
|
||||
#define VM_SCOPES_H
|
||||
|
||||
#include <PalmOS.h>
|
||||
#include "sections.h"
|
||||
#include "scope_proto.h"
|
||||
|
||||
/* A channel's choice */
|
||||
#define VM_SCOPE_DEFAULT 0 /* whatever the radio's default is */
|
||||
#define VM_SCOPE_NONE 1 /* unscoped (firmware v12+; older ones use the default) */
|
||||
#define VM_SCOPE_NAMED 2
|
||||
|
||||
void ScopesLoad(void) VM_SECT3;
|
||||
|
||||
/* The channel in slot's choice; for VM_SCOPE_NAMED also its key. */
|
||||
UInt8 ScopesForChannel(UInt8 slot, UInt8 key[VM_SCOPE_KEY_SIZE]) VM_SECT3;
|
||||
|
||||
/* Options > Channel Scope: picks a scope for the channel on screen. */
|
||||
void ChannelScopeChooseCurrent(void) VM_SECT3;
|
||||
|
||||
/* Options > Flood Scopes: the list, and the radio's default. Done goes back to returnForm. */
|
||||
void ScopesFormOpen(UInt16 returnForm) VM_SECT3;
|
||||
Boolean ScopesFormHandleEvent(EventType *event) VM_SECT3;
|
||||
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "remote.h"
|
||||
#include "sounds.h"
|
||||
#include "mutes.h"
|
||||
#include "scopes.h"
|
||||
|
||||
#define VM_APP_NAME "VisorMesh"
|
||||
#define VM_REPLY_TIMEOUT (2 * SysTicksPerSecond())
|
||||
@@ -44,6 +45,15 @@ static Int32 gUtcOffsetSecs = 0; /* local minus UTC */
|
||||
|
||||
static void NoteSound(UInt8 kind, const VmMessage *m);
|
||||
|
||||
/*
|
||||
* What the radio's flood-scope override is set to on this connection. It
|
||||
* outlasts connections (until the radio reboots), so it starts unknown and
|
||||
* the first flood send sets it.
|
||||
*/
|
||||
#define VM_SCOPE_UNKNOWN 0xFF
|
||||
static UInt8 gScopeMode = VM_SCOPE_UNKNOWN;
|
||||
static UInt8 gScopeKey[VM_SCOPE_KEY_SIZE];
|
||||
|
||||
void SessionSetUtcOffset(Int16 minutes)
|
||||
{
|
||||
gUtcOffsetSecs = (Int32)minutes * 60;
|
||||
@@ -265,6 +275,7 @@ Err SessionConnect(void)
|
||||
return err;
|
||||
}
|
||||
gConnected = true;
|
||||
gScopeMode = VM_SCOPE_UNKNOWN;
|
||||
|
||||
/* Best effort: the connection stands even if one of these fails; the stored copies stay. */
|
||||
SessionSyncClock();
|
||||
@@ -457,6 +468,113 @@ Err SessionSyncMessages(void)
|
||||
return errNone;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the scope for the next flood send: the channel's own (scopes.h), or the
|
||||
* radio's default for everything else. Skipped when it's already set, and on
|
||||
* firmware without scopes.
|
||||
*/
|
||||
static Err ApplyScope(const VmConv *conv)
|
||||
{
|
||||
UInt8 frame[VM_HEADER_LEN + 2 + VM_SCOPE_KEY_SIZE];
|
||||
UInt8 key[VM_SCOPE_KEY_SIZE];
|
||||
UInt8 mode = VM_SCOPE_DEFAULT;
|
||||
UInt16 len;
|
||||
Err err;
|
||||
|
||||
if (gDevice.fwVerCode < VM_FW_SCOPES)
|
||||
return errNone;
|
||||
if (conv && conv->type == VM_CONV_CHANNEL)
|
||||
mode = ScopesForChannel(conv->channel, key);
|
||||
if (mode == VM_SCOPE_NONE && gDevice.fwVerCode < VM_FW_UNSCOPED)
|
||||
mode = VM_SCOPE_DEFAULT;
|
||||
if (mode == gScopeMode && (mode != VM_SCOPE_NAMED || MemCmp(key, gScopeKey, VM_SCOPE_KEY_SIZE) == 0))
|
||||
return errNone;
|
||||
if (mode == VM_SCOPE_NAMED)
|
||||
len = VmBuildSetScopeKey(frame, sizeof(frame), key);
|
||||
else if (mode == VM_SCOPE_NONE)
|
||||
len = VmBuildSetUnscoped(frame, sizeof(frame));
|
||||
else
|
||||
len = VmBuildSetScopeKey(frame, sizeof(frame), NULL);
|
||||
err = Request(frame, len, VM_RESP_OK, 1);
|
||||
if (err != errNone) {
|
||||
gScopeMode = VM_SCOPE_UNKNOWN;
|
||||
return err;
|
||||
}
|
||||
gScopeMode = mode;
|
||||
if (mode == VM_SCOPE_NAMED)
|
||||
MemMove(gScopeKey, key, VM_SCOPE_KEY_SIZE);
|
||||
return errNone;
|
||||
}
|
||||
|
||||
Err SessionGetDefaultScope(Char *name)
|
||||
{
|
||||
UInt8 frame[VM_HEADER_LEN + 1];
|
||||
UInt8 key[VM_SCOPE_KEY_SIZE];
|
||||
Err err;
|
||||
|
||||
name[0] = 0;
|
||||
if (!gConnected)
|
||||
return serErrNotOpen;
|
||||
if (gDevice.fwVerCode < VM_FW_SCOPES)
|
||||
return errNone;
|
||||
err = Request(frame, VmBuildGetDefaultScope(frame, sizeof(frame)), VM_RESP_DEFAULT_FLOOD_SCOPE, 1);
|
||||
if (err == errNone && !VmParseDefaultScope(gDecoder.payload, gDecoder.len, name, key))
|
||||
err = vmErrBadReply;
|
||||
return err;
|
||||
}
|
||||
|
||||
Err SessionSetDefaultScope(const Char *name)
|
||||
{
|
||||
UInt8 frame[VM_HEADER_LEN + 1 + VM_SCOPE_NAME_SIZE + VM_SCOPE_KEY_SIZE];
|
||||
UInt16 len;
|
||||
|
||||
if (!gConnected)
|
||||
return serErrNotOpen;
|
||||
if (gDevice.fwVerCode < VM_FW_SCOPES)
|
||||
return vmErrRadio;
|
||||
len = VmBuildSetDefaultScope(frame, sizeof(frame), name);
|
||||
if (len == 0)
|
||||
return vmErrBadReply;
|
||||
return Request(frame, len, VM_RESP_OK, 1);
|
||||
}
|
||||
|
||||
Err SessionAskRegions(const UInt8 *pubKey, Char *names, UInt16 size)
|
||||
{
|
||||
static const UInt8 sentCode[] = { VM_RESP_SENT };
|
||||
static const UInt8 replyCode[] = { VM_PUSH_BINARY_RESPONSE };
|
||||
UInt8 frame[VM_HEADER_LEN + 1 + VM_PUB_KEY_SIZE + 2];
|
||||
const UInt8 *list;
|
||||
UInt32 deadline, tag;
|
||||
UInt16 len, n;
|
||||
VmSent sent;
|
||||
Err err;
|
||||
|
||||
names[0] = 0;
|
||||
if (!gConnected)
|
||||
return serErrNotOpen;
|
||||
len = VmBuildRegionsReq(frame, sizeof(frame), pubKey);
|
||||
err = RequestAny(frame, len, sentCode, 1, 1);
|
||||
if (err == errNone && !VmParseSent(gDecoder.payload, gDecoder.len, &sent))
|
||||
err = vmErrBadReply;
|
||||
if (err != errNone)
|
||||
return err;
|
||||
/* the answer comes back over the mesh: as long as the radio estimates, plus slack */
|
||||
deadline = TimGetTicks() + (sent.timeoutMs + 3000UL) / 1000UL * SysTicksPerSecond();
|
||||
for (;;) {
|
||||
err = WaitForAny(replyCode, 1, (Int32)(deadline - TimGetTicks()));
|
||||
if (err != errNone)
|
||||
return err;
|
||||
if (VmParseRegionsReply(gDecoder.payload, gDecoder.len, &tag, &list, &len) && tag == sent.expectedAck)
|
||||
break;
|
||||
if (TimGetTicks() >= deadline)
|
||||
return vmErrNoReply;
|
||||
}
|
||||
n = len < size - 1 ? len : size - 1;
|
||||
MemMove(names, list, n);
|
||||
names[n] = 0;
|
||||
return errNone;
|
||||
}
|
||||
|
||||
/* Sends message record 'index' (new or a retry) and records the outcome on it. */
|
||||
static Err SendRecord(UInt16 index)
|
||||
{
|
||||
@@ -488,6 +606,11 @@ static Err SendRecord(UInt16 index)
|
||||
|
||||
MessagesSetStatus(index, VM_ST_PENDING, 0, 0);
|
||||
gChanged |= VM_CHANGED_MESSAGES;
|
||||
err = ApplyScope(&h.conv);
|
||||
if (err != errNone) {
|
||||
MessagesSetStatus(index, VM_ST_FAILED, 0, 0);
|
||||
return err;
|
||||
}
|
||||
/* one try: a repeated send would go out twice */
|
||||
if (h.conv.type == VM_CONV_CHANNEL) {
|
||||
err = Request(frame, len, VM_RESP_OK, 1);
|
||||
@@ -597,6 +720,9 @@ Err SessionLogin(const UInt8 *pubKey, const Char *password, Boolean *isAdmin)
|
||||
n = TxtPalmToUtf8(password, StrLen(password), pw, sizeof(pw));
|
||||
if (*password && n == 0)
|
||||
return vmErrTooLong;
|
||||
err = ApplyScope(NULL);
|
||||
if (err != errNone)
|
||||
return err;
|
||||
len = VmBuildLogin(frame, sizeof(frame), pubKey, pw, n);
|
||||
err = RequestAny(frame, len, sentCode, 1, 1);
|
||||
if (err == errNone && !VmParseSent(gDecoder.payload, gDecoder.len, &sent))
|
||||
@@ -630,6 +756,9 @@ Err SessionRequestSent(const UInt8 *frame, UInt16 len, VmSent *sent)
|
||||
return serErrNotOpen;
|
||||
if (len == 0)
|
||||
return vmErrTooLong;
|
||||
err = ApplyScope(NULL);
|
||||
if (err != errNone)
|
||||
return err;
|
||||
err = RequestAny(frame, len, sentCode, 1, 1);
|
||||
if (err == errNone && !VmParseSent(gDecoder.payload, gDecoder.len, sent))
|
||||
err = vmErrBadReply;
|
||||
@@ -701,6 +830,9 @@ const VmSelfInfo *SessionSelf(void)
|
||||
|
||||
void SessionDisconnect(void)
|
||||
{
|
||||
/* leave the radio on its default scope, for other apps and the next connection */
|
||||
if (gConnected && gScopeMode != VM_SCOPE_DEFAULT && gScopeMode != VM_SCOPE_UNKNOWN)
|
||||
ApplyScope(NULL);
|
||||
TransportClose();
|
||||
gConnected = false;
|
||||
gLoginCount = 0; /* the radio keeps room sessions alive, but re-check after a reconnect */
|
||||
|
||||
@@ -31,6 +31,16 @@ Boolean SessionMessageWaiting(void);
|
||||
|
||||
/* What the radio last reported (valid while connected). */
|
||||
const VmDeviceInfo *SessionDevice(void);
|
||||
|
||||
/* The radio's default flood scope (scope_proto.h): name[VM_SCOPE_NAME_SIZE], "" for none. */
|
||||
Err SessionGetDefaultScope(Char *name);
|
||||
/* name NULL: no default. */
|
||||
Err SessionSetDefaultScope(const Char *name);
|
||||
/*
|
||||
* Asks a repeater for its regions (scope_proto.h): names gets them comma-separated
|
||||
* ("" if it has none). Only reaches neighbours; vmErrNoReply if it doesn't answer.
|
||||
*/
|
||||
Err SessionAskRegions(const UInt8 *pubKey, Char *names, UInt16 size);
|
||||
const VmSelfInfo *SessionSelf(void);
|
||||
|
||||
/* Sends a complete command frame and requires RESP_OK. */
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/* Host-side tests for flood scopes (src/scope_proto.c). Run with `make test`. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../src/scope_proto.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \
|
||||
} while (0)
|
||||
|
||||
/* The radio gave the hashtag channel "#test" this key (docs/progress.md); scopes use the same scheme. */
|
||||
static const UInt8 kTestKey[16] = {
|
||||
0x9c, 0xd8, 0xfc, 0xf2, 0x2a, 0x47, 0x33, 0x3b, 0x59, 0x1d, 0x96, 0xa2, 0xb8, 0x48, 0xb7, 0x3f
|
||||
};
|
||||
|
||||
static void test_key(void)
|
||||
{
|
||||
UInt8 key[16], key2[16];
|
||||
|
||||
VmScopeKey("test", key);
|
||||
CHECK(memcmp(key, kTestKey, 16) == 0);
|
||||
VmScopeKey("#test", key2); /* a typed '#' changes nothing */
|
||||
CHECK(memcmp(key, key2, 16) == 0);
|
||||
VmScopeKey("Test", key2); /* names are case-sensitive */
|
||||
CHECK(memcmp(key, key2, 16) != 0);
|
||||
}
|
||||
|
||||
static void test_names(void)
|
||||
{
|
||||
CHECK(VmScopeNameValid("phl"));
|
||||
CHECK(VmScopeNameValid("#us-pa-phl"));
|
||||
CHECK(!VmScopeNameValid(""));
|
||||
CHECK(!VmScopeNameValid("#"));
|
||||
CHECK(!VmScopeNameValid("$private"));
|
||||
CHECK(!VmScopeNameValid("has space"));
|
||||
CHECK(VmScopeNameValid("abcdefghijklmnopqrstuvwxyz1234")); /* 30 */
|
||||
CHECK(!VmScopeNameValid("abcdefghijklmnopqrstuvwxyz12345")); /* 31 */
|
||||
CHECK(strcmp(VmScopeBareName("#phl"), "phl") == 0);
|
||||
}
|
||||
|
||||
static void test_frames(void)
|
||||
{
|
||||
UInt8 out[64];
|
||||
UInt16 n;
|
||||
|
||||
n = VmBuildSetScopeKey(out, sizeof(out), kTestKey);
|
||||
CHECK(n == 3 + 18);
|
||||
CHECK(out[0] == '<' && out[1] == 18 && out[2] == 0);
|
||||
CHECK(out[3] == VM_CMD_SET_FLOOD_SCOPE_KEY && out[4] == 0);
|
||||
CHECK(memcmp(out + 5, kTestKey, 16) == 0);
|
||||
|
||||
n = VmBuildSetScopeKey(out, sizeof(out), NULL);
|
||||
CHECK(n == 5 && out[1] == 2 && out[3] == 54 && out[4] == 0);
|
||||
|
||||
n = VmBuildSetUnscoped(out, sizeof(out));
|
||||
CHECK(n == 5 && out[3] == 54 && out[4] == 1);
|
||||
|
||||
n = VmBuildSetDefaultScope(out, sizeof(out), "#test");
|
||||
CHECK(n == 3 + 48);
|
||||
CHECK(out[3] == VM_CMD_SET_DEFAULT_FLOOD_SCOPE);
|
||||
CHECK(memcmp(out + 4, "test\0", 5) == 0); /* stored without the '#' */
|
||||
CHECK(out[4 + 30] == 0);
|
||||
CHECK(memcmp(out + 4 + 31, kTestKey, 16) == 0);
|
||||
|
||||
n = VmBuildSetDefaultScope(out, sizeof(out), NULL);
|
||||
CHECK(n == 4 && out[3] == 63);
|
||||
CHECK(VmBuildSetDefaultScope(out, sizeof(out), "$secret") == 0);
|
||||
|
||||
n = VmBuildGetDefaultScope(out, sizeof(out));
|
||||
CHECK(n == 4 && out[3] == 64);
|
||||
CHECK(VmBuildSetScopeKey(out, 10, kTestKey) == 0); /* too small */
|
||||
}
|
||||
|
||||
static void test_parse(void)
|
||||
{
|
||||
UInt8 p[1 + 31 + 16];
|
||||
Char name[VM_SCOPE_NAME_SIZE];
|
||||
UInt8 key[16];
|
||||
|
||||
memset(p, 0, sizeof(p));
|
||||
p[0] = VM_RESP_DEFAULT_FLOOD_SCOPE;
|
||||
memcpy(p + 1, "test", 4);
|
||||
memcpy(p + 32, kTestKey, 16);
|
||||
CHECK(VmParseDefaultScope(p, sizeof(p), name, key));
|
||||
CHECK(strcmp(name, "test") == 0 && memcmp(key, kTestKey, 16) == 0);
|
||||
|
||||
CHECK(VmParseDefaultScope(p, 1, name, key)); /* none */
|
||||
CHECK(name[0] == 0 && key[0] == 0);
|
||||
CHECK(!VmParseDefaultScope(p, 20, name, key)); /* truncated */
|
||||
p[0] = 0;
|
||||
CHECK(!VmParseDefaultScope(p, sizeof(p), name, key)); /* wrong code */
|
||||
|
||||
memset(p + 1, 'x', 31); /* no NUL in the field */
|
||||
p[0] = VM_RESP_DEFAULT_FLOOD_SCOPE;
|
||||
CHECK(VmParseDefaultScope(p, sizeof(p), name, key));
|
||||
CHECK(strlen(name) == 30);
|
||||
}
|
||||
|
||||
static void test_regions(void)
|
||||
{
|
||||
static const UInt8 frame[] = {
|
||||
VM_PUSH_BINARY_RESPONSE, 0, 0x78, 0x56, 0x34, 0x12, 1, 2, 3, 4,
|
||||
'*', ',', 'u', 's', '-', 'p', 'a', ',', '$', 'p', 'r', 'v', ',', 'p', 'h', 'l', 0
|
||||
};
|
||||
UInt8 key[32], out[64];
|
||||
const UInt8 *names;
|
||||
UInt16 len, pos = 0, n;
|
||||
UInt32 tag;
|
||||
Char name[VM_SCOPE_NAME_SIZE];
|
||||
|
||||
memset(key, 0xAB, sizeof(key));
|
||||
n = VmBuildRegionsReq(out, sizeof(out), key);
|
||||
CHECK(n == 3 + 35);
|
||||
CHECK(out[3] == VM_CMD_SEND_ANON_REQ && out[4] == 0xAB && out[35] == 0xAB);
|
||||
CHECK(out[36] == VM_ANON_REQ_REGIONS && out[37] == 0);
|
||||
|
||||
CHECK(VmParseRegionsReply(frame, sizeof(frame), &tag, &names, &len));
|
||||
CHECK(tag == 0x12345678);
|
||||
CHECK(len == 16); /* stops at the NUL */
|
||||
CHECK(VmNextRegionName(names, len, &pos, name) && strcmp(name, "*") == 0);
|
||||
CHECK(VmNextRegionName(names, len, &pos, name) && strcmp(name, "us-pa") == 0);
|
||||
CHECK(VmNextRegionName(names, len, &pos, name) && strcmp(name, "$prv") == 0);
|
||||
CHECK(VmNextRegionName(names, len, &pos, name) && strcmp(name, "phl") == 0);
|
||||
CHECK(!VmNextRegionName(names, len, &pos, name));
|
||||
|
||||
CHECK(VmParseRegionsReply(frame, 10, &tag, &names, &len) && len == 0); /* no regions */
|
||||
CHECK(!VmParseRegionsReply(frame, 9, &tag, &names, &len));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_key();
|
||||
test_names();
|
||||
test_frames();
|
||||
test_parse();
|
||||
test_regions();
|
||||
if (failures) {
|
||||
printf("%d failure(s)\n", failures);
|
||||
return 1;
|
||||
}
|
||||
printf("scopes: all tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user