2011/02/14

透過 lsusb 研究 libusb

lsusb 是個很好用的工具,可以在 libusb取得,說明文件在這兒

打開 lsusb.c 可以看到程式架構很簡單除了初始化及後面的 print 及 free 外,比較重要的只有:
cnt = libusb_get_device_list(NULL, &devs)

原始碼如下:

int main(void)
{
libusb_device **devs;
int r;
ssize_t cnt;

r = libusb_init(NULL);
if (r < 0) return r; cnt = libusb_get_device_list(NULL, &devs); if (cnt < 0) return (int) cnt; print_devs(devs); libusb_free_device_list(devs, 1); libusb_exit(NULL); return 0; }


透過 libusb_get_device_list() 可以得到目前所有的 devices, 稍後再來看,先看看 print_devs()

static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0;

while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) { fprintf(stderr, "failed to get device descriptor"); return; } printf("%04x:%04x (bus %d, device %d)\n", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); } }

對每個 usb device 做的動作就只有:
int r = libusb_get_device_descriptor(dev, &desc);
然後把 desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev) 印出來

下一篇再來研究一下剩下的這兩個:
cnt = libusb_get_device_list(NULL, &devs)
int r = libusb_get_device_descriptor(dev, &desc);

0 意見: