hidpp/feature/wireless_device_status/
mod.rs1use std::sync::Arc;
5
6use num_enum::{FromPrimitive, IntoPrimitive};
7
8use crate::{
9 channel::{HidppChannel, MessageListenerGuard},
10 event::EventEmitter,
11 feature::{CreatableFeature, EmittingFeature, Feature, event_payload},
12};
13
14pub struct WirelessDeviceStatusFeature {
16 emitter: Arc<EventEmitter<WirelessDeviceStatusEvent>>,
18
19 _msg_listener: MessageListenerGuard,
21}
22
23impl CreatableFeature for WirelessDeviceStatusFeature {
24 const ID: u16 = 0x1d4b;
25 const STARTING_VERSION: u8 = 0;
26
27 fn new(chan: Arc<HidppChannel>, device_index: u8, feature_index: u8) -> Self {
28 let emitter = Arc::new(EventEmitter::new());
29
30 let listener = chan.add_msg_listener_guarded({
31 let emitter = Arc::clone(&emitter);
32
33 move |raw, matched| {
34 let Some((func, payload)) =
35 event_payload(raw, matched, device_index, feature_index)
36 else {
37 return;
38 };
39 if func.to_lo() != 0 {
41 return;
42 }
43
44 emitter.emit(WirelessDeviceStatusEvent::StatusBroadcast(
48 WirelessDeviceStatusBroadcast {
49 status: WirelessDeviceStatus::from(payload[0]),
50 request: WirelessDeviceStatusRequest::from(payload[1]),
51 reason: WirelessDeviceStatusReason::from(payload[2]),
52 },
53 ));
54 }
55 });
56
57 Self {
58 emitter,
59 _msg_listener: listener,
60 }
61 }
62}
63
64impl Feature for WirelessDeviceStatusFeature {}
65
66impl EmittingFeature<WirelessDeviceStatusEvent> for WirelessDeviceStatusFeature {
67 fn listen(&self) -> async_channel::Receiver<WirelessDeviceStatusEvent> {
68 self.emitter.create_receiver()
69 }
70}
71
72#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize))]
76#[non_exhaustive]
77pub enum WirelessDeviceStatusEvent {
78 StatusBroadcast(WirelessDeviceStatusBroadcast),
82}
83
84#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize))]
88#[non_exhaustive]
89pub struct WirelessDeviceStatusBroadcast {
90 pub status: WirelessDeviceStatus,
92
93 pub request: WirelessDeviceStatusRequest,
95
96 pub reason: WirelessDeviceStatusReason,
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
103#[cfg_attr(feature = "serde", derive(serde::Serialize))]
104#[non_exhaustive]
105#[repr(u8)]
106pub enum WirelessDeviceStatus {
107 Unknown = 0x00,
109 Reconnection = 0x01,
111 #[num_enum(catch_all)]
113 Other(u8),
114}
115
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize))]
120#[non_exhaustive]
121#[repr(u8)]
122pub enum WirelessDeviceStatusRequest {
123 NoRequest = 0x00,
125 SoftwareReconfigurationNeeded = 0x01,
127 #[num_enum(catch_all)]
129 Other(u8),
130}
131
132#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
135#[cfg_attr(feature = "serde", derive(serde::Serialize))]
136#[non_exhaustive]
137#[repr(u8)]
138pub enum WirelessDeviceStatusReason {
139 Unknown = 0x00,
141 PowerSwitchActivated = 0x01,
143 #[num_enum(catch_all)]
145 Other(u8),
146}