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
use delegate::delegate;
use embedded_time::duration::Milliseconds;
use log::error;
use packed_struct::prelude::*;
use usb_device::class_prelude::*;
use usb_device::{Result, UsbError};
use crate::hid_class::prelude::*;
use crate::interface::raw::{RawInterface, RawInterfaceConfig};
use crate::interface::{InterfaceClass, WrappedInterface, WrappedInterfaceConfig};
use crate::page::Consumer;
#[rustfmt::skip]
pub const MULTIPLE_CODE_REPORT_DESCRIPTOR: &[u8] = &[
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x75, 0x10, 0x95, 0x04, 0x15, 0x00, 0x26, 0x9C, 0x02, 0x19, 0x00, 0x2A, 0x9C, 0x02, 0x81, 0x00, 0xC0, ];
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)]
#[packed_struct(endian = "lsb", size_bytes = "8")]
pub struct MultipleConsumerReport {
#[packed_field(ty = "enum", element_size_bytes = "2")]
pub codes: [Consumer; 4],
}
#[rustfmt::skip]
pub const FIXED_FUNCTION_REPORT_DESCRIPTOR: &[u8] = &[
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x05, 0x0C, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x07, 0x09, 0xB5, 0x09, 0xB6, 0x09, 0xB7, 0x09, 0xCD, 0x09, 0xE2, 0x09, 0xE9, 0x09, 0xEA, 0x81, 0x02, 0x95, 0x01, 0x81, 0x01, 0xC0, ];
#[derive(Clone, Copy, Debug, Eq, PartialEq, PackedStruct)]
#[packed_struct(endian = "lsb", bit_numbering = "lsb0", size_bytes = "1")]
pub struct FixedFunctionReport {
#[packed_field(bits = "0")]
pub next: bool,
#[packed_field(bits = "1")]
pub previous: bool,
#[packed_field(bits = "2")]
pub stop: bool,
#[packed_field(bits = "3")]
pub play_pause: bool,
#[packed_field(bits = "4")]
pub mute: bool,
#[packed_field(bits = "5")]
pub volume_increment: bool,
#[packed_field(bits = "6")]
pub volume_decrement: bool,
}
pub struct ConsumerControlInterface<'a, B: UsbBus> {
inner: RawInterface<'a, B>,
}
impl<'a, B: UsbBus> ConsumerControlInterface<'a, B> {
pub fn write_report(&self, report: &MultipleConsumerReport) -> usb_device::Result<usize> {
let data = report.pack().map_err(|e| {
error!("Error packing MultipleConsumerReport: {:?}", e);
UsbError::ParseError
})?;
self.inner.write_report(&data)
}
pub fn default_config() -> WrappedInterfaceConfig<Self, RawInterfaceConfig<'a>> {
WrappedInterfaceConfig::new(
RawInterfaceBuilder::new(MULTIPLE_CODE_REPORT_DESCRIPTOR)
.description("Consumer Control")
.in_endpoint(UsbPacketSize::Bytes8, Milliseconds(50))
.unwrap()
.without_out_endpoint()
.build(),
(),
)
}
}
impl<'a, B: UsbBus> InterfaceClass<'a> for ConsumerControlInterface<'a, B> {
delegate! {
to self.inner{
fn report_descriptor(&self) -> &'_ [u8];
fn id(&self) -> InterfaceNumber;
fn write_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()>;
fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&'_ str>;
fn reset(&mut self);
fn set_report(&mut self, data: &[u8]) -> Result<()>;
fn get_report(&mut self, data: &mut [u8]) -> Result<usize>;
fn get_report_ack(&mut self) -> Result<()>;
fn set_idle(&mut self, report_id: u8, value: u8);
fn get_idle(&self, report_id: u8) -> u8;
fn set_protocol(&mut self, protocol: HidProtocol);
fn get_protocol(&self) -> HidProtocol;
}
}
}
impl<'a, B: UsbBus> WrappedInterface<'a, B, RawInterface<'a, B>>
for ConsumerControlInterface<'a, B>
{
fn new(interface: RawInterface<'a, B>, _: ()) -> Self {
Self { inner: interface }
}
}
pub struct ConsumerControlFixedInterface<'a, B: UsbBus> {
inner: RawInterface<'a, B>,
}
impl<'a, B: UsbBus> ConsumerControlFixedInterface<'a, B> {
pub fn write_report(&self, report: &FixedFunctionReport) -> usb_device::Result<usize> {
let data = report.pack().map_err(|e| {
error!("Error packing MultipleConsumerReport: {:?}", e);
UsbError::ParseError
})?;
self.inner.write_report(&data)
}
pub fn default_config() -> WrappedInterfaceConfig<Self, RawInterfaceConfig<'a>> {
WrappedInterfaceConfig::new(
RawInterfaceBuilder::new(FIXED_FUNCTION_REPORT_DESCRIPTOR)
.description("Consumer Control")
.in_endpoint(UsbPacketSize::Bytes8, Milliseconds(50))
.unwrap()
.without_out_endpoint()
.build(),
(),
)
}
}
impl<'a, B: UsbBus> InterfaceClass<'a> for ConsumerControlFixedInterface<'a, B> {
delegate! {
to self.inner{
fn report_descriptor(&self) -> &'_ [u8];
fn id(&self) -> InterfaceNumber;
fn write_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()>;
fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&'_ str>;
fn reset(&mut self);
fn set_report(&mut self, data: &[u8]) -> Result<()>;
fn get_report(&mut self, data: &mut [u8]) -> Result<usize>;
fn get_report_ack(&mut self) -> Result<()>;
fn set_idle(&mut self, report_id: u8, value: u8);
fn get_idle(&self, report_id: u8) -> u8;
fn set_protocol(&mut self, protocol: HidProtocol);
fn get_protocol(&self) -> HidProtocol;
}
}
}
impl<'a, B: UsbBus> WrappedInterface<'a, B, RawInterface<'a, B>>
for ConsumerControlFixedInterface<'a, B>
{
fn new(interface: RawInterface<'a, B>, _: ()) -> Self {
Self { inner: interface }
}
}