use crate::ErrorCode;
pub trait TxClient {
fn send_done(&self, buf: &'static mut [u8], acked: bool, result: Result<(), ErrorCode>);
}
pub trait RxClient {
fn receive(
&self,
buf: &'static mut [u8],
frame_len: usize,
crc_valid: bool,
result: Result<(), ErrorCode>,
);
}
pub trait ConfigClient {
fn config_done(&self, result: Result<(), ErrorCode>);
}
pub trait PowerClient {
fn changed(&self, on: bool);
}
pub const MIN_MHR_SIZE: usize = 9;
pub const MFR_SIZE: usize = 2;
pub const MAX_MTU: usize = 127;
pub const MIN_FRAME_SIZE: usize = MIN_MHR_SIZE + MFR_SIZE;
pub const MAX_FRAME_SIZE: usize = MAX_MTU;
pub const PSDU_OFFSET: usize = 2;
pub const MAX_BUF_SIZE: usize = PSDU_OFFSET + MAX_MTU;
pub const MIN_PAYLOAD_OFFSET: usize = PSDU_OFFSET + MIN_MHR_SIZE;
pub trait Radio<'a>: RadioConfig<'a> + RadioData<'a> {}
impl<'a, T: RadioConfig<'a> + RadioData<'a>> Radio<'a> for T {}
pub trait RadioConfig<'a> {
fn initialize(
&self,
spi_buf: &'static mut [u8],
reg_write: &'static mut [u8],
reg_read: &'static mut [u8],
) -> Result<(), ErrorCode>;
fn reset(&self) -> Result<(), ErrorCode>;
fn start(&self) -> Result<(), ErrorCode>;
fn stop(&self) -> Result<(), ErrorCode>;
fn is_on(&self) -> bool;
fn busy(&self) -> bool;
fn set_power_client(&self, client: &'a dyn PowerClient);
fn config_commit(&self);
fn set_config_client(&self, client: &'a dyn ConfigClient);
fn get_address(&self) -> u16; fn get_address_long(&self) -> [u8; 8]; fn get_pan(&self) -> u16; fn get_tx_power(&self) -> i8; fn get_channel(&self) -> u8; fn set_address(&self, addr: u16);
fn set_address_long(&self, addr: [u8; 8]);
fn set_pan(&self, id: u16);
fn set_tx_power(&self, power: i8) -> Result<(), ErrorCode>;
fn set_channel(&self, chan: RadioChannel);
}
pub trait RadioData<'a> {
fn set_transmit_client(&self, client: &'a dyn TxClient);
fn set_receive_client(&self, client: &'a dyn RxClient, receive_buffer: &'static mut [u8]);
fn set_receive_buffer(&self, receive_buffer: &'static mut [u8]);
fn transmit(
&self,
spi_buf: &'static mut [u8],
frame_len: usize,
) -> Result<(), (ErrorCode, &'static mut [u8])>;
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum RadioChannel {
Channel11 = 5,
Channel12 = 10,
Channel13 = 15,
Channel14 = 20,
Channel15 = 25,
Channel16 = 30,
Channel17 = 35,
Channel18 = 40,
Channel19 = 45,
Channel20 = 50,
Channel21 = 55,
Channel22 = 60,
Channel23 = 65,
Channel24 = 70,
Channel25 = 75,
Channel26 = 80,
}
impl RadioChannel {
pub fn get_channel_number(&self) -> u8 {
match *self {
RadioChannel::Channel11 => 11,
RadioChannel::Channel12 => 12,
RadioChannel::Channel13 => 13,
RadioChannel::Channel14 => 14,
RadioChannel::Channel15 => 15,
RadioChannel::Channel16 => 16,
RadioChannel::Channel17 => 17,
RadioChannel::Channel18 => 18,
RadioChannel::Channel19 => 19,
RadioChannel::Channel20 => 20,
RadioChannel::Channel21 => 21,
RadioChannel::Channel22 => 22,
RadioChannel::Channel23 => 23,
RadioChannel::Channel24 => 24,
RadioChannel::Channel25 => 25,
RadioChannel::Channel26 => 26,
}
}
}
impl TryFrom<u8> for RadioChannel {
type Error = ();
fn try_from(val: u8) -> Result<RadioChannel, ()> {
match val {
11 => Ok(RadioChannel::Channel11),
12 => Ok(RadioChannel::Channel12),
13 => Ok(RadioChannel::Channel13),
14 => Ok(RadioChannel::Channel14),
15 => Ok(RadioChannel::Channel15),
16 => Ok(RadioChannel::Channel16),
17 => Ok(RadioChannel::Channel17),
18 => Ok(RadioChannel::Channel18),
19 => Ok(RadioChannel::Channel19),
20 => Ok(RadioChannel::Channel20),
21 => Ok(RadioChannel::Channel21),
22 => Ok(RadioChannel::Channel22),
23 => Ok(RadioChannel::Channel23),
24 => Ok(RadioChannel::Channel24),
25 => Ok(RadioChannel::Channel25),
26 => Ok(RadioChannel::Channel26),
_ => Err(()),
}
}
}