use super::Endpoint;
use getset::{Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug, Clone, Getters, MutGetters, Setters, Eq)]
#[getset(get = "pub", set = "pub", get_mut = "pub(crate)")]
pub struct BluetoothLEManufacturerData {
company: u16,
data: Option<Vec<u8>>,
}
impl BluetoothLEManufacturerData {
pub fn new(company: u16, data: &Option<Vec<u8>>) -> Self {
Self {
company,
data: data.clone(),
}
}
}
impl PartialEq for BluetoothLEManufacturerData {
fn eq(&self, other: &Self) -> bool {
if self.company != *other.company() {
return false;
}
if self.data.is_none() || other.data.is_none() {
return true;
}
let data = self.data().as_ref().expect("Already checked existence");
let other_data = other.data().as_ref().expect("Already checked existence");
if data.len() == other_data.len() {
if *data == *other_data {
return true;
}
return false;
}
let needle = if data.len() < other_data.len() {
data
} else {
other_data
};
let mut haystack = if data.len() > other_data.len() {
data.as_slice()
} else {
other_data.as_slice()
};
while !haystack.is_empty() {
if haystack.starts_with(needle) {
return true;
}
haystack = &haystack[1..];
}
false
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Getters, MutGetters, Setters)]
#[getset(get = "pub", set = "pub", get_mut = "pub(crate)")]
pub struct BluetoothLESpecifier {
names: HashSet<String>,
#[serde(default)]
manufacturer_data: Vec<BluetoothLEManufacturerData>,
#[serde(default)]
advertised_services: HashSet<Uuid>,
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;
}
wildcard.pop();
if compare_name.starts_with(&wildcard) {
return true;
}
}
}
if !self.manufacturer_data.is_empty() && !other.manufacturer_data.is_empty() {
for data in &self.manufacturer_data {
if other.manufacturer_data.contains(data) {
return true;
}
}
}
if self
.advertised_services
.intersection(&other.advertised_services)
.count()
> 0
{
return true;
}
false
}
}
impl BluetoothLESpecifier {
pub fn new(
names: HashSet<String>,
manufacturer_data: Vec<BluetoothLEManufacturerData>,
advertised_services: HashSet<Uuid>,
services: HashMap<Uuid, HashMap<Endpoint, Uuid>>,
) -> Self {
Self {
names,
manufacturer_data,
advertised_services,
services,
}
}
pub fn new_from_device(
name: &str,
manufacturer_data: &HashMap<u16, Vec<u8>>,
advertised_services: &[Uuid],
) -> BluetoothLESpecifier {
let mut name_set = HashSet::new();
name_set.insert(name.to_string());
let mut data_vec = vec![];
for (company, data) in manufacturer_data.iter() {
data_vec.push(BluetoothLEManufacturerData::new(
*company,
&Some(data.clone()),
));
}
let service_set = HashSet::from_iter(advertised_services.iter().copied());
BluetoothLESpecifier {
names: name_set,
manufacturer_data: data_vec,
advertised_services: service_set,
services: HashMap::new(),
}
}
pub fn merge(&mut self, other: BluetoothLESpecifier) {
self.names = self.names.union(&other.names).cloned().collect();
self.advertised_services = self
.advertised_services
.union(&other.advertised_services)
.cloned()
.collect();
self.services.extend(other.services);
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LovenseConnectServiceSpecifier {
#[allow(dead_code)]
exists: bool,
}
impl Default for LovenseConnectServiceSpecifier {
fn default() -> Self {
Self { exists: true }
}
}
impl PartialEq for LovenseConnectServiceSpecifier {
fn eq(&self, _other: &Self) -> bool {
true
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct XInputSpecifier {
#[allow(dead_code)]
exists: bool,
}
impl Default for XInputSpecifier {
fn default() -> Self {
Self { exists: true }
}
}
impl PartialEq for XInputSpecifier {
fn eq(&self, _other: &Self) -> bool {
true
}
}
#[derive(
Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, Getters, Setters, MutGetters,
)]
pub struct VIDPIDPair {
vendor_id: u16,
product_id: u16,
}
#[derive(Serialize, Deserialize, Debug, Eq, Clone, Getters, Setters, MutGetters)]
#[getset(get = "pub", set = "pub", get_mut = "pub(crate)")]
pub struct VIDPIDSpecifier {
pairs: Vec<VIDPIDPair>,
}
impl VIDPIDSpecifier {
pub fn new(vendor_id: u16, product_id: u16) -> Self {
Self {
pairs: vec![VIDPIDPair {
vendor_id,
product_id,
}],
}
}
}
impl PartialEq for VIDPIDSpecifier {
fn eq(&self, other: &Self) -> bool {
for pair in &self.pairs {
for other_pair in &other.pairs {
if *pair == *other_pair {
return true;
}
}
}
false
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, Getters, Setters, MutGetters)]
#[getset(get = "pub", set = "pub", get_mut = "pub(crate)")]
pub struct SerialSpecifier {
baud_rate: u32,
data_bits: u8,
stop_bits: u8,
parity: char,
port: String,
}
impl SerialSpecifier {
pub fn new(port: &str, baud_rate: u32, data_bits: u8, stop_bits: u8, parity: char) -> Self {
Self {
port: port.to_owned(),
baud_rate,
data_bits,
stop_bits,
parity,
}
}
pub fn new_from_name(port: &str) -> Self {
Self {
port: port.to_owned(),
..Default::default()
}
}
}
impl PartialEq for SerialSpecifier {
fn eq(&self, other: &Self) -> bool {
if *self.port == *other.port {
return true;
}
false
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, Getters, Setters, MutGetters)]
#[getset(get = "pub", set = "pub")]
pub struct WebsocketSpecifier {
name: String,
}
impl PartialEq for WebsocketSpecifier {
fn eq(&self, other: &Self) -> bool {
if self.name == other.name {
return true;
}
false
}
}
impl WebsocketSpecifier {
pub fn new(name: &str) -> WebsocketSpecifier {
Self {
name: name.to_owned(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ProtocolCommunicationSpecifier {
#[serde(rename = "btle")]
BluetoothLE(BluetoothLESpecifier),
#[serde(rename = "hid")]
HID(VIDPIDSpecifier),
#[serde(rename = "usb")]
USB(VIDPIDSpecifier),
#[serde(rename = "serial")]
Serial(SerialSpecifier),
#[serde(rename = "xinput")]
XInput(XInputSpecifier),
#[serde(rename = "lovense_connect_service")]
LovenseConnectService(LovenseConnectServiceSpecifier),
#[serde(rename = "websocket")]
Websocket(WebsocketSpecifier),
}
impl PartialEq for ProtocolCommunicationSpecifier {
fn eq(&self, other: &ProtocolCommunicationSpecifier) -> bool {
use ProtocolCommunicationSpecifier::*;
match (self, other) {
(USB(self_spec), USB(other_spec)) => self_spec == other_spec,
(Serial(self_spec), Serial(other_spec)) => self_spec == other_spec,
(BluetoothLE(self_spec), BluetoothLE(other_spec)) => self_spec == other_spec,
(HID(self_spec), HID(other_spec)) => self_spec == other_spec,
(XInput(self_spec), XInput(other_spec)) => self_spec == other_spec,
(Websocket(self_spec), Websocket(other_spec)) => self_spec == other_spec,
(LovenseConnectService(self_spec), LovenseConnectService(other_spec)) => {
self_spec == other_spec
}
_ => false,
}
}
}
impl Eq for ProtocolCommunicationSpecifier {
}