|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use schemars::JsonSchema; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +use crate::component::SpIdentifier; |
| 9 | + |
| 10 | +#[derive( |
| 11 | + Debug, |
| 12 | + Clone, |
| 13 | + PartialEq, |
| 14 | + Eq, |
| 15 | + PartialOrd, |
| 16 | + Ord, |
| 17 | + Deserialize, |
| 18 | + Serialize, |
| 19 | + JsonSchema, |
| 20 | +)] |
| 21 | +pub struct SpIgnitionInfo { |
| 22 | + pub id: SpIdentifier, |
| 23 | + pub details: SpIgnition, |
| 24 | +} |
| 25 | + |
| 26 | +impl From<SpIgnitionInfo> for crate::ignition::v1::SpIgnitionInfo { |
| 27 | + fn from(s: SpIgnitionInfo) -> Self { |
| 28 | + Self { id: s.id, details: s.details.into() } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// State of an ignition target. |
| 33 | +// |
| 34 | +// TODO: Ignition returns much more information than we're reporting here: do |
| 35 | +// we want to expand this? |
| 36 | +#[derive( |
| 37 | + Debug, |
| 38 | + Clone, |
| 39 | + PartialEq, |
| 40 | + Eq, |
| 41 | + PartialOrd, |
| 42 | + Ord, |
| 43 | + Deserialize, |
| 44 | + Serialize, |
| 45 | + JsonSchema, |
| 46 | +)] |
| 47 | +#[serde(tag = "present")] |
| 48 | +pub enum SpIgnition { |
| 49 | + #[serde(rename = "no")] |
| 50 | + Absent, |
| 51 | + #[serde(rename = "yes")] |
| 52 | + Present { |
| 53 | + id: SpIgnitionSystemType, |
| 54 | + power: bool, |
| 55 | + ctrl_detect_0: bool, |
| 56 | + ctrl_detect_1: bool, |
| 57 | + /// Fault from the A3 power domain |
| 58 | + flt_a3: bool, |
| 59 | + /// Fault from the A2 power domain |
| 60 | + flt_a2: bool, |
| 61 | + /// Fault from the RoT |
| 62 | + flt_rot: bool, |
| 63 | + /// Fault from the SP |
| 64 | + flt_sp: bool, |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +impl From<gateway_messages::IgnitionState> for SpIgnition { |
| 69 | + fn from(state: gateway_messages::IgnitionState) -> Self { |
| 70 | + use gateway_messages::ignition::SystemPowerState; |
| 71 | + |
| 72 | + if let Some(target_state) = state.target { |
| 73 | + Self::Present { |
| 74 | + id: target_state.system_type.into(), |
| 75 | + power: matches!( |
| 76 | + target_state.power_state, |
| 77 | + SystemPowerState::On | SystemPowerState::PoweringOn |
| 78 | + ), |
| 79 | + ctrl_detect_0: target_state.controller0_present, |
| 80 | + ctrl_detect_1: target_state.controller1_present, |
| 81 | + flt_a3: target_state.faults.power_a3, |
| 82 | + flt_a2: target_state.faults.power_a2, |
| 83 | + flt_rot: target_state.faults.rot, |
| 84 | + flt_sp: target_state.faults.sp, |
| 85 | + } |
| 86 | + } else { |
| 87 | + Self::Absent |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl From<SpIgnition> for crate::ignition::v1::SpIgnition { |
| 93 | + fn from(state: SpIgnition) -> Self { |
| 94 | + match state { |
| 95 | + SpIgnition::Absent => Self::Absent, |
| 96 | + SpIgnition::Present { |
| 97 | + id, |
| 98 | + power, |
| 99 | + ctrl_detect_0, |
| 100 | + ctrl_detect_1, |
| 101 | + flt_a3, |
| 102 | + flt_a2, |
| 103 | + flt_rot, |
| 104 | + flt_sp, |
| 105 | + } => Self::Present { |
| 106 | + id: id.into(), |
| 107 | + power, |
| 108 | + ctrl_detect_0, |
| 109 | + ctrl_detect_1, |
| 110 | + flt_a3, |
| 111 | + flt_a2, |
| 112 | + flt_rot, |
| 113 | + flt_sp, |
| 114 | + }, |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[derive( |
| 120 | + Debug, |
| 121 | + Clone, |
| 122 | + Copy, |
| 123 | + PartialEq, |
| 124 | + Eq, |
| 125 | + PartialOrd, |
| 126 | + Ord, |
| 127 | + Deserialize, |
| 128 | + Serialize, |
| 129 | + JsonSchema, |
| 130 | +)] |
| 131 | +#[serde(tag = "system_type", rename_all = "snake_case")] |
| 132 | +pub enum SpIgnitionSystemType { |
| 133 | + Gimlet, |
| 134 | + Sidecar, |
| 135 | + Psc, |
| 136 | + Unknown { id: u16 }, |
| 137 | + Cosmo, |
| 138 | +} |
| 139 | + |
| 140 | +impl From<gateway_messages::ignition::SystemType> for SpIgnitionSystemType { |
| 141 | + fn from(st: gateway_messages::ignition::SystemType) -> Self { |
| 142 | + use gateway_messages::ignition::SystemType; |
| 143 | + match st { |
| 144 | + SystemType::Gimlet => Self::Gimlet, |
| 145 | + SystemType::Sidecar => Self::Sidecar, |
| 146 | + SystemType::Psc => Self::Psc, |
| 147 | + SystemType::Unknown(id) => Self::Unknown { id }, |
| 148 | + SystemType::Cosmo => Self::Cosmo, |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl From<SpIgnitionSystemType> for crate::ignition::v1::SpIgnitionSystemType { |
| 154 | + fn from(st: SpIgnitionSystemType) -> Self { |
| 155 | + match st { |
| 156 | + SpIgnitionSystemType::Gimlet => Self::Gimlet, |
| 157 | + SpIgnitionSystemType::Sidecar => Self::Sidecar, |
| 158 | + SpIgnitionSystemType::Psc => Self::Psc, |
| 159 | + // Cosmo system id is 0x4 |
| 160 | + SpIgnitionSystemType::Cosmo => Self::Unknown { id: 0x4 }, |
| 161 | + SpIgnitionSystemType::Unknown { id } => Self::Unknown { id }, |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments