Skip to main content

zigbee_core/aps/
binding.rs

1//! Binding table (2.2.8.2.1).
2//!
3//! A binding maps a local source endpoint and cluster onto a remote
4//! destination, so an application can transmit without naming an address
5//! (`DstAddrMode::None`, 2.2.8.4.1).
6use thiserror::Error;
7use zigbee_macros::impl_byte;
8use zigbee_types::IeeeAddress;
9use zigbee_types::StorageVec;
10
11impl_byte! {
12    #[tag(u8)]
13    /// Destination addressing mode of a binding (2.4.3.2.2).
14    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
15    pub enum BindingAddrMode {
16        /// 16-bit group address, destination endpoint omitted
17        Group = 0x01,
18        /// 64-bit extended address plus destination endpoint
19        Device = 0x03,
20        #[fallback = true]
21        Reserved(u8),
22    }
23}
24
25impl BindingAddrMode {
26    /// Whether the binding names a group.
27    pub fn is_group(self) -> bool {
28        self == Self::Group
29    }
30
31    /// Whether the binding names a single device endpoint.
32    pub fn is_device(self) -> bool {
33        self == Self::Device
34    }
35}
36
37impl Default for BindingAddrMode {
38    fn default() -> Self {
39        Self::Device
40    }
41}
42
43impl_byte! {
44    /// A binding table entry (2.2.8.2.1).
45    #[derive(Debug, Clone, Default, PartialEq, Eq)]
46    pub struct Binding {
47        /// local endpoint the binding applies to
48        pub src_endpoint: u8,
49        /// cluster the binding applies to
50        pub cluster_id: u16,
51        pub dst_addr_mode: BindingAddrMode,
52        #[parse_if = dst_addr_mode.is_group()]
53        pub dst_group: Option<u16>,
54        #[parse_if = dst_addr_mode.is_device()]
55        pub dst_ieee: Option<IeeeAddress>,
56        #[parse_if = dst_addr_mode.is_device()]
57        pub dst_endpoint: Option<u8>,
58    }
59}
60
61impl Binding {
62    /// A binding to a single remote endpoint.
63    pub fn device(
64        src_endpoint: u8,
65        cluster_id: u16,
66        dst_ieee: IeeeAddress,
67        dst_endpoint: u8,
68    ) -> Self {
69        Self {
70            src_endpoint,
71            cluster_id,
72            dst_addr_mode: BindingAddrMode::Device,
73            dst_group: None,
74            dst_ieee: Some(dst_ieee),
75            dst_endpoint: Some(dst_endpoint),
76        }
77    }
78
79    /// A binding to a group address.
80    pub fn group(src_endpoint: u8, cluster_id: u16, dst_group: u16) -> Self {
81        Self {
82            src_endpoint,
83            cluster_id,
84            dst_addr_mode: BindingAddrMode::Group,
85            dst_group: Some(dst_group),
86            dst_ieee: None,
87            dst_endpoint: None,
88        }
89    }
90
91    /// Whether the entry routes traffic of `src_endpoint`/`cluster_id`.
92    pub fn matches(&self, src_endpoint: u8, cluster_id: u16) -> bool {
93        self.src_endpoint == src_endpoint && self.cluster_id == cluster_id
94    }
95}
96
97/// The binding table as held in the AIB.
98pub type BindingTable<const N: usize> = StorageVec<Binding, N>;
99
100/// Create a binding link (2.2.4.3.1), rejecting an exact duplicate.
101pub(crate) fn create_binding_link<const N: usize>(
102    table: &mut BindingTable<N>,
103    binding: Binding,
104) -> Result<(), BindingError> {
105    if table.contains(&binding) {
106        return Ok(());
107    }
108    table.push(binding).map_err(|_| BindingError::TableFull)
109}
110
111/// Remove a binding link (2.2.4.3.3).
112pub(crate) fn remove_binding_link<const N: usize>(
113    table: &mut BindingTable<N>,
114    binding: &Binding,
115) -> Result<(), BindingError> {
116    let Some(index) = table.iter().position(|entry| entry == binding) else {
117        return Err(BindingError::InvalidBinding);
118    };
119    table.remove(index);
120    Ok(())
121}
122
123#[derive(Error, Debug)]
124#[error("SourceError")]
125pub(crate) enum BindingError {
126    IllegalRequest,
127    InvalidBinding,
128    TableFull,
129}