use crate::std;
use std::fmt;
use crate::{
impl_message_ops, impl_omnibus_nop_reply, len::FLASH_DOWNLOAD_REPLY_8BIT, MessageOps,
MessageType,
};
use super::FlashDownloadReply;
pub mod index {
pub const PACKET0: usize = 3;
pub const PACKET1: usize = 4;
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FlashDownloadReply8bit {
buf: [u8; FLASH_DOWNLOAD_REPLY_8BIT],
}
impl FlashDownloadReply8bit {
pub fn new() -> Self {
let mut msg = Self {
buf: [0u8; FLASH_DOWNLOAD_REPLY_8BIT],
};
msg.init();
msg.set_message_type(MessageType::FirmwareDownload);
msg
}
}
impl_message_ops!(FlashDownloadReply8bit);
impl_omnibus_nop_reply!(FlashDownloadReply8bit);
impl FlashDownloadReply for FlashDownloadReply8bit {
fn packet_number(&self) -> u16 {
u16::from_le_bytes([self.buf[index::PACKET0], self.buf[index::PACKET1]])
}
fn set_packet_number(&mut self, n: u16) {
self.buf[index::PACKET0..=index::PACKET1].copy_from_slice(n.to_le_bytes().as_ref());
}
}
impl fmt::Display for FlashDownloadReply8bit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AckNak: {}, DeviceType: {}, MessageType: {}, PacketNumber: {}",
self.acknak(),
self.device_type(),
self.message_type(),
self.packet_number(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Result;
#[test]
#[rustfmt::skip]
fn flash_download_reply_8bit_from_buf() -> Result<()> {
let msg_bytes = [
0x02, 0x07, 0x50,
0x34, 0x12,
0x03, 0x71,
];
let mut msg = FlashDownloadReply8bit::new();
msg.from_buf(msg_bytes.as_ref())?;
assert_eq!(msg.message_type(), MessageType::FirmwareDownload);
assert_eq!(msg.packet_number(), 0x1234);
Ok(())
}
}