0

I want to setup an asynchronous bulk transfer to a callback routine but I never enter the callback routine. I changed the code to a synchronous transfer and it works. Please help me understand what I'm doing wrong with the asynchronous transfer. Below is the synchronous transfer

#include <stdio.h>
#include <libusb.h>

#define LB04_VID 4302
#define LB04_PID 60307

unsigned char in_buf[32];

void hexdump(unsigned char* data, int len)
{
    int i;

    for (i = 0; i < len; i++) printf("%02X ", data[i]);
    puts("\n");
}

int main(int argc, char* argv[])
{
    libusb_device** devs;
    libusb_device_handle* dev_handle;
    libusb_context* context = NULL;

    size_t list;
    int ret;
    int iLen;

    ret = libusb_init(&context);

    if (ret < 0) {
        perror("libusb_init");
        return 1;
    }

    libusb_set_option(context, LIBUSB_OPTION_MAX);
    list = libusb_get_device_list(context, &devs);
    if (list < 0) {
        perror("libusb_get_device_list");
        return 1;
    }

    dev_handle = libusb_open_device_with_vid_pid(context, LB04_VID, LB04_PID);
    libusb_free_device_list(devs, 1);

    printf("found XHC-HB04 device\n");

    if (dev_handle) {
        if (libusb_kernel_driver_active(dev_handle, 0) == 1) {
            libusb_detach_kernel_driver(dev_handle, 0);
        }

        ret = libusb_claim_interface(dev_handle, 0);
        if (ret < 0) {
            perror("libusb_claim_interface");
            return 1;
        }

        ret = libusb_set_configuration(dev_handle, 1);

        while (1) {
            ret = libusb_bulk_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN), in_buf, sizeof(in_buf), &iLen, 0);

            hexdump((unsigned char*)&in_buf, iLen);
        }
        libusb_release_interface(dev_handle, 0);
        libusb_close(dev_handle);
    }

    libusb_exit(context);
}

This loops forever like I want. However, when I try to use asynchronous transfer like the below, my callback never gets called. Please help as I'm stuck with what to try next.

#include <stdio.h>
#include <libusb.h>

#define LB04_VID 4302
#define LB04_PID 60307

unsigned char in_buf[32];
libusb_device** devs;
libusb_device_handle* dev_handle;
libusb_context* context = NULL;
struct libusb_transfer* transfer_in = NULL;

int setup_asynch_transfer(libusb_device_handle* dev_handle);


void hexdump(unsigned char* data, int len)
{
    int i;

    for (i = 0; i < len; i++)
        printf("%02X ", data[i]);

    puts("\n");
}
void cb_response_in(struct libusb_transfer* transfer)
{
    printf("cb_response_in\n");
    if (transfer->actual_length > 0)
        hexdump((unsigned char*)&in_buf, transfer->actual_length);

    setup_asynch_transfer(transfer->dev_handle);
}

int setup_asynch_transfer(libusb_device_handle* dev_handle)
{
    int ret;

    printf("setup_asynch_transfer\n");

    transfer_in = libusb_alloc_transfer(0);
    libusb_fill_bulk_transfer(transfer_in, dev_handle, (0x01 | LIBUSB_ENDPOINT_IN),
        in_buf, sizeof(in_buf),
        cb_response_in, NULL, 500); // no user data
    
    return (ret = libusb_submit_transfer(transfer_in));
}


int main(int argc, char* argv[])
{
    size_t list;
    int ret;

    ret = libusb_init(&context);

    if (ret < 0) {
        perror("libusb_init");
        return 1;
    }

    libusb_set_option(context, LIBUSB_OPTION_MAX);
    list = libusb_get_device_list(context, &devs);
    if (list < 0) {
        perror("libusb_get_device_list");
        return 1;
    }

    dev_handle = libusb_open_device_with_vid_pid(context, LB04_VID, LB04_PID);
    libusb_free_device_list(devs, 1);

    printf("found XHC-HB04 device\n");

    if (dev_handle) {
        if (libusb_kernel_driver_active(dev_handle, 0) == 1) {
            libusb_detach_kernel_driver(dev_handle, 0);
        }

        ret = libusb_claim_interface(dev_handle, 0);
        if (ret < 0) {
            perror("libusb_claim_interface");
            return 1;
        }

        ret = libusb_set_configuration(dev_handle, 1);


        ret = setup_asynch_transfer(dev_handle);

            // Loop forever
            while (1);

        libusb_release_interface(dev_handle, 0);
        libusb_close(dev_handle);
    }

    libusb_exit(context);
}


I made sure I could execute a bulk synchronous transfer and then converted the code to execute asynchronous transfer. I expected the callback routine to print the data and schedule another asynchronous routine while the main routine waited in a while loop forever.

I'm using libusb-1.0 and Microsoft Visual Studio Community 2022 on Windows 10.

0 Answers0