Skip to content

Commit

Permalink
feat: Add typeshare annotations to required types
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Mar 3, 2024
1 parent 8bf19a4 commit a87267f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions soundcore-lib/src/api/state/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use enumflags2::BitFlags;
use serde::{Deserialize, Serialize};
use typeshare::typeshare;

use crate::models::{AgeRange, Battery, ButtonModel, CustomHearID, FirmwareVer, HearID, SerialNumber, SideTone, SoundcoreFeatureFlags, SoundMode, TwsStatus, WearDetection};

/// This is a generalized version of the state for all devices
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Hash, Default)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct SoundcoreDeviceState {
pub feature_flags: BitFlags<SoundcoreFeatureFlags>,
pub battery: Battery,
Expand Down
2 changes: 2 additions & 0 deletions soundcore-lib/src/btaddr.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::fmt::{Debug, Display};

use serde::{Deserialize, Serialize};
use typeshare::typeshare;

use crate::error::{SoundcoreLibError, SoundcoreLibResult};

#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[typeshare]
pub struct BluetoothAdrr {
pub address: [u8; 6],
}
Expand Down
2 changes: 2 additions & 0 deletions soundcore-lib/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use typeshare::typeshare;

pub struct DeviceManager<B>
where
Expand Down Expand Up @@ -102,6 +103,7 @@ where
/// A discovered BLE device. The DiscoveredDevice can be upgraded to a SoundcoreBLEDevice.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "camelCase", tag = "type")]
#[typeshare]
pub struct DiscoveredDevice {
/// The BLE device descriptor.
pub descriptor: BLEDeviceDescriptor,
Expand Down
14 changes: 9 additions & 5 deletions src-tauri/src/async_bridge/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use soundcore_lib::{
device_manager::{create_device_manager, DeviceManager},
};

use super::{BridgeCommand, BridgeResponse};
use super::{BridgeCommand, BridgeResponse, NewStateResponse};

struct CommandLoopState<B: BLEConnectionManager> {
manager: DeviceManager<B>,
Expand Down Expand Up @@ -54,14 +54,14 @@ async fn handle_command<B: BLEConnectionManager>(
output_tx: mpsc::Sender<BridgeResponse>,
) -> BridgeResponse {
match command {
BridgeCommand::ScanBle => command_loop_state
BridgeCommand::Scan => command_loop_state
.lock()
.await
.manager
.ble_scan(None)
.await
.map(BridgeResponse::ScanResult),
BridgeCommand::DisconnectBle(addr) => {
BridgeCommand::Disconnect(addr) => {
let addr_clone = addr.clone();
command_loop_state
.lock()
Expand All @@ -71,7 +71,7 @@ async fn handle_command<B: BLEConnectionManager>(
.await
.map(|_| BridgeResponse::Disconnected(addr_clone))
}
BridgeCommand::ConnectBle(d) => {
BridgeCommand::Connect(d) => {
let addr = d.clone().descriptor.addr;
let device = command_loop_state.lock().await.manager.connect(d).await;
let addr_clone = addr.clone();
Expand All @@ -82,8 +82,12 @@ async fn handle_command<B: BLEConnectionManager>(
while let Ok(()) = state_channel.changed().await {
let state = state_channel.borrow().clone();
// TODO: Add logging
let new_state_response = NewStateResponse {
addr: addr_clone.clone(),
state,
};
output_tx
.send(BridgeResponse::NewState((addr_clone.clone(), state)))
.send(BridgeResponse::NewState(new_state_response))
.await;
}
});
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/async_bridge/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use soundcore_lib::device_manager::DiscoveredDevice;
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase", tag = "command", content = "payload")]
pub enum BridgeCommand {
ScanBle,
ConnectBle(DiscoveredDevice),
DisconnectBle(BluetoothAdrr),
Scan,
Connect(DiscoveredDevice),
Disconnect(BluetoothAdrr),
}
14 changes: 11 additions & 3 deletions src-tauri/src/async_bridge/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ use serde::Serialize;
use soundcore_lib::api::SoundcoreDeviceState;
use soundcore_lib::btaddr::BluetoothAdrr;
use soundcore_lib::device_manager::DiscoveredDevice;
use typeshare::typeshare;

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase", tag = "kind", content = "payload")]
#[typeshare]
pub enum BridgeResponse {
ScanResult(Vec<DiscoveredDevice>),
ConnectionEstablished(BluetoothAdrr),
NewState((BluetoothAdrr, SoundcoreDeviceState)),
NewState(NewStateResponse),
Disconnected(BluetoothAdrr),
Error(String),
}

unsafe impl Send for BridgeResponse {}
unsafe impl Sync for BridgeResponse {}
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct NewStateResponse {
pub addr: BluetoothAdrr,
pub state: SoundcoreDeviceState,
}

0 comments on commit a87267f

Please sign in to comment.