From add4f35da7b34ae733c415aa41827f64378641eb Mon Sep 17 00:00:00 2001 From: blackspherefollower Date: Sun, 17 Nov 2024 09:56:41 +0000 Subject: [PATCH] fix: Correct the state addressing for ScalarCmd This does not apply to RotateCmd because there isn't a v3->v4 feature index converter yet. Fixes #668 --- .../protocol/actuator_command_manager.rs | 26 ++++---- buttplug/tests/test_device_protocols.rs | 4 ++ .../tcode_linear_and_vibrate_user_config.json | 61 +++++++++++++++++++ .../test_tcode_linear_and_vibrate.yaml | 45 ++++++++++++++ 4 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 buttplug/tests/util/device_test/device_test_case/config/tcode_linear_and_vibrate_user_config.json create mode 100644 buttplug/tests/util/device_test/device_test_case/test_tcode_linear_and_vibrate.yaml diff --git a/buttplug/src/server/device/protocol/actuator_command_manager.rs b/buttplug/src/server/device/protocol/actuator_command_manager.rs index df369faf0..1af2e9048 100644 --- a/buttplug/src/server/device/protocol/actuator_command_manager.rs +++ b/buttplug/src/server/device/protocol/actuator_command_manager.rs @@ -19,6 +19,7 @@ use crate::core::{ ScalarSubcommandV4, }, }; +use ahash::{HashMap, HashMapExt}; use getset::Getters; use std::{ collections::HashSet, @@ -205,16 +206,17 @@ impl ActuatorCommandManager { ); } - let mut final_result: Vec> = vec![ - None; - self - .feature_status - .iter() - .filter(|x| x - .messages() - .contains(&ButtplugActuatorFeatureMessageType::ScalarCmd)) - .count() - ]; + let mut idxs = HashMap::new(); + for (i, x) in self.feature_status.iter().enumerate() { + if x + .messages() + .contains(&ButtplugActuatorFeatureMessageType::ScalarCmd) + { + idxs.insert(i, idxs.len()); + } + } + + let mut final_result: Vec> = vec![None; idxs.len()]; let mut commands: Vec<(u32, ActuatorType, (f64, bool))> = vec![]; msg @@ -228,7 +230,7 @@ impl ActuatorCommandManager { )?; result.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); result.iter().for_each(|(index, actuator, value)| { - final_result[*index as usize] = Some((*actuator, value.0)) + final_result[idxs[&(*index as usize)]] = Some((*actuator, value.0)) }); Ok(final_result) } @@ -255,7 +257,7 @@ impl ActuatorCommandManager { .feature_status .iter() .filter(|x| x - .messages() + .messages() .contains(&ButtplugActuatorFeatureMessageType::RotateCmd)) .count() ]; diff --git a/buttplug/tests/test_device_protocols.rs b/buttplug/tests/test_device_protocols.rs index 2877d37e9..23f4dbeea 100644 --- a/buttplug/tests/test_device_protocols.rs +++ b/buttplug/tests/test_device_protocols.rs @@ -113,6 +113,7 @@ async fn load_test_case(test_file: &str) -> DeviceTestCase { #[test_case("test_feelingso.yaml" ; "FeelingSo Protocol")] #[test_case("test_deepsire.yaml" ; "DeepSire Protocol")] #[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")] +#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")] #[tokio::test] async fn test_device_protocols_embedded_v3(test_file: &str) { //tracing_subscriber::fmt::init(); @@ -216,6 +217,7 @@ async fn test_device_protocols_embedded_v3(test_file: &str) { #[test_case("test_feelingso.yaml" ; "FeelingSo Protocol")] #[test_case("test_deepsire.yaml" ; "DeepSire Protocol")] #[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")] +#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")] #[tokio::test] async fn test_device_protocols_json_v3(test_file: &str) { //tracing_subscriber::fmt::init(); @@ -290,6 +292,7 @@ async fn test_device_protocols_json_v3(test_file: &str) { #[test_case("test_amorelie_joy_protocol.yaml" ; "Amorelie Joy Protocol")] #[test_case("test_deepsire.yaml" ; "DeepSire Protocol")] #[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")] +#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")] #[tokio::test] async fn test_device_protocols_embedded_v2(test_file: &str) { util::device_test::client::client_v2::run_embedded_test_case(&load_test_case(test_file).await) @@ -363,6 +366,7 @@ async fn test_device_protocols_embedded_v2(test_file: &str) { #[test_case("test_amorelie_joy_protocol.yaml" ; "Amorelie Joy Protocol")] #[test_case("test_deepsire.yaml" ; "DeepSire Protocol")] #[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")] +#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")] #[tokio::test] async fn test_device_protocols_json_v2(test_file: &str) { util::device_test::client::client_v2::run_json_test_case(&load_test_case(test_file).await).await; diff --git a/buttplug/tests/util/device_test/device_test_case/config/tcode_linear_and_vibrate_user_config.json b/buttplug/tests/util/device_test/device_test_case/config/tcode_linear_and_vibrate_user_config.json new file mode 100644 index 000000000..46e29ccbc --- /dev/null +++ b/buttplug/tests/util/device_test/device_test_case/config/tcode_linear_and_vibrate_user_config.json @@ -0,0 +1,61 @@ +{ + "version": { + "major": 3, + "minor": 999 + }, + "user-configs": { + "protocols": { + "tcode-v03": { + "communication": [{ + "btle": { + "names": [ + "tcode-v03" + ], + "services": { + "0000eea0-0000-1000-8000-00805f9b34fb": { + "tx": "0000ee01-0000-1000-8000-00805f9b34fb" + } + } + } + } + ], + "configurations": [] + } + }, + "devices": [ + { + "identifier": { + "protocol": "tcode-v03", + "identifier": "tcode-v03", + "address": "COM7" + }, + "config": { + "name": "TCode v0.3 (Single Linear Axis + Single Vibe)", + "features": [{ + "description": "", + "feature-type": "Position", + "actuator": { + "step-range": [0, 100], + "step-limit": [0, 100], + "messages": ["LinearCmd"] + } + }, { + "description": "", + "feature-type": "Vibrate", + "actuator": { + "step-range": [0, 99], + "step-limit": [0, 99], + "messages": ["ScalarCmd"] + } + } + ], + "user-config": { + "allow": false, + "deny": false, + "index": 0 + } + } + } + ] + } +} \ No newline at end of file diff --git a/buttplug/tests/util/device_test/device_test_case/test_tcode_linear_and_vibrate.yaml b/buttplug/tests/util/device_test/device_test_case/test_tcode_linear_and_vibrate.yaml new file mode 100644 index 000000000..56d665952 --- /dev/null +++ b/buttplug/tests/util/device_test/device_test_case/test_tcode_linear_and_vibrate.yaml @@ -0,0 +1,45 @@ +user_device_config_file: "tcode_linear_and_vibrate_user_config.json" +devices: + - identifier: + name: "tcode-v03" + address: "COM7" + expected_name: "TCode v0.3 (Single Linear Axis + Single Vibe)" +device_commands: + - !Messages + device_index: 0 + messages: + - !Linear + - Index: 0 + Position: 0.51 + Duration: 200 + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [76, 48, 53, 48, 73, 50, 48, 48, 10] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Vibrate + - Index: 0 + Speed: 1 + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [86, 48, 57, 57, 10] + write_with_response: false + - !Messages + device_index: 0 + messages: + - !Stop + - !Commands + device_index: 0 + commands: + - !Write + endpoint: tx + data: [86, 48, 48, 48, 10] + write_with_response: false