Skip to content

Commit e85ae13

Browse files
committed
wip
1 parent 2b2b991 commit e85ae13

File tree

5 files changed

+86
-55
lines changed

5 files changed

+86
-55
lines changed

app/gimlet/base.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ task-slots = [
287287
"packrat",
288288
"user_leds",
289289
"vpd",
290+
"i2c_driver",
290291
]
291292
features = [
292293
"gimlet",

task/control-plane-agent/src/inventory.rs

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
use drv_i2c_devices::at24csw080::At24Csw080;
56
use gateway_messages::measurement::{
67
Measurement, MeasurementError, MeasurementKind,
78
};
@@ -18,54 +19,56 @@ use userlib::UnwrapLite;
1819

1920
userlib::task_slot!(VALIDATE, validate);
2021
userlib::task_slot!(SENSOR, sensor);
21-
userlib::task_slot!(I2C, i2c);
22+
userlib::task_slot!(I2C, i2c_driver);
2223

2324
pub(crate) struct Inventory {
2425
validate_task: Validate,
2526
sensor_task: SensorTask,
27+
fruid_buf: &'static mut [u8],
2628
}
2729

28-
pub(crate) struct FixedStr {
29-
buf: [u8; 32],
30-
len: usize,
31-
}
32-
33-
impl FixedStr {
34-
pub fn copy_from_slice(slice: &[u8]) -> Result<Self, core::str::Utf8Error> {
35-
core::str::from_utf8(slice)?;
36-
let mut buf = [0u8; 32];
37-
buf[..slice.len()].copy_from_slice(slice);
38-
Ok(Self {
39-
buf,
40-
len: slice.len(),
41-
})
42-
}
43-
44-
pub fn from_buf(
45-
bytes: [u8; 32],
46-
len: usize,
47-
) -> Result<Self, core::str::Utf8Error> {
48-
core::str::from_utf8(&bytes[..len])?;
49-
Ok(Self { buf: bytes, len })
50-
}
51-
}
52-
53-
impl AsRef<str> for FixedStr {
54-
fn as_ref(&self) -> &str {
55-
unsafe {
56-
// SAFETY: This is checked when constructing the FixedStr.
57-
core::str::from_utf8_unchecked(&self.buf[..self.len])
58-
}
59-
}
60-
}
30+
// pub(crate) struct FixedStr {
31+
// buf: [u8; 32],
32+
// len: usize,
33+
// }
34+
35+
// impl FixedStr {
36+
// pub fn copy_from_slice(slice: &[u8]) -> Result<Self, core::str::Utf8Error> {
37+
// core::str::from_utf8(slice)?;
38+
// let mut buf = [0u8; 32];
39+
// buf[..slice.len()].copy_from_slice(slice);
40+
// Ok(Self {
41+
// buf,
42+
// len: slice.len(),
43+
// })
44+
// }
45+
46+
// pub fn from_buf(
47+
// bytes: [u8; 32],
48+
// len: usize,
49+
// ) -> Result<Self, core::str::Utf8Error> {
50+
// core::str::from_utf8(&bytes[..len])?;
51+
// Ok(Self { buf: bytes, len })
52+
// }
53+
// }
54+
55+
// impl AsRef<str> for FixedStr {
56+
// fn as_ref(&self) -> &str {
57+
// unsafe {
58+
// // SAFETY: This is checked when constructing the FixedStr.
59+
// core::str::from_utf8_unchecked(&self.buf[..self.len])
60+
// }
61+
// }
62+
// }
6163

6264
impl Inventory {
63-
pub(crate) fn new() -> Self {
65+
pub(crate) fn new(fruid_buf: &'static mut [u8]) -> Self {
6466
let () = devices_with_static_validation::ASSERT_EACH_DEVICE_FITS_IN_ONE_PACKET;
6567

6668
Self {
6769
validate_task: Validate::from(VALIDATE.get_task_id()),
6870
sensor_task: SensorTask::from(SENSOR.get_task_id()),
71+
fruid_buf,
6972
}
7073
}
7174

@@ -93,11 +96,11 @@ impl Inventory {
9396
}
9497
}
9598

96-
pub(crate) fn component_details(
97-
&self,
99+
pub(crate) fn component_details<'buf>(
100+
&'buf mut self,
98101
component: &SpComponent,
99102
component_index: BoundsChecked,
100-
) -> ComponentDetails<FixedStr> {
103+
) -> ComponentDetails<&'buf str> {
101104
// `component_index` is guaranteed to be in the range
102105
// `0..num_component_details(component)`, and we only return a value
103106
// greater than 0 from that method for indices in the VALIDATE_DEVICES
@@ -108,20 +111,38 @@ impl Inventory {
108111
Ok(Index::ValidateDevice(i)) => i,
109112
Ok(Index::OurDevice(_)) | Err(_) => panic!(),
110113
};
114+
let device = &VALIDATE_DEVICES[val_device_index];
115+
// First, measurement channels...
116+
if let Some(sensor_description) =
117+
device.sensors.get(component_index.0 as usize)
118+
{
119+
let value = self
120+
.sensor_task
121+
.get(sensor_description.id)
122+
.map_err(|err| SensorErrorConvert(err).into());
123+
124+
return ComponentDetails::Measurement(Measurement {
125+
name: sensor_description.name.unwrap_or(""),
126+
kind: MeasurementKindConvert(sensor_description.kind).into(),
127+
value,
128+
});
129+
}
111130

112-
let sensor_description = &VALIDATE_DEVICES[val_device_index].sensors
113-
[component_index.0 as usize];
114-
115-
let value = self
116-
.sensor_task
117-
.get(sensor_description.id)
118-
.map_err(|err| SensorErrorConvert(err).into());
131+
// If the index is greater than the maximum number of measurement
132+
// channels, it must be a FRUID.
133+
let Some(fruid) = device.fruid else {
134+
// Index is bounds-checked.
135+
unreachable!()
136+
};
119137

120-
ComponentDetails::Measurement(Measurement {
121-
name: sensor_description.name.unwrap_or(""),
122-
kind: MeasurementKindConvert(sensor_description.kind).into(),
123-
value,
124-
})
138+
match fruid {
139+
FruidMode::At24Csw080Barcode(f) => {
140+
let dev = f(I2C.get_task_id());
141+
todo!()
142+
}
143+
FruidMode::At24Csw080Nested(_) => todo!(),
144+
FruidMode::Tmp117(_) => todo!(),
145+
}
125146
}
126147

127148
pub(crate) fn device_description(

task/control-plane-agent/src/mgs_common.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,30 @@ pub(crate) struct MgsCommon {
5757
dump_state: DumpState,
5858

5959
reset_component_requested: Option<SpComponent>,
60-
inventory: Inventory,
60+
pub inventory: Inventory,
6161
base_mac_address: MacAddress,
6262
packrat: Packrat,
6363
sprot: SpRot,
6464
update_sp: Update,
6565
sensor: Sensor,
6666
}
6767

68+
const FRUID_BUF_SIZE: usize = 256;
69+
6870
impl MgsCommon {
6971
pub(crate) fn claim_static_resources(base_mac_address: MacAddress) -> Self {
72+
use static_cell::ClaimOnceCell;
73+
let fruid_buf = {
74+
static BUF: ClaimOnceCell<[u8; FRUID_BUF_SIZE]> =
75+
ClaimOnceCell::new([0; FRUID_BUF_SIZE]);
76+
BUF.claim()
77+
};
7078
Self {
7179
sp_update: SpUpdate::new(),
7280
rot_update: RotUpdate::new(),
7381
dump_state: DumpState::new(),
74-
7582
reset_component_requested: None,
76-
inventory: Inventory::new(),
83+
inventory: Inventory::new(fruid_buf),
7784
base_mac_address,
7885
packrat: Packrat::from(PACKRAT.get_task_id()),
7986
sprot: SpRot::from(SPROT.get_task_id()),

task/control-plane-agent/src/mgs_compute_sled.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ impl SpHandler for MgsHandler {
913913
&mut self,
914914
component: SpComponent,
915915
index: BoundsChecked,
916-
) -> ComponentDetails<crate::inventory::FixedStr> {
917-
self.common.inventory().component_details(&component, index)
916+
) -> ComponentDetails<&str> {
917+
self.common.inventory.component_details(&component, index)
918918
}
919919

920920
fn component_get_active_slot(

task/validate-api/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {
5757
let mut missing_ids = 0;
5858
let mut duplicate_ids = 0;
5959
let mut ids_too_long = 0;
60+
#[cfg(feature = "fruid")]
6061
let mut bad_fruids = 0;
6162
//
6263
// The DEVICE_INDICES_BY_SORTED_ID array is used to look up indices by ID
@@ -179,6 +180,7 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {
179180
SpComponent::MAX_ID_LENGTH,
180181
);
181182

183+
#[cfg(feature = "fruid")]
182184
anyhow::ensure!(
183185
bad_fruids == 0,
184186
"{bad_fruids} devices have invalid FRUID configs!"

0 commit comments

Comments
 (0)