usbconnectionlistener.h
1 #ifndef USBCONNECTIONLISTENER_H
2 #define USBCONNECTIONLISTENER_H
3 
4 #include <QObject>
5 #include <QString>
6 #include <QRunnable>
7 #include <libusb.h>
8 #include <libudev.h>
9 
10 #define ACC_REQ_GET_PROTOCOL 51
11 #define USB_DIR_IN 0x80
12 #define USB_DIR_OUT 0x00
13 #define USB_TYPE_VENDOR 0x40
14 
15 #define VEN_ID_GOOGLE 0x18D1
16 #define DEV_ID_OAP 0x2D00
17 #define DEV_ID_OAP_WITH_ADB 0x2D01
18 
19 enum USB_EVENT_TYPE
20 {
21  USB_CONNECTED = 0,
22  USB_REMOVED = 1,
23  MASS_STORAGE_CONNECTED = 2,
24  ANDROID_DEVICE_CONNECTED = 3,
25 };
26 /*
27  * First run echo 'SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0666"' | sudo tee /lib/udev/rules.d/99-allow-all-usb.rules && sudo udevadm control --reload-rules
28  */
29 class UsbConnectionListener : public QObject,public QRunnable
30 {
31 Q_OBJECT
32 public:
34  virtual ~UsbConnectionListener();
35  virtual void run() override;
36  void stop();
37  void emitSignal(USB_EVENT_TYPE event_type, QString device_descriptor);
38  void emitSignal(USB_EVENT_TYPE event_type, QString device_descriptor, QList<QString> mountList);
39  static bool isAndroidDevice(libusb_device_handle *device);
40  static bool isOapDevice(libusb_device_descriptor *desc);
41  static QList<QString> getMountPoint(QString serialNumber);
42  static bool hasMassStorageInterface(libusb_device *dev);
43 private:
44  bool done = false;
45  static int LIBUSB_CALL hotplugCallback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data);
46  libusb_context *hotplug_context = NULL;
47 signals:
48  void usbDriveConnected(QString device_descriptor);
49  void androidDeviceConnected(QString device_descriptor);
50  void deviceRemoved(QString device_descriptor);
51 };
52 
53 #endif // USBCONNECTIONLISTENER_H
Definition: usbconnectionlistener.h:29