zigbee_core/aps/
binding.rs1use thiserror::Error;
7use zigbee_macros::impl_byte;
8use zigbee_types::IeeeAddress;
9use zigbee_types::StorageVec;
10
11impl_byte! {
12 #[tag(u8)]
13 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
15 pub enum BindingAddrMode {
16 Group = 0x01,
18 Device = 0x03,
20 #[fallback = true]
21 Reserved(u8),
22 }
23}
24
25impl BindingAddrMode {
26 pub fn is_group(self) -> bool {
28 self == Self::Group
29 }
30
31 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 #[derive(Debug, Clone, Default, PartialEq, Eq)]
46 pub struct Binding {
47 pub src_endpoint: u8,
49 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 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 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 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
97pub type BindingTable<const N: usize> = StorageVec<Binding, N>;
99
100pub(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
111pub(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}