Skip to content

Commit 7b78d8c

Browse files
committed
feat: Add StopDeviceCmdV4 and StopAllDevicesV4
Allows input/output choice in stopping, while also being backward compatible.
1 parent 385963a commit 7b78d8c

File tree

28 files changed

+238
-66
lines changed

28 files changed

+238
-66
lines changed

crates/buttplug_client/src/device/device.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use buttplug_core::message::InputType;
1717
use buttplug_core::{
1818
errors::ButtplugDeviceError,
1919
message::{
20-
ButtplugServerMessageV4, DeviceFeature, DeviceMessageInfoV4, OutputType, StopDeviceCmdV0,
20+
ButtplugServerMessageV4, DeviceFeature, DeviceMessageInfoV4, OutputType, StopDeviceCmdV4,
2121
},
2222
util::stream::convert_broadcast_receiver_to_stream,
2323
};
@@ -308,7 +308,14 @@ impl ButtplugClientDevice {
308308
// All devices accept StopDeviceCmd
309309
self
310310
.event_loop_sender
311-
.send_message_expect_ok(StopDeviceCmdV0::new(self.index).into())
311+
.send_message_expect_ok(StopDeviceCmdV4::new(self.index, true, true).into())
312+
}
313+
314+
pub fn stop_features(&self, inputs: bool, outputs: bool) -> ButtplugClientResultFuture {
315+
// All devices accept StopDeviceCmd
316+
self
317+
.event_loop_sender
318+
.send_message_expect_ok(StopDeviceCmdV4::new(self.index, inputs, outputs).into())
312319
}
313320

314321
pub(crate) fn set_device_connected(&self, connected: bool) {

crates/buttplug_client/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use buttplug_core::{
2828
RequestDeviceListV0,
2929
RequestServerInfoV4,
3030
StartScanningV0,
31-
StopAllDevicesV0,
31+
StopAllDevicesV4,
3232
StopScanningV0,
3333
},
3434
util::{
@@ -435,7 +435,7 @@ impl ButtplugClient {
435435
pub fn stop_all_devices(&self) -> ButtplugClientResultFuture {
436436
self
437437
.message_sender
438-
.send_message_expect_ok(StopAllDevicesV0::default().into())
438+
.send_message_expect_ok(StopAllDevicesV4::default().into())
439439
}
440440

441441
pub fn event_stream(&self) -> impl Stream<Item = ButtplugClientEvent> + use<> {

crates/buttplug_core/schema/buttplug-schema.json

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,48 @@
670670
"minimum": 0
671671
}
672672
}
673+
},
674+
"StopDeviceCmd": {
675+
"type": "object",
676+
"description": "Stops the all actions currently being taken by a device.",
677+
"properties": {
678+
"Id": {
679+
"$ref": "#/components/ClientId"
680+
},
681+
"DeviceIndex": {
682+
"$ref": "#/components/DeviceIndex"
683+
},
684+
"Inputs": {
685+
"type": "boolean"
686+
},
687+
"Outputs": {
688+
"type": "boolean"
689+
}
690+
},
691+
"additionalProperties": false,
692+
"required": [
693+
"Id",
694+
"DeviceIndex"
695+
]
696+
},
697+
"StopAllDevices": {
698+
"type": "object",
699+
"description": "Stops all actions currently being taken by all connected devices.",
700+
"properties": {
701+
"Id": {
702+
"$ref": "#/components/ClientId"
703+
},
704+
"Inputs": {
705+
"type": "boolean"
706+
},
707+
"Outputs": {
708+
"type": "boolean"
709+
}
710+
},
711+
"additionalProperties": false,
712+
"required": [
713+
"Id"
714+
]
673715
}
674716
},
675717
"SpecV3Messages": {
@@ -1997,10 +2039,10 @@
19972039
"$ref": "#/messages/SpecV0Messages/StartScanning"
19982040
},
19992041
"StopAllDevices": {
2000-
"$ref": "#/messages/SpecV0Messages/StopAllDevices"
2042+
"$ref": "#/messages/SpecV4Messages/StopAllDevices"
20012043
},
20022044
"StopDeviceCmd": {
2003-
"$ref": "#/messages/SpecV0Messages/StopDeviceCmd"
2045+
"$ref": "#/messages/SpecV4Messages/StopDeviceCmd"
20042046
},
20052047
"StopScanning": {
20062048
"$ref": "#/messages/SpecV0Messages/StopScanning"

crates/buttplug_core/src/message/device_feature.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub enum InputType {
5252
Button,
5353
#[serde(alias = "pressure")]
5454
Pressure,
55+
#[serde(alias = "depth")]
56+
Depth,
57+
#[serde(alias = "position")]
58+
Position,
5559
// Temperature,
5660
// Accelerometer,
5761
// Gyro,
@@ -305,6 +309,10 @@ pub struct DeviceFeatureInput {
305309
pressure: Option<DeviceFeatureInputProperties>,
306310
#[serde(skip_serializing_if = "Option::is_none")]
307311
button: Option<DeviceFeatureInputProperties>,
312+
#[serde(skip_serializing_if = "Option::is_none")]
313+
depth: Option<DeviceFeatureInputProperties>,
314+
#[serde(skip_serializing_if = "Option::is_none")]
315+
position: Option<DeviceFeatureInputProperties>,
308316
}
309317

310318
impl DeviceFeatureInput {
@@ -314,6 +322,8 @@ impl DeviceFeatureInput {
314322
InputType::Rssi => self.rssi.is_some(),
315323
InputType::Pressure => self.pressure.is_some(),
316324
InputType::Button => self.button.is_some(),
325+
InputType::Depth => self.depth.is_some(),
326+
InputType::Position => self.position.is_some(),
317327
InputType::Unknown => false,
318328
}
319329
}
@@ -324,6 +334,8 @@ impl DeviceFeatureInput {
324334
InputType::Rssi => self.rssi(),
325335
InputType::Pressure => self.pressure(),
326336
InputType::Button => self.button(),
337+
InputType::Depth => self.depth(),
338+
InputType::Position => self.position(),
327339
InputType::Unknown => &None,
328340
}
329341
}

crates/buttplug_core/src/message/serializer/json_serializer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use super::ButtplugSerializerError;
99
use crate::{errors::{ButtplugError, ButtplugMessageError}, message::{ButtplugMessage, ButtplugMessageFinalizer, ErrorV0}};
1010
use jsonschema::Validator;
11-
use log::info;
1211
use serde::{Deserialize, Serialize};
1312
use serde_json::{Deserializer, Value};
1413
use std::fmt::{Debug, Display};
@@ -34,7 +33,6 @@ pub fn vec_to_protocol_json<T>(validator: &Validator, msg: &[T]) -> Result<Strin
3433
where
3534
T: ButtplugMessage + Serialize + Deserialize<'static> + Debug,
3635
{
37-
info!("Serializing {:?}", msg);
3836
let return_error_msg = |e: &dyn Display| {
3937
let err = ButtplugMessageError::MessageSerializationError(ButtplugSerializerError::JsonSerializerError(e.to_string()));
4038
// Just return the error message. For the server, we'll need to wrap it. For the client, we'll just die.

crates/buttplug_core/src/message/v0/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ mod ping;
55
mod request_device_list;
66
mod scanning_finished;
77
mod start_scanning;
8-
mod stop_all_devices;
9-
mod stop_device_cmd;
108
mod stop_scanning;
119

1210
pub use device_removed::DeviceRemovedV0;
@@ -16,6 +14,4 @@ pub use ping::PingV0;
1614
pub use request_device_list::RequestDeviceListV0;
1715
pub use scanning_finished::ScanningFinishedV0;
1816
pub use start_scanning::StartScanningV0;
19-
pub use stop_all_devices::StopAllDevicesV0;
20-
pub use stop_device_cmd::StopDeviceCmdV0;
2117
pub use stop_scanning::StopScanningV0;

crates/buttplug_core/src/message/v4/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ mod output_cmd;
1313
mod request_server_info;
1414
mod server_info;
1515
mod spec_enums;
16+
mod stop_all_devices;
17+
mod stop_device_cmd;
1618

1719
pub use {
1820
device_list::DeviceListV4,
@@ -23,4 +25,6 @@ pub use {
2325
request_server_info::RequestServerInfoV4,
2426
server_info::ServerInfoV4,
2527
spec_enums::{ButtplugClientMessageV4, ButtplugDeviceMessageNameV4, ButtplugServerMessageV4},
28+
stop_all_devices::StopAllDevicesV4,
29+
stop_device_cmd::StopDeviceCmdV4
2630
};

crates/buttplug_core/src/message/v4/spec_enums.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::message::{
1919
ScanningFinishedV0,
2020
ServerInfoV4,
2121
StartScanningV0,
22-
StopAllDevicesV0,
23-
StopDeviceCmdV0,
22+
StopAllDevicesV4,
23+
StopDeviceCmdV4,
2424
StopScanningV0,
2525
v4::input_cmd::InputCmdV4,
2626
};
@@ -49,8 +49,8 @@ pub enum ButtplugClientMessageV4 {
4949
StopScanning(StopScanningV0),
5050
RequestDeviceList(RequestDeviceListV0),
5151
// Generic commands
52-
StopDeviceCmd(StopDeviceCmdV0),
53-
StopAllDevices(StopAllDevicesV0),
52+
StopDeviceCmd(StopDeviceCmdV4),
53+
StopAllDevices(StopAllDevicesV4),
5454
OutputCmd(OutputCmdV4),
5555
InputCmd(InputCmdV4),
5656
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2+
//
3+
// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4+
//
5+
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6+
// for full license information.
7+
8+
use crate::message::{
9+
ButtplugMessage,
10+
ButtplugMessageError,
11+
ButtplugMessageFinalizer,
12+
ButtplugMessageValidator,
13+
};
14+
use serde::{Deserialize, Serialize};
15+
use getset::CopyGetters;
16+
17+
fn mk_true() -> bool { true }
18+
19+
#[derive(
20+
Debug, ButtplugMessage, ButtplugMessageFinalizer, PartialEq, Eq, Clone, Serialize, Deserialize, CopyGetters
21+
)]
22+
#[serde(rename_all = "PascalCase")]
23+
pub struct StopAllDevicesV4 {
24+
id: u32,
25+
#[serde(default = "mk_true")]
26+
#[getset(get_copy = "pub")]
27+
inputs: bool,
28+
#[serde(default = "mk_true")]
29+
#[getset(get_copy = "pub")]
30+
outputs: bool,
31+
}
32+
33+
impl Default for StopAllDevicesV4 {
34+
fn default() -> Self {
35+
Self { id: 1, inputs: true, outputs: true }
36+
}
37+
}
38+
39+
impl ButtplugMessageValidator for StopAllDevicesV4 {
40+
fn is_valid(&self) -> Result<(), ButtplugMessageError> {
41+
self.is_not_system_id(self.id)
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2+
//
3+
// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4+
//
5+
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6+
// for full license information.
7+
8+
use crate::message::{
9+
ButtplugDeviceMessage,
10+
ButtplugMessage,
11+
ButtplugMessageError,
12+
ButtplugMessageFinalizer,
13+
ButtplugMessageValidator,
14+
};
15+
use getset::CopyGetters;
16+
use serde::{Deserialize, Serialize};
17+
18+
fn mk_true() -> bool { true }
19+
20+
#[derive(
21+
Debug,
22+
ButtplugDeviceMessage,
23+
ButtplugMessageFinalizer,
24+
PartialEq,
25+
Eq,
26+
Clone,
27+
Serialize,
28+
Deserialize,
29+
CopyGetters
30+
)]
31+
#[serde(rename_all = "PascalCase")]
32+
pub struct StopDeviceCmdV4 {
33+
id: u32,
34+
device_index: u32,
35+
#[serde(default = "mk_true")]
36+
#[getset(get_copy = "pub")]
37+
inputs: bool,
38+
#[getset(get_copy = "pub")]
39+
#[serde(default = "mk_true")]
40+
outputs: bool,
41+
}
42+
43+
impl StopDeviceCmdV4 {
44+
pub fn new(device_index: u32, inputs: bool, outputs: bool) -> Self {
45+
Self {
46+
id: 1,
47+
device_index,
48+
inputs,
49+
outputs
50+
}
51+
}
52+
}
53+
54+
impl ButtplugMessageValidator for StopDeviceCmdV4 {
55+
fn is_valid(&self) -> Result<(), ButtplugMessageError> {
56+
self.is_not_system_id(self.id)
57+
}
58+
}

0 commit comments

Comments
 (0)