Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 2 / 0 / 2
Functions: 100.0% 1 / 0 / 1
Branches: 100.0% 4 / 0 / 4

pebble/system/settings/plugin_source.h
Line Branch Exec Source
1 /**
2 * @file plugin_source.h
3 * @brief The struct the framework keeps the weather, calendar, and stock source choices in, for a face
4 * built with a plugin define, and the checks that decide which of them it keeps.
5 *
6 * The watch never reads the choice. It stores what the settings page saves and sends it back when the
7 * phone asks for the watch's settings, so a wearer who picked the Pebble app's plugin still has it
8 * after the Pebble app is reinstalled. settings.c builds its schema from PLUGIN_SOURCE_FIELDS and
9 * walks it after the face's own, on SETTINGS_PLUGIN_SOURCE_KEY, so the face writes no field of its own
10 * for it. A save skips the write while flash already holds the choices.
11 *
12 * A kind keeps its choice when its define is on and the face declares its select key. The build
13 * refuses the select key beside a plugin only providers list, so a plugin only kind keeps nothing.
14 * The build also writes each kept kind's default as PLUGIN_SOURCE_WEATHER_DEFAULT,
15 * PLUGIN_SOURCE_CALENDAR_DEFAULT, or PLUGIN_SOURCE_STOCK_DEFAULT, from the face's providers list,
16 * so the watch's default is always the settings page's. The build stops when the face's own picker
17 * does not offer it, or when the page opens on another source.
18 *
19 * @ingroup lib_settings
20 */
21 #pragma once
22 #include "system/settings/settings.h"
23
24 #include <stddef.h>
25 #include <string.h>
26
27 /**
28 * @addtogroup lib_settings
29 * @{
30 */
31
32 /// 1 when the watch keeps the weather source choice, for a face with PLUGIN_WEATHER and WEATHER_PROVIDER
33 #if defined(PLUGIN_WEATHER) && PLUGIN_WEATHER && defined(HAS_MESSAGE_KEY_WEATHER_PROVIDER)
34 #define PLUGIN_SOURCE_KEEPS_WEATHER 1
35 #else
36 #define PLUGIN_SOURCE_KEEPS_WEATHER 0
37 #endif
38
39 /// 1 when the watch keeps the calendar source choice, for a face with PLUGIN_CALENDAR and CALENDAR_SOURCE
40 #if defined(PLUGIN_CALENDAR) && PLUGIN_CALENDAR && defined(HAS_MESSAGE_KEY_CALENDAR_SOURCE)
41 #define PLUGIN_SOURCE_KEEPS_CALENDAR 1
42 #else
43 #define PLUGIN_SOURCE_KEEPS_CALENDAR 0
44 #endif
45
46 /// 1 when the watch keeps the stock source choice, for a face with PLUGIN_STOCKS and STOCK_PROVIDER
47 #if defined(PLUGIN_STOCKS) && PLUGIN_STOCKS && defined(HAS_MESSAGE_KEY_STOCK_PROVIDER)
48 #define PLUGIN_SOURCE_KEEPS_STOCK 1
49 #else
50 #define PLUGIN_SOURCE_KEEPS_STOCK 0
51 #endif
52
53 /// The watch keeps at least one source choice, so the framework's schema joins the chain
54 #define PLUGIN_SOURCE_KEEPS_ANY (PLUGIN_SOURCE_KEEPS_WEATHER || PLUGIN_SOURCE_KEEPS_CALENDAR || PLUGIN_SOURCE_KEEPS_STOCK)
55
56 /**
57 * @brief The size of the weather source buffer. `weatherapi` is the longest value the settings page
58 * saves, and its 10 characters plus its end leave one byte spare.
59 */
60 #define PLUGIN_SOURCE_WEATHER_LEN 12
61
62 /**
63 * @brief The size of the calendar source buffer. `plugin` is the longest value the settings page
64 * saves, and its 6 characters plus its end leave one byte spare.
65 */
66 #define PLUGIN_SOURCE_CALENDAR_LEN 8
67
68 /**
69 * @brief The size of the stock source buffer. `alphavantage` is the longest value the settings page
70 * saves, and its 12 characters plus its end leave one byte spare.
71 */
72 #define PLUGIN_SOURCE_STOCK_LEN 14
73
74 #define PLUGIN_SOURCE_VERSION 2 ///< The schema's version, raised by one with each appended field
75 #define PLUGIN_SOURCE_FIRST_SIZE 21 ///< The struct's size in its first release, which never changes
76
77 /**
78 * @brief The saved choices, version byte first.
79 *
80 * Every buffer is always there, whichever kinds the face keeps. A face that turns another kind on
81 * in a later release then reads the same saved blob, and the new kind starts on its default. A
82 * version 1 blob is the first 21 bytes and ends before the stock buffer, so reading one leaves the
83 * stock choice on the default it was given before the read.
84 */
85 typedef struct
86 {
87 uint8_t version; ///< The schema version this blob was saved at
88 char weather[PLUGIN_SOURCE_WEATHER_LEN]; ///< What the weather's Data Source select saved
89 char calendar[PLUGIN_SOURCE_CALENDAR_LEN]; ///< What the Calendar Source select saved
90 char stock[PLUGIN_SOURCE_STOCK_LEN]; ///< What the stocks' Data Source select saved
91 } PluginSourceSettings;
92
93 // a saved choice is read back from these offsets, so moving a field would read the other kind's
94 // bytes into it after an update. a new field goes on the end
95 _Static_assert(offsetof(PluginSourceSettings, weather) == 1, "the weather source moved, so older saves would read wrong");
96 _Static_assert(offsetof(PluginSourceSettings, calendar) == 13, "the calendar source moved, so older saves would read wrong");
97 _Static_assert(offsetof(PluginSourceSettings, stock) == PLUGIN_SOURCE_FIRST_SIZE, "the stock source has to start where a first release blob ends");
98
99 #if PLUGIN_SOURCE_KEEPS_WEATHER
100 #ifndef PLUGIN_SOURCE_WEATHER_DEFAULT
101 #error "PLUGIN_WEATHER keeps the weather source on the watch, so it needs PLUGIN_SOURCE_WEATHER_DEFAULT. Build the face with paf build, which writes it from the providers list"
102 #endif
103 // the "" in front makes anything but a string literal stop the build
104 _Static_assert(sizeof("" PLUGIN_SOURCE_WEATHER_DEFAULT) > 1 && sizeof("" PLUGIN_SOURCE_WEATHER_DEFAULT) <= PLUGIN_SOURCE_WEATHER_LEN,
105 "the default weather source has to be 1 to 11 characters, which is as much as the watch keeps");
106 #endif
107
108 #if PLUGIN_SOURCE_KEEPS_CALENDAR
109 #ifndef PLUGIN_SOURCE_CALENDAR_DEFAULT
110 #error "PLUGIN_CALENDAR keeps the calendar source on the watch, so it needs PLUGIN_SOURCE_CALENDAR_DEFAULT. Build the face with paf build, which writes it from the providers list"
111 #endif
112 _Static_assert(sizeof("" PLUGIN_SOURCE_CALENDAR_DEFAULT) > 1 && sizeof("" PLUGIN_SOURCE_CALENDAR_DEFAULT) <= PLUGIN_SOURCE_CALENDAR_LEN,
113 "the default calendar source has to be 1 to 7 characters, which is as much as the watch keeps");
114 #endif
115
116 #if PLUGIN_SOURCE_KEEPS_STOCK
117 #ifndef PLUGIN_SOURCE_STOCK_DEFAULT
118 #error "PLUGIN_STOCKS keeps the stock source on the watch, so it needs PLUGIN_SOURCE_STOCK_DEFAULT. Build the face with paf build, which writes it from the providers list"
119 #endif
120 _Static_assert(sizeof("" PLUGIN_SOURCE_STOCK_DEFAULT) > 1 && sizeof("" PLUGIN_SOURCE_STOCK_DEFAULT) <= PLUGIN_SOURCE_STOCK_LEN,
121 "the default stock source has to be 1 to 13 characters, which is as much as the watch keeps");
122 #endif
123
124 /**
125 * @brief The field the framework keeps the weather source in, with a comma after it, or nothing when
126 * the watch keeps no weather choice.
127 *
128 * It is a face-only text field, so it has no typed read. Its default is the one the build wrote from
129 * the face's providers list, which is the settings page's own default. So a watch that never got a
130 * save sends back the choice the phone would make anyway.
131 */
132 #if PLUGIN_SOURCE_KEEPS_WEATHER
133 #define PLUGIN_SOURCE_WEATHER_FIELD \
134 { .id = SETTING_COUNT, .message_key = &MESSAGE_KEY_WEATHER_PROVIDER, .type = SETTING_CSTRING, \
135 .offset = offsetof(PluginSourceSettings, weather), .size = PLUGIN_SOURCE_WEATHER_LEN, \
136 .default_str = PLUGIN_SOURCE_WEATHER_DEFAULT },
137 #else
138 #define PLUGIN_SOURCE_WEATHER_FIELD
139 #endif
140
141 /**
142 * @brief The field the framework keeps the calendar source in, with a comma after it, or nothing when
143 * the watch keeps no calendar choice. It works as the weather one does.
144 */
145 #if PLUGIN_SOURCE_KEEPS_CALENDAR
146 #define PLUGIN_SOURCE_CALENDAR_FIELD \
147 { .id = SETTING_COUNT, .message_key = &MESSAGE_KEY_CALENDAR_SOURCE, .type = SETTING_CSTRING, \
148 .offset = offsetof(PluginSourceSettings, calendar), .size = PLUGIN_SOURCE_CALENDAR_LEN, \
149 .default_str = PLUGIN_SOURCE_CALENDAR_DEFAULT },
150 #else
151 #define PLUGIN_SOURCE_CALENDAR_FIELD
152 #endif
153
154 /**
155 * @brief The field the framework keeps the stock source in, with a comma after it, or nothing when
156 * the watch keeps no stock choice. It works as the weather one does.
157 */
158 #if PLUGIN_SOURCE_KEEPS_STOCK
159 #define PLUGIN_SOURCE_STOCK_FIELD { .id = SETTING_COUNT, .message_key = &MESSAGE_KEY_STOCK_PROVIDER, .type = SETTING_CSTRING, .offset = offsetof(PluginSourceSettings, stock), .size = PLUGIN_SOURCE_STOCK_LEN, .default_str = PLUGIN_SOURCE_STOCK_DEFAULT },
160 #else
161 #define PLUGIN_SOURCE_STOCK_FIELD
162 #endif
163
164 /// The framework schema's whole field table, for inside its braces
165 #define PLUGIN_SOURCE_FIELDS PLUGIN_SOURCE_WEATHER_FIELD PLUGIN_SOURCE_CALENDAR_FIELD PLUGIN_SOURCE_STOCK_FIELD
166
167 /**
168 * @brief Whether a save has to write the choices to flash.
169 *
170 * settings_save writes every schema on every save, and the choices only change when the wearer picks
171 * another source. So the write is skipped while the choices match what flash holds. With nothing known
172 * to be on flash it always writes, even when the choices are still the defaults. Skipping that write
173 * would leave the key empty, and the watch would count as fresh on every launch.
174 *
175 * @param now The choices as they are now.
176 * @param on_flash The choices as they were last loaded or written.
177 * @param on_flash_known Whether @p on_flash is really what flash holds. False when the key was empty
178 * at launch, or was dropped for a blob the watch could not read.
179 * @return True when the save has to write.
180 */
181 3 static inline bool plugin_source_write_needed(const PluginSourceSettings *now, const PluginSourceSettings *on_flash,
182 bool on_flash_known)
183 {
184
4/4
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 1 time.
3 return !on_flash_known || memcmp(now, on_flash, sizeof(*now)) != 0;
185 }
186
187 /** @} */
188