Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/buttplug_core/src/connector/remote_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ use super::{
ButtplugConnectorResultFuture,
transport::{ButtplugConnectorTransport, ButtplugTransportIncomingMessage},
};
use crate::{
message::{
ButtplugMessage,
serializer::{ButtplugMessageSerializer, ButtplugSerializedMessage},
},
use crate::message::{
ButtplugMessage,
serializer::{ButtplugMessageSerializer, ButtplugSerializedMessage},
};
use futures::{FutureExt, future::BoxFuture, select};
use log::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/buttplug_core/src/util/async_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ pub async fn sleep(duration: Duration) {
#[macro_export]
macro_rules! spawn {
($future:expr) => {
$crate::util::async_manager::spawn($future, tracing::span!(tracing::Level::INFO, "Buttplug Async Task"))
$crate::util::async_manager::spawn(
$future,
tracing::span!(tracing::Level::INFO, "Buttplug Async Task"),
)
};
($name:expr, $future:expr) => {
$crate::util::async_manager::spawn($future, tracing::span!(tracing::Level::INFO, $name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use crate::device::{
},
};
use async_trait::async_trait;
use buttplug_core::{
errors::ButtplugDeviceError,
util::async_manager,
};
use buttplug_core::{errors::ButtplugDeviceError, util::async_manager};
use buttplug_server_device_config::{
Endpoint,
ProtocolCommunicationSpecifier,
Expand Down
5 changes: 1 addition & 4 deletions crates/buttplug_server/src/device/protocol_impl/hgod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use crate::device::{
},
};
use async_trait::async_trait;
use buttplug_core::{
errors::ButtplugDeviceError,
util::async_manager,
};
use buttplug_core::{errors::ButtplugDeviceError, util::async_manager};
use buttplug_server_device_config::{
Endpoint,
ProtocolCommunicationSpecifier,
Expand Down
19 changes: 11 additions & 8 deletions crates/buttplug_server/src/device/protocol_impl/honeyplaybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::device::{
},
};
use async_trait::async_trait;
use buttplug_core::errors::ButtplugDeviceError;
use buttplug_core::message::{InputReadingV4, InputTypeReading, InputValue};
use buttplug_core::util::async_manager;
use buttplug_core::errors::ButtplugDeviceError;
use buttplug_server_device_config::Endpoint;
use buttplug_server_device_config::{
ProtocolCommunicationSpecifier,
Expand Down Expand Up @@ -217,13 +217,16 @@ impl HoneyPlayBox {
);
let packet_id = Arc::new(AtomicU8::new(count));
let last_send = Arc::new(RwLock::new(Instant::now()));
buttplug_core::spawn!("HoneyPlayboxKeepalive", hpb_keepalive(
hardware.clone(),
random_key,
last_command.clone(),
packet_id.clone(),
last_send.clone(),
));
buttplug_core::spawn!(
"HoneyPlayboxKeepalive",
hpb_keepalive(
hardware.clone(),
random_key,
last_command.clone(),
packet_id.clone(),
last_send.clone(),
)
);

Self {
random_key,
Expand Down
93 changes: 85 additions & 8 deletions crates/buttplug_server/src/device/protocol_impl/joyhub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,61 @@
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use std::sync::atomic::{AtomicU8, Ordering};

use uuid::{Uuid, uuid};

use crate::device::hardware::{HardwareCommand, HardwareWriteCmd};
use crate::device::{
hardware::{HardwareCommand, HardwareWriteCmd},
protocol::{ProtocolHandler, generic_protocol_setup},
hardware::Hardware,
protocol::{
ProtocolHandler,
ProtocolIdentifier,
ProtocolInitializer,
generic_protocol_initializer_setup,
},
};
use async_trait::async_trait;
use buttplug_core::errors::ButtplugDeviceError;
use buttplug_server_device_config::Endpoint;
use buttplug_core::util::async_manager;
use buttplug_server_device_config::{
Endpoint,
ProtocolCommunicationSpecifier,
ServerDeviceDefinition,
UserDeviceIdentifier,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use std::time::Duration;
use uuid::{Uuid, uuid};

const JOYHUB_PROTOCOL_UUID: Uuid = uuid!("c0f6785a-0056-4a2a-a2a9-dc7ca4ae2a0d");

generic_protocol_setup!(JoyHub, "joyhub");
generic_protocol_initializer_setup!(JoyHub, "joyhub");

#[derive(Default)]
pub struct JoyHubInitializer {}

#[async_trait]
impl ProtocolInitializer for JoyHubInitializer {
async fn initialize(
&mut self,
hardware: Arc<Hardware>,
_def: &ServerDeviceDefinition,
) -> Result<Arc<dyn ProtocolHandler>, ButtplugDeviceError> {
Ok(Arc::new(JoyHub::new(hardware.clone())))
}
}

pub struct JoyHub {
last_cmds: [AtomicU8; 4],
hardware: Arc<Hardware>,
}

impl JoyHub {
pub fn new(hardware: Arc<Hardware>) -> Self {
Self {
last_cmds: [const { AtomicU8::new(0) }; 4],
hardware,
}
}

fn form_hardware_command(
&self,
index: u32,
Expand All @@ -52,6 +86,24 @@ impl JoyHub {
}
}

async fn cancel_spray(device: Arc<Hardware>, feature_id: Uuid) {
async_manager::sleep(Duration::from_millis(1000)).await;
if let Err(e) = device
.write_value(&HardwareWriteCmd::new(
&[feature_id],
Endpoint::Tx,
vec![0xa0, 0x24, 0x00, 0x00, 0x00, 0x00],
false,
))
.await
{
warn!(
"Failed to stop the lube pump (the device has probably disconnected): {:?}",
e
);
}
}

impl ProtocolHandler for JoyHub {
fn handle_output_vibrate_cmd(
&self,
Expand Down Expand Up @@ -157,4 +209,29 @@ impl ProtocolHandler for JoyHub {
.into(),
])
}

fn handle_output_spray_cmd(
&self,
_feature_index: u32,
feature_id: Uuid,
level: u32,
) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
buttplug_core::spawn!(
"JoyHub spray canceller",
cancel_spray(self.hardware.clone(), feature_id)
);
Ok(vec![
HardwareWriteCmd::new(
&[feature_id],
Endpoint::Tx,
if level == 0 {
vec![0xa0, 0x24, 0x00, 0x00, 0x00, 0x00]
} else {
vec![0xa0, 0x24, 0x01, 0x00, 0x01, 0xff]
},
false,
)
.into(),
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ use crate::device::{
hardware::{Hardware, HardwareCommand, HardwareWriteCmd},
protocol::{ProtocolHandler, ProtocolKeepaliveStrategy},
};
use buttplug_core::{
errors::ButtplugDeviceError,
message::InputReadingV4,
util::async_manager,
};
use buttplug_core::{errors::ButtplugDeviceError, message::InputReadingV4, util::async_manager};
use buttplug_server_device_config::Endpoint;
use futures::future::BoxFuture;
use std::{
Expand All @@ -35,10 +31,10 @@ pub struct LovenseStroker {
impl LovenseStroker {
pub fn new(hardware: Arc<Hardware>, need_range_zerod: bool) -> Self {
let linear_info = Arc::new((AtomicU32::new(0), AtomicU32::new(0)));
buttplug_core::spawn!("LovenseStroker update linear movement", update_linear_movement(
hardware.clone(),
linear_info.clone(),
));
buttplug_core::spawn!(
"LovenseStroker update linear movement",
update_linear_movement(hardware.clone(), linear_info.clone(),)
);
Self {
linear_info,
need_range_zerod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::device::{
},
};
use async_trait::async_trait;
use buttplug_core::{errors::ButtplugDeviceError};
use buttplug_core::errors::ButtplugDeviceError;
use buttplug_server_device_config::{
Endpoint,
ProtocolCommunicationSpecifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use std::sync::{
};
use uuid::{Uuid, uuid};

use rand::distr::Alphanumeric;
use rand::RngExt;
use rand::distr::Alphanumeric;
use regex::Regex;
use sha2::{Digest, Sha256};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ProtocolInitializer for VorzeSAInitializer {
}
}
"vorze-sa-dual-vibrator" => {
if hwname.contains("omorfi") {
if hwname.contains("omor") {
Ok(Arc::new(dual_vibrator::VorzeSADualVibrator::default()))
} else {
Err(ButtplugDeviceError::ProtocolNotImplemented(format!(
Expand Down
11 changes: 4 additions & 7 deletions crates/buttplug_server/src/device/protocol_impl/xuanhuan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use crate::device::{
},
};
use async_trait::async_trait;
use buttplug_core::{
errors::ButtplugDeviceError,
util::async_manager,
};
use buttplug_core::{errors::ButtplugDeviceError, util::async_manager};
use buttplug_server_device_config::{
Endpoint,
ProtocolCommunicationSpecifier,
Expand Down Expand Up @@ -83,9 +80,9 @@ impl Xuanhuan {
fn new(device: Arc<Hardware>) -> Self {
let current_command = Arc::new(AtomicU8::new(0));
let current_command_clone = current_command.clone();
buttplug_core::spawn!("Xuanhuan vibration update",
async move { vibration_update_handler(device, current_command_clone).await }
);
buttplug_core::spawn!("Xuanhuan vibration update", async move {
vibration_update_handler(device, current_command_clone).await
});
Self { current_command }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use buttplug_core::{
message::{ButtplugServerMessageV4, DeviceListV4, ScanningFinishedV0},
};
use buttplug_core::message::{ButtplugServerMessageV4, DeviceListV4, ScanningFinishedV0};
use buttplug_server_device_config::DeviceConfigurationManager;
use tracing::info_span;

use super::server_device_manager::DeviceManagerCommand;
use crate::device::{
DeviceHandle,
InternalDeviceEvent,
Expand All @@ -23,7 +22,6 @@ use futures::{FutureExt, future};
use std::sync::Arc;
use tokio::sync::{broadcast, mpsc};
use tokio_util::sync::CancellationToken;
use super::server_device_manager::DeviceManagerCommand;

/// Scanning state machine for the device manager event loop.
/// Replaces the previous combination of scanning_bringup_in_progress, scanning_started,
Expand Down Expand Up @@ -286,27 +284,35 @@ impl ServerDeviceManagerEventLoop {
// Clone sender again for the forwarding task that build_device_handle will spawn
let device_event_sender_for_forwarding = self.device_event_sender.clone();

buttplug_core::util::async_manager::spawn(async move {
match build_device_handle(
device_config_manager,
creator,
protocol_specializers,
device_event_sender_for_forwarding,
).await {
Ok(device_handle) => {
if device_event_sender_clone
.send(InternalDeviceEvent::Connected(device_handle))
.await
.is_err() {
error!("Device manager disappeared before connection established, device will be dropped.");
buttplug_core::util::async_manager::spawn(
async move {
match build_device_handle(
device_config_manager,
creator,
protocol_specializers,
device_event_sender_for_forwarding,
)
.await
{
Ok(device_handle) => {
if device_event_sender_clone
.send(InternalDeviceEvent::Connected(device_handle))
.await
.is_err()
{
error!(
"Device manager disappeared before connection established, device will be dropped."
);
}
}
Err(e) => {
error!("Device errored while trying to connect: {:?}", e);
}
},
Err(e) => {
error!("Device errored while trying to connect: {:?}", e);
}
}
connecting_devices.remove(&address);
}, span);
connecting_devices.remove(&address);
},
span,
);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/buttplug_server/src/message/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ pub use device_message_info::DeviceMessageInfoV1;
pub use linear_cmd::{LinearCmdV1, VectorSubcommandV1};
pub use request_server_info::RequestServerInfoV1;
pub use rotate_cmd::{RotateCmdV1, RotationSubcommandV1};
pub use spec_enums::{ButtplugClientMessageV1, ButtplugDeviceMessageNameV1, ButtplugServerMessageV1};
pub use spec_enums::{
ButtplugClientMessageV1,
ButtplugDeviceMessageNameV1,
ButtplugServerMessageV1,
};
pub use vibrate_cmd::{VibrateCmdV1, VibrateSubcommandV1};
9 changes: 6 additions & 3 deletions crates/buttplug_server/src/message/v4/checked_output_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ impl TryFromDeviceAttributes<OutputCmdV4> for CheckedOutputCmdV4 {
if let Some(output_map) = feature.output() {
let output_type = cmd.command().as_output_type();
if output_map.is_disabled(output_type) {
return Err(ButtplugError::from(ButtplugDeviceError::MessageNotSupported(
format!("Output type {:?} is disabled for this device", output_type),
)));
return Err(ButtplugError::from(
ButtplugDeviceError::MessageNotSupported(format!(
"Output type {:?} is disabled for this device",
output_type
)),
));
}
let value = cmd.command().value();
let new_value = output_map
Expand Down
Loading