core/io/link_change.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file link_change.h | ||
| 3 | * @brief Tells a real change of the phone link from a reading that changes nothing. | ||
| 4 | * | ||
| 5 | * The system store hands each reading of the link here and acts on the answer. The first reading | ||
| 6 | * after the store starts only seeds, so a face never buzzes or catches up at launch. A reading that | ||
| 7 | * matches the one before is no change either. Only a real drop or a real return reaches the face's | ||
| 8 | * buzz, its reconnect callback, and the stores listening for the link. | ||
| 9 | * | ||
| 10 | * @ingroup lib_core | ||
| 11 | */ | ||
| 12 | #pragma once | ||
| 13 | #include <stdbool.h> | ||
| 14 | |||
| 15 | /** | ||
| 16 | * @addtogroup lib_core | ||
| 17 | * @{ | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @brief What one reading of the link means. | ||
| 22 | */ | ||
| 23 | typedef enum | ||
| 24 | { | ||
| 25 | LINK_SAME, ///< The first reading, or one that matches the last. Nothing to act on | ||
| 26 | LINK_DROPPED, ///< The phone link just went down | ||
| 27 | LINK_RECONNECTED ///< The phone link just came back | ||
| 28 | } LinkChange; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @brief The link as last read. Zeroed is a store that has read nothing yet. | ||
| 32 | */ | ||
| 33 | typedef struct | ||
| 34 | { | ||
| 35 | bool connected; ///< True when the phone link was up at the last reading | ||
| 36 | bool seeded; ///< True once a reading has come in, so the next one can be a change | ||
| 37 | } LinkState; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @brief Takes one reading of the link and says what it means. | ||
| 41 | * | ||
| 42 | * The new reading is kept whatever it means, so a store reading the state from inside a buzz or a | ||
| 43 | * catch-up sees the link as it is now. | ||
| 44 | * | ||
| 45 | * @param state The link as last read. Updated to this reading. | ||
| 46 | * @param connected True when the phone link is up now. | ||
| 47 | * @return LINK_DROPPED or LINK_RECONNECTED for a real change, and LINK_SAME for the first reading or a repeat. | ||
| 48 | */ | ||
| 49 | 13 | static inline LinkChange link_change_read(LinkState *state, bool connected) | |
| 50 | { | ||
| 51 | 13 | bool was_connected = state->connected; | |
| 52 | 13 | bool seeded = state->seeded; | |
| 53 | |||
| 54 | 13 | state->connected = connected; | |
| 55 | 13 | state->seeded = true; | |
| 56 | |||
| 57 |
4/4✓ Branch 2 → 3 taken 6 times.
✓ Branch 2 → 4 taken 7 times.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 4 times.
|
13 | if (!seeded || was_connected == connected) |
| 58 | { | ||
| 59 | 9 | return LINK_SAME; | |
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 2 times.
|
4 | return connected ? LINK_RECONNECTED : LINK_DROPPED; |
| 63 | } | ||
| 64 | |||
| 65 | /** @} */ | ||
| 66 |