rdxusb_protocol/
lib.rs
1#![no_std]
2
3use bytemuck::{Pod, Zeroable};
4
5pub const ENDPOINT_IN: u8 = 0x81;
7pub const ENDPOINT_OUT: u8 = 0x02;
9
10pub const MESSAGE_ARB_ID_EXT: u32 = 0x80000000;
12pub const MESSAGE_ARB_ID_RTR: u32 = 0x40000000;
14pub const MESSAGE_ARB_ID_DEVICE: u32 = 0x20000000;
22
23
24#[derive(Debug, PartialEq, Eq, Clone, Copy, Pod, Zeroable)]
26#[repr(C, packed)]
27pub struct RdxUsbFsPacket {
28 pub timestamp_ns: u64,
30 pub arb_id: u32, pub dlc: u8,
34 pub channel: u8,
36 pub flags: u16,
38 pub data: [u8; 48]
40}
41
42#[derive(Debug, Copy, Clone, PartialEq, Eq, Pod, Zeroable)]
44#[repr(C, packed)]
45pub struct RdxUsbPacket {
46 pub timestamp_ns: u64,
48 pub arb_id: u32, pub dlc: u8,
52 pub channel: u8,
54 pub flags: u16,
56 pub data: [u8; 64]
58}
59
60impl From<RdxUsbFsPacket> for RdxUsbPacket {
61 fn from(value: RdxUsbFsPacket) -> Self {
62 let mut data = [0u8; 64];
63 data[..48].copy_from_slice(&value.data);
64 Self {
65 timestamp_ns: value.timestamp_ns,
66 arb_id: value.arb_id,
67 dlc: value.dlc,
68 channel: value.channel,
69 flags: value.flags,
70 data,
71 }
72 }
73}
74
75impl TryFrom<RdxUsbPacket> for RdxUsbFsPacket {
76 type Error = RdxUsbPacket;
77
78 fn try_from(value: RdxUsbPacket) -> Result<Self, Self::Error> {
79 if value.dlc > 48 { return Err(value); }
80 let len = value.dlc as usize;
81 let mut data = [0u8; 48];
82 data[..len].copy_from_slice(&value.data[..len]);
83 Ok(RdxUsbFsPacket {
84 timestamp_ns: value.timestamp_ns,
85 arb_id: value.arb_id,
86 dlc: value.dlc,
87 channel: value.channel,
88 flags: value.flags,
89 data,
90 })
91
92 }
93}
94
95impl RdxUsbFsPacket {
96 pub const fn id(&self) -> u32 {
98 self.arb_id & 0x1fff_ffff
99 }
100
101 pub const fn extended(&self) -> bool {
103 self.arb_id & MESSAGE_ARB_ID_EXT != 0
104 }
105
106 pub const fn rtr(&self) -> bool {
108 self.arb_id & MESSAGE_ARB_ID_RTR != 0
109 }
110
111 pub const fn device(&self) -> bool {
113 self.arb_id & MESSAGE_ARB_ID_DEVICE != 0
114 }
115
116 pub const SIZE: usize = core::mem::size_of::<Self>();
118
119 pub fn encode(&self) -> &[u8; Self::SIZE] {
120 bytemuck::cast_ref(self)
121 }
122
123 pub fn from_buf(buf: [u8; Self::SIZE]) -> Self {
124 bytemuck::cast(buf)
125 }
126}
127
128impl RdxUsbPacket {
129 pub const fn id(&self) -> u32 {
131 self.arb_id & 0x1fff_ffff
132 }
133
134 pub const fn extended(&self) -> bool {
136 self.arb_id & MESSAGE_ARB_ID_EXT != 0
137 }
138
139 pub const fn rtr(&self) -> bool {
141 self.arb_id & MESSAGE_ARB_ID_RTR != 0
142 }
143
144 pub const fn device(&self) -> bool {
146 self.arb_id & MESSAGE_ARB_ID_DEVICE != 0
147 }
148
149 pub const SIZE: usize = core::mem::size_of::<Self>();
151
152 pub fn into_array(self) -> [u8; Self::SIZE] {
153 unsafe { core::mem::transmute(self) }
154 }
155
156 pub fn from_buf(buf: [u8; Self::SIZE]) -> Self {
157 unsafe { core::mem::transmute(buf) }
158 }
159}
160
161#[derive(Debug, PartialEq, Eq, Clone, Copy, Pod, Zeroable)]
163#[repr(C, packed)]
164pub struct RdxUsbDeviceInfo {
165 pub sku: u16,
167 pub interface_idx: u8,
169 pub n_channels: u8,
171 pub protocol_version_major: u16,
173 pub protocol_version_minor: u16,
175 pub reserved: [u8; 24]
177}
178
179impl RdxUsbDeviceInfo {
180 pub const SIZE: usize = core::mem::size_of::<Self>();
182
183 pub fn encode(&self) -> &[u8; Self::SIZE] {
184 bytemuck::cast_ref(self)
185 }
186
187 pub fn from_buf(buf: [u8; Self::SIZE]) -> Self {
188 bytemuck::cast(buf)
189 }
190}
191
192#[derive(Debug, PartialEq, Eq, Clone, Copy)]
194#[repr(u8)]
195pub enum RdxUsbCtrl {
196 DeviceInfo = 0,
197}
198
199pub const PROTOCOL_VERSION_MAJOR_FS: u16 = 1;