1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::marker::PhantomData;

use crate::{
    connection::{CompletionCode, IpmiCommand, LogicalUnit, Message, NetFn, ParseResponseError},
    log_vec, Loggable,
};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SdrCount;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SensorCount;

trait FromOpValue {
    fn from(value: u8) -> Self;
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NumberOfSensors(pub u8);

impl FromOpValue for NumberOfSensors {
    fn from(value: u8) -> Self {
        Self(value)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NumberOfSdrs(pub u8);

impl FromOpValue for NumberOfSdrs {
    fn from(value: u8) -> Self {
        Self(value)
    }
}

#[derive(Debug, Clone)]
pub struct DeviceSdrInfo<T> {
    pub operation_value: T,
    pub dynamic_population: bool,
    pub lun_0_has_sensors: bool,
    pub lun_1_has_sensors: bool,
    pub lun_2_has_sensors: bool,
    pub lun_3_has_sensors: bool,
    pub sensor_population_epoch: Option<u32>,
}

impl<T> DeviceSdrInfo<T> {
    pub fn lun_has_sensors(&self, lun: LogicalUnit) -> bool {
        match lun {
            LogicalUnit::Zero => self.lun_0_has_sensors,
            LogicalUnit::One => self.lun_1_has_sensors,
            LogicalUnit::Two => self.lun_2_has_sensors,
            LogicalUnit::Three => self.lun_3_has_sensors,
        }
    }

    fn partial_log(&self, mut log: Vec<crate::fmt::LogItem>) -> Vec<crate::fmt::LogItem> {
        let mut luns_with_sensors = Vec::new();
        if self.lun_0_has_sensors {
            luns_with_sensors.push(0);
        }
        if self.lun_1_has_sensors {
            luns_with_sensors.push(1);
        }
        if self.lun_2_has_sensors {
            luns_with_sensors.push(2);
        }
        if self.lun_3_has_sensors {
            luns_with_sensors.push(3);
        }

        log.push((1, "LUNs with sensors", format!("{:?}", luns_with_sensors)).into());

        if let Some(epoch) = self.sensor_population_epoch {
            log.push((1, "Sensor pop. epoch", format!("0x{epoch:04X}")).into());
        }

        log
    }

    fn parse(data: &[u8]) -> Option<Self>
    where
        T: FromOpValue,
    {
        if data.len() < 2 {
            return None;
        }

        let op_value = data[0];
        let dynamic_population = (data[1] & 0x80) == 0x80;

        let lun_3_has_sensors = (data[1] & 0x08) == 0x08;
        let lun_2_has_sensors = (data[1] & 0x04) == 0x04;
        let lun_1_has_sensors = (data[1] & 0x02) == 0x02;
        let lun_0_has_sensors = (data[1] & 0x01) == 0x01;

        let sensor_population_epoch = if dynamic_population && data.len() < 6 {
            return None;
        } else if dynamic_population {
            Some(u32::from_le_bytes([data[2], data[3], data[4], data[5]]))
        } else {
            None
        };

        Some(Self {
            operation_value: T::from(op_value),
            dynamic_population,
            lun_0_has_sensors,
            lun_1_has_sensors,
            lun_2_has_sensors,
            lun_3_has_sensors,
            sensor_population_epoch,
        })
    }
}

impl Loggable for DeviceSdrInfo<NumberOfSdrs> {
    fn into_log(&self) -> Vec<crate::fmt::LogItem> {
        let log = log_vec![
            (0, "Device SDR information"),
            (1, "Number of SDRs", self.operation_value.0)
        ];
        self.partial_log(log)
    }
}

impl Loggable for DeviceSdrInfo<NumberOfSensors> {
    fn into_log(&self) -> Vec<crate::fmt::LogItem> {
        let log = log_vec![
            (0, "Device SDR information"),
            (1, "Number of sensors", self.operation_value.0)
        ];
        self.partial_log(log)
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetDeviceSdrInfo<T> {
    _phantom: PhantomData<T>,
}

impl<T> GetDeviceSdrInfo<T> {
    pub fn new(_: T) -> Self {
        Self {
            _phantom: PhantomData::default(),
        }
    }
}

impl From<GetDeviceSdrInfo<SdrCount>> for Message {
    fn from(_: GetDeviceSdrInfo<SdrCount>) -> Self {
        Message::new_request(NetFn::SensorEvent, 0x20, vec![0x01])
    }
}

impl From<GetDeviceSdrInfo<SensorCount>> for Message {
    fn from(_: GetDeviceSdrInfo<SensorCount>) -> Self {
        Message::new_request(NetFn::SensorEvent, 0x20, vec![0x01])
    }
}

impl IpmiCommand for GetDeviceSdrInfo<SdrCount> {
    type Output = DeviceSdrInfo<NumberOfSdrs>;

    type Error = ();

    fn parse_response(
        completion_code: CompletionCode,
        data: &[u8],
    ) -> Result<Self::Output, ParseResponseError<Self::Error>> {
        Self::check_cc_success(completion_code)?;

        DeviceSdrInfo::parse(data).ok_or(ParseResponseError::NotEnoughData)
    }
}

impl IpmiCommand for GetDeviceSdrInfo<SensorCount> {
    type Output = DeviceSdrInfo<NumberOfSensors>;

    type Error = ();

    fn parse_response(
        completion_code: CompletionCode,
        data: &[u8],
    ) -> Result<Self::Output, ParseResponseError<Self::Error>> {
        Self::check_cc_success(completion_code)?;

        DeviceSdrInfo::parse(data).ok_or(ParseResponseError::NotEnoughData)
    }
}