29 #if defined(_MSC_VER) && defined(_DEBUG) 46 #define _CRTDBG_MAP_ALLOC 48 #define CRTDBG_MAP_ALLOC 62 #define LIBUSBEMU_DEBUG_NEW new ( _NORMAL_BLOCK, __FILE__, __LINE__) 63 #define new LIBUSBEMU_DEBUG_NEW 77 #define LIBUSBEMU_DEBUG_BUILD 80 #ifdef LIBUSBEMU_DEBUG_BUILD 81 #define LIBUSB_DEBUG_CMD(cmd) cmd 83 #define LIBUSB_DEBUG_CMD(cmd) 84 #endif//LIBUSBEMU_DEBUG_BUILD 93 const usb_version* version = usb_get_version();
94 fprintf(stdout,
"libusb-win32: dll version %d.%d.%d.%d | driver (libusb0.sys) version %d.%d.%d.%d\n",
95 version->dll.major, version->dll.minor, version->dll.micro, version->dll.nano,
96 version->driver.major, version->driver.minor, version->driver.micro, version->driver.nano);
119 libusb_context::TMapDevices::iterator itDevice (ctx->
devices.begin());
121 if (NULL != device.handles)
128 const int handles = device.handles->size();
129 for (
int h=0; h<handles; ++h)
131 libusb_device::TMapHandles::iterator itHandle = device.handles->begin();
147 usb_set_debug(level);
164 usb_bus* bus = usb_get_busses();
167 struct usb_device*
device = bus->devices;
171 device = device->next;
180 libusb_context::TMapDevices::iterator it (ctx->
devices.begin());
181 libusb_context::TMapDevices::iterator end (ctx->
devices.end());
182 for (
int i=0; it!=end; ++it, ++i)
183 devlist[i] = &it->second;
185 devlist[ctx->
devices.size()] = NULL;
197 while (list[i] != NULL)
209 usb_device_descriptor& device_desc (device->descriptor);
221 usb_dev_handle* usb_handle (usb_open(dev->
device));
222 if (NULL == usb_handle)
232 libusb_device::TMapHandles::iterator it = dev->
handles->insert(std::make_pair(usb_handle,dummy)).first;
233 *handle = &(it->second);
234 assert((*handle)->dev == dev);
235 assert((*handle)->handle == usb_handle);
255 LIBUSBEMU_ERROR(
"libusb_close() attempted to close an unregistered handle!\n");
258 if (0 != usb_close(dev_handle->
handle))
270 int bytes = usb_get_string(dev_handle->
handle, (
int)desc_index, (
int)langid, (
char*)data, (
size_t)length);
279 int bytes = usb_get_string_simple(dev_handle->
handle, (
int)desc_index, (
char*)data, (
size_t)length);
289 int ret = usb_set_configuration(dev->
handle, configuration);
307 if (0 != usb_set_altinterface(dev->
handle, alternate_setting))
325 if (0 != usb_set_configuration(dev->
handle, 1))
330 if (0 != usb_claim_interface(dev->
handle, interface_number))
350 if (0 != usb_release_interface(dev->
handle, interface_number))
376 while (list[i] != NULL) {
397 timeout = (0 == timeout) ? 60000 : timeout;
398 int bytes_transferred = usb_control_msg(dev_handle->
handle, bmRequestType, bRequest, wValue, wIndex, (
char*)data, wLength, timeout);
399 if (bytes_transferred < 0)
409 return(bytes_transferred);
415 timeout = (0 == timeout) ? 60000 : timeout;
416 int bytes_transferred;
418 bytes_transferred = usb_bulk_read(dev_handle->
handle, endpoint, (
char*)data, length, timeout);
420 bytes_transferred = usb_bulk_write(dev_handle->
handle, endpoint, (
char*)data, length, timeout);
422 if (bytes_transferred < 0) {
433 *transferred = bytes_transferred;
456 if (NULL == transfer)
464 if (!libusb_device::TListTransfers::Orphan(wrapper))
466 LIBUSBEMU_ERROR(
"libusb_free_transfer() attempted to free an active transfer!");
470 if (0 != usb_free_async(&wrapper->
usb))
539 if (NULL == wrapper->
usb)
544 int ret = usb_submit_async(wrapper->
usb, (
char*)transfer->
buffer, transfer->
length);
557 libusb_device::TMapIsocTransfers::iterator it = isoTransfers.find(transfer->
endpoint);
558 if (isoTransfers.end() == it)
561 it = isoTransfers.insert(std::make_pair(transfer->
endpoint, dummy)).first;
568 void**
state =
new void* [2];
570 state[1] = (
void*)transfer->
endpoint;
588 int ret = usb_cancel_async(wrapper->
usb);
616 hDoneDeliveringPoolLocal.
WaitAll();
664 assert(libusb_device::TListTransfers::Orphan(wrapper));
689 void**
state = (
void**)params;
700 bool boAbort (
false);
702 bool boDeliverRequested (
false);
719 while(!listTransfers.
Empty() || !listReadyLocal.
Empty())
722 if (!listReadyLocal.
Empty())
725 if (!boDeliverRequested)
727 doneDelivering.
Reset();
729 boDeliverRequested =
true;
734 boDeliverRequested =
false;
735 while (!listReadyLocal.
Empty())
738 listReadyLocal.
Remove(wrapper);
746 if (!listTransfers.
Empty())
749 int read =
ReapTransfer(wrapper, 10000, &listReadyLocal);
752 while (!listTransfers.
Empty())
754 while (!listReadyLocal.
Empty())
761 LIBUSB_DEBUG_CMD(fprintf(stdout,
"ReapThreadProc(): no pending transfers, sleeping until delivery...\n"));
762 if (!boDeliverRequested)
764 doneDelivering.
Reset();
766 boDeliverRequested =
true;
777 LIBUSB_DEBUG_CMD(fprintf(stderr,
"Thread is aborting: releasing transfers...\n"));
779 boDeliverRequested =
true;
791 fprintf(stderr,
"Thread loop aborted.\n");
804 poThreadObject = NULL;
812 void* context (wrapper->
usb);
815 const int read = usb_reap_async_nocancel(context, timeout);
834 libusb_device::TListTransfers::RemoveNode(wrapper);
839 if (NULL != lstReady)
840 lstReady->
Append(wrapper);
853 enum EReapResult { EIO = -5, EINVAL = -22, ETIMEOUT = -116 };
870 libusb_device::TListTransfers::RemoveNode(wrapper);
884 libusb_device::TListTransfers::RemoveNode(wrapper);
907 unsigned int remaining (read);
909 for (
int i=0; i<pkts; ++i)
950 const unsigned int pktlen =
sizeof(
pkt_hdr) + pktstrm->pkt_size;
951 const unsigned int pktend =
sizeof(
pkt_hdr) + pktstrm->last_pkt_size;
953 unsigned int remaining (read);
954 unsigned int leftover (transfer->
length);
955 unsigned char* pktbuffer (transfer->
buffer);
957 for (
int i=0; i<pkts; ++i)
962 if ((header.
magic[0] ==
'R') && (header.
magic[1] ==
'B'))
964 switch(header.
flag & 0x0F)
974 fprintf(stdout,
"0x%02X\n", header.
flag);
991 fprintf(stdout,
"%d remaining out of %d\n", remaining, read);
992 if (remaining == read)
993 fprintf(stdout,
"no bytes consumed!\n");
struct usb_device * device
int WaitAnyUntilTimeout(const unsigned int milliseconds)
#define LIBUSB_DEBUG_CMD(cmd)
const bool Remove(T *node)
int ReapThreadProc(void *params)
enum libusb_transfer_status status
void libusb_free_device_list(libusb_device **list, int unref_devices)
void libusb_close(libusb_device_handle *dev_handle)
void libusb_exit(libusb_context *ctx)
enum libusb_transfer_status status
TListTransfers listTransfers
int libusbemu_handle_isochronous(libusb_context *ctx, const unsigned int milliseconds)
int libusb_control_transfer(libusb_device_handle *dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout)
#define LIBUSBEMU_ERROR_LIBUSBWIN32()
void PreprocessTransferFreenect(libusb_transfer *transfer, const int read)
int libusb_get_string_descriptor(libusb_device_handle *dev_handle, uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
int libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc)
void libusbemu_unregister_device(libusb_device *dev)
void libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
QuickList< transfer_wrapper > TListTransfers
void libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, unsigned int length)
EventList hDoneDeliveringPool
int libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length)
void(* libusb_transfer_cb_fn)(struct libusb_transfer *transfer)
static void(* PreprocessTransfer)(libusb_transfer *, const int)(PreprocessTransferFreenect)
int libusb_bulk_transfer(libusb_device_handle *dev_handle, uint8_t endpoint, uint8_t *data, int length, int *transferred, unsigned int timeout)
QuickMutex mutDeliveryPool
libusb_transfer_cb_fn callback
const bool WaitUntilTimeout(const unsigned int milliseconds)
int libusb_init(libusb_context **context)
const bool AttachEvent(QuickEvent *poEvent)
int libusb_set_configuration(libusb_device_handle *dev, int configuration)
static libusb_context * default_context
unsigned int actual_length
void libusbemu_clear_transfer(transfer_wrapper *wrapper)
libusb_device * libusbemu_register_device(libusb_context *ctx, struct usb_device *dev)
int libusb_handle_events(libusb_context *ctx)
EventList hAllowDeliveryPool
void libusbemu_deliver_transfer(transfer_wrapper *wrapper)
void libusb_free_transfer(struct libusb_transfer *transfer)
int libusb_cancel_transfer(struct libusb_transfer *transfer)
std::map< usb_dev_handle *, libusb_device_handle > TMapHandles
MyFreenectDevice * device
libusb_device_handle * libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *timeout)
struct libusb_iso_packet_descriptor * iso_packet_desc
struct libusb_transfer * libusb_alloc_transfer(int iso_packets)
libusb_device_handle * dev_handle
int libusbemu_setup_transfer(transfer_wrapper *wrapper)
#define LIBUSBEMU_ERROR(msg)
void PreprocessTransferNaive(libusb_transfer *transfer, const int read)
typedef void(ONI_CALLBACK_TYPE *DeviceConnectedCallback)(const OniDeviceInfo *
static freenect_context * ctx
int libusb_claim_interface(libusb_device_handle *dev, int interface_number)
T MIN(const T &v1, const T &v2)
static QuickThread Myself()
transfer_wrapper * libusbemu_get_transfer_wrapper(libusb_transfer *transfer)
int libusb_open(libusb_device *dev, libusb_device_handle **handle)
void libusb_set_debug(libusb_context *ctx, int level)
EventList hWantToDeliverPool
struct libusb_context_t libusb_context
const bool DetachEvent(QuickEvent *poEvent)
int libusb_submit_transfer(struct libusb_transfer *transfer)
int ReapTransfer(transfer_wrapper *, unsigned int, libusb_device::TListTransfers *)
QuickThread * poReapThread
TMapIsocTransfers * isoTransfers
int libusb_release_interface(libusb_device_handle *dev, int interface_number)
ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
std::map< int, isoc_handle > TMapIsocTransfers
int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number, int alternate_setting)