diff --git a/palm-client/src/app.c b/palm-client/src/app.c index 9970ae5..6db1b21 100644 --- a/palm-client/src/app.c +++ b/palm-client/src/app.c @@ -77,6 +77,8 @@ void AppErrorText(Err err, Char *out) StrCopy(out, "Not connected."); else if (err == vmErrNoModule) StrCopy(out, "No module in the slot."); + else if (err == vmErrCancelled) + StrCopy(out, "Connect stopped."); else if (err == vmErrLoginFailed) StrCopy(out, "Login refused: wrong password?"); else @@ -93,7 +95,7 @@ Err AppConnect(AppStatusFn status, Boolean quiet) if (status) status("Connecting..."); err = SessionConnect(); - if (err != errNone && !quiet) { + if (err != errNone && err != vmErrCancelled && !quiet) { AppErrorText(err, msg); FrmCustomAlert(SettingsErrorAlert, msg, "", ""); } diff --git a/palm-client/src/session.c b/palm-client/src/session.c index 4e231c0..a75d194 100644 --- a/palm-client/src/session.c +++ b/palm-client/src/session.c @@ -22,6 +22,8 @@ #define VM_CLOCK_VALID 1735689600UL /* 2025-01-01: a radio clock before this was never set */ #define PALM_EPOCH_OFFSET 2082844800UL /* 1904-01-01 (Palm) to 1970-01-01 (Unix) */ +#define VM_YIELD_SLICE (SysTicksPerSecond() / 10) /* how often a connect looks for the user */ + static VmFrameDecoder gDecoder; static Boolean gConnected = false; static Boolean gMsgWaiting = false; @@ -45,6 +47,18 @@ static Int32 gUtcOffsetSecs = 0; /* local minus UTC */ static void NoteSound(UInt8 kind, const VmMessage *m); +/* How a connect gives way to the user (SessionConnect). */ +#define VM_YIELD_NEVER 0 +#define VM_YIELD_BETWEEN 1 /* before each request: replies to a request are never abandoned */ +#define VM_YIELD_ANYTIME 2 /* also while waiting: the handshake, whose link is closed on cancel */ +static UInt8 gYield = VM_YIELD_NEVER; + +/* True if the user tapped or pressed a key; the event stays queued for the app. */ +static Boolean UserWaiting(void) +{ + return gYield != VM_YIELD_NEVER && EvtSysEventAvail(true); +} + /* * 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 @@ -135,6 +149,12 @@ static Err WaitForAny(const UInt8 *codes, UInt16 n, Int32 timeout) left = (Int32)(deadline - TimGetTicks()); if (left <= 0) return vmErrNoReply; + if (gYield == VM_YIELD_ANYTIME) { + if (UserWaiting()) + return vmErrCancelled; + if (left > VM_YIELD_SLICE) + left = VM_YIELD_SLICE; + } gRxPos = 0; gRxLen = TransportRecv(gRx, sizeof(gRx), left, &err); } @@ -173,6 +193,8 @@ static Err RequestAny(const UInt8 *frame, UInt16 len, const UInt8 *codes, UInt16 UInt16 t; for (t = 0; t < tries; t++) { + if (UserWaiting()) + return vmErrCancelled; DrainInput(); err = TransportSend(frame, len); if (err == serErrTimeOut) @@ -259,6 +281,7 @@ UInt32 SessionRadioTime(void) /* Opens the link and runs DEVICE_QUERY + APP_START; on error the link is closed. */ static Err OpenAndQuery(void) { + UInt16 t; Err err; SessionDisconnect(); @@ -270,10 +293,13 @@ static Err OpenAndQuery(void) gMsgWaiting = false; /* Over USB the Visor only appears to the host after SrmOpen; give the bridge time to attach. */ + gYield = VM_YIELD_ANYTIME; if (TransportGetPort() == VM_PORT_USB) - SysTaskDelay(SysTicksPerSecond()); + for (t = 0; t < 10 && !UserWaiting(); t++) + SysTaskDelay(VM_YIELD_SLICE); - err = QueryRadio(VM_CONNECT_TRIES); + err = UserWaiting() ? vmErrCancelled : QueryRadio(VM_CONNECT_TRIES); + gYield = VM_YIELD_BETWEEN; if (err != errNone) { TransportClose(); return err; @@ -287,24 +313,28 @@ Err SessionConnect(void) { Err err = OpenAndQuery(); - if (err != errNone) - return err; - /* 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; - SessionSyncMessages(); - return errNone; + 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 */ + } + gYield = VM_YIELD_NEVER; + return err; } +/* Gives way like SessionConnect: a tap in the app in front ends the check early. */ Err SessionConnectLight(void) { Err err = OpenAndQuery(); - if (err == errNone) - SessionSyncMessages(); + if (err == errNone && SessionSyncMessages() == vmErrCancelled) + gMsgWaiting = true; + gYield = VM_YIELD_NEVER; return err; } @@ -344,6 +374,8 @@ Err SessionGetContacts(UInt32 since, SessionContactFn fn, void *ctx, UInt32 *cou return vmErrBadReply; for (;;) { + if (UserWaiting()) + return vmErrCancelled; /* the rest of the list arrives and is drained */ err = WaitForAny(codes, 2, VM_REPLY_TIMEOUT); if (err != errNone) return err; diff --git a/palm-client/src/session.h b/palm-client/src/session.h index 8d35571..0d76411 100644 --- a/palm-client/src/session.h +++ b/palm-client/src/session.h @@ -15,6 +15,7 @@ #define vmErrBadReply (appErrorClass | 3) /* reply had the right code but didn't parse */ #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 */ /* What SessionPoll found changed, so the screen on show can redraw. */ #define VM_CHANGED_MESSAGES 0x0001 @@ -23,7 +24,16 @@ #define VM_CHANGED_REMOTE 0x0008 /* the admin console has new lines */ -/* Opens the link and runs DEVICE_QUERY + APP_START. On error the link is closed. */ +/* + * Opens the link and runs DEVICE_QUERY + APP_START, then refreshes the clock, + * channels and contacts and pulls queued messages. On error the link is closed. + * + * A connect gives way to the user: a pen tap or key press (left in the queue + * for the app) stops it. Before the radio has answered, that's + * vmErrCancelled and the link is closed; after, the connection stands and + * only the refresh steps left are skipped, between requests so no reply is + * lost (the stored copies are kept, and the next poll pulls the messages). + */ Err SessionConnect(void); /* * Like SessionConnect without refreshing the channels and contacts (the stored diff --git a/palm-client/src/transport.h b/palm-client/src/transport.h index 46d0b32..d65b97b 100644 --- a/palm-client/src/transport.h +++ b/palm-client/src/transport.h @@ -15,7 +15,7 @@ #define VM_PORT_USB 'HsUd' /* Handspring "USB" driver: the Visor's GENERIC USB port */ #define VM_PORT_SBLORA 'SbLo' /* not a Serial Manager port: transport_sb.c */ -#define vmErrNoModule (appErrorClass | 6) /* no SB-LoRa module answered (session.h has 1-5) */ +#define vmErrNoModule (appErrorClass | 6) /* no SB-LoRa module answered (session.h has 1-5 and 7) */ void TransportSetPort(UInt32 port); /* takes effect on the next TransportOpen */ UInt32 TransportGetPort(void);