From e91d5ec24aa7e935091f02e8e5bad41f89ccd7ff Mon Sep 17 00:00:00 2001 From: Kyle Machulis Date: Fri, 20 Oct 2023 13:22:25 -0700 Subject: [PATCH] chore: Run rustfmt --- .../btleplug/btleplug_adapter_task.rs | 8 ++-- .../btleplug/btleplug_comm_manager.rs | 19 ++++++-- .../btleplug/btleplug_hardware.rs | 12 ++--- buttplug/src/server/device/hardware/mod.rs | 17 +++---- .../server/device/protocol/adrienlastic.rs | 2 +- .../src/server/device/protocol/lovense.rs | 7 ++- buttplug/src/server/device/protocol/mod.rs | 6 +-- .../server/device/protocol/thehandy/mod.rs | 11 +++-- buttplug/src/server/device/server_device.rs | 48 ++++++++++++++----- 9 files changed, 85 insertions(+), 45 deletions(-) diff --git a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_adapter_task.rs b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_adapter_task.rs index 390d71051..9542c9948 100644 --- a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_adapter_task.rs +++ b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_adapter_task.rs @@ -43,7 +43,7 @@ pub struct BtleplugAdapterTask { event_sender: Sender, command_receiver: Receiver, adapter_connected: Arc, - requires_keepalive: bool + requires_keepalive: bool, } impl BtleplugAdapterTask { @@ -51,13 +51,13 @@ impl BtleplugAdapterTask { event_sender: Sender, command_receiver: Receiver, adapter_connected: Arc, - requires_keepalive: bool + requires_keepalive: bool, ) -> Self { Self { event_sender, command_receiver, adapter_connected, - requires_keepalive + requires_keepalive, } } @@ -118,7 +118,7 @@ impl BtleplugAdapterTask { &properties.services, peripheral.clone(), adapter.clone(), - self.requires_keepalive + self.requires_keepalive, )); if self .event_sender diff --git a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_comm_manager.rs b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_comm_manager.rs index 3ffe31089..2287daaa0 100644 --- a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_comm_manager.rs +++ b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_comm_manager.rs @@ -24,7 +24,7 @@ use tokio::sync::mpsc::{channel, Sender}; #[derive(Default, Clone)] pub struct BtlePlugCommunicationManagerBuilder { - require_keepalive: bool + require_keepalive: bool, } impl BtlePlugCommunicationManagerBuilder { @@ -39,7 +39,10 @@ impl HardwareCommunicationManagerBuilder for BtlePlugCommunicationManagerBuilder &mut self, sender: Sender, ) -> Box { - Box::new(BtlePlugCommunicationManager::new(sender, self.require_keepalive)) + Box::new(BtlePlugCommunicationManager::new( + sender, + self.require_keepalive, + )) } } @@ -50,12 +53,20 @@ pub struct BtlePlugCommunicationManager { } impl BtlePlugCommunicationManager { - pub fn new(event_sender: Sender, require_keepalive: bool) -> Self { + pub fn new( + event_sender: Sender, + require_keepalive: bool, + ) -> Self { let (sender, receiver) = channel(256); let adapter_connected = Arc::new(AtomicBool::new(false)); let adapter_connected_clone = adapter_connected.clone(); async_manager::spawn(async move { - let mut task = BtleplugAdapterTask::new(event_sender, receiver, adapter_connected_clone, require_keepalive); + let mut task = BtleplugAdapterTask::new( + event_sender, + receiver, + adapter_connected_clone, + require_keepalive, + ); task.run().await; }); Self { diff --git a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_hardware.rs b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_hardware.rs index 0d3987448..91465794e 100644 --- a/buttplug/src/server/device/hardware/communication/btleplug/btleplug_hardware.rs +++ b/buttplug/src/server/device/hardware/communication/btleplug/btleplug_hardware.rs @@ -55,7 +55,7 @@ pub(super) struct BtleplugHardwareConnector { services: Vec, device: T, adapter: Adapter, - requires_keepalive: bool + requires_keepalive: bool, } impl BtleplugHardwareConnector { @@ -65,7 +65,7 @@ impl BtleplugHardwareConnector { services: &[Uuid], device: T, adapter: Adapter, - requires_keepalive: bool + requires_keepalive: bool, ) -> Self { Self { name: name.to_owned(), @@ -73,7 +73,7 @@ impl BtleplugHardwareConnector { services: services.to_vec(), device, adapter, - requires_keepalive + requires_keepalive, } } } @@ -122,7 +122,7 @@ impl HardwareConnector for BtleplugHardwareConnector { &self.name, self.device.clone(), self.adapter.clone(), - self.requires_keepalive + self.requires_keepalive, ))) } } @@ -131,7 +131,7 @@ pub struct BtleplugHardwareSpecializer { name: String, device: T, adapter: Adapter, - requires_keepalive: bool + requires_keepalive: bool, } impl BtleplugHardwareSpecializer { @@ -140,7 +140,7 @@ impl BtleplugHardwareSpecializer { name: name.to_owned(), device, adapter, - requires_keepalive + requires_keepalive, } } } diff --git a/buttplug/src/server/device/hardware/mod.rs b/buttplug/src/server/device/hardware/mod.rs index 81442ef19..d0ceff750 100644 --- a/buttplug/src/server/device/hardware/mod.rs +++ b/buttplug/src/server/device/hardware/mod.rs @@ -1,9 +1,6 @@ pub mod communication; -use std::{ - fmt::Debug, - time::Duration, sync::Arc -}; +use std::{fmt::Debug, sync::Arc, time::Duration}; use crate::{ core::{ @@ -16,9 +13,9 @@ use async_trait::async_trait; use futures::future::BoxFuture; use futures_util::FutureExt; use getset::{CopyGetters, Getters}; +use instant::Instant; use serde::{Deserialize, Serialize}; use tokio::sync::{broadcast, RwLock}; -use instant::Instant; /// Parameters for reading data from a [Hardware](crate::device::Hardware) endpoint /// @@ -247,9 +244,9 @@ pub struct Hardware { /// Internal implementation details internal_impl: Box, /// Requires a keepalive signal to be sent by the Server Device class - #[getset(get_copy="pub")] + #[getset(get_copy = "pub")] requires_keepalive: bool, - last_write_time: Arc> + last_write_time: Arc>, } impl Hardware { @@ -265,7 +262,7 @@ impl Hardware { endpoints: endpoints.into(), internal_impl, requires_keepalive: false, - last_write_time: Arc::new(RwLock::new(Instant::now())) + last_write_time: Arc::new(RwLock::new(Instant::now())), } } @@ -329,14 +326,14 @@ impl Hardware { &self, msg: &HardwareWriteCmd, ) -> BoxFuture<'static, Result<(), ButtplugDeviceError>> { - let write_fut = self.internal_impl.write_value(msg); if self.requires_keepalive { let last_write_time = self.last_write_time.clone(); async move { *last_write_time.write().await = Instant::now(); write_fut.await - }.boxed() + } + .boxed() } else { write_fut } diff --git a/buttplug/src/server/device/protocol/adrienlastic.rs b/buttplug/src/server/device/protocol/adrienlastic.rs index 430fa52f6..3502888e5 100644 --- a/buttplug/src/server/device/protocol/adrienlastic.rs +++ b/buttplug/src/server/device/protocol/adrienlastic.rs @@ -20,7 +20,7 @@ pub struct AdrienLastic {} impl ProtocolHandler for AdrienLastic { fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy { - super::ProtocolKeepaliveStrategy::RepeatLastPacketStrategy + super::ProtocolKeepaliveStrategy::RepeatLastPacketStrategy } fn handle_scalar_vibrate_cmd( diff --git a/buttplug/src/server/device/protocol/lovense.rs b/buttplug/src/server/device/protocol/lovense.rs index cd7074468..33843bf90 100644 --- a/buttplug/src/server/device/protocol/lovense.rs +++ b/buttplug/src/server/device/protocol/lovense.rs @@ -158,10 +158,13 @@ pub struct Lovense { } impl ProtocolHandler for Lovense { - fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy { // For Lovense, we'll just repeat the device type packet and drop the result. - super::ProtocolKeepaliveStrategy::RepeatPacketStrategy(HardwareWriteCmd::new(Endpoint::Tx, b"DeviceType;".to_vec(), false)) + super::ProtocolKeepaliveStrategy::RepeatPacketStrategy(HardwareWriteCmd::new( + Endpoint::Tx, + b"DeviceType;".to_vec(), + false, + )) } fn handle_scalar_cmd( diff --git a/buttplug/src/server/device/protocol/mod.rs b/buttplug/src/server/device/protocol/mod.rs index b2cc951ab..4a2c7c47b 100644 --- a/buttplug/src/server/device/protocol/mod.rs +++ b/buttplug/src/server/device/protocol/mod.rs @@ -131,9 +131,9 @@ use std::{collections::HashMap, sync::Arc}; /// things alive. Currently this only applies to iOS backgrounding with bluetooth devices, but since /// we never know which of our hundreds of supported devices someone might connect, we need context /// as to which keepalive strategy to use. -/// +/// /// When choosing a keepalive strategy for a protocol: -/// +/// /// - All protocols use NoStrategy by default. For many devices, sending trash will break them in /// very weird ways and we can't risk that, so we need to know the protocol context. /// - If the protocol already needs its own keepalive (Satisfyer, Mysteryvibe, etc...), use @@ -159,7 +159,7 @@ pub enum ProtocolKeepaliveStrategy { /// will be useful for most devices that purely use scalar commands. RepeatLastPacketStrategy, /// Call a specific method on the protocol implementation to generate keepalive packets. - CustomStrategy + CustomStrategy, } pub trait ProtocolIdentifierFactory: Send + Sync { diff --git a/buttplug/src/server/device/protocol/thehandy/mod.rs b/buttplug/src/server/device/protocol/thehandy/mod.rs index 1f2341425..af0c95670 100644 --- a/buttplug/src/server/device/protocol/thehandy/mod.rs +++ b/buttplug/src/server/device/protocol/thehandy/mod.rs @@ -116,19 +116,22 @@ pub struct TheHandy { } impl ProtocolHandler for TheHandy { - fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy { let ping_payload = handyplug::Payload { messages: vec![handyplug::Message { message: Some(handyplug::message::Message::Ping(Ping { id: 999 })), - }] + }], }; let mut ping_buf = vec![]; ping_payload .encode(&mut ping_buf) .expect("Infallible encode."); - - super::ProtocolKeepaliveStrategy::RepeatPacketStrategy(HardwareWriteCmd::new(Endpoint::Tx, ping_buf, true)) + + super::ProtocolKeepaliveStrategy::RepeatPacketStrategy(HardwareWriteCmd::new( + Endpoint::Tx, + ping_buf, + true, + )) } fn handle_fleshlight_launch_fw12_cmd( diff --git a/buttplug/src/server/device/server_device.rs b/buttplug/src/server/device/server_device.rs index bf9a2643f..783724036 100644 --- a/buttplug/src/server/device/server_device.rs +++ b/buttplug/src/server/device/server_device.rs @@ -44,7 +44,7 @@ use crate::{ }, ButtplugServerResultFuture, }, - util::{self, stream::convert_broadcast_receiver_to_stream, async_manager}, + util::{self, async_manager, stream::convert_broadcast_receiver_to_stream}, }; use core::hash::{Hash, Hasher}; use dashmap::DashSet; @@ -56,7 +56,12 @@ use tokio_stream::StreamExt; use super::{ configuration::{ProtocolDeviceAttributes, ServerDeviceMessageAttributes}, - protocol::{generic_command_manager::GenericCommandManager, ProtocolSpecializer, ProtocolKeepaliveStrategy}, hardware::HardwareWriteCmd, + hardware::HardwareWriteCmd, + protocol::{ + generic_command_manager::GenericCommandManager, + ProtocolKeepaliveStrategy, + ProtocolSpecializer, + }, }; #[derive(Debug)] @@ -164,11 +169,19 @@ pub(super) async fn build_server_device( // We now have fully initialized hardware, return a server device. let device = ServerDevice::new(identifier, handler, hardware, &attrs); - + // If we need a keepalive with a packet replay, set this up via stopping the device on connect. - if requires_keepalive && matches!(strategy, ProtocolKeepaliveStrategy::RepeatLastPacketStrategy) { + if requires_keepalive + && matches!( + strategy, + ProtocolKeepaliveStrategy::RepeatLastPacketStrategy + ) + { if let Err(e) = device.handle_stop_device_cmd().await { - return Err(ButtplugDeviceError::DeviceConnectionError(format!("Error setting up keepalive: {}", e))); + return Err(ButtplugDeviceError::DeviceConnectionError(format!( + "Error setting up keepalive: {}", + e + ))); } } @@ -183,7 +196,7 @@ pub struct ServerDevice { /// Unique identifier for the device identifier: ServerDeviceIdentifier, raw_subscribed_endpoints: Arc>, - keepalive_packet: Arc>> + keepalive_packet: Arc>>, } impl Debug for ServerDevice { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -220,7 +233,12 @@ impl ServerDevice { let keepalive_packet = Arc::new(RwLock::new(None)); let gcm = GenericCommandManager::new(attributes); // If we've gotten here, we know our hardware is connected. This means we can start the keepalive if it's required. - if hardware.requires_keepalive() && !matches!(handler.keepalive_strategy(), ProtocolKeepaliveStrategy::NoStrategy) { + if hardware.requires_keepalive() + && !matches!( + handler.keepalive_strategy(), + ProtocolKeepaliveStrategy::NoStrategy + ) + { let hardware = hardware.clone(); let strategy = handler.keepalive_strategy(); let keepalive_packet = keepalive_packet.clone(); @@ -243,11 +261,14 @@ impl ServerDevice { break; } } - }, + } _ => { - info!("Protocol keepalive strategy {:?} not implemented, replacing with NoStrategy", strategy); + info!( + "Protocol keepalive strategy {:?} not implemented, replacing with NoStrategy", + strategy + ); } - } + } } // Arbitrary wait time for now. util::sleep(wait_duration).await; @@ -548,7 +569,12 @@ impl ServerDevice { // disconnected. for command in commands { hardware.parse_message(&command).await?; - if hardware.requires_keepalive() && matches!(keepalive_type, ProtocolKeepaliveStrategy::RepeatLastPacketStrategy) { + if hardware.requires_keepalive() + && matches!( + keepalive_type, + ProtocolKeepaliveStrategy::RepeatLastPacketStrategy + ) + { if let HardwareCommand::Write(command) = command { *keepalive_packet.write().await = Some(command); }