hidpp/feature/change_host/
mod.rs1use std::sync::Arc;
5
6use crate::{
7 channel::HidppChannel,
8 feature::{CreatableFeature, Feature, FeatureEndpoint},
9 protocol::v20::Hidpp20Error,
10};
11
12bitflags::bitflags! {
13 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
16 pub struct ChangeHostCapabilities: u8 {
17 const ENHANCED_HOST_SWITCH = 1 << 0;
21 }
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27#[non_exhaustive]
28pub struct ChangeHostInfo {
29 pub host_count: u8,
31 pub current_host: u8,
33 pub capabilities: ChangeHostCapabilities,
35}
36
37#[derive(Clone)]
39pub struct ChangeHostFeature {
40 endpoint: FeatureEndpoint,
42}
43
44impl CreatableFeature for ChangeHostFeature {
45 const ID: u16 = 0x1814;
46 const STARTING_VERSION: u8 = 0;
47
48 fn new(chan: Arc<HidppChannel>, device_index: u8, feature_index: u8) -> Self {
49 Self {
50 endpoint: FeatureEndpoint::new(chan, device_index, feature_index),
51 }
52 }
53}
54
55impl Feature for ChangeHostFeature {}
56
57impl ChangeHostFeature {
58 pub async fn get_host_info(&self) -> Result<ChangeHostInfo, Hidpp20Error> {
60 let payload = self.endpoint.call(0, [0; 3]).await?.extend_payload();
61 Ok(ChangeHostInfo {
62 host_count: payload[0],
63 current_host: payload[1],
64 capabilities: ChangeHostCapabilities::from_bits_retain(payload[2]),
65 })
66 }
67
68 pub async fn set_current_host(&self, host: u8) -> Result<(), Hidpp20Error> {
74 self.endpoint.notify(1, [host, 0, 0]).await
75 }
76
77 pub async fn get_cookies(&self, host_count: u8) -> Result<Vec<u8>, Hidpp20Error> {
82 let count = usize::from(host_count);
83 let payload = self.endpoint.call(2, [0; 3]).await?.extend_payload();
84 if count > payload.len() {
85 return Err(Hidpp20Error::UnsupportedResponse);
86 }
87 Ok(payload[..count].to_vec())
88 }
89
90 pub async fn set_cookie(&self, host: u8, cookie: u8) -> Result<(), Hidpp20Error> {
96 self.endpoint.call(3, [host, cookie, 0]).await?;
97 Ok(())
98 }
99}