zigbee/
lib.rs

1//! This library aims to provide a standard API for the interoperation of
2//! other crates that implements (parts of) the Zigbee network, that
3//! provides a protocol stack to be used in internet of things applications.
4//! Its architecture follow the stack as proposed in the Zigbee
5//! Specification document attached in the doc directory.
6//! MAC and PHY layers are provided by the IEEE 802.15.4-2003 standard.
7//! This library covers the upper layes as the Zigbee Standard does.
8
9use std::time::Duration;
10/// This module focus on the application layer.
11pub mod apl {
12    /// This module provides the Frames and the primitives of the
13    /// Application Support sub-layer Data Entity and Mangement Entity
14    pub mod aps {
15        mod frame_format{
16            pub enum FrameType {
17                Data,
18                Command,
19                Acknowledgement
20            }
21            pub enum DeliveryMode {
22                NormalUnicastDelivery = 0,
23                Broadcast = 2,
24                GroupAddressing = 3
25            }
26            pub struct FrameControlField {
27                pub frame_type:  FrameType, //this should be a bit bield of 2 bits
28                pub delivery_mode: DeliveryMode,
29                pub ack_format: bool, //false for data frame acknowledgement and true for APS command frame acknowledgement.
30                pub security: bool,
31                pub ack_request: bool,
32                pub extended_header_present: bool
33            }
34            pub struct AddressingFields {
35                pub destination_endpoint: Option<u8>, // only for broadcast or normal unicast
36                pub group_address: Option<u16>, // only for group addressing
37                pub cluster_identifier: Option<u16>, // only for data or ack frames
38                pub profile_identifier: Option<u16>, // only for data or ack frames
39                pub source_endpoint: u8 
40            }
41
42            pub enum FragmentationField {
43                NotFragmented,
44                FirstPart,
45                NotFirstPart
46            }
47            pub struct AckBitfield {
48
49            }
50            pub struct ExtHeader {
51                pub fragmentation: FragmentationField,
52                pub block_number: Option<u8>,
53                pub ack_bitfield: Option<AckBitfield>
54            }
55            
56            pub struct Apdu<'a> {
57                pub frame_control: FrameControlField,
58                pub address: AddressingFields,
59                pub aps_counter: u8,
60                pub extended_header: Option<ExtHeader>,
61                pub frame_payload: &'a[u8]
62            }
63        }
64
65        #[derive(Clone, Copy)]
66        pub enum AddrAndEp{
67            None,
68            ShortAddressNoEp(u16),
69            ShortAddressWithEp(u16, u8),
70            LongAddress(u64, u8)
71        }
72
73        pub enum SecurityStatus {
74            Unsecured,
75            SecuredNwkKey,
76            SecuredLinkKey
77        }
78
79        pub enum IndicationStatus {
80            Success,
81            DefragUnupported,
82            DefragDeferred,
83            SecurityProcessingError/*(Error)*/// error 
84        }
85
86        pub struct TxOptions {
87            pub security_enabled: bool,
88            pub use_nwk_key: bool,
89            pub acknowledged_transmission: bool,
90            pub fragmentation_permitted: bool,
91            pub include_extended_nonce_in_aps_security_frame: bool,
92        }
93
94        /// The information that the APS provides to the upper layer when a
95        /// new frame is received. This is transmitted to the NHLE issuing the
96        /// APSDE-DATA.indication primitive, calling the function provided on
97        /// the registration of the interested endpoint.
98        pub struct DataIndication<'a> {
99            pub dst_addr_ep: AddrAndEp,
100            pub src_addr_ep: AddrAndEp,
101            pub profile_id: u16,
102            pub cluster_id: u16,
103            pub asdu: &'a[u8],
104            pub status: IndicationStatus,
105            pub security_status: SecurityStatus,
106            pub link_quality: u8,
107            pub rx_time: ::Duration
108        }
109
110        pub enum DataConfirmStatus {
111            Success,
112            NoShortAddress,
113            NoBoundDevice,
114            SecurityFail,
115            NoAck,
116            AsduTooLong, //Application service data unit, specification contains a typo
117            NldeError/*(Error)*/
118        }
119
120        /// The data contained in the APSDE-DATA.confirm primitive that is issued by the
121        /// APS to the Next Higher Layer Entity in response to a APSDE-DATA.request
122        pub struct DataConfirm {
123            pub dst: AddrAndEp,
124            pub src_endpoint: u8,
125            pub status: DataConfirmStatus,
126            pub tx_time: ::Duration
127        }
128
129        /// The arguments needed to issue the APSDE-DATA.request primitive
130        pub struct DataRequest<'a> {
131            pub dst: AddrAndEp,
132            pub profile_id: u16,
133            pub cluster_id: u16,
134            pub src_endpoint: u8,
135            pub asdu: &'a[u8],
136            pub options: TxOptions,
137            pub radius: u8
138        }
139
140        /// This trait is implemented by all these structs that implements an
141        /// Application Support sub-layer Data Entity in order to provide the
142        /// functionalities required by the specification.
143        ///
144        /// Crates that need the functionalities of a zigbee APSDE can rely on this trait.
145        pub trait ApsdeSap{
146            fn data_request(&self, request: DataRequest) -> DataConfirm;
147            fn register_application_object(&self, indication_callback: Fn(DataIndication));
148        }
149
150        pub struct BindRequest {
151            pub src_addr: u64,
152            pub src_endpoint: u8,
153            pub cluster_id: u16,
154            pub dst_addr: AddrAndEp
155        }
156
157        pub enum BindStatus {
158            Success,
159            IllegalRequest,
160            TableFull,
161            NotSupported
162        }
163        
164        pub struct BindConfirm {
165            pub status: BindStatus,
166            pub src_addr: u64,
167            pub src_endpoint: u8,
168            pub cluster_id: u16,
169            pub dst_addr: AddrAndEp
170        }
171
172        type UnbindRequest = BindRequest;
173
174        pub enum UnbindStatus {
175            Success,
176            IllegalRequest,
177            InvalidBinding
178        }
179
180        pub struct UnbindConfirm {
181            pub status: UnbindStatus,
182            pub src_addr: u64,
183            pub src_endpoint: u8,
184            pub cluster_id: u16,
185            pub dst_addr: AddrAndEp
186        }
187
188        pub struct BindingTable {
189        }
190        pub struct ChannelMask {
191        }
192        pub struct GroupTable<'a> {
193            entries: &'a Iterator<Item=(u16, Iterator<Item = u8>)>
194        }
195        pub struct PermissionsConfiguration {
196        }
197
198        pub enum AddGroupStatus {
199            Success,
200            InvalidParameter,
201            TableFull
202        }
203        pub struct AddGroupConfirm{
204            pub status: AddGroupStatus,
205            pub group_address: u16,
206            pub endpoint: u8
207        }
208
209        pub enum RemoveGroupStatus {
210            Success,
211            InvalidGroup,
212            InvalidParameter,
213        }
214        pub struct RemoveGroupConfirm{
215            pub status: RemoveGroupStatus,
216            pub group_address: u16,
217            pub endpoint: u8
218        }
219
220        pub enum RemoveAllGroupsStatus {
221            Success,
222            InvalidParameter
223        }
224        pub struct RemoveAllGroupsConfirm{
225            pub status: RemoveAllGroupsStatus,
226            pub endpoint: u8
227        }
228
229        pub trait ApsmeSap{
230            fn bind_request(&self, request: BindRequest) -> BindConfirm;
231            fn unbind_request(&self, request: UnbindRequest) -> UnbindConfirm;
232            fn binding_table(&self) -> Option<BindingTable> { None }
233            fn designated_coordinator(&self) -> bool { false }
234            fn channel_mask(&self) -> Option<ChannelMask>;
235            fn use_extended_pan_id(&self) -> u64 { 0 }
236            fn group_table(&self) -> Option<GroupTable> { None }
237            fn nonmember_radius(&self) -> u8 { 2 }
238            fn permissions_configuration(&self) -> Option<PermissionsConfiguration>{
239                None
240            }
241            fn use_insecure_join(&self) -> bool { true }
242            fn interframe_delay(&self) -> u8;
243            fn last_channel_energy(&self) -> Option<u8>;
244            fn last_channel_failure_rate(&self) -> Option<f32>;
245            fn channel_timer(&self) -> Option<f32>;
246            fn max_window_size(&self) -> Option<u8>;
247            fn set_binding_table(&self, BindingTable) -> Result<(), ()>;
248            fn set_designated_coordinator(&self, bool) -> Result<(), ()>;
249            fn set_channel_mask(&self, ChannelMask) -> Result<(), ()>;
250            fn set_extended_pan_id(&self, u64) -> Result<(), ()>;
251            fn set_group_table(&self, GroupTable) -> Result<(), ()>;
252            fn set_nonmember_radius(&self, u8) -> Result<(), ()>;
253            fn set_permissions_configuration(&self, PermissionsConfiguration) -> Result<(), ()>;
254            fn set_insecure_join(&self, bool) -> Result<(), ()>;
255            fn set_interframe_delay(&self, u8) -> Result<(), ()>;
256            fn set_last_channel_energy(&self, u8) -> Result<(), ()>;
257            fn set_last_channel_failure_rate(&self, f32) -> Result<(), ()>;
258            fn set_channel_timer(&self, f32) -> Result<(), ()>;
259            fn set_max_window_size(&self, u8) -> Result<(), ()>;
260            fn add_group_request(&self, group_adderss: u16, endpoint: u8) -> AddGroupConfirm;
261            fn remove_group_request(&self, group_adderss: u16, endpoint: u8) -> RemoveGroupConfirm;
262            fn remove_all_groups_request(&self, endpoint: u8) -> RemoveAllGroupsConfirm;
263        }
264        pub const MAX_DESCRIPTOR_SIZE: usize = 64;
265        pub const MAX_FRAME_RETRIES: u32 = 3;
266        pub const MIN_DUPLICATE_REJECTION_TABLE_SIZE: usize = 1;
267        pub const MIN_HEADER_OVERHEAD: usize = 0x0C;
268    }
269}
270/// This module contains traits and data structures for the network layer.
271pub mod nwk{
272    pub trait NlmeSap{
273        fn nlme_get(&self);
274        fn nlme_set(&self);
275    }
276}