Skip to content

Commit 7a3bf3c

Browse files
blackspherefollowerqdot
authored andcommitted
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
1 parent cfd9a65 commit 7a3bf3c

File tree

4 files changed

+124
-12
lines changed

4 files changed

+124
-12
lines changed

buttplug/src/server/device/protocol/actuator_command_manager.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::core::{
1919
ScalarSubcommandV4,
2020
},
2121
};
22+
use ahash::{HashMap, HashMapExt};
2223
use getset::Getters;
2324
use std::{
2425
collections::HashSet,
@@ -205,16 +206,17 @@ impl ActuatorCommandManager {
205206
);
206207
}
207208

208-
let mut final_result: Vec<Option<(ActuatorType, u32)>> = vec![
209-
None;
210-
self
211-
.feature_status
212-
.iter()
213-
.filter(|x| x
214-
.messages()
215-
.contains(&ButtplugActuatorFeatureMessageType::ScalarCmd))
216-
.count()
217-
];
209+
let mut idxs = HashMap::new();
210+
for (i, x) in self.feature_status.iter().enumerate() {
211+
if x
212+
.messages()
213+
.contains(&ButtplugActuatorFeatureMessageType::ScalarCmd)
214+
{
215+
idxs.insert(i, idxs.len());
216+
}
217+
}
218+
219+
let mut final_result: Vec<Option<(ActuatorType, u32)>> = vec![None; idxs.len()];
218220

219221
let mut commands: Vec<(u32, ActuatorType, (f64, bool))> = vec![];
220222
msg
@@ -228,7 +230,7 @@ impl ActuatorCommandManager {
228230
)?;
229231
result.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
230232
result.iter().for_each(|(index, actuator, value)| {
231-
final_result[*index as usize] = Some((*actuator, value.0))
233+
final_result[idxs[&(*index as usize)]] = Some((*actuator, value.0))
232234
});
233235
Ok(final_result)
234236
}
@@ -255,7 +257,7 @@ impl ActuatorCommandManager {
255257
.feature_status
256258
.iter()
257259
.filter(|x| x
258-
.messages()
260+
.messages()
259261
.contains(&ButtplugActuatorFeatureMessageType::RotateCmd))
260262
.count()
261263
];

buttplug/tests/test_device_protocols.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ async fn load_test_case(test_file: &str) -> DeviceTestCase {
113113
#[test_case("test_feelingso.yaml" ; "FeelingSo Protocol")]
114114
#[test_case("test_deepsire.yaml" ; "DeepSire Protocol")]
115115
#[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")]
116+
#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")]
116117
#[tokio::test]
117118
async fn test_device_protocols_embedded_v3(test_file: &str) {
118119
//tracing_subscriber::fmt::init();
@@ -216,6 +217,7 @@ async fn test_device_protocols_embedded_v3(test_file: &str) {
216217
#[test_case("test_feelingso.yaml" ; "FeelingSo Protocol")]
217218
#[test_case("test_deepsire.yaml" ; "DeepSire Protocol")]
218219
#[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")]
220+
#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")]
219221
#[tokio::test]
220222
async fn test_device_protocols_json_v3(test_file: &str) {
221223
//tracing_subscriber::fmt::init();
@@ -290,6 +292,7 @@ async fn test_device_protocols_json_v3(test_file: &str) {
290292
#[test_case("test_amorelie_joy_protocol.yaml" ; "Amorelie Joy Protocol")]
291293
#[test_case("test_deepsire.yaml" ; "DeepSire Protocol")]
292294
#[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")]
295+
#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")]
293296
#[tokio::test]
294297
async fn test_device_protocols_embedded_v2(test_file: &str) {
295298
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) {
363366
#[test_case("test_amorelie_joy_protocol.yaml" ; "Amorelie Joy Protocol")]
364367
#[test_case("test_deepsire.yaml" ; "DeepSire Protocol")]
365368
#[test_case("test_xuanhuan_protocol.yaml" ; "Xuanhuan Protocol")]
369+
#[test_case("test_tcode_linear_and_vibrate.yaml" ; "TCode (Linear + Vibrate)")]
366370
#[tokio::test]
367371
async fn test_device_protocols_json_v2(test_file: &str) {
368372
util::device_test::client::client_v2::run_json_test_case(&load_test_case(test_file).await).await;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"version": {
3+
"major": 3,
4+
"minor": 999
5+
},
6+
"user-configs": {
7+
"protocols": {
8+
"tcode-v03": {
9+
"communication": [{
10+
"btle": {
11+
"names": [
12+
"tcode-v03"
13+
],
14+
"services": {
15+
"0000eea0-0000-1000-8000-00805f9b34fb": {
16+
"tx": "0000ee01-0000-1000-8000-00805f9b34fb"
17+
}
18+
}
19+
}
20+
}
21+
],
22+
"configurations": []
23+
}
24+
},
25+
"devices": [
26+
{
27+
"identifier": {
28+
"protocol": "tcode-v03",
29+
"identifier": "tcode-v03",
30+
"address": "COM7"
31+
},
32+
"config": {
33+
"name": "TCode v0.3 (Single Linear Axis + Single Vibe)",
34+
"features": [{
35+
"description": "",
36+
"feature-type": "Position",
37+
"actuator": {
38+
"step-range": [0, 100],
39+
"step-limit": [0, 100],
40+
"messages": ["LinearCmd"]
41+
}
42+
}, {
43+
"description": "",
44+
"feature-type": "Vibrate",
45+
"actuator": {
46+
"step-range": [0, 99],
47+
"step-limit": [0, 99],
48+
"messages": ["ScalarCmd"]
49+
}
50+
}
51+
],
52+
"user-config": {
53+
"allow": false,
54+
"deny": false,
55+
"index": 0
56+
}
57+
}
58+
}
59+
]
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
user_device_config_file: "tcode_linear_and_vibrate_user_config.json"
2+
devices:
3+
- identifier:
4+
name: "tcode-v03"
5+
address: "COM7"
6+
expected_name: "TCode v0.3 (Single Linear Axis + Single Vibe)"
7+
device_commands:
8+
- !Messages
9+
device_index: 0
10+
messages:
11+
- !Linear
12+
- Index: 0
13+
Position: 0.51
14+
Duration: 200
15+
- !Commands
16+
device_index: 0
17+
commands:
18+
- !Write
19+
endpoint: tx
20+
data: [76, 48, 53, 48, 73, 50, 48, 48, 10]
21+
write_with_response: false
22+
- !Messages
23+
device_index: 0
24+
messages:
25+
- !Vibrate
26+
- Index: 0
27+
Speed: 1
28+
- !Commands
29+
device_index: 0
30+
commands:
31+
- !Write
32+
endpoint: tx
33+
data: [86, 48, 57, 57, 10]
34+
write_with_response: false
35+
- !Messages
36+
device_index: 0
37+
messages:
38+
- !Stop
39+
- !Commands
40+
device_index: 0
41+
commands:
42+
- !Write
43+
endpoint: tx
44+
data: [86, 48, 48, 48, 10]
45+
write_with_response: false

0 commit comments

Comments
 (0)