jacdac_rs/service/
control.rs1use alloc::vec::Vec;
2
3use super::{
4 reports::{ActionReport, EventReport},
5 serivce_error::ServiceError,
6 service::Service,
7};
8
9#[derive(Debug, Default)]
10pub struct Control {
11 flags: u16,
12 classes: Vec<u32>,
13}
14
15impl Control {
16 pub fn classes(&self) -> &Vec<u32> {
17 &self.classes
18 }
19
20 fn buffer_to_classes(buffer: &[u8]) -> Vec<u32> {
21 let mut result = Vec::with_capacity(buffer.len() / 4);
22
23 for i in (0..(buffer.len())).step_by(4) {
24 if i + 4 > buffer.len() {
25 break;
26 }
27
28 result.push(u32::from_le_bytes(buffer[i..(i + 4)].try_into().unwrap()));
29 }
30
31 result
32 }
33}
34
35impl Service for Control {
36 fn handle_event_report(&mut self, _event: EventReport) -> Result<(), ServiceError> {
39 unimplemented!()
40 }
41
42 fn handle_action_report(&mut self, action: ActionReport) -> Result<(), ServiceError> {
43 if action.code != 0 {
44 return Err(ServiceError::UnknownEventCode);
45 }
46
47 if action.payload.len() < 8 {
48 return Err(ServiceError::InvalidPayloadSize(action.payload.len(), 8));
49 }
50
51 self.flags = u16::from_le_bytes(action.payload[0..=1].try_into().unwrap());
52 self.classes = Self::buffer_to_classes(&action.payload[4..]);
54
55 Ok(())
56 }
57}