diff --git a/Cargo.toml b/Cargo.toml index eb6a730..ca16c46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ tokio = { version = "1" } serde = { version = "1" } async-trait = { version = "0.1" } futures = { version = "0.3" } -soundcore-lib = { path = "./soundcore-lib" } +soundcore-lib = { path = "./soundcore-lib", default-features = false } [profile.release] lto = true diff --git a/manager-app/Cargo.toml b/manager-app/Cargo.toml index 4b14c27..2e45e76 100644 --- a/manager-app/Cargo.toml +++ b/manager-app/Cargo.toml @@ -29,10 +29,10 @@ tauri = { version = "1.2.5", features = [ "process-command-api", "system-tray", ] } -soundcore-lib = { workspace = true } +soundcore-lib = { workspace = true, features = ["btleplug-backend"] } [dev-dependencies] -soundcore-lib = { workspace = true, features = ["mock"], default-features = false} +soundcore-lib = { workspace = true, features = ["mock"], default-features = false } [features] # by default Tauri runs in production mode diff --git a/soundcore-lib/Cargo.toml b/soundcore-lib/Cargo.toml index 201ae75..d3b345b 100644 --- a/soundcore-lib/Cargo.toml +++ b/soundcore-lib/Cargo.toml @@ -36,6 +36,7 @@ btleplug = { version = "0.11", features = ["serde"], optional = true } [dev-dependencies] test_data = { path = "../test_data" } +soundcore-lib = { path = ".", features = ["mock"], default-features = false } [target.'cfg(target_os = "windows")'.dependencies] diff --git a/soundcore-lib/src/ble.rs b/soundcore-lib/src/ble.rs index 3077645..4efa0a4 100644 --- a/soundcore-lib/src/ble.rs +++ b/soundcore-lib/src/ble.rs @@ -8,9 +8,9 @@ use typeshare::typeshare; use crate::btaddr::BluetoothAdrr; use crate::error::SoundcoreLibResult; -#[cfg(feature = "btleplug-backend")] +#[cfg(all(feature = "btleplug-backend", not(feature = "mock")))] pub mod btleplug; -#[cfg(feature = "winrt-backend")] +#[cfg(all(feature = "winrt-backend", not(feature = "mock")))] pub mod windows; #[async_trait] @@ -80,15 +80,32 @@ pub trait DeviceDescriptor { pub struct BLEDeviceDescriptor { pub addr: BluetoothAdrr, pub name: String, + #[cfg(all(feature = "btleplug-backend", not(feature = "mock")))] + #[typeshare(skip)] + pub id: ::btleplug::platform::PeripheralId, } impl BLEDeviceDescriptor { + #[cfg(any(not(feature = "btleplug-backend"), feature = "mock"))] pub fn new(mac_addr: impl Into, name: impl Into) -> Self { Self { addr: mac_addr.into(), name: name.into(), } } + + #[cfg(all(feature = "btleplug-backend", not(feature = "mock")))] + pub fn new( + mac_addr: impl Into, + id: impl Into<::btleplug::platform::PeripheralId>, + name: impl Into, + ) -> Self { + Self { + addr: mac_addr.into(), + id: id.into(), + name: name.into(), + } + } } impl DeviceDescriptor for BLEDeviceDescriptor { diff --git a/soundcore-lib/src/ble/btleplug/connection_factory.rs b/soundcore-lib/src/ble/btleplug/connection_factory.rs index 9097cb4..a1df2d0 100644 --- a/soundcore-lib/src/ble/btleplug/connection_factory.rs +++ b/soundcore-lib/src/ble/btleplug/connection_factory.rs @@ -1,32 +1,31 @@ use async_trait::async_trait; use btleplug::api::{Central, Peripheral as _}; -use btleplug::platform::{Adapter, Manager, Peripheral}; +use btleplug::platform::{Adapter, Manager, Peripheral, PeripheralId}; -use crate::ble::{BLEConnectionFactory, BLEDeviceDescriptor}; -use crate::btaddr::BluetoothAdrr; -use crate::error::SoundcoreLibError; use crate::{ - ble::{btleplug::connection::BtlePlugConnection, BLEConnectionUuidSet}, + ble::{BLEConnectionUuidSet, btleplug::connection::BtlePlugConnection}, error::SoundcoreLibResult, }; +use crate::ble::{BLEConnectionFactory, BLEDeviceDescriptor}; +use crate::error::SoundcoreLibError; pub struct BtlePlugConnectionFactory { - manager: Manager, + _manager: Manager, adapters: Vec, } impl BtlePlugConnectionFactory { pub fn new(manager: Manager, adapters: Vec) -> SoundcoreLibResult { - Ok(Self { manager, adapters }) + Ok(Self { _manager: manager, adapters }) } async fn find_peripheral( adapter: &Adapter, - addr: &BluetoothAdrr, + id: PeripheralId, ) -> SoundcoreLibResult> { - for periph in adapter.peripherals().await? { - if periph.address() == addr.to_owned().into() { - return Ok(Some(periph)); + for peripheral in adapter.peripherals().await? { + if id == peripheral.id() { + return Ok(Some(peripheral)); } } Ok(None) @@ -46,7 +45,7 @@ impl BLEConnectionFactory for BtlePlugConnectionFactory { for ad in adapters { let _peripherals: Vec = ad.peripherals().await?; let peripheral: Option = - Self::find_peripheral(&ad, &descriptor.addr).await?; + Self::find_peripheral(&ad, descriptor.id.clone()).await?; if peripheral.is_some() { adapter_and_peripheral = Some((ad, peripheral.unwrap())); break; diff --git a/soundcore-lib/src/ble/btleplug/scanner.rs b/soundcore-lib/src/ble/btleplug/scanner.rs index a75b9e5..2445af4 100644 --- a/soundcore-lib/src/ble/btleplug/scanner.rs +++ b/soundcore-lib/src/ble/btleplug/scanner.rs @@ -105,6 +105,7 @@ impl BtlePlugScanner { Some(BLEDeviceDescriptor::new( BluetoothAdrr::try_from(peripheral.address()).ok()?, + peripheral.id(), name, )) } diff --git a/soundcore-lib/src/device_manager.rs b/soundcore-lib/src/device_manager.rs index 0cb4d48..bdc365a 100644 --- a/soundcore-lib/src/device_manager.rs +++ b/soundcore-lib/src/device_manager.rs @@ -18,6 +18,7 @@ use typeshare::typeshare; // TODO: Specify clippy & fmt features #[allow(unused_imports)] +#[cfg(all(feature = "btleplug-backend", not(feature = "mock")))] use crate::ble::btleplug::manager::BtlePlugBLEManager; pub struct DeviceManager diff --git a/soundcore-lib/src/lib.rs b/soundcore-lib/src/lib.rs index 18d1a10..15c21de 100644 --- a/soundcore-lib/src/lib.rs +++ b/soundcore-lib/src/lib.rs @@ -5,7 +5,7 @@ pub mod devices; pub mod error; pub mod models; pub mod packets; -mod parsers; +pub(crate) mod parsers; pub mod types; #[allow(non_snake_case)] mod utils; diff --git a/soundcore-lib/src/mocks/connection.rs b/soundcore-lib/src/mocks/connection.rs index e5f7a8c..6070d03 100644 --- a/soundcore-lib/src/mocks/connection.rs +++ b/soundcore-lib/src/mocks/connection.rs @@ -1,6 +1,7 @@ use std::str::FromStr; use async_trait::async_trait; +use btleplug::platform::PeripheralId; use tokio::sync::Mutex; use crate::btaddr::BluetoothAdrr; diff --git a/soundcore-lib/src/models/eq_profile.rs b/soundcore-lib/src/models/eq_profile.rs index 1a1a4ac..1159e3c 100644 --- a/soundcore-lib/src/models/eq_profile.rs +++ b/soundcore-lib/src/models/eq_profile.rs @@ -28,7 +28,7 @@ pub enum EQProfile { #[default] SoundcoreSignature = 0x0000, Acoustic = 0x0001, - BassBoosted = 0x0002, + BassBooster = 0x0002, BassReducer = 0x0003, Classical = 0x0004, Podcast = 0x0005, @@ -76,7 +76,7 @@ impl EQProfile { let eq: [i8; 8] = match self { Self::SoundcoreSignature => [0, 0, 0, 0, 0, 0, 0, 0], Self::Acoustic => [40, 10, 20, 20, 40, 40, 40, 20], - Self::BassBoosted => [40, 30, 10, 0, 0, 0, 0, 0], + Self::BassBooster => [40, 30, 10, 0, 0, 0, 0, 0], Self::BassReducer => [-40, -30, -10, 0, 0, 0, 0, 0], Self::Classical => [30, 30, -20, -20, 0, 20, 30, 40], Self::Podcast => [-30, 20, 40, 40, 30, 20, 0, -20], diff --git a/soundcore-lib/src/packets/response.rs b/soundcore-lib/src/packets/response.rs index 376f9a9..6293038 100644 --- a/soundcore-lib/src/packets/response.rs +++ b/soundcore-lib/src/packets/response.rs @@ -1,23 +1,23 @@ use log::error; use nom::error::VerboseError; -use crate::api::SoundcoreDeviceState; -use crate::parsers::TaggedData; +pub use bass_up::*; +pub use info::*; +pub use sound_mode::*; +pub use state::*; + use crate::{ models::ResponsePacketKind, parsers::{parse_and_check_checksum, parse_packet_header}, }; +use crate::api::SoundcoreDeviceState; +use crate::parsers::TaggedData; +mod bass_up; mod battery; mod info; mod sound_mode; mod state; -mod bass_up; - -pub use info::*; -pub use sound_mode::*; -pub use state::*; -pub use bass_up::*; #[derive(Debug)] pub enum ResponsePacket { diff --git a/soundcore-lib/src/parsers/eq_configuration.rs b/soundcore-lib/src/parsers/eq_configuration.rs index 2009d9e..943c8ce 100644 --- a/soundcore-lib/src/parsers/eq_configuration.rs +++ b/soundcore-lib/src/parsers/eq_configuration.rs @@ -19,13 +19,13 @@ pub fn parse_stereo_eq_configuration<'a, E: ParseError<'a>>( map( pair(parse_eq_profile, parse_stereo_eq(eq_bands)), |(profile, eq)| match profile { - EQProfile::Custom => StereoEQConfiguration { profile, eq }, + EQProfile::Custom => StereoEQConfiguration { profile, eq}, _ => StereoEQConfiguration { profile, eq: StereoEQ { left: profile.eq(), right: profile.eq(), - }, + } }, }, ),