1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! Ember key types and structures.
use core::num::TryFromIntError;
use core::time::Duration;
use le_stream::{FromLeStream, ToLeStream};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use crate::ember::types::Eui64;
/// Type alias for Ember key data.
pub type Data = [u8; 16];
/// Ember key type.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum Type {
/// A shared key between the Trust Center and a device.
TrustCenterLinkKey = 0x01,
/// A shared secret used for deriving keys between the Trust Center and a device.
TrustCenterMasterKey = 0x02,
/// The current active Network Key used by all devices in the network.
CurrentNetworkKey = 0x03,
/// The alternate Network Key that was previously in use, or the newer key that will be switched to.
NextNetworkKey = 0x04,
/// An Application Link Key shared with another (non-Trust Center) device.
ApplicationLinkKey = 0x05,
/// An Application Master Key shared secret used to derive an Application Link Key.
ApplicationMasterKey = 0x06,
}
impl From<Type> for u8 {
fn from(typ: Type) -> Self {
typ as Self
}
}
impl TryFrom<u8> for Type {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_u8(value).ok_or(value)
}
}
/// Ember key struct bitmask.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Ord, PartialOrd, FromPrimitive)]
#[repr(u16)]
pub enum Bitmask {
/// The key has a sequence number associated with it.
HasSequenceNumber = 0x00001,
/// The key has an outgoing frame counter associated with it.
HasOutgoingFrameCounter = 0x0002,
/// The key has an incoming frame counter associated with it.
HasIncomingFrameCounter = 0x0004,
/// The key has a Partner IEEE address associated with it.
HasPartnerEui64 = 0x0008,
}
impl From<Bitmask> for u16 {
fn from(bitmask: Bitmask) -> Self {
bitmask as Self
}
}
impl TryFrom<u16> for Bitmask {
type Error = u16;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Self::from_u16(value).ok_or(value)
}
}
/// A structure containing a key and its associated data.
#[derive(Clone, Debug, Eq, PartialEq, FromLeStream, ToLeStream)]
pub struct Struct {
bitmask: u16,
typ: u8,
key: Data,
outgoing_frame_counter: u32,
incoming_frame_counter: u32,
sequence_number: u8,
partner_eui64: Eui64,
}
impl Struct {
/// Creates a new Ember key struct.
#[must_use]
pub fn new(
bitmask: Bitmask,
typ: Type,
key: Data,
outgoing_frame_counter: u32,
incoming_frame_counter: u32,
sequence_number: u8,
partner_eui64: Eui64,
) -> Self {
Self {
bitmask: bitmask.into(),
typ: typ.into(),
key,
outgoing_frame_counter,
incoming_frame_counter,
sequence_number,
partner_eui64,
}
}
/// Return a bitmask indicating the presence of data within the various fields in the structure.
///
/// # Errors
/// Returns the number of the bitmask if the bitmask is invalid.
pub fn bitmask(&self) -> Result<Bitmask, u16> {
Bitmask::try_from(self.bitmask)
}
/// Return the type of the key.
///
/// # Errors
/// Returns the number of the type if the type is invalid.
pub fn typ(&self) -> Result<Type, u8> {
Type::try_from(self.typ)
}
/// Return the actual key data.
#[must_use]
pub const fn key(&self) -> &Data {
&self.key
}
/// Return the outgoing frame counter associated with the key.
#[must_use]
pub const fn outgoing_frame_counter(&self) -> u32 {
self.outgoing_frame_counter
}
/// Return the frame counter of the partner device associated with the key.
#[must_use]
pub const fn incoming_frame_counter(&self) -> u32 {
self.incoming_frame_counter
}
/// Return the sequence number associated with the key.
#[must_use]
pub const fn sequence_number(&self) -> u8 {
self.sequence_number
}
/// Return the IEEE address of the partner device also in possession of the key.
#[must_use]
pub const fn partner_eui64(&self) -> Eui64 {
self.partner_eui64
}
}
/// The transient key data structure.
#[derive(Clone, Debug, Eq, PartialEq, FromLeStream, ToLeStream)]
pub struct TransientData {
eui64: Eui64,
key_data: Data,
bitmask: u16,
remaining_time_seconds: u16,
network_index: u8,
}
impl TransientData {
/// Create new transient data.
#[must_use]
pub fn new(
eui64: Eui64,
key_data: Data,
bitmask: Bitmask,
remaining_time_seconds: u16,
network_index: u8,
) -> Self {
Self {
eui64,
key_data,
bitmask: bitmask.into(),
remaining_time_seconds,
network_index,
}
}
/// Try to create a new transient data.
///
/// # Errors
/// Returns a [`TryFromIntError`] if the remaining time is too large.
pub fn try_new(
eui64: Eui64,
key_data: Data,
bitmask: Bitmask,
remaining_time: Duration,
network_index: u8,
) -> Result<Self, TryFromIntError> {
Ok(Self {
eui64,
key_data,
bitmask: bitmask.into(),
remaining_time_seconds: remaining_time.as_secs().try_into()?,
network_index,
})
}
/// Return the IEEE address paired with the transient link key.
#[must_use]
pub const fn eui64(&self) -> Eui64 {
self.eui64
}
/// Return the key data structure matching the transient key.
#[must_use]
pub const fn data(&self) -> &Data {
&self.key_data
}
/// Returns the bitmask.
///
/// This bitmask indicates whether various fields in the structure contain valid data.
///
/// # Errors
/// Returns the number of the bitmask if the bitmask is invalid.
pub fn bitmask(&self) -> Result<Bitmask, u16> {
Bitmask::try_from(self.bitmask)
}
/// Return the number of seconds remaining before the key is automatically
/// timed out of the transient key table.
#[must_use]
pub const fn remaining_time_seconds(&self) -> u16 {
self.remaining_time_seconds
}
/// Return the time remaining before the key is automatically
/// timed out of the transient key table.
#[must_use]
pub const fn remaining_time(&self) -> Duration {
Duration::from_secs(self.remaining_time_seconds as u64)
}
/// Return the network index indicates which NWK uses this key.
#[must_use]
pub const fn network_index(&self) -> u8 {
self.network_index
}
}
/// Ember key status.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
#[repr(u8)]
pub enum Status {
/// The application link key has been established.
AppLinkKeyEstablished = 0x01,
/// The Trust Center link key has been established.
TrustCenterLinkKeyEstablished = 0x03,
/// The key establishment process has timed out.
KeyEstablishmentTimeout = 0x04,
/// The key table is full.
KeyTableFull = 0x05,
/// Trust Center status.
TrustCenter(TrustCenter),
}
impl From<Status> for u8 {
fn from(status: Status) -> Self {
match status {
Status::AppLinkKeyEstablished => 0x01,
Status::TrustCenterLinkKeyEstablished => 0x03,
Status::KeyEstablishmentTimeout => 0x04,
Status::KeyTableFull => 0x05,
Status::TrustCenter(trust_center) => trust_center.into(),
}
}
}
impl FromPrimitive for Status {
fn from_i64(n: i64) -> Option<Self> {
Self::from_u64(n.try_into().ok()?)
}
fn from_u64(n: u64) -> Option<Self> {
match n {
0x01 => Some(Self::AppLinkKeyEstablished),
0x03 => Some(Self::TrustCenterLinkKeyEstablished),
0x04 => Some(Self::KeyEstablishmentTimeout),
0x05 => Some(Self::KeyTableFull),
n => TrustCenter::from_u64(n).map(Self::TrustCenter),
}
}
}
impl TryFrom<u8> for Status {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_u8(value).ok_or(value)
}
}
/// Ember key Trust Center status.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum TrustCenter {
/// The Trust Center has responded to request for an application key.
RespondedToKeyRequest = 0x06,
/// The Trust Center has sent the application key to the requester.
AppKeySentToRequester = 0x07,
/// The Trust Center has failed to respond to a request for an application key.
ResponseToKeyRequestFailed = 0x08,
/// The Trust Center does not support the key type requested.
RequestKeyTypeNotSupported = 0x09,
/// The Trust Center does not have a link key for the requester.
NoLinkKeyForRequester = 0x0A,
/// The Trust Center does not have a EUI64 for the requester.
RequesterEui64Unknown = 0x0B,
/// The Trust Center has received the first application key request.
ReceivedFirstAppKeyRequest = 0x0C,
/// The Trust Center has timed out waiting for the second application key request.
TimeoutWaitingForSecondAppKeyRequest = 0x0D,
/// The Trust Center has received a request for an application key that does not match the key it is expecting.
NonMatchingAppKeyRequestReceived = 0x0E,
/// The Trust Center has failed to send the application keys.
FailedToSendAppKeys = 0x0F,
/// The Trust Center has failed to store the application key request.
FailedToStoreAppKeyRequest = 0x10,
/// The Trust Center has rejected the application key request.
RejectedAppKeyRequest = 0x11,
}
impl From<TrustCenter> for u8 {
fn from(tc: TrustCenter) -> Self {
tc as Self
}
}
impl TryFrom<u8> for TrustCenter {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_u8(value).ok_or(value)
}
}