HappyGook
HappyGook

Reputation: 23

Transfer Failed Error in gousb: how to debug libusb write issue?

I am working on a program that communicates with a usb device through gousb library. While attempting to send data, I get a "transfer failed" error during the Write operation.

func SendIQData(device *gousb.OutEndpoint, data []byte) error {
    const chunkSize=512
    log.Printf("Endpoint address: %d, Max packet size: %d", device.Desc.Address, device.Desc.MaxPacketSize)
    log.Printf("Sending total data size: %d bytes",len(data))

    for i := 0; i < len(data); i += chunkSize {
        end := i + chunkSize
        if end > len(data) {
            end = len(data) 
        }

        chunk := data[i:end]
        log.Printf("Sending chunk: %d bytes (from %d to %d)", len(chunk), i, end)

        n, err := device.Write(chunk)
        if err != nil {
            log.Printf("Error sending chunk at index %d: %v", i/packetSize, err)
            if errors.Unwrap(err) != nil {
                log.Printf("Root cause of transfer error: %v", errors.Unwrap(err)) 
            }
        }
        log.Printf("Successfully sent %d bytes", n)
    }


    return nil
}

The gousb library successfully initializes the USB context, device, and endpoints without any errors.

The interface and endpoint are successfully opened using:

intf, err := config.Interface(0, 0)

And the endpoint is retrieved using:

ep, err := intf.OutEndpoint(2)

I am running the program on a Raspberry Pi with Debian OS. Whenever I try to send the data, I get this simple message "transfer failed" in bash.

2025/01/23 12:54:42 Sending chunk: 512 bytes (from 1047552 to 1048064)
2025/01/23 12:54:42 Error sending chunk at index 2046: transfer failed
2025/01/23 12:54:42 Successfully sent 0 bytes

I could not find any other logs and this message alone makes no sense whatsoever. My guess is that it happens because the error is shown by gousb which takes the original error from libusb and wraps it into this from. Is it possible to unwrap it somehow?

Upvotes: 2

Views: 22

Answers (0)

Related Questions