use crate::status::Status;
use super::RxBw;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WisunMode {
Mode1a = 0,
Mode1b = 1,
Mode2a = 2,
Mode2b = 3,
Mode3 = 4,
Mode4a = 5,
Mode4b = 6,
Mode5 = 7,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WisunFcsLen {
Fcs32b = 0,
Fcs16b = 1,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WisunFec {
None = 0,
Nrnsc = 1,
Rsc = 2,
RscIntlvr = 3,
}
pub fn set_wisun_mode_cmd(wisun_mode: WisunMode, rx_bw: RxBw) -> [u8; 4] {
let mut cmd = [0u8; 4];
cmd[0] = 0x02;
cmd[1] = 0x70;
cmd[2] |= (wisun_mode as u8) & 0x7;
cmd[3] |= rx_bw as u8;
cmd
}
#[allow(clippy::too_many_arguments)]
pub fn set_wisun_packet_params_cmd(wisun_fcs_len: WisunFcsLen, whitening_en: bool, crc_hw: bool, mode_switch_tx: bool, wisun_fec: WisunFec, frame_len_tx: u16, pbl_len_tx: u8, pbl_detect: u8) -> [u8; 7] {
let mut cmd = [0u8; 7];
cmd[0] = 0x02;
cmd[1] = 0x71;
cmd[2] |= ((wisun_fcs_len as u8) & 0x1) << 5;
if whitening_en { cmd[2] |= 16; }
if crc_hw { cmd[2] |= 8; }
if mode_switch_tx { cmd[2] |= 4; }
cmd[2] |= (wisun_fec as u8) & 0x3;
cmd[3] |= ((frame_len_tx >> 8) & 0xFF) as u8;
cmd[4] |= (frame_len_tx & 0xFF) as u8;
cmd[5] |= pbl_len_tx;
cmd[6] |= pbl_detect;
cmd
}
pub fn get_wisun_packet_status_req() -> [u8; 2] {
[0x02, 0x73]
}
pub fn set_wisun_packet_len_cmd(frame_len_tx: u16) -> [u8; 4] {
let mut cmd = [0u8; 4];
cmd[0] = 0x02;
cmd[1] = 0x74;
cmd[2] |= ((frame_len_tx >> 8) & 0xFF) as u8;
cmd[3] |= (frame_len_tx & 0xFF) as u8;
cmd
}
pub fn get_wisun_rx_stats_req() -> [u8; 2] {
[0x02, 0x6C]
}
#[derive(Default)]
pub struct WisunPacketStatusRsp([u8; 11]);
impl WisunPacketStatusRsp {
pub fn new() -> Self {
Self::default()
}
pub fn status(&mut self) -> Status {
Status::from_slice(&self.0[..2])
}
pub fn header(&self) -> u16 {
(self.0[3] as u16) |
((self.0[2] as u16) << 8)
}
pub fn pkt_len(&self) -> u16 {
(self.0[5] as u16) |
((self.0[4] as u16) << 8)
}
pub fn rssi_avg(&self) -> u16 {
(((self.0[9] >> 2) & 0x1) as u16) |
((self.0[6] as u16) << 1)
}
pub fn rssi_sync(&self) -> u16 {
((self.0[9] & 0x1) as u16) |
((self.0[7] as u16) << 1)
}
pub fn syncword_idx(&self) -> bool {
(self.0[8] >> 7) & 0x1 != 0
}
pub fn lqi(&self) -> u8 {
self.0[10]
}
}
impl AsMut<[u8]> for WisunPacketStatusRsp {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
#[derive(Default)]
pub struct WisunRxStatsRsp([u8; 8]);
impl WisunRxStatsRsp {
pub fn new() -> Self {
Self::default()
}
pub fn status(&mut self) -> Status {
Status::from_slice(&self.0[..2])
}
pub fn pkt_rx(&self) -> u16 {
(self.0[3] as u16) |
((self.0[2] as u16) << 8)
}
pub fn crc_error(&self) -> u16 {
(self.0[5] as u16) |
((self.0[4] as u16) << 8)
}
pub fn len_error(&self) -> u16 {
(self.0[7] as u16) |
((self.0[6] as u16) << 8)
}
}
impl AsMut<[u8]> for WisunRxStatsRsp {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}