pebble/io/outbox_queue.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file outbox_queue.h | ||
| 3 | * @brief The outbox's work queue, its failed set, and the slot holding whatever is in flight. | ||
| 4 | * | ||
| 5 | * One AppMessage goes out at a time, so everything the watch wants to send queues here first. A | ||
| 6 | * job the phone nacks is held in the failed set and moved back for a later pass, because its pkjs | ||
| 7 | * was usually asleep or busy and that first send is often what wakes it. Holding it means a whole | ||
| 8 | * poll, or the one settings reply a launch gets, is not lost to one nack. | ||
| 9 | * | ||
| 10 | * None of that needs the SDK. The transport owns one of these, does the sending and the timers, and | ||
| 11 | * asks here what to send next, which keeps the deciding testable on the host. | ||
| 12 | * | ||
| 13 | * @ingroup lib_io | ||
| 14 | */ | ||
| 15 | #pragma once | ||
| 16 | #include <stdbool.h> | ||
| 17 | |||
| 18 | #include "io/appmessage/appmessage_features.h" | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @addtogroup lib_io | ||
| 22 | * @{ | ||
| 23 | */ | ||
| 24 | |||
| 25 | // a kind is held once at most, so the queue and the failed set never need more slots than there are | ||
| 26 | // kinds. the weather, stock, calendar, settings, and fresh reply kinds fit in the 6 every face has, | ||
| 27 | // with one to spare. phone battery and now playing each add a slot, only on a face that compiles them | ||
| 28 | // in, so a face with every kind can have all of them fail at once and keep each one for the retry. | ||
| 29 | // sizing to the kinds a face has keeps every other face's outbox the size it was | ||
| 30 | #if defined(APPMESSAGE_HAS_PHONE) | ||
| 31 | #define OUTBOX_PHONE_SLOTS 1 ///< the phone battery request has a slot | ||
| 32 | #else | ||
| 33 | #define OUTBOX_PHONE_SLOTS 0 ///< no phone battery request on this face | ||
| 34 | #endif | ||
| 35 | #if defined(APPMESSAGE_HAS_MUSIC) | ||
| 36 | #define OUTBOX_MUSIC_SLOTS 1 ///< the now playing request has a slot | ||
| 37 | #else | ||
| 38 | #define OUTBOX_MUSIC_SLOTS 0 ///< no now playing request on this face | ||
| 39 | #endif | ||
| 40 | |||
| 41 | /// Most jobs the work queue or the failed set holds at once | ||
| 42 | #define OUTBOX_QUEUE_MAX (6 + OUTBOX_PHONE_SLOTS + OUTBOX_MUSIC_SLOTS) | ||
| 43 | |||
| 44 | /** | ||
| 45 | * @brief What an outbox job sends. | ||
| 46 | */ | ||
| 47 | typedef enum | ||
| 48 | { | ||
| 49 | OUTBOX_NONE, ///< The zero value, so a default-initialized job slot reads as nothing rather than a real request | ||
| 50 | OUTBOX_WEATHER, ///< A weather request | ||
| 51 | OUTBOX_SETTINGS, ///< The settings reply | ||
| 52 | OUTBOX_FRESH, ///< The short settings reply, only whether the watch booted with nothing saved | ||
| 53 | OUTBOX_STOCK, ///< A stock request | ||
| 54 | OUTBOX_CALENDAR, ///< A calendar request | ||
| 55 | OUTBOX_PHONE, ///< A phone battery request | ||
| 56 | OUTBOX_MUSIC ///< A now playing request | ||
| 57 | } OutboxKind; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @brief One job waiting to go out, or already in flight. | ||
| 61 | */ | ||
| 62 | typedef struct | ||
| 63 | { | ||
| 64 | OutboxKind kind; ///< What this job sends | ||
| 65 | int retries_left; ///< Retry passes left after a failed send | ||
| 66 | } OutboxJob; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @brief Everything the outbox is holding: what is waiting, what nacked, and what is out. | ||
| 70 | * | ||
| 71 | * Zero-initialized is the empty state, since `OUTBOX_NONE` is the enum's zero. | ||
| 72 | */ | ||
| 73 | typedef struct | ||
| 74 | { | ||
| 75 | OutboxJob queue[OUTBOX_QUEUE_MAX]; ///< This pass's work queue, oldest first | ||
| 76 | int queue_len; ///< How many jobs are in the queue | ||
| 77 | OutboxJob failed[OUTBOX_QUEUE_MAX]; ///< Requests that nacked this pass, held for the next | ||
| 78 | int failed_len; ///< How many jobs are in the failed set | ||
| 79 | OutboxJob inflight; ///< The job in flight, OUTBOX_NONE when nothing is out | ||
| 80 | } OutboxQueue; | ||
| 81 | |||
| 82 | /** | ||
| 83 | * @brief Whether anything is in flight, which is what says the outbox is busy. | ||
| 84 | * | ||
| 85 | * @param outbox The outbox to read. | ||
| 86 | * @return True while a send is waiting on its sent or failed callback. | ||
| 87 | */ | ||
| 88 | 6 | static inline bool outbox_busy(const OutboxQueue *outbox) | |
| 89 | { | ||
| 90 | 6 | return outbox->inflight.kind != OUTBOX_NONE; | |
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * @brief Whether this kind is already somewhere in the outbox. | ||
| 95 | * | ||
| 96 | * Covers all three places a job can be, so a poll that repeats while an earlier one is still | ||
| 97 | * working its way out does not queue a second copy of the same request. | ||
| 98 | * | ||
| 99 | * @param outbox The outbox to read. | ||
| 100 | * @param kind The job kind to look for. | ||
| 101 | * @return True when that kind is in flight, queued, or held after a nack. | ||
| 102 | */ | ||
| 103 | 31 | static inline bool outbox_pending(const OutboxQueue *outbox, OutboxKind kind) | |
| 104 | { | ||
| 105 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 30 times.
|
31 | if (kind == OUTBOX_NONE) |
| 106 | { | ||
| 107 | // the empty flight slot and every unused array entry read as OUTBOX_NONE, so without this | ||
| 108 | // an idle outbox would answer that nothing is already pending | ||
| 109 | 1 | return false; | |
| 110 | } | ||
| 111 | |||
| 112 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 29 times.
|
30 | if (outbox->inflight.kind == kind) |
| 113 | { | ||
| 114 | 1 | return true; | |
| 115 | } | ||
| 116 | |||
| 117 |
2/2✓ Branch 10 → 7 taken 53 times.
✓ Branch 10 → 11 taken 28 times.
|
81 | for (int i = 0; i < outbox->queue_len; i++) |
| 118 | { | ||
| 119 |
2/2✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 52 times.
|
53 | if (outbox->queue[i].kind == kind) |
| 120 | { | ||
| 121 | 1 | return true; | |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 |
2/2✓ Branch 15 → 12 taken 1 time.
✓ Branch 15 → 16 taken 27 times.
|
28 | for (int i = 0; i < outbox->failed_len; i++) |
| 126 | { | ||
| 127 |
1/2✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 14 not taken.
|
1 | if (outbox->failed[i].kind == kind) |
| 128 | { | ||
| 129 | 1 | return true; | |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 27 | return false; | |
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * @brief Add a job to the work queue unless its kind is already pending. | ||
| 138 | * | ||
| 139 | * @param outbox The outbox to add to. | ||
| 140 | * @param kind The job kind to send. | ||
| 141 | * @param retries Retry passes left if the first send nacks. | ||
| 142 | * @return True when the job was added, false when it was already pending or the queue is full. | ||
| 143 | */ | ||
| 144 | 30 | static inline bool outbox_push(OutboxQueue *outbox, OutboxKind kind, int retries) | |
| 145 | { | ||
| 146 |
4/4✓ Branch 3 → 4 taken 27 times.
✓ Branch 3 → 5 taken 3 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 26 times.
|
30 | if (outbox_pending(outbox, kind) || outbox->queue_len >= OUTBOX_QUEUE_MAX) |
| 147 | { | ||
| 148 | 4 | return false; | |
| 149 | } | ||
| 150 | |||
| 151 | 26 | outbox->queue[outbox->queue_len].kind = kind; | |
| 152 | 26 | outbox->queue[outbox->queue_len].retries_left = retries; | |
| 153 | 26 | outbox->queue_len++; | |
| 154 | 26 | return true; | |
| 155 | } | ||
| 156 | |||
| 157 | /** | ||
| 158 | * @brief Drop the front job off the work queue, shifting the rest forward. | ||
| 159 | * | ||
| 160 | * @param outbox The outbox to drop from. | ||
| 161 | */ | ||
| 162 | 13 | static inline void outbox_pop_front(OutboxQueue *outbox) | |
| 163 | { | ||
| 164 |
2/2✓ Branch 4 → 3 taken 23 times.
✓ Branch 4 → 5 taken 13 times.
|
36 | for (int i = 1; i < outbox->queue_len; i++) |
| 165 | { | ||
| 166 | 23 | outbox->queue[i - 1] = outbox->queue[i]; | |
| 167 | } | ||
| 168 | |||
| 169 |
2/2✓ Branch 5 → 6 taken 12 times.
✓ Branch 5 → 7 taken 1 time.
|
13 | if (outbox->queue_len > 0) |
| 170 | { | ||
| 171 | 12 | outbox->queue_len--; | |
| 172 | } | ||
| 173 | 13 | } | |
| 174 | |||
| 175 | /** | ||
| 176 | * @brief Take the head of the queue as the job now in flight. | ||
| 177 | * | ||
| 178 | * The caller sends it first and only takes it once the send was accepted, so a refused send leaves | ||
| 179 | * the head where it was. | ||
| 180 | * | ||
| 181 | * @param outbox The outbox to take from. | ||
| 182 | */ | ||
| 183 | 12 | static inline void outbox_take_head(OutboxQueue *outbox) | |
| 184 | { | ||
| 185 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 11 times.
|
12 | if (outbox->queue_len == 0) |
| 186 | { | ||
| 187 | 1 | return; // outbox_clear leaves the array behind, so an empty queue still has one to read | |
| 188 | } | ||
| 189 | |||
| 190 | 11 | outbox->inflight = outbox->queue[0]; | |
| 191 | 11 | outbox_pop_front(outbox); | |
| 192 | } | ||
| 193 | |||
| 194 | /** | ||
| 195 | * @brief Mark whatever was in flight as settled, whichever way it went. | ||
| 196 | * | ||
| 197 | * @param outbox The outbox to clear. | ||
| 198 | * @return The job that was in flight, so a nacked one can be handed to outbox_hold_failed. | ||
| 199 | */ | ||
| 200 | 8 | static inline OutboxJob outbox_release(OutboxQueue *outbox) | |
| 201 | { | ||
| 202 | 8 | OutboxJob settled = outbox->inflight; | |
| 203 | 8 | outbox->inflight.kind = OUTBOX_NONE; | |
| 204 | 8 | return settled; | |
| 205 | } | ||
| 206 | |||
| 207 | /** | ||
| 208 | * @brief Hold a nacked job for the next pass, if it still has retries. | ||
| 209 | * | ||
| 210 | * @param outbox The outbox to hold it in. | ||
| 211 | * @param job The job that just nacked. | ||
| 212 | */ | ||
| 213 | 20 | static inline void outbox_hold_failed(OutboxQueue *outbox, OutboxJob job) | |
| 214 | { | ||
| 215 | // a failure reported once the slot is already free comes with no job, and holding it would | ||
| 216 | // queue a send for nothing ahead of the real requests | ||
| 217 |
4/4✓ Branch 2 → 3 taken 19 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 17 times.
|
20 | if (job.kind == OUTBOX_NONE || job.retries_left <= 0) |
| 218 | { | ||
| 219 | 3 | return; | |
| 220 | } | ||
| 221 | |||
| 222 |
1/2✓ Branch 5 → 6 taken 17 times.
✗ Branch 5 → 7 not taken.
|
17 | if (outbox->failed_len < OUTBOX_QUEUE_MAX) |
| 223 | { | ||
| 224 | 17 | outbox->failed[outbox->failed_len].kind = job.kind; | |
| 225 | 17 | outbox->failed[outbox->failed_len].retries_left = job.retries_left - 1; | |
| 226 | 17 | outbox->failed_len++; | |
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | /** | ||
| 231 | * @brief Move the whole failed set back into the work queue for another pass. | ||
| 232 | * | ||
| 233 | * @param outbox The outbox to move within. | ||
| 234 | * @return How many jobs were moved back. | ||
| 235 | */ | ||
| 236 | 2 | static inline int outbox_retry_pass(OutboxQueue *outbox) | |
| 237 | { | ||
| 238 | 2 | int moved = 0; | |
| 239 | |||
| 240 |
3/4✓ Branch 7 → 8 taken 9 times.
✓ Branch 7 → 9 taken 2 times.
✓ Branch 8 → 3 taken 9 times.
✗ Branch 8 → 9 not taken.
|
11 | while (outbox->failed_len > 0 && outbox->queue_len < OUTBOX_QUEUE_MAX) |
| 241 | { | ||
| 242 | 9 | outbox->queue[outbox->queue_len++] = outbox->failed[0]; | |
| 243 |
2/2✓ Branch 5 → 4 taken 22 times.
✓ Branch 5 → 6 taken 9 times.
|
31 | for (int i = 1; i < outbox->failed_len; i++) |
| 244 | { | ||
| 245 | 22 | outbox->failed[i - 1] = outbox->failed[i]; | |
| 246 | } | ||
| 247 | 9 | outbox->failed_len--; | |
| 248 | 9 | moved++; | |
| 249 | } | ||
| 250 | |||
| 251 | 2 | return moved; | |
| 252 | } | ||
| 253 | |||
| 254 | /** | ||
| 255 | * @brief Forget everything waiting, for a phone that is no longer there. | ||
| 256 | * | ||
| 257 | * Nothing reaches a phone that is not connected, so a store's next poll asks again once it is back | ||
| 258 | * rather than a stale queue draining into it. | ||
| 259 | * | ||
| 260 | * @param outbox The outbox to empty. | ||
| 261 | */ | ||
| 262 | 1 | static inline void outbox_clear(OutboxQueue *outbox) | |
| 263 | { | ||
| 264 | 1 | outbox->queue_len = 0; | |
| 265 | 1 | outbox->failed_len = 0; | |
| 266 | 1 | } | |
| 267 | |||
| 268 | /** @} */ | ||
| 269 |