Hi,
I have the code below to write some data to a bulk usb output endpoint using the sync api.
// Find device
// Write data
do {
r = libusb_bulk_transfer(handle, endpoint_out, wdata, swdata, &size, 1000);
if (r == LIBUSB_ERROR_PIPE) {
libusb_clear_halt(handle, endpoint_out);
}
i++;
} while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX));
// Read response
int rsize;
char rdata[srdata] = {0};
r = libusb_bulk_transfer(handle, endpoint_in, &rdata, srdata, &rsize, 1000);
if (r < 0)
{
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(r));
}
How could I achieve the same with the async api? I have tried the code below, but I do not seem to get the same data back.
struct libusb_transfer *transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(transfer, handle, endpoint_out, wdata, swdata, cb_write, NULL, 0);
if (libusb_submit_transfer(transfer) < 0)
printf("Failed to submit the read command.");
while (1) {
r = libusb_handle_events(context);
if (r < 0)
printf("Failed to handle events\n");
}
Then, in the write callback:
static void cb_write(struct libusb_transfer *transfer)
{
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "Write transfer status %d?\n", transfer->status);
libusb_free_transfer(transfer);
transfer = NULL;
return;
}
struct libusb_transfer *read_transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(read_transfer, handle, endpoint_in, rdata, srdata, cb_ready, NULL, 0);
if (libusb_submit_transfer(read_transfer) < 0)
printf("Failed to submit the read operation.\n");
else
printf("Submitted the read operation.\n");
Then, I was thinking that I would need to do something for a read callback:
static void cb_read(struct libusb_transfer *transfer)
{
if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
fprintf(stderr, "Read transfer status: %d, data length: %d\n", transfer->status, transfer->length);
libusb_free_transfer(transfer);
transfer = NULL;
return;
}
if (libusb_submit_transfer(transfer) < 0)
printf("Failed to submit the transfer\n");
}
So, I guess the real question is whether I am meant to handle the situation of write a request and then read the response in this way with the async libusb api or somehow different.
Kind regards,
Laszlo Papp
PS.: I got most of this logic from the examples.