注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liangxh2008的博客

 
 
 

日志

 
 

Getting Started with libusb on Ubuntu  

2010-05-12 15:39:48|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Getting Started with libusb on Ubuntu

What is libusb?

libusb is a library which user level applications can used to access USB devices without worrying about the underlying operating system. Or we can see it as a high-level API which wraps low-level kernel interactions with USB modules and provide developer with the facility to implement device driver for a USB device from the user space.

libusb is easy to use and libusb API provide high level abstraction to the Kernel structures and allows the developers to have access to these structures through the USBFS(USBfilesystem).

libusb supports Linux, FreeBSD, NetBSD, OpenBSD, Darwin, MacOS X and Windows through the libusb-win32 project.

Installing libusb on Ubuntu

libusb-0.1-4 and libusb-dev packages are available in Ubuntu 8.04 repositories. You can use follwing command to install them.

sudo apt-get install libusb-dev libusb-0.1-4

Simple Example

Here is a simple example application which gather all the possible technical/hardware details of a USB devices connected to the computer. Some devices will not contain some details. This example is based on the Developing Linux Device Drivers using Libusb API article. Inline comments will describe the code.

Make sure that you have at least one USB device plugged into your computer.

#include <stdio.h>
#include <usb.h>

void print_endpoint(struct usb_endpoint_descriptor *endpoint) {
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\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(" 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(" 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: %02xh\n", config->bmAttributes);
printf(" MaxPower: %d\n", config->MaxPower);

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

int main(void) {

struct usb_bus *bus;
struct usb_device *dev;

/* Initialize libusb */
usb_init();

/* Find all USB busses on system */
usb_find_busses();

/* Find all devices on all USB devices */
usb_find_devices();

printf("bus/device idVendor/idProduct\n");

/* usb_busses is a global variable. */
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
int ret, i;
char string[256];
usb_dev_handle *udev;

printf("%s/%s %04X/%04X\n", bus->dirname, dev->filename,
dev->descriptor.idVendor, dev->descriptor.idProduct);

/* Opens a USB device */
udev = usb_open(dev);
if (udev) {
if (dev->descriptor.iManufacturer) {

/* Retrieves a string descriptor from a device using the first language */
ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof (string));
if (ret > 0)
printf("- Manufacturer : %s\n", string);
else
printf("- Unable to fetch manufacturer string\n");
}

if (dev->descriptor.iProduct) {
ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof (string));
if (ret > 0)
printf("- Product : %s\n", string);
else
printf("- Unable to fetch product string\n");
}

if (dev->descriptor.iSerialNumber) {
ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof (string));
if (ret > 0)
printf("- Serial Number: %s\n", string);
else
printf("- Unable to fetch serial number string\n");
}

/* Closes a USB device */
usb_close(udev);
}

if (!dev->config) {
printf(" Couldn't retrieve descriptors\n");
continue;
}

for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
print_configuration(&dev->config[i]);
}
}

return 0;
}

For further details about the structures please look at the /usr/include/usb.h file.

  评论这张
 
阅读(784)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017