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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Bluetooth Mesh Addresses.
//! All address are 16-bit except for Virtual Addresses. Virtual Address are 128-bit UUIDs but only
//! a 16-bit hash of the UUID is sent with message.
//!
//! | Bits (16)             | Type          |
//! | --------------------- | ------------- |
//! | 0b0000 0000 0000 0000 | Unassigned    |
//! | 0b0xxx xxxx xxxx xxxx | Unicast       |
//! | 0b10xx xxxx xxxx xxxx | Virtual       |
//! | 0b11xx xxxx xxxx xxxx | Group         |
//!
//! Endian depends on layer!!
//! Little: Access/Foundation
//! Big: Everything else
use crate::bytes::ToFromBytesEndian;
use crate::crypto::aes::AESCipher;
use crate::crypto::k_funcs::VTAD;
use crate::uuid::UUID;
use core::convert::{TryFrom, TryInto};

pub const ADDRESS_LEN: usize = 2;

const UNICAST_BIT: u16 = 0x8000;
const UNICAST_MASK: u16 = !UNICAST_BIT;

const GROUP_BIT: u16 = 0xC000;
const GROUP_MASK: u16 = !GROUP_BIT;

const VIRTUAL_BIT: u16 = 0x8000;
const VIRTUAL_MASK: u16 = GROUP_MASK;

/// Element Unicast Address. Each Element has one Unicast assigned to it.
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct UnicastAddress(u16);
/// Group Address. Some Group Address are reserved.
///
/// | Values        | Group Name    |
/// | ------------- | ------------- |
/// | 0xFF00-0xFFFB | RFU           |
/// | 0xFFFC        | All Proxies   |
/// | 0xFFFD        | All Friends   |
/// | 0xFFFE        | All Relays    |
/// | 0xFFFF        | All Nodes     |
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct GroupAddress(u16);
impl GroupAddress {
    /// # Panics
    /// Panics if `group_address` isn't a value group address.
    pub fn new(group_address: u16) -> Self {
        match Self::try_from(group_address) {
            Ok(g) => g,
            Err(_) => panic!("invalid group address given"),
        }
    }
    /// Group address corresponding to all proxies nodes.
    pub const fn all_proxies() -> GroupAddress {
        GroupAddress(0xFFFC)
    }
    /// Group address corresponding to all friends nodes.
    pub const fn all_friends() -> GroupAddress {
        GroupAddress(0xFFFD)
    }
    /// Group address corresponding to all relay nodes.
    pub const fn all_relays() -> GroupAddress {
        GroupAddress(0xFFE)
    }
    /// Group address corresponding to all nodes.
    pub const fn all_nodes() -> GroupAddress {
        GroupAddress(0xFFFF)
    }
}
const VIRTUAL_ADDRESS_HASH_MAX: u16 = (1_u16 << 14) - 1;
/// Only stores the 14 bit hash of the virtual UUID.
/// For the full 128 bit UUID, look at [`VirtualAddress`]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct VirtualAddressHash(u16);
impl VirtualAddressHash {
    /// Create a 14 bit `VirtualAddressHash` from the 16 bit input.
    /// # Panics
    /// Panics if `address > VIRTUAL_ADDRESS_HASH_MAX`.
    pub fn new(address: u16) -> VirtualAddressHash {
        assert!(address <= VIRTUAL_ADDRESS_HASH_MAX);
        VirtualAddressHash(address)
    }
    /// Creates a 14 bit `VirtualAddressHash` by masking a u16 to a u14.
    pub fn new_masked(address: u16) -> VirtualAddressHash {
        VirtualAddressHash(address & VIRTUAL_ADDRESS_HASH_MAX)
    }
}
/// Stores the 14-bit hash and full 128 bit virtual UUID. Only the 14-bit hash is sent with
/// messages over the air. During the application decryption process, the UUID is supplied to the
/// AES CCM decryptor as associated data. If the hash matches but the decryption fails (MIC doesn't
/// match), the message doesn't belong to that VirtualAddress.
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct VirtualAddress(VirtualAddressHash, UUID);
impl VirtualAddress {
    /// Creates a Virtual Address by calculate the hash of the UUID (using AES CMAC).
    pub fn hash_uuid(uuid: &UUID) -> VirtualAddressHash {
        let k = AESCipher::from(VTAD).cmac(uuid.as_ref());
        VirtualAddressHash::new_masked(u16::from_be_bytes([k.as_ref()[15], k.as_ref()[14]]))
    }
    pub fn new(uuid: &UUID) -> VirtualAddress {
        VirtualAddress(Self::hash_uuid(uuid), uuid.clone())
    }
    fn new_parts(hash: VirtualAddressHash, uuid: &UUID) -> Self {
        VirtualAddress(hash, *uuid)
    }
    pub fn uuid(&self) -> &UUID {
        &self.1
    }
    pub fn hash(&self) -> VirtualAddressHash {
        self.0
    }
}
impl AsRef<UUID> for VirtualAddress {
    fn as_ref(&self) -> &UUID {
        &self.1
    }
}
impl From<&UUID> for VirtualAddress {
    fn from(uuid: &UUID) -> Self {
        Self::new(uuid)
    }
}
impl UnicastAddress {
    /// Creates a new `UnicastAddress`.
    /// # Panics
    /// Panics if the `u16` is not a valid `UnicastAddress`. (Panics if `u16==0 || u16&UNICAST_BIT!=0`)
    #[must_use]
    pub fn new(v: u16) -> UnicastAddress {
        assert!((v & UNICAST_BIT) != 0 || v == 0, "non unicast address");
        UnicastAddress(v)
    }
    /// Creates a Unicast address by masking any u16 into it.
    /// # Panics
    /// Panics if the `u16` masked equals `0`.
    #[must_use]
    pub fn from_mask_u16(v: u16) -> UnicastAddress {
        assert_ne!(v & UNICAST_MASK, 0, "unassigned unicast address");
        UnicastAddress(v & UNICAST_MASK)
    }
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
pub struct AddressError(());
impl TryFrom<u16> for UnicastAddress {
    type Error = AddressError;

    fn try_from(v: u16) -> Result<UnicastAddress, Self::Error> {
        if v == 0 {
            Err(AddressError(()))
        } else if v & UNICAST_BIT == 0 {
            Ok(UnicastAddress(v))
        } else {
            Err(AddressError(()))
        }
    }
}

impl TryFrom<u16> for GroupAddress {
    type Error = AddressError;

    fn try_from(v: u16) -> Result<GroupAddress, Self::Error> {
        if v & 0xC000 == 0xC000 {
            Ok(GroupAddress(v))
        } else {
            Err(AddressError(()))
        }
    }
}

impl TryFrom<u16> for VirtualAddressHash {
    type Error = AddressError;
    fn try_from(v: u16) -> Result<VirtualAddressHash, Self::Error> {
        if v & 0xC000 == 0x8000 {
            Ok(VirtualAddressHash(v))
        } else {
            Err(AddressError(()))
        }
    }
}

impl From<UnicastAddress> for u16 {
    #[must_use]
    fn from(v: UnicastAddress) -> Self {
        v.0
    }
}
impl From<GroupAddress> for u16 {
    #[must_use]
    fn from(v: GroupAddress) -> Self {
        v.0
    }
}
impl From<VirtualAddressHash> for u16 {
    #[must_use]
    fn from(v: VirtualAddressHash) -> Self {
        v.0
    }
}
impl From<VirtualAddress> for u16 {
    #[must_use]
    fn from(v: VirtualAddress) -> Self {
        (v.0).0
    }
}
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum Address {
    Unassigned,
    Unicast(UnicastAddress),
    Group(GroupAddress),
    Virtual(VirtualAddress),
    VirtualHash(VirtualAddressHash),
}

impl Address {
    #[must_use]
    pub fn is_assigned(&self) -> bool {
        match self {
            Address::Unassigned => false,
            _ => true,
        }
    }
    #[must_use]
    pub fn is_unicast(&self) -> bool {
        match self {
            Address::Unicast(_) => true,
            _ => false,
        }
    }

    #[must_use]
    pub fn is_group(&self) -> bool {
        match self {
            Address::Group(_) => true,
            _ => false,
        }
    }

    #[must_use]
    pub fn is_virtual(&self) -> bool {
        match self {
            Address::Virtual(_) => true,
            Address::VirtualHash(_) => true,
            _ => false,
        }
    }

    #[must_use]
    pub fn is_full_virtual(&self) -> bool {
        match self {
            Address::Virtual(_) => true,
            _ => false,
        }
    }
    #[must_use]
    pub fn virtual_hash(&self) -> Option<VirtualAddressHash> {
        match self {
            Address::Virtual(v) => Some(v.0),
            Address::VirtualHash(h) => Some(*h),
            _ => None,
        }
    }
    #[must_use]
    pub fn unicast(&self) -> Option<UnicastAddress> {
        match self {
            Address::Unicast(u) => Some(*u),
            _ => None,
        }
    }
    #[must_use]
    pub fn group(&self) -> Option<GroupAddress> {
        match self {
            Address::Group(g) => Some(*g),
            _ => None,
        }
    }
    #[must_use]
    pub fn value(&self) -> u16 {
        self.into()
    }
}

impl Default for Address {
    #[must_use]
    fn default() -> Self {
        Address::Unassigned
    }
}

impl From<u16> for Address {
    #[must_use]
    fn from(v: u16) -> Address {
        if v == 0 {
            Address::Unassigned
        } else if v & GROUP_BIT == 0 {
            Address::Unicast(UnicastAddress(v))
        } else if v & GROUP_BIT == GROUP_BIT {
            Address::Group(GroupAddress(v))
        } else {
            Address::VirtualHash(VirtualAddressHash(v))
        }
    }
}

impl From<&Address> for u16 {
    #[must_use]
    fn from(v: &Address) -> Self {
        match v {
            Address::Unassigned => 0,
            Address::Unicast(u) => u.0,
            Address::Group(g) => g.0,
            Address::Virtual(v) => (v.0).0,
            Address::VirtualHash(vh) => vh.0,
        }
    }
}

impl TryFrom<&Address> for UnicastAddress {
    type Error = AddressError;

    fn try_from(value: &Address) -> Result<Self, Self::Error> {
        match value {
            Address::Unicast(u) => Ok(*u),
            _ => Err(AddressError(())),
        }
    }
}
impl TryFrom<&Address> for VirtualAddressHash {
    type Error = AddressError;

    fn try_from(value: &Address) -> Result<Self, Self::Error> {
        match value {
            Address::VirtualHash(h) => Ok(*h),
            _ => Err(AddressError(())),
        }
    }
}
impl TryFrom<&Address> for VirtualAddress {
    type Error = AddressError;

    fn try_from(value: &Address) -> Result<Self, Self::Error> {
        match value {
            Address::Virtual(v) => Ok(*v),
            _ => Err(AddressError(())),
        }
    }
}
impl TryFrom<&Address> for GroupAddress {
    type Error = AddressError;

    fn try_from(value: &Address) -> Result<Self, Self::Error> {
        match value {
            Address::Group(g) => Ok(*g),
            _ => Err(AddressError(())),
        }
    }
}
impl ToFromBytesEndian for Address {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        match self {
            Address::Unassigned => 0_u16.to_bytes_le(),
            Address::Unicast(u) => (u.0).to_bytes_le(),
            Address::Group(g) => (g.0).to_bytes_le(),
            Address::Virtual(v) => ((v.0).0).to_bytes_le(),
            Address::VirtualHash(h) => (h.0).to_bytes_le(),
        }
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        match self {
            Address::Unassigned => 0_u16.to_bytes_be(),
            Address::Unicast(u) => (u.0).to_bytes_be(),
            Address::Group(g) => (g.0).to_bytes_be(),
            Address::Virtual(v) => ((v.0).0).to_bytes_be(),
            Address::VirtualHash(h) => (h.0).to_bytes_be(),
        }
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        Some(u16::from_bytes_le(bytes)?.into())
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        Some(u16::from_bytes_be(bytes)?.into())
    }
}

impl ToFromBytesEndian for UnicastAddress {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_le(bytes)?.try_into().ok()
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_be(bytes)?.try_into().ok()
    }
}

impl ToFromBytesEndian for VirtualAddressHash {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_le(bytes)?.try_into().ok()
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_be(bytes)?.try_into().ok()
    }
}
impl ToFromBytesEndian for GroupAddress {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_le(bytes)?.try_into().ok()
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        u16::from_bytes_be(bytes)?.try_into().ok()
    }
}