Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions crates/ziggurat-driver/src/zigbee_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use ziggurat_phy::{
use ziggurat_zigbee::Instant as CoreInstant;
use ziggurat_zigbee::aps::frame::{ApsFrame, parse_aps_frame};
use ziggurat_zigbee::beacon::ZigbeeBeacon;
use ziggurat_zigbee::zcl::endpoints::Endpoints;
use ziggurat_zigbee::zdp::descriptors::Descriptors;

use thiserror::Error;

Expand Down Expand Up @@ -37,6 +39,7 @@ mod route;
mod send_handle;
mod sends;
mod tasklets;
mod zcl;
mod zdp;

use send_handle::SendWatches;
Expand Down Expand Up @@ -81,6 +84,9 @@ const FRAME_COUNTER_NOTIFY_INTERVAL: u32 = 100;
/// that wasn't reading has already missed it and re-syncs on reconnect.
const NOTIFICATION_QUEUE_CAP: usize = 64;

/// Channels 11 to 26 of channel page 0, the default `apsChannelMaskList`.
const ALL_2400_MHZ_CHANNELS: u32 = 0x07FF_F800;

/// A synchronous admission failure, returned by the `send_*` entry points.
///
/// An `Err` means nothing was enqueued and no later confirmation follows; `retry_in`
Expand Down Expand Up @@ -368,6 +374,8 @@ pub struct Nib {
pub nwk_security: NwkSecurity,
/// The EUI64-to-network-address map, one owner per network address
pub address_map: AddressMap,
/// `nwkManagerAddr`: the device that manages the network's channel
pub manager_address: Nwk,
/// `nwkIsConcentrator`: we advertise many-to-one routes to ourselves and
/// source-route through the route records they bring back. Whoever controls the
/// node decides, at any time.
Expand All @@ -394,6 +402,9 @@ pub struct Aib {
/// derivation, command encryption). Holds the non-spec TCLK seed used to derive
/// keys when the network was taken over from a microcontroller stack.
pub aps_security: ApsSecurity,
/// `apsChannelMaskList`: the channels the network may use, for a router that has
/// to find it again after missing a channel change
pub channel_mask: u32,
}

/// Host-side mirror of the MAC PIB attributes we drive on the RCP. The MAC sub-layer
Expand Down Expand Up @@ -422,6 +433,12 @@ pub struct ZigbeeCore {
/// lock because resolving one reads the address map and the routing table.
pub address_conflicts: AddressConflicts,

/// The local endpoints and manufacturer codes, read by ZDP discovery requests.
pub descriptors: Descriptors,

/// The application endpoints the stack serves itself, rather than the client.
pub endpoints: Endpoints,

/// Deadline until which the coordinator advertises `association_permit` in its
/// beacon and accepts direct MAC associations. A deadline rather than a flag lets
/// renewals extend the window. `None` or past means direct joins are denied.
Expand Down Expand Up @@ -484,7 +501,6 @@ pub struct State {

pub role: NwkDeviceType,
pub capability_information: NwkCapabilityInformation,
pub nwk_manager_addr: Nwk,
/// Our tree depth, advertised in beacons. The coordinator is the root, at 0.
pub depth: u8,

Expand Down Expand Up @@ -540,6 +556,7 @@ impl State {
FRAME_COUNTER_NOTIFY_INTERVAL,
),
address_map: AddressMap::new(config.network_address, config.ieee_address),
manager_address: Nwk(0x0000),
is_concentrator: config.concentrator,
},
aib: Aib {
Expand All @@ -550,6 +567,7 @@ impl State {
config.tclk_seed.clone(),
FRAME_COUNTER_NOTIFY_INTERVAL,
),
channel_mask: ALL_2400_MHZ_CHANNELS,
},
mac: MacState {
channel: config.channel,
Expand All @@ -558,6 +576,12 @@ impl State {
},
sends: SendTable::default(),
address_conflicts: AddressConflicts::default(),
descriptors: Descriptors::new(
config.network_address,
config.ieee_address,
config.role,
),
endpoints: Endpoints::default(),
permitting_joins_until: None,
trust_center_joins_until: None,
beacon_spam_until: None,
Expand Down Expand Up @@ -588,7 +612,6 @@ impl State {
security_capability: NwkSecurityCapability::Capable,
allocate_address: true,
},
nwk_manager_addr: Nwk(0x0000),
network_address: config.network_address,
extended_pan_id: config.extended_pan_id,
security_level: 5,
Expand Down Expand Up @@ -1076,10 +1099,11 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
continue;
}

// The ZDP commands that maintain stack state (the neighbor and
// address tables live here, not in the client) are consumed by
// the stack; the client still observes the frames
self.handle_zdp_frame(&nwk_frame, &aps_frame);
// The stack consumes the ZDP commands that read or change its own
// state: the neighbor and address tables, and the descriptors.
// The client still observes the frames
self.handle_zdp_frame(&nwk_frame, &aps_frame, aps_source_eui64);
self.handle_zcl_frame(&nwk_frame, &aps_frame, aps_source_eui64);

let notification = ZigbeeNotification::ReceivedApsCommand {
source: nwk_frame.nwk_header.source,
Expand Down
80 changes: 80 additions & 0 deletions crates/ziggurat-driver/src/zigbee_stack/zcl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::runtime::Runtime;
use ziggurat_ieee_802154::types::Eui64;
use ziggurat_phy::RadioPhy;
use ziggurat_zigbee::aps::frame::{ApsDataFrame, ApsDeliveryMode};
use ziggurat_zigbee::nwk::frame::{NwkFrame, is_broadcast};
use ziggurat_zigbee::zcl::endpoints::{BROADCAST_ENDPOINT, Endpoint, Indication};

use super::{ApsAck, RouteDirective, TxPriority, ZigbeeStack};

impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
/// Serve an application endpoint from the stack, rather than the client. Its
/// simple descriptor joins the ones ZDP requests read.
pub fn add_application_endpoint(&self, endpoint: Endpoint) {
let mut core = self.core();
core.descriptors.add_endpoint(endpoint.simple_descriptor());
core.endpoints.add(endpoint);
}

/// Carry out a ZCL frame for the endpoints the stack serves. Replies mirror the
/// APS security of the frame; `aps_security` is the sender's EUI64 if it was
/// APS-secured. The client still observes the frame.
pub(super) fn handle_zcl_frame(
&self,
nwk_frame: &NwkFrame,
aps_frame: &ApsDataFrame,
aps_security: Option<Eui64>,
) {
// Group frames carry no endpoint, and we serve no groups
let Some(destination_endpoint) = aps_frame.destination_endpoint else {
return;
};

let replies = {
let mut core = self.core();

if destination_endpoint != BROADCAST_ENDPOINT
&& !core.endpoints.contains(destination_endpoint)
{
return;
}

let indication = Indication {
source: nwk_frame.nwk_header.source,
source_endpoint: aps_frame.source_endpoint,
destination_endpoint,
profile_id: aps_frame.profile_id,
cluster_id: aps_frame.cluster_id,
unicast: aps_frame.frame_control.delivery_mode == ApsDeliveryMode::Unicast
&& !is_broadcast(nwk_frame.nwk_header.destination),
aps_secured: aps_security.is_some(),
asdu: &aps_frame.asdu,
};

core.endpoints.receive(&indication, self.core_now())
};

for reply in replies {
if let Err(err) = self.send_aps_unicast(
reply.destination,
reply.profile_id,
reply.cluster_id,
reply.source_endpoint,
reply.destination_endpoint,
ApsAck::None,
None,
0,
reply.asdu.to_vec(),
aps_security,
false,
TxPriority::UserNormal,
RouteDirective::StackDecides,
) {
tracing::warn!(
"Failed to send a ZCL reply to {:?}: {err}",
reply.destination
);
}
}
}
}
Loading