use std::sync::Arc;
use super::{CreatableFeature, Feature, FeatureEndpoint, FeatureType};
use crate::{channel::HidppChannel, protocol::v20::Hidpp20Error};
#[derive(Clone)]
pub struct RootFeature {
endpoint: FeatureEndpoint,
}
impl CreatableFeature for RootFeature {
const ID: u16 = 0x0000;
const STARTING_VERSION: u8 = 0;
fn new(chan: Arc<HidppChannel>, device_index: u8, _: u8) -> Self {
Self {
endpoint: FeatureEndpoint::new(chan, device_index, 0),
}
}
}
impl Feature for RootFeature {}
impl RootFeature {
pub async fn get_feature(&self, id: u16) -> Result<Option<FeatureInformation>, Hidpp20Error> {
let payload = self
.endpoint
.call(0, [(id >> 8) as u8, id as u8, 0x00])
.await?
.extend_payload();
if payload[0] == 0 {
return Ok(None);
}
Ok(Some(FeatureInformation {
index: payload[0],
typ: FeatureType::from(payload[1]),
version: payload[2],
}))
}
pub async fn ping(&self, data: u8) -> Result<u8, Hidpp20Error> {
let payload = self
.endpoint
.call(1, [0x00, 0x00, data])
.await?
.extend_payload();
Ok(payload[2])
}
}
#[derive(Clone, Copy, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct FeatureInformation {
pub index: u8,
pub typ: FeatureType,
pub version: u8,
}