From 2c8809c1e10a63c047f338d5e5bebb6da0cc7e1f Mon Sep 17 00:00:00 2001 From: Lucas Silva Shepard Date: Sat, 29 Jul 2023 18:02:53 -0700 Subject: [PATCH] [fp-rs] Converted bluetooth module into standalone crate, with fastpair UI provided as an example. --- fastpair/rust/bluetooth/Cargo.toml | 7 ++- .../{src/main.rs => examples/fastpair_ui.rs} | 8 +-- .../src/{bluetooth => }/api/adapter.rs | 4 +- .../src/{bluetooth => }/api/device.rs | 2 +- .../bluetooth/src/{bluetooth => }/api/mod.rs | 0 fastpair/rust/bluetooth/src/bluetooth/mod.rs | 56 ------------------- .../src/{bluetooth => }/common/address.rs | 2 +- .../{bluetooth => }/common/advertisement.rs | 0 .../src/{bluetooth => }/common/error.rs | 0 .../src/{bluetooth => }/common/mod.rs | 0 fastpair/rust/bluetooth/src/lib.rs | 40 ++++++++++++- .../{bluetooth => }/unsupported/adapter.rs | 4 +- .../src/{bluetooth => }/unsupported/device.rs | 2 +- .../src/{bluetooth => }/unsupported/mod.rs | 0 .../src/{bluetooth => }/windows/adapter.rs | 2 +- .../src/{bluetooth => }/windows/address.rs | 2 +- .../{bluetooth => }/windows/advertisement.rs | 2 +- .../src/{bluetooth => }/windows/device.rs | 2 +- .../src/{bluetooth => }/windows/error.rs | 2 +- .../src/{bluetooth => }/windows/mod.rs | 0 .../rust/bluetooth/tests/integration_test.rs | 2 +- 21 files changed, 58 insertions(+), 79 deletions(-) rename fastpair/rust/bluetooth/{src/main.rs => examples/fastpair_ui.rs} (96%) rename fastpair/rust/bluetooth/src/{bluetooth => }/api/adapter.rs (93%) rename fastpair/rust/bluetooth/src/{bluetooth => }/api/device.rs (98%) rename fastpair/rust/bluetooth/src/{bluetooth => }/api/mod.rs (100%) delete mode 100644 fastpair/rust/bluetooth/src/bluetooth/mod.rs rename fastpair/rust/bluetooth/src/{bluetooth => }/common/address.rs (98%) rename fastpair/rust/bluetooth/src/{bluetooth => }/common/advertisement.rs (100%) rename fastpair/rust/bluetooth/src/{bluetooth => }/common/error.rs (100%) rename fastpair/rust/bluetooth/src/{bluetooth => }/common/mod.rs (100%) rename fastpair/rust/bluetooth/src/{bluetooth => }/unsupported/adapter.rs (93%) rename fastpair/rust/bluetooth/src/{bluetooth => }/unsupported/device.rs (98%) rename fastpair/rust/bluetooth/src/{bluetooth => }/unsupported/mod.rs (100%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/adapter.rs (99%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/address.rs (97%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/advertisement.rs (99%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/device.rs (98%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/error.rs (98%) rename fastpair/rust/bluetooth/src/{bluetooth => }/windows/mod.rs (100%) diff --git a/fastpair/rust/bluetooth/Cargo.toml b/fastpair/rust/bluetooth/Cargo.toml index 02b48ebc..0c666219 100644 --- a/fastpair/rust/bluetooth/Cargo.toml +++ b/fastpair/rust/bluetooth/Cargo.toml @@ -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", diff --git a/fastpair/rust/bluetooth/src/main.rs b/fastpair/rust/bluetooth/examples/fastpair_ui.rs similarity index 96% rename from fastpair/rust/bluetooth/src/main.rs rename to fastpair/rust/bluetooth/examples/fastpair_ui.rs index 19d77c00..920ace4c 100644 --- a/fastpair/rust/bluetooth/src/main.rs +++ b/fastpair/rust/bluetooth/examples/fastpair_ui.rs @@ -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( diff --git a/fastpair/rust/bluetooth/src/bluetooth/api/adapter.rs b/fastpair/rust/bluetooth/src/api/adapter.rs similarity index 93% rename from fastpair/rust/bluetooth/src/bluetooth/api/adapter.rs rename to fastpair/rust/bluetooth/src/api/adapter.rs index 39ab9310..de9bc43f 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/api/adapter.rs +++ b/fastpair/rust/bluetooth/src/api/adapter.rs @@ -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. diff --git a/fastpair/rust/bluetooth/src/bluetooth/api/device.rs b/fastpair/rust/bluetooth/src/api/device.rs similarity index 98% rename from fastpair/rust/bluetooth/src/bluetooth/api/device.rs rename to fastpair/rust/bluetooth/src/api/device.rs index 97cba1d8..df43ec72 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/api/device.rs +++ b/fastpair/rust/bluetooth/src/api/device.rs @@ -14,7 +14,7 @@ use async_trait::async_trait; -use crate::bluetooth::common::{ +use crate::common::{ BleAddress, BluetoothError, ClassicAddress, PairingResult, }; diff --git a/fastpair/rust/bluetooth/src/bluetooth/api/mod.rs b/fastpair/rust/bluetooth/src/api/mod.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/api/mod.rs rename to fastpair/rust/bluetooth/src/api/mod.rs diff --git a/fastpair/rust/bluetooth/src/bluetooth/mod.rs b/fastpair/rust/bluetooth/src/bluetooth/mod.rs deleted file mode 100644 index 0acdb250..00000000 --- a/fastpair/rust/bluetooth/src/bluetooth/mod.rs +++ /dev/null @@ -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 { - platform::BleAdapter::default().await - } - - pub async fn new_ble_device( - addr: BleAddress, - ) -> Result { - platform::BleDevice::new(addr).await - } - - pub async fn new_classic_device( - addr: ClassicAddress, - ) -> Result { - platform::ClassicDevice::new(addr).await - } -} diff --git a/fastpair/rust/bluetooth/src/bluetooth/common/address.rs b/fastpair/rust/bluetooth/src/common/address.rs similarity index 98% rename from fastpair/rust/bluetooth/src/bluetooth/common/address.rs rename to fastpair/rust/bluetooth/src/common/address.rs index 485ff731..db0481f6 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/common/address.rs +++ b/fastpair/rust/bluetooth/src/common/address.rs @@ -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. diff --git a/fastpair/rust/bluetooth/src/bluetooth/common/advertisement.rs b/fastpair/rust/bluetooth/src/common/advertisement.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/common/advertisement.rs rename to fastpair/rust/bluetooth/src/common/advertisement.rs diff --git a/fastpair/rust/bluetooth/src/bluetooth/common/error.rs b/fastpair/rust/bluetooth/src/common/error.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/common/error.rs rename to fastpair/rust/bluetooth/src/common/error.rs diff --git a/fastpair/rust/bluetooth/src/bluetooth/common/mod.rs b/fastpair/rust/bluetooth/src/common/mod.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/common/mod.rs rename to fastpair/rust/bluetooth/src/common/mod.rs diff --git a/fastpair/rust/bluetooth/src/lib.rs b/fastpair/rust/bluetooth/src/lib.rs index c9a294f5..e454012b 100644 --- a/fastpair/rust/bluetooth/src/lib.rs +++ b/fastpair/rust/bluetooth/src/lib.rs @@ -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 { + platform::BleAdapter::default().await + } + + pub async fn new_ble_device( + addr: BleAddress, + ) -> Result { + platform::BleDevice::new(addr).await + } + + pub async fn new_classic_device( + addr: ClassicAddress, + ) -> Result { + platform::ClassicDevice::new(addr).await + } +} diff --git a/fastpair/rust/bluetooth/src/bluetooth/unsupported/adapter.rs b/fastpair/rust/bluetooth/src/unsupported/adapter.rs similarity index 93% rename from fastpair/rust/bluetooth/src/bluetooth/unsupported/adapter.rs rename to fastpair/rust/bluetooth/src/unsupported/adapter.rs index de883e3c..7ae210e1 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/unsupported/adapter.rs +++ b/fastpair/rust/bluetooth/src/unsupported/adapter.rs @@ -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. diff --git a/fastpair/rust/bluetooth/src/bluetooth/unsupported/device.rs b/fastpair/rust/bluetooth/src/unsupported/device.rs similarity index 98% rename from fastpair/rust/bluetooth/src/bluetooth/unsupported/device.rs rename to fastpair/rust/bluetooth/src/unsupported/device.rs index 0ac68775..244ea06c 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/unsupported/device.rs +++ b/fastpair/rust/bluetooth/src/unsupported/device.rs @@ -14,7 +14,7 @@ use async_trait::async_trait; -use crate::bluetooth::{ +use crate::{ api, common::{BleAddress, BluetoothError, ClassicAddress, PairingResult}, }; diff --git a/fastpair/rust/bluetooth/src/bluetooth/unsupported/mod.rs b/fastpair/rust/bluetooth/src/unsupported/mod.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/unsupported/mod.rs rename to fastpair/rust/bluetooth/src/unsupported/mod.rs diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/adapter.rs b/fastpair/rust/bluetooth/src/windows/adapter.rs similarity index 99% rename from fastpair/rust/bluetooth/src/bluetooth/windows/adapter.rs rename to fastpair/rust/bluetooth/src/windows/adapter.rs index cf4519f6..7130cb97 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/windows/adapter.rs +++ b/fastpair/rust/bluetooth/src/windows/adapter.rs @@ -53,7 +53,7 @@ use windows::{ Foundation::TypedEventHandler, }; -use crate::bluetooth::{ +use crate::{ api, common::{BleAdvertisement, BleDataTypeId, BluetoothError}, }; diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/address.rs b/fastpair/rust/bluetooth/src/windows/address.rs similarity index 97% rename from fastpair/rust/bluetooth/src/bluetooth/windows/address.rs rename to fastpair/rust/bluetooth/src/windows/address.rs index d08ae4f3..5423c429 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/windows/address.rs +++ b/fastpair/rust/bluetooth/src/windows/address.rs @@ -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 for BleAddressKind { diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/advertisement.rs b/fastpair/rust/bluetooth/src/windows/advertisement.rs similarity index 99% rename from fastpair/rust/bluetooth/src/bluetooth/windows/advertisement.rs rename to fastpair/rust/bluetooth/src/windows/advertisement.rs index 526e2d5a..dbbdfa2e 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/windows/advertisement.rs +++ b/fastpair/rust/bluetooth/src/windows/advertisement.rs @@ -29,7 +29,7 @@ use windows::{ Storage::Streams::DataReader, }; -use crate::bluetooth::common::{ +use crate::common::{ BleAddress, BleAddressKind, BleAdvertisement, BleDataTypeId, BluetoothError, ServiceData, }; diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/device.rs b/fastpair/rust/bluetooth/src/windows/device.rs similarity index 98% rename from fastpair/rust/bluetooth/src/bluetooth/windows/device.rs rename to fastpair/rust/bluetooth/src/windows/device.rs index cf0e293f..a3d2d649 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/windows/device.rs +++ b/fastpair/rust/bluetooth/src/windows/device.rs @@ -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 { diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/error.rs b/fastpair/rust/bluetooth/src/windows/error.rs similarity index 98% rename from fastpair/rust/bluetooth/src/bluetooth/windows/error.rs rename to fastpair/rust/bluetooth/src/windows/error.rs index 59e12a31..9a7f6d4c 100644 --- a/fastpair/rust/bluetooth/src/bluetooth/windows/error.rs +++ b/fastpair/rust/bluetooth/src/windows/error.rs @@ -14,7 +14,7 @@ use windows::Devices::Enumeration::DevicePairingResultStatus; -use crate::bluetooth::common::{BluetoothError, PairingResult}; +use crate::common::{BluetoothError, PairingResult}; impl From for BluetoothError { fn from(err: windows::core::Error) -> Self { diff --git a/fastpair/rust/bluetooth/src/bluetooth/windows/mod.rs b/fastpair/rust/bluetooth/src/windows/mod.rs similarity index 100% rename from fastpair/rust/bluetooth/src/bluetooth/windows/mod.rs rename to fastpair/rust/bluetooth/src/windows/mod.rs diff --git a/fastpair/rust/bluetooth/tests/integration_test.rs b/fastpair/rust/bluetooth/tests/integration_test.rs index 18021531..b3332b2a 100644 --- a/fastpair/rust/bluetooth/tests/integration_test.rs +++ b/fastpair/rust/bluetooth/tests/integration_test.rs @@ -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::*;