pebble/io/long_text.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file long_text.c | ||
| 3 | * @brief The deciding behind the face's long text setting: when it can be set, whether it fits, what | ||
| 4 | * the reply carries, and whether an arrival changed it. | ||
| 5 | * | ||
| 6 | * @ingroup lib_io | ||
| 7 | */ | ||
| 8 | #include "io/long_text.h" | ||
| 9 | |||
| 10 | #include <pebble.h> | ||
| 11 | #include <string.h> | ||
| 12 | |||
| 13 | 13 | bool long_text_set(LongText *slot, uint32_t key, uint16_t cap, LongTextProvider provider, | |
| 14 | LongTextReceiver receiver, bool opened) | ||
| 15 | { | ||
| 16 | // the outbox is sized at open, so a text set after it could outgrow the settings reply, and the | ||
| 17 | // phone would get an empty one on every launch | ||
| 18 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 12 times.
|
13 | if (opened) |
| 19 | { | ||
| 20 | 1 | APP_LOG(APP_LOG_LEVEL_ERROR, "long text set after appmessage_open, so it is left out"); | |
| 21 | 1 | return false; | |
| 22 | } | ||
| 23 | |||
| 24 | // a cap of 0 has no room for even the end of the text, and the buffer for one past the most sits | ||
| 25 | // on the stack for each send or compare | ||
| 26 |
4/4✓ Branch 5 → 6 taken 11 times.
✓ Branch 5 → 7 taken 1 time.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 9 taken 10 times.
|
12 | if (cap == 0 || cap > LONG_TEXT_CAP_MAX) |
| 27 | { | ||
| 28 | 2 | APP_LOG(APP_LOG_LEVEL_ERROR, "long text cap %d is outside 1 to %d", (int)cap, LONG_TEXT_CAP_MAX); | |
| 29 | 2 | return false; | |
| 30 | } | ||
| 31 | |||
| 32 | // without the provider every save would count as a change, and without the receiver nothing would | ||
| 33 | // keep what the page sends | ||
| 34 |
4/4✓ Branch 9 → 10 taken 9 times.
✓ Branch 9 → 11 taken 1 time.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 13 taken 8 times.
|
10 | if (!provider || !receiver) |
| 35 | { | ||
| 36 | 2 | APP_LOG(APP_LOG_LEVEL_ERROR, "long text needs both a provider and a receiver"); | |
| 37 | 2 | return false; | |
| 38 | } | ||
| 39 | |||
| 40 | 8 | *slot = (LongText){ .key = key, .cap = cap, .provider = provider, .receiver = receiver }; | |
| 41 | 8 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 3 | bool long_text_fits(uint32_t text_bytes, uint32_t reply_bytes, uint32_t outbox_max, uint32_t inbox_room) | |
| 45 | { | ||
| 46 |
4/4✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
|
3 | return reply_bytes + text_bytes <= outbox_max && text_bytes <= inbox_room; |
| 47 | } | ||
| 48 | |||
| 49 | 5 | void long_text_held(const LongText *slot, char *out) | |
| 50 | { | ||
| 51 | // the last byte is set after the provider runs, so a provider that fills the buffer to the brim | ||
| 52 | // still leaves an ended string | ||
| 53 | 5 | out[0] = '\0'; | |
| 54 | 5 | slot->provider(out, slot->cap); | |
| 55 | 5 | out[slot->cap - 1] = '\0'; | |
| 56 | 5 | } | |
| 57 | |||
| 58 | 4 | bool long_text_arrived(const LongText *slot, const char *text) | |
| 59 | 4 | { | |
| 60 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 3 times.
|
4 | if (slot->cap == 0) |
| 61 | { | ||
| 62 | 1 | return false; | |
| 63 | } | ||
| 64 | |||
| 65 | // the face's text is rebuilt into a stack buffer of the cap's size for the compare, which | ||
| 66 | // long_text_set holds to LONG_TEXT_CAP_MAX | ||
| 67 | 3 | char held[slot->cap]; | |
| 68 | 3 | long_text_held(slot, held); | |
| 69 | |||
| 70 |
2/2✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 1 time.
|
3 | if (strncmp(text, held, (size_t)slot->cap - 1) == 0) |
| 71 | { | ||
| 72 | 2 | return false; | |
| 73 | } | ||
| 74 | |||
| 75 | 1 | slot->receiver(text); | |
| 76 | 1 | return true; | |
| 77 | } | ||
| 78 |