use crate::std;
use std::fmt;
use crate::{
impl_message_ops, impl_omnibus_nop_reply, len::FLASH_DOWNLOAD_REPLY_7BIT, seven_bit_u16,
u16_seven_bit, MessageOps, MessageType,
};
use super::FlashDownloadReply;
pub mod index {
pub const PACKET0: usize = 3;
pub const PACKET3: usize = 6;
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FlashDownloadReply7bit {
buf: [u8; FLASH_DOWNLOAD_REPLY_7BIT],
}
impl FlashDownloadReply7bit {
pub fn new() -> Self {
let mut msg = Self {
buf: [0u8; FLASH_DOWNLOAD_REPLY_7BIT],
};
msg.init();
msg.set_message_type(MessageType::FirmwareDownload);
msg
}
}
impl_message_ops!(FlashDownloadReply7bit);
impl_omnibus_nop_reply!(FlashDownloadReply7bit);
impl FlashDownloadReply for FlashDownloadReply7bit {
fn packet_number(&self) -> u16 {
seven_bit_u16(self.buf[index::PACKET0..=index::PACKET3].as_ref())
}
fn set_packet_number(&mut self, n: u16) {
self.buf[index::PACKET0..=index::PACKET3].copy_from_slice(u16_seven_bit(n).as_ref());
}
}
impl fmt::Display for FlashDownloadReply7bit {
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_7bit_from_buf() -> Result<()> {
let msg_bytes = [
0x02, 0x09, 0x50,
0x01, 0x02, 0x03, 0x04,
0x03, 0x5d,
];
let mut msg = FlashDownloadReply7bit::new();
msg.from_buf(msg_bytes.as_ref())?;
assert_eq!(msg.message_type(), MessageType::FirmwareDownload);
assert_eq!(msg.packet_number(), 0x1234);
Ok(())
}
}