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
use {
    byteorder::{self, ByteOrder},
    rubble::link::ad_structure::AdStructure,
};

pub struct Data([u8; 17]);

impl<'a, 'b> Data
where
    'a: 'b,
{
    pub fn new(
        interval: Interval,
        pair_status: PairingStatus,
        device_id: u64,
        accessory_category: AccessoryCategory,
        global_state_number: u16,
        configuration_number: u8,
    ) -> Self {
        let mut inner = [0u8; 17];

        // CoID of Apple, Inc.
        inner[0] = 0x4C;
        inner[1] = 0x00;

        // Type (HomeKit)
        inner[2] = 0x06;

        // Advertising Interval and Length
        inner[3] = interval.value();

        // Status Flags
        inner[4] = pair_status.value();

        // 48-bit Device ID
        byteorder::BigEndian::write_u48(&mut inner[5..12], device_id);

        // Accessory Category Identifier
        byteorder::BigEndian::write_u16(&mut inner[11..13], accessory_category.value());

        // Global State Number
        byteorder::BigEndian::write_u16(&mut inner[13..15], global_state_number);

        // Configuration Number
        inner[15] = configuration_number;

        // Compatible Version
        inner[16] = 0x02;

        Self(inner)
    }

    pub fn as_adstructure(&'a self) -> AdStructure<'b> {
        AdStructure::Unknown {
            ty: 0xFF,
            data: &self.0,
        }
    }
}

pub enum Interval {
    _10_25MS,
    _26_100MS,
    _101_300MS,
    _301_500MS,
    _501_1250MS,
    _1251_2500MS,
    _2500MS,
}

impl Interval {
    fn value(&self) -> u8 {
        match *self {
            Interval::_10_25MS => 0x2D,
            Interval::_26_100MS => 0x4D,
            Interval::_101_300MS => 0x6D,
            Interval::_301_500MS => 0x8D,
            Interval::_501_1250MS => 0xAD,
            Interval::_1251_2500MS => 0xCD,
            Interval::_2500MS => 0xED,
        }
    }
}

pub enum PairingStatus {
    Paired,
    NotPaired,
}

impl PairingStatus {
    fn value(&self) -> u8 {
        match *self {
            PairingStatus::Paired => 0b0,
            PairingStatus::NotPaired => 0b1,
        }
    }
}

pub enum AccessoryCategory {
    Other,
    Bridge,
    Fan,
    Garage,
    Lightbulb,
    DoorLock,
    Outlet,
    Switch,
    Thermostat,
    Sensor,
    SecuritySystem,
    Door,
    Window,
    WindowCovering,
    ProgrammableSwitch,
    RangeExtender,
    IPCamera,
    VideoDoorBell,
    AirPurifier,
    Reserved,
}

impl AccessoryCategory {
    fn value(&self) -> u16 {
        match *self {
            AccessoryCategory::Other => 1,
            AccessoryCategory::Bridge => 2,
            AccessoryCategory::Fan => 3,
            AccessoryCategory::Garage => 4,
            AccessoryCategory::Lightbulb => 5,
            AccessoryCategory::DoorLock => 6,
            AccessoryCategory::Outlet => 7,
            AccessoryCategory::Switch => 8,
            AccessoryCategory::Thermostat => 9,
            AccessoryCategory::Sensor => 10,
            AccessoryCategory::SecuritySystem => 11,
            AccessoryCategory::Door => 12,
            AccessoryCategory::Window => 13,
            AccessoryCategory::WindowCovering => 14,
            AccessoryCategory::ProgrammableSwitch => 15,
            AccessoryCategory::RangeExtender => 16,
            AccessoryCategory::IPCamera => 17,
            AccessoryCategory::VideoDoorBell => 18,
            AccessoryCategory::AirPurifier => 19,
            AccessoryCategory::Reserved => 65535,
        }
    }
}

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

    #[test]
    fn test_snapshots() {
        assert_debug_snapshot_matches!(
            "data_as_adstructure",
            Data::new(
                Interval::_501_1250MS,
                PairingStatus::NotPaired,
                188899839028173,
                AccessoryCategory::Lightbulb,
                1,
                1,
            )
            .as_adstructure()
        )
    }
}