[prev in list] [next in list] [prev in thread] [next in thread] 

List:       libusb-devel
Subject:    [libusb] Async reading
From:       Laszlo Papp <lpapp () kde ! org>
Date:       2020-07-21 16:43:31
Message-ID: CAOMwXhMGx5=Fx3tgRyJWq1=ieG+p+uTSfmN25se81vwnaw6UZg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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.

[Attachment #5 (text/html)]

<div dir="ltr">Hi,<div><br></div><div>I have the code below to write some data to a \
bulk usb output endpoint using the sync api.</div><div><br></div><div>// Find \
device</div><div>// Write data</div><div>do {<br></div><div>   r = \
libusb_bulk_transfer(handle, endpoint_out, wdata, swdata, &amp;size, 1000);<br>   if \
(r == LIBUSB_ERROR_PIPE) {<br>      libusb_clear_halt(handle, endpoint_out);<br>   \
}<br>   i++;<br>			} while ((r == LIBUSB_ERROR_PIPE) &amp;&amp; \
(i&lt;RETRY_MAX));<br><br>// Read response<br>int rsize;<br>			char rdata[srdata] = \
{0};<br>			r = libusb_bulk_transfer(handle, endpoint_in, &amp;rdata, srdata, \
&amp;rsize, 1000);<br>			if (r &lt; 0)<br>			{<br>   \
printf(&quot;libusb_bulk_transfer failed: %s\n&quot;, \
libusb_error_name(r));<br>			}</div><div><br></div><div>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.</div><div><br></div><div>   struct libusb_transfer *transfer = \
libusb_alloc_transfer(0);<br>   libusb_fill_bulk_transfer(transfer, handle, \
endpoint_out, wdata, swdata, cb_write, NULL, 0); <br>   if \
(libusb_submit_transfer(transfer) &lt; 0)<br>            printf(&quot;Failed to \
submit the read command.&quot;);<br>   while (1) {<br>      r = \
libusb_handle_events(context);<br>      if (r &lt; 0)<br>         printf(&quot;Failed \
to handle events\n&quot;);<br>      }<br></div><div><br></div><div>Then, in the write \
callback:</div><div><br></div><div>static void cb_write(struct libusb_transfer \
*transfer)<br>{<br>      if (transfer-&gt;status != LIBUSB_TRANSFER_COMPLETED) {<br>  \
fprintf(stderr, &quot;Write transfer status %d?\n&quot;, transfer-&gt;status);<br>    \
libusb_free_transfer(transfer);<br>            transfer = NULL;<br>            \
return;<br>      }    <br><br>struct libusb_transfer *read_transfer = \
libusb_alloc_transfer(0);<br>libusb_fill_bulk_transfer(read_transfer, handle, \
endpoint_in, rdata, srdata, cb_ready, NULL, 0);<br>	if \
(libusb_submit_transfer(read_transfer) &lt; 0)<br>	      printf(&quot;Failed to \
submit the read operation.\n&quot;);<br>	else<br>   printf(&quot;Submitted the read \
operation.\n&quot;);<br><br>Then, I was thinking that I would need to do something \
for a read callback:</div><div><br></div><div>static void cb_read(struct \
libusb_transfer *transfer)<br>{<br>      if (transfer-&gt;status != \
LIBUSB_TRANSFER_COMPLETED) {<br>            fprintf(stderr, &quot;Read transfer \
status: %d, data length: %d\n&quot;, transfer-&gt;status, transfer-&gt;length);<br>   \
libusb_free_transfer(transfer);<br>            transfer = NULL;<br>            \
return;<br>      }    <br><br>      if (libusb_submit_transfer(transfer) &lt; 0)<br>  \
printf(&quot;Failed to submit the \
transfer\n&quot;);<br>}</div><div><br></div><div>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.</div><div><br></div><div>Kind regards,</div><div>Laszlo \
Papp</div><div><br></div><div>PS.: I got most of this logic from the \
examples.<br><br></div></div>





_______________________________________________
libusb-devel mailing list
libusb-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusb-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic