···88//! Representation and management of devices connected to the server.
991010use super::{
1111- ButtplugClientError,
1212- ButtplugClientMessageFuturePair,
1313- ButtplugClientRequest,
1414- ButtplugClientResultFuture,
1515- ButtplugServerMessageFuture,
1111+ ButtplugClientError, ButtplugClientMessageFuturePair, ButtplugClientRequest,
1212+ ButtplugClientResultFuture, ButtplugServerMessageFuture,
1613};
1714use crate::{
1815 core::{
1916 connector::ButtplugConnectorError,
2017 errors::{ButtplugDeviceError, ButtplugError, ButtplugMessageError},
2118 messages::{
2222- ActuatorType,
2323- ButtplugCurrentSpecClientMessage,
2424- ButtplugCurrentSpecDeviceMessageType,
2525- ButtplugCurrentSpecServerMessage,
2626- ButtplugDeviceMessageType,
2727- ButtplugMessage,
2828- DeviceMessageAttributes,
2929- DeviceMessageInfo,
3030- Endpoint,
3131- LinearCmd,
3232- RawReadCmd,
3333- RawSubscribeCmd,
3434- RawUnsubscribeCmd,
3535- RawWriteCmd,
3636- RotateCmd,
3737- RotationSubcommand,
3838- ScalarCmd,
3939- ScalarSubcommand,
4040- SensorReadCmd,
4141- SensorSubscribeCmd,
4242- SensorType,
4343- SensorUnsubscribeCmd,
4444- StopDeviceCmd,
1919+ ActuatorType, ButtplugCurrentSpecClientMessage, ButtplugCurrentSpecDeviceMessageType,
2020+ ButtplugCurrentSpecServerMessage, ButtplugDeviceMessageType, ButtplugMessage,
2121+ DeviceMessageAttributes, DeviceMessageInfo, Endpoint, LinearCmd, RawReadCmd, RawSubscribeCmd,
2222+ RawUnsubscribeCmd, RawWriteCmd, RotateCmd, RotationSubcommand, ScalarCmd, ScalarSubcommand,
2323+ SensorReadCmd, SensorSubscribeCmd, SensorType, SensorUnsubscribeCmd, StopDeviceCmd,
4524 VectorSubcommand,
4625 },
4726 },
4827 util::stream::convert_broadcast_receiver_to_stream,
4928};
5029use futures::{future, FutureExt, Stream};
3030+use getset::{CopyGetters, Getters};
5131use std::{
5232 collections::HashMap,
5333 fmt,
···5636 Arc,
5737 },
5838};
5959-use getset::{Getters, CopyGetters};
6039use tokio::sync::broadcast;
6140use tracing_futures::Instrument;
6241···152131/// to a device connected to the server.
153132pub struct ButtplugClientDevice {
154133 /// Name of the device
155155- #[getset(get="pub")]
134134+ #[getset(get = "pub")]
156135 name: String,
157136 /// Index of the device, matching the index in the
158137 /// [ButtplugServer][crate::server::ButtplugServer]'s
159138 /// [DeviceManager][crate::server::device_manager::DeviceManager].
160160- #[getset(get_copy="pub")]
139139+ #[getset(get_copy = "pub")]
161140 index: u32,
162141 /// Map of messages the device can take, along with the attributes of those
163142 /// messages.
164164- #[getset(get="pub")]
143143+ #[getset(get = "pub")]
165144 message_attributes: DeviceMessageAttributes,
166145 /// Sends commands from the [ButtplugClientDevice] instance to the
167146 /// [ButtplugClient][super::ButtplugClient]'s event loop, which will then send
···310289 .into(),
311290 ),
312291 }
313313- }.boxed()
292292+ }
293293+ .boxed()
314294 }
315295316296 /// Commands device to vibrate, assuming it has the features to do so.
···372352 self.send_message_expect_ok(msg)
373353 }
374354375375-pub fn scalar(&self, scalar_cmd: &ScalarCommand) -> ButtplugClientResultFuture {
376376- if self.message_attributes.scalar_cmd().is_none() {
377377- return self.create_boxed_future_client_error(
378378- ButtplugDeviceError::MessageNotSupported(ButtplugDeviceMessageType::VibrateCmd).into(),
379379- );
380380- }
355355+ pub fn scalar(&self, scalar_cmd: &ScalarCommand) -> ButtplugClientResultFuture {
356356+ if self.message_attributes.scalar_cmd().is_none() {
357357+ return self.create_boxed_future_client_error(
358358+ ButtplugDeviceError::MessageNotSupported(ButtplugDeviceMessageType::VibrateCmd).into(),
359359+ );
360360+ }
381361382382- let scalar_count: u32 = self
383383- .message_attributes
384384- .scalar_cmd()
385385- .as_ref()
386386- .expect("Already checked existence")
387387- .len() as u32;
362362+ let scalar_count: u32 = self
363363+ .message_attributes
364364+ .scalar_cmd()
365365+ .as_ref()
366366+ .expect("Already checked existence")
367367+ .len() as u32;
388368389389- let mut scalar_vec: Vec<ScalarSubcommand>;
390390- match scalar_cmd {
391391- ScalarCommand::Scalar((scalar, actuator)) => {
392392- scalar_vec = Vec::with_capacity(scalar_count as usize);
393393- for i in 0..scalar_count {
394394- scalar_vec.push(ScalarSubcommand::new(i, *scalar, *actuator));
395395- }
396396- }
397397- ScalarCommand::ScalarMap(map) => {
398398- if map.len() as u32 > scalar_count {
399399- return self.create_boxed_future_client_error(
400400- ButtplugDeviceError::DeviceFeatureCountMismatch(scalar_count, map.len() as u32)
401401- .into(),
402402- );
369369+ let mut scalar_vec: Vec<ScalarSubcommand>;
370370+ match scalar_cmd {
371371+ ScalarCommand::Scalar((scalar, actuator)) => {
372372+ scalar_vec = Vec::with_capacity(scalar_count as usize);
373373+ for i in 0..scalar_count {
374374+ scalar_vec.push(ScalarSubcommand::new(i, *scalar, *actuator));
375375+ }
403376 }
404404- scalar_vec = Vec::with_capacity(map.len() as usize);
405405- for (idx, (scalar, actuator)) in map {
406406- if *idx >= scalar_count {
377377+ ScalarCommand::ScalarMap(map) => {
378378+ if map.len() as u32 > scalar_count {
407379 return self.create_boxed_future_client_error(
408408- ButtplugDeviceError::DeviceFeatureIndexError(scalar_count, *idx).into(),
380380+ ButtplugDeviceError::DeviceFeatureCountMismatch(scalar_count, map.len() as u32).into(),
409381 );
410382 }
411411- scalar_vec.push(ScalarSubcommand::new(*idx, *scalar, *actuator));
383383+ scalar_vec = Vec::with_capacity(map.len() as usize);
384384+ for (idx, (scalar, actuator)) in map {
385385+ if *idx >= scalar_count {
386386+ return self.create_boxed_future_client_error(
387387+ ButtplugDeviceError::DeviceFeatureIndexError(scalar_count, *idx).into(),
388388+ );
389389+ }
390390+ scalar_vec.push(ScalarSubcommand::new(*idx, *scalar, *actuator));
391391+ }
412392 }
413413- }
414414- ScalarCommand::ScalarVec(vec) => {
415415- if vec.len() as u32 > scalar_count {
416416- return self.create_boxed_future_client_error(
417417- ButtplugDeviceError::DeviceFeatureCountMismatch(scalar_count, vec.len() as u32)
418418- .into(),
419419- );
420420- }
421421- scalar_vec = Vec::with_capacity(vec.len() as usize);
422422- for (i, (scalar, actuator)) in vec.iter().enumerate() {
423423- scalar_vec.push(ScalarSubcommand::new(i as u32, *scalar, *actuator));
393393+ ScalarCommand::ScalarVec(vec) => {
394394+ if vec.len() as u32 > scalar_count {
395395+ return self.create_boxed_future_client_error(
396396+ ButtplugDeviceError::DeviceFeatureCountMismatch(scalar_count, vec.len() as u32).into(),
397397+ );
398398+ }
399399+ scalar_vec = Vec::with_capacity(vec.len() as usize);
400400+ for (i, (scalar, actuator)) in vec.iter().enumerate() {
401401+ scalar_vec.push(ScalarSubcommand::new(i as u32, *scalar, *actuator));
402402+ }
424403 }
425404 }
405405+ let msg = ScalarCmd::new(self.index, scalar_vec).into();
406406+ self.send_message_expect_ok(msg)
426407 }
427427- let msg = ScalarCmd::new(self.index, scalar_vec).into();
428428- self.send_message_expect_ok(msg)
429429-}
430408431409 /// Commands device to move linearly, assuming it has the features to do so.
432410 pub fn linear(&self, linear_cmd: &LinearCommand) -> ButtplugClientResultFuture {
···570548 return self.create_boxed_future_client_error(
571549 ButtplugDeviceError::ProtocolSensorNotSupported(*sensor_type).into(),
572550 );
573573- }
551551+ }
574552 let msg = SensorReadCmd::new(self.index, sensor_indexes[0], *sensor_type).into();
575553 let reply = self.send_message(msg);
576576- async move {
554554+ async move {
577555 if let ButtplugCurrentSpecServerMessage::SensorReading(data) = reply.await? {
578556 Ok(data.data().clone())
579557 } else {
580580- Err(ButtplugError::ButtplugMessageError(ButtplugMessageError::UnexpectedMessageType("SensorReading".to_owned())).into())
558558+ Err(
559559+ ButtplugError::ButtplugMessageError(ButtplugMessageError::UnexpectedMessageType(
560560+ "SensorReading".to_owned(),
561561+ ))
562562+ .into(),
563563+ )
581564 }
582582- }.boxed()
565565+ }
566566+ .boxed()
583567 }
584568585569 pub fn battery_level(&self) -> ButtplugClientResultFuture<f64> {
···602586 pub fn raw_write(
603587 &self,
604588 endpoint: Endpoint,
605605- data: Vec<u8>,
589589+ data: &Vec<u8>,
606590 write_with_response: bool,
607591 ) -> ButtplugClientResultFuture {
608592 if self.message_attributes.raw_write_cmd().is_none() {
···613597 let msg = ButtplugCurrentSpecClientMessage::RawWriteCmd(RawWriteCmd::new(
614598 self.index,
615599 endpoint,
616616- data,
600600+ data.clone(),
617601 write_with_response,
618602 ));
619603 self.send_message_expect_ok(msg)
···649633 .into(),
650634 ),
651635 }
652652- }.boxed()
636636+ }
637637+ .boxed()
653638 }
654639655640 pub fn raw_subscribe(&self, endpoint: Endpoint) -> ButtplugClientResultFuture {
···703688 }
704689}
705690706706-impl Eq for ButtplugClientDevice {
707707-}
691691+impl Eq for ButtplugClientDevice {}
708692709693impl PartialEq for ButtplugClientDevice {
710694 fn eq(&self, other: &Self) -> bool {
+3-1
buttplug/src/server/device/configuration/mod.rs
···324324 }
325325326326 /// Check to make sure the message attributes of an instance are valid.
327327- // TODO Can we do this in new() instead and return a result there?
328327 fn is_valid(&self) -> Result<(), ButtplugDeviceError> {
329328 if let Some(attrs) = self.message_attributes.scalar_cmd() {
330329 for attr in attrs {
···679678 raw_endpoints: &[Endpoint],
680679 ) -> Option<ProtocolDeviceAttributes> {
681680 let mut flat_attrs = if let Some(attrs) = self.protocol_attributes.get(&identifier.into()) {
681681+ debug!("User device config found for {:?}", identifier);
682682 attrs.flatten()
683683 } else if let Some(attrs) = self.protocol_attributes.get(&ProtocolAttributesIdentifier {
684684 address: None,
685685 attributes_identifier: identifier.attributes_identifier().clone(),
686686 protocol: identifier.protocol().clone(),
687687 }) {
688688+ debug!("Protocol + Identifier device config found for {:?}", identifier);
688689 attrs.flatten()
689690 } else if let Some(attrs) = self.protocol_attributes.get(&ProtocolAttributesIdentifier {
690691 address: None,
691692 attributes_identifier: ProtocolAttributesType::Default,
692693 protocol: identifier.protocol().clone(),
693694 }) {
695695+ debug!("Protocol device config found for {:?}", identifier);
694696 attrs.flatten()
695697 } else {
696698 return None;
+6
buttplug/src/server/mod.rs
···303303 .protocol_attributes(ident.clone(), attributes.clone());
304304 }
305305306306+ for (ident, attributes) in protocol_map.user_configs() {
307307+ self
308308+ .device_manager_builder
309309+ .protocol_attributes(ident.into(), attributes.clone());
310310+ }
311311+306312 let device_manager = Arc::new(self.device_manager_builder.finish(output_sender.clone())?);
307313308314 // Spawn the ping timer task, assuming the ping time is > 0.