tokio_linux_uhid/lib.rs
1//! # Tokio UHID
2//!
3//! A safe wrapper around the userspace API to the HID subsystem in the linux kernel.
4//!
5//! ## Example
6//! ```rust,no_run
7//!# extern crate futures;
8//!# extern crate tokio_linux_uhid;
9//!# extern crate tokio_core;
10//!
11//! use tokio_linux_uhid::{Bus, CreateParams, UHIDDevice};
12//!
13//! // Formulate a 'HID Report Descriptor' to describe the function of your device.
14//! // This tells the kernel how to interpret the HID packets you send to the device.
15//! const RDESC: [u8; 85] = [
16//! 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
17//! 0x09, 0x02, /* USAGE (Mouse) */
18//! 0xa1, 0x01, /* COLLECTION (Application) */
19//! 0x09, 0x01, /* USAGE (Pointer) */
20//! 0xa1, 0x00, /* COLLECTION (Physical) */
21//! 0x85, 0x01, /* REPORT_ID (1) */
22//! 0x05, 0x09, /* USAGE_PAGE (Button) */
23//! 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
24//! 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
25//! 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
26//! 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
27//! 0x95, 0x03, /* REPORT_COUNT (3) */
28//! 0x75, 0x01, /* REPORT_SIZE (1) */
29//! 0x81, 0x02, /* INPUT (Data,Var,Abs) */
30//! 0x95, 0x01, /* REPORT_COUNT (1) */
31//! 0x75, 0x05, /* REPORT_SIZE (5) */
32//! 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */
33//! 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
34//! 0x09, 0x30, /* USAGE (X) */
35//! 0x09, 0x31, /* USAGE (Y) */
36//! 0x09, 0x38, /* USAGE (WHEEL) */
37//! 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
38//! 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
39//! 0x75, 0x08, /* REPORT_SIZE (8) */
40//! 0x95, 0x03, /* REPORT_COUNT (3) */
41//! 0x81, 0x06, /* INPUT (Data,Var,Rel) */
42//! 0xc0, /* END_COLLECTION */
43//! 0xc0, /* END_COLLECTION */
44//! 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
45//! 0x09, 0x06, /* USAGE (Keyboard) */
46//! 0xa1, 0x01, /* COLLECTION (Application) */
47//! 0x85, 0x02, /* REPORT_ID (2) */
48//! 0x05, 0x08, /* USAGE_PAGE (Led) */
49//! 0x19, 0x01, /* USAGE_MINIMUM (1) */
50//! 0x29, 0x03, /* USAGE_MAXIMUM (3) */
51//! 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
52//! 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
53//! 0x95, 0x03, /* REPORT_COUNT (3) */
54//! 0x75, 0x01, /* REPORT_SIZE (1) */
55//! 0x91, 0x02, /* Output (Data,Var,Abs) */
56//! 0x95, 0x01, /* REPORT_COUNT (1) */
57//! 0x75, 0x05, /* REPORT_SIZE (5) */
58//! 0x91, 0x01, /* Output (Cnst,Var,Abs) */
59//! 0xc0, /* END_COLLECTION */
60//! ];
61//!
62//! fn main() {
63//! // All the parameters used to create the HID device
64//! let create_params = CreateParams {
65//! name: String::from("test-uhid-device"),
66//! phys: String::from(""),
67//! uniq: String::from(""),
68//! bus: Bus::USB,
69//! vendor: 0x15d9,
70//! product: 0x0a37,
71//! version: 0,
72//! country: 0,
73//! // Most inportant field - HID Report Descriptor
74//! data: RDESC.to_vec(),
75//! };
76//!
77//! let core = tokio_core::reactor::Core::new().unwrap();
78//! let handle = core.handle();
79//! // Give the UHID device a handle to the tokio event loop and the create parameters
80//! let mut uhid_device = UHIDDevice::create(&handle, create_params, None).unwrap();
81//!
82//! // Formulate a HID Packet
83//! let button_flags = 0;
84//! let mouse_abs_hor = 20;
85//! let mouse_abs_ver = 0;
86//! let wheel = 0;
87//! let data: [u8; 5] = [1, button_flags, mouse_abs_hor, mouse_abs_ver, wheel];
88//!
89//! // Send the HID packet to the device. Cursor should move 20 points to the right.
90//! uhid_device.send_input(&data).unwrap();
91//! }
92//! ```
93#[macro_use]
94extern crate bitflags;
95#[macro_use]
96extern crate slog;
97#[macro_use]
98extern crate quick_error;
99
100extern crate bytes;
101extern crate futures;
102extern crate mio;
103extern crate nix;
104extern crate slog_stdlog;
105extern crate tokio_core;
106extern crate tokio_io;
107extern crate uhid_sys;
108
109mod character_device_file;
110mod character_device;
111mod uhid_codec;
112mod uhid_device;
113mod poll_evented_read_wrapper;
114
115pub use uhid_device::UHIDDevice;
116pub use uhid_device::CreateParams;
117pub use uhid_codec::{Bus, InputEvent, OutputEvent, StreamError};