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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Buttplug Rust Source Code File - See https://buttplug.io for more info.

//

// Copyright 2016-2019 Nonpolynomial Labs LLC. All rights reserved.

//

// Licensed under the BSD 3-Clause license. See LICENSE file in the project root

// for full license information.


//! Device specific identification and protocol implementations.


use crate::{
  core::{
    errors::{ButtplugDeviceError, ButtplugError},
    messages::MessageAttributesMap,
  },
  device::Endpoint,
  util::json::JSONValidator,
};
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::mem;
use std::sync::{Arc, RwLock};
use uuid::Uuid;

static DEVICE_CONFIGURATION_JSON: &str =
  include_str!("../../dependencies/buttplug-device-config/buttplug-device-config.json");
static DEVICE_CONFIGURATION_JSON_SCHEMA: &str =
  include_str!("../../dependencies/buttplug-device-config/buttplug-device-config-schema.json");
static USER_DEVICE_CONFIGURATION_JSON_SCHEMA: &str =
  include_str!("../../dependencies/buttplug-device-config/buttplug-user-device-config-schema.json");
static DEVICE_EXTERNAL_CONFIGURATION_JSON: Lazy<Arc<RwLock<Option<String>>>> =
  Lazy::new(|| Arc::new(RwLock::new(None)));
static DEVICE_USER_CONFIGURATION_JSON: Lazy<Arc<RwLock<Option<String>>>> =
  Lazy::new(|| Arc::new(RwLock::new(None)));

pub fn set_external_device_config(config: Option<String>) {
  let mut c = DEVICE_EXTERNAL_CONFIGURATION_JSON.write().unwrap();
  *c = config;
}

pub fn set_user_device_config(config: Option<String>) {
  let mut c = DEVICE_USER_CONFIGURATION_JSON.write().unwrap();
  *c = config;
}

#[allow(dead_code)]
fn clear_user_device_config() {
  let mut c = DEVICE_USER_CONFIGURATION_JSON.write().unwrap();
  *c = None;
}

// Note: There's a ton of extra structs in here just to deserialize the json

// file. Just leave them and build extras (for instance,

// DeviceProtocolConfiguration) if needed elsewhere in the codebase. It's not

// gonna hurt anything and making a ton of serde attributes is just going to get

// confusing (see the messages impl).


#[derive(Deserialize, Debug, Clone)]
pub struct BluetoothLESpecifier {
  pub names: HashSet<String>,
  pub services: HashMap<Uuid, HashMap<Endpoint, Uuid>>,
}

impl PartialEq for BluetoothLESpecifier {
  fn eq(&self, other: &Self) -> bool {
    if self.names.intersection(&other.names).count() > 0 {
      return true;
    }
    for name in &self.names {
      for other_name in &other.names {
        let compare_name: &String;
        let mut wildcard: String;
        if name.ends_with('*') {
          wildcard = name.clone();
          compare_name = &other_name;
        } else if other_name.ends_with('*') {
          wildcard = other_name.clone();
          compare_name = &name;
        } else {
          continue;
        }
        // Remove asterisk from the end of the wildcard

        wildcard.pop();
        if compare_name.starts_with(&wildcard) {
          return true;
        }
      }
    }
    false
  }
}

impl BluetoothLESpecifier {
  pub fn new_from_device(name: &str) -> BluetoothLESpecifier {
    let mut set = HashSet::new();
    set.insert(name.to_string());
    BluetoothLESpecifier {
      names: set,
      services: HashMap::new(),
    }
  }
}

#[derive(Deserialize, Debug, Clone, Copy)]
pub struct XInputSpecifier {
  exists: bool,
}

impl Default for XInputSpecifier {
  fn default() -> Self {
    Self { exists: true }
  }
}

impl PartialEq for XInputSpecifier {
  fn eq(&self, _other: &Self) -> bool {
    true
  }
}

#[derive(Deserialize, Debug, PartialEq, Clone, Copy)]
pub struct HIDSpecifier {
  #[serde(rename = "vendor-id")]
  vendor_id: u16,
  #[serde(rename = "product-id")]
  product_id: u16,
}

#[derive(Deserialize, Debug, Clone, Default)]
pub struct SerialSpecifier {
  #[serde(rename = "baud-rate")]
  pub baud_rate: u32,
  #[serde(rename = "data-bits")]
  pub data_bits: u8,
  #[serde(rename = "stop-bits")]
  pub stop_bits: u8,
  pub parity: char,
  pub port: String,
}

impl SerialSpecifier {
  pub fn new_from_name(port: &str) -> Self {
    let mut specifier = Self::default();
    specifier.port = port.to_owned();
    specifier
  }
}

impl PartialEq for SerialSpecifier {
  fn eq(&self, other: &Self) -> bool {
    self.port == other.port
  }
}

#[derive(Deserialize, Debug, PartialEq, Clone, Copy)]
pub struct USBSpecifier {
  #[serde(rename = "vendor-id")]
  vendor_id: u16,
  #[serde(rename = "product-id")]
  product_id: u16,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub enum DeviceSpecifier {
  BluetoothLE(BluetoothLESpecifier),
  HID(HIDSpecifier),
  USB(USBSpecifier),
  Serial(SerialSpecifier),
  XInput(XInputSpecifier),
}

#[derive(Deserialize, Debug, Clone)]
pub struct ProtocolAttributes {
  identifier: Option<Vec<String>>,
  name: Option<HashMap<String, String>>,
  messages: Option<MessageAttributesMap>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct ProtocolDefinition {
  // Can't get serde flatten specifiers into a String/DeviceSpecifier map, so

  // they're kept separate here, and we return them in get_specifiers(). Feels

  // very clumsy, but we really don't do this a bunch during a session.

  pub usb: Option<Vec<USBSpecifier>>,
  pub btle: Option<BluetoothLESpecifier>,
  pub serial: Option<Vec<SerialSpecifier>>,
  pub hid: Option<Vec<HIDSpecifier>>,
  pub xinput: Option<XInputSpecifier>,
  pub defaults: Option<ProtocolAttributes>,
  pub configurations: Vec<ProtocolAttributes>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct UserProtocolDefinition {
  // Right now, we only allow users to specify serial ports through this

  // interface. It will contain more additions in the future.

  pub serial: Option<Vec<SerialSpecifier>>,
}

fn option_some_eq<T>(a: &Option<T>, b: &T) -> bool
where
  T: PartialEq,
{
  a.as_ref().map_or(false, |x| x == b)
}

fn option_some_eq_vec<T>(a_opt: &Option<Vec<T>>, b: &T) -> bool
where
  T: PartialEq,
{
  a_opt.as_ref().map_or(false, |a_vec| a_vec.contains(b))
}

impl PartialEq<DeviceSpecifier> for ProtocolDefinition {
  fn eq(&self, other: &DeviceSpecifier) -> bool {
    // TODO This seems like a really gross way to do this?

    match other {
      DeviceSpecifier::USB(other_usb) => option_some_eq_vec(&self.usb, other_usb),
      DeviceSpecifier::Serial(other_serial) => option_some_eq_vec(&self.serial, other_serial),
      DeviceSpecifier::BluetoothLE(other_btle) => option_some_eq(&self.btle, other_btle),
      DeviceSpecifier::HID(other_hid) => option_some_eq_vec(&self.hid, other_hid),
      DeviceSpecifier::XInput(other_xinput) => option_some_eq(&self.xinput, other_xinput),
    }
  }
}

#[derive(Deserialize, Debug)]
pub struct ProtocolConfiguration {
  pub(self) protocols: HashMap<String, ProtocolDefinition>,
}

#[derive(Deserialize, Debug)]
pub struct UserProtocolConfiguration {
  pub protocols: HashMap<String, UserProtocolDefinition>,
}

impl ProtocolConfiguration {
  pub fn merge_user_config(&mut self, other: UserProtocolConfiguration) {
    // For now, we're only merging serial info in.

    for (protocol, conf) in other.protocols {
      if self.protocols.contains_key(&protocol) {
        let our_serial_conf_option = &mut self.protocols.get_mut(&protocol).unwrap().serial;
        let mut other_serial_conf = conf.serial;
        if let Some(ref mut our_serial_config) = our_serial_conf_option {
          our_serial_config.extend(other_serial_conf.unwrap());
        } else {
          mem::swap(our_serial_conf_option, &mut other_serial_conf);
        }
      }
    }
  }
}

#[derive(Clone, Debug)]
pub struct DeviceProtocolConfiguration {
  defaults: Option<ProtocolAttributes>,
  configurations: Vec<ProtocolAttributes>,
}

impl DeviceProtocolConfiguration {
  pub fn new(
    defaults: Option<ProtocolAttributes>,
    configurations: Vec<ProtocolAttributes>,
  ) -> Self {
    Self {
      defaults,
      configurations,
    }
  }

  pub fn get_attributes(
    &self,
    identifier: &str,
  ) -> Result<(HashMap<String, String>, MessageAttributesMap), ButtplugError> {
    let mut attributes = MessageAttributesMap::new();
    // If we find defaults, set those up first.

    if let Some(ref attrs) = self.defaults {
      if let Some(ref msg_attrs) = attrs.messages {
        attributes = msg_attrs.clone();
      }
    }
    match self.configurations.iter().find(|attrs| {
      attrs
        .identifier
        .as_ref()
        .unwrap()
        .contains(&identifier.to_owned())
    }) {
      Some(ref attrs) => {
        if let Some(ref msg_attrs) = attrs.messages {
          attributes.extend(msg_attrs.clone());
        }
        Ok((attrs.name.as_ref().unwrap().clone(), attributes))
      }
      None => Err(
        ButtplugDeviceError::ProtocolAttributesNotFound(format!(
          "Cannot find identifier {} in protocol.",
          identifier
        ))
        .into(),
      ),
    }
  }
}

pub struct DeviceConfigurationManager {
  pub(self) config: ProtocolConfiguration,
}

unsafe impl Send for DeviceConfigurationManager {
}
unsafe impl Sync for DeviceConfigurationManager {
}

impl Default for DeviceConfigurationManager {
  fn default() -> Self {
    let external_config_guard = DEVICE_EXTERNAL_CONFIGURATION_JSON.clone();
    let external_config = external_config_guard.read().unwrap();
    let mut config: ProtocolConfiguration;
    // TODO We should already load the JSON into the file statics, and just

    // clone it out of our statics as needed.

    let config_validator = JSONValidator::new(DEVICE_CONFIGURATION_JSON_SCHEMA);

    if let Some(ref cfg) = *external_config {
      match config_validator.validate(&cfg) {
        Ok(_) => config = serde_json::from_str(&cfg).unwrap(),
        Err(e) => panic!(
          "Built-in configuration schema is invalid! Aborting! {:?}",
          e
        ),
      }
    } else {
      match config_validator.validate(DEVICE_CONFIGURATION_JSON) {
        Ok(_) => config = serde_json::from_str(DEVICE_CONFIGURATION_JSON).unwrap(),
        Err(e) => panic!(
          "Built-in configuration schema is invalid! Aborting! {:?}",
          e
        ),
      }
    }

    let user_validator = JSONValidator::new(USER_DEVICE_CONFIGURATION_JSON_SCHEMA);
    let user_config_guard = DEVICE_USER_CONFIGURATION_JSON.clone();
    let user_config_str = user_config_guard.read().unwrap();
    if let Some(ref user_cfg) = *user_config_str {
      match user_validator.validate(&user_cfg.to_string()) {
        Ok(_) => config.merge_user_config(serde_json::from_str(&user_cfg.to_string()).unwrap()),
        Err(e) => panic!("User configuration schema is invalid! Aborting! {:?}", e),
      }
    }

    DeviceConfigurationManager { config }
  }
}

impl DeviceConfigurationManager {
  pub fn find_configuration(
    &self,
    specifier: &DeviceSpecifier,
  ) -> Option<(String, ProtocolDefinition)> {
    info!("Looking for protocol that matches spec: {:?}", specifier);
    for (name, def) in self.config.protocols.iter() {
      if def == specifier {
        debug!("Found protocol for spec!");
        return Some((name.clone(), def.clone()));
      }
    }
    info!("No protocol found for spec!");
    None
  }

  pub fn get_protocol_config(&self, name: &str) -> Option<DeviceProtocolConfiguration> {
    info!("Looking for protocol {}", name);
    // TODO It feels like maybe there should be a cleaner way to do this,

    // but I'm not really sure what it is?

    if let Some(proto) = self.config.protocols.get(name) {
      info!("Found a protocol definition for {}", name);
      Some(DeviceProtocolConfiguration::new(
        proto.defaults.clone(),
        proto.configurations.clone(),
      ))
    } else {
      debug!("No matching protocol definition found.");
      None
    }
  }
}

#[cfg(test)]
mod test {
  use super::{
    clear_user_device_config,
    set_user_device_config,
    BluetoothLESpecifier,
    DeviceConfigurationManager,
    DeviceProtocolConfiguration,
    DeviceSpecifier,
  };
  use crate::core::messages::ButtplugDeviceMessageType;

  #[test]
  fn test_load_config() {
    let config = DeviceConfigurationManager::default();
    debug!("{:?}", config.config);
  }

  #[test]
  fn test_config_equals() {
    let config = DeviceConfigurationManager::default();
    let launch = DeviceSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device("Launch"));
    assert!(config.find_configuration(&launch).is_some());
  }

  #[test]
  fn test_config_wildcard_equals() {
    let config = DeviceConfigurationManager::default();
    let lovense =
      DeviceSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device("LVS-Whatever"));
    assert!(config.find_configuration(&lovense).is_some());
  }

  #[test]
  fn test_specific_device_config_creation() {
    let config = DeviceConfigurationManager::default();
    let lovense =
      DeviceSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device("LVS-Whatever"));
    let proto = config.find_configuration(&lovense).unwrap();
    let proto_config =
      DeviceProtocolConfiguration::new(proto.1.defaults.clone(), proto.1.configurations);
    let (name_map, message_map) = proto_config.get_attributes("P").unwrap();
    // Make sure we got the right name

    assert_eq!(name_map.get("en-us").unwrap(), "Lovense Edge");
    // Make sure we overwrote the default of 1

    assert_eq!(
      message_map
        .get(&ButtplugDeviceMessageType::VibrateCmd)
        .unwrap()
        .feature_count
        .unwrap(),
      2
    );
  }

  #[test]
  fn test_user_config_loading() {
    let mut config = DeviceConfigurationManager::default();
    assert!(config.config.protocols.contains_key("erostek-et312"));
    assert!(config
      .config
      .protocols
      .get("erostek-et312")
      .unwrap()
      .serial
      .as_ref()
      .is_some());
    assert_eq!(
      config
        .config
        .protocols
        .get("erostek-et312")
        .unwrap()
        .serial
        .as_ref()
        .unwrap()
        .len(),
      1
    );
    set_user_device_config(Some(
      r#"
        { 
            "protocols": {
                "erostek-et312": {
                    "serial": [
                        {
                            "port": "COM1",
                            "baud-rate": 19200,
                            "data-bits": 8,
                            "parity": "N",
                            "stop-bits": 1
                        }
                    ]
                }
            }
        }
        "#
      .to_string(),
    ));
    config = DeviceConfigurationManager::default();
    assert!(config.config.protocols.contains_key("erostek-et312"));
    assert!(config
      .config
      .protocols
      .get("erostek-et312")
      .unwrap()
      .serial
      .as_ref()
      .is_some());
    assert_eq!(
      config
        .config
        .protocols
        .get("erostek-et312")
        .unwrap()
        .serial
        .as_ref()
        .unwrap()
        .len(),
      2
    );
    assert!(config
      .config
      .protocols
      .get("erostek-et312")
      .unwrap()
      .serial
      .as_ref()
      .unwrap()
      .iter()
      .any(|x| x.port == "COM1"));
    clear_user_device_config();
  }
}