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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use {
    super::PdoState,
    _20millivolts_mod::_20millivolts,
    _250milliwatts_mod::_250milliwatts,
    _50milliamperes_mod::_50milliamperes,
    _50millivolts_mod::_50millivolts,
    byteorder::{ByteOrder, LittleEndian},
    defmt::Format,
    heapless::Vec,
    proc_bitfield::bitfield,
    uom::si::{self, electric_current::centiampere, electric_potential::decivolt, power::watt},
};

mod _20millivolts_mod {
    unit! {
        system: uom::si;
        quantity: uom::si::electric_potential;

        @_20millivolts: 0.02; "_20mV", "_20millivolts", "_20millivolts";
    }
}

mod _50milliamperes_mod {
    unit! {
        system: uom::si;
        quantity: uom::si::electric_current;

        @_50milliamperes: 0.05; "_50mA", "_50milliamps", "_50milliamps";
    }
}

mod _50millivolts_mod {
    unit! {
        system: uom::si;
        quantity: uom::si::electric_potential;

        @_50millivolts: 0.05; "_50mV", "_50millivolts", "_50millivolts";
    }
}

mod _250milliwatts_mod {
    unit! {
        system: uom::si;
        quantity: uom::si::power;

        @_250milliwatts: 0.25; "_250mW", "_250milliwatts", "_250milliwatts";
    }
}

#[derive(Clone, Copy, Debug, Format)]
pub enum PowerDataObjectType {
    FixedSupply,
    Battery,
    VariableSupply,
    PPS,
    AVS,
}

#[derive(Clone, Copy, Debug, Format)]
pub enum PowerDataObject {
    FixedSupply(FixedSupply),
    Battery(Battery),
    VariableSupply(VariableSupply),
    AugmentedPowerDataObject(AugmentedPowerDataObject),
    Unknown(PowerDataObjectRaw),
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct PowerDataObjectRaw(pub u32): Debug, FromRaw, IntoRaw {
        pub kind: u8 @ 30..=31,
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct FixedSupply(pub u32): Debug, FromRaw, IntoRaw {
        /// Fixed supply
        pub kind: u8 @ 30..=31,
        /// Dual-role power
        pub dual_role_power: bool @ 29,
        /// USB suspend supported
        pub usb_suspend_supported: bool @ 28,
        /// Unconstrained power
        pub unconstrained_power: bool @ 27,
        /// USB communications capable
        pub usb_communications_capable: bool @ 26,
        /// Dual-role data
        pub dual_role_data: bool @ 25,
        /// Unchunked extended messages supported
        pub unchunked_extended_messages_supported: bool @ 24,
        /// EPR mode capable
        pub epr_mode_capable: bool @ 23,
        /// Peak current
        pub peak_current: u8 @ 20..=21,
        /// Voltage in 50mV units
        pub raw_voltage: u16 @ 10..=19,
        /// Maximum current in 10mA units
        pub raw_max_current: u16 @ 0..=9,
    }
}

impl FixedSupply {
    pub fn voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_50millivolts>(self.raw_voltage())
    }

    pub fn max_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<centiampere>(self.raw_max_current())
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct Battery(pub u32): Debug, FromRaw, IntoRaw {
        /// Battery
        pub kind: u8 @ 30..=31,
        /// Maximum Voltage in 50mV units
        pub raw_max_voltage: u16 @ 20..=29,
        /// Minimum Voltage in 50mV units
        pub raw_min_voltage: u16 @ 10..=19,
        /// Maximum Allowable Power in 250mW units
        pub raw_max_power: u16 @ 0..=9,
    }
}

impl Battery {
    pub fn max_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_50millivolts>(self.raw_max_voltage())
    }

    pub fn min_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_50millivolts>(self.raw_min_voltage())
    }

    pub fn max_power(&self) -> si::u16::Power {
        si::u16::Power::new::<_250milliwatts>(self.raw_max_power())
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct VariableSupply(pub u32): Debug, FromRaw, IntoRaw {
        /// Variable supply (non-battery)
        pub kind: u8 @ 30..=31,
        /// Maximum Voltage in 50mV units
        pub raw_max_voltage: u16 @ 20..=29,
        /// Minimum Voltage in 50mV units
        pub raw_min_voltage: u16 @ 10..=19,
        /// Maximum current in 10mA units
        pub raw_max_current: u16 @ 0..=9,
    }
}

impl VariableSupply {
    pub fn max_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_50millivolts>(self.raw_max_voltage())
    }

    pub fn min_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_50millivolts>(self.raw_min_voltage())
    }

    pub fn max_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<centiampere>(self.raw_max_current())
    }
}

#[derive(Clone, Copy, Debug, Format)]
pub enum AugmentedPowerDataObject {
    SPR(SPRProgrammablePowerSupply),
    EPR(EPRAdjustableVoltageSupply),
    Unknown(u32),
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct AugmentedPowerDataObjectRaw(pub u32): Debug, FromRaw, IntoRaw {
        /// Augmented power data object
        pub kind: u8 @ 30..=31,
        pub supply: u8 @ 28..=29,
        pub power_capabilities: u32 @ 0..=27,
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct SPRProgrammablePowerSupply(pub u32): Debug, FromRaw, IntoRaw {
        /// Augmented power data object
        pub kind: u8 @ 30..=31,
        /// SPR programmable power supply
        pub supply: u8 @ 28..=29,
        pub pps_power_limited: bool @ 27,
        /// Maximum voltage in 100mV increments
        pub raw_max_voltage: u8 @ 17..=24,
        /// Minimum Voltage in 100mV increments
        pub raw_min_voltage: u8 @ 8..=15,
        /// Maximum Current in 50mA increments
        pub raw_max_current: u8 @ 0..=6,
    }
}

impl SPRProgrammablePowerSupply {
    pub fn max_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<decivolt>(self.raw_max_voltage().into())
    }

    pub fn min_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<decivolt>(self.raw_min_voltage().into())
    }

    pub fn max_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<_50milliamperes>(self.raw_max_current().into())
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct EPRAdjustableVoltageSupply(pub u32): Debug, FromRaw, IntoRaw {
        /// Augmented power data object
        pub kind: u8 @ 30..=31,
        /// EPR adjustable voltage supply
        pub supply: u8 @ 28..=29,
        pub peak_current: u8 @ 26..=27,
        /// Maximum voltage in 100mV increments
        pub raw_max_voltage: u16 @ 17..=25,
        /// Minimum Voltage in 100mV increments
        pub raw_min_voltage: u8 @ 8..=15,
        /// PDP in 1W increments
        pub raw_pd_power: u8 @ 0..=7,
    }
}

impl EPRAdjustableVoltageSupply {
    pub fn max_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<decivolt>(self.raw_max_voltage())
    }

    pub fn min_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<decivolt>(self.raw_min_voltage().into())
    }

    pub fn pd_power(&self) -> si::u8::Power {
        si::u8::Power::new::<watt>(self.raw_pd_power())
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct RawRequestDataObject(pub u32): Debug, FromRaw, IntoRaw {
        /// Valid range 1..=14
        pub object_position: u8 @ 28..=31,
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct FixedVariableRequestDataObject(pub u32): Debug, FromRaw, IntoRaw {
        /// Valid range 1..=14
        pub object_position: u8 @ 28..=31,
        pub giveback_flag: bool @ 27,
        pub capability_mismatch: bool @ 26,
        pub usb_communications_capable: bool @ 25,
        pub no_usb_suspend: bool @ 24,
        pub unchunked_extended_messages_supported: bool @ 23,
        pub epr_mode_capable: bool @ 22,
        pub raw_operating_current: u16 @ 10..=19,
        pub raw_max_operating_current: u16 @ 0..=9,
    }
}

impl FixedVariableRequestDataObject {
    pub fn to_bytes(&self, buf: &mut [u8]) {
        LittleEndian::write_u32(buf, self.0);
    }

    pub fn operating_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<centiampere>(self.raw_operating_current())
    }

    pub fn max_operating_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<centiampere>(self.raw_max_operating_current())
    }
}

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct BatteryRequestDataObject(pub u32): Debug, FromRaw, IntoRaw {
        /// Object position (0000b and 1110b…1111b are Reserved and Shall Not be used)
        pub object_position: u8 @ 28..=31,
        /// GiveBackFlag = 0
        pub giveback_flag: bool @ 27,
        /// Capability mismatch
        pub capability_mismatch: bool @ 26,
        /// USB communications capable
        pub usb_communications_capable: bool @ 25,
        /// No USB Suspend
        pub no_usb_suspend: bool @ 24,
        /// Unchunked extended messages supported
        pub unchunked_extended_messages_supported: bool @ 23,
        /// EPR mode capable
        pub epr_mode_capable: bool @ 22,
        /// Operating power in 250mW units
        pub raw_operating_power: u16 @ 10..=19,
        /// Maximum operating power in 250mW units
        pub raw_max_operating_power: u16 @ 0..=9,
    }
}

impl BatteryRequestDataObject {
    pub fn to_bytes(&self, buf: &mut [u8]) {
        LittleEndian::write_u32(buf, self.0);
    }

    pub fn operating_power(&self) -> si::u32::Power {
        si::u32::Power::new::<_250milliwatts>(self.raw_operating_power().into())
    }

    pub fn max_operating_power(&self) -> si::u32::Power {
        si::u32::Power::new::<_250milliwatts>(self.raw_max_operating_power().into())
    }
}

bitfield!(
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct PPSRequestDataObject(pub u32): Debug, FromRaw, IntoRaw {
        /// Object position (0000b and 1110b…1111b are Reserved and Shall Not be used)
        pub object_position: u8 @ 28..=31,
        /// Capability mismatch
        pub capability_mismatch: bool @ 26,
        /// USB communications capable
        pub usb_communications_capable: bool @ 25,
        /// No USB Suspend
        pub no_usb_suspend: bool @ 24,
        /// Unchunked extended messages supported
        pub unchunked_extended_messages_supported: bool @ 23,
        /// EPR mode capable
        pub epr_mode_capable: bool @ 22,
        /// Output voltage in 20mV units
        pub raw_output_voltage: u16 @ 9..=20,
        /// Operating current in 50mA units
        pub raw_operating_current: u16 @ 0..=6,
    }
);

impl PPSRequestDataObject {
    pub fn to_bytes(&self, buf: &mut [u8]) {
        LittleEndian::write_u32(buf, self.0);
    }

    pub fn output_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_20millivolts>(self.raw_output_voltage())
    }

    pub fn operating_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<_50milliamperes>(self.raw_operating_current())
    }
}

bitfield!(
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct AVSRequestDataObject(pub u32): Debug, FromRaw, IntoRaw {
        /// Object position (0000b and 1110b…1111b are Reserved and Shall Not be used)
        pub object_position: u8 @ 28..=31,
        /// Capability mismatch
        pub capability_mismatch: bool @ 26,
        /// USB communications capable
        pub usb_communications_capable: bool @ 25,
        /// No USB Suspend
        pub no_usb_suspend: bool @ 24,
        /// Unchunked extended messages supported
        pub unchunked_extended_messages_supported: bool @ 23,
        /// EPR mode capable
        pub epr_mode_capable: bool @ 22,
        /// Output voltage in 20mV units
        pub raw_output_voltage: u16 @ 9..=20,
        /// Operating current in 50mA units
        pub raw_operating_current: u16 @ 0..=6,
    }
);

impl AVSRequestDataObject {
    pub fn to_bytes(&self, buf: &mut [u8]) {
        LittleEndian::write_u32(buf, self.0);
    }

    pub fn output_voltage(&self) -> si::u16::ElectricPotential {
        si::u16::ElectricPotential::new::<_20millivolts>(self.raw_output_voltage())
    }

    pub fn operating_current(&self) -> si::u16::ElectricCurrent {
        si::u16::ElectricCurrent::new::<_50milliamperes>(self.raw_operating_current())
    }
}

#[derive(Debug, Clone, Format)]
pub enum Request {
    FixedSupply(FixedVariableRequestDataObject),
    VariableSupply(FixedVariableRequestDataObject),
    Battery(BatteryRequestDataObject),
    PPS(PPSRequestDataObject),
    AVS(AVSRequestDataObject),
    Unknown(RawRequestDataObject),
}

impl Request {
    pub fn object_position(&self) -> u8 {
        match self {
            Request::FixedSupply(p) => p.object_position(),
            Request::VariableSupply(p) => p.object_position(),
            Request::Battery(p) => p.object_position(),
            Request::PPS(p) => p.object_position(),
            Request::AVS(p) => p.object_position(),
            Request::Unknown(p) => p.object_position(),
        }
    }
}

#[derive(Debug, Clone, Format)]
pub struct SourceCapabilities(pub(crate) Vec<PowerDataObject, 8>);

impl SourceCapabilities {
    pub fn vsafe_5v(&self) -> Option<&FixedSupply> {
        self.0.first().and_then(|supply| {
            if let PowerDataObject::FixedSupply(supply) = supply {
                Some(supply)
            } else {
                None
            }
        })
    }

    pub fn dual_role_power(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::dual_role_power)
            .unwrap_or_default()
    }

    pub fn usb_suspend_supported(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::usb_suspend_supported)
            .unwrap_or_default()
    }

    pub fn unconstrained_power(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::unconstrained_power)
            .unwrap_or_default()
    }

    pub fn dual_role_data(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::dual_role_data)
            .unwrap_or_default()
    }

    pub fn unchunked_extended_messages_supported(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::unchunked_extended_messages_supported)
            .unwrap_or_default()
    }

    pub fn epr_mode_capable(&self) -> bool {
        self.vsafe_5v()
            .map(FixedSupply::epr_mode_capable)
            .unwrap_or_default()
    }

    pub fn pdos(&self) -> &[PowerDataObject] {
        &self.0
    }
}

impl PdoState for SourceCapabilities {
    fn pdo_at_object_position(&self, position: u8) -> Option<PowerDataObjectType> {
        self.pdos()
            .get(position.saturating_sub(1) as usize)
            .and_then(|pdo| match pdo {
                PowerDataObject::FixedSupply(_) => Some(PowerDataObjectType::FixedSupply),
                PowerDataObject::Battery(_) => Some(PowerDataObjectType::Battery),
                PowerDataObject::VariableSupply(_) => Some(PowerDataObjectType::VariableSupply),
                PowerDataObject::AugmentedPowerDataObject(augmented) => match augmented {
                    AugmentedPowerDataObject::SPR(_) => Some(PowerDataObjectType::PPS),
                    AugmentedPowerDataObject::EPR(_) => Some(PowerDataObjectType::AVS),
                    AugmentedPowerDataObject::Unknown(_) => None,
                },
                PowerDataObject::Unknown(_) => None,
            })
    }
}

impl PdoState for Option<SourceCapabilities> {
    fn pdo_at_object_position(&self, position: u8) -> Option<PowerDataObjectType> {
        self.as_ref().pdo_at_object_position(position)
    }
}

impl PdoState for Option<&SourceCapabilities> {
    fn pdo_at_object_position(&self, position: u8) -> Option<PowerDataObjectType> {
        self.and_then(|s| s.pdo_at_object_position(position))
    }
}