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
use {
    crate::{
        header::{ControlMessageType, DataMessageType, Header, SpecificationRevision},
        messages::{
            pdo::{FixedVariableRequestDataObject, PPSRequestDataObject, SourceCapabilities},
            vdo::{
                CertStatVDO, ProductVDO, UFPTypeVDO, VDMCommand, VDMCommandType, VDMHeader,
                VDMHeaderStructured, VDMIdentityHeader, VDMType, VDMVersionMajor, VDMVersionMinor,
            },
            Message,
        },
        DataRole, PowerRole,
    },
    core::future::Future,
    defmt::{debug, warn, Format},
    embassy_time::Instant,
    heapless::Vec,
};

pub trait Driver {
    fn init(&mut self) -> impl Future<Output = ()>;

    fn poll(&mut self, now: Instant) -> impl Future<Output = ()>;

    fn get_pending_message(&mut self) -> Option<Message>;

    fn did_change_protocol(&mut self) -> bool;

    fn send_message(&mut self, header: Header, payload: &[u8]) -> impl Future<Output = ()>;

    fn state(&mut self) -> DriverState;
}

/// Sink events
#[derive(Format)]
pub enum Event {
    /// Power delivery protocol has changed
    ProtocolChanged,
    /// Source capabilities have changed (immediately request power)
    SourceCapabilitiesChanged(SourceCapabilities),
    /// Requested power has been accepted (but not ready yet)
    PowerAccepted,
    /// Requested power has been rejected
    PowerRejected,
    /// Requested power is now ready
    PowerReady,
    /// VDM received
    VDMReceived((VDMHeader, Vec<u32, 7>)),
}

/// Requests made to sink
#[derive(Format)]
pub enum Request {
    RequestPower {
        /// Index of the desired PowerDataObject
        index: usize,
        current: u16,
    },
    RequestPPS {
        /// Index of the desired PowerDataObject
        index: usize,
        /// Requested voltage (in mV)
        voltage: u16,
        /// Requested maximum current (in mA)
        current: u16,
    },
    REQDiscoverIdentity,
    ACKDiscoverIdentity {
        identity: VDMIdentityHeader,
        cert_stat: CertStatVDO,
        product: ProductVDO,
        product_type_ufp: UFPTypeVDO,
        // Does not exist yet...        product_type_dfp: Option<DFP>,
    },
    REQDiscoverSVIDS,
}

/// Driver state
#[derive(PartialEq, Clone, Copy)]
pub enum DriverState {
    /// VBUS is present, monitoring for activity on CC1/CC2
    Usb20,
    /// Activity on CC1/CC2 has been detected, waiting for first USB PD message
    UsbPdWait,
    /// Successful USB PD communication established
    UsbPd,
    /// Wait period after a failure
    UsbRetryWait,
}

pub struct Sink<DRIVER> {
    driver: DRIVER,

    protocol: Protocol,

    /// Requested voltage (in mV)
    requested_voltage: u16,

    /// Requested maximum current (in mA)
    requested_max_current: u16,

    /// Active voltage (in mV)
    active_voltage: u16,

    /// Active maximum current (in mA)
    active_max_current: u16,

    /// Specification revision (of last message)
    spec_rev: u8,
}

impl<DRIVER: Driver> Sink<DRIVER> {
    pub fn new(driver: DRIVER) -> Self {
        Self {
            driver,
            protocol: Protocol::Usb20,
            requested_voltage: 0,
            requested_max_current: 0,
            active_voltage: 5000,
            active_max_current: 900,
            spec_rev: 1,
        }
    }

    pub async fn init(&mut self) {
        self.driver.init().await;
        self.update_protocol();
    }

    /// Call continously until `None` is returned.
    pub async fn poll(&mut self, now: Instant) -> Option<Event> {
        // poll inner driver
        self.driver.poll(now).await;

        if let Some(message) = self.driver.get_pending_message() {
            return self.handle_msg(message);
        };

        if self.driver.did_change_protocol() {
            if self.update_protocol() {
                Some(Event::ProtocolChanged)
            } else {
                None
            }
        } else {
            None
        }
    }

    pub async fn request(&mut self, request: Request) {
        match request {
            Request::RequestPower { index, current } => self.request_power(current, index).await,

            Request::RequestPPS {
                index,
                voltage,
                current,
            } => {
                // Payload is 4 bytes
                let mut payload = [0; 4];
                // Add one to index to account for array offsets starting at 0 and obj_pos
                // starting at 1...
                let obj_pos = index + 1;
                assert!(obj_pos > 0b0000 && obj_pos <= 0b1110);

                // Create PPS request data object
                let pps = PPSRequestDataObject(0)
                    .with_object_position(obj_pos as u8)
                    .with_raw_operating_current(current / 50) // Convert current from millis to 50ma units
                    .with_raw_output_voltage(voltage / 20) // Convert voltage from millis to 20mv units
                    .with_capability_mismatch(false)
                    .with_epr_mode_capable(false)
                    .with_usb_communications_capable(true);
                pps.to_bytes(&mut payload[0..4]);

                // Create header
                let header = Header(0)
                    .with_message_type_raw(DataMessageType::Request as u8)
                    .with_num_objects(1)
                    .with_spec_revision(SpecificationRevision::from(self.spec_rev))
                    .with_port_power_role(PowerRole::Sink);

                // Send request message
                self.driver.send_message(header, &payload).await
            }

            Request::ACKDiscoverIdentity {
                identity,
                cert_stat,
                product,
                product_type_ufp,
                //product_type_dfp,
            } => {
                debug!("ACKDiscoverIdentity");
                // The size of this array will actually change depending on data...
                // TODO: Fix this!
                let mut payload = [0; 5 * 4];
                let header = Header(0)
                    .with_message_type_raw(DataMessageType::VendorDefined as u8)
                    .with_num_objects(5) // 5 VDOs, vdm header, id header, cert, product, UFP product type
                    .with_port_data_role(DataRole::Ufp)
                    .with_port_power_role(PowerRole::Sink)
                    .with_spec_revision(SpecificationRevision::from(self.spec_rev));

                let vdm_header_vdo = VDMHeader::Structured(
                    VDMHeaderStructured(0)
                        .with_command(VDMCommand::DiscoverIdentity)
                        .with_command_type(VDMCommandType::ResponderACK)
                        .with_object_position(0) // 0 Must be used for descover identity
                        .with_standard_or_vid(0xff00) // PD SID must be used with descover identity
                        //.with_vdm_type(VDMType::Structured)
                        .with_vdm_version_major(VDMVersionMajor::Version2x.into())
                        .with_vdm_version_minor(VDMVersionMinor::Version20.into()),
                );
                vdm_header_vdo.to_bytes(&mut payload[0..4]);
                identity.to_bytes(&mut payload[4..8]);
                cert_stat.to_bytes(&mut payload[8..12]);
                product.to_bytes(&mut payload[12..16]);
                product_type_ufp.to_bytes(&mut payload[16..20]);
                // if let Some(product_type_dfp) = product_type_dfp {
                //     // 20..24 are padding bytes
                //     product_type_dfp.to_bytes(&mut payload[24..32]);
                // }
                debug!("Sending VDM {:x}", payload);
                self.driver.send_message(header, &payload).await;
                debug!("Sent VDM");
            }
            Request::REQDiscoverSVIDS => {
                debug!("REQDiscoverSVIDS");
                let mut payload = [0; 4];
                let header = Header(0)
                    .with_message_type_raw(DataMessageType::VendorDefined as u8)
                    .with_num_objects(1) // 1 VDO, vdm header
                    .with_port_data_role(DataRole::Ufp)
                    .with_port_power_role(PowerRole::Sink)
                    .with_spec_revision(SpecificationRevision::from(self.spec_rev));

                let vdm_header_vdo = VDMHeader::Structured(
                    VDMHeaderStructured(0)
                        .with_command(VDMCommand::DiscoverSVIDS)
                        .with_command_type(VDMCommandType::InitiatorREQ)
                        .with_object_position(0) // 0 Must be used for discover SVIDS
                        .with_standard_or_vid(0xff00) // PD SID must be used with discover SVIDS
                        .with_vdm_type(VDMType::Structured)
                        .with_vdm_version_major(VDMVersionMajor::Version10.into())
                        .with_vdm_version_minor(VDMVersionMinor::Version20.into()),
                );
                vdm_header_vdo.to_bytes(&mut payload[0..4]);
                debug!("Sending VDM {:x}", payload);
                self.driver.send_message(header, &payload).await;
                debug!("Sent VDM");
            }
            Request::REQDiscoverIdentity => {
                debug!("REQDiscoverIdentity");
                let mut payload = [0; 4];
                let header = Header(0)
                    .with_message_type_raw(DataMessageType::VendorDefined as u8)
                    .with_num_objects(1) // 1 VDO, vdm header
                    .with_port_data_role(DataRole::Ufp)
                    .with_port_power_role(PowerRole::Sink)
                    .with_spec_revision(SpecificationRevision::from(self.spec_rev));

                let vdm_header_vdo = VDMHeader::Structured(
                    VDMHeaderStructured(0)
                        .with_command(VDMCommand::DiscoverIdentity)
                        .with_command_type(VDMCommandType::InitiatorREQ)
                        .with_object_position(0) // 0 Must be used for descover identity
                        .with_standard_or_vid(0xff00) // PD SID must be used with descover identity
                        .with_vdm_type(VDMType::Structured)
                        .with_vdm_version_major(VDMVersionMajor::Version10.into())
                        .with_vdm_version_minor(VDMVersionMinor::Version20.into()),
                );
                vdm_header_vdo.to_bytes(&mut payload[0..4]);
                debug!("Sending VDM {:x}", payload);
                self.driver.send_message(header, &payload).await;
                debug!("Sent VDM");
            }
        }
    }

    fn update_protocol(&mut self) -> bool {
        let old_protocol = self.protocol;

        if self.driver.state() == DriverState::UsbPd {
            self.protocol = Protocol::UsbPd;
        } else {
            self.protocol = Protocol::Usb20;
            self.active_voltage = 5000;
            self.active_max_current = 900;
        }

        self.protocol != old_protocol
    }

    fn handle_msg(&mut self, message: Message) -> Option<Event> {
        use ControlMessageType::*;
        match message {
            Message::Control(Accept) => Some(Event::PowerAccepted),
            Message::Control(Reject) => {
                self.requested_voltage = 0;
                self.requested_max_current = 0;
                Some(Event::PowerRejected)
            }
            Message::Control(PsRdy) => {
                self.active_voltage = self.requested_voltage;
                self.active_max_current = self.requested_max_current;
                self.requested_voltage = 0;
                self.requested_max_current = 0;
                Some(Event::PowerReady)
            }
            Message::Control(SoftReset) => {
                warn!("UNHANDLED: Soft RESET request.");
                None
            }
            Message::Control(_) => None,
            Message::SourceCapabilities(caps) => Some(Event::SourceCapabilitiesChanged(caps)),
            Message::VendorDefined((hdr, data)) => {
                match hdr {
                    VDMHeader::Structured(hdr) => {
                        warn!(
                            "UNHANDLED: Structured VDM! CMD_TYPE: {:?}, CMD:
                        {:?}",
                            hdr.command_type(),
                            hdr.command()
                        );
                    }
                    VDMHeader::Unstructured(hdr) => {
                        warn!(
                            "UNHANDLED: Unstructured VDM! SVID: {:x}, DATA:
                        {:x}",
                            hdr.standard_or_vid(),
                            hdr.data()
                        );
                    }
                }
                Some(Event::VDMReceived((hdr, data)))
            }
            Message::Request(_) => None,
            Message::Unknown => unimplemented!(),
        }
    }

    async fn request_power(&mut self, max_current: u16, index: usize) {
        // Create 'request' message
        let mut payload = [0; 4];

        self.set_request_payload_fixed(&mut payload, index as u8, max_current);

        let header = Header(0)
            .with_message_type_raw(DataMessageType::Request as u8)
            .with_num_objects(1)
            .with_spec_revision(SpecificationRevision::from(self.spec_rev))
            .with_port_power_role(PowerRole::Sink);

        // Send message
        self.driver.send_message(header, &payload).await;
    }

    fn set_request_payload_fixed(&mut self, payload: &mut [u8], obj_pos: u8, mut current: u16) {
        current = (current + 5) / 10;

        if current > 0x3ff {
            current = 0x3ff;
        }

        let obj_pos = obj_pos + 1;
        assert!(obj_pos > 0b0000 && obj_pos <= 0b1110);

        FixedVariableRequestDataObject(0)
            .with_raw_operating_current(current)
            .with_raw_max_operating_current(current)
            .with_object_position(obj_pos)
            .with_no_usb_suspend(true)
            .with_usb_communications_capable(true)
            .to_bytes(payload);
    }
}

/// Power supply type
#[derive(Clone, Copy, PartialEq)]
pub enum SupplyType {
    /// Fixed supply (Vmin = Vmax)
    Fixed = 0,
    /// Battery
    Battery = 1,
    /// Variable supply (non-battery)
    Variable = 2,
    /// Programmable power supply
    Pps = 3,
}

/// Power deliver protocol
#[derive(Format, PartialEq, Clone, Copy)]
enum Protocol {
    /// No USB PD communication (5V only)
    Usb20,
    /// USB PD communication
    UsbPd,
}