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
use {
    crate::{DataRole, PowerRole},
    byteorder::{ByteOrder, LittleEndian},
    defmt::Format,
    proc_bitfield::bitfield,
};

bitfield! {
    #[derive(Clone, Copy, PartialEq, Eq, Format)]
    pub struct Header(pub u16): Debug, FromRaw, IntoRaw {
        pub extended: bool @ 15,
        pub num_objects: u8 [get usize] @ 12..=14,
        pub message_id: u8 @ 9..=11,
        pub port_power_role: bool [get PowerRole, set PowerRole] @ 8,
        pub spec_revision: u8 [get SpecificationRevision, set SpecificationRevision] @ 6..=7,
        pub port_data_role: bool [get DataRole, set DataRole] @ 5,
        pub message_type_raw: u8 @ 0..4,
    }
}

impl Header {
    pub fn from_bytes(buf: &[u8]) -> Self {
        assert!(buf.len() == 2);

        Header(LittleEndian::read_u16(buf))
    }

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

    pub fn message_type(&self) -> MessageType {
        if self.num_objects() == 0 {
            MessageType::Control(self.message_type_raw().into())
        } else {
            MessageType::Data(self.message_type_raw().into())
        }
    }
}

#[derive(Debug, Format)]
pub enum SpecificationRevision {
    R1_0,
    R2_0,
    R3_0,
}

impl From<u8> for SpecificationRevision {
    fn from(value: u8) -> Self {
        match value {
            0b00 => Self::R1_0,
            0b01 => Self::R2_0,
            0b10 => Self::R3_0,
            _ => unimplemented!(),
        }
    }
}

impl From<SpecificationRevision> for u8 {
    fn from(value: SpecificationRevision) -> Self {
        match value {
            SpecificationRevision::R1_0 => 0b00,
            SpecificationRevision::R2_0 => 0b01,
            SpecificationRevision::R3_0 => 0b10,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Format)]
pub enum MessageType {
    Control(ControlMessageType),
    Data(DataMessageType),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Format)]
pub enum ControlMessageType {
    GoodCRC = 0b0_0001,
    GotoMin = 0b0_0010,
    Accept = 0b0_0011,
    Reject = 0b0_0100,
    Ping = 0b0_0101,
    PsRdy = 0b0_0110,
    GetSourceCap = 0b0_0111,
    GetSinkCap = 0b0_1000,
    DrSwap = 0b0_1001,
    PrSwap = 0b0_1010,
    VconnSwap = 0b0_1011,
    Wait = 0b0_1100,
    SoftReset = 0b0_1101,
    DataReset = 0b0_1110,
    DataResetComplete = 0b0_1111,
    NotSupported = 0b1_0000,
    GetSourceCapExtended = 0b1_0001,
    GetStatus = 0b1_0010,
    FrSwap = 0b1_0011,
    GetPpsStatus = 0b1_0100,
    GetCountryCodes = 0b1_0101,
    GetSinkCapExtended = 0b1_0110,
    GetSourceInfo = 0b1_0111,
    GetRevision = 0b1_1000,
    Reserved,
}

impl From<u8> for ControlMessageType {
    fn from(value: u8) -> Self {
        match value {
            0b0_0001 => Self::GoodCRC,
            0b0_0010 => Self::GotoMin,
            0b0_0011 => Self::Accept,
            0b0_0100 => Self::Reject,
            0b0_0101 => Self::Ping,
            0b0_0110 => Self::PsRdy,
            0b0_0111 => Self::GetSourceCap,
            0b0_1000 => Self::GetSinkCap,
            0b0_1001 => Self::DrSwap,
            0b0_1010 => Self::PrSwap,
            0b0_1011 => Self::VconnSwap,
            0b0_1100 => Self::Wait,
            0b0_1101 => Self::SoftReset,
            0b0_1110 => Self::DataReset,
            0b0_1111 => Self::DataResetComplete,
            0b1_0000 => Self::NotSupported,
            0b1_0001 => Self::GetSourceCapExtended,
            0b1_0010 => Self::GetStatus,
            0b1_0011 => Self::FrSwap,
            0b1_0100 => Self::GetPpsStatus,
            0b1_0101 => Self::GetCountryCodes,
            0b1_0110 => Self::GetSinkCapExtended,
            0b1_0111 => Self::GetSourceInfo,
            0b1_1000 => Self::GetRevision,
            _ => Self::Reserved,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Format)]
pub enum DataMessageType {
    SourceCapabilities = 0b0_0001,
    Request = 0b0_0010,
    Bist = 0b0_0011,
    SinkCapabilities = 0b0_0100,
    BatteryStatus = 0b0_0101,
    Alert = 0b0_0110,
    GetCountryInfo = 0b0_0111,
    EnterUsb = 0b0_1000,
    EprRequest = 0b0_1001,
    EprMode = 0b0_1010,
    SourceInfo = 0b0_1011,
    Revision = 0b0_1100,
    VendorDefined = 0b0_1111,
    Reserved,
}

impl From<u8> for DataMessageType {
    fn from(value: u8) -> Self {
        match value {
            0b0_0001 => Self::SourceCapabilities,
            0b0_0010 => Self::Request,
            0b0_0011 => Self::Bist,
            0b0_0100 => Self::SinkCapabilities,
            0b0_0101 => Self::BatteryStatus,
            0b0_0110 => Self::Alert,
            0b0_0111 => Self::GetCountryInfo,
            0b0_1000 => Self::EnterUsb,
            0b0_1001 => Self::EprRequest,
            0b0_1010 => Self::EprMode,
            0b0_1011 => Self::SourceInfo,
            0b0_1100 => Self::Revision,
            0b0_1111 => Self::VendorDefined,
            _ => Self::Reserved,
        }
    }
}