1pub mod commands;
2mod error;
3use commands::Command;
4use error::EldritchError;
5use std::fmt::Debug;
6
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[derive(Clone, Debug, PartialEq)]
9pub struct AddressedCommand {
10 pub device_id: u8,
11 pub command: Command,
12 #[cfg(feature = "debug")]
13 pub command_data: Vec<u8>,
14}
15
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[derive(Clone, PartialEq, PartialOrd)]
18pub struct FixedPointDecimal {
19 raw_val: i16,
20}
21
22impl FixedPointDecimal {
23 pub fn get_real_val(&self) -> f32 {
24 f32::from(self.raw_val) / 2_f32.powi(11)
25 }
26
27 pub fn get_rounded_val(&self) -> f32 {
28 (self.get_real_val() * 100.0).round() / 100.0
29 }
30
31 pub fn from_data(data: &[u8; 2]) -> Self {
32 assert!(data.len() == 2);
33 Self {
34 raw_val: u16::from_le_bytes(*data) as i16,
35 }
36 }
37}
38
39impl Debug for FixedPointDecimal {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(
42 f,
43 "FixedPointDecimal {{ raw_val: {}, real_val: {} }}",
44 self.raw_val,
45 self.get_real_val()
46 )
47 }
48}
49
50impl PartialEq<f32> for FixedPointDecimal {
51 fn eq(&self, other: &f32) -> bool {
52 &self.get_real_val() == other
53 }
54}
55
56impl PartialOrd<f32> for FixedPointDecimal {
57 fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
58 let real_val = self.get_real_val();
59 if &real_val == other {
60 Some(std::cmp::Ordering::Equal)
61 } else if &real_val < other {
62 Some(std::cmp::Ordering::Less)
63 } else {
64 Some(std::cmp::Ordering::Greater)
65 }
66 }
67}
68
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70#[derive(Clone, Debug, PartialEq)]
71pub enum Operation {
72 Assign,
73 Increment,
74 Toggle,
75}
76
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78#[derive(Clone, Debug, PartialEq)]
79struct CommandHeader {
80 device_id: u8,
81 command_length: u8,
82 command_id: u8,
83}
84
85#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
86#[derive(Debug, PartialEq)]
87struct PacketData {
88 data: Vec<u8>,
89 cursor: u8,
90}
91
92impl PacketData {
93 pub fn new(packet_data: Vec<u8>) -> Result<Self, EldritchError> {
94 if packet_data.len() > 255 {
95 Err(EldritchError::PacketToLarge)
96 } else {
97 Ok(Self {
98 data: packet_data,
99 cursor: 0,
100 })
101 }
102 }
103
104 pub fn parse_header(&mut self) -> Result<CommandHeader, EldritchError> {
105 let device_id = self
106 .data
107 .get(self.cursor as usize)
108 .ok_or(EldritchError::InvalidHeader)?;
109 let command_length = self
110 .data
111 .get((self.cursor + 1) as usize)
112 .ok_or(EldritchError::InvalidHeader)?;
113 let command_id = self
114 .data
115 .get((self.cursor + 2) as usize)
116 .ok_or(EldritchError::InvalidHeader)?;
117
118 let header = if let Some(reserved) = self.data.get((self.cursor + 3) as usize) {
119 if *reserved == 0 {
120 Ok(CommandHeader {
121 device_id: *device_id,
122 command_length: *command_length,
123 command_id: *command_id,
124 })
125 } else {
126 Err(EldritchError::InvalidHeader)
127 }
128 } else {
129 Err(EldritchError::InvalidHeader)
130 };
131 self.cursor += 4;
132 header
133 }
134
135 fn has_data(&self) -> bool {
136 usize::from(self.cursor) < self.data.len()
137 }
138
139 fn get_slice(&mut self, slice_len: u8) -> Result<&[u8], EldritchError> {
140 let new_cur = self.cursor + slice_len;
141 if usize::from(new_cur) > self.data.len() {
142 return Err(EldritchError::EndOfPacket);
143 }
144 let slice_data = &self.data[usize::from(self.cursor)..usize::from(new_cur)];
145 self.cursor = new_cur;
146 Ok(slice_data)
147 }
148}
149
150pub fn parse_frame_packet(data: Vec<u8>) -> Result<Vec<AddressedCommand>, EldritchError> {
151 let mut packet = PacketData::new(data)?;
152 let mut commands: Vec<AddressedCommand> = Vec::new();
153
154 while packet.has_data() {
155 let header = packet.parse_header()?;
156 match packet.get_slice(header.command_length) {
158 Ok(command_data) => {
159 commands.push(AddressedCommand {
161 device_id: header.device_id,
162 command: commands::parse_command(command_data)?,
163 #[cfg(feature = "debug")]
164 command_data: Vec::from(command_data),
165 });
166 }
167 Err(EldritchError::EndOfPacket) => break,
168 Err(err) => Err(err)?,
169 };
170
171 match packet.get_slice(calculate_padding_length(header.command_length)) {
172 Ok(padding) => verify_padding(padding, header.command_length)?,
173 Err(EldritchError::EndOfPacket) => break,
174 Err(err) => Err(err)?,
175 };
176 }
177
178 Ok(commands)
179}
180
181fn calculate_padding_length(command_length: u8) -> u8 {
182 if command_length.is_multiple_of(4) {
183 0
184 } else {
185 4 - (command_length % 4)
186 }
187}
188
189fn verify_padding(padding: &[u8], command_length: u8) -> Result<(), EldritchError> {
190 if !(padding.len() + usize::from(command_length)).is_multiple_of(4) {
191 return Err(EldritchError::PaddingViolation(String::from(
192 "Padding length is incorrect",
193 )));
194 }
195
196 let mut padding_errors: Vec<Result<(), EldritchError>> = padding
197 .iter()
198 .enumerate()
199 .map(|(idx, byte)| {
200 if *byte != 0x00 {
201 Err(EldritchError::PaddingViolation(format!(
202 "Padding byte at index: {idx} is not 0x00",
203 )))
204 } else {
205 Ok(())
206 }
207 })
208 .filter(|check| check.is_err())
209 .collect();
210
211 if !padding_errors.is_empty() {
212 padding_errors.remove(0)
213 } else {
214 Ok(())
215 }
216}
217
218#[cfg(test)]
219mod fixed_point_test {
220 use super::*;
221
222 #[test]
223 fn fpd_fifteen_percent() {
224 let fifteen_percent = FixedPointDecimal { raw_val: 0x0133 };
225 let float_value = fifteen_percent.get_rounded_val();
226 assert_eq!(float_value, 0.15);
227 }
228
229 #[test]
230 fn fpd_minus_point_three() {
231 let minus_point_three = FixedPointDecimal {
232 raw_val: 0xfd9au16 as i16,
233 };
234 let float_value = minus_point_three.get_rounded_val();
235 assert_eq!(float_value, -0.3_f32);
236 }
237}
238
239#[cfg(test)]
240mod packet_data_test {
241 use crate::commands::{
242 color_correction_commands::{ColorCorrectionCommand, RedGreenBlueLuma},
243 display_commands::DisplayCommand,
244 lens_commands::LensCommand,
245 video_commands::{VideoCommand, VideoModeData},
246 };
247
248 use super::*;
249
250 #[test]
251 fn new() {
252 let packet_data = vec![
253 0x00, 0x05, 0x00, 0x00, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
257
258 if let Ok(packet) = PacketData::new(packet_data) {
259 assert_eq!(packet.cursor, 0);
260 assert_eq!(packet.data.len(), 12);
261 } else {
262 panic!();
263 }
264 }
265
266 #[test]
267 fn parse_header() {
268 let packet_data = vec![
269 0x00, 0x05, 0x00, 0x00, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
273
274 let mut packet =
275 PacketData::new(packet_data).expect("Failed to build PacketData with known good data");
276
277 if let Ok(header) = packet.parse_header() {
278 assert_eq!(
279 header,
280 CommandHeader {
281 device_id: 0,
282 command_length: 5,
283 command_id: 0
284 }
285 );
286 } else {
287 panic!();
288 }
289 }
290
291 #[test]
292 fn parse_header_bad_reserved_byte() {
293 let packet_data = vec![
294 0x00, 0x05, 0x00, 0xFF, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
298
299 let mut packet = PacketData::new(packet_data).expect("Verified packet data");
300
301 if let Err(error) = packet.parse_header() {
302 assert_eq!(error, EldritchError::InvalidHeader);
303 assert_eq!(4, packet.cursor);
304 } else {
305 panic!();
306 }
307 }
308
309 #[test]
310 fn has_data_true() {
311 let packet_data = vec![
312 0x00, 0x05, 0x00, 0xFF, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
316
317 let mut packet = PacketData::new(packet_data).expect("Verified packet data");
318 packet.cursor = 10;
319 println!("Packet: {:?}", packet);
320 assert!(packet.has_data());
321 }
322
323 #[test]
324 fn has_data_false() {
325 let packet_data = vec![
326 0x00, 0x05, 0x00, 0xFF, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
330
331 let mut packet = PacketData::new(packet_data).expect("Verified packet data");
332 packet.cursor = 12;
333 println!("Packet: {:?}", packet);
334 assert!(!packet.has_data());
335 }
336
337 #[test]
338 fn get_slice() {
339 let packet_data = vec![
340 0x00, 0x05, 0x00, 0xFF, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
344 let mut packet = PacketData::new(packet_data).expect("Verified packet data");
345 packet.cursor = 4;
346
347 if let Ok(cmd_data) = packet.get_slice(5) {
348 assert_eq!([0x00, 0x80, 0x01, 0x9a, 0xfd,] as [u8; 5], cmd_data);
349 assert_eq!(9, packet.cursor);
350 } else {
351 panic!();
352 }
353 if let Ok(padding) = packet.get_slice(calculate_padding_length(5)) {
354 assert_eq!([0x00, 0x00, 0x00] as [u8; 3], padding);
355 assert_eq!(12, packet.cursor);
356 } else {
357 panic!();
358 }
359 }
360
361 #[test]
362 fn get_slice_error() {
363 let packet_data = vec![
364 0x00, 0x05, 0x00, 0xFF, 0x00, 0x80, 0x01, 0x9a, 0xfd, 0x00, 0x00, 0x00, ];
368 let mut packet = PacketData::new(packet_data).expect("Verified packet data");
369 packet.cursor = 8;
370
371 if let Err(error) = packet.get_slice(5) {
372 assert_eq!(EldritchError::EndOfPacket, error);
373 } else {
374 panic!();
375 }
376 }
377
378 #[test]
379 fn parse_large_packet() {
380 let frame_packet = vec![
381 0x04, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xff, 0x05, 0x00, 0x00, 0x00, 0x06,
382 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x01, 0x05, 0x03, 0x00,
383 0x10, 0x27, 0x00, 0x00, 0x04, 0x06, 0x00, 0x00, 0x04, 0x02, 0x80, 0x01, 0x33, 0x01,
384 0x00, 0x00, 0xff, 0x09, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x18, 0x01, 0x03, 0x00,
385 0x00, 0x00, 0x00, 0x00, 0x04, 0x0c, 0x00, 0x00, 0x08, 0x01, 0x80, 0x01, 0x00, 0x00,
386 0x9a, 0xfd, 0x9a, 0xfd, 0x00, 0x00,
387 ];
388 let commands = parse_frame_packet(frame_packet).expect("Test Frame should parse");
389 let mut commands = commands.iter();
390
391 assert_eq!(
392 commands.next(),
393 Some(AddressedCommand {
394 device_id: 4,
395 command: Command::Lens(LensCommand::InstantaneousAutoFocus),
396 })
397 .as_ref()
398 );
399
400 assert_eq!(
401 commands.next(),
402 Some(AddressedCommand {
403 device_id: 255,
404 command: Command::Lens(LensCommand::OpticalImageStabalization {
405 operation: Operation::Assign,
406 data: true
407 })
408 })
409 .as_ref()
410 );
411
412 assert_eq!(
413 commands.next(),
414 Some(AddressedCommand {
415 device_id: 4,
416 command: Command::Video(VideoCommand::ExposureUS {
417 operation: Operation::Assign,
418 data: 10000
419 })
420 })
421 .as_ref()
422 );
423
424 assert_eq!(
425 commands.next(),
426 Some(AddressedCommand {
427 device_id: 4,
428 command: Command::Display(DisplayCommand::ZebraLevel {
429 operation: Operation::Increment,
430 data: FixedPointDecimal {
431 raw_val: 0x0133u16 as i16
432 }
433 })
434 })
435 .as_ref()
436 );
437
438 assert_eq!(
439 commands.next(),
440 Some(AddressedCommand {
441 device_id: 255,
442 command: Command::Video(VideoCommand::VideoMode {
443 operation: Operation::Assign,
444 data: VideoModeData {
445 frame_rate: 24,
446 m_rate: 1,
447 dimensions: 3,
448 interlaced: 0,
449 color_space: 0,
450 }
451 })
452 })
453 .as_ref()
454 );
455
456 assert_eq!(
457 commands.next(),
458 Some(AddressedCommand {
459 device_id: 4,
460 command: Command::ColorCorrection(ColorCorrectionCommand::GammaAdjust {
461 operation: Operation::Increment,
462 data: RedGreenBlueLuma {
463 red: FixedPointDecimal { raw_val: 0x00 },
464 green: FixedPointDecimal {
465 raw_val: 0xfd9au16 as i16
466 },
467 blue: FixedPointDecimal {
468 raw_val: 0xfd9au16 as i16
469 },
470 luma: FixedPointDecimal { raw_val: 0x00 },
471 }
472 })
473 })
474 .as_ref()
475 );
476 }
477
478 #[test]
479 fn long_real_packet_one() {
480 let cmd_data: [u8; 243] = [
481 0x01, 0x06, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x8D, 0x1C, 0x00, 0x00, 0x01, 0x06,
482 0x00, 0x00, 0x01, 0x10, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00,
483 0x01, 0x0D, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x01, 0x01,
484 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x01, 0x02, 0x02, 0x00,
485 0x04, 0x10, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x01, 0x05, 0x03, 0x00, 0x1B, 0x41,
486 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x01, 0x08, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
487 0x01, 0x05, 0x00, 0x00, 0x04, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0C,
488 0x00, 0x00, 0x08, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00,
489 0x01, 0x0C, 0x00, 0x00, 0x08, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490 0x00, 0x00, 0x01, 0x0C, 0x00, 0x00, 0x08, 0x02, 0x80, 0x00, 0x00, 0x08, 0x00, 0x08,
491 0x00, 0x08, 0xCE, 0x07, 0x01, 0x0C, 0x00, 0x00, 0x08, 0x03, 0x80, 0x00, 0x00, 0x00,
492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x08, 0x04, 0x80, 0x00,
493 0x00, 0x04, 0x00, 0x08, 0x01, 0x06, 0x00, 0x00, 0x08, 0x05, 0x80, 0x00, 0x00, 0x08,
494 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x08, 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08,
495 0x02, 0x06, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x9B, 0x12, 0x00, 0x00, 0x02, 0x06,
496 0x00, 0x00, 0x01, 0x10, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00,
497 0x01, 0x0D, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x01, 0x01,
498 0x01, 0x00, 0x08, 0x00, 0x00,
499 ];
500 let result = parse_frame_packet(cmd_data.to_vec());
501 let _commands = result.unwrap();
502 }
503
504 #[test]
505 fn long_real_packet_two() {
506 let cmd_data: [u8; 251] = [
507 0x03, 0x0C, 0x00, 0x00, 0x08, 0x02, 0x80, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08,
508 0x00, 0x08, 0x03, 0x0C, 0x00, 0x00, 0x08, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
509 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, 0x08, 0x04, 0x80, 0x00, 0x00, 0x04,
510 0x00, 0x08, 0x03, 0x06, 0x00, 0x00, 0x08, 0x05, 0x80, 0x00, 0x00, 0x08, 0x00, 0x00,
511 0x03, 0x08, 0x00, 0x00, 0x08, 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08, 0x04, 0x06,
512 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0xF2, 0x15, 0x00, 0x00, 0x04, 0x06, 0x00, 0x00,
513 0x01, 0x10, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x05, 0x00, 0x00, 0x01, 0x0D,
514 0x01, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x04, 0x05, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00,
515 0x02, 0x00, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x01, 0x02, 0x02, 0x00, 0x04, 0x10,
516 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x01, 0x05, 0x03, 0x00, 0x1B, 0x41, 0x00, 0x00,
517 0x04, 0x05, 0x00, 0x00, 0x01, 0x08, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x05,
518 0x00, 0x00, 0x04, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x0C, 0x00, 0x00,
519 0x08, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x0C,
520 0x00, 0x00, 0x08, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
521 0x04, 0x0C, 0x00, 0x00, 0x08, 0x02, 0x80, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08,
522 0x00, 0x08, 0x04, 0x0C, 0x00, 0x00, 0x08, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
523 0x00, 0x00, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x08, 0x04, 0x80, 0x00, 0x00, 0x04,
524 0x00, 0x08, 0x04, 0x06, 0x00, 0x00, 0x08, 0x05, 0x80, 0x00, 0x00, 0x08, 0x00,
525 ];
526 let result = parse_frame_packet(cmd_data.to_vec());
527 let _commands = result.unwrap();
528 }
529
530 #[test]
531 fn long_real_packet_three() {
532 let cmd_data: [u8; 247] = [
533 0x10, 0x05, 0x00, 0x00, 0x04, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0C,
534 0x00, 0x00, 0x08, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
535 0x10, 0x0C, 0x00, 0x00, 0x08, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
536 0x00, 0x00, 0x10, 0x0C, 0x00, 0x00, 0x08, 0x02, 0x80, 0x00, 0x00, 0x08, 0x00, 0x08,
537 0x00, 0x08, 0x00, 0x08, 0x10, 0x0C, 0x00, 0x00, 0x08, 0x03, 0x80, 0x00, 0x00, 0x00,
538 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x08, 0x04, 0x80, 0x00,
539 0x00, 0x04, 0x00, 0x08, 0x10, 0x06, 0x00, 0x00, 0x08, 0x05, 0x80, 0x00, 0x00, 0x08,
540 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x08, 0x06, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08,
541 0x11, 0x06, 0x00, 0x00, 0x01, 0x10, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x05,
542 0x00, 0x00, 0x01, 0x0D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x05, 0x00, 0x00,
543 0x01, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x11, 0x08, 0x00, 0x00, 0x01, 0x05,
544 0x03, 0x00, 0x20, 0x4E, 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x01, 0x08, 0x01, 0x00,
545 0x01, 0x00, 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x04, 0x04, 0x01, 0x00, 0x00, 0x00,
546 0x00, 0x00, 0x11, 0x0C, 0x00, 0x00, 0x08, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
547 0x00, 0x00, 0x00, 0x00, 0x11, 0x0C, 0x00, 0x00, 0x08, 0x01, 0x80, 0x00, 0x00, 0x00,
548 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x0C, 0x00, 0x00, 0x08, 0x02, 0x80, 0x00,
549 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x11, 0x0C, 0x00, 0x00, 0x08, 0x03,
550 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
551 ];
552 let result = parse_frame_packet(cmd_data.to_vec());
553 let _commands = result.unwrap();
554 }
555
556 #[test]
557 fn short_real_packet_one() {
558 let cmd_data: [u8; 23] = [
559 0x02, 0x06, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x64, 0x20, 0x00, 0x00, 0x02, 0x06,
560 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x07, 0x21, 0x00,
561 ];
562 let result = parse_frame_packet(cmd_data.to_vec());
563 let _commands = result.unwrap();
564 }
565}
566
567#[cfg(test)]
568mod lib_test {
569 use super::*;
570 use crate::commands::{lens_commands::LensCommand, Command};
571
572 #[test]
573 fn parse_packet_single_command() {
574 let packet_data = vec![
575 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x33, 0x01, 0x00, 0x00, ];
579
580 if let Ok(commands) = parse_frame_packet(packet_data) {
581 println!("parse_packet_single_command() commands: {:?}", commands);
582 assert_eq!(1, commands.len());
583 assert_eq!(
584 AddressedCommand {
585 device_id: 0,
586 command: Command::Lens(LensCommand::Focus {
587 operation: Operation::Increment,
588 data: FixedPointDecimal {
589 raw_val: 0x0133u16 as i16
590 }
591 })
592 },
593 *commands.first().expect("Length asserted to be one")
594 );
595 } else {
596 panic!();
597 }
598 }
599
600 #[test]
601 fn parse_packet_two_commands() {
602 let packet_data = vec![
603 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x33, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x33, 0x01, 0x00, 0x00, ];
610
611 if let Ok(commands) = parse_frame_packet(packet_data) {
612 println!("parse_packet_single_command() commands: {:?}", commands);
613 assert_eq!(2, commands.len());
614 assert_eq!(
615 AddressedCommand {
616 device_id: 0,
617 command: Command::Lens(LensCommand::Focus {
618 operation: Operation::Increment,
619 data: FixedPointDecimal {
620 raw_val: 0x0133u16 as i16
621 }
622 })
623 },
624 *commands
625 .first()
626 .expect("Length asserted to be more then one")
627 );
628 assert_eq!(
629 AddressedCommand {
630 device_id: 0,
631 command: Command::Lens(LensCommand::Focus {
632 operation: Operation::Increment,
633 data: FixedPointDecimal {
634 raw_val: 0x0133u16 as i16
635 }
636 })
637 },
638 *commands.get(1).expect("Length asserted to be two")
639 );
640 } else {
641 panic!();
642 }
643 }
644
645 #[test]
646 fn calculate_padding_length_no_padding() {
647 assert_eq!(0_u8, calculate_padding_length(8));
648 assert_eq!(0_u8, calculate_padding_length(4));
649 }
650
651 #[test]
652 fn calculate_padding_length_one() {
653 assert_eq!(1_u8, calculate_padding_length(3));
654 assert_eq!(1_u8, calculate_padding_length(7));
655 }
656
657 #[test]
658 fn calculate_padding_length_two() {
659 assert_eq!(2_u8, calculate_padding_length(2));
660 assert_eq!(2_u8, calculate_padding_length(6));
661 }
662
663 #[test]
664 fn calculate_padding_length_three() {
665 assert_eq!(3_u8, calculate_padding_length(1));
666 assert_eq!(3_u8, calculate_padding_length(5));
667 }
668
669 #[test]
670 fn verify_padding_success() {
671 let padding: [u8; 3] = [0x00; 3];
672 let command_length = 5;
673
674 assert_eq!(Ok(()), verify_padding(&padding, command_length));
675 }
676
677 #[test]
678 fn verify_padding_too_short() {
679 let padding: [u8; 2] = [0x00; 2];
680 let command_length = 5;
681
682 assert_eq!(
683 Err(EldritchError::PaddingViolation(String::from(
684 "Padding length is incorrect"
685 ))),
686 verify_padding(&padding, command_length)
687 );
688 }
689
690 #[test]
691 fn verify_padding_nonzero_byte_idx_0() {
692 let padding: [u8; 3] = [0xff, 0x00, 0x00];
693 let command_length = 5;
694
695 assert_eq!(
696 Err(EldritchError::PaddingViolation(String::from(
697 "Padding byte at index: 0 is not 0x00"
698 ))),
699 verify_padding(&padding, command_length),
700 )
701 }
702
703 #[test]
704 fn verify_padding_nonzero_byte_idx_2() {
705 let padding: [u8; 3] = [0x00, 0x00, 0xff];
706 let command_length = 5;
707
708 assert_eq!(
709 Err(EldritchError::PaddingViolation(String::from(
710 "Padding byte at index: 2 is not 0x00"
711 ))),
712 verify_padding(&padding, command_length),
713 )
714 }
715}