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
use alloc::vec;
use core::convert::TryInto;
use crate::convert_packet::{ConvertPacket, ConvertPacketError};
use crate::event::event_code::*;
use crate::event::EventError;
use crate::packet::Packet;
use crate::protocol::BROADCAST_ADDRESS;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct ConfiguratorHelloEvent {}
impl ConvertPacket<ConfiguratorHelloEvent> for ConfiguratorHelloEvent {
fn try_from_packet(packet: &Packet) -> Result<Self, ConvertPacketError> {
if packet.data.len() != 2 {
return Err(ConvertPacketError::WrongSize);
}
if packet.is_error {
return Err(ConvertPacketError::WrongType);
}
if u16::from_be_bytes(packet.data[0..=1].try_into().unwrap())
!= CONFIGURATOR_HELLO_EVENT_CODE
{
return Err(ConvertPacketError::Event(EventError::WrongEventType));
}
Ok(ConfiguratorHelloEvent {})
}
fn to_packet(&self) -> Packet {
let mut data = vec![];
for byte in u16::to_be_bytes(CONFIGURATOR_HELLO_EVENT_CODE).iter() {
data.push(*byte);
}
Packet {
is_error: false,
device_address: BROADCAST_ADDRESS,
data,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const EVENT_PACKET: Packet = Packet {
is_error: false,
device_address: 0x0000,
data: vec![],
};
#[test]
fn try_from_packet_test() {
let mut packet = EVENT_PACKET;
packet.data = vec![
((CONFIGURATOR_HELLO_EVENT_CODE >> 8) & 0xff) as u8,
((CONFIGURATOR_HELLO_EVENT_CODE >> 0) & 0xff) as u8,
];
ConfiguratorHelloEvent::try_from_packet(&packet).unwrap();
}
#[test]
fn to_packet_test() {
let event = ConfiguratorHelloEvent {};
let mut packet = EVENT_PACKET;
packet.device_address = BROADCAST_ADDRESS;
packet.data = vec![
((CONFIGURATOR_HELLO_EVENT_CODE >> 8) & 0xff) as u8,
((CONFIGURATOR_HELLO_EVENT_CODE >> 0) & 0xff) as u8,
];
assert_eq!(event.to_packet(), packet);
}
}