| 254 | /// as well as functions for communication. |
| 255 | #[async_trait] |
| 256 | pub trait Peripheral: Send + Sync + Clone + Debug { |
| 257 | /// Returns the unique identifier of the peripheral. |
| 258 | fn id(&self) -> PeripheralId; |
| 259 | |
| 260 | /// Returns the MAC address of the peripheral. |
| 261 | fn address(&self) -> BDAddr; |
| 262 | |
| 263 | /// Returns the currently negotiated mtu size |
| 264 | fn mtu(&self) -> u16; |
| 265 | |
| 266 | /// Returns the set of properties associated with the peripheral. These may be updated over time |
| 267 | /// as additional advertising reports are received. |
| 268 | async fn properties(&self) -> Result<Option<PeripheralProperties>>; |
| 269 | |
| 270 | /// The set of services we've discovered for this device. This will be empty until |
| 271 | /// `discover_services` is called. |
| 272 | fn services(&self) -> BTreeSet<Service>; |
| 273 | |
| 274 | /// The set of characteristics we've discovered for this device. This will be empty until |
| 275 | /// `discover_services` is called. |
| 276 | fn characteristics(&self) -> BTreeSet<Characteristic> { |
| 277 | self.services() |
| 278 | .iter() |
| 279 | .flat_map(|service| service.characteristics.clone().into_iter()) |
| 280 | .collect() |
| 281 | } |
| 282 | |
| 283 | /// Returns true iff we are currently connected to the device. |
| 284 | async fn is_connected(&self) -> Result<bool>; |
| 285 | |
| 286 | /// Creates a connection to the device. If this method returns Ok there has been successful |
| 287 | /// connection. Note that peripherals allow only one connection at a time. Operations that |
| 288 | /// attempt to communicate with a device will fail until it is connected. |
| 289 | async fn connect(&self) -> Result<()>; |
| 290 | |
| 291 | /// Like [`connect`](Peripheral::connect), but returns [`Error::TimedOut`](crate::Error::TimedOut) |
| 292 | /// if the connection is not established within the given duration. |
| 293 | async fn connect_with_timeout(&self, timeout: Duration) -> Result<()> { |
| 294 | tokio::time::timeout(timeout, self.connect()) |
| 295 | .await |
| 296 | .map_err(|_| crate::Error::TimedOut(timeout))? |
| 297 | } |
| 298 | |
| 299 | /// Terminates a connection to the device. |
| 300 | async fn disconnect(&self) -> Result<()>; |
| 301 | |
| 302 | /// Discovers all services for the device, including their characteristics. |
| 303 | async fn discover_services(&self) -> Result<()>; |
| 304 | |
| 305 | /// Like [`discover_services`](Peripheral::discover_services), but returns |
| 306 | /// [`Error::TimedOut`](crate::Error::TimedOut) if discovery does not complete within the |
| 307 | /// given duration. |
| 308 | async fn discover_services_with_timeout(&self, timeout: Duration) -> Result<()> { |
| 309 | tokio::time::timeout(timeout, self.discover_services()) |
| 310 | .await |
| 311 | .map_err(|_| crate::Error::TimedOut(timeout))? |
| 312 | } |
| 313 |
no outgoing calls
no test coverage detected