Skip to content

Commit 7cb12a1

Browse files
committed
PeriphialkIdentifier
1 parent c4a2e4d commit 7cb12a1

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

src/api/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@ pub enum WriteType {
174174
WithoutResponse,
175175
}
176176

177+
pub type PeripheralId = BDAddr;
178+
177179
/// Peripheral is the device that you would like to communicate with (the "server" of BLE). This
178180
/// struct contains both the current state of the device (its properties, characteristics, etc.)
179181
/// as well as functions for communication.
180182
#[async_trait]
181183
pub trait Peripheral: Send + Sync + Clone + Debug {
184+
/// Returns the MAC address of the peripheral.
185+
fn id(&self) -> PeripheralId;
186+
182187
/// Returns the MAC address of the peripheral.
183188
fn address(&self) -> BDAddr;
184189

@@ -236,23 +241,23 @@ pub trait Peripheral: Send + Sync + Clone + Debug {
236241
)]
237242
#[derive(Debug, Clone)]
238243
pub enum CentralEvent {
239-
DeviceDiscovered(BDAddr),
240-
DeviceUpdated(BDAddr),
241-
DeviceConnected(BDAddr),
242-
DeviceDisconnected(BDAddr),
244+
DeviceDiscovered(PeripheralId),
245+
DeviceUpdated(PeripheralId),
246+
DeviceConnected(PeripheralId),
247+
DeviceDisconnected(PeripheralId),
243248
/// Emitted when a Manufacturer Data advertisement has been received from a device
244249
ManufacturerDataAdvertisement {
245-
address: BDAddr,
250+
address: PeripheralId,
246251
manufacturer_data: HashMap<u16, Vec<u8>>,
247252
},
248253
/// Emitted when a Service Data advertisement has been received from a device
249254
ServiceDataAdvertisement {
250-
address: BDAddr,
255+
address: PeripheralId,
251256
service_data: HashMap<Uuid, Vec<u8>>,
252257
},
253258
/// Emitted when the advertised services for a device has been updated
254259
ServicesAdvertisement {
255-
address: BDAddr,
260+
address: PeripheralId,
256261
services: Vec<Uuid>,
257262
},
258263
}
@@ -280,7 +285,7 @@ pub trait Central: Send + Sync + Clone {
280285
async fn peripherals(&self) -> Result<Vec<Self::Peripheral>>;
281286

282287
/// Returns a particular [`Peripheral`] by its address if it has been discovered.
283-
async fn peripheral(&self, address: BDAddr) -> Result<Self::Peripheral>;
288+
async fn peripheral(&self, address: PeripheralId) -> Result<Self::Peripheral>;
284289

285290
/// Add a [`Peripheral`] from a MAC address without a scan result. Not supported on all Bluetooth systems.
286291
async fn add_peripheral(&self, address: BDAddr) -> Result<Self::Peripheral>;

src/bluez/peripheral.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::pin::Pin;
1010
use std::sync::{Arc, Mutex};
1111

1212
use crate::api::{
13-
self, AddressType, BDAddr, CharPropFlags, Characteristic, PeripheralProperties,
13+
self, AddressType, BDAddr, CharPropFlags, Characteristic, PeripheralId, PeripheralProperties,
1414
ValueNotification, WriteType,
1515
};
1616
use crate::{Error, Result};
@@ -58,6 +58,10 @@ impl Peripheral {
5858

5959
#[async_trait]
6060
impl api::Peripheral for Peripheral {
61+
fn id(&self) -> PeripheralId {
62+
self.address()
63+
}
64+
6165
fn address(&self) -> BDAddr {
6266
self.mac_address
6367
}

src/common/adapter_manager.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
// Copyright (c) 2014 The Rust Project Developers
1414
use crate::{
15-
api::{BDAddr, CentralEvent, Peripheral},
15+
api::{CentralEvent, Peripheral, PeripheralId},
1616
common::util::send_notification,
1717
};
1818
use dashmap::{mapref::one::RefMut, DashMap};
@@ -26,7 +26,7 @@ pub struct AdapterManager<PeripheralType>
2626
where
2727
PeripheralType: Peripheral,
2828
{
29-
peripherals: Arc<DashMap<BDAddr, PeripheralType>>,
29+
peripherals: Arc<DashMap<PeripheralId, PeripheralType>>,
3030
async_senders: Arc<Mutex<Vec<UnboundedSender<CentralEvent>>>>,
3131
}
3232

@@ -61,12 +61,12 @@ where
6161
Box::pin(receiver)
6262
}
6363

64-
pub fn add_peripheral(&self, addr: BDAddr, peripheral: PeripheralType) {
64+
pub fn add_peripheral(&self, addr: PeripheralId, peripheral: PeripheralType) {
6565
assert!(
6666
!self.peripherals.contains_key(&addr),
6767
"Adding a peripheral that's already in the map."
6868
);
69-
assert_eq!(peripheral.address(), addr, "Device has unexpected address."); // TODO remove addr argument
69+
assert_eq!(peripheral.id(), addr, "Device has unexpected address."); // TODO remove addr argument
7070
self.peripherals.insert(addr, peripheral);
7171
}
7272

@@ -77,11 +77,14 @@ where
7777
.collect()
7878
}
7979

80-
pub fn peripheral_mut(&self, address: BDAddr) -> Option<RefMut<BDAddr, PeripheralType>> {
80+
pub fn peripheral_mut(
81+
&self,
82+
address: PeripheralId,
83+
) -> Option<RefMut<PeripheralId, PeripheralType>> {
8184
self.peripherals.get_mut(&address)
8285
}
8386

84-
pub fn peripheral(&self, address: BDAddr) -> Option<PeripheralType> {
87+
pub fn peripheral(&self, address: PeripheralId) -> Option<PeripheralType> {
8588
self.peripherals
8689
.get(&address)
8790
.map(|val| val.value().clone())

src/corebluetooth/peripheral.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use super::{
1414
};
1515
use crate::{
1616
api::{
17-
self, BDAddr, CentralEvent, CharPropFlags, Characteristic, PeripheralProperties,
18-
ValueNotification, WriteType,
17+
self, BDAddr, CentralEvent, CharPropFlags, Characteristic, PeripheralId,
18+
PeripheralProperties, ValueNotification, WriteType,
1919
},
2020
common::adapter_manager::AdapterManager,
2121
Error, Result,
@@ -170,6 +170,10 @@ impl Debug for Peripheral {
170170

171171
#[async_trait]
172172
impl api::Peripheral for Peripheral {
173+
fn id(&self) -> PeripheralId {
174+
self.address()
175+
}
176+
173177
fn address(&self) -> BDAddr {
174178
// TODO: look at moving/copying address out of properties so we don't have to
175179
// take a lock here! (the address for the peripheral won't ever change)

src/winrtble/peripheral.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
api::{
2020
bleuuid::{uuid_from_u16, uuid_from_u32},
2121
AddressType, BDAddr, CentralEvent, Characteristic, Peripheral as ApiPeripheral,
22-
PeripheralProperties, ValueNotification, WriteType,
22+
PeripheralId, PeripheralProperties, ValueNotification, WriteType,
2323
},
2424
common::adapter_manager::AdapterManager,
2525
Error, Result,
@@ -224,6 +224,10 @@ impl Debug for Peripheral {
224224

225225
#[async_trait]
226226
impl ApiPeripheral for Peripheral {
227+
fn id(&self) -> PeripheralId {
228+
self.address()
229+
}
230+
227231
/// Returns the address of the peripheral.
228232
fn address(&self) -> BDAddr {
229233
self.shared.address

0 commit comments

Comments
 (0)