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 ;
56use gateway_messages:: measurement:: {
67 Measurement , MeasurementError , MeasurementKind ,
78} ;
@@ -18,54 +19,56 @@ use userlib::UnwrapLite;
1819
1920userlib:: task_slot!( VALIDATE , validate) ;
2021userlib:: task_slot!( SENSOR , sensor) ;
21- userlib:: task_slot!( I2C , i2c ) ;
22+ userlib:: task_slot!( I2C , i2c_driver ) ;
2223
2324pub ( 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
6264impl 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 (
0 commit comments