use core::future::Future;
use crate::ember::multi_phy::{nwk, radio};
use crate::ember::{
Eui64, MAX_END_DEVICE_CHILDREN, NodeId, PerDeviceDutyCycle, beacon, child, concentrator,
duty_cycle, neighbor, network, node, route,
};
use crate::error::Error;
use crate::ezsp::network::{InitBitmask, scan};
use crate::frame::parameters::networking::{
child_id, clear_stored_beacons, energy_scan_request, find_and_rejoin_network,
find_unused_pan_id, form_network, get_child_data, get_current_duty_cycle,
get_duty_cycle_limits, get_duty_cycle_state, get_first_beacon, get_logical_channel,
get_neighbor, get_neighbor_frame_counter, get_network_parameters, get_next_beacon,
get_num_stored_beacons, get_parent_child_parameters, get_radio_channel, get_radio_parameters,
get_route_table_entry, get_routing_shortcut_threshold, get_source_route_table_entry,
get_source_route_table_filled_size, get_source_route_table_total_size, id, join_network,
join_network_directly, leave_network, multi_phy_set_radio_channel, multi_phy_set_radio_power,
multi_phy_start, multi_phy_stop, neighbor_count, network_init, network_state, permit_joining,
send_link_power_delta_request, set_broken_route_error_code, set_child_data, set_concentrator,
set_duty_cycle_limits_in_stack, set_logical_and_radio_channel, set_manufacturer_code,
set_neighbor_frame_counter, set_power_descriptor, set_radio_channel,
set_radio_ieee802154_cca_mode, set_radio_power, set_routing_shortcut_threshold, start_scan,
stop_scan,
};
use crate::transport::Transport;
pub trait Networking {
fn child_id(
&mut self,
child_index: u8,
) -> impl Future<Output = Result<Option<NodeId>, Error>> + Send;
fn clear_stored_beacons(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
fn energy_scan_request(
&mut self,
target: NodeId,
scan_channels: u32,
scan_duration: u8,
scan_count: u16,
) -> impl Future<Output = Result<(), Error>> + Send;
fn find_and_rejoin_network(
&mut self,
have_current_network_key: bool,
channel_mask: u32,
) -> impl Future<Output = Result<(), Error>> + Send;
fn find_unused_pan_id(
&mut self,
channel_mask: u32,
duration: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn form_network(
&mut self,
parameters: network::Parameters,
) -> impl Future<Output = Result<(), Error>> + Send;
fn get_child_data(
&mut self,
index: u8,
) -> impl Future<Output = Result<child::Data, Error>> + Send;
fn get_current_duty_cycle(
&mut self,
max_devices: u8,
) -> impl Future<
Output = Result<heapless::Vec<PerDeviceDutyCycle, MAX_END_DEVICE_CHILDREN>, Error>,
> + Send;
fn get_duty_cycle_limits(
&mut self,
) -> impl Future<Output = Result<duty_cycle::Limits, Error>> + Send;
fn get_duty_cycle_state(
&mut self,
) -> impl Future<Output = Result<duty_cycle::State, Error>> + Send;
fn get_first_beacon(&mut self) -> impl Future<Output = Result<beacon::Iterator, Error>> + Send;
fn get_logical_channel(&mut self) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_neighbor(
&mut self,
index: u8,
) -> impl Future<Output = Result<neighbor::TableEntry, Error>> + Send;
fn get_neighbor_frame_counter(
&mut self,
eui64: Eui64,
) -> impl Future<Output = Result<u32, Error>> + Send;
fn get_network_parameters(
&mut self,
) -> impl Future<Output = Result<(node::Type, network::Parameters), Error>> + Send;
fn get_next_beacon(&mut self) -> impl Future<Output = Result<beacon::Data, Error>> + Send;
fn get_num_stored_beacons(&mut self) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_parent_child_parameters(
&mut self,
) -> impl Future<Output = Result<get_parent_child_parameters::Response, Error>> + Send;
fn get_radio_channel(&mut self) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_radio_parameters(
&mut self,
phy_index: u8,
) -> impl Future<Output = Result<radio::Parameters, Error>> + Send;
fn get_route_table_entry(
&mut self,
index: u8,
) -> impl Future<Output = Result<route::TableEntry, Error>> + Send;
fn get_routing_shortcut_threshold(&mut self) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_source_route_table_entry(
&mut self,
index: u8,
) -> impl Future<Output = Result<get_source_route_table_entry::Entry, Error>> + Send;
fn get_source_route_table_filled_size(
&mut self,
) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_source_route_table_total_size(
&mut self,
) -> impl Future<Output = Result<u8, Error>> + Send;
fn id(&mut self, child_id: NodeId) -> impl Future<Output = Result<u8, Error>> + Send;
fn join_network(
&mut self,
node_type: node::Type,
parameters: network::Parameters,
) -> impl Future<Output = Result<(), Error>> + Send;
fn join_network_directly(
&mut self,
local_node_type: node::Type,
beacon: beacon::Data,
radio_tx_power: i8,
clear_beacons_after_network_up: bool,
) -> impl Future<Output = Result<(), Error>> + Send;
fn leave_network(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
fn multi_phy_set_radio_channel(
&mut self,
phy_index: u8,
page: u8,
channel: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn multi_phy_set_radio_power(
&mut self,
phy_index: u8,
power: i8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn multi_phy_start(
&mut self,
phy_index: u8,
page: u8,
channel: u8,
power: i8,
bitmask: nwk::Config,
) -> impl Future<Output = Result<(), Error>> + Send;
fn multi_phy_stop(&mut self, phy_index: u8) -> impl Future<Output = Result<(), Error>> + Send;
fn neighbor_count(&mut self) -> impl Future<Output = Result<u8, Error>> + Send;
fn network_init(
&mut self,
bitmask: InitBitmask,
) -> impl Future<Output = Result<(), Error>> + Send;
fn network_state(&mut self) -> impl Future<Output = Result<network::Status, Error>> + Send;
fn permit_joining(
&mut self,
duration: network::Duration,
) -> impl Future<Output = Result<(), Error>> + Send;
fn send_link_power_delta_request(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
fn set_broken_route_error_code(
&mut self,
error_code: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_child_data(
&mut self,
index: u8,
child_data: child::Data,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_concentrator(
&mut self,
parameters: Option<concentrator::Parameters>,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_duty_cycle_limits_in_stack(
&mut self,
limits: duty_cycle::Limits,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_logical_and_radio_channel(
&mut self,
radio_channel: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_manufacturer_code(
&mut self,
code: u16,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_neighbor_frame_counter(
&mut self,
eui64: Eui64,
frame_counter: u32,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_power_descriptor(
&mut self,
power_descriptor: u16,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_radio_channel(&mut self, channel: u8) -> impl Future<Output = Result<(), Error>> + Send;
fn set_radio_ieee802154_cca_mode(
&mut self,
cca_mode: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_radio_power(&mut self, power: i8) -> impl Future<Output = Result<(), Error>> + Send;
fn set_routing_shortcut_threshold(
&mut self,
cost_thresh: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn start_scan(
&mut self,
scan_type: scan::Type,
channel_mask: u32,
duration: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn stop_scan(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
}
impl<T> Networking for T
where
T: Transport,
{
async fn child_id(&mut self, child_index: u8) -> Result<Option<NodeId>, Error> {
self.communicate(child_id::Command::new(child_index))
.await
.map(|response| response.child_id())
}
async fn clear_stored_beacons(&mut self) -> Result<(), Error> {
self.communicate(clear_stored_beacons::Command)
.await
.map(drop)
}
async fn energy_scan_request(
&mut self,
target: NodeId,
scan_channels: u32,
scan_duration: u8,
scan_count: u16,
) -> Result<(), Error> {
self.communicate(energy_scan_request::Command::new(
target,
scan_channels,
scan_duration,
scan_count,
))
.await?
.try_into()
}
async fn find_and_rejoin_network(
&mut self,
have_current_network_key: bool,
channel_mask: u32,
) -> Result<(), Error> {
self.communicate(find_and_rejoin_network::Command::new(
have_current_network_key,
channel_mask,
))
.await?
.try_into()
}
async fn find_unused_pan_id(&mut self, channel_mask: u32, duration: u8) -> Result<(), Error> {
self.communicate(find_unused_pan_id::Command::new(channel_mask, duration))
.await?
.try_into()
}
async fn form_network(&mut self, parameters: network::Parameters) -> Result<(), Error> {
self.communicate(form_network::Command::new(parameters))
.await?
.try_into()
}
async fn get_child_data(&mut self, index: u8) -> Result<child::Data, Error> {
self.communicate(get_child_data::Command::new(index))
.await?
.try_into()
}
async fn get_current_duty_cycle(
&mut self,
max_devices: u8,
) -> Result<heapless::Vec<PerDeviceDutyCycle, MAX_END_DEVICE_CHILDREN>, Error> {
self.communicate(get_current_duty_cycle::Command::new(max_devices))
.await?
.try_into()
}
async fn get_duty_cycle_limits(&mut self) -> Result<duty_cycle::Limits, Error> {
self.communicate(get_duty_cycle_limits::Command)
.await?
.try_into()
}
async fn get_duty_cycle_state(&mut self) -> Result<duty_cycle::State, Error> {
self.communicate(get_duty_cycle_state::Command)
.await?
.try_into()
}
async fn get_first_beacon(&mut self) -> Result<beacon::Iterator, Error> {
self.communicate(get_first_beacon::Command)
.await?
.try_into()
}
async fn get_logical_channel(&mut self) -> Result<u8, Error> {
self.communicate(get_logical_channel::Command)
.await
.map(|response| response.logical_channel())
}
async fn get_neighbor(&mut self, index: u8) -> Result<neighbor::TableEntry, Error> {
self.communicate(get_neighbor::Command::new(index))
.await?
.try_into()
}
async fn get_neighbor_frame_counter(&mut self, eui64: Eui64) -> Result<u32, Error> {
self.communicate(get_neighbor_frame_counter::Command::new(eui64))
.await?
.try_into()
}
async fn get_network_parameters(&mut self) -> Result<(node::Type, network::Parameters), Error> {
self.communicate(get_network_parameters::Command)
.await?
.try_into()
}
async fn get_next_beacon(&mut self) -> Result<beacon::Data, Error> {
self.communicate(get_next_beacon::Command).await?.try_into()
}
async fn get_num_stored_beacons(&mut self) -> Result<u8, Error> {
self.communicate(get_num_stored_beacons::Command)
.await
.map(|response| response.num_beacons())
}
async fn get_parent_child_parameters(
&mut self,
) -> Result<get_parent_child_parameters::Response, Error> {
self.communicate(get_parent_child_parameters::Command).await
}
async fn get_radio_channel(&mut self) -> Result<u8, Error> {
self.communicate(get_radio_channel::Command)
.await
.map(|response| response.channel())
}
async fn get_radio_parameters(&mut self, phy_index: u8) -> Result<radio::Parameters, Error> {
self.communicate(get_radio_parameters::Command::new(phy_index))
.await?
.try_into()
}
async fn get_route_table_entry(&mut self, index: u8) -> Result<route::TableEntry, Error> {
self.communicate(get_route_table_entry::Command::new(index))
.await?
.try_into()
}
async fn get_routing_shortcut_threshold(&mut self) -> Result<u8, Error> {
self.communicate(get_routing_shortcut_threshold::Command)
.await
.map(|response| response.routing_shortcut_thresh())
}
async fn get_source_route_table_entry(
&mut self,
index: u8,
) -> Result<get_source_route_table_entry::Entry, Error> {
self.communicate(get_source_route_table_entry::Command::new(index))
.await?
.try_into()
}
async fn get_source_route_table_filled_size(&mut self) -> Result<u8, Error> {
self.communicate(get_source_route_table_filled_size::Command)
.await
.map(|response| response.source_route_table_filled_size())
}
async fn get_source_route_table_total_size(&mut self) -> Result<u8, Error> {
self.communicate(get_source_route_table_total_size::Command)
.await
.map(|response| response.source_route_table_total_size())
}
async fn id(&mut self, child_id: NodeId) -> Result<u8, Error> {
self.communicate(id::Command::new(child_id))
.await
.map(|response| response.child_index())
}
async fn join_network(
&mut self,
node_type: node::Type,
parameters: network::Parameters,
) -> Result<(), Error> {
self.communicate(join_network::Command::new(node_type, parameters))
.await?
.try_into()
}
async fn join_network_directly(
&mut self,
local_node_type: node::Type,
beacon: beacon::Data,
radio_tx_power: i8,
clear_beacons_after_network_up: bool,
) -> Result<(), Error> {
self.communicate(join_network_directly::Command::new(
local_node_type,
beacon,
radio_tx_power,
clear_beacons_after_network_up,
))
.await?
.try_into()
}
async fn leave_network(&mut self) -> Result<(), Error> {
self.communicate(leave_network::Command).await?.try_into()
}
async fn multi_phy_set_radio_channel(
&mut self,
phy_index: u8,
page: u8,
channel: u8,
) -> Result<(), Error> {
self.communicate(multi_phy_set_radio_channel::Command::new(
phy_index, page, channel,
))
.await?
.try_into()
}
async fn multi_phy_set_radio_power(&mut self, phy_index: u8, power: i8) -> Result<(), Error> {
self.communicate(multi_phy_set_radio_power::Command::new(phy_index, power))
.await?
.try_into()
}
async fn multi_phy_start(
&mut self,
phy_index: u8,
page: u8,
channel: u8,
power: i8,
bitmask: nwk::Config,
) -> Result<(), Error> {
self.communicate(multi_phy_start::Command::new(
phy_index, page, channel, power, bitmask,
))
.await?
.try_into()
}
async fn multi_phy_stop(&mut self, phy_index: u8) -> Result<(), Error> {
self.communicate(multi_phy_stop::Command::new(phy_index))
.await?
.try_into()
}
async fn neighbor_count(&mut self) -> Result<u8, Error> {
self.communicate(neighbor_count::Command)
.await
.map(|response| response.value())
}
async fn network_init(&mut self, bitmask: InitBitmask) -> Result<(), Error> {
self.communicate(network_init::Command::new(bitmask))
.await?
.try_into()
}
async fn network_state(&mut self) -> Result<network::Status, Error> {
self.communicate(network_state::Command).await?.try_into()
}
async fn permit_joining(&mut self, duration: network::Duration) -> Result<(), Error> {
self.communicate(permit_joining::Command::new(duration))
.await?
.try_into()
}
async fn send_link_power_delta_request(&mut self) -> Result<(), Error> {
self.communicate(send_link_power_delta_request::Command)
.await?
.try_into()
}
async fn set_broken_route_error_code(&mut self, error_code: u8) -> Result<(), Error> {
self.communicate(set_broken_route_error_code::Command::new(error_code))
.await?
.try_into()
}
async fn set_child_data(&mut self, index: u8, child_data: child::Data) -> Result<(), Error> {
self.communicate(set_child_data::Command::new(index, child_data))
.await?
.try_into()
}
async fn set_concentrator(
&mut self,
parameters: Option<concentrator::Parameters>,
) -> Result<(), Error> {
self.communicate(set_concentrator::Command::from(parameters))
.await?
.try_into()
}
async fn set_duty_cycle_limits_in_stack(
&mut self,
limits: duty_cycle::Limits,
) -> Result<(), Error> {
self.communicate(set_duty_cycle_limits_in_stack::Command::from(limits))
.await?
.try_into()
}
async fn set_logical_and_radio_channel(&mut self, radio_channel: u8) -> Result<(), Error> {
self.communicate(set_logical_and_radio_channel::Command::new(radio_channel))
.await?
.try_into()
}
async fn set_manufacturer_code(&mut self, code: u16) -> Result<(), Error> {
self.communicate(set_manufacturer_code::Command::new(code))
.await
.map(drop)
}
async fn set_neighbor_frame_counter(
&mut self,
eui64: Eui64,
frame_counter: u32,
) -> Result<(), Error> {
self.communicate(set_neighbor_frame_counter::Command::new(
eui64,
frame_counter,
))
.await?
.try_into()
}
async fn set_power_descriptor(&mut self, power_descriptor: u16) -> Result<(), Error> {
self.communicate(set_power_descriptor::Command::new(power_descriptor))
.await
.map(drop)
}
async fn set_radio_channel(&mut self, channel: u8) -> Result<(), Error> {
self.communicate(set_radio_channel::Command::new(channel))
.await?
.try_into()
}
async fn set_radio_ieee802154_cca_mode(&mut self, cca_mode: u8) -> Result<(), Error> {
self.communicate(set_radio_ieee802154_cca_mode::Command::new(cca_mode))
.await?
.try_into()
}
async fn set_radio_power(&mut self, power: i8) -> Result<(), Error> {
self.communicate(set_radio_power::Command::new(power))
.await?
.try_into()
}
async fn set_routing_shortcut_threshold(&mut self, cost_thresh: u8) -> Result<(), Error> {
self.communicate(set_routing_shortcut_threshold::Command::new(cost_thresh))
.await?
.try_into()
}
async fn start_scan(
&mut self,
scan_type: scan::Type,
channel_mask: u32,
duration: u8,
) -> Result<(), Error> {
self.communicate(start_scan::Command::new(scan_type, channel_mask, duration))
.await?
.try_into()
}
async fn stop_scan(&mut self) -> Result<(), Error> {
self.communicate(stop_scan::Command).await?.try_into()
}
}