Repository navigation
Expand file tree
/
Copy pathusbconnection.cpp
More file actions
156 lines (127 loc) · 4.85 KB
/
Copy pathusbconnection.cpp
File metadata and controls
156 lines (127 loc) · 4.85 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
#include <QDebug>
#include <iostream>
#include <QTime>
#include <QCoreApplication>
#include "dfuexplorer.h"
#include "usbconnection.h"
#include "libusb-1.0/libusb.h"
void USBConnection::delay(int count){
QTime dieTime= QTime::currentTime().addMSecs(count);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
bool USBConnection::isDFU(){
/* This enumerates all of the interfaces of the DFU configuration,
* looking for a DFU interface to use for the connection.
*/
for(configuration=0; configuration<desc.bNumConfigurations; configuration++){
//qDebug()<<"Configuration "<<configuration;
libusb_get_config_descriptor(dev, configuration, &cdesc);
for(interface=0; interface<cdesc->bNumInterfaces; interface++){
//qDebug()<<"Interface "<<interface<<"has"<<
// cdesc->interface[interface].num_altsetting<<"alt settings.";
for(int altsetting=0;
altsetting<cdesc->interface[interface].num_altsetting;
altsetting++){
auto alt=cdesc->interface[interface].altsetting[altsetting];
//FIXME: Do we need to check the protocol number too?
if(alt.bInterfaceClass==254 && alt.bInterfaceSubClass==1){
if(dfuexplorer_verbose){
qDebug()<<"Found DFU endpoint with "<<alt.bNumEndpoints<<"endpoints";
qDebug()<<"Interface number "<<alt.bInterfaceNumber;
qDebug()<<"Descriptor type"<<alt.bDescriptorType;
qDebug()<<"Extra Length:" <<alt.extra_length;
}
if(alt.bInterfaceNumber!=0)
qDebug()<<"Weird, the interface number is"<<alt.bInterfaceNumber;
interface=alt.bInterfaceNumber;
if(dfuexplorer_verbose)
qDebug()<<"Extra length is "<<alt.extra_length;
if(alt.extra_length==9){
uint8_t* extra=(uint8_t*)alt.extra;
assert(alt.extra_length==9);
//qDebug()<<"Extra type"<<extra[1];
assert(extra[0]==9);
assert(extra[1]==0x21);
memcpy(&dfudesc, alt.extra, 9);
if(dfuexplorer_verbose){
qDebug()<<Qt::hex<<extra[0]<<extra[1]<<extra[2]<<extra[3]
<<extra[4]<<extra[5]<<extra[6]<<extra[7]
<<extra[8];
qDebug()<<"Length is "<<dfudesc.bLength;
qDebug()<<"Attributes are "<<dfudesc.bmAttributes;
qDebug()<<"Timeout is "<<dfudesc.wDetachTimeOut;
qDebug()<<"Transfer size is "<<dfudesc.wTransferSize<<"BCD is "<<dfudesc.bcdDFUVersion;
}
return true;
}
}
}
}
}
return false;
}
void USBConnection::printAllStrings(){
QString s="", lasts="";
for(int i=0; i<256; i++){
//for(int j=0; j<256; j++){
s=getString(i/*, j*/);
if(lasts!=s)
qDebug()<<s;
lasts=s;
//}
}
}
USBConnection::USBConnection(libusb_device *d) {
this->dev=d;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
std::cout<<"failed to get device descriptor\n";
return;
}
bus_number=libusb_get_bus_number(dev);
device_address=libusb_get_device_address(dev);
/* FIXME: We need to handle the port numbers.
r = libusb_get_port_numbers(dev, path, sizeof(path));
if (r > 0) {
printf(" path: %d", path[0]);
for (j = 1; j < r; j++)
printf(".%d", path[j]);
}
printf("\n");
*/
}
int USBConnection::connect(){
//Are we already connected?
if(handle) return 1;
//Make a new connection.
int e=libusb_open(this->dev, &handle);
//Success!
if(!e){
return 1;
}
//Failure.
handle=0;
return 0;
}
int USBConnection::disconnect(){
handle=0;
return 1;
}
QString USBConnection::getString(int id, int lang){
if(connect()){
unsigned char str[1024];
//int i=libusb_get_string_descriptor(handle, desc.iProduct, 0, str, 255);
int i=libusb_get_string_descriptor_ascii(handle, id, str, 255);
if(i>0)
return QString::asprintf("%s", str);
return "";
}
return "";
}
QString USBConnection::listing(){
return QString::asprintf("Bus %03d Device %03d: ID %04x:%04x ",
bus_number, device_address,
desc.idVendor, desc.idProduct
)+getString(desc.iProduct);
}