forked from pholat/QtUsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb-container.cpp
96 lines (86 loc) · 2.29 KB
/
usb-container.cpp
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
#include "usb-container.h"
namespace {
static UsbDev deft;
}
UsbContainer::UsbContainer(QString *errorLog) : _error(0), _numOfDev(0), _errorLog(errorLog)
{
_error = usbLibInit();
if(_error < 0) QTextStream(_errorLog) << "Error: Initializing libusb" << endl;
else {
_numOfDev = libusb_get_device_list(_ctx, &_device_list);
if (_numOfDev < 1) {
QTextStream(_errorLog) << "Error: No dev found" << endl;
return;
} else {
for (uint i=0; i<_numOfDev; i++) {
_usbDevices.push_back(UsbDev(_device_list[i],i,errorLog));
}
}
}
}
UsbContainer::~UsbContainer()
{
libusb_exit(_ctx);
}
int UsbContainer::usbLibInit()
{
uint error;
return error = libusb_init(&_ctx);
}
// Lists all devices which are accesible with no root privileges
QStringList UsbContainer::listNonRootDevices()
{
QStringList tmpList;
for(int i=0; i<_usbDevices.size(); i++) {
if(_usbDevices.at(i).isNonSudoDev()) {
tmpList.append(_usbDevices.at(i).getProductString());
}
}
return tmpList;
}
QStringList UsbContainer::getDeviceInfo(int i)
{
QString tmp;
QTextStream(&tmp) << _usbDevices.size();
QStringList tmpList;
tmpList.append(tmp);
QTextStream(&tmp) << i;
tmpList.append(tmp);
return tmpList;
}
QStringList UsbContainer::getDeviceInfo(QString &stringi)
{
QStringList tmp;
size_t size = _usbDevices.size(), i;
for(i=0; i<size ; ++i) {
if(_usbDevices[i].getProductString() == stringi ) {
break;
}
}
if(i == size) {
tmp.append("No such device");
} else {
tmp = _usbDevices[i].devInfo();
}
return tmp;
}
UsbDev *UsbContainer::getDevice( const QString &devInfo ) {
auto iter = _usbDevices.begin();
for ( int i =0; i < _usbDevices.size(); ++i, iter +=1 ) {
if ( iter->getProductString() == devInfo ) {
return iter;
}
}
return &deft;
}
UsbDev *UsbContainer::getDevice( const int num ) {
int nonsudodevnr =0;
auto iter = _usbDevices.begin();
for ( int i =0; i < _usbDevices.size(); ++i, ++iter ) {
if ( iter->isNonSudoDev() ) {
if( num == nonsudodevnr ) return iter;
++nonsudodevnr;
}
}
return &deft;
}