Skip to content

Commit

Permalink
pbio/sys/storage_settings: Use bit flags instead of bit fields.
Browse files Browse the repository at this point in the history
Bit fields are not guaranteed to have the same order if we write them to disk and read them back with another firmware build.
  • Loading branch information
laurensvalk committed Jun 7, 2024
1 parent 7887e97 commit 31c36c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions lib/pbio/include/pbsys/storage_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
#include <pbsys/config.h>

/**
* System settings. All data types are little-endian.
* System setting flags
*/
typedef struct _pbsys_storage_settings_t {
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
typedef enum {
/**
* User has enabled Bluetooth Low Energy.
* Bluetooth is enabled by the user (defaults to true).
*/
bool bluetooth_ble_user_enabled : 1;
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED = (1 << 0),
} pbsys_storage_settings_flags_t;

/**
* System settings. All data types are little-endian.
*/
typedef struct _pbsys_storage_settings_t {
uint32_t flags;
} pbsys_storage_settings_t;

#if PBSYS_CONFIG_STORAGE
Expand Down
8 changes: 4 additions & 4 deletions lib/pbio/sys/storage_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings) {
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
settings->bluetooth_ble_user_enabled = true;
settings->flags |= PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
#endif
}

Expand All @@ -35,7 +35,7 @@ bool pbsys_storage_settings_bluetooth_enabled(void) {
if (!settings) {
return true;
}
return settings->bluetooth_ble_user_enabled;
return settings->flags & PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
#else
return true;
#endif // PBSYS_CONFIG_BLUETOOTH_TOGGLE
Expand All @@ -55,13 +55,13 @@ void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)
|| pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)
// Ignore if last request not yet finished processing.
|| settings->bluetooth_ble_user_enabled != pbdrv_bluetooth_is_ready()
|| pbsys_storage_settings_bluetooth_enabled() != pbdrv_bluetooth_is_ready()
) {
return;
}

// Toggle the user enabled state and poll process to take action.
settings->bluetooth_ble_user_enabled = !settings->bluetooth_ble_user_enabled;
settings->flags ^= PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
pbsys_storage_request_write();
pbsys_bluetooth_process_poll();
#endif
Expand Down

0 comments on commit 31c36c6

Please sign in to comment.