-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdevThread.h
75 lines (62 loc) · 2.52 KB
/
UdevThread.h
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
#ifndef UDEVTHREAD_H
#define UDEVTHREAD_H
#include <libudev.h>
#include <QThread>
class UdevThread : public QThread
{
Q_OBJECT
void run() {
struct udev *udev;
struct udev_device *dev;
udev = udev_new();
if (!udev) {
qInfo("Can't create udev\n");
exit(1);
}
// Set up a monitor to monitor hidraw devices
struct udev_monitor *mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
udev_monitor_enable_receiving(mon);
while (1) {
int fd = udev_monitor_get_fd(mon);
fd_set fds;
struct timeval tv;
int ret;
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(fd+1, &fds, NULL, NULL, &tv);
// Check if our file descriptor has received data.
if (ret > 0 && FD_ISSET(fd, &fds)) {
//qInfo("\nselect() says there should be data\n");
// Make the call to receive the device. select() ensured that this will not block.
dev = udev_monitor_receive_device(mon);
if (dev) {
/* qInfo("Got Device\n");
qInfo(" Node: %s\n", udev_device_get_devnode(dev));
qInfo(" Subsystem: %s\n", udev_device_get_subsystem(dev));
qInfo(" Devtype: %s\n", udev_device_get_devtype(dev));
qInfo(" Action: %s\n",udev_device_get_action(dev));
*/
QString udevAction(udev_device_get_action(dev));
QString udevNode(udev_device_get_devnode(dev));
if (strcmp(udev_device_get_devtype(dev), "disk") == 0 && strstr(udev_device_get_devnode(dev), "sd") != NULL ) {
emit resultReady(udevAction, udevNode + " " + udev_device_get_property_value(dev, "ID_MODEL"));
//qInfo("%s\n",udev_device_get_property_value(dev, "ID_MODEL"));
}
udev_device_unref(dev);
}
else {
qInfo("No Device from receive_device(). An error occured.\n");
}
}
usleep(250*100);
}
// Free the enumerator object
udev_unref(udev);
}
signals:
void resultReady(const QString &action, const QString &devnode);
};
#endif // UDEVTHREAD_H