palm-client: Node Settings screen (name, radio preset/params, TX power, path hash)
Settings validates against the firmware's limits before sending anything, then sends only what changed (SET_ADVERT_NAME, SET_RADIO_PARAMS with the client-repeat byte passed through from DEVICE_INFO, SET_RADIO_TX_POWER, SET_PATH_HASH_MODE), requires RESP_OK for each and names the step that failed, then re-reads DEVICE_INFO/SELF_INFO. The preset pop-up uses a snapshot of MeshCore's suggested presets (api.meshcore.nz, 27 entries) in tools/meshcore-presets.json; src/presets.c is generated from it by tools/gen_presets.py with exact decimal conversion. Radio state moves into session.c; TX power is now signed; adds fixed-point decimal parse/format helpers with host tests.
This commit is contained in:
1 parent
87e337b6fd
commit
5a347be52d
15 files changed
+1058
-35
No files matched your search
@@ -10,7 +10,7 @@ APP = visormesh
|
||||
NAME = VisorMesh
|
||||
CREATOR = VMsh
|
||||
|
||||
SRCS = src/main.c src/protocol.c src/transport_srm.c src/session.c src/ports.c
|
||||
SRCS = src/main.c src/protocol.c src/transport_srm.c src/session.c src/settings.c src/presets.c src/ports.c
|
||||
OBJS = $(SRCS:src/%.c=build/%.o)
|
||||
|
||||
CC = m68k-palmos-gcc
|
||||
|
||||
@@ -23,8 +23,60 @@ BEGIN
|
||||
LABEL "Firmware:" AUTOID AT (5 90) FONT 1
|
||||
LABEL " " ID MainFirmwareLabel AT (50 90)
|
||||
|
||||
BUTTON "Disconnect" ID MainConnectButton AT (20 140 AUTO AUTO)
|
||||
BUTTON "Ports" ID MainPortsButton AT (PREVRIGHT+10 PREVTOP AUTO AUTO)
|
||||
BUTTON "Disconnect" ID MainConnectButton AT (5 140 AUTO AUTO)
|
||||
BUTTON "Settings" ID MainSettingsButton AT (PREVRIGHT+6 PREVTOP AUTO AUTO)
|
||||
BUTTON "Ports" ID MainPortsButton AT (PREVRIGHT+6 PREVTOP AUTO AUTO)
|
||||
END
|
||||
|
||||
FORM ID SettingsForm AT (0 0 160 160)
|
||||
NOFRAME
|
||||
USABLE
|
||||
SAVEBEHIND
|
||||
BEGIN
|
||||
TITLE "Node Settings"
|
||||
LABEL "Name:" AUTOID AT (5 18) FONT 1
|
||||
FIELD ID SettingsNameField AT (45 18 110 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 31
|
||||
|
||||
LABEL "Preset:" AUTOID AT (5 34) FONT 1
|
||||
POPUPTRIGGER "Custom" ID SettingsPresetTrigger AT (45 33 AUTO AUTO) LEFTANCHOR
|
||||
LIST "" ID SettingsPresetList AT (40 33 118 AUTO) NONUSABLE VISIBLEITEMS 10
|
||||
POPUPLIST ID SettingsPresetTrigger SettingsPresetList
|
||||
|
||||
LABEL "Freq:" AUTOID AT (5 50) FONT 1
|
||||
FIELD ID SettingsFreqField AT (45 50 45 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 9
|
||||
LABEL "MHz" AUTOID AT (93 50)
|
||||
|
||||
LABEL "BW:" AUTOID AT (5 64) FONT 1
|
||||
FIELD ID SettingsBwField AT (45 64 30 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 6
|
||||
LABEL "kHz" AUTOID AT (78 64)
|
||||
|
||||
LABEL "SF:" AUTOID AT (5 78) FONT 1
|
||||
FIELD ID SettingsSfField AT (45 78 16 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 2 NUMERIC
|
||||
LABEL "CR:" AUTOID AT (75 78) FONT 1
|
||||
FIELD ID SettingsCrField AT (95 78 12 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 1 NUMERIC
|
||||
|
||||
LABEL "Power:" AUTOID AT (5 92) FONT 1
|
||||
FIELD ID SettingsPowerField AT (45 92 20 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 3
|
||||
LABEL "dBm" AUTOID AT (68 92)
|
||||
|
||||
LABEL "Path hash:" AUTOID AT (5 107) FONT 1
|
||||
PUSHBUTTON "1B" ID SettingsHash1Push AT (60 106 20 12) GROUP SettingsHashGroup
|
||||
PUSHBUTTON "2B" ID SettingsHash2Push AT (PREVRIGHT+1 PREVTOP 20 12) GROUP SettingsHashGroup
|
||||
PUSHBUTTON "3B" ID SettingsHash3Push AT (PREVRIGHT+1 PREVTOP 20 12) GROUP SettingsHashGroup
|
||||
|
||||
LABEL " " ID SettingsStatusLabel AT (5 124)
|
||||
|
||||
BUTTON "Save" ID SettingsSaveButton AT (5 140 AUTO AUTO)
|
||||
BUTTON "Cancel" ID SettingsCancelButton AT (PREVRIGHT+6 PREVTOP AUTO AUTO)
|
||||
GRAFFITISTATEINDICATOR AT (145 145)
|
||||
END
|
||||
|
||||
ALERT ID SettingsErrorAlert
|
||||
ERROR
|
||||
BEGIN
|
||||
TITLE "Settings"
|
||||
MESSAGE "^1"
|
||||
BUTTONS "OK"
|
||||
END
|
||||
|
||||
FORM ID PortsForm AT (0 0 160 160)
|
||||
|
||||
+18
-11
@@ -4,14 +4,11 @@
|
||||
#include "transport.h"
|
||||
#include "session.h"
|
||||
#include "ports.h"
|
||||
#include "settings.h"
|
||||
|
||||
#define VM_MIN_ROM_VERSION sysMakeROMVersion(3, 5, 0, sysROMStageRelease, 0)
|
||||
#define VM_LABEL_CHARS 22 /* the info labels in the .rcp are padded to this width */
|
||||
|
||||
/* Radio state shown on the main form; kept here so it survives a trip to the Ports form. */
|
||||
static VmDeviceInfo gDevice;
|
||||
static VmSelfInfo gSelf;
|
||||
|
||||
static Err RomVersionCompatible(UInt32 required, UInt16 launchFlags)
|
||||
{
|
||||
UInt32 romVersion;
|
||||
@@ -57,6 +54,8 @@ static void FormatBandwidth(Char *out, UInt32 hz)
|
||||
|
||||
static void ShowRadioInfo(FormType *form)
|
||||
{
|
||||
const VmDeviceInfo *dev = SessionDevice();
|
||||
const VmSelfInfo *self = SessionSelf();
|
||||
Char buf[40], bw[12];
|
||||
|
||||
if (!SessionIsConnected()) {
|
||||
@@ -68,15 +67,15 @@ static void ShowRadioInfo(FormType *form)
|
||||
CtlSetLabel(FrmGetObjectPtr(form, FrmGetObjectIndex(form, MainConnectButton)), "Connect");
|
||||
return;
|
||||
}
|
||||
SetLabel(form, MainNameLabel, gSelf.name);
|
||||
FormatFreq(buf, gSelf.freqKHz);
|
||||
SetLabel(form, MainNameLabel, self->name);
|
||||
FormatFreq(buf, self->freqKHz);
|
||||
SetLabel(form, MainFreqLabel, buf);
|
||||
FormatBandwidth(bw, gSelf.bwHz);
|
||||
StrPrintF(buf, "BW %s SF%u CR%u", bw, gSelf.sf, gSelf.cr);
|
||||
FormatBandwidth(bw, self->bwHz);
|
||||
StrPrintF(buf, "BW %s SF%u CR%u", bw, self->sf, self->cr);
|
||||
SetLabel(form, MainModeLabel, buf);
|
||||
StrPrintF(buf, "%u dBm (max %u)", gSelf.txPowerDbm, gSelf.maxTxPowerDbm);
|
||||
StrPrintF(buf, "%d dBm (max %u)", (Int16)(Int8)self->txPowerDbm, self->maxTxPowerDbm);
|
||||
SetLabel(form, MainPowerLabel, buf);
|
||||
StrPrintF(buf, "%s (%u)", gDevice.version, gDevice.fwVerCode);
|
||||
StrPrintF(buf, "%s (%u)", dev->version, dev->fwVerCode);
|
||||
SetLabel(form, MainFirmwareLabel, buf);
|
||||
CtlSetLabel(FrmGetObjectPtr(form, FrmGetObjectIndex(form, MainConnectButton)), "Disconnect");
|
||||
}
|
||||
@@ -93,7 +92,7 @@ static void ConnectOrDisconnect(FormType *form)
|
||||
}
|
||||
|
||||
SetLabel(form, MainNameLabel, "Connecting...");
|
||||
err = SessionConnect(&gDevice, &gSelf);
|
||||
err = SessionConnect();
|
||||
if (err == errNone) {
|
||||
ShowRadioInfo(form);
|
||||
return;
|
||||
@@ -140,6 +139,12 @@ static Boolean MainFormHandleEvent(EventType *event)
|
||||
case MainPortsButton:
|
||||
FrmGotoForm(PortsForm);
|
||||
return true;
|
||||
case MainSettingsButton:
|
||||
if (SessionIsConnected())
|
||||
FrmGotoForm(SettingsForm);
|
||||
else
|
||||
FrmCustomAlert(SettingsErrorAlert, "Connect to the radio first.", "", "");
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -162,6 +167,8 @@ static Boolean AppHandleEvent(EventType *event)
|
||||
FrmSetEventHandler(form, MainFormHandleEvent);
|
||||
else if (formId == PortsForm)
|
||||
FrmSetEventHandler(form, PortsFormHandleEvent);
|
||||
else if (formId == SettingsForm)
|
||||
FrmSetEventHandler(form, SettingsFormHandleEvent);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* MeshCore suggested radio presets. GENERATED by tools/gen_presets.py from
|
||||
* tools/meshcore-presets.json (https://api.meshcore.nz/api/v1/config, fetched 2026-11-01).
|
||||
* Don't edit by hand.
|
||||
*/
|
||||
#include "presets.h"
|
||||
|
||||
const VmPreset gPresets[] = {
|
||||
{ "Australia", 915800UL, 250000UL, 10, 5, 0 },
|
||||
{ "Australia (Narrow)", 916575UL, 62500UL, 7, 7, 0 },
|
||||
{ "Australia (Mid)", 915075UL, 125000UL, 9, 5, 0 },
|
||||
{ "Australia: SA, WA", 923125UL, 62500UL, 8, 8, 0 },
|
||||
{ "Australia: QLD", 923125UL, 62500UL, 8, 5, 0 },
|
||||
{ "Brazil", 923125UL, 62500UL, 8, 8, 0 },
|
||||
{ "Canada", 910525UL, 62500UL, 7, 5, 3 },
|
||||
{ "Costa Rica", 910525UL, 125000UL, 11, 5, 0 },
|
||||
{ "EU/UK (Narrow)", 869618UL, 62500UL, 8, 8, 0 },
|
||||
{ "EU/UK (Deprecated)", 869525UL, 250000UL, 11, 5, 0 },
|
||||
{ "Czech Republic (Narrow)", 869432UL, 62500UL, 7, 5, 2 },
|
||||
{ "EU 433MHz (Long Range)", 433650UL, 250000UL, 11, 5, 0 },
|
||||
{ "EU 433MHz (Narrow)", 433650UL, 62500UL, 8, 8, 0 },
|
||||
{ "Hungary", 869618UL, 62500UL, 7, 5, 2 },
|
||||
{ "Netherlands", 869618UL, 62500UL, 7, 5, 0 },
|
||||
{ "Netherlands (Limburg)", 869618UL, 62500UL, 8, 8, 2 },
|
||||
{ "New Zealand (Narrow)", 917375UL, 62500UL, 7, 5, 2 },
|
||||
{ "New Zealand (Gisborne)", 917375UL, 250000UL, 11, 5, 1 },
|
||||
{ "Portugal 433", 433375UL, 62500UL, 9, 6, 0 },
|
||||
{ "Portugal 868", 869618UL, 62500UL, 7, 6, 0 },
|
||||
{ "Slovakia", 869618UL, 62500UL, 7, 5, 2 },
|
||||
{ "Switzerland", 869618UL, 62500UL, 8, 8, 0 },
|
||||
{ "Thailand", 921000UL, 62500UL, 7, 5, 0 },
|
||||
{ "USA", 910525UL, 62500UL, 7, 5, 0 },
|
||||
{ "USA - Southern California", 927875UL, 62500UL, 7, 5, 3 },
|
||||
{ "Vietnam (Narrow)", 920250UL, 62500UL, 8, 5, 0 },
|
||||
{ "Vietnam (Deprecated)", 920250UL, 250000UL, 11, 5, 0 },
|
||||
};
|
||||
|
||||
const UInt16 gPresetCount = sizeof(gPresets) / sizeof(gPresets[0]);
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef VM_PRESETS_H
|
||||
#define VM_PRESETS_H
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
typedef struct {
|
||||
const Char *title;
|
||||
UInt32 freqKHz;
|
||||
UInt32 bwHz;
|
||||
UInt8 sf;
|
||||
UInt8 cr;
|
||||
UInt8 pathHashSize; /* bytes; 0 = preset doesn't specify one */
|
||||
} VmPreset;
|
||||
|
||||
extern const VmPreset gPresets[];
|
||||
extern const UInt16 gPresetCount;
|
||||
|
||||
#endif
|
||||
+151
-1
@@ -30,6 +30,88 @@ static void CopyName(Char *dst, UInt16 dstSize, const UInt8 *src, UInt16 n)
|
||||
dst[i] = 0;
|
||||
}
|
||||
|
||||
Boolean VmParseDecimal(const Char *s, UInt16 scale, UInt32 *out)
|
||||
{
|
||||
UInt32 v = 0;
|
||||
UInt16 decimals = 0, digits = 0;
|
||||
Boolean dot = 0;
|
||||
|
||||
for (; *s; s++) {
|
||||
if (*s == '.') {
|
||||
if (dot)
|
||||
return 0;
|
||||
dot = 1;
|
||||
continue;
|
||||
}
|
||||
if (*s < '0' || *s > '9')
|
||||
return 0;
|
||||
if (dot && ++decimals > scale)
|
||||
return 0;
|
||||
if (v > 400000000UL) /* keep v * 10 well inside 32 bits */
|
||||
return 0;
|
||||
v = v * 10 + (UInt32)(*s - '0');
|
||||
digits++;
|
||||
}
|
||||
if (digits == 0)
|
||||
return 0;
|
||||
for (; decimals < scale; decimals++) {
|
||||
if (v > 400000000UL)
|
||||
return 0;
|
||||
v *= 10;
|
||||
}
|
||||
*out = v;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void VmFormatDecimal(Char *out, UInt32 value, UInt16 scale)
|
||||
{
|
||||
Char tmp[12];
|
||||
UInt16 n = 0, i, intDigits, frac;
|
||||
Char *p = out;
|
||||
|
||||
do {
|
||||
tmp[n++] = (Char)('0' + value % 10);
|
||||
value /= 10;
|
||||
} while (value);
|
||||
while (n <= scale)
|
||||
tmp[n++] = '0'; /* at least one integer digit */
|
||||
intDigits = n - scale;
|
||||
for (i = 0; i < intDigits; i++)
|
||||
*p++ = tmp[n - 1 - i];
|
||||
/* fractional part without trailing zeros */
|
||||
frac = scale;
|
||||
while (frac > 0 && tmp[scale - frac] == '0')
|
||||
frac--;
|
||||
if (frac) {
|
||||
*p++ = '.';
|
||||
for (i = 0; i < frac; i++)
|
||||
*p++ = tmp[scale - 1 - i];
|
||||
}
|
||||
*p = 0;
|
||||
}
|
||||
|
||||
Boolean VmParseInt(const Char *s, Int32 *out)
|
||||
{
|
||||
Boolean neg = 0;
|
||||
Int32 v = 0;
|
||||
UInt16 digits = 0;
|
||||
|
||||
if (*s == '-') {
|
||||
neg = 1;
|
||||
s++;
|
||||
}
|
||||
for (; *s; s++) {
|
||||
if (*s < '0' || *s > '9' || digits >= 9)
|
||||
return 0;
|
||||
v = v * 10 + (*s - '0');
|
||||
digits++;
|
||||
}
|
||||
if (digits == 0)
|
||||
return 0;
|
||||
*out = neg ? -v : v;
|
||||
return 1;
|
||||
}
|
||||
|
||||
UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len)
|
||||
{
|
||||
if (len > VM_MAX_FRAME || outSize < (UInt16)(len + VM_HEADER_LEN))
|
||||
@@ -132,6 +214,70 @@ UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize)
|
||||
return VmEncodeFrame(out, outSize, p, 1);
|
||||
}
|
||||
|
||||
static void PutLE32(UInt8 *p, UInt32 v)
|
||||
{
|
||||
p[0] = (UInt8)v;
|
||||
p[1] = (UInt8)(v >> 8);
|
||||
p[2] = (UInt8)(v >> 16);
|
||||
p[3] = (UInt8)(v >> 24);
|
||||
}
|
||||
|
||||
/* CMD_SET_ADVERT_NAME: code, name (no terminator; the firmware keeps up to 31 chars). */
|
||||
UInt16 VmBuildSetAdvertName(UInt8 *out, UInt16 outSize, const Char *name)
|
||||
{
|
||||
UInt8 p[1 + VM_NAME_MAX];
|
||||
UInt16 n = 0;
|
||||
|
||||
p[0] = VM_CMD_SET_ADVERT_NAME;
|
||||
while (name[n] && n < VM_NAME_MAX) {
|
||||
p[1 + n] = (UInt8)name[n];
|
||||
n++;
|
||||
}
|
||||
if (n == 0)
|
||||
return 0;
|
||||
return VmEncodeFrame(out, outSize, p, (UInt16)(1 + n));
|
||||
}
|
||||
|
||||
/*
|
||||
* CMD_SET_RADIO_PARAMS: code, freq (kHz), bw (Hz), sf, cr, [client repeat, v9+].
|
||||
* Sending repeat = 0 turns client repeat off, so pass through what DEVICE_INFO reported.
|
||||
*/
|
||||
UInt16 VmBuildSetRadioParams(UInt8 *out, UInt16 outSize, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr,
|
||||
Boolean includeRepeat, Boolean repeat)
|
||||
{
|
||||
UInt8 p[12];
|
||||
|
||||
p[0] = VM_CMD_SET_RADIO_PARAMS;
|
||||
PutLE32(p + 1, freqKHz);
|
||||
PutLE32(p + 5, bwHz);
|
||||
p[9] = sf;
|
||||
p[10] = cr;
|
||||
if (!includeRepeat)
|
||||
return VmEncodeFrame(out, outSize, p, 11);
|
||||
p[11] = repeat ? 1 : 0;
|
||||
return VmEncodeFrame(out, outSize, p, 12);
|
||||
}
|
||||
|
||||
UInt16 VmBuildSetTxPower(UInt8 *out, UInt16 outSize, Int8 dbm)
|
||||
{
|
||||
UInt8 p[2];
|
||||
|
||||
p[0] = VM_CMD_SET_RADIO_TX_POWER;
|
||||
p[1] = (UInt8)dbm;
|
||||
return VmEncodeFrame(out, outSize, p, 2);
|
||||
}
|
||||
|
||||
/* CMD_SET_PATH_HASH_MODE: code, 0 (sub-command), mode (path hash bytes - 1). */
|
||||
UInt16 VmBuildSetPathHashMode(UInt8 *out, UInt16 outSize, UInt8 mode)
|
||||
{
|
||||
UInt8 p[3];
|
||||
|
||||
p[0] = VM_CMD_SET_PATH_HASH_MODE;
|
||||
p[1] = 0;
|
||||
p[2] = mode;
|
||||
return VmEncodeFrame(out, outSize, p, 3);
|
||||
}
|
||||
|
||||
/*
|
||||
* DEVICE_INFO: code, fw_ver_code, max_contacts / 2, max_channels, ble_pin (4),
|
||||
* build_date[12], manufacturer[40], version[20], then optional newer fields.
|
||||
@@ -146,6 +292,10 @@ Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out)
|
||||
CopyName(out->buildDate, sizeof(out->buildDate), p + 8, 12);
|
||||
CopyName(out->manufacturer, sizeof(out->manufacturer), p + 20, 40);
|
||||
CopyName(out->version, sizeof(out->version), p + 60, 20);
|
||||
out->hasRepeat = len > 80;
|
||||
out->repeatEnabled = out->hasRepeat && p[80] != 0;
|
||||
out->hasPathHashMode = len > 81;
|
||||
out->pathHashMode = out->hasPathHashMode ? p[81] : 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -159,7 +309,7 @@ Boolean VmParseSelfInfo(const UInt8 *p, UInt16 len, VmSelfInfo *out)
|
||||
if (len < SELF_INFO_FIXED_LEN || p[0] != VM_RESP_SELF_INFO)
|
||||
return 0;
|
||||
out->advType = p[1];
|
||||
out->txPowerDbm = p[2];
|
||||
out->txPowerDbm = (Int8)p[2];
|
||||
out->maxTxPowerDbm = p[3];
|
||||
CopyBytes(out->pubKey, p + 4, VM_PUB_KEY_SIZE);
|
||||
out->latE6 = (Int32)GetLE32(p + 36);
|
||||
|
||||
@@ -41,8 +41,12 @@ typedef char Char;
|
||||
/* Commands (app -> radio) */
|
||||
#define VM_CMD_APP_START 1
|
||||
#define VM_CMD_GET_CONTACTS 4
|
||||
#define VM_CMD_SET_ADVERT_NAME 8
|
||||
#define VM_CMD_SYNC_NEXT_MESSAGE 10
|
||||
#define VM_CMD_SET_RADIO_PARAMS 11
|
||||
#define VM_CMD_SET_RADIO_TX_POWER 12
|
||||
#define VM_CMD_DEVICE_QUERY 22
|
||||
#define VM_CMD_SET_PATH_HASH_MODE 61
|
||||
|
||||
/* Replies (radio -> app) */
|
||||
#define VM_RESP_OK 0
|
||||
@@ -80,11 +84,28 @@ typedef struct {
|
||||
Char buildDate[13];
|
||||
Char manufacturer[41];
|
||||
Char version[21];
|
||||
Boolean hasRepeat; /* firmware v9+ reports client repeat */
|
||||
Boolean repeatEnabled;
|
||||
Boolean hasPathHashMode; /* firmware v10+ reports the path hash mode */
|
||||
UInt8 pathHashMode; /* path hash size in bytes = mode + 1 */
|
||||
} VmDeviceInfo;
|
||||
|
||||
/* Limits the firmware enforces (MyMesh.cpp); checked before sending. */
|
||||
#define VM_FREQ_MIN_KHZ 150000UL
|
||||
#define VM_FREQ_MAX_KHZ 2500000UL
|
||||
#define VM_BW_MIN_HZ 7000UL
|
||||
#define VM_BW_MAX_HZ 500000UL
|
||||
#define VM_SF_MIN 5
|
||||
#define VM_SF_MAX 12
|
||||
#define VM_CR_MIN 5
|
||||
#define VM_CR_MAX 8
|
||||
#define VM_TX_POWER_MIN (-9)
|
||||
#define VM_NAME_MAX 31
|
||||
#define VM_PATH_HASH_MODE_MAX 2
|
||||
|
||||
typedef struct {
|
||||
UInt8 advType;
|
||||
UInt8 txPowerDbm;
|
||||
Int8 txPowerDbm; /* can be negative (down to -9) */
|
||||
UInt8 maxTxPowerDbm;
|
||||
UInt8 pubKey[VM_PUB_KEY_SIZE];
|
||||
Int32 latE6; /* degrees * 1e6 */
|
||||
@@ -120,6 +141,17 @@ typedef struct {
|
||||
UInt16 textLen;
|
||||
} VmMessage;
|
||||
|
||||
/*
|
||||
* Fixed-point decimals without floating point (the 68k has no FPU):
|
||||
* VmParseDecimal("910.525", 3) = 910525, ("62.5", 3) = 62500, ("921", 3) = 921000.
|
||||
* Returns false on anything but digits with at most one '.' and 'scale' decimals.
|
||||
* VmFormatDecimal(910525, 3) = "910.525", (62500, 3) = "62.5", (921000, 3) = "921".
|
||||
*/
|
||||
Boolean VmParseDecimal(const Char *s, UInt16 scale, UInt32 *out);
|
||||
void VmFormatDecimal(Char *out, UInt32 value, UInt16 scale);
|
||||
/* Optional leading '-', then digits. */
|
||||
Boolean VmParseInt(const Char *s, Int32 *out);
|
||||
|
||||
/* Framing */
|
||||
UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len);
|
||||
void VmDecoderReset(VmFrameDecoder *d);
|
||||
@@ -130,6 +162,11 @@ UInt16 VmBuildDeviceQuery(UInt8 *out, UInt16 outSize);
|
||||
UInt16 VmBuildAppStart(UInt8 *out, UInt16 outSize, const Char *appName);
|
||||
UInt16 VmBuildGetContacts(UInt8 *out, UInt16 outSize, UInt32 since);
|
||||
UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize);
|
||||
UInt16 VmBuildSetAdvertName(UInt8 *out, UInt16 outSize, const Char *name);
|
||||
UInt16 VmBuildSetRadioParams(UInt8 *out, UInt16 outSize, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr,
|
||||
Boolean includeRepeat, Boolean repeat);
|
||||
UInt16 VmBuildSetTxPower(UInt8 *out, UInt16 outSize, Int8 dbm);
|
||||
UInt16 VmBuildSetPathHashMode(UInt8 *out, UInt16 outSize, UInt8 mode);
|
||||
|
||||
/* Reply parsers: take a decoded payload (code byte first). Return true if it has the expected code and length. */
|
||||
Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out);
|
||||
|
||||
@@ -11,8 +11,28 @@
|
||||
#define MainModeLabel 1007
|
||||
#define MainPowerLabel 1008
|
||||
#define MainFirmwareLabel 1009
|
||||
#define MainSettingsButton 1010
|
||||
#define MainLinkGroup 1
|
||||
|
||||
#define SettingsForm 1200
|
||||
#define SettingsNameField 1201
|
||||
#define SettingsPresetTrigger 1202
|
||||
#define SettingsPresetList 1203
|
||||
#define SettingsFreqField 1204
|
||||
#define SettingsBwField 1205
|
||||
#define SettingsSfField 1206
|
||||
#define SettingsCrField 1207
|
||||
#define SettingsPowerField 1208
|
||||
#define SettingsHash1Push 1209
|
||||
#define SettingsHash2Push 1210
|
||||
#define SettingsHash3Push 1211
|
||||
#define SettingsStatusLabel 1212
|
||||
#define SettingsSaveButton 1213
|
||||
#define SettingsCancelButton 1214
|
||||
#define SettingsHashGroup 2
|
||||
|
||||
#define SettingsErrorAlert 2001
|
||||
|
||||
#define PortsForm 1100
|
||||
#define PortsDoneButton 1101
|
||||
|
||||
|
||||
+50
-18
@@ -10,6 +10,8 @@
|
||||
static VmFrameDecoder gDecoder;
|
||||
static Boolean gConnected = false;
|
||||
static Boolean gMsgWaiting = false;
|
||||
static VmDeviceInfo gDevice;
|
||||
static VmSelfInfo gSelf;
|
||||
|
||||
/*
|
||||
* Reads frames until one with the wanted code arrives. Pushes are noted and
|
||||
@@ -61,12 +63,31 @@ static Err Request(const UInt8 *frame, UInt16 len, UInt8 code, UInt16 tries)
|
||||
return err == serErrTimeOut ? vmErrNoReply : err;
|
||||
}
|
||||
|
||||
Err SessionConnect(VmDeviceInfo *device, VmSelfInfo *self)
|
||||
/* DEVICE_QUERY + APP_START into gDevice/gSelf. The first request gets extra tries (USB attach). */
|
||||
static Err QueryRadio(UInt16 firstTries)
|
||||
{
|
||||
UInt8 frame[VM_HEADER_LEN + 8 + VM_NAME_SIZE];
|
||||
UInt16 len;
|
||||
Err err;
|
||||
|
||||
len = VmBuildDeviceQuery(frame, sizeof(frame));
|
||||
err = Request(frame, len, VM_RESP_DEVICE_INFO, firstTries);
|
||||
if (err == errNone && !VmParseDeviceInfo(gDecoder.payload, gDecoder.len, &gDevice))
|
||||
err = vmErrBadReply;
|
||||
if (err != errNone)
|
||||
return err;
|
||||
|
||||
len = VmBuildAppStart(frame, sizeof(frame), VM_APP_NAME);
|
||||
err = Request(frame, len, VM_RESP_SELF_INFO, 1);
|
||||
if (err == errNone && !VmParseSelfInfo(gDecoder.payload, gDecoder.len, &gSelf))
|
||||
err = vmErrBadReply;
|
||||
return err;
|
||||
}
|
||||
|
||||
Err SessionConnect(void)
|
||||
{
|
||||
Err err;
|
||||
|
||||
SessionDisconnect();
|
||||
err = TransportOpen();
|
||||
if (err != errNone)
|
||||
@@ -78,26 +99,37 @@ Err SessionConnect(VmDeviceInfo *device, VmSelfInfo *self)
|
||||
if (TransportGetPort() == VM_PORT_USB)
|
||||
SysTaskDelay(SysTicksPerSecond());
|
||||
|
||||
len = VmBuildDeviceQuery(frame, sizeof(frame));
|
||||
err = Request(frame, len, VM_RESP_DEVICE_INFO, VM_CONNECT_TRIES);
|
||||
if (err == errNone && !VmParseDeviceInfo(gDecoder.payload, gDecoder.len, device))
|
||||
err = vmErrBadReply;
|
||||
if (err != errNone)
|
||||
goto fail;
|
||||
|
||||
len = VmBuildAppStart(frame, sizeof(frame), VM_APP_NAME);
|
||||
err = Request(frame, len, VM_RESP_SELF_INFO, 1);
|
||||
if (err == errNone && !VmParseSelfInfo(gDecoder.payload, gDecoder.len, self))
|
||||
err = vmErrBadReply;
|
||||
if (err != errNone)
|
||||
goto fail;
|
||||
|
||||
err = QueryRadio(VM_CONNECT_TRIES);
|
||||
if (err != errNone) {
|
||||
TransportClose();
|
||||
return err;
|
||||
}
|
||||
gConnected = true;
|
||||
return errNone;
|
||||
}
|
||||
|
||||
fail:
|
||||
TransportClose();
|
||||
return err;
|
||||
Err SessionRefresh(void)
|
||||
{
|
||||
if (!gConnected)
|
||||
return serErrNotOpen;
|
||||
return QueryRadio(1);
|
||||
}
|
||||
|
||||
Err SessionCommand(const UInt8 *frame, UInt16 len)
|
||||
{
|
||||
if (!gConnected)
|
||||
return serErrNotOpen;
|
||||
return Request(frame, len, VM_RESP_OK, 1);
|
||||
}
|
||||
|
||||
const VmDeviceInfo *SessionDevice(void)
|
||||
{
|
||||
return &gDevice;
|
||||
}
|
||||
|
||||
const VmSelfInfo *SessionSelf(void)
|
||||
{
|
||||
return &gSelf;
|
||||
}
|
||||
|
||||
void SessionDisconnect(void)
|
||||
|
||||
@@ -14,9 +14,18 @@
|
||||
#define vmErrBadReply (appErrorClass | 3) /* reply had the right code but didn't parse */
|
||||
|
||||
/* Opens the link and runs DEVICE_QUERY + APP_START. On error the link is closed. */
|
||||
Err SessionConnect(VmDeviceInfo *device, VmSelfInfo *self);
|
||||
Err SessionConnect(void);
|
||||
void SessionDisconnect(void);
|
||||
Boolean SessionIsConnected(void);
|
||||
Boolean SessionMessageWaiting(void);
|
||||
|
||||
/* What the radio last reported (valid while connected). */
|
||||
const VmDeviceInfo *SessionDevice(void);
|
||||
const VmSelfInfo *SessionSelf(void);
|
||||
|
||||
/* Sends a complete command frame and requires RESP_OK. */
|
||||
Err SessionCommand(const UInt8 *frame, UInt16 len);
|
||||
/* Re-reads DEVICE_INFO and SELF_INFO, e.g. after changing settings. */
|
||||
Err SessionRefresh(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Node settings: name, radio parameters (with MeshCore's suggested presets),
|
||||
* TX power and path hash size. Save sends only what changed, each command
|
||||
* must come back RESP_OK, then the radio is re-read.
|
||||
*/
|
||||
#include <PalmOS.h>
|
||||
#include "resources.h"
|
||||
#include "protocol.h"
|
||||
#include "presets.h"
|
||||
#include "session.h"
|
||||
#include "settings.h"
|
||||
|
||||
#define CUSTOM_LABEL "Custom"
|
||||
|
||||
static const Char *gPresetTitles[64]; /* list choices; more than the snapshot has */
|
||||
|
||||
static void *GetObject(FormType *form, UInt16 id)
|
||||
{
|
||||
return FrmGetObjectPtr(form, FrmGetObjectIndex(form, id));
|
||||
}
|
||||
|
||||
static void SetFieldText(FormType *form, UInt16 id, const Char *text)
|
||||
{
|
||||
FieldType *field = GetObject(form, id);
|
||||
MemHandle oldH = FldGetTextHandle(field);
|
||||
MemHandle h = MemHandleNew(StrLen(text) + 1);
|
||||
|
||||
if (!h)
|
||||
return;
|
||||
StrCopy(MemHandleLock(h), text);
|
||||
MemHandleUnlock(h);
|
||||
FldSetTextHandle(field, h);
|
||||
if (oldH)
|
||||
MemHandleFree(oldH);
|
||||
if (FrmVisible(form))
|
||||
FldDrawField(field);
|
||||
}
|
||||
|
||||
static const Char *GetFieldText(FormType *form, UInt16 id)
|
||||
{
|
||||
const Char *t = FldGetTextPtr(GetObject(form, id));
|
||||
|
||||
return t ? t : "";
|
||||
}
|
||||
|
||||
static void SetStatus(FormType *form, const Char *text)
|
||||
{
|
||||
UInt16 index = FrmGetObjectIndex(form, SettingsStatusLabel);
|
||||
|
||||
FrmHideObject(form, index);
|
||||
FrmCopyLabel(form, SettingsStatusLabel, text);
|
||||
FrmShowObject(form, index);
|
||||
}
|
||||
|
||||
static void ShowError(const Char *msg)
|
||||
{
|
||||
FrmCustomAlert(SettingsErrorAlert, msg, "", "");
|
||||
}
|
||||
|
||||
static void SelectHashSize(FormType *form, UInt8 bytes)
|
||||
{
|
||||
UInt16 id = bytes == 3 ? SettingsHash3Push : bytes == 2 ? SettingsHash2Push : SettingsHash1Push;
|
||||
|
||||
FrmSetControlGroupSelection(form, SettingsHashGroup, id);
|
||||
}
|
||||
|
||||
static UInt8 SelectedHashSize(FormType *form)
|
||||
{
|
||||
UInt16 id = FrmGetObjectId(form, FrmGetControlGroupSelection(form, SettingsHashGroup));
|
||||
|
||||
return id == SettingsHash3Push ? 3 : id == SettingsHash2Push ? 2 : 1;
|
||||
}
|
||||
|
||||
static void SetRadioFields(FormType *form, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr)
|
||||
{
|
||||
Char buf[16];
|
||||
|
||||
VmFormatDecimal(buf, freqKHz, 3);
|
||||
SetFieldText(form, SettingsFreqField, buf);
|
||||
VmFormatDecimal(buf, bwHz, 3);
|
||||
SetFieldText(form, SettingsBwField, buf);
|
||||
StrIToA(buf, sf);
|
||||
SetFieldText(form, SettingsSfField, buf);
|
||||
StrIToA(buf, cr);
|
||||
SetFieldText(form, SettingsCrField, buf);
|
||||
}
|
||||
|
||||
/* Index of the preset matching these radio settings, or -1. */
|
||||
static Int16 FindPreset(UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr)
|
||||
{
|
||||
UInt16 i;
|
||||
|
||||
for (i = 0; i < gPresetCount; i++)
|
||||
if (gPresets[i].freqKHz == freqKHz && gPresets[i].bwHz == bwHz &&
|
||||
gPresets[i].sf == sf && gPresets[i].cr == cr)
|
||||
return (Int16)i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void InitForm(FormType *form)
|
||||
{
|
||||
const VmSelfInfo *self = SessionSelf();
|
||||
const VmDeviceInfo *dev = SessionDevice();
|
||||
ListType *list = GetObject(form, SettingsPresetList);
|
||||
ControlType *trigger = GetObject(form, SettingsPresetTrigger);
|
||||
UInt16 i, n = gPresetCount;
|
||||
Int16 match;
|
||||
Char buf[8];
|
||||
|
||||
if (n > sizeof(gPresetTitles) / sizeof(gPresetTitles[0]))
|
||||
n = sizeof(gPresetTitles) / sizeof(gPresetTitles[0]);
|
||||
for (i = 0; i < n; i++)
|
||||
gPresetTitles[i] = gPresets[i].title;
|
||||
LstSetListChoices(list, (Char **)gPresetTitles, n);
|
||||
LstSetHeight(list, n < 10 ? n : 10);
|
||||
|
||||
SetFieldText(form, SettingsNameField, self->name);
|
||||
SetRadioFields(form, self->freqKHz, self->bwHz, self->sf, self->cr);
|
||||
StrIToA(buf, self->txPowerDbm);
|
||||
SetFieldText(form, SettingsPowerField, buf);
|
||||
SelectHashSize(form, (UInt8)(dev->pathHashMode + 1));
|
||||
if (!dev->hasPathHashMode) {
|
||||
CtlSetEnabled(GetObject(form, SettingsHash1Push), false);
|
||||
CtlSetEnabled(GetObject(form, SettingsHash2Push), false);
|
||||
CtlSetEnabled(GetObject(form, SettingsHash3Push), false);
|
||||
}
|
||||
|
||||
match = FindPreset(self->freqKHz, self->bwHz, self->sf, self->cr);
|
||||
if (match >= 0) {
|
||||
LstSetSelection(list, match);
|
||||
CtlSetLabel(trigger, gPresets[match].title);
|
||||
} else {
|
||||
LstSetSelection(list, noListSelection);
|
||||
CtlSetLabel(trigger, CUSTOM_LABEL);
|
||||
}
|
||||
}
|
||||
|
||||
static void ApplyPreset(FormType *form, UInt16 index)
|
||||
{
|
||||
const VmPreset *p = &gPresets[index];
|
||||
|
||||
SetRadioFields(form, p->freqKHz, p->bwHz, p->sf, p->cr);
|
||||
if (p->pathHashSize && SessionDevice()->hasPathHashMode)
|
||||
SelectHashSize(form, p->pathHashSize);
|
||||
}
|
||||
|
||||
/* Sends one command, reporting which setting failed. Returns true on RESP_OK. */
|
||||
static Boolean Send(FormType *form, const UInt8 *frame, UInt16 len, const Char *what)
|
||||
{
|
||||
Char msg[64];
|
||||
Err err;
|
||||
|
||||
StrPrintF(msg, "Setting %s...", what);
|
||||
SetStatus(form, msg);
|
||||
err = SessionCommand(frame, len);
|
||||
if (err == errNone)
|
||||
return true;
|
||||
if (err == vmErrRadio)
|
||||
StrPrintF(msg, "The radio rejected the %s.", what);
|
||||
else if (err == vmErrNoReply)
|
||||
StrPrintF(msg, "No reply while setting the %s.", what);
|
||||
else
|
||||
StrPrintF(msg, "Error 0x%x while setting the %s.", err, what);
|
||||
SetStatus(form, "");
|
||||
ShowError(msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
static Boolean Save(FormType *form)
|
||||
{
|
||||
const VmSelfInfo *self = SessionSelf();
|
||||
const VmDeviceInfo *dev = SessionDevice();
|
||||
const Char *name = GetFieldText(form, SettingsNameField);
|
||||
UInt8 frame[VM_HEADER_LEN + 1 + VM_NAME_MAX];
|
||||
UInt32 freq, bw;
|
||||
Int32 sf, cr, power;
|
||||
UInt8 hashMode = (UInt8)(SelectedHashSize(form) - 1);
|
||||
UInt16 len;
|
||||
Char msg[64];
|
||||
|
||||
/* Validate everything first, with the firmware's limits, so nothing is half-applied. */
|
||||
if (StrLen(name) == 0 || StrLen(name) > VM_NAME_MAX) {
|
||||
ShowError("Enter a node name (1-31 characters).");
|
||||
return false;
|
||||
}
|
||||
if (!VmParseDecimal(GetFieldText(form, SettingsFreqField), 3, &freq) ||
|
||||
freq < VM_FREQ_MIN_KHZ || freq > VM_FREQ_MAX_KHZ) {
|
||||
ShowError("Frequency must be 150-2500 MHz, up to 3 decimals.");
|
||||
return false;
|
||||
}
|
||||
if (!VmParseDecimal(GetFieldText(form, SettingsBwField), 3, &bw) ||
|
||||
bw < VM_BW_MIN_HZ || bw > VM_BW_MAX_HZ) {
|
||||
ShowError("Bandwidth must be 7-500 kHz.");
|
||||
return false;
|
||||
}
|
||||
if (!VmParseInt(GetFieldText(form, SettingsSfField), &sf) || sf < VM_SF_MIN || sf > VM_SF_MAX) {
|
||||
ShowError("SF must be 5-12.");
|
||||
return false;
|
||||
}
|
||||
if (!VmParseInt(GetFieldText(form, SettingsCrField), &cr) || cr < VM_CR_MIN || cr > VM_CR_MAX) {
|
||||
ShowError("CR must be 5-8.");
|
||||
return false;
|
||||
}
|
||||
if (!VmParseInt(GetFieldText(form, SettingsPowerField), &power) ||
|
||||
power < VM_TX_POWER_MIN || power > self->maxTxPowerDbm) {
|
||||
StrPrintF(msg, "TX power must be %d to %u dBm.", VM_TX_POWER_MIN, self->maxTxPowerDbm);
|
||||
ShowError(msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StrCompare(name, self->name) != 0) {
|
||||
len = VmBuildSetAdvertName(frame, sizeof(frame), name);
|
||||
if (!Send(form, frame, len, "name"))
|
||||
return false;
|
||||
}
|
||||
if (freq != self->freqKHz || bw != self->bwHz || sf != self->sf || cr != self->cr) {
|
||||
len = VmBuildSetRadioParams(frame, sizeof(frame), freq, bw, (UInt8)sf, (UInt8)cr,
|
||||
dev->hasRepeat, dev->repeatEnabled);
|
||||
if (!Send(form, frame, len, "radio settings"))
|
||||
return false;
|
||||
}
|
||||
if (power != self->txPowerDbm) {
|
||||
len = VmBuildSetTxPower(frame, sizeof(frame), (Int8)power);
|
||||
if (!Send(form, frame, len, "TX power"))
|
||||
return false;
|
||||
}
|
||||
if (dev->hasPathHashMode && hashMode != dev->pathHashMode) {
|
||||
len = VmBuildSetPathHashMode(frame, sizeof(frame), hashMode);
|
||||
if (!Send(form, frame, len, "path hash size"))
|
||||
return false;
|
||||
}
|
||||
|
||||
SetStatus(form, "Reading back...");
|
||||
if (SessionRefresh() != errNone) {
|
||||
SetStatus(form, "");
|
||||
ShowError("Saved, but reading the settings back failed. Reconnect to check them.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Boolean SettingsFormHandleEvent(EventType *event)
|
||||
{
|
||||
FormType *form = FrmGetActiveForm();
|
||||
|
||||
switch (event->eType) {
|
||||
case frmOpenEvent:
|
||||
InitForm(form);
|
||||
FrmDrawForm(form);
|
||||
FrmSetFocus(form, FrmGetObjectIndex(form, SettingsNameField));
|
||||
return true;
|
||||
case popSelectEvent:
|
||||
if (event->data.popSelect.listID == SettingsPresetList && event->data.popSelect.selection >= 0)
|
||||
ApplyPreset(form, (UInt16)event->data.popSelect.selection);
|
||||
return false; /* let the OS update the trigger's label */
|
||||
case ctlSelectEvent:
|
||||
switch (event->data.ctlSelect.controlID) {
|
||||
case SettingsSaveButton:
|
||||
if (Save(form))
|
||||
FrmGotoForm(MainForm);
|
||||
return true;
|
||||
case SettingsCancelButton:
|
||||
FrmGotoForm(MainForm);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef VM_SETTINGS_H
|
||||
#define VM_SETTINGS_H
|
||||
|
||||
#include <PalmOS.h>
|
||||
|
||||
Boolean SettingsFormHandleEvent(EventType *event);
|
||||
|
||||
#endif
|
||||
@@ -115,7 +115,10 @@ static void test_decode_real_device_info(void)
|
||||
CHECK(strcmp(info.buildDate, "14 Aug 2026") == 0);
|
||||
CHECK(strcmp(info.manufacturer, "Seeed Xiao-nrf52") == 0);
|
||||
CHECK(strcmp(info.version, "v1.17.1") == 0);
|
||||
CHECK(info.hasRepeat && !info.repeatEnabled);
|
||||
CHECK(info.hasPathHashMode && info.pathHashMode == 0);
|
||||
CHECK(!VmParseDeviceInfo(d.payload, 79, &info));
|
||||
CHECK(VmParseDeviceInfo(d.payload, 80, &info) && !info.hasRepeat && !info.hasPathHashMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +148,68 @@ static void test_builders_match_bench_frames(void)
|
||||
CHECK(VmBuildAppStart(out, 10, "VisorMesh") == 0);
|
||||
}
|
||||
|
||||
/* Settings commands, laid out per the MyMesh.cpp handlers (multi-byte fields little-endian). */
|
||||
static void test_settings_builders(void)
|
||||
{
|
||||
UInt8 out[64];
|
||||
static const UInt8 name[] = { 0x3c, 0x09, 0x00, 0x08, 'V', 'i', 's', 'o', 'r', 'N', 'e', 'o' };
|
||||
/* USA preset: 910.525 MHz (0x000de4bd), BW 62.5 kHz (62500 = 0x0000f424), SF7, CR5, repeat off */
|
||||
static const UInt8 radio[] = {
|
||||
0x3c, 0x0c, 0x00, 0x0b, 0xbd, 0xe4, 0x0d, 0x00, 0x24, 0xf4, 0x00, 0x00, 0x07, 0x05, 0x00
|
||||
};
|
||||
static const UInt8 power[] = { 0x3c, 0x02, 0x00, 0x0c, 0xf7 };
|
||||
static const UInt8 hash[] = { 0x3c, 0x03, 0x00, 0x3d, 0x00, 0x02 };
|
||||
|
||||
CHECK(VmBuildSetAdvertName(out, sizeof(out), "VisorNeo") == sizeof(name));
|
||||
CHECK(memcmp(out, name, sizeof(name)) == 0);
|
||||
CHECK(VmBuildSetAdvertName(out, sizeof(out), "") == 0);
|
||||
CHECK(VmBuildSetAdvertName(out, sizeof(out), "0123456789012345678901234567890123456789") == 3 + 1 + 31);
|
||||
|
||||
CHECK(VmBuildSetRadioParams(out, sizeof(out), 910525UL, 62500UL, 7, 5, 1, 0) == sizeof(radio));
|
||||
CHECK(memcmp(out, radio, sizeof(radio)) == 0);
|
||||
CHECK(VmBuildSetRadioParams(out, sizeof(out), 910525UL, 62500UL, 7, 5, 0, 0) == sizeof(radio) - 1);
|
||||
CHECK(out[1] == 0x0b);
|
||||
CHECK(VmBuildSetRadioParams(out, sizeof(out), 910525UL, 62500UL, 7, 5, 1, 1) == sizeof(radio));
|
||||
CHECK(out[sizeof(radio) - 1] == 1);
|
||||
|
||||
CHECK(VmBuildSetTxPower(out, sizeof(out), -9) == sizeof(power));
|
||||
CHECK(memcmp(out, power, sizeof(power)) == 0);
|
||||
CHECK(VmBuildSetPathHashMode(out, sizeof(out), 2) == sizeof(hash));
|
||||
CHECK(memcmp(out, hash, sizeof(hash)) == 0);
|
||||
}
|
||||
|
||||
static void test_decimals(void)
|
||||
{
|
||||
UInt32 v;
|
||||
Int32 i;
|
||||
Char buf[16];
|
||||
|
||||
CHECK(VmParseDecimal("910.525", 3, &v) && v == 910525UL);
|
||||
CHECK(VmParseDecimal("921", 3, &v) && v == 921000UL);
|
||||
CHECK(VmParseDecimal("62.5", 3, &v) && v == 62500UL);
|
||||
CHECK(VmParseDecimal("250", 3, &v) && v == 250000UL);
|
||||
CHECK(VmParseDecimal("0.5", 3, &v) && v == 500UL);
|
||||
CHECK(VmParseDecimal(".5", 3, &v) && v == 500UL);
|
||||
CHECK(!VmParseDecimal("910.5255", 3, &v));
|
||||
CHECK(!VmParseDecimal("9.1.0", 3, &v));
|
||||
CHECK(!VmParseDecimal("", 3, &v));
|
||||
CHECK(!VmParseDecimal(".", 3, &v));
|
||||
CHECK(!VmParseDecimal("91a", 3, &v));
|
||||
CHECK(!VmParseDecimal("99999999999", 3, &v));
|
||||
|
||||
VmFormatDecimal(buf, 910525UL, 3); CHECK(strcmp(buf, "910.525") == 0);
|
||||
VmFormatDecimal(buf, 921000UL, 3); CHECK(strcmp(buf, "921") == 0);
|
||||
VmFormatDecimal(buf, 62500UL, 3); CHECK(strcmp(buf, "62.5") == 0);
|
||||
VmFormatDecimal(buf, 7800UL, 3); CHECK(strcmp(buf, "7.8") == 0);
|
||||
VmFormatDecimal(buf, 500UL, 3); CHECK(strcmp(buf, "0.5") == 0);
|
||||
VmFormatDecimal(buf, 0UL, 3); CHECK(strcmp(buf, "0") == 0);
|
||||
VmFormatDecimal(buf, 869618UL, 3); CHECK(strcmp(buf, "869.618") == 0);
|
||||
|
||||
CHECK(VmParseInt("22", &i) && i == 22);
|
||||
CHECK(VmParseInt("-9", &i) && i == -9);
|
||||
CHECK(!VmParseInt("-", &i) && !VmParseInt("", &i) && !VmParseInt("2x", &i));
|
||||
}
|
||||
|
||||
static void test_parse_self_info(void)
|
||||
{
|
||||
VmFrameDecoder d;
|
||||
@@ -248,6 +313,8 @@ int main(void)
|
||||
test_decode_max_frame();
|
||||
test_decode_real_device_info();
|
||||
test_builders_match_bench_frames();
|
||||
test_settings_builders();
|
||||
test_decimals();
|
||||
test_parse_self_info();
|
||||
test_parse_contacts();
|
||||
test_parse_messages();
|
||||
|
||||
Executable
+69
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generates src/presets.c from tools/meshcore-presets.json (a snapshot of MeshCore's
|
||||
suggested radio presets; the Visor has no internet to fetch them).
|
||||
|
||||
tools/gen_presets.py # regenerate from the snapshot
|
||||
tools/gen_presets.py --fetch # refresh the snapshot first
|
||||
|
||||
Frequencies become kHz and bandwidths Hz, converted from the decimal strings
|
||||
exactly (no floating point).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
from decimal import Decimal
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
SNAPSHOT = os.path.join(HERE, 'meshcore-presets.json')
|
||||
OUT = os.path.join(HERE, '..', 'src', 'presets.c')
|
||||
URL = 'https://api.meshcore.nz/api/v1/config'
|
||||
|
||||
|
||||
def fetch():
|
||||
req = urllib.request.Request(URL, headers={'User-Agent': 'VisorMesh (Palm client preset snapshot)'})
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
data = json.load(r)
|
||||
import datetime
|
||||
snap = {'source': URL, 'fetched': datetime.date.today().isoformat(),
|
||||
'entries': data['config']['suggested_radio_settings']['entries']}
|
||||
with open(SNAPSHOT, 'w') as f:
|
||||
json.dump(snap, f, indent=2)
|
||||
f.write('\n')
|
||||
|
||||
|
||||
def c_string(s):
|
||||
return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
|
||||
|
||||
|
||||
def main():
|
||||
if '--fetch' in sys.argv:
|
||||
fetch()
|
||||
snap = json.load(open(SNAPSHOT))
|
||||
rows = []
|
||||
for e in snap['entries']:
|
||||
title = e['title'].encode('ascii', 'replace').decode()
|
||||
khz = int(Decimal(e['frequency']) * 1000)
|
||||
hz = int(Decimal(e['bandwidth']) * 1000)
|
||||
sf, cr = int(e['spreading_factor']), int(e['coding_rate'])
|
||||
hash_size = int(e.get('network_settings', {}).get('path_hash_size', 0))
|
||||
rows.append(f'\t{{ {c_string(title)}, {khz}UL, {hz}UL, {sf}, {cr}, {hash_size} }},')
|
||||
with open(OUT, 'w') as f:
|
||||
f.write(f'''/*
|
||||
* MeshCore suggested radio presets. GENERATED by tools/gen_presets.py from
|
||||
* tools/meshcore-presets.json ({snap["source"]}, fetched {snap["fetched"]}).
|
||||
* Don't edit by hand.
|
||||
*/
|
||||
#include "presets.h"
|
||||
|
||||
const VmPreset gPresets[] = {{
|
||||
''' + '\n'.join(rows) + f'''
|
||||
}};
|
||||
|
||||
const UInt16 gPresetCount = sizeof(gPresets) / sizeof(gPresets[0]);
|
||||
''')
|
||||
print(f'{len(rows)} presets -> {os.path.relpath(OUT)}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,246 @@
|
||||
{
|
||||
"source": "https://api.meshcore.nz/api/v1/config",
|
||||
"fetched": "2026-11-01",
|
||||
"entries": [
|
||||
{
|
||||
"title": "Australia",
|
||||
"description": "915.800MHz / SF10 / BW250 / CR5",
|
||||
"frequency": "915.800",
|
||||
"spreading_factor": "10",
|
||||
"bandwidth": "250",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Australia (Narrow)",
|
||||
"description": "916.575MHz / SF7 / BW62.5 / CR7",
|
||||
"frequency": "916.575",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "7"
|
||||
},
|
||||
{
|
||||
"title": "Australia (Mid)",
|
||||
"description": "915.075MHz / SF9 / BW125 / CR5",
|
||||
"frequency": "915.075",
|
||||
"spreading_factor": "9",
|
||||
"bandwidth": "125",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Australia: SA, WA",
|
||||
"description": "923.125MHz / SF8 / BW62.5 / CR8",
|
||||
"frequency": "923.125",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8"
|
||||
},
|
||||
{
|
||||
"title": "Australia: QLD",
|
||||
"description": "923.125MHz / SF8 / BW62.5 / CR5",
|
||||
"frequency": "923.125",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Brazil",
|
||||
"description": "923.125MHz / SF8 / BW62.5 / CR8",
|
||||
"frequency": "923.125",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8"
|
||||
},
|
||||
{
|
||||
"title": "Canada",
|
||||
"description": "910.525MHz / SF7 / BW62.5 / CR5 / 3B",
|
||||
"frequency": "910.525",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Costa Rica",
|
||||
"description": "910.525MHz / SF11 / BW125 / CR5",
|
||||
"frequency": "910.525",
|
||||
"spreading_factor": "11",
|
||||
"bandwidth": "125",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "EU/UK (Narrow)",
|
||||
"description": "869.618MHz / SF8 / BW62.5 / CR8",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8"
|
||||
},
|
||||
{
|
||||
"title": "EU/UK (Deprecated)",
|
||||
"description": "869.525MHz / SF11 / BW250 / CR5",
|
||||
"frequency": "869.525",
|
||||
"spreading_factor": "11",
|
||||
"bandwidth": "250",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Czech Republic (Narrow)",
|
||||
"description": "869.432MHz / SF7 / BW62.5 / CR5 / 2B",
|
||||
"frequency": "869.432",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "EU 433MHz (Long Range)",
|
||||
"description": "433.650MHz / SF11 / BW250 / CR5",
|
||||
"frequency": "433.650",
|
||||
"spreading_factor": "11",
|
||||
"bandwidth": "250",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "EU 433MHz (Narrow)",
|
||||
"description": "433.650MHz / SF8 / BW62.5 / CR8",
|
||||
"frequency": "433.650",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8"
|
||||
},
|
||||
{
|
||||
"title": "Hungary",
|
||||
"description": "869.618MHz / SF7 / BW62.5 / CR5 / 2B",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Netherlands",
|
||||
"description": "869.618MHz / SF7 / BW62.5 / CR5",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Netherlands (Limburg)",
|
||||
"description": "869.618MHz / SF8 / BW62.5 / CR8 / 2B",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8",
|
||||
"network_settings": {
|
||||
"path_hash_size": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "New Zealand (Narrow)",
|
||||
"description": "917.375MHz / SF7 / BW62.5 / CR5 / 2B",
|
||||
"frequency": "917.375",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "New Zealand (Gisborne)",
|
||||
"description": "917.375MHz / SF11 / BW250 / CR5 / 1B",
|
||||
"frequency": "917.375",
|
||||
"spreading_factor": "11",
|
||||
"bandwidth": "250",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Portugal 433",
|
||||
"description": "433.375MHz / SF9 / BW62.5 / CR6",
|
||||
"frequency": "433.375",
|
||||
"spreading_factor": "9",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "6"
|
||||
},
|
||||
{
|
||||
"title": "Portugal 868",
|
||||
"description": "869.618MHz / SF7 / BW62.5 / CR6",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "6"
|
||||
},
|
||||
{
|
||||
"title": "Slovakia",
|
||||
"description": "869.618MHz / SF7 / BW62.5 / CR5 / 2B",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Switzerland",
|
||||
"description": "869.618MHz / SF8 / BW62.5 / CR8",
|
||||
"frequency": "869.618",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "8"
|
||||
},
|
||||
{
|
||||
"title": "Thailand",
|
||||
"description": "921MHz / SF7 / BW62.5 / CR5",
|
||||
"frequency": "921",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "USA",
|
||||
"description": "910.525MHz / SF7 / BW62.5 / CR5",
|
||||
"frequency": "910.525",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "USA - Southern California",
|
||||
"description": "927.875MHz / SF7 / BW62.5 / CR5 / 3B",
|
||||
"frequency": "927.875",
|
||||
"spreading_factor": "7",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5",
|
||||
"network_settings": {
|
||||
"path_hash_size": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Vietnam (Narrow)",
|
||||
"description": "920.250MHz / SF8 / BW62.5 / CR5",
|
||||
"frequency": "920.250",
|
||||
"spreading_factor": "8",
|
||||
"bandwidth": "62.5",
|
||||
"coding_rate": "5"
|
||||
},
|
||||
{
|
||||
"title": "Vietnam (Deprecated)",
|
||||
"description": "920.250MHz / SF11 / BW250 / CR5",
|
||||
"frequency": "920.250",
|
||||
"spreading_factor": "11",
|
||||
"bandwidth": "250",
|
||||
"coding_rate": "5"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user