Buttplug sex toy control library
1// Buttplug Rust Source Code File - See https://buttplug.io for more info.
2//
3// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved.
4//
5// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
6// for full license information.
7
8//! Device configuration, connection, and communication
9//!
10//! Welcome to the guts of Buttplug.
11//!
12//! Structs in the device module are used by the [Buttplug Server](crate::server) (specifically the
13//! [Device Manager](crate::server::device_manager::DeviceManager)) to identify devices that
14//! Buttplug can connect to, and match them to supported protocols in order to establish
15//! communication, translate ButtplugMessages to raw hardware commands, and send those commands to
16//! the hardware.
17//!
18//! # What even is a device in Buttplug?
19//!
20//! Devices in buttplug consist of two components:
21//!
22//! - Implementations (represented by [Hardware]), which handle the actual communication with
23//! hardware. Implementations are created by a [DeviceCommunicationManager], which handles the
24//! discovery method for that type of hardware (Bluetooth scanning, USB bus scanning, listening on
25//! network ports, etc...)
26//! - Protocols (represented by [ButtplugProtocol]), which hold information about the capabilities
27//! of a device (can it vibrate/rotate/etc, at what speeds, so on and so forth), and translate
28//! from [Buttplug Device Messages](buttplug_core::messages::ButtplugDeviceMessage) into strings or
29//! binary arrays to send to devices via their implementation.
30//!
31//! # Device Lifetimes in Buttplug
32//!
33//! Creation and handling of devices happens in stages in Buttplug: configuring what the library can
34//! support, creating a device once a usable one is found, commanding connected devices, and
35//! disconnection/reconnection. This is process makes up most of the reason the library exists, so
36//! we'll cover it at a high level here.
37//!
38//! ## Configuration and Bringup
39//!
40//! Configuration of the device creation system happens when we bring up a
41//! [ButtplugServer](crate::server::ButtplugServer) and configure the [DeviceManager] that is owns.
42//! Information that needs to be added for device creation includes:
43//!
44//! - Protocols that the library implements, or that developers add themselves.
45//! - Device configurations related to those protocols, so we can identify and connect to devices
46//! that are compatible with them.
47//! - Lists of device addresses that we will either never connect to or only connect to.
48//!
49//! This information is entered via the public [DeviceManager] API, and stored between the
50//! [DeviceManager] and the [DeviceConfigurationManager] (which is owned by the [DeviceManager]).
51//!
52//! After all of the information is added, the [DeviceManager] is considered ready to discover
53//! devices.
54//!
55//! ## Device Discovery and Creation
56//!
57//! To create a device, we go through the following steps:
58//!
59//! - When the server receives a StartScanning message, all comm managers start looking for devices.
60//! Strategies for scanning can vary between [DeviceCommunicationManager]s, either using long term
61//! scans (bluetooth) or repeated timed scans (USB, HID, XInput, etc... which check their
62//! respective busses once per second) for new devices.
63//! - For each device that is found in any [DeviceCommunicationManager], we emit a DeviceFound event
64//! with that device's identifying information. This information is sent to the
65//! [DeviceConfigurationManager], in order to make sure we can connect (we won't try to connect to
66//! devices we're already connected to, or to devices on the deny list, or to any device that
67//! /isn't/ on the allow list if it exists) identify protocols that may work with the device. With
68//! some protocols and types of communication, we require being connected to the device to discern
69//! the specific protocol to use, so this step may return more than one potential protocol. For
70//! instance, if we're working with Bluetooth LE advertisements, we only get a certain set of info
71//! in ads and may need to have the device connected to figure out exactly what type of device it
72//! is and what it supports.
73//! - If not matching protocols are found, we end discovery of the device at this step. If the
74//! advertisement information changes (i.e. if we get more bluetooth advertisement information),
75//! we may try connecting to the device again. For devices where the info won't change, we
76//! ignore the device until a new scan session is started.
77//! - If matching protocols are returned, we move on to the device connection and setup phase. We
78//! first establish a hardware connection with the device, and then cycle through each protocol we
79//! were handed to see if any of them work with the device. We accept the first protocol found
80//! that works with the device.
81//! - If none of the protocols we were found match, we disconnect and end discovery.
82//! - Once we have both connected hardware and a protocol, we run the protocol's initialization
83//! step. For some protocols, this is required to know what type of device we're talking to or to
84//! put the device in a mode where we can interact with it. However, for many protocols, this is
85//! just a no-op and the device connects ready to run.
86//! - Finally, with everything connected and configured, we have all the information we need to see
87//! if there are any user configurations to apply to the device. This is where users can set
88//! limits different aspects of specific devices, like vibration speed, stroke length, etc...
89//!
90//! Once we've made it through this, the device is handed to the [DeviceManager], and the
91//! [ButtplugServer] notifies the [ButtplugClient] (if one is connected) of the new device via the
92//! DeviceAdded message.
93//!
94//! ## Commanding
95//!
96//!
97
98pub mod hardware;
99pub mod protocol;
100pub mod protocol_impl;
101pub mod server_device;
102mod server_device_manager;
103mod server_device_manager_event_loop;
104
105pub use protocol_impl::get_default_protocol_map;
106pub use server_device::{ServerDevice, ServerDeviceEvent};
107pub use server_device_manager::{ServerDeviceManager, ServerDeviceManagerBuilder};