@@ -34,10 +34,14 @@ static pbsys_storage_data_map_t *map = &pbsys_user_ram_data_map.data_map;
3434static 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.
107111static 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