hid-io-core 0.1.4

HID-IO is a host-side daemon for advanced HID devices.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
#![cfg(target_os = "linux")]
/* Copyright (C) 2020-2022 by Jacob Alexander
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::api::common_capnp;
use crate::api::Endpoint;
use crate::api::UhidInfo;
use crate::mailbox;
use crate::module::vhid;
use hid_io_protocol::HidIoCommandId;
use libc::{c_int, c_short, c_ulong, c_void};
use std::io::{Error, ErrorKind};
use std::os::unix::io::AsRawFd;

/// Default OutputEvent handler
/// Prints useful debug information when even when the events aren't normally used
fn default_output_event(
    output_event: Result<uhid_virt::OutputEvent, uhid_virt::StreamError>,
    params: uhid_virt::CreateParams,
) -> Result<(), Error> {
    match output_event {
        Ok(event) => match event {
            uhid_virt::OutputEvent::Start { dev_flags } => {
                let mut flags: String = "".to_string();
                for flag in dev_flags {
                    match flag {
                        uhid_virt::DevFlags::FeatureReportsNumbered => {
                            flags += "FeatureReportsNumbered,"
                        }
                        uhid_virt::DevFlags::InputReportsNumbered => {
                            flags += "InputReportsNumbered,"
                        }
                        uhid_virt::DevFlags::OutputReportsNumbered => {
                            flags += "OutputReportsNumbered,"
                        }
                    }
                }
                debug!("Start({}): dev_flags={}", params.name, flags);
                Ok(())
            }
            uhid_virt::OutputEvent::Stop => {
                debug!("Stop({})", params.name);
                Ok(())
            }
            uhid_virt::OutputEvent::Open => {
                debug!("Open({})", params.name);
                Ok(())
            }
            uhid_virt::OutputEvent::Close => {
                debug!("Close({})", params.name);
                Ok(())
            }
            uhid_virt::OutputEvent::Output { data } => {
                debug!("Output({}): {:?}", params.name, data);
                Ok(())
            }
            uhid_virt::OutputEvent::GetReport {
                id,
                report_number,
                report_type,
            } => {
                warn!(
                    "GetReport({}): id={} report_number={} report_type={:?}",
                    params.name, id, report_number, report_type
                );
                Ok(())
            }
            uhid_virt::OutputEvent::SetReport {
                id,
                report_number,
                report_type,
                data,
            } => {
                warn!(
                    "SetReport({}): id={} report_number={} report_type={:?} data={:?}",
                    params.name, id, report_number, report_type, data
                );
                Ok(())
            }
        },
        Err(msg) => {
            match msg {
                // Standard errors (e.g. permission denied)
                uhid_virt::StreamError::Io(err) => Err(err),
                // Unknown errors
                uhid_virt::StreamError::UnknownEventType(code) => Err(Error::new(
                    ErrorKind::Other,
                    format!("Unknown error code: {code}"),
                )),
            }
        }
    }
}

/// uhid NKRO Keyboard
/// To create multiple unique devices, make sure to set uniq to a unique value so to differentiate
/// betweent devices
pub struct KeyboardNkro {
    mailbox: mailbox::Mailbox,
    uid: u64,
    _endpoint: Endpoint,
    params: uhid_virt::CreateParams,
    device: uhid_virt::UHIDDevice<std::fs::File>,
}

impl KeyboardNkro {
    #![allow(clippy::too_many_arguments)]
    pub fn new(
        mailbox: mailbox::Mailbox,
        name: String,
        phys: String,
        uniq: String,
        bus: uhid_virt::Bus,
        vendor: u32,
        product: u32,
        version: u32,
        country: u32,
    ) -> std::io::Result<KeyboardNkro> {
        // Setup creation parameters
        let params = uhid_virt::CreateParams {
            name,
            phys,
            uniq,
            bus,
            vendor,
            product,
            version,
            country,
            rd_data: vhid::KEYBOARD_NKRO.to_vec(),
        };

        // Initialize uhid device
        let device = uhid_virt::UHIDDevice::create(params.clone())?;

        // Assign uid to newly created device (need path location for uniqueness)
        let path = "/dev/uhid".to_string();
        let mut uhid_info = UhidInfo::new(params.clone());
        let uid = mailbox.clone().assign_uid(uhid_info.key(), path).unwrap();

        // Setup Endpoint
        let mut endpoint = Endpoint::new(common_capnp::NodeType::HidKeyboard, uid);
        endpoint.set_uhid_params(uhid_info);

        // Register node
        mailbox.clone().register_node(endpoint.clone());

        Ok(KeyboardNkro {
            mailbox,
            uid,
            _endpoint: endpoint,
            params,
            device,
        })
    }

    /// Sends a keyboard HID message
    /// This command does not maintain any state from any previously sent commands
    pub fn send(&mut self, keyboard_hid_codes: Vec<u8>) -> Result<(), Error> {
        // 28 byte message
        let mut data = vec![0; 28];

        // Iterate over hid codes, building the bitmask
        for key in &keyboard_hid_codes {
            match key {
                // 224-231 (1 byte/8 bits) - Modifier Section - Byte 0
                224..=231 => {
                    data[0] |= 1 << (key ^ 0xE0);
                }
                // 4-164 (21 bytes/161 bits + 4 bits + 3 bits) - Keyboard Section - Bytes 1-22
                // 176-221 (6 bytes/46 bits) - Keypad Section
                4..=164 | 176..=221 => {
                    let byte_pos = key / 8; // Determine which byte
                    let bit_mask = 1 << (key - 8 * byte_pos); // Determine which bit
                    data[byte_pos as usize + 1] |= bit_mask; // Offset array by 1 to start at Byte 1
                }
                _ => {}
            };
        }
        debug!("NKRO: {:?}", data);

        // Write message
        match self.device.write(&data) {
            Ok(_) => Ok(()),
            Err(msg) => Err(msg),
        }
    }

    /// Process a single event
    /// This command will block, so make sure to call it in a separate thread
    pub fn process(&mut self) -> Result<(), Error> {
        // Blocks until an event is received
        let output_event = self.device.read();

        // Handle LED events
        if let Ok(uhid_virt::OutputEvent::Output { data }) = &output_event {
            // NOTE: data is not processed and is sent as a bitfield
            // Send message containing LED events
            self.mailbox
                .try_send_command(
                    mailbox::Address::DeviceHid { uid: self.uid },
                    mailbox::Address::All,
                    HidIoCommandId::HidKeyboardLed,
                    data.to_vec(),
                    false,
                )
                .unwrap();
        }

        // Default event handler
        default_output_event(output_event, self.params.clone())
    }
}

impl Drop for KeyboardNkro {
    fn drop(&mut self) {
        // Unregister node
        self.mailbox.unregister_node(self.uid);
    }
}

/// uhid 6KRO Keyboard
/// To create multiple unique devices, make sure to set uniq to a unique value so to differentiate
/// betweent devices
pub struct Keyboard6kro {
    mailbox: mailbox::Mailbox,
    uid: u64,
    _endpoint: Endpoint,
    params: uhid_virt::CreateParams,
    device: uhid_virt::UHIDDevice<std::fs::File>,
}

impl Keyboard6kro {
    #![allow(clippy::too_many_arguments)]
    pub fn new(
        mailbox: mailbox::Mailbox,
        name: String,
        phys: String,
        uniq: String,
        bus: uhid_virt::Bus,
        vendor: u32,
        product: u32,
        version: u32,
        country: u32,
    ) -> std::io::Result<Keyboard6kro> {
        // Setup creation parameters
        let params = uhid_virt::CreateParams {
            name,
            phys,
            uniq,
            bus,
            vendor,
            product,
            version,
            country,
            rd_data: vhid::KEYBOARD_6KRO.to_vec(),
        };

        // Initialize uhid device
        let device = uhid_virt::UHIDDevice::create(params.clone())?;

        // Assign uid to newly created device (need path location for uniqueness)
        let path = "/dev/uhid".to_string();
        let mut uhid_info = UhidInfo::new(params.clone());
        let uid = mailbox.clone().assign_uid(uhid_info.key(), path).unwrap();

        // Setup Endpoint
        let mut endpoint = Endpoint::new(common_capnp::NodeType::HidKeyboard, uid);
        endpoint.set_uhid_params(uhid_info);

        // Register node
        mailbox.clone().register_node(endpoint.clone());

        Ok(Keyboard6kro {
            mailbox,
            uid,
            _endpoint: endpoint,
            params,
            device,
        })
    }

    /// Sends a keyboard HID message
    /// This command does not maintain any state from any previously sent commands
    pub fn send(&mut self, keyboard_hid_codes: Vec<u8>) -> Result<(), Error> {
        // 8 byte message
        // Byte 0: Modifiers
        // Byte 1: Reserved
        // Byte 2-7: Keys
        let mut data = vec![0; 8];

        // Iterate over hid codes, building message
        let mut key_pos = 2;
        for key in &keyboard_hid_codes {
            match key {
                // 224-231 (1 byte/8 bits) - Modifier Section - Byte 0
                224..=231 => {
                    data[0] |= 1 << (key ^ 0xE0);
                }
                // 4-164, 176-221 (Bytes 2-7)
                4..=164 | 176..=221 => {
                    // Only add the first 6 keys, ignore the rest in this range
                    // (first byte is for modifiers, second byte is reserved)
                    if key_pos < 8 {
                        data[key_pos] = *key;
                        key_pos += 1;
                    }
                }
                _ => {}
            };
        }
        debug!("6KRO: {:?}", data);

        // Write message
        match self.device.write(&data) {
            Ok(_) => Ok(()),
            Err(msg) => Err(msg),
        }
    }

    /// Process a single event
    /// This command will block, so make sure to call it in a separate thread
    pub fn process(&mut self) -> Result<(), Error> {
        // Blocks until an event is received
        let output_event = self.device.read();

        // Handle LED events
        if let Ok(uhid_virt::OutputEvent::Output { data }) = &output_event {
            // NOTE: data is not processed and is sent as a bitfield
            // Send message containing LED events
            self.mailbox
                .try_send_command(
                    mailbox::Address::DeviceHid { uid: self.uid },
                    mailbox::Address::All,
                    HidIoCommandId::HidKeyboardLed,
                    data.to_vec(),
                    false,
                )
                .unwrap();
        }

        // Default event handler
        default_output_event(output_event, self.params.clone())
    }
}

impl Drop for Keyboard6kro {
    fn drop(&mut self) {
        // Unregister node
        self.mailbox.unregister_node(self.uid);
    }
}

/*
pub struct Mouse {
    mailbox: mailbox::Mailbox,
    uid: u64,
    endpoint: Endpoint,
    params: uhid_virt::CreateParams,
    device: uhid_virt::UHIDDevice<std::fs::File>,
}

impl Mouse {
    pub fn new(
        mailbox: mailbox::Mailbox,
        name: String,
        phys: String,
        uniq: String,
        bus: uhid_virt::Bus,
        vendor: u32,
        product: u32,
        version: u32,
        country: u32,
    ) -> std::io::Result<Mouse> {
        // Setup creation parameters
        let params = uhid_virt::CreateParams {
            name,
            phys,
            uniq,
            bus,
            vendor,
            product,
            version,
            country,
            rd_data: vhid::MOUSE.to_vec(),
        };

        // Initialize uhid device
        let device = uhid_virt::UHIDDevice::create(params.clone())?;

        // Assign uid to newly created device (need path location for uniqueness)
        let path = "/dev/uhid".to_string();
        let mut uhid_info = UhidInfo::new(params.clone());
        let uid = mailbox.clone().assign_uid(uhid_info.key(), path).unwrap();

        // Setup Endpoint
        let mut endpoint = Endpoint::new(common_capnp::NodeType::HidMouse, uid);
        endpoint.set_uhid_params(uhid_info);

        // Register node
        mailbox.clone().register_node(endpoint.clone());

        Ok(Mouse { mailbox, uid, endpoint, params, device })
    }
}

impl Drop for Mouse {
    fn drop(&mut self) {
        // Unregister node
        self.mailbox.unregister_node(self.uid);
    }
}

pub struct Xbox360Controller {
    mailbox: mailbox::Mailbox,
    uid: u64,
    endpoint: Endpoint,
    params: uhid_virt::CreateParams,
    device: uhid_virt::UHIDDevice<std::fs::File>,
}

impl std::io::Result<Xbox360Controller> {
    pub fn new(
        mailbox: mailbox::Mailbox,
        name: String,
        phys: String,
        uniq: String,
        bus: uhid_virt::Bus,
        vendor: u32,
        product: u32,
        version: u32,
        country: u32,
    ) -> Xbox360Controller {
        // Setup creation parameters
        let params = uhid_virt::CreateParams {
            name,
            phys,
            uniq,
            bus,
            vendor,
            product,
            version,
            country,
            rd_data: vhid::XBOX_360_CONTROLLER.to_vec(),
        };

        // Initialize uhid device
        let device = uhid_virt::UHIDDevice::create(params.clone())?;

        // Assign uid to newly created device (need path location for uniqueness)
        let path = "/dev/uhid".to_string();
        let mut uhid_info = UhidInfo::new(params.clone());
        let uid = mailbox.clone().assign_uid(uhid_info.key(), path).unwrap();

        // Setup Endpoint
        let mut endpoint = Endpoint::new(common_capnp::NodeType::HidJoystick, uid);
        endpoint.set_uhid_params(uhid_info);

        // Register node
        mailbox.clone().register_node(endpoint.clone());

        Ok(Xbox360Controller { mailbox, uid, endpoint, params, device })
    }
}

impl Drop for Xbox360Controller {
    fn drop(&mut self) {
        // Unregister node
        self.mailbox.unregister_node(self.uid);
    }
}

pub struct SysCtrlConsControl {
    mailbox: mailbox::Mailbox,
    uid: u64,
    endpoint: Endpoint,
    params: uhid_virt::CreateParams,
    device: uhid_virt::UHIDDevice<std::fs::File>,
}

impl SysCtrlConsControl {
    pub fn new(
        mailbox: mailbox::Mailbox,
        name: String,
        phys: String,
        uniq: String,
        bus: uhid_virt::Bus,
        vendor: u32,
        product: u32,
        version: u32,
        country: u32,
    ) -> std::io::Result<SysCtrlConsControl> {
        // Setup creation parameters
        let params = uhid_virt::CreateParams {
            name,
            phys,
            uniq,
            bus,
            vendor,
            product,
            version,
            country,
            rd_data: vhid::SYSCTRL_CONSCTRL.to_vec(),
        };

        // Initialize uhid device
        let device = uhid_virt::UHIDDevice::create(params.clone())?;

        // Assign uid to newly created device (need path location for uniqueness)
        let path = "/dev/uhid".to_string();
        let mut uhid_info = UhidInfo::new(params.clone());
        let uid = mailbox.clone().assign_uid(uhid_info.key(), path).unwrap();

        // Setup Endpoint
        let mut endpoint = Endpoint::new(common_capnp::NodeType::HidKeyboard, uid);
        endpoint.set_uhid_params(uhid_info);

        // Register node
        mailbox.clone().register_node(endpoint.clone());

        Ok(SysCtrlConsControl { mailbox, uid, endpoint, params, device })
    }

    /// Process a single event
    /// This command will block, so make sure to call it in a separate thread
    pub fn process(&mut self) -> Result<(), Error> {
        // Blocks until an event is received
        let output_event = self.device.read();

        // Handle LED events
        if let Ok(event) = &output_event {
            match event {
                uhid_virt::OutputEvent::Output { data } => {
                    // NOTE: data is not processed and is sent as a bitfield
                    // Send message containing LED events
                    self.mailbox.send_command(
                        mailbox::Address::DeviceHid { uid: self.uid },
                        mailbox::Address::All,
                        HidIoCommandId::HIDKeyboardLED,
                        data.to_vec(),
                    );
                }
                _ => {}
            }
        }

        // Default event handler
        default_output_event(output_event, self.params.clone())
    }
}

impl Drop for SysCtrlConsControl {
    fn drop(&mut self) {
        // Unregister node
        self.mailbox.unregister_node(self.uid);
    }
}
*/

/// uhid initialization
///
/// Sets up processing threads for uhid
pub async fn initialize(_mailbox: mailbox::Mailbox) {
    info!("Initializing vhid/uhid...");

    // Spawn watcher thread (tokio)
    // TODO - api monitoring
    //        * Create new virtual hid device, return uid
    //        * Destroy hid device by uid
    //        * Lookup hid device information using uid
    // TODO - Can this functionality be moved up to vhid instead of uhid?
}

#[allow(dead_code)]
#[repr(C)]
struct pollfd {
    fd: c_int,
    events: c_short,
    revents: c_short,
}

#[repr(C)]
struct sigset_t {
    __private: c_void,
}

#[allow(non_camel_case_types)]
type nfds_t = c_ulong;

const POLLIN: c_short = 0x0001;

extern "C" {
    fn ppoll(
        fds: *mut pollfd,
        nfds: nfds_t,
        timeout_ts: *mut libc::timespec,
        sigmask: *const sigset_t,
    ) -> c_int;
}

/// Use parameters to find a uhid device using udev
/// If we don't find the device right away, start to poll
pub fn udev_find_device(
    vid: u16,
    pid: u16,
    subsystem: String,
    uniq: String,
    timeout: std::time::Duration,
) -> Result<udev::Device, std::io::Error> {
    // First look in the list of devices
    let mut enumerator = udev::Enumerator::new().unwrap();
    enumerator.match_subsystem("input").unwrap();
    enumerator
        .match_attribute("id/vendor", format!("{:04x}", vhid::IC_VID))
        .unwrap();
    enumerator
        .match_attribute("id/product", format!("{:04x}", vhid::IC_PID_KEYBOARD))
        .unwrap();
    enumerator.match_attribute("uniq", uniq.clone()).unwrap();

    // Validate parameters
    let mut devices = enumerator.scan_devices().unwrap();
    if let Some(device) = devices.next() {
        return Ok(device);
    }

    // Couldn't find, setup a watcher

    // Locate hid device with udev
    let mut socket = udev::MonitorBuilder::new()
        .unwrap()
        .match_subsystem(subsystem)
        .unwrap()
        .listen()
        .unwrap();

    // Setup socket polling
    let mut fds = vec![pollfd {
        fd: socket.as_raw_fd(),
        events: POLLIN,
        revents: 0,
    }];

    // Setup ppoll timeout (needed to pump the loop)
    let mut ptimeout = libc::timespec {
        tv_sec: 1,
        tv_nsec: 0,
    };

    // Loop until we find the result
    let start_time = std::time::Instant::now();
    while start_time.elapsed() < timeout {
        // Setup poll
        let result = unsafe {
            ppoll(
                fds[..].as_mut_ptr(),
                fds.len() as nfds_t,
                &mut ptimeout,
                std::ptr::null(),
            )
        };

        if result < 0 {
            panic!("Error: {}", std::io::Error::last_os_error());
        }

        // Read message from socket
        let event = match socket.next() {
            Some(evt) => evt,
            None => {
                std::thread::sleep(std::time::Duration::from_millis(10));
                continue;
            }
        };

        // Validate input uhid device
        if event.event_type() == udev::EventType::Add || event.event_type() == udev::EventType::Bind
        {
            // Locate parent
            if let Some(parent) = event.parent() {
                // Match VID:PID
                let found_vid = parent
                    .attribute_value("id/vendor")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                let found_pid = parent
                    .attribute_value("id/product")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                let found_uniq = parent
                    .attribute_value("uniq")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                if found_vid == format!("{vid:04x}")
                    && found_pid == format!("{pid:04x}")
                    && found_uniq == uniq
                {
                    return Ok(event.device());
                }
            }
        }
    }
    Err(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Could not locate udev device",
    ))
}

/* TODO Move to udev_tokio when possible
/// Use parameters to find a uhid device using udev
/// If we don't find the device right away, start to poll
pub async fn udev_find_device2(
    vid: u16,
    pid: u16,
    subsystem: String,
    uniq: String,
    timeout: std::time::Duration,
) -> Result<tokio_udev::Device, std::io::Error> {
    // First look in the list of devices
    let mut enumerator = tokio_udev::Enumerator::new().unwrap();
    enumerator.match_subsystem("input").unwrap();
    enumerator
        .match_attribute("id/vendor", format!("{:04x}", vhid::IC_VID))
        .unwrap();
    enumerator
        .match_attribute("id/product", format!("{:04x}", vhid::IC_PID_KEYBOARD))
        .unwrap();
    enumerator.match_attribute("uniq", uniq.clone()).unwrap();

    // Validate parameters
    let mut devices = enumerator.scan_devices().unwrap();
    if let Some(device) = devices.next() {
        return Ok(device);
    }

    // Couldn't find, setup a watcher

    // Locate hid device with udev
    let builder = tokio_udev::MonitorBuilder::new()
        .expect("Couldn't create builder")
        .match_subsystem(subsystem)
        .expect("Failed to add subsystem filter");

    // Setup monitor
    let monitor = builder.listen().expect("Couldn't create MonitorSocket");
    monitor.for_each(|event| {
    //tokio::time::timeout(timeout, monitor.for_each(|event| {
        // Validate input uhid device
        if event.event_type() == tokio_udev::EventType::Add || event.event_type() == tokio_udev::EventType::Bind
        {
            // Locate parent
            if let Some(parent) = event.parent() {
                // Match VID:PID
                let found_vid = parent
                    .attribute_value("id/vendor")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                let found_pid = parent
                    .attribute_value("id/product")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                let found_uniq = parent
                    .attribute_value("uniq")
                    .unwrap_or_else(|| std::ffi::OsStr::new(""))
                    .to_str()
                    .unwrap();
                if found_vid == format!("{:04x}", vid)
                    && found_pid == format!("{:04x}", pid)
                    && found_uniq == uniq
                {
                    return Ok(event.device());
                }
            }
        }
    }).await;

    Err(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Could not locate udev device",
    ))
}
*/

// ------- Test Cases -------

#[cfg(test)]
mod test {
    use super::*;
    use crate::device::evdev;
    use crate::logging::setup_logging_lite;
    use std::sync::{Arc, RwLock};

    // This test will fail unless your user has permission to read/write to /dev/uhid
    #[test]
    #[ignore]
    fn uhid_keyboard_nkro_test() {
        setup_logging_lite().ok();
        let name = "uhid-keyboard-nkro-test".to_string();
        let mailbox = mailbox::Mailbox {
            ..Default::default()
        };

        // Adjust next uid to make it easier to debug parallel tests
        *mailbox.last_uid.write().unwrap() = 20;

        // Generate a unique key (to handle parallel tests)
        let uniq = nanoid::nanoid!();

        // Instantiate hid device
        let mut keyboard = KeyboardNkro::new(
            mailbox.clone(),
            name,
            "".to_string(),
            uniq.clone(),
            uhid_virt::Bus::USB,
            vhid::IC_VID as u32,
            vhid::IC_PID_KEYBOARD as u32,
            0,
            0,
        )
        .unwrap();

        // Make sure device is there (will poll for a while just in case uhid/kernel is slow)
        let device = match evdev::udev_find_input_event_device(
            vhid::IC_VID,
            vhid::IC_PID_KEYBOARD,
            "input".to_string(),
            uniq,
            std::time::Duration::new(10, 0),
        ) {
            Ok(device) => device,
            Err(err) => {
                panic!("Could not find udev device... {}", err);
            }
        };

        // Find evdev mapping to uhid device
        while !device.is_initialized() {} // Wait for udev to finish setting up device
        let fd_path = format!("/dev/input/{}", device.sysname().to_str().unwrap());

        // Now that both uhid and evdev nodes are setup we can attempt to send some keypresses to
        // validate that evdev is working correctly
        // However, before we can send any keypresses, a mailbox receiver is setup to watch for the incoming
        // messages
        let mut receiver = mailbox.sender.subscribe(); // Subscribe to mailbox messages

        let rt = tokio::runtime::Runtime::new().unwrap();
        let status: Arc<RwLock<bool>> = Arc::new(RwLock::new(false));
        let status2 = status.clone();

        // Start listening for mailbox messages
        rt.spawn(async move {
            // Looking for this sequence of active event codes
            // All modifiers, plus 11 keys (minimum for nkro)
            let expected_codes = vec![
                225, 226, 227, 228, 229, 230, 231, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
            ];

            loop {
                match receiver.recv().await {
                    Ok(msg) => {
                        // Check to see if the key events were sent in the correct order
                        // We're looking for the full message in order to evaluate true
                        if !*status.clone().read().unwrap() {
                            let mut code_pos = 0;
                            for code in msg.data.data {
                                if code != expected_codes[code_pos] {
                                    error!("{} != {}", code, expected_codes[code_pos]);
                                    break;
                                }
                                code_pos += 1;
                                if code_pos >= expected_codes.len() {
                                    *(status.clone().write().unwrap()) = true;
                                }
                            }
                        }
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                        panic!("Mailbox has been closed unexpectedly!");
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => {
                        panic!(
                            "Mailbox has received too many messages, lagging by: {}",
                            skipped
                        );
                    }
                };
            }
        });

        // Start listening for evdev events
        rt.spawn(async move {
            tokio::task::spawn_blocking(move || {
                evdev::EvdevDevice::new(mailbox.clone(), fd_path)
                    .unwrap()
                    .process()
                    .unwrap();
            });
        });

        rt.block_on(async {
            // Make sure everything is initialized and monitoring
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;

            // Send A;A,B;B key using uhid device
            // TODO integrate layouts-rs from HID-IO (to have symbolic testing inputs)
            // Testing nkro (11 keys + modifiers)
            keyboard
                .send(vec![
                    4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
                    0xE7,
                ])
                .unwrap();
            // XXX (HaaTa): Need to give uhid (and evdev) some time to process the event
            //              Otherwise evdev may decide to just drop the event entirely
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            keyboard.send(vec![]).unwrap();

            // Give some time for the events to propagate
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        });

        // Force the runtime to shutdown
        rt.shutdown_timeout(std::time::Duration::from_millis(100));
        let status: bool = *status2.read().unwrap();
        assert!(status, "Test failed");
    }

    // This test will fail unless your user has permission to read/write to /dev/uhid
    #[test]
    #[ignore]
    fn uhid_keyboard_6kro_test() {
        setup_logging_lite().ok();
        let name = "uhid-keyboard-6kro-test".to_string();
        let mailbox = mailbox::Mailbox {
            ..Default::default()
        };

        // Adjust next uid to make it easier to debug parallel tests
        *mailbox.last_uid.write().unwrap() = 30;

        // Generate a unique key (to handle parallel tests)
        let uniq = nanoid::nanoid!();

        // Instantiate hid device
        let mut keyboard = Keyboard6kro::new(
            mailbox.clone(),
            name,
            "".to_string(),
            uniq.clone(),
            uhid_virt::Bus::USB,
            vhid::IC_VID as u32,
            vhid::IC_PID_KEYBOARD as u32,
            0,
            0,
        )
        .unwrap();

        // Make sure device is there (will poll for a while just in case uhid/kernel is slow)
        let device = match evdev::udev_find_input_event_device(
            vhid::IC_VID,
            vhid::IC_PID_KEYBOARD,
            "input".to_string(),
            uniq,
            std::time::Duration::new(10, 0),
        ) {
            Ok(device) => device,
            Err(err) => {
                panic!("Could not find udev device... {}", err);
            }
        };

        // Find evdev mapping to uhid device
        while !device.is_initialized() {} // Wait for udev to finish setting up device
        let fd_path = format!("/dev/input/{}", device.sysname().to_str().unwrap());

        // Now that both uhid and evdev nodes are setup we can attempt to send some keypresses to
        // validate that evdev is working correctly
        // However, before we can send any keypresses, a mailbox receiver is setup to watch for the incoming
        // messages
        let mut receiver = mailbox.sender.subscribe(); // Subscribe to mailbox messages

        let rt = tokio::runtime::Runtime::new().unwrap();
        let status: Arc<RwLock<bool>> = Arc::new(RwLock::new(false));
        let status2 = status.clone();

        // Start listening for mailbox messages
        rt.spawn(async move {
            // Looking for this sequence of active event codes
            // All modifiers, plus only the first 6 sent key events
            let expected_codes = vec![225, 226, 227, 228, 229, 230, 231, 4, 5, 6, 7, 8, 9];

            loop {
                match receiver.recv().await {
                    Ok(msg) => {
                        // Check to see if the key events were sent in the correct order
                        // We're looking for the full message in order to evaluate true
                        if !*status.clone().read().unwrap() {
                            let mut code_pos = 0;
                            for code in msg.data.data {
                                if code != expected_codes[code_pos] {
                                    error!("{} != {}", code, expected_codes[code_pos]);
                                    break;
                                }
                                code_pos += 1;
                                if code_pos >= expected_codes.len() {
                                    *(status.clone().write().unwrap()) = true;
                                }
                            }
                        }
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                        panic!("Mailbox has been closed unexpectedly!");
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => {
                        panic!(
                            "Mailbox has received too many messages, lagging by: {}",
                            skipped
                        );
                    }
                };
            }
        });

        // Start listening for evdev events
        rt.spawn(async move {
            tokio::task::spawn_blocking(move || {
                evdev::EvdevDevice::new(mailbox.clone(), fd_path)
                    .unwrap()
                    .process()
                    .unwrap();
            });
        });

        rt.block_on(async {
            // Make sure everything is initialized and monitoring
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;

            // Send A;A,B;B key using uhid device
            // TODO integrate layouts-rs from HID-IO (to have symbolic testing inputs)
            // Testing 6kro limit handling
            keyboard
                .send(vec![
                    4, 5, 6, 7, 8, 9, 10, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
                ])
                .unwrap();
            keyboard.send(vec![]).unwrap();

            // Give some time for the events to propagate
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        });

        // Force the runtime to shutdown
        rt.shutdown_timeout(std::time::Duration::from_millis(100));
        let status: bool = *status2.read().unwrap();
        assert!(status, "Test failed");
    }
}