1 |
douglas |
1 |
// Steering Wheel Remote |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include <cassert> |
8 |
|
|
#include <iostream> |
9 |
douglas |
2 |
#include <set> |
10 |
douglas |
1 |
#include <sstream> |
11 |
|
|
#include <string> |
12 |
|
|
|
13 |
|
|
#include <dev/usb/usb.h> |
14 |
|
|
#include <dev/usb/usbhid.h> |
15 |
douglas |
2 |
#include <err.h> |
16 |
douglas |
1 |
#include <fcntl.h> |
17 |
|
|
#include <sys/ioctl.h> |
18 |
|
|
#include <usbhid.h> |
19 |
|
|
|
20 |
douglas |
2 |
#include <foreach.hpp> |
21 |
|
|
#include <posix.hpp> |
22 |
|
|
|
23 |
|
|
inline std::ostream &operator <<(std::ostream &output, uint8_t number) |
24 |
douglas |
1 |
{ |
25 |
douglas |
2 |
return output << unsigned(number); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
std::set<std::string> AirClicks() |
29 |
|
|
{ |
30 |
|
|
std::set<std::string> airclicks; |
31 |
|
|
|
32 |
|
|
_forall (unsigned, number, 0, 10) |
33 |
douglas |
1 |
{ |
34 |
douglas |
2 |
std::ostringstream device_; |
35 |
douglas |
1 |
|
36 |
douglas |
2 |
device_ << "/dev/usb" << number; |
37 |
douglas |
1 |
|
38 |
douglas |
2 |
const std::string &device(device_.str()); |
39 |
douglas |
1 |
|
40 |
douglas |
2 |
try |
41 |
|
|
{ |
42 |
|
|
int usb(Posix::CheckError(::open(device.c_str(), O_RDONLY))); |
43 |
douglas |
1 |
|
44 |
douglas |
2 |
_forall (uint8_t, address, 1, USB_MAX_DEVICES) |
45 |
|
|
{ |
46 |
|
|
usb_device_info info; |
47 |
douglas |
1 |
|
48 |
douglas |
2 |
info.udi_addr = address; |
49 |
douglas |
1 |
|
50 |
douglas |
2 |
try |
51 |
|
|
{ |
52 |
|
|
Posix::CheckError(::ioctl(usb, USB_DEVICEINFO, &info)); |
53 |
|
|
} |
54 |
|
|
catch (const Posix::Error &error) |
55 |
|
|
{ |
56 |
|
|
if (error.GetCode() != ENXIO) |
57 |
|
|
::warn("%s: address %u", device.c_str(), address); |
58 |
douglas |
1 |
|
59 |
douglas |
2 |
continue; |
60 |
|
|
} |
61 |
douglas |
1 |
|
62 |
douglas |
2 |
std::cout << info.udi_product << std::endl; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
Posix::CheckError(::close(usb)); |
66 |
|
|
} |
67 |
|
|
catch (const Posix::Error &error) |
68 |
|
|
{ |
69 |
|
|
switch (error.GetCode()) |
70 |
|
|
{ |
71 |
|
|
case ENOENT: |
72 |
|
|
case ENXIO: |
73 |
|
|
continue; |
74 |
|
|
default: |
75 |
|
|
::warn("%s", device.c_str()); |
76 |
|
|
} |
77 |
|
|
} |
78 |
douglas |
1 |
} |
79 |
|
|
|
80 |
douglas |
2 |
return airclicks; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
int main(int argc, char *argv[]) |
84 |
|
|
{ |
85 |
|
|
std::set<std::string> airclicks(AirClicks()); |
86 |
|
|
|
87 |
douglas |
1 |
return 0; |
88 |
|
|
} |