Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions src/main/application/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,53 @@

/* --------------------------------------------------- CONSTANTS ---------------------------------------------------- */

/**
* @def CONFIG_VERSION_CURRENT
* @brief The currently active configuration version.
*/
#define CONFIG_VERSION_CURRENT ( 1 )

/**
* @def MINIMUM_SAVE_PERIOD
* @brief Minimum elapsed time between saving config to storage.
*/
#define MINIMUM_SAVE_PERIOD ( 5 * TICKS_PER_SEC )

/* ----------------------------------------------------- TYPES ------------------------------------------------------ */

/**
* @typedef config_version_t
* @brief Enumeration of the supported configuration versions.
* @note This is to support backwards compatibility with saved configuration when the software changes.
*/
typedef uint8_t config_version_t;
enum
{
CONFIG_VERSION_0, /**< Configuration version 0. */

CONFIG_VERSION_COUNT, /**< Number of valid configuration versions.*/

CONFIG_VERSION_CURRENT /**< Current configuration version. */
= CONFIG_VERSION_0
};

/**
* @typedef config_storage_t
* @brief Struct defining data saved in storage for unit configuration.
*/
typedef struct
{
config_version_t version; /**< Saved version number. */
union
{
config_t current; /**< Configuration data as current value. */
};

} config_storage_t;

/* ----------------------------------------------------- MACROS ----------------------------------------------------- */

_Static_assert( _CONFIG_DFLT_BUZZER_FREQUENCY >= BUZZER_MINIMUM_FREQUENCY &&
_CONFIG_DFLT_BUZZER_FREQUENCY <= BUZZER_MAXIMUM_FREQUENCY,
"Invalid default buzzer frequency!" );

_Static_assert( STORAGE_CONFIG_SIZE >= sizeof( config_storage_t ),
"Not enough storage allocated for configuration!" );

/* --------------------------------------------------- VARIABLES ---------------------------------------------------- */

static config_t s_config; /**< Currently active app configuration. */
Expand Down Expand Up @@ -129,13 +158,17 @@ void config_init( void )
{
// Try to get configuration from storage, and restore defaults if that fails.
// In this case, any existing configuration will be lost the next time config is saved.
config_t config;
if( ! storage_get_config( CONFIG_VERSION_CURRENT, sizeof( config_t ), & config ) ||
! validate_config( & config ) )
config_default( & config );
// In the future, this could do migration from previous configuration versions.
config_storage_t storage;
if( ! storage_get_config( & storage, sizeof( storage ) ) ||
storage.version != CONFIG_VERSION_CURRENT ||
! validate_config( & storage.current ) )
{
config_default( & storage.current );
}

// Set local copy
s_config = config;
s_config = storage.current;

} /* config_init() */

Expand Down Expand Up @@ -170,7 +203,11 @@ void config_tick( tick_t tick )

static void flush( tick_t tick )
{
storage_set_config( CONFIG_VERSION_CURRENT, sizeof( config_t ), & s_config );
config_storage_t storage;
storage.version = CONFIG_VERSION_CURRENT;
storage.current = s_config;

storage_set_config( & storage, sizeof( storage ) );
s_modified = false;
s_save_tick = tick;

Expand Down
3 changes: 2 additions & 1 deletion src/main/application/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "application/keyer.h"
#include "application/led.h"
#include "application/wpm.h"
#include "utility/utility.h"

/* ----------------------------------------------------- TYPES ------------------------------------------------------ */

Expand Down Expand Up @@ -60,7 +61,7 @@ typedef struct
/** If set to `true`, the keyer will emit dashes from the left paddle and dots from the right paddle. */
bool keyer_paddle_invert;

} __attribute__((packed)) config_t;
} PACKED_STRUCT config_t;

/**
* @typedef config_version_t
Expand Down
4 changes: 3 additions & 1 deletion src/main/application/intf_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <stdint.h>

#include "utility/utility.h"

/* --------------------------------------------------- CONSTANTS ---------------------------------------------------- */

/**
Expand Down Expand Up @@ -91,6 +93,6 @@ typedef struct
uint16_t size; /**< Total size of message payload. */
uint16_t crc; /**< 16-bit CRC of message payload. */

} __attribute__((packed)) intf_header_t;
} PACKED_STRUCT intf_header_t;

#endif /* !defined( APPLICATION_INTF_TYPES_H ) */
Loading