[fp-rs] Converted bluetooth module into standalone crate, with fastpair UI provided as an example.

This commit is contained in:
Lucas Silva Shepard
2023-08-13 16:51:16 -07:00
parent 6074e1beba
commit 2c8809c1e1
21 changed files with 58 additions and 79 deletions
+5 -2
View File
@@ -13,19 +13,22 @@
# limitations under the License.
[package]
name = "fastpair"
name = "bluetooth"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = { version = "0.3", features = ["executor"] }
futures = { version = "0.3" }
tracing = "0.1.37"
cfg-if = "1.0.0"
async-trait = "0.1"
thiserror = "1.0.43"
[dev-dependencies]
futures = { version = "0.3", features = ["executor"] }
[target.'cfg(windows)'.dependencies]
windows = { version = "0.48", features = [
"Devices_Bluetooth",
@@ -25,11 +25,11 @@ use futures::{
lock::Mutex,
};
mod bluetooth;
extern crate bluetooth;
use crate::bluetooth::{
BleAdapter, BleDataTypeId, BleDevice, ClassicAddress, ClassicDevice,
Platform,
use bluetooth::{
api::{BleAdapter, BleDevice, ClassicDevice},
BleDataTypeId, ClassicAddress, Platform,
};
async fn get_user_input(
@@ -14,9 +14,7 @@
use async_trait::async_trait;
use crate::bluetooth::common::{
BleAdvertisement, BleDataTypeId, BluetoothError,
};
use crate::common::{BleAdvertisement, BleDataTypeId, BluetoothError};
/// Concrete types implementing this trait are Bluetooth Central devices.
/// They provide methods for retrieving nearby connections and device info.
@@ -14,7 +14,7 @@
use async_trait::async_trait;
use crate::bluetooth::common::{
use crate::common::{
BleAddress, BluetoothError, ClassicAddress, PairingResult,
};
@@ -1,56 +0,0 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Split into separate crate once demo is finished, providing custom error types
// instead of using anyhow.
// b/290070686
pub mod api;
pub mod common;
pub use api::{BleAdapter, BleDevice, ClassicDevice};
pub use common::{
BleAddress, BleAdvertisement, BleDataTypeId, BluetoothError, ClassicAddress,
};
cfg_if::cfg_if! {
if #[cfg(windows)] {
mod windows;
use self::windows as platform;
} else {
mod unsupported;
use unsupported as platform;
}
}
pub struct Platform;
impl Platform {
pub async fn default_adapter(
) -> Result<impl api::BleAdapter, BluetoothError> {
platform::BleAdapter::default().await
}
pub async fn new_ble_device(
addr: BleAddress,
) -> Result<impl api::BleDevice, BluetoothError> {
platform::BleDevice::new(addr).await
}
pub async fn new_classic_device(
addr: ClassicAddress,
) -> Result<impl api::ClassicDevice, BluetoothError> {
platform::ClassicDevice::new(addr).await
}
}
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::bluetooth::common::BluetoothError;
use super::BluetoothError;
/// BLE Addresses can either be the peripheral's public MAC address, or various
/// types of random addresses.
+38 -2
View File
@@ -12,5 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/// Library file, exports modules for use in integration tests and external crates.
pub mod bluetooth;
pub mod api;
mod common;
use api::{BleAdapter, BleDevice, ClassicDevice};
pub use common::{
BleAddress, BleAdvertisement, BleDataTypeId, BluetoothError, ClassicAddress,
};
cfg_if::cfg_if! {
if #[cfg(windows)] {
mod windows;
use self::windows as platform;
} else {
mod unsupported;
use unsupported as platform;
}
}
pub struct Platform;
impl Platform {
pub async fn default_adapter(
) -> Result<impl api::BleAdapter, BluetoothError> {
platform::BleAdapter::default().await
}
pub async fn new_ble_device(
addr: BleAddress,
) -> Result<impl api::BleDevice, BluetoothError> {
platform::BleDevice::new(addr).await
}
pub async fn new_classic_device(
addr: ClassicAddress,
) -> Result<impl api::ClassicDevice, BluetoothError> {
platform::ClassicDevice::new(addr).await
}
}
@@ -15,9 +15,7 @@
use async_trait::async_trait;
use super::BleDevice;
use crate::bluetooth::{
api, common::BluetoothError, BleAdvertisement, BleDataTypeId,
};
use crate::{api, common::BluetoothError, BleAdvertisement, BleDataTypeId};
/// Concrete type implementing `Adapter`, used for unsupported devices.
/// Every method should panic.
@@ -14,7 +14,7 @@
use async_trait::async_trait;
use crate::bluetooth::{
use crate::{
api,
common::{BleAddress, BluetoothError, ClassicAddress, PairingResult},
};
@@ -53,7 +53,7 @@ use windows::{
Foundation::TypedEventHandler,
};
use crate::bluetooth::{
use crate::{
api,
common::{BleAdvertisement, BleDataTypeId, BluetoothError},
};
@@ -16,7 +16,7 @@
//https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothaddresstype?view=winrt-22621
use windows::Devices::Bluetooth::BluetoothAddressType;
use crate::bluetooth::common::{BleAddressKind, BluetoothError};
use crate::common::{BleAddressKind, BluetoothError};
// Convenience for converting from Windows API to crate API.
impl TryFrom<BluetoothAddressType> for BleAddressKind {
@@ -29,7 +29,7 @@ use windows::{
Storage::Streams::DataReader,
};
use crate::bluetooth::common::{
use crate::common::{
BleAddress, BleAddressKind, BleAdvertisement, BleDataTypeId,
BluetoothError, ServiceData,
};
@@ -49,7 +49,7 @@ use windows::{
Foundation::TypedEventHandler,
};
use crate::bluetooth::{api, common::{BleAddress, ClassicAddress, BluetoothError, PairingResult}};
use crate::{api, common::{BleAddress, ClassicAddress, BluetoothError, PairingResult}};
/// Concrete type implementing `Device`, used for Windows BLE.
pub struct BleDevice {
@@ -14,7 +14,7 @@
use windows::Devices::Enumeration::DevicePairingResultStatus;
use crate::bluetooth::common::{BluetoothError, PairingResult};
use crate::common::{BluetoothError, PairingResult};
impl From<windows::core::Error> for BluetoothError {
fn from(err: windows::core::Error) -> Self {
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use fastpair::*;
use bluetooth::*;
mod tests {
use super::*;