ross_protocol/event/
gateway.rs

1use 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;
8
9#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
10pub struct GatewayDiscoverEvent {
11    pub device_address: u16,
12    pub gateway_address: u16,
13}
14
15impl ConvertPacket<GatewayDiscoverEvent> for GatewayDiscoverEvent {
16    fn try_from_packet(packet: &Packet) -> Result<Self, ConvertPacketError> {
17        if packet.data.len() != 4 {
18            return Err(ConvertPacketError::WrongSize);
19        }
20
21        if packet.is_error {
22            return Err(ConvertPacketError::WrongType);
23        }
24
25        if u16::from_be_bytes(packet.data[0..=1].try_into().unwrap()) != GATEWAY_DISCOVER_EVENT_CODE
26        {
27            return Err(ConvertPacketError::Event(EventError::WrongEventType));
28        }
29
30        let device_address = packet.device_address;
31        let gateway_address = u16::from_be_bytes(packet.data[2..=3].try_into().unwrap());
32
33        Ok(Self {
34            device_address,
35            gateway_address,
36        })
37    }
38
39    fn to_packet(&self) -> Packet {
40        let mut data = vec![];
41
42        for byte in u16::to_be_bytes(GATEWAY_DISCOVER_EVENT_CODE).iter() {
43            data.push(*byte);
44        }
45
46        for byte in u16::to_be_bytes(self.gateway_address).iter() {
47            data.push(*byte);
48        }
49
50        Packet {
51            is_error: false,
52            device_address: self.device_address,
53            data,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    const EVENT_PACKET: Packet = Packet {
63        is_error: false,
64        device_address: 0xabab,
65        data: vec![],
66    };
67
68    #[test]
69    fn discover_try_from_packet_test() {
70        let mut packet = EVENT_PACKET;
71        packet.data = vec![
72            ((GATEWAY_DISCOVER_EVENT_CODE >> 8) & 0xff) as u8, // event code
73            ((GATEWAY_DISCOVER_EVENT_CODE >> 0) & 0xff) as u8, // event code
74            0x00,                                              // gateway address
75            0x00,                                              // gateway address
76        ];
77
78        let event = GatewayDiscoverEvent::try_from_packet(&packet).unwrap();
79
80        assert_eq!(event.device_address, 0xabab);
81        assert_eq!(event.gateway_address, 0x0000);
82    }
83
84    #[test]
85    fn discover_to_packet_test() {
86        let event = GatewayDiscoverEvent {
87            device_address: 0xabab,
88            gateway_address: 0x0000,
89        };
90
91        let mut packet = EVENT_PACKET;
92        packet.data = vec![
93            ((GATEWAY_DISCOVER_EVENT_CODE >> 8) & 0xff) as u8, // event code
94            ((GATEWAY_DISCOVER_EVENT_CODE >> 0) & 0xff) as u8, // event code
95            0x00,                                              // gateway address
96            0x00,                                              // gateway address
97        ];
98
99        assert_eq!(event.to_packet(), packet);
100    }
101}