···1616 hardware::communication::{HardwareCommunicationManager, HardwareCommunicationManagerEvent},
1717 ServerDevice,
1818 ServerDeviceEvent,
1919+ protocol::ProtocolManager
1920};
2021use dashmap::{DashMap, DashSet};
2122use futures::{future, pin_mut, FutureExt, StreamExt};
···5354 loop_cancellation_token: CancellationToken,
5455 /// True if stop scanning message was sent, means we won't send scanning finished.
5556 stop_scanning_received: AtomicBool,
5757+ /// Protocol map, for mapping user definitions to protocols
5858+ protocol_manager: ProtocolManager
5659}
57605861impl ServerDeviceManagerEventLoop {
···8083 connecting_devices: Arc::new(DashSet::new()),
8184 loop_cancellation_token,
8285 stop_scanning_received: AtomicBool::new(false),
8686+ protocol_manager: ProtocolManager::default()
8387 }
8488 }
8589···190194 //
191195 // We used to do this in build_server_device, but we shouldn't mark devices as actually
192196 // connecting until after this happens, so we're moving it back here.
193193- // TODO FIX THIS ASAP
194194- /*
195197 let protocol_specializers = self
196196- .device_config_manager
197197- .protocol_specializers(&creator.specifier());
198198- */
199199- let protocol_specializers = vec![];
198198+ .protocol_manager
199199+ .protocol_specializers(
200200+ &creator.specifier(),
201201+ self.device_config_manager.base_communication_specifiers(),
202202+ self.device_config_manager.user_communication_specifiers()
203203+ );
204204+200205 // If we have no identifiers, then there's nothing to do here. Throw an error.
201206 if protocol_specializers.is_empty() {
202207 debug!(
+2-223
crates/buttplug_server_device_config/src/lib.rs
···158158159159#[derive(Default, Clone)]
160160pub struct DeviceConfigurationManagerBuilder {
161161- skip_default_protocols: bool,
162161 communication_specifiers: HashMap<String, Vec<ProtocolCommunicationSpecifier>>,
163162 user_communication_specifiers: DashMap<String, Vec<ProtocolCommunicationSpecifier>>,
164163 base_device_definitions: HashMap<BaseDeviceIdentifier, BaseDeviceDefinition>,
165164 user_device_definitions: DashMap<UserDeviceIdentifier, DeviceDefinition>,
166166- // Map of protocol names to their respective protocol instance factories
167167- // protocols: Vec<(String, Arc<dyn ProtocolIdentifierFactory>)>,
168165}
169166170167impl DeviceConfigurationManagerBuilder {
···228225 }
229226 self
230227 }
231231- /*
232232- /// Add a protocol instance factory for a [ButtplugProtocol]
233233- pub fn protocol_factory<T>(&mut self, factory: T) -> &mut Self
234234- where
235235- T: ProtocolIdentifierFactory + 'static,
236236- {
237237- self
238238- .protocols
239239- .push((factory.identifier().to_owned(), Arc::new(factory)));
240240- self
241241- }
242242- */
243243- pub fn skip_default_protocols(&mut self) -> &mut Self {
244244- self.skip_default_protocols = true;
245245- self
246246- }
247228248229 pub fn finish(&mut self) -> Result<DeviceConfigurationManager, ButtplugDeviceError> {
249249- // Map of protocol names to their respective protocol instance factories
250250- /*
251251- let mut protocol_map = if !self.skip_default_protocols {
252252- get_default_protocol_map()
253253- } else {
254254- HashMap::new()
255255- };
256256-257257- for (name, protocol) in &self.protocols {
258258- if protocol_map.contains_key(name) {
259259- // TODO Fill in error
260260- }
261261- protocol_map.insert(name.clone(), protocol.clone());
262262- }
263263- */
264230 // Build and validate the protocol attributes tree.
265231 let mut attribute_tree_map = HashMap::new();
266232267233 // Add all the defaults first, they won't have parent attributes.
268234 for (ident, attr) in &self.base_device_definitions {
269269- // If we don't have a protocol loaded for this configuration block, just drop it. We can't do
270270- // anything with it anyways.
271271- /*
272272- if !protocol_map.contains_key(ident.protocol()) {
273273- debug!(
274274- "Protocol {:?} in base configurations does not exist in system, discarding definition.",
275275- ident.protocol()
276276- );
277277- continue;
278278- }
279279- */
280235 /*
281236 for feature in attr.features() {
282237 if let Err(e) = feature.is_valid() {
···292247 // Finally, add in user configurations, which will have an address.
293248 for kv in &self.user_device_definitions {
294249 let (ident, attr) = (kv.key(), kv.value());
295295- // If we don't have a protocol loaded for this configuration block, just drop it. We can't do
296296- // anything with it anyways.
297297- /*
298298- if !protocol_map.contains_key(ident.protocol()) {
299299- warn!(
300300- "Protocol {:?} in user configurations does not exist in system, discarding definition.",
301301- ident.protocol()
302302- );
303303- continue;
304304- }
305305- */
306250 for feature in attr.features() {
307251 if let Err(e) = feature.is_valid() {
308252 error!("Feature {attr:?} for ident {ident:?} is not valid, skipping addition: {e:?}");
···334278/// information about what commands can be sent to the device (Vibrate, Rotate, etc...), and the
335279/// parameters for those commands (number of power levels, stroke distances, etc...).
336280#[derive(Getters)]
281281+#[getset(get = "pub")]
337282pub struct DeviceConfigurationManager {
338338- // Map of protocol names to their respective protocol instance factories
339339- // protocol_map: HashMap<String, Arc<dyn ProtocolIdentifierFactory>>,
340283 /// Communication specifiers from the base device config, mapped from protocol name to vector of
341284 /// specifiers. Should not change/update during a session.
342285 base_communication_specifiers: HashMap<String, Vec<ProtocolCommunicationSpecifier>>,
···344287 base_device_definitions: HashMap<BaseDeviceIdentifier, BaseDeviceDefinition>,
345288 /// Communication specifiers provided by the user, mapped from protocol name to vector of
346289 /// specifiers. Loaded at session start, may change over life of session.
347347- #[getset(get = "pub")]
348290 user_communication_specifiers: DashMap<String, Vec<ProtocolCommunicationSpecifier>>,
349349- /// Device definitions from the base device config. Loaded at session start, may change over life
291291+ /// Device definitions from the user device config. Loaded at session start, may change over life
350292 /// of session.
351351- #[getset(get = "pub")]
352293 user_device_definitions: DashMap<UserDeviceIdentifier, DeviceDefinition>,
353294}
354295···483424 ) -> HashMap<String, Vec<ProtocolCommunicationSpecifier>> {
484425 self.base_communication_specifiers.clone()
485426 }
486486- /*
487487- pub fn protocol_specializers(
488488- &self,
489489- specifier: &ProtocolCommunicationSpecifier,
490490- ) -> Vec<ProtocolSpecializer> {
491491- debug!(
492492- "Looking for protocol that matches specifier: {:?}",
493493- specifier
494494- );
495495- let mut specializers = vec![];
496427497497- let mut update_specializer_map =
498498- |name: &str, specifiers: &Vec<ProtocolCommunicationSpecifier>| {
499499- if specifiers.contains(specifier) {
500500- info!(
501501- "Found protocol {:?} for user specifier {:?}.",
502502- name, specifier
503503- );
504504-505505- if self.protocol_map.contains_key(name) {
506506- specializers.push(ProtocolSpecializer::new(
507507- specifiers.clone(),
508508- self
509509- .protocol_map
510510- .get(name)
511511- .expect("already checked existence")
512512- .create(),
513513- ));
514514- } else {
515515- warn!(
516516- "No protocol implementation for {:?} found for specifier {:?}.",
517517- name, specifier
518518- );
519519- }
520520- }
521521- };
522522-523523- // Loop through both maps, as chaining between DashMap and HashMap gets kinda gross.
524524- for spec in self.user_communication_specifiers.iter() {
525525- update_specializer_map(spec.key(), spec.value());
526526- }
527527- for (name, specifiers) in self.base_communication_specifiers.iter() {
528528- update_specializer_map(name, specifiers);
529529- }
530530- specializers
531531- }
532532- */
533428 pub fn device_definition(&self, identifier: &UserDeviceIdentifier) -> Option<DeviceDefinition> {
534429 let features = if let Some(attrs) = self.user_device_definitions.get(identifier) {
535430 debug!("User device config found for {:?}", identifier);
···567462 Some(features)
568463 }
569464}
570570-571571-/*
572572-#[cfg(test)]
573573-mod test {
574574- use super::*;
575575- use crate::{
576576- core::message::{OutputType, FeatureType},
577577- server::message::server_device_feature::{ServerDeviceFeature, ServerDeviceFeatureOutput},
578578- };
579579- use std::{
580580- collections::{HashMap, HashSet},
581581- ops::RangeInclusive,
582582- };
583583-584584- fn create_unit_test_dcm() -> DeviceConfigurationManager {
585585- let mut builder = DeviceConfigurationManagerBuilder::default();
586586- let specifiers = ProtocolCommunicationSpecifier::BluetoothLE(BluetoothLESpecifier::new(
587587- HashSet::from(["LVS-*".to_owned(), "LovenseDummyTestName".to_owned()]),
588588- vec![],
589589- HashSet::new(),
590590- HashMap::new(),
591591- ));
592592- let mut feature_actuator = HashMap::new();
593593- feature_actuator.insert(
594594- OutputType::Vibrate,
595595- ServerDeviceFeatureOutput::new(&RangeInclusive::new(0, 20), &RangeInclusive::new(0, 20)),
596596- );
597597- builder
598598- .communication_specifier("lovense", &[specifiers])
599599- .protocol_features(
600600- &BaseDeviceIdentifier::new("lovense", &Some("P".to_owned())),
601601- &BaseDeviceDefinition::new(
602602- "Lovense Edge",
603603- &uuid::Uuid::new_v4(),
604604- &None,
605605- &vec![
606606- ServerDeviceFeature::new(
607607- "Edge Vibration 1",
608608- &uuid::Uuid::new_v4(),
609609- &None,
610610- FeatureType::Vibrate,
611611- &Some(feature_actuator.clone()),
612612- &None,
613613- ),
614614- ServerDeviceFeature::new(
615615- "Edge Vibration 2",
616616- &uuid::Uuid::new_v4(),
617617- &None,
618618- FeatureType::Vibrate,
619619- &Some(feature_actuator.clone()),
620620- &None,
621621- ),
622622- ],
623623- &None
624624- ),
625625- )
626626- .finish()
627627- .unwrap()
628628- }
629629-630630- #[test]
631631- fn test_config_equals() {
632632- let config = create_unit_test_dcm();
633633- let spec = ProtocolCommunicationSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device(
634634- "LVS-Something",
635635- &HashMap::new(),
636636- &[],
637637- ));
638638- assert!(!config.protocol_specializers(&spec).is_empty());
639639- }
640640-641641- #[test]
642642- fn test_config_wildcard_equals() {
643643- let config = create_unit_test_dcm();
644644- let spec = ProtocolCommunicationSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device(
645645- "LVS-Whatever",
646646- &HashMap::new(),
647647- &[],
648648- ));
649649- assert!(!config.protocol_specializers(&spec).is_empty());
650650- }
651651- /*
652652- #[test]
653653- fn test_specific_device_config_creation() {
654654- let dcm = create_unit_test_dcm(false);
655655- let spec = ProtocolCommunicationSpecifier::BluetoothLE(BluetoothLESpecifier::new_from_device(
656656- "LVS-Whatever",
657657- &HashMap::new(),
658658- &[],
659659- ));
660660- assert!(!dcm.protocol_specializers(&spec).is_empty());
661661- let config: ProtocolDeviceAttributes = dcm
662662- .device_definition(
663663- &UserDeviceIdentifier::new("Whatever", "lovense", &Some("P".to_owned())),
664664- &[],
665665- )
666666- .expect("Should be found")
667667- .into();
668668- // Make sure we got the right name
669669- assert_eq!(config.name(), "Lovense Edge");
670670- // Make sure we overwrote the default of 1
671671- assert_eq!(
672672- config
673673- .message_attributes()
674674- .scalar_cmd()
675675- .as_ref()
676676- .expect("Test, assuming infallible")
677677- .get(0)
678678- .expect("Test, assuming infallible")
679679- .step_count(),
680680- 20
681681- );
682682- }
683683- */
684684-}
685685-*/