ads_proto/proto/
command_id.rs1use crate::proto::proto_traits::{ReadFrom, WriteTo};
2
3use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
4use std::io::{self, Read, Write};
5
6#[repr(u16)]
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub enum CommandID {
9 Invalid,
10 ReadDeviceInfo,
11 Read,
12 Write,
13 ReadState,
14 WriteControl,
15 AddDeviceNotification,
16 DeleteDeviceNotification,
17 DeviceNotification,
18 ReadWrite,
19}
20
21impl WriteTo for CommandID {
22 fn write_to<W: Write>(&self, mut wtr: W) -> io::Result<()> {
23 wtr.write_u16::<LittleEndian>(*self as u16)?;
24 Ok(())
25 }
26}
27
28impl ReadFrom for CommandID {
29 fn read_from<R: Read>(read: &mut R) -> io::Result<Self> {
30 Ok(CommandID::from(read.read_u16::<LittleEndian>()?))
31 }
32}
33
34impl From<u16> for CommandID {
35 fn from(command_value: u16) -> Self {
36 match command_value {
37 0 => CommandID::Invalid,
38 1 => CommandID::ReadDeviceInfo,
39 2 => CommandID::Read,
40 3 => CommandID::Write,
41 4 => CommandID::ReadState,
42 5 => CommandID::WriteControl,
43 6 => CommandID::AddDeviceNotification,
44 7 => CommandID::DeleteDeviceNotification,
45 8 => CommandID::DeviceNotification,
46 9 => CommandID::ReadWrite,
47 _ => CommandID::Invalid,
48 }
49 }
50}
51
52impl CommandID {
53 pub fn as_u16(&self) -> u16 {
54 *self as u16
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 #[test]
62 fn command_id_write_to_test() {
63 let command = CommandID::AddDeviceNotification;
64 let mut buffer: Vec<u8> = Vec::new();
65 command.write_to(&mut buffer).unwrap();
66 assert_eq!(buffer, [6, 0]);
67 }
68
69 #[test]
70 fn command_id_write_to2_test() {
71 let mut buffer: Vec<u8> = Vec::new();
72 CommandID::Read.write_to(&mut buffer).unwrap();
73 assert_eq!(buffer, [2, 0]);
74 }
75
76 #[test]
77 fn command_id_read_from_test() {
78 let data: Vec<u8> = vec![3, 0];
79 let command_id = CommandID::read_from(&mut data.as_slice()).unwrap();
80 assert_eq!(command_id, CommandID::Write);
81 }
82
83 #[test]
84 fn command_id_as_u16_test() {
85 assert_eq!(CommandID::Invalid.as_u16(), 0);
86 assert_eq!(CommandID::ReadDeviceInfo.as_u16(), 1);
87 assert_eq!(CommandID::Read.as_u16(), 2);
88 assert_eq!(CommandID::Write.as_u16(), 3);
89 assert_eq!(CommandID::ReadState.as_u16(), 4);
90 assert_eq!(CommandID::WriteControl.as_u16(), 5);
91 assert_eq!(CommandID::AddDeviceNotification.as_u16(), 6);
92 assert_eq!(CommandID::DeleteDeviceNotification.as_u16(), 7);
93 assert_eq!(CommandID::DeviceNotification.as_u16(), 8);
94 assert_eq!(CommandID::ReadWrite.as_u16(), 9);
95 }
96
97 #[test]
98 fn command_id_from_test() {
99 assert_eq!(CommandID::from(0), CommandID::Invalid);
100 assert_eq!(CommandID::from(1), CommandID::ReadDeviceInfo);
101 assert_eq!(CommandID::from(2), CommandID::Read);
102 assert_eq!(CommandID::from(3), CommandID::Write);
103 assert_eq!(CommandID::from(4), CommandID::ReadState);
104 assert_eq!(CommandID::from(5), CommandID::WriteControl);
105 assert_eq!(CommandID::from(6), CommandID::AddDeviceNotification);
106 assert_eq!(CommandID::from(7), CommandID::DeleteDeviceNotification);
107 assert_eq!(CommandID::from(8), CommandID::DeviceNotification);
108 assert_eq!(CommandID::from(9), CommandID::ReadWrite);
109 assert_eq!(CommandID::from(9654), CommandID::Invalid);
110 }
111}