尝试使用libusb控制音量时出现LIBUSB_ERROR_PIPE

问题描述 投票:0回答:1

我正在尝试编写一个小型 C 程序来读取和调整 USB DAC(Apple USB-C 至 3.5mm Dongle)的音量。我一直在使用this document作为参考,但遇到了一个我似乎无法解决的问题。我收到了针对我的请求的 LIBUSB_ERROR_PIPE 响应。

libusb_control_transfer(handle, 0b10100001, 0x81, 0x0200, 0x0200, mesg, 2, 500);
是我正在使用的行,bmRequestType,bRequest来自第68页的文档,wValue是0x02(VOLUME_CONTROL),低0字节,wIndex来自在音量变化期间嗅探wireshark(我有一种感觉这是我失败的地方,但是我不明白终端 ID 是如何来源的),mesg 是 2 0x00 字节,wLength 是 2。完整的程序如下。

#include <assert.h>
#include <libusb-1.0/libusb.h>

int main(int argc, char **argv) {
    libusb_context *context;
    libusb_device_handle *handle;
    libusb_device *device;
    
    libusb_device **devs;
    ssize_t num_devs;
    
    int i;
    
    struct libusb_device_descriptor desc;
    unsigned char buffer[256];
    
    assert(!libusb_init(&context));
    
    num_devs = libusb_get_device_list(context, &devs);
    if (num_devs < 0) {
        fprintf(stderr, "Error getting device list\n");
        libusb_exit(context);
        return 1;
    }
    
    for (i = 0; i < num_devs; ++i) {
        libusb_device *dev = devs[i];
        struct libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0) {
            fprintf(stderr, "Failed to get device descriptor\n");
            continue;
        }
        if(desc.idVendor == 0x05ac && desc.idProduct == 0x110a){
            device = dev;
        }
    }
    
    assert(!libusb_get_device_descriptor(device, &desc));
    printf("Vendor ID: %04x\n", desc.idVendor);
    printf("Product ID: %04x\n", desc.idProduct);

    assert(libusb_open(device, &handle) == 0);
    device = libusb_get_device(handle);
    
    assert(libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, buffer, 256) >= 0);
    printf("Manufacturer: %s\n", buffer);
    assert(libusb_get_string_descriptor_ascii(handle, desc.iProduct, buffer, 256) >= 0);
    printf("Product: %s\n", buffer);
    if (libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buffer, 256) >= 0)
        printf("Serial No: %s\n", buffer);
    libusb_detach_kernel_driver(handle, 0);
    libusb_claim_interface(handle, 0);
    
    int result = libusb_control_transfer(handle, 0b10100001, 0x81, 0x0200, 0x0200, mesg, 2, 500);
    printf(libusb_error_name(result));
    printf("%d", result);
    printf("\n");
    
    libusb_release_interface(handle, 0);
    libusb_attach_kernel_driver(handle, 0);
    libusb_exit(context);
}
c audio usb libusb libusb-1.0
1个回答
0
投票

我认为你的

bRequest
应该是
0x1
而不是
0x81
:

int result = libusb_control_transfer(handle, 0b10100001 /* bmRequest */, 0x1 /* bRequest */, 0x0200 /* wValue */, 0x0200 /* wIndex */, mesg, 2 /* wLength */, 500);

我现在无法向您引用UAC 1.0,但这是我在wireshark中看到的,并且您的代码成功,产生的体积与我在

Audio MIDI Setup.app
中看到的相匹配。

© www.soinside.com 2019 - 2024. All rights reserved.