-
Notifications
You must be signed in to change notification settings - Fork 42
/
uhid_device.rs
148 lines (133 loc) · 4.28 KB
/
uhid_device.rs
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
use std::fs::File;
use std::io::{self, Write};
use std::os::unix::io::FromRawFd;
use std::path::Path;
use futures::{Async, AsyncSink, Poll, Sink, StartSend, Stream};
use nix;
use nix::fcntl;
use slog_stdlog;
use slog;
use slog::Drain;
use tokio_core::reactor::Handle;
use tokio_io::AsyncRead;
use poll_evented_read_wrapper::PollEventedRead;
use character_device_file::CharacterDeviceFile;
use character_device::{CharacterDevice, Decoder, Encoder, SyncSink};
use uhid_codec::*;
pub struct UHIDDevice<T> {
inner: CharacterDevice<T, UHIDCodec, UHIDCodec>,
logger: slog::Logger,
}
pub struct CreateParams {
pub name: String,
pub phys: String,
pub uniq: String,
pub bus: Bus,
pub vendor: u32,
pub product: u32,
pub version: u32,
pub country: u32,
pub data: Vec<u8>,
}
// ===== impl UHIDDevice =====
impl UHIDDevice<PollEventedRead<CharacterDeviceFile<File>>> {
pub fn create<L: Into<Option<slog::Logger>>>(
handle: &Handle,
params: CreateParams,
logger: L,
) -> io::Result<UHIDDevice<PollEventedRead<CharacterDeviceFile<File>>>> {
Self::create_with_path(Path::new("/dev/uhid"), handle, params, logger)
}
pub fn create_with_path<L: Into<Option<slog::Logger>>>(
path: &Path,
handle: &Handle,
params: CreateParams,
logger: L,
) -> io::Result<UHIDDevice<PollEventedRead<CharacterDeviceFile<File>>>> {
let fd = fcntl::open(
path,
fcntl::O_RDWR | fcntl::O_CLOEXEC | fcntl::O_NONBLOCK,
nix::sys::stat::S_IRUSR | nix::sys::stat::S_IWUSR | nix::sys::stat::S_IRGRP
| nix::sys::stat::S_IWGRP,
).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Cannot open uhid-cdev {}: {}", path.to_str().unwrap(), err),
)
})?;
let file: File = unsafe { File::from_raw_fd(fd) };
let device_file = CharacterDeviceFile::new(file);
Ok(Self::create_with(
device_file.into_io(handle)?,
params,
logger,
))
}
}
impl<T> UHIDDevice<T>
where
T: AsyncRead + Write,
{
fn create_with<L: Into<Option<slog::Logger>>>(
inner: T,
params: CreateParams,
logger: L,
) -> UHIDDevice<T> {
let logger = logger
.into()
.unwrap_or(slog::Logger::root(slog_stdlog::StdLog.fuse(), o!()));
let mut device = UHIDDevice {
inner: CharacterDevice::new(inner, UHIDCodec, UHIDCodec, logger.new(o!())),
logger: logger,
};
trace!(device.logger, "Send create device event");
device
.inner
.send(InputEvent::Create {
name: params.name,
phys: params.phys,
uniq: params.uniq,
bus: params.bus,
vendor: params.vendor,
product: params.product,
version: params.version,
country: params.country,
data: params.data,
})
.unwrap();
trace!(device.logger, "Sent create device event");
device
}
pub fn send_input(&mut self, data: &[u8]) -> Result<(), <UHIDCodec as Encoder>::Error> {
trace!(self.logger, "Send input event");
self.inner.send(InputEvent::Input {
data: data.to_vec(),
})
}
pub fn destory(mut self) -> Result<(), <UHIDCodec as Encoder>::Error> {
self.inner.send(InputEvent::Destroy)?;
self.inner.close()?;
Ok(())
}
}
impl<T: AsyncRead> Stream for UHIDDevice<T> {
type Item = <UHIDCodec as Decoder>::Item;
type Error = <UHIDCodec as Decoder>::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.inner.poll()
}
}
impl<T: Write> Sink for UHIDDevice<T> {
type SinkItem = <UHIDCodec as Encoder>::Item;
type SinkError = <UHIDCodec as Encoder>::Error;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
trace!(self.logger, "start_send");
self.inner.send(item)?;
Ok(AsyncSink::Ready)
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
trace!(self.logger, "poll_complete");
self.inner.flush()?;
Ok(Async::Ready(()))
}
}