4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <cassert> |
7 |
|
#include <iostream> |
9 |
– |
#include <sstream> |
8 |
|
#include <string> |
9 |
|
|
10 |
< |
#include <dev/usb/usb.h> |
13 |
< |
#include <dev/usb/usbhid.h> |
10 |
> |
#include <err.h> |
11 |
|
#include <fcntl.h> |
12 |
|
#include <sys/ioctl.h> |
16 |
– |
#include <usbhid.h> |
13 |
|
|
14 |
< |
int main(int argc, char *argv[]) |
15 |
< |
{ |
16 |
< |
for (unsigned number(0); true; ++number) |
17 |
< |
{ |
18 |
< |
std::ostringstream device; |
14 |
> |
#include <common.hpp> |
15 |
> |
#include <fdstream.hpp> |
16 |
> |
#include <foreach.hpp> |
17 |
> |
#include <posix.hpp> |
18 |
> |
#include <regex.hpp> |
19 |
|
|
20 |
< |
device << "/dev/usb" << number; |
20 |
> |
int Usage(const std::string &program) |
21 |
> |
{ |
22 |
> |
std::cout << _B("Usage: ") << program << _B(" [-debug] [-device=device]") << std::endl; |
23 |
|
|
24 |
< |
int usb(::open(device.str().c_str(), O_RDONLY)); |
24 |
> |
return 1; |
25 |
> |
} |
26 |
|
|
27 |
< |
if (usb == -1) |
28 |
< |
break; |
27 |
> |
int main(int argc, char *argv[]) |
28 |
> |
{ |
29 |
> |
bool debug(false); |
30 |
> |
std::string device; |
31 |
|
|
32 |
< |
usb_device_info info; |
32 |
> |
{ |
33 |
> |
Pcre::RegEx devicePath(_B("^-device=(/.+)$")); |
34 |
> |
Pcre::RegEx deviceNumber(_B("^-device=([0-9]+)$")); |
35 |
> |
Pcre::RegEx deviceName(_B("^-device=(.+)$")); |
36 |
> |
|
37 |
> |
_forall (int, index, 1, argc) |
38 |
> |
if (argv[index] == _B("-debug")) |
39 |
> |
debug = true; |
40 |
> |
else if (Pcre::RegEx::Match match = devicePath(argv[index])) |
41 |
> |
device = match[1]; |
42 |
> |
else if (Pcre::RegEx::Match match = deviceNumber(argv[index])) |
43 |
> |
device = _B("/dev/uhid") + match[1]; |
44 |
> |
else if (Pcre::RegEx::Match match = deviceName(argv[index])) |
45 |
> |
device = _B("/dev/") + match[1]; |
46 |
> |
else |
47 |
> |
return Usage(argv[0]); |
48 |
> |
} |
49 |
|
|
50 |
< |
info.udi_addr = 1; |
50 |
> |
if (device.empty()) |
51 |
> |
return Usage(argv[0]); |
52 |
|
|
53 |
< |
assert(::ioctl(usb, USB_DEVICEINFO, &info) != -1); |
53 |
> |
// if (!debug) |
54 |
> |
// Posix::CheckError(::daemon(0, 0)); |
55 |
|
|
56 |
< |
for (unsigned port(0); port != info.udi_nports; ++port) |
57 |
< |
std::cerr << unsigned(info.udi_ports[port]) << std::endl; |
56 |
> |
ext::ifdstream uhid(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
57 |
> |
char buffer[8]; |
58 |
|
|
59 |
< |
::close(usb); |
60 |
< |
} |
59 |
> |
while (uhid.read(buffer, sizeof (buffer))) |
60 |
> |
_forall (int, index, 0, 8) |
61 |
> |
std::printf("0x%08x 0x%08x\n", buffer[3], ~buffer[4] - 0x4); |
62 |
|
|
63 |
|
return 0; |
64 |
|
} |