palm-client: reconnects after a wake or a lost link run in steps on idle events (open, attach wait, DEVICE_QUERY with the reply checked without waiting), so the app stays usable while it looks for the radio; titles show "connecting"; Options > Connect and a tap on "offline" still connect in one go

This commit is contained in:
rich committed 2026-11-04 09:36:19 -05:00
1 parent f9aa7a1e93
commit b5ce1098b3
8 files changed
+242 -53

No files matched your search

+9 -3
View File
@@ -29,6 +29,7 @@ static Int16 TextWithMentions(const Char *text, UInt16 len, Coord x, Coord y, Co
static void DrawLine(UInt16 row, const RectangleType *b, void *ctx) VM_SECT2;
static void DrawCounter(void) VM_SECT2;
static void DrawStatus(void) VM_SECT2;
static UInt8 ConnState(void) VM_SECT2;
static UInt32 FirstUnread(void) VM_SECT2;
static void ScrollToNew(void) VM_SECT2;
static void Refresh(Boolean toEnd) VM_SECT2;
@@ -68,7 +69,7 @@ static UInt16 gShow = MAX_MSGS; /* messages shown (the newest) */
static RowList gList;
static Char gTitle[VM_NAME_SIZE + 2];
static Coord gTitleEnd; /* where the title's tab ends */
static Boolean gShownOnline;
static UInt8 gShownState; /* what DrawStatus showed: ConnState() */
static UInt32 gNewFrom = NO_NEW; /* unique ID of the first message unread at open */
static const Char *kDays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
@@ -338,12 +339,17 @@ static void DrawCounter(void)
FntSetFont(old);
}
static UInt8 ConnState(void)
{
return SessionIsConnected() ? 1 : SessionConnecting() ? 2 : 0;
}
/* Online or offline at the right of the title; redraws the title if "Connecting..." covered its end. */
static void DrawStatus(void)
{
Boolean over = TitleStatusLeft() < gTitleEnd;
gShownOnline = SessionIsConnected();
gShownState = ConnState();
TitleConnState("");
if (over)
FrmCopyTitle(FrmGetActiveForm(), gTitle);
@@ -625,7 +631,7 @@ void ConvoFormIdle(void)
{
if (gLines) {
DrawCounter();
if (SessionIsConnected() != gShownOnline)
if (ConnState() != gShownState)
DrawStatus();
}
}
+65 -24
View File
@@ -43,7 +43,7 @@ static Err RomVersionCompatible(UInt32 required, UInt16 launchFlags)
return sysErrRomIncompatible;
}
static void Connect(Boolean retry);
static void Connect(void);
/* Go, Channel and Options commands, whichever screen's menu they're on; Edit is the form's. */
static Boolean HandleMenu(UInt16 itemID)
@@ -64,7 +64,7 @@ static Boolean HandleMenu(UInt16 itemID)
case MainMenuShot: ShotCapture(); return true; /* the menu has gone by now */
case MainMenuShotLater: ShotLater(5); return true;
case MainMenuAbout: AppAbout(); return true;
case MainMenuConnect: Connect(false); return true;
case MainMenuConnect: Connect(); return true;
}
return false;
}
@@ -116,38 +116,83 @@ static UInt32 gLastSecs = 0, gLastTicks = 0;
static Boolean gWantConnection = false; /* we were connected when the Visor slept */
/*
* After a wake the connect often fails (the USB link isn't back yet) or a tap
* stops it, so it's tried again 2, 4, 8 ... 256 s later (8.5 min in all).
* Reconnects after a wake or a lost link run in steps (SessionConnectStep on
* idle events), so the app stays usable meanwhile. One that fails (the USB
* link isn't back yet, the bridge is stopped) is tried again 2, 4, 8 ... 256 s
* later (8.5 min in all).
*/
#define VM_RETRIES 8
#define VM_RETRIES 8
#define VM_STEP_TICKS (SysTicksPerSecond() / 10)
static UInt32 gRetryAt = 0; /* ticks; 0: no retry due */
static UInt16 gRetries = 0;
/* Shows progress in the title on Home and conversations; retry: quietly, again later if it fails. */
static void Connect(Boolean retry)
static void ScheduleRetry(void)
{
UInt16 formId = FrmGetActiveFormID();
Err err;
if (gRetries >= VM_RETRIES)
return;
gRetries++;
gRetryAt = TimGetTicks() + ((UInt32)SysTicksPerSecond() << gRetries);
if (!gRetryAt)
gRetryAt = 1;
}
gRetryAt = 0;
err = AppConnect(formId == HomeForm || formId == ConvoForm ? TitleStatus : NULL, retry);
static void Reconnected(Err err)
{
if (err == errNone) {
gWantConnection = false;
gRetries = 0;
} else if (retry && gRetries < VM_RETRIES) {
gRetries++;
gRetryAt = TimGetTicks() + ((UInt32)SysTicksPerSecond() << gRetries);
if (!gRetryAt)
gRetryAt = 1;
} else {
ScheduleRetry();
}
FrmUpdateForm(FrmGetActiveFormID(), VM_UPDATE_DATA);
}
static void StartReconnect(void)
{
Err err;
gRetryAt = 0;
err = SessionConnectStart();
if (err == vmErrPending)
FrmUpdateForm(FrmGetActiveFormID(), VM_UPDATE_DATA); /* "connecting" */
else
Reconnected(err);
}
/* On idle events: the next reconnect step, or a retry that's due. */
static void StepReconnect(void)
{
Err err;
if (SessionConnecting()) {
err = SessionConnectStep();
if (err != vmErrPending)
Reconnected(err);
} else if (gRetryAt && !SessionIsConnected() && (Int32)(TimGetTicks() - gRetryAt) >= 0) {
StartReconnect();
}
}
/* Options > Connect, or a tap on "offline": in one go, with progress in the title on Home and conversations. */
static void Connect(void)
{
UInt16 formId = FrmGetActiveFormID();
gRetryAt = 0;
if (AppConnect(formId == HomeForm || formId == ConvoForm ? TitleStatus : NULL, false) == errNone) {
gWantConnection = false;
gRetries = 0;
}
FrmUpdateForm(formId, VM_UPDATE_DATA);
}
/* How long to wait for an event: poll while connected, wake for a retry that's due. */
/* How long to wait for an event: poll while connected, step a reconnect, wake for a retry that's due. */
static Int32 EventTimeout(void)
{
Int32 left;
if (SessionConnecting())
return VM_STEP_TICKS;
if (SessionIsConnected())
return VM_POLL_TICKS;
if (!gRetryAt)
@@ -164,13 +209,10 @@ static void CheckWake(void)
if (gLastSecs) {
wall = (Int32)(secs - gLastSecs);
run = (Int32)((ticks - gLastTicks) / SysTicksPerSecond());
if (wall - run > VM_SLEEP_GAP_SECS && (SessionIsConnected() || gWantConnection)) {
if (wall - run > VM_SLEEP_GAP_SECS && (SessionIsConnected() || SessionConnecting() || gWantConnection)) {
gWantConnection = true;
SessionDisconnect();
gRetries = 0;
Connect(true);
secs = TimGetSeconds();
ticks = TimGetTicks();
StartReconnect();
}
}
gLastSecs = secs;
@@ -211,8 +253,7 @@ static void AppEventLoop(void)
EvtGetEvent(&event, EventTimeout());
CheckWake();
if (event.eType == nilEvent) {
if (gRetryAt && !SessionIsConnected() && (Int32)(TimGetTicks() - gRetryAt) >= 0)
Connect(true);
StepReconnect();
Poll();
continue;
}
+134 -20
View File
@@ -53,6 +53,7 @@ static UInt16 gRxPos, gRxLen;
static Int32 gUtcOffsetSecs = 0; /* local minus UTC */
static void NoteSound(UInt8 kind, const VmMessage *m);
static void SetConnected(void);
/* How a connect gives way to the user (SessionConnect). */
#define VM_YIELD_NEVER 0
@@ -235,20 +236,13 @@ static Err Request(const UInt8 *frame, UInt16 len, UInt8 code, UInt16 tries)
return RequestAny(frame, len, &code, 1, tries);
}
/* DEVICE_QUERY + APP_START into gDevice/gSelf. The first request gets extra tries (USB attach). */
static Err QueryRadio(UInt16 firstTries)
/* APP_START into gSelf. */
static Err StartApp(void)
{
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))
@@ -258,6 +252,20 @@ static Err QueryRadio(UInt16 firstTries)
return err;
}
/* 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 + 2];
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;
return err == errNone ? StartApp() : err;
}
/* Sets the radio's clock from ours if it's behind (the firmware only lets it move forward). */
static UInt32 gRadioTime = 0; /* radio clock at gRadioTimeAt (our local seconds) */
static Boolean gClockMismatch = false;
@@ -326,30 +334,135 @@ static Err OpenAndQuery(void)
TransportClose();
return err;
}
SetConnected();
return errNone;
}
static void SetConnected(void)
{
gConnected = true;
gHeardAt = TimGetTicks();
gScopeMode = VM_SCOPE_UNKNOWN;
return errNone;
}
/* Best effort: the connection stands even if one of these fails; the stored copies stay. */
static void SyncAll(void)
{
SessionSyncClock();
if (ChannelsRefresh(gDevice.maxChannels) == errNone)
gChanged |= VM_CHANGED_MESSAGES;
if (ContactsUpdate() == errNone)
gChanged |= VM_CHANGED_CONTACTS;
if (SessionSyncMessages() == vmErrCancelled)
gMsgWaiting = true; /* the next poll carries on */
}
Err SessionConnect(void)
{
Err err = OpenAndQuery();
if (err == errNone) {
/* Best effort: the connection stands even if one of these fails; the stored copies stay. */
SessionSyncClock();
if (ChannelsRefresh(gDevice.maxChannels) == errNone)
gChanged |= VM_CHANGED_MESSAGES;
if (ContactsUpdate() == errNone)
gChanged |= VM_CHANGED_CONTACTS;
if (SessionSyncMessages() == vmErrCancelled)
gMsgWaiting = true; /* the next poll carries on */
}
if (err == errNone)
SyncAll();
gYield = VM_YIELD_NEVER;
return err;
}
/*
* The stepped connect. Only finding the radio is in steps: with no bridge or
* radio that's where the time goes (each send waits, each reply times out).
* Once DEVICE_INFO arrives the rest takes a moment and gives way between
* requests, as SessionConnect does.
*/
#define STEP_NONE 0
#define STEP_ATTACH 1 /* link open, giving the USB host time to attach */
#define STEP_QUERY 2 /* DEVICE_QUERY sent */
static UInt8 gStep = STEP_NONE;
static UInt16 gStepTries;
static UInt32 gStepAt; /* ticks: the attach wait or the reply wait ends */
/* Whether a reply with this code came in, without waiting; pushes are handled, other frames dropped. */
static Boolean ReplyArrived(UInt8 code)
{
Err err;
for (;;) {
while (gRxPos < gRxLen) {
if (!VmDecoderFeed(&gDecoder, gRx[gRxPos++]) || gDecoder.len == 0)
continue;
if (gDecoder.payload[0] == code)
return true;
if (VM_IS_PUSH(gDecoder.payload[0]))
HandlePush();
}
gRxPos = 0;
gRxLen = TransportRecv(gRx, sizeof(gRx), 0, &err);
if (gRxLen == 0)
return false;
gHeardAt = TimGetTicks();
}
}
Err SessionConnectStart(void)
{
Err err;
SessionDisconnect();
err = TransportOpen();
if (err != errNone)
return err;
VmDecoderReset(&gDecoder);
gRxPos = gRxLen = 0;
gMsgWaiting = false;
gStep = STEP_ATTACH;
gStepTries = 0;
gStepAt = TimGetTicks() + (TransportGetPort() == VM_PORT_USB ? SysTicksPerSecond() : 0);
return vmErrPending;
}
Err SessionConnectStep(void)
{
UInt8 frame[VM_HEADER_LEN + 2];
UInt16 len;
Err err;
if (gStep == STEP_NONE)
return serErrNotOpen;
if (gStep == STEP_QUERY && ReplyArrived(VM_RESP_DEVICE_INFO)) {
gStep = STEP_NONE;
err = VmParseDeviceInfo(gDecoder.payload, gDecoder.len, &gDevice) ? errNone : vmErrBadReply;
gYield = VM_YIELD_BETWEEN;
if (err == errNone)
err = StartApp();
if (err == errNone) {
SetConnected();
SyncAll();
} else {
TransportClose();
}
gYield = VM_YIELD_NEVER;
return err;
}
if ((Int32)(TimGetTicks() - gStepAt) < 0)
return vmErrPending;
if (gStepTries >= VM_CONNECT_TRIES) {
gStep = STEP_NONE;
TransportClose();
return vmErrNoReply;
}
/* a send that fails counts as a try: with no host it fails until the bridge is back */
len = VmBuildDeviceQuery(frame, sizeof(frame));
TransportSendOnce(frame, len);
gStepTries++;
gStep = STEP_QUERY;
gStepAt = TimGetTicks() + VM_REPLY_TIMEOUT;
return vmErrPending;
}
Boolean SessionConnecting(void)
{
return gStep != STEP_NONE;
}
/* Gives way like SessionConnect: a tap in the app in front ends the check early. */
Err SessionConnectLight(void)
{
@@ -926,6 +1039,7 @@ void SessionDisconnect(void)
ApplyScope(NULL);
TransportClose();
gConnected = false;
gStep = STEP_NONE;
gLoginCount = 0; /* the radio keeps room sessions alive, but re-check after a reconnect */
}
+11
View File
@@ -16,6 +16,7 @@
#define vmErrTooLong (appErrorClass | 4) /* message text over the byte limit */
#define vmErrLoginFailed (appErrorClass | 5) /* a room/repeater refused the password */
#define vmErrCancelled (appErrorClass | 7) /* the user tapped or pressed a key during a connect */
#define vmErrPending (appErrorClass | 8) /* SessionConnectStep: still connecting */
/* What SessionPoll found changed, so the screen on show can redraw. */
#define VM_CHANGED_MESSAGES 0x0001
@@ -41,6 +42,16 @@ Err SessionConnect(void);
*/
Err SessionConnectLight(void);
void SessionDisconnect(void);
/*
* A connect in steps, so the app keeps running while it finds the radio
* (reconnects after a wake or a lost link). Start opens the link; then call
* Step every tenth of a second or so until it returns something other than
* vmErrPending: errNone once connected and synced, or the error (link
* closed). SessionConnect or SessionDisconnect abandons it.
*/
Err SessionConnectStart(void);
Err SessionConnectStep(void);
Boolean SessionConnecting(void);
Boolean SessionIsConnected(void);
/* True once after the radio stopped answering a command and the session closed itself; clears it. */
Boolean SessionTakeLost(void);
+2
View File
@@ -23,6 +23,8 @@ UInt32 TransportGetPort(void);
Err TransportOpen(void);
void TransportClose(void);
Err TransportSend(const UInt8 *buf, UInt16 len);
/* One short attempt (a fraction of a second), for a reconnect that mustn't hold up the app. */
Err TransportSendOnce(const UInt8 *buf, UInt16 len);
/*
* Waits up to timeoutTicks for data, then returns whatever is available,
+14
View File
@@ -79,6 +79,20 @@ Err TransportSend(const UInt8 *buf, UInt16 len)
return serErrTimeOut;
}
Err TransportSendOnce(const UInt8 *buf, UInt16 len)
{
Err err = errNone;
if (gPort == VM_PORT_SBLORA)
return SbTransportSend(buf, len);
if (!gPortOpen)
return serErrNotOpen;
if (SrmSend(gPortId, (void *)buf, len, &err) == len)
return errNone;
SrmClearErr(gPortId);
return err != errNone ? err : serErrTimeOut;
}
UInt16 TransportRecv(UInt8 *buf, UInt16 max, Int32 timeoutTicks, Err *errP)
{
UInt32 avail = 0;
+3 -3
View File
@@ -40,7 +40,7 @@ void TitleConnState(const Char *prefix)
Char buf[24];
StrCopy(buf, prefix);
StrCat(buf, SessionIsConnected() ? "online" : "offline");
StrCat(buf, SessionIsConnected() ? "online" : SessionConnecting() ? "connecting" : "offline");
TitleStatus(buf);
}
@@ -53,7 +53,7 @@ Boolean TitleStatusTapped(const EventType *event)
{
EventType e;
if (event->eType != penDownEvent || SessionIsConnected() ||
if (event->eType != penDownEvent || SessionIsConnected() || SessionConnecting() ||
event->screenY > 12 || event->screenX < gLeft - 4)
return false;
MemSet(&e, sizeof(e), 0);
@@ -66,7 +66,7 @@ Boolean TitleStatusTapped(const EventType *event)
Coord TitleFit(Char *title)
{
FontID old = FntSetFont(vmSmallFont);
Int16 maxW = 158 - FntCharsWidth("offline", 7) - 4 - TITLE_PAD;
Int16 maxW = 158 - FntCharsWidth("connecting", 10) - 4 - TITLE_PAD;
Int16 w = maxW, len = StrLen(title);
Boolean fits;
+4 -3
View File
@@ -1,6 +1,7 @@
/*
* The right end of a form's title bar: the connection state ("online",
* "offline", "Connecting..." while it connects), drawn in the UI font. Tapping
* "offline", "connecting" during a reconnect in steps, "Connecting..." during
* one in one go), drawn in the UI font. Tapping
* it while offline is the same as Options > Connect. Call TitleStatusReset
* when the form opens.
*/
@@ -13,13 +14,13 @@
void TitleStatusReset(void) VM_SECT3;
/* Draws text there, erasing back to where the previous text started. */
void TitleStatus(const Char *text) VM_SECT3;
/* "online" or "offline", after prefix (may be empty). */
/* "online", "connecting" or "offline", after prefix (may be empty). */
void TitleConnState(const Char *prefix) VM_SECT3;
/* The left edge of what's drawn there now (160: nothing). */
Coord TitleStatusLeft(void) VM_SECT3;
/* A tap on it while offline: queues Options > Connect. */
Boolean TitleStatusTapped(const EventType *event) VM_SECT3;
/* Cuts title with an ellipsis so its tab ends before "offline"; returns where the tab ends. */
/* Cuts title with an ellipsis so its tab ends before "connecting"; returns where the tab ends. */
Coord TitleFit(Char *title) VM_SECT3;
#endif