Storage
Persistent storage in Lokey is handled by the Storage trait, which provides an async API for storing, fetching, and removing typed entries in non-volatile memory (typically flash). A storage can handle typed entries that implement the Entry trait.
The concrete storage backend is created by the MCU, via the McuCreator trait. See the MCUs page for details.
See the storage module for the complete API reference.
Responsibilities
A Storage type defines the following functionality:
- Writing entries: Writing typed values via the
Entrytrait. - Reading entries: Reading typed values via the
Entrytrait. - Deleting entries: Deleting typed values via the
Entrytrait.
An Entry type defines how a typed value is stored in storage:
- Tag: A unique byte-array identifier for the entry type.
- Size: The fixed byte size of the entry's serialized form.
- Serialization: How to convert between the typed value and its raw byte representation.
Storage selection
In the Device implementation, type StorageConfig = ... selects which storage configuration to use, and fn storage_config() returns the configuration value. The device's MCU must implement McuCreator for that configuration type.
use lokey::storage::DefaultStorageConfig;
impl Device for MyDevice {
type Mcu = Nrf;
type StorageConfig = DefaultStorageConfig;
fn storage_config() -> Self::StorageConfig {
DefaultStorageConfig {
flash_page_range: 0xF0..0x100,
}
}
// ...
}Example Entry implementation
This example implements Entry for MyEntry as a singleton, meaning there can be no more than one instance of this entry type in storage. This is achieved by using () as the tag parameter and returning a fixed tag. If you wanted to store multiple boolean values, you could for example use u8 as the tag parameter and return a tag that incorporates the parameter value, allowing up to 256 distinct entries.
use generic_array::GenericArray;
use lokey::storage::{Entry, BASE_TAG_SIZE};
struct MyEntry(bool);
impl Entry for MyEntry {
type Size = typenum::U1;
type TagParam = ();
const BASE_TAG: [u8; BASE_TAG_SIZE] = [0x0, 0x0, 0x0, 0x1];
fn from_bytes(bytes: &GenericArray<u8, Self::Size>) -> Option<Self> {
Some(Self(bytes[0] != 0))
}
fn to_bytes(&self) -> GenericArray<u8, Self::Size> {
[self.0 as u8].into()
}
}
{
let mut write_transaction = storage.write_transaction().await;
// Store the entry
write_transaction.write((), MyEntry(true)).await?;
// Calling store again will overwrite the previous value, since the tag is the same
write_transaction.write((), MyEntry(false)).await?;
// Commit the changes
write_transaction.commit().await?;
}
{
let read_transaction = storage.read_transaction().await;
// Fetch the entry
let value = read_transaction.read::<MyEntry>(()).await?;
}
{
let mut write_transaction = storage.write_transaction().await;
// Remove the entry
write_transaction.delete::<MyEntry>(()).await?;
// Commit the changes
write_transaction.commit().await?;
}Provided implementations
The following storage implementations are provided:
lokey::storage::DefaultStorage– A flash-backed map storage implementation, configured withDefaultStorageConfiglokey::storage::EmptyStorage– A no-op storage useful for devices that do not require persistent storage, configured withEmptyStorageConfig
Crates that provide MCU support (for example lokey-nrf and lokey-rp) implement McuCreator for these configurations, so you can select them directly via type StorageConfig in your device.
Entry tags
Each entry must have a tag that is unique across all entry types. This table lists the already used entry tags. If you created a new entry type, please make sure it does not conflict with any of the listed entry types and add it to the table so there is no conflict between entry types of third-party crates.