Skip to content

Commit fc6b10a

Browse files
authored
Move by_refdes into pub(crate) module (#1994)
This is standalone prep for Cosmo, making the macro useable outside of the Gimlet inventory. In addition, we add a variant to the macro which lets the caller control the array size. This is needed when there are designators of different lengths in the same inventory slot, e.g. `U123` and `U51`.
1 parent b4d5c3f commit fc6b10a

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

task/host-sp-comms/src/bsp/gimlet_bcde.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! SP inventory types and implementation
66
//!
77
//! This reduces clutter in the main `ServerImpl` implementation
8-
use super::ServerImpl;
8+
use super::{inventory::by_refdes, ServerImpl};
99

1010
use drv_i2c_api::I2cDevice;
1111
use drv_i2c_api::ResponseCode;
@@ -21,31 +21,6 @@ use host_sp_messages::{InventoryData, InventoryDataResult};
2121
userlib::task_slot!(I2C, i2c_driver);
2222
userlib::task_slot!(SPI, spi_driver);
2323

24-
/// `const` function to convert a `&'static str` to a fixed-size byte array
25-
///
26-
/// This must be called a `const` parameter of `s.len()`
27-
const fn byteify<const N: usize>(s: &'static [u8]) -> [u8; N] {
28-
let mut out = [0u8; N];
29-
let mut i = 0;
30-
while i < s.len() {
31-
out[i] = s[i];
32-
i += 1;
33-
}
34-
out
35-
}
36-
macro_rules! by_refdes {
37-
($refdes:ident, $dev:ident) => {
38-
paste::paste! {{
39-
const BYTE_ARRAY: &'static [u8] = stringify!($refdes).as_bytes();
40-
(
41-
byteify::<{ BYTE_ARRAY.len() }>(BYTE_ARRAY),
42-
i2c_config::devices::[<$dev _ $refdes:lower >] as fn(TaskId) -> I2cDevice,
43-
i2c_config::sensors::[<$dev:upper _ $refdes:upper _SENSORS>]
44-
)
45-
}}
46-
};
47-
}
48-
4924
impl ServerImpl {
5025
/// Number of devices in our inventory
5126
pub(crate) const INVENTORY_COUNT: u32 = 72;

task/host-sp-comms/src/inventory.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,39 @@
99
1010
/// Inventory API version (always 0 for now)
1111
pub(crate) const INVENTORY_API_VERSION: u32 = 0;
12+
13+
/// `const` function to convert a `&'static str` to a fixed-size byte array
14+
///
15+
/// This must be called a `const` parameter of `s.len()`
16+
#[allow(dead_code)]
17+
pub(crate) const fn byteify<const N: usize>(s: &'static [u8]) -> [u8; N] {
18+
let mut out = [0u8; N];
19+
let mut i = 0;
20+
while i < s.len() {
21+
out[i] = s[i];
22+
i += 1;
23+
}
24+
out
25+
}
26+
27+
#[allow(unused_macros)]
28+
macro_rules! by_refdes {
29+
// Length is found based on refdes
30+
($refdes:ident, $dev:ident) => {
31+
by_refdes!($refdes, $dev, stringify!($refdes).as_bytes().len())
32+
};
33+
// Length is provided, e.g. if it is not consistent between refdes
34+
($refdes:ident, $dev:ident, $n:expr) => {
35+
paste::paste! {{
36+
const BYTE_ARRAY: &'static [u8] = stringify!($refdes).as_bytes();
37+
(
38+
$crate::inventory::byteify::<{ $n }>(BYTE_ARRAY),
39+
i2c_config::devices::[<$dev _ $refdes:lower >] as fn(TaskId) -> I2cDevice,
40+
i2c_config::sensors::[<$dev:upper _ $refdes:upper _SENSORS>]
41+
)
42+
}}
43+
};
44+
}
45+
46+
#[allow(unused_imports)]
47+
pub(crate) use by_refdes;

0 commit comments

Comments
 (0)