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
101102struct 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
108110std::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
655693std::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;
0 commit comments