ross_protocol/event/
configurator.rs1use alloc::vec;
2use core::convert::TryInto;
3
4use crate::convert_packet::{ConvertPacket, ConvertPacketError};
5use crate::event::event_code::*;
6use crate::event::EventError;
7use crate::packet::Packet;
8use crate::protocol::BROADCAST_ADDRESS;
9
10#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
11pub struct ConfiguratorHelloEvent {}
12
13impl ConvertPacket<ConfiguratorHelloEvent> for ConfiguratorHelloEvent {
14 fn try_from_packet(packet: &Packet) -> Result<Self, ConvertPacketError> {
15 if packet.data.len() != 2 {
16 return Err(ConvertPacketError::WrongSize);
17 }
18
19 if packet.is_error {
20 return Err(ConvertPacketError::WrongType);
21 }
22
23 if u16::from_be_bytes(packet.data[0..=1].try_into().unwrap())
24 != CONFIGURATOR_HELLO_EVENT_CODE
25 {
26 return Err(ConvertPacketError::Event(EventError::WrongEventType));
27 }
28
29 Ok(ConfiguratorHelloEvent {})
30 }
31
32 fn to_packet(&self) -> Packet {
33 let mut data = vec![];
34
35 for byte in u16::to_be_bytes(CONFIGURATOR_HELLO_EVENT_CODE).iter() {
36 data.push(*byte);
37 }
38
39 Packet {
40 is_error: false,
41 device_address: BROADCAST_ADDRESS,
42 data,
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 const EVENT_PACKET: Packet = Packet {
52 is_error: false,
53 device_address: 0x0000,
54 data: vec![],
55 };
56
57 #[test]
58 fn try_from_packet_test() {
59 let mut packet = EVENT_PACKET;
60 packet.data = vec![
61 ((CONFIGURATOR_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((CONFIGURATOR_HELLO_EVENT_CODE >> 0) & 0xff) as u8, ];
64
65 ConfiguratorHelloEvent::try_from_packet(&packet).unwrap();
66 }
67
68 #[test]
69 fn to_packet_test() {
70 let event = ConfiguratorHelloEvent {};
71
72 let mut packet = EVENT_PACKET;
73 packet.device_address = BROADCAST_ADDRESS;
74 packet.data = vec![
75 ((CONFIGURATOR_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((CONFIGURATOR_HELLO_EVENT_CODE >> 0) & 0xff) as u8, ];
78
79 assert_eq!(event.to_packet(), packet);
80 }
81}