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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::{
    core::{
        errors::ButtplugError,
        messages::{
            self, ButtplugDeviceCommandMessageUnion, ButtplugOutMessage, MessageAttributesMap,
            RawReadCmd, RawReading, RawWriteCmd, SubscribeCmd, UnsubscribeCmd,
        },
    },
    device::{
        configuration_manager::{DeviceConfigurationManager, DeviceSpecifier, ProtocolDefinition},
        protocol::ButtplugProtocol,
        Endpoint,
    },
};
use async_trait::async_trait;
use broadcaster::BroadcastChannel;
use futures_channel;

pub type BoundedDeviceEventBroadcaster = BroadcastChannel<
    ButtplugDeviceEvent,
    futures_channel::mpsc::Sender<ButtplugDeviceEvent>,
    futures_channel::mpsc::Receiver<ButtplugDeviceEvent>,
>;

#[derive(PartialEq, Debug)]
pub struct DeviceReadCmd {
    pub endpoint: Endpoint,
    pub length: u32,
    pub timeout_ms: u32,
}

impl DeviceReadCmd {
    pub fn new(endpoint: Endpoint, length: u32, timeout_ms: u32) -> Self {
        Self {
            endpoint,
            length,
            timeout_ms,
        }
    }
}

impl From<RawReadCmd> for DeviceReadCmd {
    fn from(msg: RawReadCmd) -> Self {
        Self {
            endpoint: msg.endpoint,
            length: msg.expected_length,
            timeout_ms: msg.timeout,
        }
    }
}

#[derive(PartialEq, Debug)]
pub struct DeviceWriteCmd {
    pub endpoint: Endpoint,
    pub data: Vec<u8>,
    pub write_with_response: bool,
}

impl DeviceWriteCmd {
    pub fn new(endpoint: Endpoint, data: Vec<u8>, write_with_response: bool) -> Self {
        Self {
            endpoint,
            data,
            write_with_response,
        }
    }
}

impl From<RawWriteCmd> for DeviceWriteCmd {
    fn from(msg: RawWriteCmd) -> Self {
        Self {
            endpoint: msg.endpoint,
            data: msg.data,
            write_with_response: msg.write_with_response,
        }
    }
}

#[derive(PartialEq, Debug)]
pub struct DeviceSubscribeCmd {
    pub endpoint: Endpoint,
}

impl DeviceSubscribeCmd {
    pub fn new(endpoint: Endpoint) -> Self {
        Self { endpoint }
    }
}

impl From<SubscribeCmd> for DeviceSubscribeCmd {
    fn from(msg: SubscribeCmd) -> Self {
        Self {
            endpoint: msg.endpoint,
        }
    }
}

#[derive(PartialEq, Debug)]
pub struct DeviceUnsubscribeCmd {
    pub endpoint: Endpoint,
}

impl DeviceUnsubscribeCmd {
    pub fn new(endpoint: Endpoint) -> Self {
        Self { endpoint }
    }
}

impl From<UnsubscribeCmd> for DeviceUnsubscribeCmd {
    fn from(msg: UnsubscribeCmd) -> Self {
        Self {
            endpoint: msg.endpoint,
        }
    }
}

#[derive(PartialEq, Debug)]
pub enum DeviceImplCommand {
    // Endpoint, data, write with response
    Write(DeviceWriteCmd),
    // Endpoint, length, timeout in ms
    Read(DeviceReadCmd),
    Subscribe(DeviceSubscribeCmd),
    Unsubscribe(DeviceUnsubscribeCmd),
}

impl From<RawReadCmd> for DeviceImplCommand {
    fn from(msg: RawReadCmd) -> Self {
        DeviceImplCommand::Read(msg.into())
    }
}

impl From<RawWriteCmd> for DeviceImplCommand {
    fn from(msg: RawWriteCmd) -> Self {
        DeviceImplCommand::Write(msg.into())
    }
}

impl From<SubscribeCmd> for DeviceImplCommand {
    fn from(msg: SubscribeCmd) -> Self {
        DeviceImplCommand::Subscribe(msg.into())
    }
}

impl From<UnsubscribeCmd> for DeviceImplCommand {
    fn from(msg: UnsubscribeCmd) -> Self {
        DeviceImplCommand::Unsubscribe(msg.into())
    }
}

impl From<DeviceReadCmd> for DeviceImplCommand {
    fn from(msg: DeviceReadCmd) -> Self {
        DeviceImplCommand::Read(msg)
    }
}

impl From<DeviceWriteCmd> for DeviceImplCommand {
    fn from(msg: DeviceWriteCmd) -> Self {
        DeviceImplCommand::Write(msg)
    }
}

impl From<DeviceSubscribeCmd> for DeviceImplCommand {
    fn from(msg: DeviceSubscribeCmd) -> Self {
        DeviceImplCommand::Subscribe(msg)
    }
}

impl From<DeviceUnsubscribeCmd> for DeviceImplCommand {
    fn from(msg: DeviceUnsubscribeCmd) -> Self {
        DeviceImplCommand::Unsubscribe(msg)
    }
}

pub struct ButtplugDeviceImplInfo {
    pub endpoints: Vec<Endpoint>,
    pub manufacturer_name: Option<String>,
    pub product_name: Option<String>,
    pub serial_number: Option<String>,
}

pub enum ButtplugDeviceCommand {
    Connect,
    Message(DeviceImplCommand),
    Disconnect,
}

pub enum ButtplugDeviceReturn {
    Connected(ButtplugDeviceImplInfo),
    Ok(messages::Ok),
    RawReading(messages::RawReading),
    Error(ButtplugError),
}

#[derive(Debug, Clone)]
pub enum ButtplugDeviceEvent {
    Notification(Endpoint, Vec<u8>),
    Removed,
}

#[async_trait]
pub trait DeviceImpl: Sync + Send {
    fn name(&self) -> &str;
    fn address(&self) -> &str;
    fn connected(&self) -> bool;
    fn endpoints(&self) -> Vec<Endpoint>;
    async fn disconnect(&self);
    fn box_clone(&self) -> Box<dyn DeviceImpl>;
    fn get_event_receiver(&self) -> BoundedDeviceEventBroadcaster;

    async fn read_value(&self, msg: DeviceReadCmd) -> Result<RawReading, ButtplugError>;
    async fn write_value(&self, msg: DeviceWriteCmd) -> Result<(), ButtplugError>;
    async fn subscribe(&self, msg: DeviceSubscribeCmd) -> Result<(), ButtplugError>;
    async fn unsubscribe(&self, msg: DeviceUnsubscribeCmd) -> Result<(), ButtplugError>;
}

impl Clone for Box<dyn DeviceImpl> {
    fn clone(&self) -> Box<dyn DeviceImpl> {
        self.box_clone()
    }
}

#[async_trait]
pub trait ButtplugDeviceImplCreator: Sync + Send {
    fn get_specifier(&self) -> DeviceSpecifier;
    async fn try_create_device_impl(
        &mut self,
        protocol: ProtocolDefinition,
    ) -> Result<Box<dyn DeviceImpl>, ButtplugError>;
}

#[derive(Clone)]
pub struct ButtplugDevice {
    protocol: Box<dyn ButtplugProtocol>,
    device: Box<dyn DeviceImpl>,
}

impl ButtplugDevice {
    pub fn new(protocol: Box<dyn ButtplugProtocol>, device: Box<dyn DeviceImpl>) -> Self {
        Self { protocol, device }
    }

    pub async fn try_create_device(
        mut device_creator: Box<dyn ButtplugDeviceImplCreator>,
    ) -> Result<Option<ButtplugDevice>, ButtplugError> {
        let device_mgr = DeviceConfigurationManager::new();
        // First off, we need to see if we even have a configuration available
        // for the device we're trying to create. If we don't, return Ok(None),
        // because this isn't actually an error. However, if we *do* have a
        // configuration but something goes wrong after this, then it's an
        // error.

        match device_mgr.find_configuration(&device_creator.get_specifier()) {
            Some((config_name, config)) => {
                // Now that we have both a possible device implementation and a
                // configuration for that device, try to initialize the implementation.
                // This usually means trying to connect to whatever the device is,
                // finding endpoints, etc.
                if let Some(proto_creator) = device_mgr.get_protocol_creator(&config_name) {
                    match device_creator.try_create_device_impl(config).await {
                        Ok(device_impl) => {
                            info!("Found Buttplug Device {}", device_impl.name());
                            // If we've made it this far, we now have a connected device
                            // implementation with endpoints set up. We now need to run whatever
                            // protocol initialization might need to happen. We'll fetch a protocol
                            // creator, pass the device implementation to it, then let it do
                            // whatever it needs. For most protocols, this is a no-op. However, for
                            // devices like Lovense, some Kiiroo, etc, this can get fairly
                            // complicated.
                            match proto_creator.try_create_protocol(&device_impl).await {
                                Ok(protocol_impl) => {
                                    Ok(Some(ButtplugDevice::new(protocol_impl, device_impl)))
                                }
                                Err(e) => Err(e),
                            }
                        }
                        Err(e) => Err(e),
                    }
                } else {
                    Ok(None)
                }
            }
            None => return Ok(None),
        }
    }

    pub fn name(&self) -> &str {
        self.protocol.name()
    }

    pub fn message_attributes(&self) -> MessageAttributesMap {
        self.protocol.message_attributes()
    }

    pub async fn parse_message(
        &mut self,
        message: &ButtplugDeviceCommandMessageUnion,
    ) -> Result<ButtplugOutMessage, ButtplugError> {
        self.protocol.parse_message(&self.device, message).await
    }

    pub fn get_event_receiver(&self) -> BoundedDeviceEventBroadcaster {
        self.device.get_event_receiver()
    }
    // TODO Handle raw messages here.
}