Skip to content

Commit 6967339

Browse files
committed
feat: Add simple message builder functions to generic messages
Building generic messages by hand sucks, especially when it's a repeated packet. Build in simplification functions. Fixes #408 Affects #403
1 parent c0edd18 commit 6967339

18 files changed

Lines changed: 122 additions & 297 deletions

Buttplug.Core/Messages/Messages.cs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
55
using System.Reflection;
6+
using Newtonsoft.Json;
67

78
// Namespace containing all Buttplug messages, as specified by the Buttplug Message Spec at
89
// https://docs.buttplug.io/buttplug. For consistency sake, all message descriptions are stated in
@@ -966,6 +967,30 @@ public VibrateSubcommand(uint aIndex, double aSpeed)
966967
}
967968
}
968969

970+
public static VibrateCmd Create(uint aDeviceIndex, uint aMsgId, double aSpeed, uint aCmdCount)
971+
{
972+
return Create(aDeviceIndex, aMsgId, Enumerable.Repeat(aSpeed, (int)aCmdCount).ToArray(), aCmdCount);
973+
}
974+
975+
public static VibrateCmd Create(uint aDeviceIndex, uint aMsgId, IEnumerable<double> aSpeeds, uint aCmdCount)
976+
{
977+
if (aCmdCount != aSpeeds.Count())
978+
{
979+
throw new ArgumentException("Number of speeds and number of commands must match.");
980+
}
981+
982+
var cmdList = new List<VibrateSubcommand>((int)aCmdCount);
983+
// TODO This pattern sucks
984+
uint i = 0;
985+
foreach (var speed in aSpeeds)
986+
{
987+
cmdList.Add(new VibrateSubcommand(i, speed));
988+
++i;
989+
}
990+
991+
return new VibrateCmd(aDeviceIndex, cmdList, aMsgId);
992+
}
993+
969994
/// <summary>
970995
/// List of vibrator speeds.
971996
/// </summary>
@@ -1049,6 +1074,26 @@ public RotateSubcommand(uint aIndex, double aSpeed, bool aClockwise)
10491074
}
10501075
}
10511076

1077+
public static RotateCmd Create(uint aDeviceIndex, uint aMsgId, double aSpeed, bool aClockwise, uint aCmdCount)
1078+
{
1079+
return Create(aDeviceIndex, aMsgId, Enumerable.Repeat((aSpeed, aClockwise), (int)aCmdCount), aCmdCount);
1080+
}
1081+
1082+
public static RotateCmd Create(uint aDeviceIndex, uint aMsgId, IEnumerable<(double speed, bool clockwise)> aCmds, uint aCmdCount)
1083+
{
1084+
if (aCmdCount != aCmds.Count())
1085+
{
1086+
throw new ArgumentException("Number of speeds and number of commands must match.");
1087+
}
1088+
var cmdList = new List<RotateSubcommand>((int)aCmdCount);
1089+
uint i = 0;
1090+
foreach (var cmd in aCmds)
1091+
{
1092+
cmdList.Add(new RotateSubcommand(i, cmd.speed, cmd.clockwise));
1093+
}
1094+
return new RotateCmd(aDeviceIndex, cmdList, aMsgId);
1095+
}
1096+
10521097
/// <summary>
10531098
/// List of rotation speeds and directions.
10541099
/// </summary>
@@ -1132,6 +1177,28 @@ public VectorSubcommand(uint aIndex, uint aDuration, double aPosition)
11321177
}
11331178
}
11341179

1180+
public static LinearCmd Create(uint aDeviceIndex, uint aMsgId, uint aDuration, double aPosition, uint aCmdCount)
1181+
{
1182+
return Create(aDeviceIndex, aMsgId, Enumerable.Repeat((aDuration, aPosition), (int)aCmdCount), aCmdCount);
1183+
}
1184+
1185+
public static LinearCmd Create(uint aDeviceIndex, uint aMsgId, IEnumerable<(uint duration, double position)> aCmds, uint aCmdCount)
1186+
{
1187+
if (aCmdCount != aCmds.Count())
1188+
{
1189+
throw new ArgumentException("Number of speeds and number of commands must match.");
1190+
}
1191+
var cmdList = new List<VectorSubcommand>((int)aCmdCount);
1192+
uint i = 0;
1193+
foreach (var cmd in aCmds)
1194+
{
1195+
cmdList.Add(new VectorSubcommand(i, cmd.duration, cmd.position));
1196+
++i;
1197+
}
1198+
1199+
return new LinearCmd(aDeviceIndex, cmdList, aMsgId);
1200+
}
1201+
11351202
/// <summary>
11361203
/// List of linear movement vectors.
11371204
/// </summary>

Buttplug.Server.Test/Bluetooth/Devices/FleshlightLaunchTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,7 @@ public void TestVectorCmd()
8484
[Test]
8585
public void TestInvalidVectorCmdTooManyFeatures()
8686
{
87-
var msg = new LinearCmd(4,
88-
new List<LinearCmd.VectorSubcommand>
89-
{
90-
new LinearCmd.VectorSubcommand(0, 500, 0.75),
91-
new LinearCmd.VectorSubcommand(1, 500, 0.75),
92-
});
87+
var msg = LinearCmd.Create(4, 0, 500, 0.75, 2);
9388
testUtil.TestInvalidDeviceMessage(msg);
9489
}
9590

@@ -107,10 +102,7 @@ public void TestInvalidVectorCmdWrongFeatures()
107102
[Test]
108103
public void TestInvalidVectorNotEnoughFeatures()
109104
{
110-
var msg = new LinearCmd(4,
111-
new List<LinearCmd.VectorSubcommand>
112-
{
113-
});
105+
var msg = LinearCmd.Create(4, 0, 500, 0.75, 0);
114106
testUtil.TestInvalidDeviceMessage(msg);
115107
}
116108
}

Buttplug.Server.Test/Bluetooth/Devices/KiirooGen2VibeTests.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public void TestSingleMotorVibrateCmd()
5353
}
5454
}
5555

56-
// TODO: Test invalid SingleMotorVibrateCmd?
57-
5856
[Test]
5957
public void TestVibrateCmd()
6058
{
@@ -91,30 +89,7 @@ public void TestInvalidVibrateCmd()
9189
{
9290
var testUtil = new BluetoothDeviceTestUtils<KiirooGen2VibeBluetoothInfo>();
9391
testUtil.SetupTest(item.Key);
94-
var features = new List<VibrateCmd.VibrateSubcommand>();
95-
testUtil.TestInvalidDeviceMessage(new VibrateCmd(4, features));
96-
97-
for (var i = 0u; i < item.Value.VibeCount + 1; ++i)
98-
{
99-
features.Add(new VibrateCmd.VibrateSubcommand(i, 0.5));
100-
}
101-
102-
testUtil.TestInvalidDeviceMessage(new VibrateCmd(4, features));
103-
}
104-
}
105-
106-
[Test]
107-
public void TestInvalidVibrateCmdWrongFeatures()
108-
{
109-
foreach (var item in KiirooGen2Vibe.DevInfos)
110-
{
111-
var testUtil = new BluetoothDeviceTestUtils<KiirooGen2VibeBluetoothInfo>();
112-
testUtil.SetupTest(item.Key);
113-
var features = new List<VibrateCmd.VibrateSubcommand>()
114-
{
115-
new VibrateCmd.VibrateSubcommand(0xffffffff, 0.5)
116-
};
117-
testUtil.TestInvalidDeviceMessage(new VibrateCmd(4, features));
92+
testUtil.TestInvalidVibrateCmd(item.Value.VibeCount);
11893
}
11994
}
12095
}

Buttplug.Server.Test/Bluetooth/Devices/KiirooTests.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,13 @@ public void TestVibrateCmd()
7878
(Encoding.ASCII.GetBytes("2,\n"), testUtil.NoCharacteristic),
7979
};
8080

81-
testUtil.TestDeviceMessage(
82-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
83-
{
84-
new VibrateCmd.VibrateSubcommand(0, 0.5),
85-
}), expected, false);
81+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 1), expected, false);
8682
}
8783

8884
[Test]
8985
public void TestInvalidVibrateCmd()
9086
{
91-
testUtil.TestInvalidDeviceMessage(
92-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
93-
{
94-
}));
95-
testUtil.TestInvalidDeviceMessage(
96-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
97-
{
98-
new VibrateCmd.VibrateSubcommand(0, 0.5),
99-
new VibrateCmd.VibrateSubcommand(1, 0.5),
100-
}));
101-
testUtil.TestInvalidDeviceMessage(
102-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
103-
{
104-
new VibrateCmd.VibrateSubcommand(0xffffffff, 0.5),
105-
}));
87+
testUtil.TestInvalidVibrateCmd(1);
10688
}
10789
}
10890
}

Buttplug.Server.Test/Bluetooth/Devices/LovenseTests.cs

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,13 @@ public void TestVibrateCmd()
8585
(Encoding.ASCII.GetBytes("Vibrate:10;"), testUtil.NoCharacteristic),
8686
};
8787

88-
testUtil.TestDeviceMessage(
89-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
90-
{
91-
new VibrateCmd.VibrateSubcommand(0, 0.5),
92-
}), expected, false);
88+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 1), expected, false);
9389
}
9490

9591
[Test]
9692
public void TestInvalidVibrateCmd()
9793
{
98-
testUtil.TestInvalidDeviceMessage(
99-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
100-
{
101-
}));
102-
testUtil.TestInvalidDeviceMessage(
103-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
104-
{
105-
new VibrateCmd.VibrateSubcommand(0, 0.5),
106-
new VibrateCmd.VibrateSubcommand(1, 0.5),
107-
}));
94+
testUtil.TestInvalidVibrateCmd(1);
10895
}
10996
}
11097

@@ -187,28 +174,13 @@ public void TestVibrateCmd()
187174
(Encoding.ASCII.GetBytes("Vibrate2:10;"), testUtil.NoCharacteristic),
188175
};
189176

190-
testUtil.TestDeviceMessage(
191-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
192-
{
193-
new VibrateCmd.VibrateSubcommand(0, 0.5),
194-
new VibrateCmd.VibrateSubcommand(1, 0.5),
195-
}), expected, false);
177+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 2), expected, false);
196178
}
197179

198180
[Test]
199181
public void TestInvalidVibrateCmd()
200182
{
201-
testUtil.TestInvalidDeviceMessage(
202-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
203-
{
204-
}));
205-
testUtil.TestInvalidDeviceMessage(
206-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
207-
{
208-
new VibrateCmd.VibrateSubcommand(0, 0.5),
209-
new VibrateCmd.VibrateSubcommand(1, 0.5),
210-
new VibrateCmd.VibrateSubcommand(2, 0.5),
211-
}));
183+
testUtil.TestInvalidVibrateCmd(2);
212184
}
213185
}
214186

@@ -256,11 +228,7 @@ public void TestStopDeviceCmd()
256228
(Encoding.ASCII.GetBytes("Rotate:10;"), testUtil.NoCharacteristic),
257229
};
258230

259-
testUtil.TestDeviceMessage(
260-
new RotateCmd(4, new List<RotateCmd.RotateSubcommand>()
261-
{
262-
new RotateCmd.RotateSubcommand(0, 0.5, true),
263-
}), expected, false);
231+
testUtil.TestDeviceMessage(RotateCmd.Create(4, 1, 0.5, true, 1), expected, false);
264232

265233
expected =
266234
new List<(byte[], uint)>()
@@ -280,38 +248,22 @@ public void TestRotateCmd()
280248
(Encoding.ASCII.GetBytes("Rotate:10;"), testUtil.NoCharacteristic),
281249
};
282250

283-
testUtil.TestDeviceMessage(
284-
new RotateCmd(4, new List<RotateCmd.RotateSubcommand>()
285-
{
286-
new RotateCmd.RotateSubcommand(0, 0.5, true),
287-
}), expected, false);
251+
testUtil.TestDeviceMessage(RotateCmd.Create(4, 1, 0.5, true, 1), expected, false);
288252

289253
expected =
290254
new List<(byte[], uint)>()
291255
{
292256
(Encoding.ASCII.GetBytes("RotateChange;"), testUtil.NoCharacteristic),
293257
};
294258

295-
testUtil.TestDeviceMessage(
296-
new RotateCmd(4, new List<RotateCmd.RotateSubcommand>()
297-
{
298-
new RotateCmd.RotateSubcommand(0, 0.5, false),
299-
}), expected, false);
259+
testUtil.TestDeviceMessage(RotateCmd.Create(4, 1, 0.5, false, 1), expected, false);
300260
}
301261

302262
[Test]
303263
public void TestInvalidVibrateCmd()
304264
{
305-
testUtil.TestInvalidDeviceMessage(
306-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
307-
{
308-
}));
309-
testUtil.TestInvalidDeviceMessage(
310-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
311-
{
312-
new VibrateCmd.VibrateSubcommand(0, 0.5),
313-
new VibrateCmd.VibrateSubcommand(1, 0.5),
314-
}));
265+
testUtil.TestInvalidDeviceMessage(RotateCmd.Create(4, 1, 0.5, true, 0));
266+
testUtil.TestInvalidDeviceMessage(RotateCmd.Create(4, 1, 0.5, true, 2));
315267
testUtil.TestInvalidDeviceMessage(
316268
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
317269
{

Buttplug.Server.Test/Bluetooth/Devices/MagicMotionTests.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,13 @@ public void TestVibrateCmd()
7777
(new byte[] { 0x0b, 0xff, 0x04, 0x0a, 0x32, 0x32, 0x00, 0x04, 0x08, 0x80, 0x64, 0x00 }, (uint)MagicMotionBluetoothInfo.Chrs.Tx),
7878
};
7979

80-
testUtil.TestDeviceMessage(
81-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
82-
{
83-
new VibrateCmd.VibrateSubcommand(0, 0.5),
84-
}), expected, false);
80+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 1), expected, false);
8581
}
8682

8783
[Test]
8884
public void TestInvalidVibrateCmd()
8985
{
90-
testUtil.TestInvalidDeviceMessage(
91-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
92-
{
93-
}));
94-
testUtil.TestInvalidDeviceMessage(
95-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
96-
{
97-
new VibrateCmd.VibrateSubcommand(0, 0.5),
98-
new VibrateCmd.VibrateSubcommand(1, 0.5),
99-
}));
100-
testUtil.TestInvalidDeviceMessage(
101-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
102-
{
103-
new VibrateCmd.VibrateSubcommand(0xffffffff, 0.5),
104-
}));
86+
testUtil.TestInvalidVibrateCmd(1);
10587
}
10688
}
10789
}

Buttplug.Server.Test/Bluetooth/Devices/VibratissimoTests.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,13 @@ public void TestVibrateCmd()
8484
(new byte[] { 0x80, 0x00 }, (uint)VibratissimoBluetoothInfo.Chrs.TxSpeed),
8585
};
8686

87-
testUtil.TestDeviceMessage(
88-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
89-
{
90-
new VibrateCmd.VibrateSubcommand(0, 0.5),
91-
}), expected, false);
87+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 1), expected, false);
9288
}
9389

9490
[Test]
9591
public void TestInvalidVibrateCmd()
9692
{
97-
testUtil.TestInvalidDeviceMessage(
98-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
99-
{
100-
}));
101-
testUtil.TestInvalidDeviceMessage(
102-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
103-
{
104-
new VibrateCmd.VibrateSubcommand(0, 0.5),
105-
new VibrateCmd.VibrateSubcommand(1, 0.5),
106-
}));
107-
testUtil.TestInvalidDeviceMessage(
108-
new VibrateCmd(4, new List<VibrateCmd.VibrateSubcommand>()
109-
{
110-
new VibrateCmd.VibrateSubcommand(0xffffffff, 0.5),
111-
}));
93+
testUtil.TestInvalidVibrateCmd(1);
11294
}
11395
}
11496
}

0 commit comments

Comments
 (0)