Skip to content

Commit c041cae

Browse files
committed
pbio/sys/storage: Add basics for persistent settings.
1 parent aea63d0 commit c041cae

3 files changed

Lines changed: 71 additions & 8 deletions

File tree

lib/pbio/include/pbsys/storage.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
#include <pbsys/config.h>
1818

19+
/**
20+
* System settings. All data types are little-endian.
21+
*/
22+
typedef struct _pbsys_storage_settings_t {
23+
/**
24+
* User has enabled Bluetooth Low Energy.
25+
*/
26+
bool bluetooth_ble_user_enabled : 1;
27+
} pbsys_storage_settings_t;
28+
1929
#if PBSYS_CONFIG_STORAGE
2030

2131
// Sanity check that application RAM is enough to load ROM and still do something useful
@@ -51,6 +61,11 @@ typedef struct _pbsys_storage_data_map_t {
5161
* End-user read-write accessible data.
5262
*/
5363
uint8_t user_data[PBSYS_CONFIG_STORAGE_USER_DATA_SIZE];
64+
/**
65+
* System settings. Settings will be reset to defaults when the firmware
66+
* version changes due to an update.
67+
*/
68+
pbsys_storage_settings_t settings;
5469
/**
5570
* Size of the application program (size of code only).
5671
*/
@@ -67,6 +82,10 @@ pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, u
6782

6883
pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32_t size);
6984

85+
pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings);
86+
87+
void pbsys_storage_request_settings_write(void);
88+
7089
#else
7190

7291
#define PBSYS_STORAGE_MAX_PROGRAM_SIZE (0)
@@ -80,6 +99,13 @@ static inline pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t
8099
return PBIO_ERROR_NOT_SUPPORTED;
81100
}
82101

102+
static inline pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings) {
103+
*settings = NULL;
104+
return PBIO_ERROR_NOT_SUPPORTED;
105+
}
106+
107+
static inline void pbsys_storage_request_settings_write(void) {
108+
}
83109

84110
#endif // PBSYS_CONFIG_STORAGE
85111

lib/pbio/sys/bluetooth.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <pbsys/bluetooth.h>
2222
#include <pbsys/command.h>
2323
#include <pbsys/status.h>
24+
#include <pbsys/storage.h>
2425

2526
#include "light.h"
2627

@@ -202,10 +203,13 @@ bool pbsys_bluetooth_tx_is_idle(void) {
202203
return !send_busy && lwrb_get_full(&stdout_ring_buf) == 0;
203204
}
204205

205-
static bool pbsys_bluetooth_user_enabled = true;
206+
static pbsys_storage_settings_t *settings;
206207

207208
bool pbsys_bluetooth_is_user_enabled(void) {
208-
return pbsys_bluetooth_user_enabled;
209+
if (!settings) {
210+
return false;
211+
}
212+
return settings->bluetooth_ble_user_enabled;
209213
}
210214

211215
void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
@@ -219,13 +223,14 @@ void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
219223
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
220224
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)
221225
// Ignore if last request not yet finished processing.
222-
|| pbsys_bluetooth_user_enabled != pbdrv_bluetooth_is_ready()
226+
|| settings->bluetooth_ble_user_enabled != pbdrv_bluetooth_is_ready()
223227
) {
224228
return;
225229
}
226230

227231
// Toggle the user enabled state and poll process to take action.
228-
pbsys_bluetooth_user_enabled = !pbsys_bluetooth_user_enabled;
232+
settings->bluetooth_ble_user_enabled = !settings->bluetooth_ble_user_enabled;
233+
pbsys_storage_request_settings_write();
229234
pbsys_bluetooth_process_poll();
230235
}
231236

@@ -316,6 +321,8 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
316321

317322
PROCESS_BEGIN();
318323

324+
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_get_settings(&settings) == PBIO_SUCCESS);
325+
319326
pbdrv_bluetooth_set_on_event(pbsys_bluetooth_process_poll);
320327
pbdrv_bluetooth_set_receive_handler(handle_receive);
321328

@@ -325,7 +332,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
325332
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
326333

327334
// Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
328-
PROCESS_WAIT_UNTIL(pbsys_bluetooth_user_enabled || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
335+
PROCESS_WAIT_UNTIL(settings->bluetooth_ble_user_enabled || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
329336
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
330337
break;
331338
}
@@ -346,7 +353,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
346353
pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
347354
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
348355
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
349-
|| !pbsys_bluetooth_user_enabled);
356+
|| !settings->bluetooth_ble_user_enabled);
350357

351358
// Now change the state depending on which of the above was triggered.
352359

@@ -365,7 +372,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
365372
// The Bluetooth enabled flag can only change while disconnected and
366373
// while no program is running. So here it just serves to skip the
367374
// Bluetooth loop below and go directly to the disable step below it.
368-
while (pbsys_bluetooth_user_enabled
375+
while (settings->bluetooth_ble_user_enabled
369376
&& pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
370377
&& !pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
371378

@@ -406,7 +413,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
406413

407414
// Indicate status only if user requested Bluetooth to be disabled to
408415
// avoid always flashing red in between program runs when disconnected.
409-
if (!pbsys_bluetooth_user_enabled) {
416+
if (!settings->bluetooth_ble_user_enabled) {
410417
pbsys_status_light_bluetooth_set_color(PBIO_COLOR_RED);
411418
}
412419

lib/pbio/sys/storage.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ union {
3131

3232
static pbsys_storage_data_map_t *map = &pbsys_user_ram_data_map.data_map;
3333

34+
static bool data_map_is_loaded = false;
35+
3436
/**
3537
* Sets write size to how much data must be written on shutdown. This is not
3638
* simply a boolean flag because it is also used as the load size on boot.
@@ -76,6 +78,30 @@ pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32
7678
return PBIO_SUCCESS;
7779
}
7880

81+
/**
82+
* Requests that settings will be saved on shutdown. Should be called by
83+
* functions that change user settings.
84+
*/
85+
void pbsys_storage_request_settings_write(void) {
86+
update_write_size();
87+
}
88+
89+
/**
90+
* Gets the stored system settings.
91+
*
92+
* @param [out] settings The settings.
93+
* @returns ::PBIO_ERROR_AGAIN if settings still being loaded.
94+
* Otherwise, ::PBIO_SUCCESS.
95+
*/
96+
pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings) {
97+
if (!data_map_is_loaded) {
98+
return PBIO_ERROR_AGAIN;
99+
}
100+
101+
*settings = &map->settings;
102+
return PBIO_SUCCESS;
103+
}
104+
79105
#if PBSYS_CONFIG_STORAGE_OVERLAPS_BOOTLOADER_CHECKSUM
80106
// Updates checksum in data map to satisfy bootloader requirements.
81107
static void pbsys_storage_update_checksum(void) {
@@ -236,6 +262,10 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
236262
update_write_size();
237263
}
238264

265+
// Poke processes that await on system settings to become available.
266+
data_map_is_loaded = true;
267+
process_post(PROCESS_BROADCAST, PROCESS_EVENT_COM, NULL);
268+
239269
// Initialization done.
240270
pbsys_init_busy_down();
241271

0 commit comments

Comments
 (0)