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
// 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.

//! This module declares native Rust encodings equivalent to FIDL structs for the
//! Bluetooth LowEnergy interfaces. These structs use standard Rust primitives
//! rather than the default mapping from FIDL, and derive the `Clone` trait for a
//! more ergonomic api than those exposed in the `fidl_fuchsia_bluetooth_le`
//! crate.
//!
//! These types also implement the `From` trait, so usage when receiving a fidl
//! struct is simply a case of calling `.into(...)` (`From` implies `Into`):
//!
//! ```ignore
//!   fn use_peer(fidl_peer: fidl_fuchsia_bluetooth_le::RemoteDevice) {
//!      let peer: Le::RemoteDevice = fidl_peer.into();
//!      ...
//!   }
//! ```

use fidl_fuchsia_bluetooth::Appearance;
use fidl_fuchsia_bluetooth_le as fidl;
use std::{fmt, str::FromStr};

use crate::error::Error;
use crate::types::{id::PeerId, uuid::Uuid};

#[derive(Clone, Debug)]
pub struct RemoteDevice {
    pub identifier: String,
    pub connectable: bool,
    pub rssi: Option<i8>,
    pub advertising_data: Option<AdvertisingData>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Peer {
    pub id: PeerId,
    pub name: Option<String>,
    pub connectable: bool,
    pub rssi: Option<i8>,
    pub advertising_data: Option<AdvertisingData>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct AdvertisingData {
    pub name: Option<String>,
    pub tx_power_level: Option<i8>,
    pub appearance: Option<Appearance>,
    pub service_uuids: Vec<Uuid>,
    pub service_data: Vec<ServiceData>,
    pub manufacturer_data: Vec<ManufacturerData>,
    pub uris: Vec<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ServiceData {
    pub uuid: Uuid,
    pub data: Vec<u8>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ManufacturerData {
    pub company_id: u16,
    pub data: Vec<u8>,
}

impl TryFrom<fidl::RemoteDevice> for RemoteDevice {
    type Error = Error;
    fn try_from(src: fidl::RemoteDevice) -> Result<RemoteDevice, Self::Error> {
        Ok(RemoteDevice {
            identifier: src.identifier,
            connectable: src.connectable,
            rssi: src.rssi.map(|v| v.value),
            advertising_data: match src.advertising_data {
                Some(a) => Some(AdvertisingData::try_from(*a)?),
                None => None,
            },
        })
    }
}

impl TryFrom<fidl::Peer> for Peer {
    type Error = Error;
    fn try_from(src: fidl::Peer) -> Result<Peer, Error> {
        Ok(Peer {
            id: src.id.map(PeerId::from).ok_or(Error::missing("le.Peer.id"))?,
            name: src.name,
            connectable: src.connectable.unwrap_or(false),
            rssi: src.rssi,
            advertising_data: src.advertising_data.map(|ad| ad.into()),
        })
    }
}

impl From<fidl::AdvertisingData> for AdvertisingData {
    fn from(src: fidl::AdvertisingData) -> AdvertisingData {
        AdvertisingData {
            name: src.name,
            tx_power_level: src.tx_power_level,
            appearance: src.appearance,
            service_uuids: src
                .service_uuids
                .unwrap_or(vec![])
                .into_iter()
                .map(|uuid| Uuid::from(uuid))
                .collect(),
            service_data: src
                .service_data
                .unwrap_or(vec![])
                .into_iter()
                .map(|data| data.into())
                .collect(),
            manufacturer_data: src
                .manufacturer_data
                .unwrap_or(vec![])
                .into_iter()
                .map(|data| data.into())
                .collect(),
            uris: src.uris.unwrap_or(vec![]),
        }
    }
}

impl TryFrom<fidl::AdvertisingDataDeprecated> for AdvertisingData {
    type Error = Error;
    fn try_from(src: fidl::AdvertisingDataDeprecated) -> Result<AdvertisingData, Self::Error> {
        Ok(AdvertisingData {
            name: src.name,
            tx_power_level: src.tx_power_level.map(|v| v.value),
            appearance: src
                .appearance
                .map(|v| {
                    Appearance::from_primitive(v.value)
                        .ok_or(Error::conversion("invalid AdvertisingDataDeprecated.appearance"))
                })
                .map_or(Ok(None), |v| v.map(Some))?,
            service_uuids: src
                .service_uuids
                .unwrap_or(vec![])
                .into_iter()
                .map(|uuid| Uuid::from_str(&uuid).map_err(|e| e.into()))
                .collect::<Result<Vec<Uuid>, Error>>()?,
            service_data: src
                .service_data
                .unwrap_or(vec![])
                .into_iter()
                .map(ServiceData::try_from)
                .collect::<Result<Vec<_>, Error>>()?,
            manufacturer_data: src
                .manufacturer_specific_data
                .unwrap_or(vec![])
                .into_iter()
                .map(|data| data.into())
                .collect(),
            uris: src.uris.unwrap_or(vec![]),
        })
    }
}

impl TryFrom<fidl::ServiceDataEntry> for ServiceData {
    type Error = Error;
    fn try_from(src: fidl::ServiceDataEntry) -> Result<ServiceData, Self::Error> {
        Ok(ServiceData { uuid: Uuid::from_str(&src.uuid)?, data: src.data })
    }
}

impl From<fidl::ServiceData> for ServiceData {
    fn from(src: fidl::ServiceData) -> ServiceData {
        ServiceData { uuid: Uuid::from(src.uuid), data: src.data }
    }
}

impl From<fidl::ManufacturerSpecificDataEntry> for ManufacturerData {
    fn from(src: fidl::ManufacturerSpecificDataEntry) -> ManufacturerData {
        ManufacturerData { company_id: src.company_id, data: src.data }
    }
}

impl From<fidl::ManufacturerData> for ManufacturerData {
    fn from(src: fidl::ManufacturerData) -> ManufacturerData {
        ManufacturerData { company_id: src.company_id, data: src.data }
    }
}

impl fmt::Display for RemoteDevice {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let connectable = if self.connectable { "connectable" } else { "non-connectable" };

        write!(f, "[device ({}) ", connectable)?;

        if let Some(rssi) = &self.rssi {
            write!(f, "rssi: {}, ", rssi)?;
        }

        if let Some(ad) = &self.advertising_data {
            if let Some(name) = &ad.name {
                write!(f, "{}, ", name)?;
            }
            for m in &ad.manufacturer_data {
                write!(f, "(mfct data: {:#06x} - {:x?}), ", m.company_id, m.data)?;
            }
        }

        write!(f, "id: {}]", self.identifier)
    }
}

impl fmt::Display for Peer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let connectable = if self.connectable { "connectable" } else { "non-connectable" };

        write!(f, "[peer ({}) ", connectable)?;

        if let Some(rssi) = &self.rssi {
            write!(f, "rssi: {}, ", rssi)?;
        }

        if let Some(ad) = &self.advertising_data {
            if let Some(name) = &ad.name {
                write!(f, "{}, ", name)?;
            }
        }

        write!(f, "id: {}]", self.id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fidl_fuchsia_bluetooth as fbt;
    use fidl_fuchsia_bluetooth_le as fble;

    #[test]
    fn advertising_data_from_fidl_empty() {
        let empty_data = fble::AdvertisingData::default();
        let expected = AdvertisingData {
            name: None,
            tx_power_level: None,
            appearance: None,
            service_uuids: vec![],
            service_data: vec![],
            manufacturer_data: vec![],
            uris: vec![],
        };
        let data = AdvertisingData::from(empty_data);
        assert_eq!(expected, data);
    }

    #[test]
    fn advertising_data_from_fidl() {
        let uuid = fbt::Uuid {
            value: [
                0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x0d, 0x18,
                0x00, 0x00,
            ],
        };
        let data = fble::AdvertisingData {
            name: Some("hello".to_string()),
            tx_power_level: Some(-10),
            appearance: Some(fbt::Appearance::Watch),
            service_uuids: Some(vec![uuid.clone()]),
            service_data: Some(vec![fble::ServiceData { uuid: uuid.clone(), data: vec![1, 2, 3] }]),
            manufacturer_data: Some(vec![fble::ManufacturerData {
                company_id: 1,
                data: vec![3, 4, 5],
            }]),
            uris: Some(vec!["some/uri".to_string()]),
            ..Default::default()
        };
        let expected = AdvertisingData {
            name: Some("hello".to_string()),
            tx_power_level: Some(-10),
            appearance: Some(fbt::Appearance::Watch),
            service_uuids: vec![Uuid::new16(0x180d)],
            service_data: vec![ServiceData { uuid: Uuid::new16(0x180d), data: vec![1, 2, 3] }],
            manufacturer_data: vec![ManufacturerData { company_id: 1, data: vec![3, 4, 5] }],
            uris: vec!["some/uri".to_string()],
        };
        let data = AdvertisingData::from(data);
        assert_eq!(expected, data);
    }

    #[test]
    fn advertising_data_from_deprecated_fidl_malformed_appearance() {
        let data = fble::AdvertisingDataDeprecated {
            name: None,
            tx_power_level: None,
            appearance: Some(Box::new(fbt::UInt16 { value: 1 })), // fbt::Appearance does not declare this entry
            service_uuids: None,
            service_data: None,
            manufacturer_specific_data: None,
            solicited_service_uuids: None,
            uris: None,
        };
        let data = AdvertisingData::try_from(data);
        assert!(data.is_err());
    }

    #[test]
    fn advertising_data_from_deprecated_fidl_malformed_service_uuid() {
        let data = fble::AdvertisingDataDeprecated {
            name: None,
            tx_power_level: None,
            appearance: None,
            service_uuids: Some(vec!["💩".to_string()]),
            service_data: None,
            manufacturer_specific_data: None,
            solicited_service_uuids: None,
            uris: None,
        };
        let data = AdvertisingData::try_from(data);
        assert!(data.is_err());
    }

    #[test]
    fn advertising_data_from_deprecated_fidl_malformed_service_data() {
        let data = fble::AdvertisingDataDeprecated {
            name: None,
            tx_power_level: None,
            appearance: None,
            service_uuids: None,
            service_data: Some(vec![fble::ServiceDataEntry {
                uuid: "💩".to_string(),
                data: vec![1, 2],
            }]),
            manufacturer_specific_data: None,
            solicited_service_uuids: None,
            uris: None,
        };
        let data = AdvertisingData::try_from(data);
        assert!(data.is_err());
    }

    #[test]
    fn advertising_data_from_deprecated_fidl() {
        let data = fble::AdvertisingDataDeprecated {
            name: Some("hello".to_string()),
            tx_power_level: Some(Box::new(fbt::Int8 { value: -10 })),
            appearance: Some(Box::new(fbt::UInt16 { value: 64 })), // "Phone"
            service_uuids: Some(vec!["0000180d-0000-1000-8000-00805f9b34fb".to_string()]),
            service_data: Some(vec![fble::ServiceDataEntry {
                uuid: "0000180d-0000-1000-8000-00805f9b34fb".to_string(),
                data: vec![1, 2],
            }]),
            manufacturer_specific_data: Some(vec![fble::ManufacturerSpecificDataEntry {
                company_id: 1,
                data: vec![1],
            }]),
            solicited_service_uuids: None,
            uris: Some(vec!["some/uri".to_string()]),
        };
        let expected = AdvertisingData {
            name: Some("hello".to_string()),
            tx_power_level: Some(-10),
            appearance: Some(fbt::Appearance::Phone),
            service_uuids: vec![Uuid::new16(0x180d)],
            service_data: vec![ServiceData { uuid: Uuid::new16(0x180d), data: vec![1, 2] }],
            manufacturer_data: vec![ManufacturerData { company_id: 1, data: vec![1] }],
            uris: vec!["some/uri".to_string()],
        };
        let data = AdvertisingData::try_from(data).expect("expected successful conversion");
        assert_eq!(expected, data);
    }

    #[test]
    fn peer_from_fidl_no_id() {
        let peer = fble::Peer {
            id: None, // Omitted
            ..Default::default()
        };
        let peer = Peer::try_from(peer);
        assert!(peer.is_err());
    }

    #[test]
    fn peer_from_fidl_mandatory_fields_only() {
        let peer = fble::Peer { id: Some(fbt::PeerId { value: 1 }), ..Default::default() };
        let expected = Peer {
            id: PeerId(1),
            name: None,
            connectable: false,
            rssi: None,
            advertising_data: None,
        };
        let peer = Peer::try_from(peer).expect("expected successful conversion");
        assert_eq!(expected, peer);
    }

    #[test]
    fn peer_from_fidl() {
        let peer = fble::Peer {
            id: Some(fbt::PeerId { value: 1 }),
            connectable: Some(true),
            rssi: Some(-10),
            advertising_data: Some(fble::AdvertisingData {
                name: Some("hello".to_string()),
                tx_power_level: Some(-10),
                appearance: Some(fbt::Appearance::Watch),
                ..Default::default()
            }),
            ..Default::default()
        };
        let expected = Peer {
            id: PeerId(1),
            name: None,
            connectable: true,
            rssi: Some(-10),
            advertising_data: Some(AdvertisingData {
                name: Some("hello".to_string()),
                tx_power_level: Some(-10),
                appearance: Some(fbt::Appearance::Watch),
                service_uuids: vec![],
                service_data: vec![],
                manufacturer_data: vec![],
                uris: vec![],
            }),
        };
        let peer = Peer::try_from(peer).expect("expected successful conversion");
        assert_eq!(expected, peer);
    }
}