Skip to content

Commit

Permalink
Fixed handling of errors returned by libusb_submit_transfer()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpevzner committed Dec 5, 2024
1 parent 3064b4d commit 85049d8
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions usbio_libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func libusbContext(nopnp bool) (*C.libusb_context, error) {
)
}

// Start libusb thread (required for hotplug)
// Start libusb thread (required for hotplug and asynchronous I/O)
go func() {
runtime.LockOSThread()
for {
Expand Down Expand Up @@ -787,19 +787,22 @@ func (iface *UsbInterface) Send(ctx context.Context,
0,
)

// Submit transfer and wait for completion
// Submit transfer
rc := C.libusb_submit_transfer(xfer)
if rc >= 0 {
select {
case <-ctx.Done():
C.libusb_cancel_transfer(xfer)
case <-doneChan:
}
if rc < 0 {
return 0, UsbError{"libusb_submit_transfer", UsbErrCode(rc)}
}

<-doneChan
n, err = libusbTransferStatusDecode(ctx, xfer)
// Wait for completion
select {
case <-ctx.Done():
C.libusb_cancel_transfer(xfer)
case <-doneChan:
}

<-doneChan
n, err = libusbTransferStatusDecode(ctx, xfer)

return
}

Expand Down Expand Up @@ -847,19 +850,24 @@ func (iface *UsbInterface) Recv(ctx context.Context,
0,
)

// Submit transfer and wait for completion
// Submit transfer
rc := C.libusb_submit_transfer(xfer)
if rc >= 0 {
select {
case <-ctx.Done():
C.libusb_cancel_transfer(xfer)
case <-doneChan:
}
if rc < 0 {
return 0, UsbError{"libusb_submit_transfer", UsbErrCode(rc)}
}

C.libusb_interrupt_event_handler(libusbContextPtr)

<-doneChan
n, err = libusbTransferStatusDecode(ctx, xfer)
// Wait for completion
select {
case <-ctx.Done():
C.libusb_cancel_transfer(xfer)
case <-doneChan:
}

<-doneChan
n, err = libusbTransferStatusDecode(ctx, xfer)

return
}

Expand Down

0 comments on commit 85049d8

Please sign in to comment.