Skip to content

Commit d1b66d1

Browse files
committed
pbio/sys/storage: Set default settings on update.
1 parent c041cae commit d1b66d1

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

lib/pbio/include/pbsys/storage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32
8484

8585
pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings);
8686

87-
void pbsys_storage_request_settings_write(void);
87+
void pbsys_storage_request_write(void);
8888

8989
#else
9090

@@ -104,7 +104,7 @@ static inline pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t *
104104
return PBIO_ERROR_NOT_SUPPORTED;
105105
}
106106

107-
static inline void pbsys_storage_request_settings_write(void) {
107+
static inline void pbsys_storage_request_write(void) {
108108
}
109109

110110
#endif // PBSYS_CONFIG_STORAGE

lib/pbio/sys/bluetooth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void pbsys_bluetooth_is_user_enabled_request_toggle(void) {
230230

231231
// Toggle the user enabled state and poll process to take action.
232232
settings->bluetooth_ble_user_enabled = !settings->bluetooth_ble_user_enabled;
233-
pbsys_storage_request_settings_write();
233+
pbsys_storage_request_write();
234234
pbsys_bluetooth_process_poll();
235235
}
236236

lib/pbio/sys/storage.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ static pbsys_storage_data_map_t *map = &pbsys_user_ram_data_map.data_map;
3434
static bool data_map_is_loaded = false;
3535

3636
/**
37-
* Sets write size to how much data must be written on shutdown. This is not
38-
* simply a boolean flag because it is also used as the load size on boot.
37+
* Requests that storage (program, user data, settings) will be saved (some
38+
* time before shutdown). Should be called by functions that change data.
39+
*
40+
* This is done by setting the write size to how much data must be written on
41+
* shutdown. This is not simply a boolean flag because it is also used as the
42+
* load size on boot.
3943
*/
40-
static void update_write_size(void) {
44+
void pbsys_storage_request_write(void) {
4145
map->write_size = sizeof(pbsys_storage_data_map_t) + map->program_size;
4246
}
4347

@@ -56,7 +60,7 @@ pbio_error_t pbsys_storage_set_user_data(uint32_t offset, const uint8_t *data, u
5660
}
5761
// Update data and write size to request write on poweroff.
5862
memcpy(map->user_data + offset, data, size);
59-
update_write_size();
63+
pbsys_storage_request_write();
6064
return PBIO_SUCCESS;
6165
}
6266

@@ -78,14 +82,6 @@ pbio_error_t pbsys_storage_get_user_data(uint32_t offset, uint8_t **data, uint32
7882
return PBIO_SUCCESS;
7983
}
8084

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-
8985
/**
9086
* Gets the stored system settings.
9187
*
@@ -102,6 +98,14 @@ pbio_error_t pbsys_storage_get_settings(pbsys_storage_settings_t **settings) {
10298
return PBIO_SUCCESS;
10399
}
104100

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+
105109
#if PBSYS_CONFIG_STORAGE_OVERLAPS_BOOTLOADER_CHECKSUM
106110
// Updates checksum in data map to satisfy bootloader requirements.
107111
static void pbsys_storage_update_checksum(void) {
@@ -151,14 +155,16 @@ pbio_error_t pbsys_storage_set_program_size(uint32_t size) {
151155
// Update program size.
152156
map->program_size = size;
153157

154-
// Program size was updated, so set the write size.
155-
update_write_size();
158+
// Program size was updated, so request write.
159+
pbsys_storage_request_write();
156160

157161
return PBIO_SUCCESS;
158162
}
159163

160164
/**
161-
* Writes data to user RAM.
165+
* Writes program data to user RAM.
166+
*
167+
* Should be combined with at least one call to ::pbsys_storage_set_program_size.
162168
*
163169
* @param [in] offset The offset in bytes from the base user RAM address.
164170
* @param [in] data The data to write.
@@ -256,10 +262,12 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
256262
// size to 0, which is what happens here since it is in the map.
257263
memset(map, 0, sizeof(pbsys_storage_data_map_t));
258264

259-
// Make sure the new version will be written on shutdown, even if no
260-
// new program is uploaded.
265+
// Set firmware version used to create current storage map.
261266
map->stored_firmware_version = PBIO_HEXVERSION;
262-
update_write_size();
267+
268+
// Set defaults user settings. This also raises the write flag to save
269+
// on shutdown.
270+
pbsys_storage_set_default_settings();
263271
}
264272

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

0 commit comments

Comments
 (0)