2012-01-24 71 views
1

我想使用libusb庫爲USB編寫一些測試應用程序。 任何人都可以請建議如何設置使用usb_control_msg調用控制轉移?使用libusb庫的USB應用程序

運行以下代碼時出現錯誤描述符錯誤。

#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include "usb.h" 
static int vendor_id; 
static int product_id; 

typedef struct{ 
    int requesttype; 
    int request; 
    int value; 
    int index; 
    char *bytes; 
    int size; 
    int timeout; 
}ctrlmsg_param; 

void print_endpoint(struct usb_endpoint_descriptor *endpoint) 
{ 
    printf("=====End point Information====\n"); 
    printf("bEndpointAddress: %x\n", endpoint->bEndpointAddress); 
    printf("bmAttributes:  %x\n", endpoint->bmAttributes); 
    printf("wMaxPacketSize: %d\n", endpoint->wMaxPacketSize); 
    printf("bInterval:  %d\n", endpoint->bInterval); 
    printf("bRefresh:   %d\n", endpoint->bRefresh); 
    printf("bSynchAddress: %d\n", endpoint->bSynchAddress); 
} 

void print_altsetting(struct usb_interface_descriptor *interface) 
{ 
    int i; 

    printf("\n=====Alternate Setting Information====\n"); 
    printf("bInterfaceNumber: %d\n", interface->bInterfaceNumber); 
    printf("bAlternateSetting: %d\n", interface->bAlternateSetting); 
    printf("bNumEndpoints:  %d\n", interface->bNumEndpoints); 
    printf("bInterfaceClass: %d\n", interface->bInterfaceClass); 
    printf("bInterfaceSubClass: %d\n", interface->bInterfaceSubClass); 
    printf("bInterfaceProtocol: %d\n", interface->bInterfaceProtocol); 
    printf("iInterface:   %d\n", interface->iInterface); 

    for (i = 0; i < interface->bNumEndpoints; i++) 
     print_endpoint(&interface->endpoint[i]); 
} 

void print_interface(struct usb_interface *interface) 
{ 
    int i; 

    for (i = 0; i < interface->num_altsetting; i++) 
     print_altsetting(&interface->altsetting[i]); 
} 

void print_configuration(struct usb_config_descriptor *config) 
{ 
    int i; 

    printf("=====Configuration Information====\n"); 
    printf("wTotalLength:   %d\n", config->wTotalLength); 
    printf("bNumInterfaces:  %d\n", config->bNumInterfaces); 
    printf("bConfigurationValue: %d\n", config->bConfigurationValue); 
    printf("iConfiguration:  %d\n", config->iConfiguration); 
    printf("bmAttributes:   %x\n", config->bmAttributes); 
    printf("MaxPower:    %d\n", config->MaxPower); 

    for (i = 0; i < config->bNumInterfaces; i++) 
     print_interface(&config->interface[i]); 
} 

int print_device(struct usb_device *dev) 
{ 
    usb_dev_handle *udev; 
    char str[100]; 
    int ret, i; 
    udev = usb_open(dev); 
    if (udev) { 
     if (dev->descriptor.iManufacturer) { 
      ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, str, sizeof(str)); 
      if (ret > 0) 
      { 
       printf("Manufacturer is %s\n",str); 
      } 
     } 
     if (dev->descriptor.iProduct) { 
      ret = usb_get_string_simple(udev, dev->descriptor.iProduct, str, sizeof(str)); 
      if (ret > 0) 
      { 
       printf("Product is %s\n",str); 
      } 
     } 

    } 

    if (udev) 
     usb_close(udev); 
    printf("Possible configurations are %x\n",dev->descriptor.bNumConfigurations); 
    sleep(2); 
    for (i = 0; i < dev->descriptor.bNumConfigurations; i++) 
     print_configuration(&dev->config[i]); 

    return 0; 
} 
int htod(const char* str) 
{ 
    int decimal; 
    sscanf(str, "%x", &decimal); 
    return decimal; 
} 
void set_data(struct usb_device *dev) 
{ 

ctrlmsg_param param; 
param.requesttype= 0; 
param.request=0; 
param.value=0; 
param.index=0; 
param.bytes=10; 
param.size=0; 
param.timeout=5000; 
usb_control_msg(dev, param.requesttype, param.request, param.value, param.index, param.bytes, param.size, param.timeout); 
printf("error is %s\n",strerror(errno)); 
return; 

} 
int main(int argc, char *argv[]) 
{ 
    struct usb_bus *bus; 
    struct usb_device *dev; 

    if(argc != 3) 
    { 
     printf("Error in number of arguments\n"); 
     printf("Usage:./usb_info <vendor id> <product id>\n"); 
     exit(0); 
    } 

    vendor_id=htod(argv[1]); 
    product_id=htod(argv[2]); 

    printf("initializing USB library\n"); 
    usb_init(); 

    printf("Finding Buses and Devices\n"); 
    usb_find_busses(); 
    usb_find_devices(); 

    for (bus = usb_get_busses(); bus; bus = bus->next) { 
     for (dev = bus->devices; dev; dev = dev->next) { 
      if ((dev->descriptor.idProduct == product_id) && (dev->descriptor.idVendor == vendor_id)){   
       printf("Found device with produxt id %x and vendor id %x\n",product_id,vendor_id); 
       print_device(dev); 
       set_data(dev); 
       print_device(dev); 
      } 
     } 
    } 
    return 0; 
} 

問候, 桑迪普

+0

解決此問題的任何進展? – RunHolt

回答

2

我認爲你的意思是usb_control_msg()回報「壞描述符」的錯誤代碼。請澄清,如果這是不正確的。

USB控制傳輸具有一些非常特定的格式規則,如果您正在形成的數據包被髮送到任何兼容設備,它將返回總線上的請求錯誤/停頓。

要發送的控制轉移:

bmRequestType = 0x00 
bRequest  = 0x00 
wValue  = 0x0000 
wIndex  = 0x0000 
wSize   = 0x0000 

這應該由USB設備被解釋爲GET_STATUS請求,所以wLength需要是2,和bmRequestType需要具有最高位組,表示這是一個IN方向請求(從主機的角度來看)。這全部來自www.usb.org上USB規範1.1/2.0/3.1的第​​9章。

參數char *bytes(您的param.bytes)也需要是您正在進行的呼叫中的地址/指針。

一個很好的標準控制轉移與將測試:

bmRequestType = 0x80 
bRequest  = 0x06 
wValue  = 0x0001 
wIndex  = 0x0000 
wSize   = 0x0008 

此請求將返回前8個字節的設備描述符,它是適用於每一個USB設備,在所有狀態。

其他傳輸類型(批量,中斷)沒有這些嚴格的格式規則,並且可以更容易啓動。我想你已經移過這個問題了,因爲這個問題已經發布了很長一段時間了,但也許這個迴應仍然會幫助別人。