Skip to content

Commit 694f94b

Browse files
committed
chore: Bring LiBo code up to date
- Actually add LiBo code to VS projects - Change internal structure to match new BluetoothInfo format - Remove unused code - Add tests Affects #410, #411
1 parent bb87264 commit 694f94b

3 files changed

Lines changed: 112 additions & 88 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Buttplug.Core.Messages;
7+
using Buttplug.Server.Bluetooth.Devices;
8+
using Buttplug.Server.Test.Util;
9+
using JetBrains.Annotations;
10+
using NUnit.Framework;
11+
12+
namespace Buttplug.Server.Test.Bluetooth.Devices
13+
{
14+
internal class LiBoTest
15+
{
16+
[NotNull]
17+
private BluetoothDeviceTestUtils<LiBoBluetoothInfo> testUtil;
18+
19+
[SetUp]
20+
public void Init()
21+
{
22+
testUtil = new BluetoothDeviceTestUtils<LiBoBluetoothInfo>();
23+
testUtil.SetupTest("PiPiJing");
24+
}
25+
26+
[Test]
27+
public void TestAllowedMessages()
28+
{
29+
testUtil.TestDeviceAllowedMessages(new Dictionary<System.Type, uint>()
30+
{
31+
{ typeof(StopDeviceCmd), 0 },
32+
{ typeof(SingleMotorVibrateCmd), 0 },
33+
{ typeof(VibrateCmd), 1 },
34+
});
35+
}
36+
37+
// StopDeviceCmd noop test handled in GeneralDeviceTests
38+
39+
[Test]
40+
public void TestStopDeviceCmd()
41+
{
42+
var expected =
43+
new List<(byte[], uint)>()
44+
{
45+
(new byte[] { 2 }, (uint)LiBoBluetoothInfo.Chrs.WriteVibrate),
46+
};
47+
48+
testUtil.TestDeviceMessage(new SingleMotorVibrateCmd(4, 0.5), expected, false);
49+
50+
expected =
51+
new List<(byte[], uint)>()
52+
{
53+
(new byte[] { 0 }, (uint)LiBoBluetoothInfo.Chrs.WriteVibrate),
54+
};
55+
56+
testUtil.TestDeviceMessage(new StopDeviceCmd(4), expected, false);
57+
}
58+
59+
[Test]
60+
public void TestSingleMotorVibrateCmd()
61+
{
62+
var expected =
63+
new List<(byte[], uint)>()
64+
{
65+
(new byte[] { 2 }, (uint)LiBoBluetoothInfo.Chrs.WriteVibrate),
66+
};
67+
68+
testUtil.TestDeviceMessage(new SingleMotorVibrateCmd(4, 0.5), expected, false);
69+
}
70+
71+
[Test]
72+
public void TestVibrateCmd()
73+
{
74+
var expected =
75+
new List<(byte[], uint)>()
76+
{
77+
(new byte[] { 2 }, (uint)LiBoBluetoothInfo.Chrs.WriteVibrate),
78+
};
79+
80+
testUtil.TestDeviceMessage(VibrateCmd.Create(4, 1, 0.5, 1), expected, false);
81+
}
82+
83+
[Test]
84+
public void TestInvalidVibrateCmd()
85+
{
86+
testUtil.TestInvalidVibrateCmd(1);
87+
}
88+
}
89+
}

Buttplug.Server.Test/Buttplug.Server.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="Bluetooth\Devices\GeneralDeviceTests.cs" />
6666
<Compile Include="Bluetooth\Devices\KiirooGen2VibeTests.cs" />
6767
<Compile Include="Bluetooth\Devices\KiirooTests.cs" />
68+
<Compile Include="Bluetooth\Devices\LiBoTest.cs" />
6869
<Compile Include="Bluetooth\Devices\MysteryVibeTests.cs" />
6970
<Compile Include="Bluetooth\Devices\VorzeSATests.cs" />
7071
<Compile Include="ButtplugDeviceTests.cs" />

Buttplug.Server/Bluetooth/Devices/LiBo.cs

Lines changed: 22 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,37 @@ internal class LiBoBluetoothInfo : IBluetoothDeviceInfo
1111
{
1212
public enum Chrs : uint
1313
{
14-
Tx1 = 0,
15-
Tx2,
16-
Rx,
14+
WriteShock = 0,
15+
WriteVibrate,
16+
ReadBattery,
1717
}
1818

1919
public Guid[] Services { get; } =
2020
{
2121
new Guid("00006000-0000-1000-8000-00805f9b34fb"), // Write Service
22-
new Guid("00006050-0000-1000-8000-00805f9b34fb"), // Read service (battery)
2322

24-
// I'm pretty sure that the 2nd one here is ignored due to a call to Services.First() in the bluetooth manager
23+
// TODO Commenting out battery service until we can handle multiple services.
24+
25+
// new Guid("00006050-0000-1000-8000-00805f9b34fb"), // Read service (battery)
2526
};
2627

28+
public string[] NamePrefixes { get; } = { };
29+
2730
public string[] Names { get; } =
2831
{
2932
"PiPiJing"
3033
};
3134

32-
public Guid[] Characteristics { get; } =
35+
public Dictionary<uint, Guid> Characteristics { get; } = new Dictionary<uint, Guid>()
3336
{
3437
// tx1 characteristic
35-
new Guid("00006001-0000-1000-8000-00805f9b34fb"), // Shock
38+
{ (uint)Chrs.WriteShock, new Guid("00006001-0000-1000-8000-00805f9b34fb") }, // Shock
3639

3740
// tx2 characteristic
38-
new Guid("00006002-0000-1000-8000-00805f9b34fb"), // VibeMode
41+
{ (uint)Chrs.WriteVibrate, new Guid("00006002-0000-1000-8000-00805f9b34fb") }, // VibeMode
3942

4043
// rx characteristic
41-
new Guid("00006051-0000-1000-8000-00805f9b34fb"), // Read for battery level
44+
{ (uint)Chrs.ReadBattery, new Guid("00006051-0000-1000-8000-00805f9b34fb") }, // Read for battery level
4245
};
4346

4447
public IButtplugDevice CreateDevice(IButtplugLogManager aLogManager,
@@ -51,21 +54,20 @@ public IButtplugDevice CreateDevice(IButtplugLogManager aLogManager,
5154
internal class LiBo : ButtplugBluetoothDevice
5255
{
5356
private readonly uint _vibratorCount = 1;
54-
private readonly uint _shockerCount = 1;
5557
private readonly double[] _vibratorSpeed = { 0 };
56-
private readonly double[] _shockerIntensity = { 0 };
5758

5859
public LiBo(IButtplugLogManager aLogManager,
5960
IBluetoothDeviceInterface aInterface,
6061
IBluetoothDeviceInfo aInfo)
6162
: base(aLogManager,
62-
$"LiBo Device ({aInterface.Name})",
63+
$"LiBo ({aInterface.Name})",
6364
aInterface,
6465
aInfo)
6566
{
6667
MsgFuncs.Add(typeof(SingleMotorVibrateCmd), new ButtplugDeviceWrapper(HandleSingleMotorVibrateCmd));
6768
MsgFuncs.Add(typeof(VibrateCmd), new ButtplugDeviceWrapper(HandleVibrateCmd, new MessageAttributes() { FeatureCount = _vibratorCount }));
68-
// Here goes a handler for Estim shocking
69+
70+
// TODO Add a handler for Estim shocking, add a battery handler.
6971
MsgFuncs.Add(typeof(StopDeviceCmd), new ButtplugDeviceWrapper(HandleStopDeviceCmd));
7072
}
7173

@@ -81,13 +83,7 @@ private async Task<ButtplugMessage> HandleSingleMotorVibrateCmd(ButtplugDeviceMe
8183
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
8284
}
8385

84-
var subCmds = new List<VibrateCmd.VibrateSubcommand>();
85-
for (var i = 0u; i < _vibratorCount; i++)
86-
{
87-
subCmds.Add(new VibrateCmd.VibrateSubcommand(i, cmdMsg.Speed));
88-
}
89-
90-
return await HandleVibrateCmd(new VibrateCmd(cmdMsg.DeviceIndex, subCmds, cmdMsg.Id));
86+
return await HandleVibrateCmd(VibrateCmd.Create(cmdMsg.DeviceIndex, cmdMsg.Id, cmdMsg.Speed, _vibratorCount));
9187
}
9288

9389
private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
@@ -97,7 +93,7 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
9793
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
9894
}
9995

100-
if (cmdMsg.Speeds.Count < 1 || cmdMsg.Speeds.Count > _vibratorCount)
96+
if (cmdMsg.Speeds.Count != _vibratorCount)
10197
{
10298
return new Error(
10399
$"VibrateCmd requires between 1 and {_vibratorCount} vectors for this device.",
@@ -130,77 +126,15 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
130126
return new Ok(cmdMsg.Id);
131127
}
132128

133-
int mode = (int)Math.Ceiling(_vibratorSpeed[0] * 3); // Map a 0 - 100% value to a 0 - 3 value since 0 * x == 0 this will turn off the vibe if speed is 0.00
129+
// Map a 0 - 100% value to a 0 - 3 value since 0 * x == 0 this will turn off the vibe if
130+
// speed is 0.00
131+
int mode = (int)Math.Ceiling(_vibratorSpeed[0] * 3);
134132

135133
var data = new byte[] { Convert.ToByte(mode) };
136134

137135
return await Interface.WriteValue(aMsg.Id,
138-
Info.Characteristics[(uint)LiBoBluetoothInfo.Chrs.Tx2],
139-
data);
140-
}
141-
142-
private int getBatteryLevel()
143-
{
144-
throw new NotImplementedException("While most of this method is done it still needs to be adjusted/completed - see the comments");
145-
int batteryLevel = Convert.ToInt32(0x64 /* 100 in hex */); // Read from Info.Services[1] + Info.Characteristics[uint)LiBoBluetoothInfo.Chrs.Rx] here
146-
return batteryLevel; // As far as I can tell we currently have no way of reading values
147-
}
148-
149-
private async Task<ButtplugMessage> HandleShockCmd(ButtplugDeviceMessage aMsg)
150-
{
151-
throw new NotImplementedException("While most of this method is done it still needs to be adjusted/completed - see the comments");
152-
if (!(aMsg is VibrateCmd cmdMsg)) // ShockCmd once it gets implemented
153-
{
154-
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
155-
}
156-
157-
if (cmdMsg.Speeds.Count < 1 || cmdMsg.Speeds.Count > _shockerCount)
158-
{
159-
return new Error(
160-
$"ShockCmd requires between 1 and {_shockerCount} vectors for this device.", // Fix cmd name once settled
161-
Error.ErrorClass.ERROR_DEVICE,
162-
cmdMsg.Id);
163-
}
164-
165-
var changed = false;
166-
foreach (var v in cmdMsg.Speeds)
167-
{
168-
if (v.Index >= _shockerCount)
169-
{
170-
return new Error(
171-
$"Index {v.Index} is out of bounds for VibrateCmd for this device.", // Fix cmd name once settled
172-
Error.ErrorClass.ERROR_DEVICE,
173-
cmdMsg.Id);
174-
}
175-
176-
if (!(Math.Abs(v.Speed - _shockerIntensity[v.Index]) > 0.001))
177-
{
178-
continue;
179-
}
180-
181-
changed = true;
182-
_shockerIntensity[v.Index] = v.Speed;
183-
}
184-
185-
if (!changed)
186-
{
187-
return new Ok(cmdMsg.Id);
188-
}
189-
190-
int speed = 0;
191-
192-
// If intensity is set to 0 skip this with 0x00 and turn the shocker off
193-
if (_shockerIntensity[0] > 0)
194-
{
195-
speed = (int)Math.Floor(_shockerIntensity[0] * 7) << 4; // Map a 0 - 100% value to a 0 - 6 value shift 4 to have 0xY0 where Y is speed
196-
speed++; // add 1 to speed to get 0xY1 where Y is speed and 1 is mode 1 (constant shock)
197-
}
198-
199-
var data = new byte[] { Convert.ToByte(speed) };
200-
201-
return await Interface.WriteValue(aMsg.Id,
202-
Info.Characteristics[(uint)LiBoBluetoothInfo.Chrs.Tx1],
136+
(uint)LiBoBluetoothInfo.Chrs.WriteVibrate,
203137
data);
204138
}
205139
}
206-
}
140+
}

0 commit comments

Comments
 (0)