1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use super::{
ButtplugDeviceResultFuture,
ButtplugProtocol,
ButtplugProtocolCommandHandler,
ButtplugProtocolCreator,
};
use crate::{
core::errors::ButtplugDeviceError,
device::{
configuration_manager::DeviceProtocolConfiguration,
ButtplugDeviceEvent,
DeviceSubscribeCmd,
DeviceUnsubscribeCmd,
},
};
use crate::{
core::{
errors::ButtplugError,
messages::{self, ButtplugDeviceCommandMessageUnion, MessageAttributesMap},
},
device::{
protocol::{generic_command_manager::GenericCommandManager, ButtplugProtocolProperties},
DeviceImpl,
DeviceWriteCmd,
Endpoint,
},
};
use async_mutex::Mutex;
use futures::future::BoxFuture;
use futures::StreamExt;
use std::sync::Arc;
#[derive(ButtplugProtocol, ButtplugProtocolProperties)]
pub struct Lovense {
name: String,
message_attributes: MessageAttributesMap,
manager: Arc<Mutex<GenericCommandManager>>,
stop_commands: Vec<ButtplugDeviceCommandMessageUnion>,
}
impl Lovense {
pub(super) fn new(name: &str, message_attributes: MessageAttributesMap) -> Self {
let manager = GenericCommandManager::new(&message_attributes);
Self {
name: name.to_owned(),
message_attributes,
stop_commands: manager.get_stop_commands(),
manager: Arc::new(Mutex::new(manager)),
}
}
}
impl ButtplugProtocolCreator for Lovense {
fn new_protocol(name: &str, attrs: MessageAttributesMap) -> Box<dyn ButtplugProtocol> {
Box::new(Self::new(name, attrs))
}
fn try_create(
device_impl: &dyn DeviceImpl,
configuration: DeviceProtocolConfiguration,
) -> BoxFuture<'static, Result<Box<dyn ButtplugProtocol>, ButtplugError>> {
let subscribe_fut = device_impl.subscribe(DeviceSubscribeCmd::new(Endpoint::Rx));
let msg = DeviceWriteCmd::new(Endpoint::Tx, b"DeviceType;".to_vec(), false);
let info_fut = device_impl.write_value(msg);
let mut event_receiver = device_impl.get_event_receiver();
let unsubscribe_fut = device_impl.unsubscribe(DeviceUnsubscribeCmd::new(Endpoint::Rx));
Box::pin(async move {
let identifier;
subscribe_fut.await?;
info_fut.await?;
// TODO Put some sort of very quick timeout here, we should just fail if
// we don't get something back quickly.
match event_receiver.next().await {
Some(ButtplugDeviceEvent::Notification(_, n)) => {
let type_response = std::str::from_utf8(&n).unwrap().to_owned();
info!("Lovense Device Type Response: {}", type_response);
identifier = type_response.split(':').collect::<Vec<&str>>()[0].to_owned();
}
Some(ButtplugDeviceEvent::Removed) => {
return Err(
ButtplugDeviceError::ProtocolSpecificError(
"Lovense",
"Lovense Device disconnected while getting DeviceType info.",
)
.into(),
);
}
None => {
return Err(
ButtplugDeviceError::ProtocolSpecificError(
"Lovense",
"Did not get DeviceType return from Lovense device in time",
)
.into(),
);
}
};
unsubscribe_fut.await?;
let (names, attrs) = configuration.get_attributes(&identifier).unwrap();
let name = names.get("en-us").unwrap();
Ok(Self::new_protocol(name, attrs))
})
}
}
impl ButtplugProtocolCommandHandler for Lovense {
fn handle_vibrate_cmd(
&self,
device: Arc<Box<dyn DeviceImpl>>,
msg: messages::VibrateCmd,
) -> ButtplugDeviceResultFuture {
let manager = self.manager.clone();
Box::pin(async move {
// Store off result before the match, so we drop the lock ASAP.
let result = manager.lock().await.update_vibration(&msg, false)?;
// Lovense is the same situation as the Lovehoney Desire, where commands
// are different if we're addressing all motors or seperate motors.
// Difference here being that there's Lovense variants with different
// numbers of motors.
//
// Neat way of checking if everything is the same via
// https://sts10.github.io/2019/06/06/is-all-equal-function.html.
//
// Just make sure we're not matching on None, 'cause if that's the case
// we ain't got shit to do.
let mut fut_vec = vec![];
if let Some(cmds) = result {
if cmds[0].is_some() && (cmds.len() == 1 || cmds.windows(2).all(|w| w[0] == w[1])) {
let lovense_cmd = format!("Vibrate:{};", cmds[0].unwrap()).as_bytes().to_vec();
let fut = device.write_value(DeviceWriteCmd::new(Endpoint::Tx, lovense_cmd, false));
fut.await?;
return Ok(messages::Ok::default().into());
}
for (i, cmd) in cmds.iter().enumerate() {
if let Some(speed) = cmd {
let lovense_cmd = format!("Vibrate{}:{};", i + 1, speed).as_bytes().to_vec();
fut_vec.push(device.write_value(DeviceWriteCmd::new(Endpoint::Tx, lovense_cmd, false)));
}
}
}
for fut in fut_vec {
fut.await?;
}
Ok(messages::Ok::default().into())
})
}
// TODO Reimplement this for futures passing
/*
fn handle_rotate_cmd(
&self,
device: Arc<Box<dyn DeviceImpl>>,
msg: messages::RotateCmd,
) -> ButtplugServerResultFuture {
let result = self.manager.lock().await.update_rotation(msg);
match result {
Ok(cmds) => {
// Due to lovense devices having separate commands for rotation
// and speed, we can't completely depend on the generic command
// manager here.
//
// TODO Should the generic command manager maybe store the
// previous command as well as returning the next? That might
// save us having to store this in the protocol members, but I'm
// also not sure anyone but Lovense does this. For Vorze, we
// need speed and direction regardless because they form a
// single command.
if let Some((speed, clockwise)) = cmds[0] {
let mut lovense_cmds = vec![];
{
let mut last_rotation = self.last_rotation.lock().await;
if let Some((rot_speed, rot_dir)) = *last_rotation {
if rot_dir != clockwise {
lovense_cmds.push("RotateChange;".as_bytes().to_vec());
}
if rot_speed != speed {
lovense_cmds.push(format!("Rotate:{};", speed).as_bytes().to_vec());
}
}
*last_rotation = Some((speed, clockwise));
}
for cmd in lovense_cmds {
device
.write_value(DeviceWriteCmd::new(Endpoint::Tx, cmd, false))
.await?;
}
}
Ok(messages::Ok::default().into())
}
Err(e) => Err(e),
}
}
*/
}
// TODO Gonna need to add the ability to set subscribe data in tests before
// writing Lovense tests. Oops.