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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_bluetooth::{Appearance, DeviceClass};
use fidl_fuchsia_bluetooth_sys as fsys;
use fuchsia_inspect::Node;
use fuchsia_inspect_contrib::log::WriteInspect;
use std::fmt;

use crate::assigned_numbers::find_service_uuid;
use crate::error::Error;
use crate::inspect::*;
use crate::types::{Address, PeerId, Uuid};

#[derive(Clone, Debug, PartialEq)]
pub struct Peer {
    /// Uniquely identifies this peer on the current system.
    pub id: PeerId,

    /// Bluetooth device address that identifies this peer.
    /// Avoid using this and prefer to use name and/or appearance instead.
    ///
    /// NOTE: Clients should prefer to use the `id` field to keep track of
    /// peers instead of their address.
    pub address: Address,

    /// The Bluetooth technologies that are supported by this peer.
    pub technology: fsys::TechnologyType,

    /// Whether or not a BR/EDR and/or LE connection exists to this peer.
    pub connected: bool,

    /// Whether or not this peer is bonded.
    pub bonded: bool,

    /// The name of the peer, if known.
    pub name: Option<String>,

    /// The LE appearance property. Present if this peer supports LE and the
    /// appearance information was obtained over advertising and/or GATT.
    pub appearance: Option<Appearance>,

    /// The class of device for this device, if known.
    pub device_class: Option<DeviceClass>,

    /// The most recently obtained advertising signal strength for this peer. Present if known.
    pub rssi: Option<i8>,

    /// The most recently obtained transmission power for this peer. Present if known.
    pub tx_power: Option<i8>,

    /// The list of peer service UUIDs known to be available on the LE transport.
    pub le_services: Vec<Uuid>,

    /// The cached list of service UUIDs previously discovered on the BR/EDR transport.
    pub bredr_services: Vec<Uuid>,
}

impl Peer {
    /// Records the immutable properties of this peer to the Node given.
    fn record_inspect(&self, node: &Node) {
        node.record_string("peer_id", &self.id.to_string());
        node.record_string("technology", &self.technology.debug());
        node.record_string("appearance", &self.appearance.debug());
        node.record_string("address", &self.address.to_string());
        if let Some(rssi) = self.rssi {
            node.record_int("rssi", rssi as i64);
        }
        if let Some(tx_power) = self.tx_power {
            node.record_int("tx_power", tx_power as i64);
        }
        node.record_uint("connected", self.connected.to_property());
        node.record_uint("bonded", self.bonded.to_property());
        if !self.le_services.is_empty() {
            node.record_string("le_services", &self.le_services.to_property());
        }
        if !self.bredr_services.is_empty() {
            node.record_string("bredr_services", &self.bredr_services.to_property());
        }
        if let Some(name) = &self.name {
            node.record_string("name", name);
        }
    }
}

/// Generate a unique ID to use with audio_core, given the `peer_id` and whether it
/// will be an input device. Current format is:
/// [
///   0x42, 0x54, - Prefix reserved for Bluetooth Audio devices
///   0xUU, 0xID, - UUID for the service being provided locally on this device:
///      - 0x11, 0x1E Handsfree (for input devices)
///      - 0x11, 0x1F Handsfree Audio Gateway (for output devices)
///      - 0x11, 0x0A A2DP AudioSource
///      - 0x11, 0x0B A2DP AudioSink (unused for now)
///   0x00, 0x00, 0x00, 0x00 - Reserved for Future Use
///   (PeerId in big endian, 8 bytes)
/// ]
///
/// Panics if the uuid provided is not a 16-bit Bluetooth Service UUID.
pub fn peer_audio_stream_id(peer_id: PeerId, uuid: Uuid) -> [u8; 16] {
    let mut unique_id = [0; 16];
    unique_id[0] = 0x42;
    unique_id[1] = 0x54;
    let short: u16 = uuid.try_into().expect("UUID should be 16-bit");
    unique_id[2..4].copy_from_slice(&(short.to_be_bytes()));
    unique_id[8..].copy_from_slice(&(peer_id.0.to_be_bytes()));
    unique_id
}

impl ImmutableDataInspect<Peer> for ImmutableDataInspectManager {
    fn new(data: &Peer, manager: Node) -> ImmutableDataInspectManager {
        data.record_inspect(&manager);
        Self { _manager: manager }
    }
}

impl IsInspectable for Peer {
    type I = ImmutableDataInspectManager;
}

impl WriteInspect for Peer {
    fn write_inspect(&self, writer: &Node, key: impl Into<fuchsia_inspect::StringReference>) {
        writer.record_child(key, |node| {
            self.record_inspect(node);
        });
    }
}

impl fmt::Display for Peer {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(fmt, "Peer:")?;
        writeln!(fmt, "\tId:\t\t{}", self.id)?;
        writeln!(fmt, "\tAddress:\t{}", self.address.to_string())?;
        writeln!(fmt, "\tTechnology:\t{:?}", self.technology)?;
        if let Some(name) = &self.name {
            writeln!(fmt, "\tName:\t\t{}", name)?;
        }
        if let Some(appearance) = &self.appearance {
            writeln!(fmt, "\tAppearance:\t{:?}", appearance)?;
        }
        if let Some(rssi) = &self.rssi {
            writeln!(fmt, "\tRSSI:\t\t{}", rssi)?;
        }
        if let Some(tx_power) = &self.tx_power {
            writeln!(fmt, "\tTX Power:\t{}", tx_power)?;
        }
        writeln!(fmt, "\tConnected:\t{}", self.connected)?;
        writeln!(fmt, "\tBonded:\t\t{}", self.bonded)?;
        writeln!(fmt, "\tLE Services:\t{:?}", names_from_services(&self.le_services))?;
        writeln!(fmt, "\tBR/EDR Serv.:\t{:?}", names_from_services(&self.bredr_services))?;
        Ok(())
    }
}

fn names_from_services(services: &Vec<Uuid>) -> Vec<String> {
    services
        .iter()
        .map(|uuid| {
            let uuid = uuid.to_string();
            find_service_uuid(&uuid).map(|an| an.name.into()).unwrap_or(uuid)
        })
        .collect()
}

impl TryFrom<fsys::Peer> for Peer {
    type Error = Error;
    fn try_from(src: fsys::Peer) -> Result<Peer, Self::Error> {
        Ok(Peer {
            id: src.id.ok_or(Error::missing("sys.Peer.id"))?.into(),
            address: src.address.ok_or(Error::missing("sys.Peer.address"))?.into(),
            technology: src.technology.ok_or(Error::missing("sys.Peer.technology"))?,
            connected: src.connected.unwrap_or(false),
            bonded: src.bonded.unwrap_or(false),
            name: src.name.clone(),
            appearance: src.appearance,
            device_class: src.device_class,
            rssi: src.rssi,
            tx_power: src.tx_power,
            le_services: src.le_services.unwrap_or(vec![]).iter().map(Uuid::from).collect(),
            bredr_services: src.bredr_services.unwrap_or(vec![]).iter().map(Uuid::from).collect(),
        })
    }
}

impl From<&Peer> for fsys::Peer {
    fn from(src: &Peer) -> fsys::Peer {
        fsys::Peer {
            id: Some(src.id.into()),
            address: Some(src.address.into()),
            technology: Some(src.technology),
            connected: Some(src.connected),
            bonded: Some(src.bonded),
            name: src.name.clone(),
            appearance: src.appearance,
            device_class: src.device_class,
            rssi: src.rssi,
            tx_power: src.tx_power,
            services: None,
            le_services: Some(src.le_services.iter().map(|uuid| uuid.into()).collect()),
            bredr_services: Some(src.bredr_services.iter().map(|uuid| uuid.into()).collect()),
            ..Default::default()
        }
    }
}

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

    use fidl_fuchsia_bluetooth as fbt;
    use proptest::{collection::vec, option, prelude::*};

    #[test]
    fn try_from_sys_id_not_present() {
        let peer = fsys::Peer::default();
        let peer = Peer::try_from(peer);
        assert!(peer.is_err());
    }

    #[test]
    fn try_from_sys_address_not_present() {
        let peer = fsys::Peer { id: Some(fbt::PeerId { value: 1 }), ..Default::default() };
        let peer = Peer::try_from(peer);
        assert!(peer.is_err());
    }

    #[test]
    fn try_from_sys_technology_not_present() {
        let peer = fsys::Peer {
            id: Some(fbt::PeerId { value: 1 }),
            address: Some(fbt::Address {
                type_: fbt::AddressType::Public,
                bytes: [1, 2, 3, 4, 5, 6],
            }),
            ..Default::default()
        };
        let peer = Peer::try_from(peer);
        assert!(peer.is_err());
    }

    fn any_address() -> impl Strategy<Value = Address> {
        any::<[u8; 6]>().prop_map(Address::Public)
    }

    fn any_technology() -> impl Strategy<Value = fsys::TechnologyType> {
        prop_oneof![
            Just(fsys::TechnologyType::LowEnergy),
            Just(fsys::TechnologyType::Classic),
            Just(fsys::TechnologyType::DualMode),
        ]
    }

    fn any_appearance() -> impl Strategy<Value = Appearance> {
        prop_oneof![Just(Appearance::Unknown), Just(Appearance::Computer),]
    }

    fn any_device_class() -> impl Strategy<Value = DeviceClass> {
        any::<u32>().prop_map(|value| DeviceClass { value })
    }

    fn any_uuids() -> impl Strategy<Value = Vec<Uuid>> {
        vec(any::<[u8; 16]>().prop_map(Uuid::from_bytes), 0..3)
    }

    fn any_peer() -> impl Strategy<Value = Peer> {
        (
            // Trait `Strategy` is only implemented for tuples of up to size 10.
            (
                any::<u64>(), // id
                any_address(),
                any_technology(),
                any::<bool>(),                       // connected
                any::<bool>(),                       // bonded
                option::of("[a-zA-Z][a-zA-Z0-9_]*"), // name
                option::of(any_appearance()),
                option::of(any_device_class()),
                option::of(any::<i8>()), // rssi
                option::of(any::<i8>()), // tx power
            ),
            any_uuids(), // le_services
            any_uuids(), // bredr_services
        )
            .prop_map(
                |(
                    (
                        id,
                        address,
                        technology,
                        connected,
                        bonded,
                        name,
                        appearance,
                        device_class,
                        rssi,
                        tx_power,
                    ),
                    le_services,
                    bredr_services,
                )| {
                    Peer {
                        id: PeerId(id),
                        address,
                        technology,
                        connected,
                        bonded,
                        name,
                        appearance,
                        device_class,
                        rssi,
                        tx_power,
                        le_services,
                        bredr_services,
                    }
                },
            )
    }

    proptest! {
        #[test]
        fn peer_sys_roundtrip(peer in any_peer()) {
            use std::convert::TryInto;

            let sys = fsys::Peer::from(&peer);
            assert_eq!(peer, sys.try_into().expect("valid Peer"));
        }
    }

    proptest! {
        #[test]
        fn peer_audio_stream_id_generation(id1 in prop::num::u64::ANY, id2 in prop::num::u64::ANY, uuid1 in prop::num::u16::ANY, uuid2 in prop::num::u16::ANY) {
            let peer1 = PeerId(id1);
            let peer2 = PeerId(id2);
            let service1: Uuid = Uuid::new16(uuid1);
            let service2: Uuid = Uuid::new16(uuid2);

            if id1 == id2 {
                assert_eq!(peer_audio_stream_id(peer1, service1.clone()), peer_audio_stream_id(peer2, service1.clone()));
            } else {
                assert_ne!(peer_audio_stream_id(peer1, service1.clone()), peer_audio_stream_id(peer2, service1.clone()));
            }

            if service1 == service2 {
                assert_eq!(peer_audio_stream_id(peer1, service1), peer_audio_stream_id(peer1, service2));
            } else {
                assert_ne!(peer_audio_stream_id(peer1, service1), peer_audio_stream_id(peer1, service2));
            }
        }
    }
}