-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhidp_device.cc
More file actions
193 lines (161 loc) · 5.12 KB
/
hidp_device.cc
File metadata and controls
193 lines (161 loc) · 5.12 KB
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
#include "hidp_common.h"
#include "hidp_device.h"
#include <linux/hidraw.h>
#if WITH_UUID
#include <linux/uhid.h>
#else
#include <stdio.h>
#endif
#include <libudev.h>
#include <uuid/uuid.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#ifdef WITH_INPUTDEVICE_MONITOR
static int16_t NextChannelId = 1000;
#endif
InputDevice::InputDevice()
: m_channel_id(-1)
, m_fd(-1)
, m_descriptor_size(0)
, m_bus_type(0)
, m_vendor_id(0)
, m_product_id(0)
, m_input_report_size(0)
{
m_name[0] = '\0';
char temp[256] = {};
#if WITH_UUID
uuid_t id;
uuid_generate_random(id);
uuid_unparse_lower(id, temp);
#else
FILE *uuid_file = fopen("/proc/sys/kernel/random/uuid", "r");
if (uuid_file) {
fread(temp, sizeof(temp), 1, uuid_file);
fclose(uuid_file);
}
else {
// TODO
}
#endif
m_uuid = temp;
}
#ifdef WITH_INPUTDEVICE_MONITOR
InputDeviceMonitor::InputDeviceMonitor()
: m_fd(-1)
, m_udev_monitor(nullptr)
, m_udev(nullptr)
{
m_udev = udev_new();
m_udev_monitor = udev_monitor_new_from_netlink(m_udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(m_udev_monitor, "hidraw", nullptr);
udev_monitor_enable_receiving(m_udev_monitor);
m_fd = udev_monitor_get_fd(m_udev_monitor);
}
InputDeviceMonitor::~InputDeviceMonitor()
{
if (m_udev)
udev_unref(m_udev);
}
void
InputDeviceMonitor::ReadNext(InputDeviceAdded on_device_added, InputDeviceRemoved on_device_removed)
{
udev_device *dev = udev_monitor_receive_device(m_udev_monitor);
if (!dev) {
return;
}
const char *action = udev_device_get_action(dev);
XLOG_INFO("InputDeviceMonitor triggered with action '%s'", action);
if (strcasecmp(action, "remove") == 0) {
const char *syspath = udev_device_get_syspath(dev);
XLOG_INFO("remove device '%s'", syspath);
if (syspath) {
auto itr = m_device_ids.find(syspath);
if (itr != std::end(m_device_ids)) {
if (on_device_removed)
on_device_removed(itr->second);
m_device_ids.erase(itr);
}
}
}
if (strcasecmp(action, "add") == 0) {
std::unique_ptr<InputDevice> new_device = InputDeviceMonitor::FromDevice(dev);
const char *syspath = udev_device_get_syspath(dev);
m_device_ids.insert(std::make_pair(syspath, new_device->GetId()));
XLOG_INFO("insert device '%s' -> '%s'", syspath, new_device->GetId().c_str());
if (on_device_added)
on_device_added(std::move(new_device));
}
udev_device_unref(dev);
}
std::vector<std::unique_ptr<InputDevice>> InputDeviceMonitor::FindAll()
{
std::vector<std::unique_ptr<InputDevice>> devices;
udev_enumerate *udev_enum = udev_enumerate_new(m_udev);
udev_enumerate_add_match_subsystem(udev_enum, "hidraw");
udev_enumerate_scan_devices(udev_enum);
udev_list_entry *device_list = udev_enumerate_get_list_entry(udev_enum);
for (udev_list_entry *iter = device_list; iter; iter = udev_list_entry_get_next(iter)) {
const char *sysfs_path = udev_list_entry_get_name(iter);
udev_device *raw_dev = udev_device_new_from_syspath(m_udev, sysfs_path);
std::unique_ptr<InputDevice> new_device = InputDeviceMonitor::FromDevice(raw_dev);
XLOG_INFO("discoverd new device (%s) at: %s", new_device->GetId().c_str(), sysfs_path);
devices.push_back(std::move(new_device));
udev_device_unref(raw_dev);
}
udev_enumerate_unref(udev_enum);
return devices;
}
std::unique_ptr<InputDevice> InputDeviceMonitor::FromDevice(udev_device *dev)
{
std::unique_ptr<InputDevice> new_device{ new InputDevice() };
new_device->m_channel_id = NextChannelId++;
const char *device_node = udev_device_get_devnode(dev);
XLOG_INFO("creating new input device from:%s", device_node);
new_device->m_fd = open(device_node, O_RDWR);
if (new_device->m_fd == -1)
hidp_throw_errno(errno, "failed to open device %s", device_node);
hidraw_devinfo info;
int ret = ioctl(new_device->m_fd, HIDIOCGRAWINFO, &info);
if (ret == -1)
hidp_throw_errno(errno, "failed to read device info");
new_device->m_bus_type = info.bustype;
new_device->m_vendor_id = info.vendor;
new_device->m_product_id = info.product;
hidraw_report_descriptor desc;
ret = ioctl(new_device->m_fd, HIDIOCGRDESCSIZE, &desc.size);
if (ret == -1)
hidp_throw_errno(errno, "failed to read descriptor size");
new_device->m_descriptor_size = desc.size;
ret = ioctl(new_device->m_fd, HIDIOCGRDESC, &desc);
if (ret == -1)
hidp_throw_errno(errno, "failed to read descriptor");
memcpy(new_device->m_descriptor, desc.value, new_device->m_descriptor_size);
ret = ioctl(new_device->m_fd, HIDIOCGRAWNAME(255), &new_device->m_name[0]);
if (ret == -1)
hidp_throw_errno(errno, "failed to read name");
return new_device;
}
#endif
int
InputDevice::ReadInputReport()
{
ssize_t n = read(m_fd, m_input_report, sizeof(m_input_report));
if (n > 0)
m_input_report_size = static_cast<int16_t>(n);
else {
int err = errno;
XLOG_INFO("error reading input report for '%s'. %s", m_name, strerror(err));
close(m_fd);
m_fd = -1;
return -err;
}
return m_input_report_size;
}
std::string InputDevice::GetName() const
{
return std::string(m_name);
}