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
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate num_derive;

use std::fmt::{Display, Formatter};

mod util;
pub mod mgmt;

#[derive(Debug, Copy, Clone)]
pub struct Address
{
    bytes: [u8; 6]
}

impl Address
{
    pub fn from_slice(bytes: &[u8]) -> Address {
        if bytes.len() != 6 {
            panic!("bluetooth address is 6 bytes");
        }

        let mut arr = [0u8; 6];
        arr.copy_from_slice(bytes);
        Address {
            bytes: arr
        }
    }
}

impl From<[u8; 6]> for Address
{
    fn from(bytes: [u8; 6]) -> Self {
        return Address { bytes };
    }
}

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