Skip to content

Commit

Permalink
fix: use peripheral id for connecting and fix mock feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Apr 30, 2024
1 parent c603e3b commit 9b1eb94
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions manager-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions soundcore-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 19 additions & 2 deletions soundcore-lib/src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<BluetoothAdrr>, name: impl Into<String>) -> Self {
Self {
addr: mac_addr.into(),
name: name.into(),
}
}

#[cfg(all(feature = "btleplug-backend", not(feature = "mock")))]
pub fn new(
mac_addr: impl Into<BluetoothAdrr>,
id: impl Into<::btleplug::platform::PeripheralId>,
name: impl Into<String>,
) -> Self {
Self {
addr: mac_addr.into(),
id: id.into(),
name: name.into(),
}
}
}

impl DeviceDescriptor for BLEDeviceDescriptor {
Expand Down
23 changes: 11 additions & 12 deletions soundcore-lib/src/ble/btleplug/connection_factory.rs
Original file line number Diff line number Diff line change
@@ -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<Adapter>,
}

impl BtlePlugConnectionFactory {
pub fn new(manager: Manager, adapters: Vec<Adapter>) -> SoundcoreLibResult<Self> {
Ok(Self { manager, adapters })
Ok(Self { _manager: manager, adapters })
}

async fn find_peripheral(
adapter: &Adapter,
addr: &BluetoothAdrr,
id: PeripheralId,
) -> SoundcoreLibResult<Option<Peripheral>> {
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)
Expand All @@ -46,7 +45,7 @@ impl BLEConnectionFactory for BtlePlugConnectionFactory {
for ad in adapters {
let _peripherals: Vec<Peripheral> = ad.peripherals().await?;
let peripheral: Option<Peripheral> =
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;
Expand Down
1 change: 1 addition & 0 deletions soundcore-lib/src/ble/btleplug/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl BtlePlugScanner {

Some(BLEDeviceDescriptor::new(
BluetoothAdrr::try_from(peripheral.address()).ok()?,
peripheral.id(),
name,
))
}
Expand Down
1 change: 1 addition & 0 deletions soundcore-lib/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B>
Expand Down
2 changes: 1 addition & 1 deletion soundcore-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions soundcore-lib/src/mocks/connection.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions soundcore-lib/src/models/eq_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum EQProfile {
#[default]
SoundcoreSignature = 0x0000,
Acoustic = 0x0001,
BassBoosted = 0x0002,
BassBooster = 0x0002,
BassReducer = 0x0003,
Classical = 0x0004,
Podcast = 0x0005,
Expand Down Expand Up @@ -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],
Expand Down
16 changes: 8 additions & 8 deletions soundcore-lib/src/packets/response.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions soundcore-lib/src/parsers/eq_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
},
},
),
Expand Down

0 comments on commit 9b1eb94

Please sign in to comment.