diff --git a/docs/progress.md b/docs/progress.md index 83cc779..e91990c 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -71,3 +71,4 @@ - Reception was dead until the XIAO rebooted: SET_RADIO_PARAMS saves and reports the new settings, but the receiver only retunes on a restart. Settings now offers to reboot the radio (CMD_REBOOT) after a radio change and reconnects. The radio then heard the user's contact. - MeshTerm-style UI built: generated 4x6 font (PilRC's FONT compiler writes a corrupt NFNT, so gen_font.py writes it), rowlist, text layout (host-tested), message store and sync, Home/Conversation/Contacts/Node screens. Font verified readable on the Neo. - Transcript wrapped after ~4 characters on the Visor: prc-tools puts `const` data in the code section, but another file reaching an `extern const` table addresses it A5-relative as data, so it read garbage widths. Shared tables (font widths, presets) are now non-const, set in their generators. Host tests can't catch this. Rule: never share an `extern const` table across files in palm-client. +- Times were wrong: Palm OS 3.5's clock is local time with no zone, and it was used as UTC (and the Visor hadn't moved off DST). That made contact ages read 'now', and clock sync pushed a wrong time into the radio, which can only move forward. Now everything stored and sent is UTC via a UTC-offset app preference (Settings), the display converts back, and names are converted from UTF-8. diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index e14a8ec..e6297c6 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -145,6 +145,9 @@ BEGIN 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 "UTC:" AUTOID AT (95 92) FONT 1 + FIELD ID SettingsUtcField AT (120 92 24 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 5 + LABEL "h" AUTOID AT (147 92) LABEL "Path hash:" AUTOID AT (5 107) FONT 1 PUSHBUTTON "1B" ID SettingsHash1Push AT (60 106 20 12) GROUP SettingsHashGroup diff --git a/palm-client/src/app.c b/palm-client/src/app.c index 0abfa0f..7ba41e3 100644 --- a/palm-client/src/app.c +++ b/palm-client/src/app.c @@ -10,6 +10,7 @@ Boolean gAppAutoConnectTried = false; typedef struct { UInt16 version; UInt32 port; /* VM_PORT_USB or VM_PORT_SERIAL */ + Int16 utcOffset; /* local minus UTC, minutes */ } AppPrefs; void AppOpenConversation(const VmConv *conv) @@ -24,8 +25,13 @@ void AppLoadPrefs(void) UInt16 size = sizeof(prefs); if (PrefGetAppPreferences(VM_CREATOR, VM_PREFS_ID, &prefs, &size, true) != noPreferenceFound && - size == sizeof(prefs) && prefs.version == VM_PREFS_VERSION) + size == sizeof(prefs) && prefs.version == VM_PREFS_VERSION) { TransportSetPort(prefs.port); + SessionSetUtcOffset(prefs.utcOffset); + return; + } + /* no saved offset yet: start from the system's, if it has one */ + SessionSetUtcOffset(-(Int16)PrefGetPreference(prefMinutesWestOfGMT)); } void AppSavePrefs(void) @@ -34,6 +40,7 @@ void AppSavePrefs(void) prefs.version = VM_PREFS_VERSION; prefs.port = TransportGetPort(); + prefs.utcOffset = SessionUtcOffset(); PrefSetAppPreferences(VM_CREATOR, VM_PREFS_ID, VM_PREFS_VERSION, &prefs, sizeof(prefs), true); } diff --git a/palm-client/src/app.h b/palm-client/src/app.h index a8d37d5..5d244cf 100644 --- a/palm-client/src/app.h +++ b/palm-client/src/app.h @@ -10,14 +10,17 @@ #define VM_CREATOR 'VMsh' #define VM_PREFS_ID 0 -#define VM_PREFS_VERSION 1 +#define VM_PREFS_VERSION 2 /* The conversation ConvoForm shows. */ extern VmConv gAppConv; void AppOpenConversation(const VmConv *conv); -/* Loads/saves the link choice (USB or serial) so the app reconnects the same way. */ +/* + * Loads/saves the link choice (USB or serial, so the app reconnects the same way) + * and the UTC offset (the Visor's clock has no time zone). + */ void AppLoadPrefs(void); void AppSavePrefs(void); diff --git a/palm-client/src/channels.c b/palm-client/src/channels.c index af951ed..c374e05 100644 --- a/palm-client/src/channels.c +++ b/palm-client/src/channels.c @@ -2,6 +2,7 @@ #include "protocol.h" #include "session.h" #include "channels.h" +#include "ui/text.h" #define DB_NAME "VisorMeshChannels" #define DB_TYPE 'chan' @@ -95,6 +96,7 @@ Err ChannelsRefresh(UInt8 maxChannels) goto fail; if (!InUse(&c)) continue; + TxtUtf8NameToPalm(c.name, sizeof(c.name)); index = dmMaxRecordIndex; h = DmNewRecord(gDb, &index, sizeof(VmChannel)); if (!h) { diff --git a/palm-client/src/contacts.c b/palm-client/src/contacts.c index 1b85a9f..e38bc37 100644 --- a/palm-client/src/contacts.c +++ b/palm-client/src/contacts.c @@ -2,6 +2,7 @@ #include "protocol.h" #include "session.h" #include "contacts.h" +#include "ui/text.h" #define DB_NAME "VisorMeshContacts" #define DB_TYPE 'cont' @@ -96,10 +97,12 @@ static Err AddContact(const VmContact *c, void *ctx) { UInt16 index = dmMaxRecordIndex; MemHandle h = DmNewRecord(gDb, &index, sizeof(VmContact)); + VmContact copy = *c; if (!h) return DmGetLastErr(); - DmWrite(MemHandleLock(h), 0, c, sizeof(VmContact)); + TxtUtf8NameToPalm(copy.name, sizeof(copy.name)); + DmWrite(MemHandleLock(h), 0, ©, sizeof(VmContact)); MemHandleUnlock(h); DmReleaseRecord(gDb, index, true); return errNone; diff --git a/palm-client/src/contacts_form.c b/palm-client/src/contacts_form.c index b3f684c..c20c237 100644 --- a/palm-client/src/contacts_form.c +++ b/palm-client/src/contacts_form.c @@ -16,7 +16,6 @@ #include "ui/rowlist.h" #include "contacts_form.h" -#define PALM_EPOCH_OFFSET 2082844800UL /* seconds from 1904-01-01 (Palm) to 1970-01-01 (Unix) */ #define NAME_X 8 #define NAME_W 82 #define HEARD_R 110 @@ -143,7 +142,7 @@ static void ShowDetails(UInt16 row) return; FormatRoute(route, c.outPathLen); if (c.lastAdvert) { - TimSecondsToDateTime(c.lastAdvert + PALM_EPOCH_OFFSET, &dt); + TimSecondsToDateTime(SessionLocalPalm(c.lastAdvert), &dt); StrPrintF(line, "%s, %s\nKey %02x%02x%02x%02x%02x%02x...\nHeard %u-%02u-%02u %02u:%02u", TypeName(c.type), route, c.pubKey[0], c.pubKey[1], c.pubKey[2], c.pubKey[3], c.pubKey[4], c.pubKey[5], dt.year, dt.month, dt.day, dt.hour, dt.minute); diff --git a/palm-client/src/convo_form.c b/palm-client/src/convo_form.c index c660c00..2251308 100644 --- a/palm-client/src/convo_form.c +++ b/palm-client/src/convo_form.c @@ -21,7 +21,6 @@ #define LINE_H VM_CHIP_H /* chips need the extra 2 px */ #define TIME_X 2 #define TEXT_X 26 -#define PALM_EPOCH 2082844800UL #define L_DAY 0 #define L_CHIP 1 @@ -82,9 +81,9 @@ static void SuffixOf(const VmMsgHeader *h, Char *out) } } -static UInt32 LocalDay(UInt32 unix) +static UInt32 LocalDay(UInt32 utc) { - return (unix + PALM_EPOCH) / 86400UL; /* the Visor's clock is local time */ + return SessionLocalPalm(utc) / 86400UL; } static void AddLine(UInt8 kind, UInt16 msg, UInt16 off, UInt16 len, Boolean first, Boolean last, UInt32 day) @@ -189,7 +188,7 @@ static void DrawLine(UInt16 row, const RectangleType *b, void *ctx) const Char *text = (const Char *)(h + 1) + l->off; if (l->first) { - TimSecondsToDateTime(h->received + PALM_EPOCH, &dt); + TimSecondsToDateTime(SessionLocalPalm(h->received), &dt); StrPrintF(buf, "%02u:%02u", dt.hour, dt.minute); WinDrawChars(buf, StrLen(buf), x + TIME_X, y); } diff --git a/palm-client/src/resources.h b/palm-client/src/resources.h index f37b37a..52b2366 100644 --- a/palm-client/src/resources.h +++ b/palm-client/src/resources.h @@ -37,6 +37,7 @@ #define SettingsSaveButton 1213 #define SettingsCancelButton 1214 #define SettingsHashGroup 2 +#define SettingsUtcField 1215 #define SettingsErrorAlert 2001 #define ContactInfoAlert 2002 diff --git a/palm-client/src/session.c b/palm-client/src/session.c index 17c3ac9..2f387b2 100644 --- a/palm-client/src/session.c +++ b/palm-client/src/session.c @@ -30,9 +30,30 @@ static VmSelfInfo gSelf; static UInt8 gRx[64]; static UInt16 gRxPos, gRxLen; +/* + * The Visor's clock is local time with no time zone (Palm OS 3.5), so the + * offset to UTC is an app preference. Everything stored and sent is UTC. + */ +static Int32 gUtcOffsetSecs = 0; /* local minus UTC */ + +void SessionSetUtcOffset(Int16 minutes) +{ + gUtcOffsetSecs = (Int32)minutes * 60; +} + +Int16 SessionUtcOffset(void) +{ + return (Int16)(gUtcOffsetSecs / 60); +} + UInt32 SessionNow(void) { - return TimGetSeconds() - PALM_EPOCH_OFFSET; + return TimGetSeconds() - PALM_EPOCH_OFFSET - gUtcOffsetSecs; +} + +UInt32 SessionLocalPalm(UInt32 utc) +{ + return utc + PALM_EPOCH_OFFSET + gUtcOffsetSecs; } /* Acts on a frame nobody asked for: pushes from the radio. */ @@ -153,11 +174,13 @@ static Err QueryRadio(UInt16 firstTries) err = Request(frame, len, VM_RESP_SELF_INFO, 1); if (err == errNone && !VmParseSelfInfo(gDecoder.payload, gDecoder.len, &gSelf)) err = vmErrBadReply; + if (err == errNone) + TxtUtf8NameToPalm(gSelf.name, sizeof(gSelf.name)); return err; } /* Sets the radio's clock from ours if it's behind (the firmware only lets it move forward). */ -static void SyncClock(void) +void SessionSyncClock(void) { UInt8 frame[VM_HEADER_LEN + 5]; UInt32 radio, now = SessionNow(); @@ -197,7 +220,7 @@ Err SessionConnect(void) gConnected = true; /* Best effort: the connection stands even if one of these fails; the stored copies stay. */ - SyncClock(); + SessionSyncClock(); if (ChannelsRefresh(gDevice.maxChannels) == errNone) gChanged |= VM_CHANGED_MESSAGES; if (ContactsRefresh(NULL) == errNone) diff --git a/palm-client/src/session.h b/palm-client/src/session.h index ed1edc4..f73b51d 100644 --- a/palm-client/src/session.h +++ b/palm-client/src/session.h @@ -50,8 +50,15 @@ Err SessionRefresh(void); typedef Err (*SessionContactFn)(const VmContact *c, void *ctx); Err SessionGetContacts(UInt32 since, SessionContactFn fn, void *ctx, UInt32 *count, UInt32 *lastMod); -/* Our clock in Unix seconds (message timestamps). */ +/* Now as Unix seconds (UTC), from the Visor's local clock and the UTC offset. */ UInt32 SessionNow(void); +/* A UTC Unix time as the Visor's local clock reads it (for TimSecondsToDateTime). */ +UInt32 SessionLocalPalm(UInt32 utc); +/* Local time minus UTC, in minutes (e.g. -300 for EST). */ +void SessionSetUtcOffset(Int16 minutes); +Int16 SessionUtcOffset(void); +/* Sets the radio's clock to ours (UTC) if it's behind; it can only move forward. */ +void SessionSyncClock(void); /* * Call regularly (the app does on nilEvent): handles pushes, pulls waiting diff --git a/palm-client/src/settings.c b/palm-client/src/settings.c index c7b01fd..e50dd09 100644 --- a/palm-client/src/settings.c +++ b/palm-client/src/settings.c @@ -9,6 +9,7 @@ #include "presets.h" #include "session.h" #include "settings.h" +#include "app.h" #define CUSTOM_LABEL "Custom" @@ -97,6 +98,37 @@ static Int16 FindPreset(UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr) return -1; } +/* Minutes to hours: -300 -> "-5", 330 -> "5.5", 345 -> "5.75". */ +static void FormatUtcOffset(Char *out, Int16 minutes) +{ + UInt16 m = (UInt16)(minutes < 0 ? -minutes : minutes); + Char digits[8]; + + VmFormatDecimal(digits, (UInt32)m * 100 / 60, 2); + StrPrintF(out, "%s%s", minutes < 0 ? "-" : "", digits); +} + +/* "-5", "+5.5", "5.75" (quarter hours) to minutes, -12..+14 h. */ +static Boolean ParseUtcOffset(const Char *s, Int16 *minutes) +{ + Boolean neg = false; + UInt32 hundredths; + + if (*s == '-' || *s == '+') { + neg = *s == '-'; + s++; + } + if (!VmParseDecimal(s, 2, &hundredths) || hundredths % 25 != 0 || hundredths > 1400) + return false; + *minutes = (Int16)(hundredths * 60 / 100); + if (neg) { + if (*minutes > 720) + return false; + *minutes = -*minutes; + } + return true; +} + static void InitForm(FormType *form) { const VmSelfInfo *self = SessionSelf(); @@ -118,6 +150,8 @@ static void InitForm(FormType *form) SetRadioFields(form, self->freqKHz, self->bwHz, self->sf, self->cr); StrIToA(buf, self->txPowerDbm); SetFieldText(form, SettingsPowerField, buf); + FormatUtcOffset(buf, SessionUtcOffset()); + SetFieldText(form, SettingsUtcField, buf); SelectHashSize(form, (UInt8)(dev->pathHashMode + 1)); if (!dev->hasPathHashMode) { CtlSetEnabled(GetObject(form, SettingsHash1Push), false); @@ -175,6 +209,7 @@ static Boolean Save(FormType *form) UInt32 freq, bw; Int32 sf, cr, power; UInt8 hashMode = (UInt8)(SelectedHashSize(form) - 1); + Int16 utcOffset; UInt16 len; Char msg[64]; Boolean radioChanged = false; @@ -209,6 +244,17 @@ static Boolean Save(FormType *form) return false; } + if (!ParseUtcOffset(GetFieldText(form, SettingsUtcField), &utcOffset)) { + ShowError("UTC offset: hours from -12 to +14, e.g. -5 or 5.5."); + return false; + } + /* an app preference, not a radio setting: applies even if nothing else changed */ + if (utcOffset != SessionUtcOffset()) { + SessionSetUtcOffset(utcOffset); + AppSavePrefs(); + SessionSyncClock(); + } + if (StrCompare(name, self->name) != 0) { len = VmBuildSetAdvertName(frame, sizeof(frame), name); if (!Send(form, frame, len, "name")) diff --git a/palm-client/src/ui/text.c b/palm-client/src/ui/text.c index b2aa870..1d09ba6 100644 --- a/palm-client/src/ui/text.c +++ b/palm-client/src/ui/text.c @@ -174,6 +174,20 @@ UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize) return o; } +void TxtUtf8NameToPalm(Char *name, UInt16 size) +{ + Char tmp[48]; + UInt16 len = 0; + + if (size > sizeof(tmp)) + size = sizeof(tmp); + while (len < size - 1 && name[len]) + len++; + TxtUtf8ToPalm((const UInt8 *)name, len, tmp, size); + for (len = 0; len < size && (name[len] = tmp[len]) != 0; len++) + ; +} + /* Windows-1252 0x80-0x9F to Unicode (0 = unused slot). */ static const UInt16 kCp1252[32] = { 0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, diff --git a/palm-client/src/ui/text.h b/palm-client/src/ui/text.h index 9999609..8ec1055 100644 --- a/palm-client/src/ui/text.h +++ b/palm-client/src/ui/text.h @@ -46,6 +46,9 @@ UInt16 TxtSplitSender(const Char *text, UInt16 len); */ UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize); +/* Converts a NUL-terminated UTF-8 name in place to the Visor's charset (size <= 48). */ +void TxtUtf8NameToPalm(Char *name, UInt16 size); + /* * The Visor's text (Windows-1252) to UTF-8 for sending. Returns the byte count, * or 0 if it won't fit in outSize (so a message is never cut mid-character).