use std::sync::Arc;
use super::{CreatableFeature, Feature, FeatureType};
use crate::{
channel::HidppChannel,
nibble::U4,
protocol::v20::{self, Hidpp20Error},
};
#[derive(Clone)]
pub struct RootFeature {
chan: Arc<HidppChannel>,
device_index: u8,
}
impl CreatableFeature for RootFeature {
const ID: u16 = 0x0000;
const STARTING_VERSION: u8 = 0;
fn new(chan: Arc<HidppChannel>, device_index: u8, _: u8) -> Self {
Self {
chan,
device_index,
}
}
}
impl Feature for RootFeature {
}
impl RootFeature {
pub async fn get_feature(&self, id: u16) -> Result<Option<FeatureInformation>, Hidpp20Error> {
let response = self
.chan
.send_v20(v20::Message::Short(
v20::MessageHeader {
device_index: self.device_index,
feature_index: 0,
function_id: U4::from_lo(0),
software_id: self.chan.get_sw_id(),
},
[(id >> 8) as u8, id as u8, 0x00],
))
.await?;
let payload = response.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 response = self
.chan
.send_v20(v20::Message::Short(
v20::MessageHeader {
device_index: self.device_index,
feature_index: 0,
function_id: U4::from_lo(1),
software_id: self.chan.get_sw_id(),
},
[0x00, 0x00, data],
))
.await?;
let payload = response.extend_payload();
Ok(payload[2])
}
}
#[derive(Clone, Copy, Hash, Debug)]
#[non_exhaustive]
pub struct FeatureInformation {
pub index: u8,
pub typ: FeatureType,
pub version: u8,
}