Skip to content

Commit 7887e97

Browse files
committed
pbio/sys/storage: Split settings to dedicated module.
1 parent fe54637 commit 7887e97

14 files changed

Lines changed: 168 additions & 114 deletions

File tree

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
233233
sys/program_stop.c \
234234
sys/status.c \
235235
sys/storage.c \
236+
sys/storage_settings.c \
236237
sys/supervisor.c \
237238
sys/user_program.c \
238239
)

lib/pbio/include/pbsys/bluetooth.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ uint32_t pbsys_bluetooth_rx_get_available(void);
3232
pbio_error_t pbsys_bluetooth_rx(uint8_t *data, uint32_t *size);
3333
pbio_error_t pbsys_bluetooth_tx(const uint8_t *data, uint32_t *size);
3434
bool pbsys_bluetooth_tx_is_idle(void);
35-
bool pbsys_bluetooth_is_user_enabled(void);
36-
void pbsys_bluetooth_is_user_enabled_request_toggle(void);
3735

3836
#else // PBSYS_CONFIG_BLUETOOTH
3937

@@ -51,11 +49,6 @@ static inline pbio_error_t pbsys_bluetooth_tx(const uint8_t *data, uint32_t *siz
5149
static inline bool pbsys_bluetooth_tx_is_idle(void) {
5250
return false;
5351
}
54-
static inline void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
55-
}
56-
static inline bool pbsys_bluetooth_is_user_enabled(void) {
57-
return true;
58-
}
5952

6053
#endif // PBSYS_CONFIG_BLUETOOTH
6154

lib/pbio/include/pbsys/storage.h

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2022 The Pybricks Authors
2+
// Copyright (c) 2022-2024 The Pybricks Authors
33

44
/**
5-
* @addtogroup SysProgramLoad System: Load user programs.
5+
* @addtogroup SysStorage System: Load user programs, data, and settings.
66
*
7-
* Configuration for loading data.
7+
* Load user programs, data, and settings.
88
*
99
* @{
1010
*/
@@ -15,16 +15,7 @@
1515
#include <stdint.h>
1616

1717
#include <pbsys/config.h>
18-
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;
18+
#include <pbsys/storage_settings.h>
2819

2920
#if PBSYS_CONFIG_STORAGE
3021

@@ -82,31 +73,18 @@ pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, u
8273

8374
pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32_t size);
8475

85-
pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings);
86-
87-
void pbsys_storage_request_write(void);
88-
8976
#else
9077

9178
#define PBSYS_STORAGE_MAX_PROGRAM_SIZE (0)
9279

9380
static inline pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, uint32_t size) {
9481
return PBIO_ERROR_NOT_SUPPORTED;
9582
}
96-
9783
static inline pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32_t size) {
9884
*data = NULL;
9985
return PBIO_ERROR_NOT_SUPPORTED;
10086
}
10187

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_write(void) {
108-
}
109-
11088
#endif // PBSYS_CONFIG_STORAGE
11189

11290
#endif // _PBSYS_STORAGE_H_
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2024 The Pybricks Authors
3+
4+
/**
5+
* @addtogroup SysStorageSettings System: Load user settings.
6+
*
7+
* Interface for reading and storing user system settings.
8+
*
9+
* @{
10+
*/
11+
12+
#ifndef _PBSYS_STORAGE_SETTINGS_H_
13+
#define _PBSYS_STORAGE_SETTINGS_H_
14+
15+
#include <stdint.h>
16+
17+
#include <pbio/error.h>
18+
19+
#include <pbsys/config.h>
20+
21+
/**
22+
* System settings. All data types are little-endian.
23+
*/
24+
typedef struct _pbsys_storage_settings_t {
25+
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
26+
/**
27+
* User has enabled Bluetooth Low Energy.
28+
*/
29+
bool bluetooth_ble_user_enabled : 1;
30+
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
31+
} pbsys_storage_settings_t;
32+
33+
#if PBSYS_CONFIG_STORAGE
34+
35+
void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings);
36+
37+
bool pbsys_storage_settings_bluetooth_enabled(void);
38+
39+
void pbsys_storage_settings_bluetooth_enabled_request_toggle(void);
40+
41+
#else
42+
43+
static inline void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings) {
44+
}
45+
static inline bool pbsys_storage_settings_bluetooth_enabled(void) {
46+
return true;
47+
}
48+
static inline void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
49+
}
50+
51+
#endif // PBSYS_CONFIG_STORAGE
52+
53+
#endif // _PBSYS_STORAGE_SETTINGS_H_
54+
55+
/** @} */

lib/pbio/sys/bluetooth.c

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <pbsys/storage.h>
2525

2626
#include "light.h"
27+
#include "storage.h"
2728

2829
// REVISIT: this can be the negotiated MTU - 3 to allow for better throughput
2930
#define MAX_CHAR_SIZE 20
@@ -46,7 +47,7 @@ static bool send_busy;
4647

4748
PROCESS(pbsys_bluetooth_process, "Bluetooth");
4849

49-
static void pbsys_bluetooth_process_poll(void) {
50+
void pbsys_bluetooth_process_poll(void) {
5051
process_poll(&pbsys_bluetooth_process);
5152
}
5253

@@ -203,45 +204,6 @@ bool pbsys_bluetooth_tx_is_idle(void) {
203204
return !send_busy && lwrb_get_full(&stdout_ring_buf) == 0;
204205
}
205206

206-
#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
207-
static pbsys_storage_settings_t *settings;
208-
209-
bool pbsys_bluetooth_is_user_enabled(void) {
210-
if (!settings) {
211-
return false;
212-
}
213-
return settings->bluetooth_ble_user_enabled;
214-
}
215-
216-
void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
217-
218-
// Ignore toggle request in all but idle system status.
219-
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
220-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_POWER_BUTTON_PRESSED)
221-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
222-
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)
223-
// Ignore toggle is Bluetooth is currently being used in a connection.
224-
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
225-
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)
226-
// Ignore if last request not yet finished processing.
227-
|| settings->bluetooth_ble_user_enabled != pbdrv_bluetooth_is_ready()
228-
) {
229-
return;
230-
}
231-
232-
// Toggle the user enabled state and poll process to take action.
233-
settings->bluetooth_ble_user_enabled = !settings->bluetooth_ble_user_enabled;
234-
pbsys_storage_request_write();
235-
pbsys_bluetooth_process_poll();
236-
}
237-
#else // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
238-
bool pbsys_bluetooth_is_user_enabled(void) {
239-
return true;
240-
}
241-
void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
242-
}
243-
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
244-
245207
// Contiki process
246208

247209
static pbio_pybricks_error_t handle_receive(pbdrv_bluetooth_connection_t connection, const uint8_t *data, uint32_t size) {
@@ -329,8 +291,9 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
329291

330292
PROCESS_BEGIN();
331293

294+
// Ensures Bluetooth preferences are loaded before we read them.
332295
#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
333-
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_get_settings(&settings) == PBIO_SUCCESS);
296+
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_get_settings() != NULL);
334297
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
335298

336299
pbdrv_bluetooth_set_on_event(pbsys_bluetooth_process_poll);
@@ -340,7 +303,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
340303

341304
// Show inactive status only if user requested Bluetooth as disabled to
342305
// avoid always flashing red in between program runs when disconnected.
343-
if (!pbsys_bluetooth_is_user_enabled()) {
306+
if (!pbsys_storage_settings_bluetooth_enabled()) {
344307
pbsys_status_light_bluetooth_set_color(PBIO_COLOR_RED);
345308
}
346309

@@ -349,7 +312,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
349312
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
350313

351314
// Wait until Bluetooth enabled requested by user, but stop waiting on shutdown.
352-
PROCESS_WAIT_UNTIL(pbsys_bluetooth_is_user_enabled() || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
315+
PROCESS_WAIT_UNTIL(pbsys_storage_settings_bluetooth_enabled() || pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN));
353316
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
354317
break;
355318
}
@@ -370,7 +333,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
370333
pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
371334
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING)
372335
|| pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)
373-
|| !pbsys_bluetooth_is_user_enabled());
336+
|| !pbsys_storage_settings_bluetooth_enabled());
374337

375338
// Now change the state depending on which of the above was triggered.
376339

@@ -389,7 +352,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
389352
// The Bluetooth enabled flag can only change while disconnected and
390353
// while no program is running. So here it just serves to skip the
391354
// Bluetooth loop below and go directly to the disable step below it.
392-
while (pbsys_bluetooth_is_user_enabled()
355+
while (pbsys_storage_settings_bluetooth_enabled()
393356
&& pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
394357
&& !pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) {
395358

lib/pbio/sys/bluetooth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
uint32_t pbsys_bluetooth_rx_get_free(void);
1010
void pbsys_bluetooth_rx_write(const uint8_t *data, uint32_t size);
11+
void pbsys_bluetooth_process_poll(void);
1112

1213
#endif // _PBSYS_SYS_BLUETOOTH_H_

lib/pbio/sys/command.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <pbdrv/reset.h>
88
#include <pbio/protocol.h>
99

10+
#include <pbsys/storage.h>
11+
1012
#include "./bluetooth.h"
1113
#include "./storage.h"
1214
#include "./program_stop.h"

lib/pbio/sys/hmi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include <pbio/color.h>
2020
#include <pbio/event.h>
2121
#include <pbio/light.h>
22-
#include <pbsys/bluetooth.h>
2322
#include <pbsys/config.h>
2423
#include <pbsys/status.h>
24+
#include <pbsys/storage_settings.h>
2525

2626
#include "light_matrix.h"
2727
#include "light.h"
@@ -81,7 +81,7 @@ static PT_THREAD(update_bluetooth_button_wait_state(bool button_pressed)) {
8181
// button may still be pressed during user program
8282
PT_WAIT_UNTIL(pt, !button_pressed);
8383
PT_WAIT_UNTIL(pt, button_pressed);
84-
pbsys_bluetooth_is_user_enabled_request_toggle();
84+
pbsys_storage_settings_bluetooth_enabled_request_toggle();
8585
}
8686

8787
PT_END(pt);

lib/pbio/sys/storage.c

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ static pbsys_storage_data_map_t *map = &pbsys_user_ram_data_map.data_map;
3333

3434
static bool data_map_is_loaded = false;
3535

36+
pbsys_storage_settings_t *pbsys_storage_get_settings(void) {
37+
if (!data_map_is_loaded) {
38+
return NULL;
39+
}
40+
return &map->settings;
41+
}
42+
3643
/**
3744
* Requests that storage (program, user data, settings) will be saved (some
3845
* time before shutdown). Should be called by functions that change data.
@@ -82,30 +89,6 @@ pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32
8289
return PBIO_SUCCESS;
8390
}
8491

85-
/**
86-
* Gets the stored system settings.
87-
*
88-
* @param [out] settings The settings.
89-
* @returns ::PBIO_ERROR_AGAIN if settings still being loaded.
90-
* Otherwise, ::PBIO_SUCCESS.
91-
*/
92-
pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings) {
93-
if (!data_map_is_loaded) {
94-
return PBIO_ERROR_AGAIN;
95-
}
96-
97-
*settings = &map->settings;
98-
return PBIO_SUCCESS;
99-
}
100-
101-
/**
102-
* Sets the default settings after an erase.
103-
*/
104-
static void pbsys_storage_set_default_settings(void) {
105-
map->settings.bluetooth_ble_user_enabled = true;
106-
pbsys_storage_request_write();
107-
}
108-
10992
#if PBSYS_CONFIG_STORAGE_OVERLAPS_BOOTLOADER_CHECKSUM
11093
// Updates checksum in data map to satisfy bootloader requirements.
11194
static void pbsys_storage_update_checksum(void) {
@@ -261,13 +244,13 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
261244
// Reset storage except for program data. It is sufficient to set its
262245
// size to 0, which is what happens here since it is in the map.
263246
memset(map, 0, sizeof(pbsys_storage_data_map_t));
247+
pbsys_storage_set_default_settings(&map->settings);
264248

265249
// Set firmware version used to create current storage map.
266250
map->stored_firmware_version = PBIO_HEXVERSION;
267251

268-
// Set defaults user settings. This also raises the write flag to save
269-
// on shutdown.
270-
pbsys_storage_set_default_settings();
252+
// Ensure new firmware version and default settings are written.
253+
pbsys_storage_request_write();
271254
}
272255

273256
// Poke processes that await on system settings to become available.

lib/pbio/sys/storage.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <pbio/error.h>
1010
#include <pbsys/config.h>
1111
#include <pbsys/main.h>
12+
#include <pbsys/storage_settings.h>
1213

1314
#if PBSYS_CONFIG_STORAGE
1415

@@ -18,12 +19,17 @@ pbio_error_t pbsys_storage_set_program_size(uint32_t size);
1819
pbio_error_t pbsys_storage_set_program_data(uint32_t offset, const void *data, uint32_t size);
1920
pbio_error_t pbsys_storage_assert_program_valid(void);
2021
void pbsys_storage_get_program_data(pbsys_main_program_t *program);
22+
pbsys_storage_settings_t *pbsys_storage_get_settings(void);
23+
void pbsys_storage_request_write(void);
2124

2225
#else
2326
static inline void pbsys_storage_init(void) {
2427
}
2528
static inline void pbsys_storage_deinit(void) {
2629
}
30+
static inline pbsys_storage_settings_t *pbsys_storage_get_settings(void) {
31+
return NULL;
32+
}
2733
static inline pbio_error_t pbsys_storage_set_program_size(uint32_t size) {
2834
return PBIO_ERROR_NOT_SUPPORTED;
2935
}
@@ -35,6 +41,8 @@ static inline pbio_error_t pbsys_storage_assert_program_valid(void) {
3541
}
3642
static inline void pbsys_storage_get_program_data(pbsys_main_program_t *program) {
3743
}
44+
static inline void pbsys_storage_request_write(void) {
45+
}
3846

3947
#endif // PBSYS_CONFIG_STORAGE
4048

0 commit comments

Comments
 (0)