config: add persistent flash-backed configuration with a HID command interface and CLI management
Build and Release Firmware / firmware (ak4490) (push) Successful in 50s
Build and Release Firmware / firmware (cs4398) (push) Successful in 44s
Build and Release Firmware / firmware (evk) (push) Successful in 43s
Build and Release Firmware / cli (aarch64-unknown-linux-musl) (push) Successful in 1m8s
Build and Release Firmware / cli (x86_64-pc-windows-gnu) (push) Successful in 1m32s
Build and Release Firmware / cli (x86_64-unknown-linux-musl) (push) Successful in 1m6s
Build and Release Firmware / release (push) Successful in 7s
Build and Release Firmware / firmware (ak4490) (push) Successful in 50s
Build and Release Firmware / firmware (cs4398) (push) Successful in 44s
Build and Release Firmware / firmware (evk) (push) Successful in 43s
Build and Release Firmware / cli (aarch64-unknown-linux-musl) (push) Successful in 1m8s
Build and Release Firmware / cli (x86_64-pc-windows-gnu) (push) Successful in 1m32s
Build and Release Firmware / cli (x86_64-unknown-linux-musl) (push) Successful in 1m6s
Build and Release Firmware / release (push) Successful in 7s
This commit is contained in:
23 files changed
+3483
-148
No files matched your search
@@ -0,0 +1,198 @@
|
||||
use wary::Validate;
|
||||
|
||||
use super::{
|
||||
ConfigError, ConfigKey, ConfigString, ConfigTag, ConfigV1, ConfigVersion, Tlv, TlvList, tags,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, wary::Wary)]
|
||||
pub struct DeviceConfig {
|
||||
#[validate(range(1..))]
|
||||
pub usb_vendor_id: u16,
|
||||
|
||||
#[validate(range(1..))]
|
||||
pub usb_product_id: u16,
|
||||
|
||||
#[validate(dive)]
|
||||
pub usb_manufacturer: ConfigString<32>,
|
||||
|
||||
#[validate(dive)]
|
||||
pub usb_product: ConfigString<64>,
|
||||
|
||||
#[validate(dive)]
|
||||
pub usb_serial: Option<ConfigString<32>>,
|
||||
}
|
||||
|
||||
impl DeviceConfig {
|
||||
pub fn from_tlvs(tlvs: &TlvList) -> Result<Self, ConfigError> {
|
||||
let config = Self {
|
||||
usb_vendor_id: resolve::<VendorId>(tlvs)?,
|
||||
usb_product_id: resolve::<ProductId>(tlvs)?,
|
||||
usb_manufacturer: resolve::<ManufString>(tlvs)?,
|
||||
usb_product: resolve::<ProductString>(tlvs)?,
|
||||
usb_serial: tlvs.get::<SerialString>()?,
|
||||
};
|
||||
|
||||
config
|
||||
.validate(&())
|
||||
.map_err(|report| ConfigError::Invalid {
|
||||
errors: report.len(),
|
||||
})?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn with_defaults(usb_serial: Option<ConfigString<32>>) -> Result<Self, ConfigError> {
|
||||
let mut config = Self::from_tlvs(&TlvList::new())?;
|
||||
config.usb_serial = usb_serial;
|
||||
|
||||
config
|
||||
.validate(&())
|
||||
.map_err(|report| ConfigError::Invalid {
|
||||
errors: report.len(),
|
||||
})?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Serial used for the default configuration, rendered as upper case hex.
|
||||
pub fn serial_from_uuid(uuid: &[u8; 16]) -> Option<ConfigString<32>> {
|
||||
const HEX: &[u8; 16] = b"0123456789ABCDEF";
|
||||
|
||||
let mut serial = [0u8; 32];
|
||||
|
||||
for (index, &byte) in uuid.iter().enumerate() {
|
||||
serial[index * 2] = HEX[usize::from(byte >> 4)];
|
||||
serial[index * 2 + 1] = HEX[usize::from(byte & 0x0f)];
|
||||
}
|
||||
|
||||
let serial = core::str::from_utf8(&serial).ok()?;
|
||||
ConfigString::try_from(serial).ok()
|
||||
}
|
||||
|
||||
pub fn replace_tlv(&mut self, tlv: Tlv) -> Result<(), ConfigError> {
|
||||
if tlv.tag.name().is_none() {
|
||||
return Err(ConfigError::UnknownTag(tlv.tag));
|
||||
}
|
||||
|
||||
let mut tlvs = self.to_tlvs()?;
|
||||
tlvs.upsert(tlv)?;
|
||||
*self = Self::from_tlvs(&tlvs)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn to_tlvs(&self) -> Result<TlvList, ConfigError> {
|
||||
let mut tlvs = TlvList::new();
|
||||
tlvs.set::<VendorId>(self.usb_vendor_id)?;
|
||||
tlvs.set::<ProductId>(self.usb_product_id)?;
|
||||
tlvs.set::<ManufString>(self.usb_manufacturer.clone())?;
|
||||
tlvs.set::<ProductString>(self.usb_product.clone())?;
|
||||
|
||||
if let Some(serial) = &self.usb_serial {
|
||||
tlvs.set::<SerialString>(serial.clone())?;
|
||||
}
|
||||
|
||||
Ok(tlvs)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&ConfigVersion> for DeviceConfig {
|
||||
type Error = ConfigError;
|
||||
|
||||
fn try_from(value: &ConfigVersion) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
ConfigVersion::V1(config) => Self::try_from(config),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&ConfigV1> for DeviceConfig {
|
||||
type Error = ConfigError;
|
||||
|
||||
fn try_from(value: &ConfigV1) -> Result<Self, Self::Error> {
|
||||
Self::from_tlvs(&value.tlvs)
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve<K: ConfigKey>(tlvs: &TlvList) -> Result<K::Value, ConfigError> {
|
||||
tlvs.get::<K>()?
|
||||
.or_else(K::default_value)
|
||||
.ok_or(ConfigError::Missing(K::TAG))
|
||||
}
|
||||
|
||||
pub(crate) struct VendorId;
|
||||
impl ConfigKey for VendorId {
|
||||
const TAG: ConfigTag = tags::VENDOR_ID;
|
||||
type Value = u16;
|
||||
|
||||
fn default_value() -> Option<Self::Value> {
|
||||
Some(0x1209)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ProductId;
|
||||
impl ConfigKey for ProductId {
|
||||
const TAG: ConfigTag = tags::PRODUCT_ID;
|
||||
type Value = u16;
|
||||
|
||||
fn default_value() -> Option<Self::Value> {
|
||||
Some(0xcc1d)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ManufString;
|
||||
impl ConfigKey for ManufString {
|
||||
const TAG: ConfigTag = tags::MANUF_STRING;
|
||||
type Value = ConfigString<32>;
|
||||
|
||||
fn default_value() -> Option<Self::Value> {
|
||||
Some(ConfigString::try_from("VE7XEN").unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ProductString;
|
||||
impl ConfigKey for ProductString {
|
||||
const TAG: ConfigTag = tags::PRODUCT_STRING;
|
||||
type Value = ConfigString<64>;
|
||||
|
||||
fn default_value() -> Option<Self::Value> {
|
||||
Some(ConfigString::try_from("Guac Tortilla").unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct SerialString;
|
||||
impl ConfigKey for SerialString {
|
||||
const TAG: ConfigTag = tags::SERIAL_STRING;
|
||||
type Value = ConfigString<32>;
|
||||
|
||||
fn default_value() -> Option<Self::Value> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn serial_from_uuid_is_uppercase_hex() {
|
||||
let uuid = [
|
||||
0x35, 0x60, 0x14, 0xf7, 0x7b, 0x65, 0xb4, 0x5d, 0x9d, 0xbf, 0x81, 0x11, 0xc5, 0xc4,
|
||||
0x29, 0xa5,
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
DeviceConfig::serial_from_uuid(&uuid).unwrap().as_str(),
|
||||
"356014F77B65B45D9DBF8111C5C429A5"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_are_valid() {
|
||||
let config = DeviceConfig::with_defaults(None).unwrap();
|
||||
|
||||
assert_eq!(config.usb_vendor_id, 0x1209);
|
||||
assert_eq!(config.usb_product_id, 0xcc1d);
|
||||
assert!(config.usb_serial.is_none());
|
||||
assert_eq!(config.to_tlvs().unwrap().len(), 4);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user