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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Portable Battery (Type 22)
//!
//! This structure describes the attributes of the portable battery or batteries for the system.
//! The structure contains the static attributes for the group. Each structure describes a single
//! battery pack’s attributes.

use core::fmt;

use crate::{
    InfoType,
    MalformedStructureError::{self, InvalidFormattedSectionLength},
    RawStructure,
};

/// Main struct for *Portable Battery (Type 22)*
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PortableBattery<'a> {
    /// Specifies the structure’s handle
    pub handle: u16,
    /// Location of the battery
    pub location: &'a str,
    /// Company name that manufactured the battery
    pub manufacturer: &'a str,
    pub manufacture_date: ManufactureDate<'a>,
    pub serial_number: SerialNumber<'a>,
    /// String that names the battery device
    pub device_name: &'a str,
    pub device_chemistry: DeviceChemistry<'a>,
    pub design_capacity: DesignCapacity,
    /// Design voltage of the battery in mVolts.\
    /// If the value is unknown, the field contains 0.
    pub design_voltage: u16,
    /// String that contains the Smart Battery Data Specification version number supported by this
    /// battery.\
    /// If the battery does not support the function, no string is supplied.
    pub sbds_version_number: &'a str,
    /// Maximum error (as a percentage in the range 0 to 100) in the Watt-hour data reported by the
    /// battery, indicating an upper bound on how much additional energy the battery might have
    /// above the energy it reports having.\
    /// If the value is unknown, the field contains FFh.
    pub maximum_error_in_battery_data: u8,
    pub oem_specific: Option<u32>,
}

/// Date on which the battery was manufactured.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ManufactureDate<'a> {
    None,
    Basic(&'a str),
    SmartBatteryDataSpecification { year: u16, month: u8, date: u8 },
}

/// Serial number for the batter.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SerialNumber<'a> {
    None,
    Basic(&'a str),
    SmartBatteryDataSpecification(u16),
}

/// Design capacity of the battery in mWatt-hours
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DesignCapacity {
    Unknown,
    Data { value: u16, multiplier: u8 },
}

/// Identifies the battery chemistry.\
///
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DeviceChemistry<'a> {
    Other,
    Unknown,
    /// Lead Acid
    LeadAcid,
    /// Nickel Cadmium
    NickelCadmium,
    /// Nickel metal hydride
    NickelMetalHydride,
    /// Lithium-ion
    LithiumIon,
    /// Zinc air
    ZincAir,
    /// Lithium Polymer
    LithiumPolymer,
    Undefined(u8),
    /// String that identifies the battery chemistry (for example, “PbAc”)
    SmartBatteryDataSpecification(&'a str),
}

impl<'a> PortableBattery<'a> {
    pub(crate) fn try_from(structure: RawStructure<'a>) -> Result<Self, MalformedStructureError> {
        let handle = structure.handle;
        match (structure.version.major, structure.version.minor) {
            (2, 1) if structure.length != 0x10 => Err(InvalidFormattedSectionLength(
                InfoType::PortableBattery,
                handle,
                "",
                0x10,
            )),
            v if v >= (2, 2) && structure.length != 0x1A => Err(InvalidFormattedSectionLength(
                InfoType::PortableBattery,
                handle,
                "",
                0x1A,
            )),
            _ => Ok(Self {
                handle,
                location: structure.get_string(0x04)?,
                manufacturer: structure.get_string(0x05)?,
                manufacture_date: ManufactureDate::new(
                    structure
                        .get::<u8>(0x06)
                        .ok()
                        .filter(|idx| idx != &0)
                        .and_then(|idx| structure.find_string(idx).ok()),
                    structure.get::<u16>(0x12).ok(),
                ),
                serial_number: SerialNumber::new(
                    structure
                        .get::<u8>(0x07)
                        .ok()
                        .filter(|idx| idx != &0)
                        .and_then(|idx| structure.find_string(idx).ok()),
                    structure.get::<u16>(0x10).ok(),
                ),
                device_name: structure.get_string(0x08)?,
                device_chemistry: DeviceChemistry::new(structure.get::<u8>(0x09)?, structure.get_string(0x14).ok()),
                design_capacity: DesignCapacity::new(structure.get::<u16>(0x0A)?, structure.get::<u8>(0x15).ok()),
                design_voltage: structure.get::<u16>(0x0C)?,
                sbds_version_number: structure.get_string(0x0E)?,
                maximum_error_in_battery_data: structure.get::<u8>(0x0F)?,
                oem_specific: structure.get::<u32>(0x16).ok(),
            }),
        }
    }
}

impl<'a> ManufactureDate<'a> {
    fn new(basic: Option<&'a str>, sbds: Option<u16>) -> Self {
        match (basic, sbds) {
            (Some(s), _) => Self::Basic(s),
            (None, Some(date)) => Self::SmartBatteryDataSpecification {
                year: ((date & 0b1111_1110_0000_0000) >> 9) + 1980,
                month: ((date & 0b0000_0001_1110_0000) >> 5) as u8,
                date: (date & 0b0000_0000_0001_1111) as u8,
            },
            _ => Self::None,
        }
    }
}
impl<'a> fmt::Display for ManufactureDate<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::None => Ok(()),
            Self::Basic(s) => write!(f, "{}", s),
            Self::SmartBatteryDataSpecification { year, month, date } =>
            // ISO 8601
            {
                write!(f, "{:04}-{:02}-{:02}", year, month, date)
            }
        }
    }
}

impl<'a> SerialNumber<'a> {
    fn new(basic: Option<&'a str>, sbds: Option<u16>) -> Self {
        match (basic, sbds) {
            (Some(s), _) => Self::Basic(s),
            (None, Some(word)) => Self::SmartBatteryDataSpecification(word),
            _ => Self::None,
        }
    }
}
impl<'a> fmt::Display for SerialNumber<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::None => Ok(()),
            Self::Basic(s) => write!(f, "{}", s),
            Self::SmartBatteryDataSpecification(word) => write!(f, "{:#04X}", word),
        }
    }
}

impl<'a> DeviceChemistry<'a> {
    fn new(basic: u8, sbds: Option<&'a str>) -> Self {
        match basic {
            0x01 => Self::Other,
            0x02 => {
                if let Some(s) = sbds {
                    Self::SmartBatteryDataSpecification(s)
                } else {
                    Self::Unknown
                }
            }
            0x03 => Self::LeadAcid,
            0x04 => Self::NickelCadmium,
            0x05 => Self::NickelMetalHydride,
            0x06 => Self::LithiumIon,
            0x07 => Self::ZincAir,
            0x08 => Self::LithiumPolymer,
            v => Self::Undefined(v),
        }
    }
}
impl<'a> fmt::Display for DeviceChemistry<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::LeadAcid => write!(f, "Lead Acid"),
            Self::NickelCadmium => write!(f, "Nickel Cadmium"),
            Self::NickelMetalHydride => write!(f, "Nickel metal hydride"),
            Self::LithiumIon => write!(f, "Lithium-ion"),
            Self::ZincAir => write!(f, "Zinc air"),
            Self::LithiumPolymer => write!(f, "Lithium Polymer"),
            Self::Undefined(v) => write!(f, "Undefined: {}", v),
            Self::SmartBatteryDataSpecification(s) => write!(f, "{}", s),
        }
    }
}

impl DesignCapacity {
    fn new(value: u16, multipler: Option<u8>) -> Self {
        if value == 0 {
            Self::Unknown
        } else {
            Self::Data {
                value,
                multiplier: multipler.unwrap_or(1),
            }
        }
    }
}
impl From<DesignCapacity> for u64 {
    fn from(dc: DesignCapacity) -> Self {
        match dc {
            DesignCapacity::Unknown => 0,
            DesignCapacity::Data { value, multiplier } => (value * multiplier as u16).into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;
    use std::prelude::v1::*;

    #[test]
    fn manufacture_date() {
        use super::ManufactureDate;

        assert_eq!("", format!("{}", ManufactureDate::new(None, None)), "Empty");
        assert_eq!(
            "07/17/2019",
            format!("{}", ManufactureDate::new(Some("07/17/2019"), None)),
            "Basic"
        );
        assert_eq!(
            "2000-02-01",
            format!("{}", ManufactureDate::new(None, Some(0x2841))),
            "SBDS"
        );
    }

    #[test]
    fn serial_number() {
        use super::SerialNumber;

        assert_eq!("", format!("{}", SerialNumber::new(None, None)), "Empty");
        assert_eq!(
            "S/N 1111",
            format!("{}", SerialNumber::new(Some("S/N 1111"), None)),
            "Basic"
        );
        assert_eq!("0xBEAF", format!("{}", SerialNumber::new(None, Some(0xBEAF))), "SBDS");
    }

    #[test]
    fn device_chemistry() {
        use super::DeviceChemistry;

        let sample = &[
            "Undefined: 0",
            "Other",
            "Unknown",
            "Lead Acid",
            "Nickel Cadmium",
            "Nickel metal hydride",
            "Lithium-ion",
            "Zinc air",
            "Lithium Polymer",
        ];
        for (n, &s) in sample.iter().enumerate() {
            let sbds = None;
            assert_eq!(s, format!("{}", DeviceChemistry::new(n as u8, sbds)));
            if n == 0x02 {
                let sbds = Some("PbAc");
                assert_eq!("PbAc", format!("{:#}", DeviceChemistry::new(n as u8, sbds)));
            }
        }
    }

    #[test]
    fn design_capacity() {
        use super::DesignCapacity;

        assert_eq!(0u64, DesignCapacity::new(0, None).into(), "Unknown");
        assert_eq!(0u64, DesignCapacity::new(0, Some(42)).into(), "Unknown");
        assert_eq!(4800u64, DesignCapacity::new(4800, None).into(), "w/o multiplier");
        assert_eq!(9600u64, DesignCapacity::new(4800, Some(2)).into(), "With multiplier");
    }

    #[test]
    fn portable_battery() {
        use super::*;
        use crate::{InfoType, RawStructure};

        let length = 26;
        let (data, strings) =
            include_bytes!("../../tests/data/________/entries/22-0/bin")[4..].split_at(length as usize - 4);
        let structure = RawStructure {
            version: (3, 2).into(),
            info: InfoType::PortableBattery,
            length,
            handle: 0x002B,
            data,
            strings,
        };
        let sample = PortableBattery {
            handle: 0x002B,
            location: "Front",
            manufacturer: "LGC",
            manufacture_date: ManufactureDate::SmartBatteryDataSpecification {
                year: 2020,
                month: 7,
                date: 1,
            },
            serial_number: SerialNumber::SmartBatteryDataSpecification(0x058B),
            device_name: "5B10W13930",
            device_chemistry: DeviceChemistry::SmartBatteryDataSpecification("LiP"),
            design_capacity: DesignCapacity::Data {
                value: 5100,
                multiplier: 10,
            },
            design_voltage: 15400,
            sbds_version_number: "03.01",
            maximum_error_in_battery_data: 0xFF,
            oem_specific: Some(0),
        };
        let result = PortableBattery::try_from(structure).unwrap();
        assert_eq!(sample, result, "PortableBattery");
    }
}