buttplug/server/device/protocol/
zalo.rs

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
8use crate::{
9  core::{
10    errors::ButtplugDeviceError,
11    message::{ActuatorType, Endpoint},
12  },
13  server::device::{
14    hardware::{HardwareCommand, HardwareWriteCmd},
15    protocol::{generic_protocol_setup, ProtocolHandler},
16  },
17};
18
19generic_protocol_setup!(Zalo, "zalo");
20
21#[derive(Default)]
22pub struct Zalo {}
23
24impl ProtocolHandler for Zalo {
25  fn keepalive_strategy(&self) -> super::ProtocolKeepaliveStrategy {
26    super::ProtocolKeepaliveStrategy::RepeatLastPacketStrategy
27  }
28
29  fn handle_scalar_cmd(
30    &self,
31    cmds: &[Option<(ActuatorType, u32)>],
32  ) -> Result<Vec<HardwareCommand>, ButtplugDeviceError> {
33    // Store off result before the match, so we drop the lock ASAP.
34    let speed0: u8 = cmds[0].unwrap_or((ActuatorType::Vibrate, 0)).1 as u8;
35    let speed1: u8 = if cmds.len() == 1 {
36      0
37    } else {
38      cmds[1].unwrap_or((ActuatorType::Vibrate, 0)).1 as u8
39    };
40    Ok(vec![HardwareWriteCmd::new(
41      Endpoint::Tx,
42      vec![
43        if speed0 == 0 && speed1 == 0 {
44          0x02
45        } else {
46          0x01
47        },
48        if speed0 == 0 { 0x01 } else { speed0 },
49        if speed1 == 0 { 0x01 } else { speed1 },
50      ],
51      true,
52    )
53    .into()])
54  }
55}