From a87267fd125893efe356952a595c7b28499147dd Mon Sep 17 00:00:00 2001 From: Gregory Mallios Date: Sun, 3 Mar 2024 16:42:03 +0200 Subject: [PATCH] feat: Add typeshare annotations to required types --- soundcore-lib/src/api/state/state.rs | 3 +++ soundcore-lib/src/btaddr.rs | 2 ++ soundcore-lib/src/device_manager.rs | 2 ++ src-tauri/src/async_bridge/bridge.rs | 14 +++++++++----- src-tauri/src/async_bridge/command.rs | 6 +++--- src-tauri/src/async_bridge/response.rs | 14 +++++++++++--- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/soundcore-lib/src/api/state/state.rs b/soundcore-lib/src/api/state/state.rs index 27a070f..8ecff6c 100644 --- a/soundcore-lib/src/api/state/state.rs +++ b/soundcore-lib/src/api/state/state.rs @@ -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, pub battery: Battery, diff --git a/soundcore-lib/src/btaddr.rs b/soundcore-lib/src/btaddr.rs index d856320..3514367 100644 --- a/soundcore-lib/src/btaddr.rs +++ b/soundcore-lib/src/btaddr.rs @@ -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], } diff --git a/soundcore-lib/src/device_manager.rs b/soundcore-lib/src/device_manager.rs index d7108ac..6aebc01 100644 --- a/soundcore-lib/src/device_manager.rs +++ b/soundcore-lib/src/device_manager.rs @@ -14,6 +14,7 @@ use crate::{ }; use serde::{Deserialize, Serialize}; use tokio::sync::RwLock; +use typeshare::typeshare; pub struct DeviceManager where @@ -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, diff --git a/src-tauri/src/async_bridge/bridge.rs b/src-tauri/src/async_bridge/bridge.rs index 18634c7..8ff2f06 100644 --- a/src-tauri/src/async_bridge/bridge.rs +++ b/src-tauri/src/async_bridge/bridge.rs @@ -9,7 +9,7 @@ use soundcore_lib::{ device_manager::{create_device_manager, DeviceManager}, }; -use super::{BridgeCommand, BridgeResponse}; +use super::{BridgeCommand, BridgeResponse, NewStateResponse}; struct CommandLoopState { manager: DeviceManager, @@ -54,14 +54,14 @@ async fn handle_command( output_tx: mpsc::Sender, ) -> 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() @@ -71,7 +71,7 @@ async fn handle_command( .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(); @@ -82,8 +82,12 @@ async fn handle_command( 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; } }); diff --git a/src-tauri/src/async_bridge/command.rs b/src-tauri/src/async_bridge/command.rs index 3a27371..8305030 100644 --- a/src-tauri/src/async_bridge/command.rs +++ b/src-tauri/src/async_bridge/command.rs @@ -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), } diff --git a/src-tauri/src/async_bridge/response.rs b/src-tauri/src/async_bridge/response.rs index 2a32250..fe77d6f 100644 --- a/src-tauri/src/async_bridge/response.rs +++ b/src-tauri/src/async_bridge/response.rs @@ -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), 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, +} \ No newline at end of file