pebble/io/stores/store_poll.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file store_poll.h | ||
| 3 | * @brief Wall-clock deadlines for the stores that poll the phone. | ||
| 4 | * | ||
| 5 | * @ingroup lib_stores | ||
| 6 | */ | ||
| 7 | #pragma once | ||
| 8 | #include <pebble.h> | ||
| 9 | #include <time.h> | ||
| 10 | |||
| 11 | /** | ||
| 12 | * @addtogroup lib_stores | ||
| 13 | * @{ | ||
| 14 | */ | ||
| 15 | |||
| 16 | /** | ||
| 17 | * @brief The next wall-clock second a poll of this interval falls due. | ||
| 18 | * | ||
| 19 | * Counted from the epoch rather than from launch, so two watches started minutes apart poll at | ||
| 20 | * the same moments and a relaunch does not shift the phase. | ||
| 21 | * | ||
| 22 | * @param poll_min Minutes between polls. Must be above 0. | ||
| 23 | * @param now The current wall-clock time. | ||
| 24 | * @return The second the next poll is due. | ||
| 25 | */ | ||
| 26 | 26 | static inline time_t store_poll_next(int poll_min, time_t now) | |
| 27 | { | ||
| 28 | 26 | time_t interval = (time_t)poll_min * SECONDS_PER_MINUTE; | |
| 29 | 26 | return ((now / interval) + 1) * interval; | |
| 30 | } | ||
| 31 | |||
| 32 | /** | ||
| 33 | * @brief Whether @p next has come round, moving it on to the following deadline when it has. | ||
| 34 | * | ||
| 35 | * A clock that jumps forward just fires once and carries on. One that jumps back would strand the | ||
| 36 | * deadline more than an interval ahead, so that case is pulled back in. | ||
| 37 | * | ||
| 38 | * @param poll_min Minutes between polls. 0 or less means polling is off and this is never due. | ||
| 39 | * @param[in,out] next The store's deadline, read and updated in place. | ||
| 40 | * @param now The current wall-clock time. | ||
| 41 | * @return Whether a poll should go out now. | ||
| 42 | */ | ||
| 43 | 13 | static inline bool store_poll_due(int poll_min, time_t *next, time_t now) | |
| 44 | { | ||
| 45 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 11 times.
|
13 | if (poll_min <= 0) |
| 46 | { | ||
| 47 | 2 | return false; | |
| 48 | } | ||
| 49 | |||
| 50 | 11 | time_t interval = (time_t)poll_min * SECONDS_PER_MINUTE; | |
| 51 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 7 taken 10 times.
|
11 | if (*next > now + interval) |
| 52 | { | ||
| 53 | 1 | *next = store_poll_next(poll_min, now); | |
| 54 | } | ||
| 55 | |||
| 56 |
2/2✓ Branch 7 → 8 taken 5 times.
✓ Branch 7 → 9 taken 6 times.
|
11 | if (now < *next) |
| 57 | { | ||
| 58 | 5 | return false; | |
| 59 | } | ||
| 60 | |||
| 61 | 6 | *next = store_poll_next(poll_min, now); | |
| 62 | 6 | return true; | |
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @brief Whether a reading is worth asking for again now that the phone is back. | ||
| 67 | * | ||
| 68 | * The phone's side may have been closed the whole time it was away, so a face catches up a reading | ||
| 69 | * that went stale meanwhile. Only a reading that never arrived, or one at least its own interval old, | ||
| 70 | * is asked for, so bluetooth dropping in and out cannot fetch faster than the normal polling. Polling | ||
| 71 | * turned off is left alone, the same as its store. | ||
| 72 | * | ||
| 73 | * @param age_s Seconds since the reading arrived, or below 0 when it never has. | ||
| 74 | * @param poll_min Minutes between polls. 0 or less means polling is off. | ||
| 75 | * @return Whether to ask for the reading again. | ||
| 76 | */ | ||
| 77 | 9 | static inline bool store_poll_reconnect_due(int age_s, int poll_min) | |
| 78 | { | ||
| 79 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 7 times.
|
9 | if (poll_min <= 0) |
| 80 | { | ||
| 81 | 2 | return false; | |
| 82 | } | ||
| 83 | // a clock set back makes a saved reading's age negative too, so it reads as never arrived and the | ||
| 84 | // reconnect asks again. the first answer resets the age, and a reconnect means the phone is there | ||
| 85 | // to give one, so it costs about one fetch. counting that age as fresh instead would skip every | ||
| 86 | // reconnect fetch until the clock caught up, which can be hours | ||
| 87 |
4/4✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 1 time.
✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 7 taken 3 times.
|
7 | return age_s < 0 || age_s >= poll_min * SECONDS_PER_MINUTE; |
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @brief One store's polling: its interval, whether it polls at all, and when it is next due. | ||
| 92 | * | ||
| 93 | * A StorePoll has to begin zeroed, as a static one does, or go through store_poll_start before | ||
| 94 | * anything else reads it. Zero one on the stack or the heap before its first store_poll_set, or a | ||
| 95 | * stray `pinned` keeps it off for good. | ||
| 96 | */ | ||
| 97 | typedef struct | ||
| 98 | { | ||
| 99 | time_t next; ///< Wall-clock second the next recurring poll is due | ||
| 100 | int poll_min; ///< Minutes between polls. 0 or less means no recurring poll | ||
| 101 | bool live; ///< True on a live face. A store seeded with fixtures never polls | ||
| 102 | bool pinned; ///< True when init started the store not live, so no reconfigure turns it live | ||
| 103 | } StorePoll; | ||
| 104 | |||
| 105 | /** | ||
| 106 | * @brief Take a store's interval and whether it is live, as its init does. | ||
| 107 | * | ||
| 108 | * Init is the one place that decides whether the store is live at all. A store it starts with | ||
| 109 | * `live` false is pinned, and stays not live through every reconfigure, as store_poll_set covers. | ||
| 110 | * A store whose init calls store_poll_set instead is never pinned, so a reconfigure can turn it | ||
| 111 | * live. | ||
| 112 | * | ||
| 113 | * The pin only covers asking the phone. A store that also takes pushes or listens for the link | ||
| 114 | * registers its handler and its listener only when init starts it live. An init that starts it with | ||
| 115 | * `live` false clears the handler and removes the listener, so a store started live and then pinned | ||
| 116 | * takes no push over its seed. | ||
| 117 | * | ||
| 118 | * @param[in,out] poll The store's polling state. | ||
| 119 | * @param poll_min Minutes between polls. 0 or less turns polling off. | ||
| 120 | * @param live Whether the store is live. | ||
| 121 | * @param now The current wall-clock time. | ||
| 122 | * @return Whether polling is on, which is when a store may arm its first fetch. | ||
| 123 | */ | ||
| 124 | bool store_poll_start(StorePoll *poll, int poll_min, bool live, time_t now); | ||
| 125 | |||
| 126 | /** | ||
| 127 | * @brief Take a store's interval and whether it is live, as its reconfigure does. | ||
| 128 | * | ||
| 129 | * Both are always recorded. A store that store_poll_start pinned stays not live whatever @p live | ||
| 130 | * says, so a settings save in a build seeded for screenshots never starts it fetching over its | ||
| 131 | * fixture. A live store can still be switched off here and back on again. The deadline only moves | ||
| 132 | * when polling is on, and then to the next interval boundary, so a save can bring a poll nearer but | ||
| 133 | * never make one come faster than the interval allows. | ||
| 134 | * | ||
| 135 | * @param[in,out] poll The store's polling state. | ||
| 136 | * @param poll_min Minutes between polls. 0 or less turns polling off. | ||
| 137 | * @param live Whether the store should be live. | ||
| 138 | * @param now The current wall-clock time. | ||
| 139 | * @return Whether polling is on, which is when a store may arm its catch-up fetch. | ||
| 140 | */ | ||
| 141 | bool store_poll_set(StorePoll *poll, int poll_min, bool live, time_t now); | ||
| 142 | |||
| 143 | /** | ||
| 144 | * @brief A store's turn on the face's cadence: whether a poll should go out now. | ||
| 145 | * | ||
| 146 | * @param[in,out] poll The store's polling state. The deadline moves on when a poll is due. | ||
| 147 | * @param now The current wall-clock time. | ||
| 148 | * @return Whether to ask the phone now. | ||
| 149 | */ | ||
| 150 | bool store_poll_turn(StorePoll *poll, time_t now); | ||
| 151 | |||
| 152 | /** | ||
| 153 | * @brief A store's catch-up when the phone comes back: whether to ask for its reading again. | ||
| 154 | * | ||
| 155 | * Only a live store asks, so a store seeded for screenshots keeps its fixture through a reconnect. | ||
| 156 | * The age and the interval decide the rest, the same as store_poll_reconnect_due. The deadline is | ||
| 157 | * left alone, so a catch-up never pushes the next recurring poll further away. | ||
| 158 | * | ||
| 159 | * @param poll The store's polling state. | ||
| 160 | * @param age_s Seconds since the reading arrived, or below 0 when it never has. | ||
| 161 | * @return Whether to ask the phone now. | ||
| 162 | */ | ||
| 163 | bool store_poll_reconnect(const StorePoll *poll, int age_s); | ||
| 164 | |||
| 165 | /** @} */ | ||
| 166 |