28
28
#include < vector>
29
29
#include < algorithm>
30
30
#include < libusb.h>
31
+ #define WRITE_LIBUSB_ERROR (__RESULT ) libusb_error_name(__RESULT) << " " << libusb_strerror((libusb_error)__RESULT)
31
32
32
33
#include < libfreenect2/libfreenect2.hpp>
33
34
@@ -101,13 +102,16 @@ class Freenect2DeviceImpl : public Freenect2Device
101
102
struct PrintBusAndDevice
102
103
{
103
104
libusb_device *dev_;
105
+ int status_;
104
106
105
- PrintBusAndDevice (libusb_device *dev) : dev_(dev) {}
107
+ PrintBusAndDevice (libusb_device *dev, int status = 0 ) : dev_(dev), status_(status ) {}
106
108
};
107
109
108
110
std::ostream &operator <<(std::ostream &out, const PrintBusAndDevice& dev)
109
111
{
110
112
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_ );
111
115
return out;
112
116
}
113
117
@@ -130,26 +134,39 @@ class Freenect2Impl
130
134
UsbDeviceVector enumerated_devices_;
131
135
DeviceVector devices_;
132
136
137
+ bool initialized;
138
+
133
139
Freenect2Impl (void *usb_context) :
134
140
managed_usb_context_ (usb_context == 0 ),
135
141
usb_context_ (reinterpret_cast <libusb_context *>(usb_context)),
142
+ initialized (false ),
136
143
has_device_enumeration_ (false )
137
144
{
145
+ if (libusb_get_version ()->nano < 10952 )
146
+ {
147
+ LOG_ERROR << " Your libusb does not support large iso buffer!" ;
148
+ return ;
149
+ }
150
+
138
151
if (managed_usb_context_)
139
152
{
140
153
int r = libusb_init (&usb_context_);
141
- // TODO: error handling
142
154
if (r != 0 )
143
155
{
144
- LOG_ERROR << " failed to create usb context!" ;
156
+ LOG_ERROR << " failed to create usb context: " << WRITE_LIBUSB_ERROR (r);
157
+ return ;
145
158
}
146
159
}
147
160
148
161
usb_event_loop_.start (usb_context_);
162
+ initialized = true ;
149
163
}
150
164
151
165
~Freenect2Impl ()
152
166
{
167
+ if (!initialized)
168
+ return ;
169
+
153
170
clearDevices ();
154
171
clearDeviceEnumeration ();
155
172
@@ -165,11 +182,17 @@ class Freenect2Impl
165
182
166
183
void addDevice (Freenect2DeviceImpl *device)
167
184
{
185
+ if (!initialized)
186
+ return ;
187
+
168
188
devices_.push_back (device);
169
189
}
170
190
171
191
void removeDevice (Freenect2DeviceImpl *device)
172
192
{
193
+ if (!initialized)
194
+ return ;
195
+
173
196
DeviceVector::iterator it = std::find (devices_.begin (), devices_.end (), device);
174
197
175
198
if (it != devices_.end ())
@@ -184,6 +207,9 @@ class Freenect2Impl
184
207
185
208
bool tryGetDevice (libusb_device *usb_device, Freenect2DeviceImpl **device)
186
209
{
210
+ if (!initialized)
211
+ return false ;
212
+
187
213
for (DeviceVector::iterator it = devices_.begin (); it != devices_.end (); ++it)
188
214
{
189
215
if ((*it)->isSameUsbDevice (usb_device))
@@ -198,6 +224,9 @@ class Freenect2Impl
198
224
199
225
void clearDevices ()
200
226
{
227
+ if (!initialized)
228
+ return ;
229
+
201
230
DeviceVector devices (devices_.begin (), devices_.end ());
202
231
203
232
for (DeviceVector::iterator it = devices.begin (); it != devices.end (); ++it)
@@ -213,6 +242,9 @@ class Freenect2Impl
213
242
214
243
void clearDeviceEnumeration ()
215
244
{
245
+ if (!initialized)
246
+ return ;
247
+
216
248
// free enumerated device pointers, this should not affect opened devices
217
249
for (UsbDeviceVector::iterator it = enumerated_devices_.begin (); it != enumerated_devices_.end (); ++it)
218
250
{
@@ -225,6 +257,9 @@ class Freenect2Impl
225
257
226
258
void enumerateDevices ()
227
259
{
260
+ if (!initialized)
261
+ return ;
262
+
228
263
LOG_INFO << " enumerating devices..." ;
229
264
libusb_device **device_list;
230
265
int num_devices = libusb_get_device_list (usb_context_, &device_list);
@@ -277,14 +312,14 @@ class Freenect2Impl
277
312
}
278
313
else
279
314
{
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) ;
281
316
}
282
317
283
318
libusb_close (dev_handle);
284
319
}
285
320
else
286
321
{
287
- LOG_ERROR << " failed to open Kinect v2 " << PrintBusAndDevice (dev) << " ! " ;
322
+ LOG_ERROR << " failed to open Kinect v2: " << PrintBusAndDevice (dev, r) ;
288
323
}
289
324
}
290
325
}
@@ -300,6 +335,9 @@ class Freenect2Impl
300
335
301
336
int getNumDevices ()
302
337
{
338
+ if (!initialized)
339
+ return 0 ;
340
+
303
341
if (!has_device_enumeration_)
304
342
{
305
343
enumerateDevices ();
@@ -654,6 +692,9 @@ int Freenect2::enumerateDevices()
654
692
655
693
std::string Freenect2::getDeviceSerialNumber (int idx)
656
694
{
695
+ if (!impl_->initialized )
696
+ return std::string ();
697
+
657
698
return impl_->enumerated_devices_ [idx].serial ;
658
699
}
659
700
@@ -701,7 +742,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
701
742
702
743
if (r != LIBUSB_SUCCESS)
703
744
{
704
- LOG_ERROR << " failed to open Kinect v2 " << PrintBusAndDevice (dev.dev ) << " ! " ;
745
+ LOG_ERROR << " failed to open Kinect v2: " << PrintBusAndDevice (dev.dev , r) ;
705
746
delete pipeline;
706
747
707
748
return device;
@@ -740,7 +781,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
740
781
}
741
782
else if (r != LIBUSB_SUCCESS)
742
783
{
743
- LOG_ERROR << " failed to reset Kinect v2 " << PrintBusAndDevice (dev.dev ) << " ! " ;
784
+ LOG_ERROR << " failed to reset Kinect v2: " << PrintBusAndDevice (dev.dev , r) ;
744
785
delete pipeline;
745
786
746
787
return device;
@@ -755,7 +796,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline,
755
796
delete device;
756
797
device = 0 ;
757
798
758
- LOG_ERROR << " failed to open Kinect v2 " << PrintBusAndDevice (dev.dev ) << " ! " ;
799
+ LOG_ERROR << " failed to open Kinect v2: " << PrintBusAndDevice (dev.dev );
759
800
}
760
801
761
802
return device;
0 commit comments