Skip to content

Commit 704ed41

Browse files
committed
usb: Improve error reporting
1 parent 183a7d6 commit 704ed41

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

examples/protonect/src/command_transaction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include <stdint.h>
3131

32+
#define WRITE_LIBUSB_ERROR(__RESULT) libusb_error_name(__RESULT) << " " << libusb_strerror((libusb_error)__RESULT)
33+
3234
namespace libfreenect2
3335
{
3436
namespace protocol
@@ -135,7 +137,7 @@ CommandTransaction::ResultCode CommandTransaction::send(const CommandBase& comma
135137

136138
if(r != LIBUSB_SUCCESS)
137139
{
138-
LOG_ERROR << "bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r);
140+
LOG_ERROR << "bulk transfer failed: " << WRITE_LIBUSB_ERROR(r);
139141
code = Error;
140142
}
141143

@@ -157,7 +159,7 @@ void CommandTransaction::receive(CommandTransaction::Result& result)
157159

158160
if(r != LIBUSB_SUCCESS)
159161
{
160-
LOG_ERROR << "bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r);
162+
LOG_ERROR << "bulk transfer failed: " << WRITE_LIBUSB_ERROR(r);
161163
result.code = Error;
162164
}
163165
}

examples/protonect/src/libfreenect2.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <vector>
2929
#include <algorithm>
3030
#include <libusb.h>
31+
#define WRITE_LIBUSB_ERROR(__RESULT) libusb_error_name(__RESULT) << " " << libusb_strerror((libusb_error)__RESULT)
3132

3233
#include <libfreenect2/libfreenect2.hpp>
3334

@@ -101,13 +102,16 @@ class Freenect2DeviceImpl : public Freenect2Device
101102
struct PrintBusAndDevice
102103
{
103104
libusb_device *dev_;
105+
int status_;
104106

105-
PrintBusAndDevice(libusb_device *dev) : dev_(dev) {}
107+
PrintBusAndDevice(libusb_device *dev, int status = 0) : dev_(dev), status_(status) {}
106108
};
107109

108110
std::ostream &operator<<(std::ostream &out, const PrintBusAndDevice& dev)
109111
{
110112
out << "@" << int(libusb_get_bus_number(dev.dev_)) << ":" << int(libusb_get_device_address(dev.dev_));
113+
if (dev.status_)
114+
out << " " << WRITE_LIBUSB_ERROR(dev.status_);
111115
return out;
112116
}
113117

@@ -130,26 +134,39 @@ class Freenect2Impl
130134
UsbDeviceVector enumerated_devices_;
131135
DeviceVector devices_;
132136

137+
bool initialized;
138+
133139
Freenect2Impl(void *usb_context) :
134140
managed_usb_context_(usb_context == 0),
135141
usb_context_(reinterpret_cast<libusb_context *>(usb_context)),
142+
initialized(false),
136143
has_device_enumeration_(false)
137144
{
145+
if (libusb_get_version()->nano < 10952)
146+
{
147+
LOG_ERROR << "Your libusb does not support large iso buffer!";
148+
return;
149+
}
150+
138151
if(managed_usb_context_)
139152
{
140153
int r = libusb_init(&usb_context_);
141-
// TODO: error handling
142154
if(r != 0)
143155
{
144-
LOG_ERROR << "failed to create usb context!";
156+
LOG_ERROR << "failed to create usb context: " << WRITE_LIBUSB_ERROR(r);
157+
return;
145158
}
146159
}
147160

148161
usb_event_loop_.start(usb_context_);
162+
initialized = true;
149163
}
150164

151165
~Freenect2Impl()
152166
{
167+
if (!initialized)
168+
return;
169+
153170
clearDevices();
154171
clearDeviceEnumeration();
155172

@@ -165,11 +182,17 @@ class Freenect2Impl
165182

166183
void addDevice(Freenect2DeviceImpl *device)
167184
{
185+
if (!initialized)
186+
return;
187+
168188
devices_.push_back(device);
169189
}
170190

171191
void removeDevice(Freenect2DeviceImpl *device)
172192
{
193+
if (!initialized)
194+
return;
195+
173196
DeviceVector::iterator it = std::find(devices_.begin(), devices_.end(), device);
174197

175198
if(it != devices_.end())
@@ -184,6 +207,9 @@ class Freenect2Impl
184207

185208
bool tryGetDevice(libusb_device *usb_device, Freenect2DeviceImpl **device)
186209
{
210+
if (!initialized)
211+
return false;
212+
187213
for(DeviceVector::iterator it = devices_.begin(); it != devices_.end(); ++it)
188214
{
189215
if((*it)->isSameUsbDevice(usb_device))
@@ -198,6 +224,9 @@ class Freenect2Impl
198224

199225
void clearDevices()
200226
{
227+
if (!initialized)
228+
return;
229+
201230
DeviceVector devices(devices_.begin(), devices_.end());
202231

203232
for(DeviceVector::iterator it = devices.begin(); it != devices.end(); ++it)
@@ -213,6 +242,9 @@ class Freenect2Impl
213242

214243
void clearDeviceEnumeration()
215244
{
245+
if (!initialized)
246+
return;
247+
216248
// free enumerated device pointers, this should not affect opened devices
217249
for(UsbDeviceVector::iterator it = enumerated_devices_.begin(); it != enumerated_devices_.end(); ++it)
218250
{
@@ -225,6 +257,9 @@ class Freenect2Impl
225257

226258
void enumerateDevices()
227259
{
260+
if (!initialized)
261+
return;
262+
228263
LOG_INFO << "enumerating devices...";
229264
libusb_device **device_list;
230265
int num_devices = libusb_get_device_list(usb_context_, &device_list);
@@ -277,14 +312,14 @@ class Freenect2Impl
277312
}
278313
else
279314
{
280-
LOG_ERROR << "failed to get serial number of Kinect v2 " << PrintBusAndDevice(dev) << "!";
315+
LOG_ERROR << "failed to get serial number of Kinect v2: " << PrintBusAndDevice(dev, r);
281316
}
282317

283318
libusb_close(dev_handle);
284319
}
285320
else
286321
{
287-
LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev) << "!";
322+
LOG_ERROR << "failed to open Kinect v2: " << PrintBusAndDevice(dev, r);
288323
}
289324
}
290325
}
@@ -300,6 +335,9 @@ class Freenect2Impl
300335

301336
int getNumDevices()
302337
{
338+
if (!initialized)
339+
return 0;
340+
303341
if(!has_device_enumeration_)
304342
{
305343
enumerateDevices();
@@ -654,6 +692,9 @@ int Freenect2::enumerateDevices()
654692

655693
std::string Freenect2::getDeviceSerialNumber(int idx)
656694
{
695+
if (!impl_->initialized)
696+
return std::string();
697+
657698
return impl_->enumerated_devices_[idx].serial;
658699
}
659700

@@ -701,7 +742,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
701742

702743
if(r != LIBUSB_SUCCESS)
703744
{
704-
LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!";
745+
LOG_ERROR << "failed to open Kinect v2: " << PrintBusAndDevice(dev.dev, r);
705746
delete pipeline;
706747

707748
return device;
@@ -740,7 +781,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
740781
}
741782
else if(r != LIBUSB_SUCCESS)
742783
{
743-
LOG_ERROR << "failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!";
784+
LOG_ERROR << "failed to reset Kinect v2: " << PrintBusAndDevice(dev.dev, r);
744785
delete pipeline;
745786

746787
return device;
@@ -755,7 +796,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
755796
delete device;
756797
device = 0;
757798

758-
LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!";
799+
LOG_ERROR << "failed to open Kinect v2: " << PrintBusAndDevice(dev.dev);
759800
}
760801

761802
return device;

examples/protonect/src/transfer_pool.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <libfreenect2/usb/transfer_pool.h>
2828
#include <libfreenect2/logging.h>
2929

30+
#define WRITE_LIBUSB_ERROR(__RESULT) libusb_error_name(__RESULT) << " " << libusb_strerror((libusb_error)__RESULT)
31+
3032
namespace libfreenect2
3133
{
3234
namespace usb
@@ -84,8 +86,10 @@ void TransferPool::submit(size_t num_parallel_transfers)
8486
if(transfers_.size() < num_parallel_transfers)
8587
{
8688
LOG_ERROR << "too few idle transfers!";
89+
return;
8790
}
8891

92+
size_t failcount = 0;
8993
for(size_t i = 0; i < num_parallel_transfers; ++i)
9094
{
9195
libusb_transfer *transfer = transfers_[i].transfer;
@@ -95,10 +99,14 @@ void TransferPool::submit(size_t num_parallel_transfers)
9599

96100
if(r != LIBUSB_SUCCESS)
97101
{
98-
LOG_ERROR << "failed to submit transfer: " << libusb_error_name(r);
102+
LOG_ERROR << "failed to submit transfer: " << WRITE_LIBUSB_ERROR(r);
99103
transfers_[i].setStopped(true);
104+
failcount++;
100105
}
101106
}
107+
108+
if (failcount == num_parallel_transfers)
109+
LOG_ERROR << "all submissions failed. Try debugging with environment variable: LIBUSB_DEBUG=4.";
102110
}
103111

104112
void TransferPool::cancel()
@@ -109,7 +117,7 @@ void TransferPool::cancel()
109117

110118
if(r != LIBUSB_SUCCESS && r != LIBUSB_ERROR_NOT_FOUND)
111119
{
112-
LOG_ERROR << "failed to cancel transfer: " << libusb_error_name(r);
120+
LOG_ERROR << "failed to cancel transfer: " << WRITE_LIBUSB_ERROR(r);
113121
}
114122
}
115123

@@ -186,7 +194,7 @@ void TransferPool::onTransferComplete(TransferPool::Transfer* t)
186194

187195
if(r != LIBUSB_SUCCESS)
188196
{
189-
LOG_ERROR << "failed to submit transfer: " << libusb_error_name(r);
197+
LOG_ERROR << "failed to submit transfer: " << WRITE_LIBUSB_ERROR(r);
190198
t->setStopped(true);
191199
}
192200
}

examples/protonect/src/usb_control.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ UsbControl::~UsbControl()
188188
}
189189

190190
#define CHECK_LIBUSB_RESULT(__CODE, __RESULT) if((__CODE = (__RESULT == LIBUSB_SUCCESS ? Success : Error)) == Error) LOG_ERROR
191-
#define WRITE_LIBUSB_ERROR(__RESULT) "libusb error " << __RESULT << ": " << libusb_error_name(__RESULT)
191+
#define WRITE_LIBUSB_ERROR(__RESULT) libusb_error_name(__RESULT) << " " << libusb_strerror((libusb_error)__RESULT) << ". Try debugging with environment variable: export LIBUSB_DEBUG=4 ."
192192

193193
UsbControl::ResultCode UsbControl::setConfiguration()
194194
{

0 commit comments

Comments
 (0)