Skip to content

Commit

Permalink
ieee802154: Divide and share traits
Browse files Browse the repository at this point in the history
Make it explicit which interfaces are shared
  • Loading branch information
bradjc committed May 16, 2024
1 parent 6289b6c commit cb3fe53
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 93 deletions.
2 changes: 1 addition & 1 deletion boards/components/src/ieee802154.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

use capsules_core::virtualizers::virtual_aes_ccm::MuxAES128CCM;
use capsules_extra::ieee802154::device::MacDevice;
use capsules_extra::ieee802154::mac::{AwakeMac, Mac};
use capsules_extra::ieee802154::mac::{AwakeMac, Mac, RadioControl};
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
Expand Down
1 change: 1 addition & 0 deletions boards/imix/src/test/ipv6_lowpan_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use capsules_extra::ieee802154::device::{MacDevice, TxClient};
use capsules_extra::ieee802154::mac::RadioControl;
use capsules_extra::net::ieee802154::MacAddress;
use capsules_extra::net::ipv6::ip_utils::{ip6_nh, IPAddr};
use capsules_extra::net::ipv6::{IP6Header, IP6Packet, IPPayload, TransportHeader};
Expand Down
26 changes: 2 additions & 24 deletions capsules/extra/src/ieee802154/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
//! procedure in hardware, as opposed to requiring a software implementation.

use crate::ieee802154::framer::Frame;
use crate::ieee802154::mac;
use crate::net::ieee802154::{Header, KeyId, MacAddress, PanID, SecurityLevel};
use kernel::ErrorCode;

pub trait MacDevice<'a> {
pub trait MacDevice<'a>: mac::RadioControl<'a> {
/// Sets the transmission client of this MAC device
fn set_transmit_client(&self, client: &'a dyn TxClient);
/// Sets the receive client of this MAC device
Expand All @@ -30,29 +31,6 @@ pub trait MacDevice<'a> {
client: &'a dyn SecuredFrameNoDecryptRxClient,
);

/// The short 16-bit address of the MAC device
fn get_address(&self) -> u16;
/// The long 64-bit address (EUI-64) of the MAC device
fn get_address_long(&self) -> [u8; 8];
/// The 16-bit PAN ID of the MAC device
fn get_pan(&self) -> u16;

/// Set the short 16-bit address of the MAC device
fn set_address(&self, addr: u16);
/// Set the long 64-bit address (EUI-64) of the MAC device
fn set_address_long(&self, addr: [u8; 8]);
/// Set the 16-bit PAN ID of the MAC device
fn set_pan(&self, id: u16);

/// This method must be called after one or more calls to `set_*`. If
/// `set_*` is called without calling `config_commit`, there is no guarantee
/// that the underlying hardware configuration (addresses, pan ID) is in
/// line with this MAC device implementation.
fn config_commit(&self);

/// Returns if the MAC device is currently on.
fn is_on(&self) -> bool;

/// Prepares a mutable buffer slice as an 802.15.4 frame by writing the appropriate
/// header bytes into the buffer. This needs to be done before adding the
/// payload because the length of the header is not fixed.
Expand Down
36 changes: 19 additions & 17 deletions capsules/extra/src/ieee802154/framer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//

use crate::ieee802154::device::{MacDevice, RxClient, SecuredFrameNoDecryptRxClient, TxClient};
use crate::ieee802154::mac::Mac;
use crate::ieee802154::mac::{self, Mac};
use crate::net::ieee802154::{
FrameType, FrameVersion, Header, KeyId, MacAddress, PanID, Security, SecurityLevel,
};
Expand Down Expand Up @@ -798,22 +798,7 @@ impl<'a, M: Mac<'a>, A: AES128CCM<'a>> Framer<'a, M, A> {
}
}

impl<'a, M: Mac<'a>, A: AES128CCM<'a>> MacDevice<'a> for Framer<'a, M, A> {
fn set_transmit_client(&self, client: &'a dyn TxClient) {
self.tx_client.set(client);
}

fn set_receive_client(&self, client: &'a dyn RxClient) {
self.rx_client.set(client);
}

fn set_receive_secured_frame_no_decrypt_client(
&self,
client: &'a dyn super::device::SecuredFrameNoDecryptRxClient,
) {
self.secured_frame_no_decrypt_rx_client.set(client);
}

impl<'a, M: Mac<'a>, A: AES128CCM<'a>> mac::RadioControl<'a> for Framer<'a, M, A> {
fn get_address(&self) -> u16 {
self.mac.get_address()
}
Expand Down Expand Up @@ -845,6 +830,23 @@ impl<'a, M: Mac<'a>, A: AES128CCM<'a>> MacDevice<'a> for Framer<'a, M, A> {
fn is_on(&self) -> bool {
self.mac.is_on()
}
}

impl<'a, M: Mac<'a>, A: AES128CCM<'a>> MacDevice<'a> for Framer<'a, M, A> {
fn set_transmit_client(&self, client: &'a dyn TxClient) {
self.tx_client.set(client);
}

fn set_receive_client(&self, client: &'a dyn RxClient) {
self.rx_client.set(client);
}

fn set_receive_secured_frame_no_decrypt_client(
&self,
client: &'a dyn super::device::SecuredFrameNoDecryptRxClient,
) {
self.secured_frame_no_decrypt_rx_client.set(client);
}

fn prepare_data_frame(
&self,
Expand Down
50 changes: 27 additions & 23 deletions capsules/extra/src/ieee802154/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,7 @@ use kernel::hil::radio::{self, MAX_FRAME_SIZE, PSDU_OFFSET};
use kernel::utilities::cells::OptionalCell;
use kernel::ErrorCode;

pub trait Mac<'a> {
/// Initializes the layer.
fn initialize(&self) -> Result<(), ErrorCode>;

/// Sets the notified client for configuration changes
fn set_config_client(&self, client: &'a dyn radio::ConfigClient);
/// Sets the notified client for transmission completions
fn set_transmit_client(&self, client: &'a dyn radio::TxClient);
/// Sets the notified client for frame receptions
fn set_receive_client(&self, client: &'a dyn radio::RxClient);
/// Sets the buffer for packet reception
fn set_receive_buffer(&self, buffer: &'static mut [u8]);

pub trait RadioControl<'a> {
/// The short 16-bit address of the radio
fn get_address(&self) -> u16;
/// The long 64-bit address of the radio
Expand All @@ -54,6 +42,20 @@ pub trait Mac<'a> {

/// Indicates whether or not the MAC protocol is active and can send frames
fn is_on(&self) -> bool;
}

pub trait Mac<'a>: RadioControl<'a> {
/// Initializes the layer.
fn initialize(&self) -> Result<(), ErrorCode>;

/// Sets the notified client for configuration changes
fn set_config_client(&self, client: &'a dyn radio::ConfigClient);
/// Sets the notified client for transmission completions
fn set_transmit_client(&self, client: &'a dyn radio::TxClient);
/// Sets the notified client for frame receptions
fn set_receive_client(&self, client: &'a dyn radio::RxClient);
/// Sets the buffer for packet reception
fn set_receive_buffer(&self, buffer: &'static mut [u8]);

/// Transmits complete MAC frames, which must be prepared by an ieee802154::device::MacDevice
/// before being passed to the Mac layer. Returns the frame buffer in case of an error.
Expand Down Expand Up @@ -86,20 +88,11 @@ impl<'a, R: radio::Radio<'a>> AwakeMac<'a, R> {
}
}

impl<'a, R: radio::Radio<'a>> Mac<'a> for AwakeMac<'a, R> {
fn initialize(&self) -> Result<(), ErrorCode> {
// do nothing, extra buffer unnecessary
Ok(())
}

impl<'a, R: radio::Radio<'a>> RadioControl<'a> for AwakeMac<'a, R> {
fn is_on(&self) -> bool {
self.radio.is_on()
}

fn set_config_client(&self, client: &'a dyn radio::ConfigClient) {
self.radio.set_config_client(client)
}

fn set_address(&self, addr: u16) {
self.radio.set_address(addr)
}
Expand Down Expand Up @@ -127,6 +120,17 @@ impl<'a, R: radio::Radio<'a>> Mac<'a> for AwakeMac<'a, R> {
fn config_commit(&self) {
self.radio.config_commit()
}
}

impl<'a, R: radio::Radio<'a>> Mac<'a> for AwakeMac<'a, R> {
fn initialize(&self) -> Result<(), ErrorCode> {
// do nothing, extra buffer unnecessary
Ok(())
}

fn set_config_client(&self, client: &'a dyn radio::ConfigClient) {
self.radio.set_config_client(client)
}

fn set_transmit_client(&self, client: &'a dyn radio::TxClient) {
self.tx_client.set(client);
Expand Down
36 changes: 19 additions & 17 deletions capsules/extra/src/ieee802154/virtual_mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! mux_mac.add_user(virtual_mac);
//! ```

use crate::ieee802154::{device, framer};
use crate::ieee802154::{device, framer, mac};
use crate::net::ieee802154::{Header, KeyId, MacAddress, PanID, SecurityLevel};

use kernel::collections::list::{List, ListLink, ListNode};
Expand Down Expand Up @@ -276,22 +276,7 @@ impl<'a, M: device::MacDevice<'a>> ListNode<'a, MacUser<'a, M>> for MacUser<'a,
}
}

impl<'a, M: device::MacDevice<'a>> device::MacDevice<'a> for MacUser<'a, M> {
fn set_transmit_client(&self, client: &'a dyn device::TxClient) {
self.tx_client.set(client);
}

fn set_receive_client(&self, client: &'a dyn device::RxClient) {
self.rx_client.set(client);
}

fn set_receive_secured_frame_no_decrypt_client(
&self,
client: &'a dyn device::SecuredFrameNoDecryptRxClient,
) {
self.secure_frame_no_decrypt_rx_client.set(client);
}

impl<'a, M: device::MacDevice<'a>> mac::RadioControl<'a> for MacUser<'a, M> {
fn get_address(&self) -> u16 {
self.mux.mac.get_address()
}
Expand Down Expand Up @@ -323,6 +308,23 @@ impl<'a, M: device::MacDevice<'a>> device::MacDevice<'a> for MacUser<'a, M> {
fn is_on(&self) -> bool {
self.mux.mac.is_on()
}
}

impl<'a, M: device::MacDevice<'a>> device::MacDevice<'a> for MacUser<'a, M> {
fn set_transmit_client(&self, client: &'a dyn device::TxClient) {
self.tx_client.set(client);
}

fn set_receive_client(&self, client: &'a dyn device::RxClient) {
self.rx_client.set(client);
}

fn set_receive_secured_frame_no_decrypt_client(
&self,
client: &'a dyn device::SecuredFrameNoDecryptRxClient,
) {
self.secure_frame_no_decrypt_rx_client.set(client);
}

fn prepare_data_frame(
&self,
Expand Down
24 changes: 13 additions & 11 deletions capsules/extra/src/ieee802154/xmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
// Date: Nov 21 2017
//

use crate::ieee802154::mac::Mac;
use crate::ieee802154::mac::{Mac, RadioControl};
use crate::net::ieee802154::{FrameType, FrameVersion, Header, MacAddress, PanID};
use core::cell::Cell;
use kernel::hil::radio;
Expand Down Expand Up @@ -354,12 +354,7 @@ impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> rng::Client for XMac<'a, R, A> {
}

// The vast majority of these calls pass through to the underlying radio driver.
impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> Mac<'a> for XMac<'a, R, A> {
fn initialize(&self) -> Result<(), ErrorCode> {
self.state.set(XMacState::STARTUP);
Ok(())
}

impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> RadioControl<'a> for XMac<'a, R, A> {
// Always lie and say the radio is on when sleeping, as XMAC will wake up
// itself to send preambles if necessary.
fn is_on(&self) -> bool {
Expand All @@ -369,10 +364,6 @@ impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> Mac<'a> for XMac<'a, R, A> {
self.radio.is_on()
}

fn set_config_client(&self, client: &'a dyn radio::ConfigClient) {
self.radio.set_config_client(client)
}

fn set_address(&self, addr: u16) {
self.radio.set_address(addr)
}
Expand Down Expand Up @@ -400,6 +391,17 @@ impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> Mac<'a> for XMac<'a, R, A> {
fn config_commit(&self) {
self.radio.config_commit()
}
}

impl<'a, R: radio::Radio<'a>, A: Alarm<'a>> Mac<'a> for XMac<'a, R, A> {
fn initialize(&self) -> Result<(), ErrorCode> {
self.state.set(XMacState::STARTUP);
Ok(())
}

fn set_config_client(&self, client: &'a dyn radio::ConfigClient) {
self.radio.set_config_client(client)
}

fn set_transmit_client(&self, client: &'a dyn radio::TxClient) {
self.tx_client.set(client);
Expand Down

0 comments on commit cb3fe53

Please sign in to comment.