ross_protocol/event/
bootloader.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;
8
9#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
10pub struct BootloaderHelloEvent {
11 pub programmer_address: u16,
12 pub bootloader_address: u16,
13}
14
15impl ConvertPacket<BootloaderHelloEvent> for BootloaderHelloEvent {
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()) != BOOTLOADER_HELLO_EVENT_CODE
26 {
27 return Err(ConvertPacketError::Event(EventError::WrongEventType));
28 }
29
30 let programmer_address = packet.device_address;
31 let bootloader_address = u16::from_be_bytes(packet.data[2..=3].try_into().unwrap());
32
33 Ok(BootloaderHelloEvent {
34 programmer_address,
35 bootloader_address,
36 })
37 }
38
39 fn to_packet(&self) -> Packet {
40 let mut data = vec![];
41
42 for byte in u16::to_be_bytes(BOOTLOADER_HELLO_EVENT_CODE).iter() {
43 data.push(*byte);
44 }
45
46 for byte in u16::to_be_bytes(self.bootloader_address).iter() {
47 data.push(*byte);
48 }
49
50 Packet {
51 is_error: false,
52 device_address: self.programmer_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 try_from_packet_test() {
70 let mut packet = EVENT_PACKET;
71 packet.data = vec![
72 ((BOOTLOADER_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((BOOTLOADER_HELLO_EVENT_CODE >> 0) & 0xff) as u8, 0x01, 0x23, ];
77
78 let event = BootloaderHelloEvent::try_from_packet(&packet).unwrap();
79
80 assert_eq!(event.programmer_address, 0xabab);
81 assert_eq!(event.bootloader_address, 0x0123);
82 }
83
84 #[test]
85 #[should_panic]
86 fn try_from_packet_wrong_size_test() {
87 let mut packet = EVENT_PACKET;
88 packet.data = vec![
89 ((BOOTLOADER_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((BOOTLOADER_HELLO_EVENT_CODE >> 0) & 0xff) as u8, 0x01, 0x23, 0x00, ];
95
96 BootloaderHelloEvent::try_from_packet(&packet).unwrap();
97 }
98
99 #[test]
100 #[should_panic]
101 fn try_from_packet_wrong_type_test() {
102 let mut packet = EVENT_PACKET;
103 packet.data = vec![
104 ((BOOTLOADER_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((BOOTLOADER_HELLO_EVENT_CODE >> 0) & 0xff) as u8, 0x01, 0x23, ];
109 packet.is_error = true;
110
111 BootloaderHelloEvent::try_from_packet(&packet).unwrap();
112 }
113
114 #[test]
115 #[should_panic]
116 fn try_from_packet_wrong_event_type_test() {
117 let mut packet = EVENT_PACKET;
118 packet.data = vec![
119 ((PROGRAMMER_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((PROGRAMMER_HELLO_EVENT_CODE >> 0) & 0xff) as u8, 0xab, 0xab, ];
124
125 BootloaderHelloEvent::try_from_packet(&packet).unwrap();
126 }
127
128 #[test]
129 fn to_packet_test() {
130 let event = BootloaderHelloEvent {
131 programmer_address: 0xabab,
132 bootloader_address: 0x0123,
133 };
134
135 let mut packet = EVENT_PACKET;
136 packet.data = vec![
137 ((BOOTLOADER_HELLO_EVENT_CODE >> 8) & 0xff) as u8, ((BOOTLOADER_HELLO_EVENT_CODE >> 0) & 0xff) as u8, 0x01, 0x23, ];
142
143 assert_eq!(event.to_packet(), packet);
144 }
145}