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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions gateway-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ progenitor::generate_api!(
slog::debug!(log, "client response"; "result" => ?result);
}),
derives = [schemars::JsonSchema],
patch =
{ SpIdentifier = { derives = [Copy, PartialEq, Eq, PartialOrd, Ord] },
SpState = { derives = [ PartialEq, Eq, PartialOrd, Ord] }
}
patch = {
SpIdentifier = { derives = [Copy, PartialEq, Eq, PartialOrd, Ord] },
SpState = { derives = [ PartialEq, Eq, PartialOrd, Ord] },
RotState = { derives = [ PartialEq, Eq, PartialOrd, Ord] },
RotImageDetails = { derives = [ PartialEq, Eq, PartialOrd, Ord] },
RotSlot = { derives = [ PartialEq, Eq, PartialOrd, Ord] },
ImageVersion = { derives = [ PartialEq, Eq, PartialOrd, Ord] },
},
);
2 changes: 1 addition & 1 deletion gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::management_switch::SpIdentifier;
use dropshot::HttpError;
use gateway_messages::SpError;
use gateway_sp_comms::error::CommunicationError;
pub use gateway_sp_comms::error::CommunicationError;
use gateway_sp_comms::error::UpdateError;
use gateway_sp_comms::BindError;
use std::time::Duration;
Expand Down
86 changes: 84 additions & 2 deletions gateway/src/http_entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,78 @@ pub struct SpInfo {
pub enum SpState {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hell yes! This is stuff wicket craves!

Enabled {
serial_number: String,
// TODO more stuff
model: String,
revision: u32,
hubris_archive_id: String,
base_mac_address: [u8; 6],
version: ImageVersion,
power_state: PowerState,
rot: RotState,
},
CommunicationFailed {
message: String,
},
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Deserialize,
Serialize,
JsonSchema,
)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum RotState {
// TODO gateway_messages's RotState includes a couple nested structures that
// I've flattened here because they only contain one field each. When those
// structures grow we'll need to expand/change this.
Enabled {
active: RotSlot,
slot_a: Option<RotImageDetails>,
slot_b: Option<RotImageDetails>,
},
CommunicationFailed {
message: String,
},
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Deserialize,
Serialize,
JsonSchema,
)]
#[serde(tag = "slot", rename_all = "snake_case")]
pub enum RotSlot {
A,
B,
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Deserialize,
Serialize,
JsonSchema,
)]
pub struct RotImageDetails {
pub digest: String,
pub version: ImageVersion,
}

#[derive(
Debug,
Clone,
Expand Down Expand Up @@ -309,12 +374,29 @@ pub struct HostStartupOptions {
Deserialize,
JsonSchema,
)]
enum PowerState {
pub enum PowerState {
A0,
A1,
A2,
}

#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
Deserialize,
JsonSchema,
)]
pub struct ImageVersion {
pub epoch: u32,
pub version: u32,
}

/// Identifier for an SP's component's firmware slot; e.g., slots 0 and 1 for
/// the host boot flash.
#[derive(
Expand Down
70 changes: 62 additions & 8 deletions gateway/src/http_entrypoints/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

use super::HostStartupOptions;
use super::IgnitionCommand;
use super::ImageVersion;
use super::PowerState;
use super::RotImageDetails;
use super::RotSlot;
use super::RotState;
use super::SpComponentInfo;
use super::SpComponentList;
use super::SpComponentPresence;
Expand Down Expand Up @@ -94,28 +98,78 @@ impl From<PowerState> for gateway_messages::PowerState {
}
}

fn stringify_serial_number(serial: &[u8]) -> String {
// We expect serial numbers to be ASCII and 0-padded: find the first 0 byte
// and convert to a string. If that fails, hexlify the entire slice.
let first_zero =
serial.iter().position(|&b| b == 0).unwrap_or(serial.len());
impl From<gateway_messages::ImageVersion> for ImageVersion {
fn from(v: gateway_messages::ImageVersion) -> Self {
Self { epoch: v.epoch, version: v.version }
}
}

// We expect serial and model numbers to be ASCII and 0-padded: find the first 0
// byte and convert to a string. If that fails, hexlify the entire slice.
fn stringify_byte_string(bytes: &[u8]) -> String {
let first_zero = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());

str::from_utf8(&serial[..first_zero])
str::from_utf8(&bytes[..first_zero])
.map(|s| s.to_string())
.unwrap_or_else(|_err| hex::encode(serial))
.unwrap_or_else(|_err| hex::encode(bytes))
}

impl From<Result<gateway_messages::SpState, SpCommsError>> for SpState {
fn from(result: Result<gateway_messages::SpState, SpCommsError>) -> Self {
match result {
Ok(state) => Self::Enabled {
serial_number: stringify_serial_number(&state.serial_number),
serial_number: stringify_byte_string(&state.serial_number),
model: stringify_byte_string(&state.model),
revision: state.revision,
hubris_archive_id: hex::encode(&state.hubris_archive_id),
base_mac_address: state.base_mac_address,
version: ImageVersion::from(state.version),
power_state: PowerState::from(state.power_state),
rot: RotState::from(state.rot),
},
Err(err) => Self::CommunicationFailed { message: err.to_string() },
}
}
}

impl From<Result<gateway_messages::RotState, gateway_messages::RotError>>
for RotState
{
fn from(
result: Result<gateway_messages::RotState, gateway_messages::RotError>,
) -> Self {
match result {
Ok(state) => {
let boot_state = state.rot_updates.boot_state;
Self::Enabled {
active: boot_state.active.into(),
slot_a: boot_state.slot_a.map(Into::into),
slot_b: boot_state.slot_b.map(Into::into),
}
}
Err(err) => Self::CommunicationFailed { message: err.to_string() },
}
}
}

impl From<gateway_messages::RotSlot> for RotSlot {
fn from(slot: gateway_messages::RotSlot) -> Self {
match slot {
gateway_messages::RotSlot::A => Self::A,
gateway_messages::RotSlot::B => Self::B,
}
}
}

impl From<gateway_messages::RotImageDetails> for RotImageDetails {
fn from(details: gateway_messages::RotImageDetails) -> Self {
Self {
digest: hex::encode(&details.digest),
version: details.version.into(),
}
}
}

impl From<Result<gateway_messages::SpState, CommunicationError>> for SpState {
fn from(
result: Result<gateway_messages::SpState, CommunicationError>,
Expand Down
2 changes: 2 additions & 0 deletions gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub mod http_entrypoints; // TODO pub only for testing - is this right?

pub use config::Config;
pub use context::ServerContext;
pub use error::*;

use dropshot::ShutdownWaitFuture;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
Expand Down
2 changes: 1 addition & 1 deletion gateway/tests/integration_tests/location_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn discovery_both_locations() {

let resp: SpInfo = test_util::object_get(client, &url).await;
match resp.details {
SpState::Enabled { serial_number } => {
SpState::Enabled { serial_number, .. } => {
assert_eq!(serial_number, expected_serial)
}
other => panic!("unexpected state {:?}", other),
Expand Down
2 changes: 1 addition & 1 deletion gateway/tests/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn current_simulator_state(simrack: &SimRack) -> Vec<SpInfo> {

let details =
if matches!(target_state.power_state, SystemPowerState::On) {
SpState::Enabled { serial_number: sp.serial_number() }
sp.state().await
} else {
SpState::CommunicationFailed {
message: "powered off".to_string(),
Expand Down
Loading