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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::str::FromStr;

#[cfg(feature = "matches")]
mod matches;

/// Parse error for [`BdAddr::from_str`]
#[derive(Debug, thiserror::Error)]
#[error("failed to parse address")]
pub struct AddressParseError;

/// Invalid bits for this address type.
#[derive(Debug, thiserror::Error)]
#[error("Invalid bits for this address type. (expect: 0b{0:02b}, but 0b{1:02b})")]
pub struct InvalidBitsForAddressType(u8, u8);

/// Bluetooth Device Address without Address type.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct BdAddr([u8; 6]);

impl BdAddr {
    /// Convert as BR/EDR Address.
    pub fn to_br_edr_addr(self) -> Address {
        Address::BrEdr(self)
    }

    /// Convert as LE Public Device Address.
    pub fn to_le_public_addr(self) -> Address {
        Address::LePublic(PublicDeviceAddress(self))
    }

    /// Convert as LE Random Device Address.
    pub fn to_le_random_addr(self) -> Address {
        Address::LeRandom(RandomDeviceAddress::new(self))
    }
}

impl From<[u8; 6]> for BdAddr {
    fn from(v: [u8; 6]) -> Self {
        Self(v)
    }
}

impl From<BdAddr> for [u8; 6] {
    fn from(v: BdAddr) -> Self {
        v.0
    }
}

impl fmt::Display for BdAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
            self.0[5], self.0[4], self.0[3], self.0[2], self.0[1], self.0[0]
        )
    }
}

impl fmt::Debug for BdAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl FromStr for BdAddr {
    type Err = AddressParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s
            .splitn(6, ':')
            .map(|v| u8::from_str_radix(v, 16))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|_| AddressParseError)?;
        parts.reverse();
        Ok(Self(parts.try_into().map_err(|_| AddressParseError)?))
    }
}

impl TryFrom<&str> for BdAddr {
    type Error = AddressParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// LE Public Device Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PublicDeviceAddress(BdAddr);

impl From<[u8; 6]> for PublicDeviceAddress {
    fn from(v: [u8; 6]) -> Self {
        Self(v.into())
    }
}

impl From<PublicDeviceAddress> for Address {
    fn from(v: PublicDeviceAddress) -> Self {
        Self::LePublic(v)
    }
}

impl fmt::Display for PublicDeviceAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// LE Non-Resolvable Private Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NonResolvablePrivateAddress(BdAddr);

impl NonResolvablePrivateAddress {
    const TAG: u8 = 0b00;
}

impl TryFrom<[u8; 6]> for NonResolvablePrivateAddress {
    type Error = InvalidBitsForAddressType;

    fn try_from(v: [u8; 6]) -> Result<Self, Self::Error> {
        if (v[5] & 0xC0) >> 6 == Self::TAG {
            Ok(Self(v.into()))
        } else {
            Err(InvalidBitsForAddressType(Self::TAG, (v[5] & 0xC0) >> 6))
        }
    }
}

impl From<NonResolvablePrivateAddress> for RandomDeviceAddress {
    fn from(v: NonResolvablePrivateAddress) -> Self {
        Self::NonResolvable(v)
    }
}

impl fmt::Display for NonResolvablePrivateAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// LE Resolvable Private Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ResolvablePrivateAddress(BdAddr);

impl ResolvablePrivateAddress {
    const TAG: u8 = 0b01;
}

impl TryFrom<[u8; 6]> for ResolvablePrivateAddress {
    type Error = InvalidBitsForAddressType;

    fn try_from(v: [u8; 6]) -> Result<Self, Self::Error> {
        if (v[5] & 0xC0) >> 6 == Self::TAG {
            Ok(Self(v.into()))
        } else {
            Err(InvalidBitsForAddressType(Self::TAG, (v[5] & 0xC0) >> 6))
        }
    }
}

impl From<ResolvablePrivateAddress> for RandomDeviceAddress {
    fn from(v: ResolvablePrivateAddress) -> Self {
        Self::Resolvable(v)
    }
}

impl fmt::Display for ResolvablePrivateAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// LE Static Device Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StaticDeviceAddress(BdAddr);

impl StaticDeviceAddress {
    const TAG: u8 = 0b11;
}

impl TryFrom<[u8; 6]> for StaticDeviceAddress {
    type Error = InvalidBitsForAddressType;

    fn try_from(v: [u8; 6]) -> Result<Self, Self::Error> {
        if (v[5] & 0xC0) >> 6 == Self::TAG {
            Ok(Self(v.into()))
        } else {
            Err(InvalidBitsForAddressType(Self::TAG, (v[5] & 0xC0) >> 6))
        }
    }
}

impl From<StaticDeviceAddress> for RandomDeviceAddress {
    fn from(v: StaticDeviceAddress) -> Self {
        Self::Static(v)
    }
}

impl fmt::Display for StaticDeviceAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// LE Random Device Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RandomDeviceAddress {
    /// Non-Resolvable Private Address
    NonResolvable(NonResolvablePrivateAddress),

    /// Resolvable Private Address
    Resolvable(ResolvablePrivateAddress),

    /// Static Device Address
    Static(StaticDeviceAddress),

    /// Unknown
    Unknown(BdAddr),
}

impl RandomDeviceAddress {
    fn new(addr: BdAddr) -> Self {
        match (addr.0[5] & 0xC0) >> 6 {
            NonResolvablePrivateAddress::TAG => {
                Self::NonResolvable(NonResolvablePrivateAddress(addr))
            }
            ResolvablePrivateAddress::TAG => Self::Resolvable(ResolvablePrivateAddress(addr)),
            StaticDeviceAddress::TAG => Self::Static(StaticDeviceAddress(addr)),
            _ => Self::Unknown(addr),
        }
    }
}

impl From<[u8; 6]> for RandomDeviceAddress {
    fn from(v: [u8; 6]) -> Self {
        Self::new(v.into())
    }
}

impl From<RandomDeviceAddress> for Address {
    fn from(v: RandomDeviceAddress) -> Self {
        Self::LeRandom(v)
    }
}

impl fmt::Display for RandomDeviceAddress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NonResolvable(v) => v.fmt(f),
            Self::Resolvable(v) => v.fmt(f),
            Self::Static(v) => v.fmt(f),
            Self::Unknown(v) => v.fmt(f),
        }
    }
}

/// Address type for [`Address`]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AddressType {
    /// Classic BR/EDR Address
    BrEdr,

    /// LE Public Device Address
    LePublic,

    /// LE Random Device Address
    LeRandom,
}

/// Bluetooth Device Address
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Address {
    /// Classic BR/EDR Address
    BrEdr(BdAddr),

    /// LE Public Device Address
    LePublic(PublicDeviceAddress),

    /// LE Random Device Address
    LeRandom(RandomDeviceAddress),
}

impl Address {
    /// Construct Classic BR/EDR Address from bytes.
    pub fn bredr_from(b: [u8; 6]) -> Self {
        Self::BrEdr(b.into())
    }

    /// Construct LE Public Device Address from bytes.
    pub fn le_public_from(b: [u8; 6]) -> Self {
        Self::LePublic(b.into())
    }

    /// Construct LE Random Device Address from bytes.
    pub fn le_random_from(b: [u8; 6]) -> Self {
        Self::LeRandom(b.into())
    }

    /// Construct Classic BR/EDR Address from str.
    pub fn bredr_from_str(s: &str) -> Result<Self, AddressParseError> {
        Ok(Self::BrEdr(s.parse()?))
    }

    /// Construct LE Public Device Address from str.
    ///
    /// ref BLUETOOTH CORE SPECIFICATION | Vol 6, Part B | 1.3 DEVICE ADDRESS
    pub fn le_public_from_str(s: &str) -> Result<Self, AddressParseError> {
        Ok(Self::LePublic(PublicDeviceAddress(s.parse()?)))
    }

    /// Construct LE Public Device Address from str.
    ///
    /// ref BLUETOOTH CORE SPECIFICATION | Vol 6, Part B | 1.3 DEVICE ADDRESS
    pub fn le_random_from_str(s: &str) -> Result<Self, AddressParseError> {
        Ok(Self::LeRandom(RandomDeviceAddress::new(s.parse()?)))
    }

    /// Unwrap inner BdAddr.
    pub fn into_bd_addr(self) -> BdAddr {
        match self {
            Self::BrEdr(addr) => addr,
            Self::LePublic(PublicDeviceAddress(addr)) => addr,
            Self::LeRandom(RandomDeviceAddress::NonResolvable(NonResolvablePrivateAddress(
                addr,
            ))) => addr,
            Self::LeRandom(RandomDeviceAddress::Resolvable(ResolvablePrivateAddress(addr))) => addr,
            Self::LeRandom(RandomDeviceAddress::Static(StaticDeviceAddress(addr))) => addr,
            Self::LeRandom(RandomDeviceAddress::Unknown(addr)) => addr,
        }
    }

    /// Get address type.
    pub fn address_type(&self) -> AddressType {
        match self {
            Self::BrEdr(..) => AddressType::BrEdr,
            Self::LePublic(..) => AddressType::LePublic,
            Self::LeRandom(..) => AddressType::LeRandom,
        }
    }
}

impl fmt::Display for Address {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BrEdr(v) => v.fmt(f),
            Self::LePublic(v) => v.fmt(f),
            Self::LeRandom(v) => v.fmt(f),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_display() {
        let addr = BdAddr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!("55:44:33:22:11:00", addr.clone().to_string());
        assert_eq!([0x00, 0x11, 0x22, 0x33, 0x44, 0x55], <[u8; 6]>::from(addr));
    }

    #[test]
    fn test_parse() {
        let addr = "55:44:33:22:11:00".parse().unwrap();
        assert_eq!(BdAddr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]), addr);
    }

    #[test]
    fn test_bredr_parse() {
        let addr = Address::bredr_from_str("55:44:33:22:11:00").unwrap();
        assert_eq!("55:44:33:22:11:00", addr.to_string());
        assert_eq!("BrEdr(55:44:33:22:11:00)", &format!("{:?}", addr));

        let err = Address::bredr_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").unwrap_err();
        assert_eq!("AddressParseError", &format!("{:?}", err));
        assert_eq!("failed to parse address", &format!("{:}", err));
    }

    #[test]
    fn test_le_public_parse() {
        let addr = Address::le_public_from_str("55:44:33:22:11:00").unwrap();
        assert_eq!("55:44:33:22:11:00", addr.to_string());
        assert_eq!(
            "LePublic(PublicDeviceAddress(55:44:33:22:11:00))",
            &format!("{:?}", addr)
        );

        let err = Address::le_public_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").unwrap_err();
        assert_eq!("AddressParseError", &format!("{:?}", err));
        assert_eq!("failed to parse address", &format!("{:}", err));
    }

    #[test]
    fn test_le_random_nonresolvable_parse() {
        let addr = Address::le_random_from_str("35:44:33:22:11:00").unwrap();
        assert_eq!("35:44:33:22:11:00", addr.to_string());
        assert_eq!(
            "LeRandom(NonResolvable(NonResolvablePrivateAddress(35:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        let err = Address::le_random_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").unwrap_err();
        assert_eq!("AddressParseError", &format!("{:?}", err));
        assert_eq!("failed to parse address", &format!("{:}", err));
    }

    #[test]
    fn test_le_random_resolvable_parse() {
        let addr = Address::le_random_from_str("75:44:33:22:11:00").unwrap();
        assert_eq!("75:44:33:22:11:00", addr.to_string());
        assert_eq!(
            "LeRandom(Resolvable(ResolvablePrivateAddress(75:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        assert!(Address::le_random_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").is_err());
    }

    #[test]
    fn test_le_random_static_parse() {
        let addr = Address::le_random_from_str("F5:44:33:22:11:00").unwrap();
        assert_eq!("f5:44:33:22:11:00", addr.to_string());
        assert_eq!(
            "LeRandom(Static(StaticDeviceAddress(f5:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        assert!(Address::le_random_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").is_err());
    }

    #[test]
    fn test_le_random_unknown_parse() {
        let addr = Address::le_random_from_str("B5:44:33:22:11:00").unwrap();
        assert_eq!("b5:44:33:22:11:00", addr.to_string());
        assert_eq!(
            "LeRandom(Unknown(b5:44:33:22:11:00))",
            &format!("{:?}", addr)
        );

        assert!(Address::le_random_from_str("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ").is_err());
    }

    #[test]
    fn test_bredr_from() {
        let addr = Address::bredr_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!("BrEdr(55:44:33:22:11:00)", &format!("{:?}", addr));
    }

    #[test]
    fn test_le_public_from() {
        let addr = Address::le_public_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!(
            "LePublic(PublicDeviceAddress(55:44:33:22:11:00))",
            &format!("{:?}", addr)
        );
    }

    #[test]
    fn test_le_random_from() {
        let addr = Address::le_random_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x35]);
        assert_eq!(
            "LeRandom(NonResolvable(NonResolvablePrivateAddress(35:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        let addr = Address::le_random_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        assert_eq!(
            "LeRandom(Resolvable(ResolvablePrivateAddress(55:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        let addr = Address::le_random_from([0x00, 0x11, 0x22, 0x33, 0x44, 0xF5]);
        assert_eq!(
            "LeRandom(Static(StaticDeviceAddress(f5:44:33:22:11:00)))",
            &format!("{:?}", addr)
        );

        let addr = Address::le_random_from([0x00, 0x11, 0x22, 0x33, 0x44, 0xB5]);
        assert_eq!(
            "LeRandom(Unknown(b5:44:33:22:11:00))",
            &format!("{:?}", addr)
        );
    }

    #[test]
    fn test_non_resolvable_try_from() {
        let addr =
            NonResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x35]).unwrap();
        assert_eq!(
            "NonResolvablePrivateAddress(35:44:33:22:11:00)",
            &format!("{:?}", addr)
        );

        let result = NonResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55])
            .unwrap_err();
        assert_eq!("InvalidBitsForAddressType(0, 1)", &format!("{:?}", result));
        assert_eq!(
            "Invalid bits for this address type. (expect: 0b00, but 0b01)",
            &format!("{:}", result)
        );
    }

    #[test]
    fn test_resolvable_try_from() {
        let addr =
            ResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
        assert_eq!(
            "ResolvablePrivateAddress(55:44:33:22:11:00)",
            &format!("{:?}", addr)
        );

        let result =
            ResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0xB5]).unwrap_err();
        assert_eq!("InvalidBitsForAddressType(1, 2)", &format!("{:?}", result));
        assert_eq!(
            "Invalid bits for this address type. (expect: 0b01, but 0b10)",
            &format!("{:}", result)
        );
    }

    #[test]
    fn test_static_try_from() {
        let addr = StaticDeviceAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0xF5]).unwrap();
        assert_eq!(
            "StaticDeviceAddress(f5:44:33:22:11:00)",
            &format!("{:?}", addr)
        );

        let result =
            StaticDeviceAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x05]).unwrap_err();
        assert_eq!("InvalidBitsForAddressType(3, 0)", &format!("{:?}", result));
        assert_eq!(
            "Invalid bits for this address type. (expect: 0b11, but 0b00)",
            &format!("{:}", result)
        );
    }

    #[test]
    fn test_convert_bredr() {
        let addr = BdAddr::from_str("55:44:33:22:11:00")
            .unwrap()
            .to_br_edr_addr();
        assert_eq!(Address::bredr_from_str("55:44:33:22:11:00").unwrap(), addr);
    }

    #[test]
    fn test_convert_le_public() {
        let addr = BdAddr::from_str("55:44:33:22:11:00")
            .unwrap()
            .to_le_public_addr();
        assert_eq!(
            Address::from(PublicDeviceAddress::from([
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55
            ])),
            addr
        );
    }

    #[test]
    fn test_convert_le_random() {
        let addr = BdAddr::from_str("35:44:33:22:11:00")
            .unwrap()
            .to_le_random_addr();
        assert_eq!(
            Address::from(RandomDeviceAddress::from(
                NonResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x35])
                    .unwrap()
            )),
            addr
        );

        let addr = BdAddr::from_str("75:44:33:22:11:00")
            .unwrap()
            .to_le_random_addr();
        assert_eq!(
            Address::from(RandomDeviceAddress::from(
                ResolvablePrivateAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x75]).unwrap()
            )),
            addr
        );

        let addr = BdAddr::from_str("F5:44:33:22:11:00")
            .unwrap()
            .to_le_random_addr();
        assert_eq!(
            Address::from(RandomDeviceAddress::from(
                StaticDeviceAddress::try_from([0x00, 0x11, 0x22, 0x33, 0x44, 0xF5]).unwrap()
            )),
            addr
        );
    }

    #[test]
    fn test_into_bd_addr() {
        let addr = Address::bredr_from_str("55:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("55:44:33:22:11:00").unwrap(), addr);

        let addr = Address::le_public_from_str("55:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("55:44:33:22:11:00").unwrap(), addr);

        let addr = Address::le_random_from_str("35:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("35:44:33:22:11:00").unwrap(), addr);

        let addr = Address::le_random_from_str("75:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("75:44:33:22:11:00").unwrap(), addr);

        let addr = Address::le_random_from_str("F5:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("F5:44:33:22:11:00").unwrap(), addr);

        let addr = Address::le_random_from_str("B5:44:33:22:11:00")
            .unwrap()
            .into_bd_addr();
        assert_eq!(BdAddr::try_from("B5:44:33:22:11:00").unwrap(), addr);
    }

    #[test]
    fn test_address_type() {
        let ty = Address::bredr_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).address_type();
        assert_eq!("BrEdr", format!("{:?}", ty));
        assert_eq!(AddressType::BrEdr, ty);
        let ty = Address::le_public_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).address_type();
        assert_eq!("LePublic", format!("{:?}", ty));
        assert_eq!(AddressType::LePublic, ty);
        let ty = Address::le_random_from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).address_type();
        assert_eq!("LeRandom", format!("{:?}", ty));
        assert_eq!(AddressType::LeRandom, ty);
    }
}