Skip to content

Commit

Permalink
Adding semver to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Jan 8, 2021
1 parent 0a1c340 commit 9d70cb1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/settings/channel_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
//! Unauthorized usage, editing, or copying is strictly prohibited.
//! Proprietary and confidential.

use super::{SinaraBoardId, SinaraConfiguration};
use super::{SemVersion, SinaraBoardId, SinaraConfiguration};
use crate::{linear_transformation::LinearTransformation, Error, I2cProxy};
use microchip_24aa02e48::Microchip24AA02E48;

/// The expected semver of the BoosterChannelSettings. This version must be updated whenever the
/// `BoosterChannelData` layout is updated.
const EXPECTED_VERSION: SemVersion = SemVersion {
major: 1,
minor: 0,
patch: 1,
};

/// Represents booster channel-specific configuration values.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct BoosterChannelData {
version: SemVersion,
pub output_interlock_threshold: f32,
pub bias_voltage: f32,
pub enabled: bool,
Expand All @@ -24,6 +33,7 @@ impl BoosterChannelData {
/// Generate default booster channel data.
pub fn default() -> Self {
Self {
version: EXPECTED_VERSION,
output_interlock_threshold: 0.0,
bias_voltage: -3.2,
enabled: false,
Expand Down Expand Up @@ -63,6 +73,11 @@ impl BoosterChannelData {
return Err(Error::Invalid);
}

// Validate the CRC of the settings.
if config.version != EXPECTED_VERSION {
return Err(Error::Invalid);
}

Ok(config)
}

Expand Down
16 changes: 15 additions & 1 deletion src/settings/global_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ use heapless::{consts, String};
use serde::{Deserialize, Serialize};
use w5500::{Ipv4Addr, MacAddress};

use super::{SinaraBoardId, SinaraConfiguration};
use super::{SemVersion, SinaraBoardId, SinaraConfiguration};

use core::fmt::Write;

/// The expected semver of the BoosterChannelSettings. This version must be updated whenever the
/// `BoosterMainBoardData` layout is updated.
const EXPECTED_VERSION: SemVersion = SemVersion {
major: 1,
minor: 0,
patch: 1,
};

fn array_to_addr(addr: &[u8; 4]) -> Ipv4Addr {
Ipv4Addr::new(addr[0], addr[1], addr[2], addr[3])
}
Expand All @@ -24,6 +32,7 @@ fn identifier_is_valid<'a>(id: &'a str) -> bool {
/// Represents booster mainboard-specific configuration values.
#[derive(Serialize, Deserialize)]
struct BoosterMainBoardData {
version: SemVersion,
ip_address: [u8; 4],
broker_address: [u8; 4],
gateway_address: [u8; 4],
Expand All @@ -50,6 +59,7 @@ impl BoosterMainBoardData {
id[..name.len()].copy_from_slice(name.as_str().as_bytes());

Self {
version: EXPECTED_VERSION,
ip_address: [10, 0, 0, 1],
broker_address: [10, 0, 0, 2],
gateway_address: [10, 0, 0, 0],
Expand All @@ -70,6 +80,10 @@ impl BoosterMainBoardData {
pub fn deserialize(data: &[u8; 64]) -> Result<Self, Error> {
let config: BoosterMainBoardData = postcard::from_bytes(data).unwrap();

if config.version != EXPECTED_VERSION {
return Err(Error::Invalid);
}

// Validate configuration parameters.
let identifier = core::str::from_utf8(config.id()).map_err(|_| Error::Invalid)?;
if identifier_is_valid(identifier) == false {
Expand Down
7 changes: 7 additions & 0 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ use sinara::{BoardId as SinaraBoardId, SinaraConfiguration};

pub use channel_settings::BoosterChannelSettings;
pub use global_settings::BoosterSettings;

#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
pub struct SemVersion {
major: u8,
minor: u8,
patch: u8,
}
13 changes: 13 additions & 0 deletions src/settings/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Booster NGFW NVM settings
//!
//! # Copyright
//! Copyright (C) 2020 QUARTIQ GmbH - All Rights Reserved
//! Unauthorized usage, editing, or copying is strictly prohibited.
//! Proprietary and confidential.

#[derive(serde::Serialize, serde::Deserialize, PartialEq)]
pub struct SemVersion {
major: u8,
minor: u8,
patch: u8,
}

0 comments on commit 9d70cb1

Please sign in to comment.