http://www.freedesktop.org/software/systemd/libudev/
链接libudev库的一些接口函数
#include<libudev.h>
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<unistd.h>
int main(void)
{
struct udev*udev;
structudev_enumerate *enumerate;
structudev_list_entry *devices, *dev_list_entry;
structudev_device *dev;
int ret;
/*Create the udev object */
udev =udev_new();
if (!udev) {
printf("Can'tcreate udev\n");
exit(1);
}
/* Create a listof the devices in the 'hidraw' subsystem. *//*创建一个枚举设备的链表*/
enumerate =udev_enumerate_new(udev);
//udev_enumerate_add_match_subsystem(enumerate, "block");
udev_enumerate_add_match_sysattr(enumerate,"idVendor","125f");/*设置链表的匹配类型*/
ret =udev_enumerate_scan_devices(enumerate); /*搜索相匹配的设备*/
devices =udev_enumerate_get_list_entry(enumerate); /*获取*/
/* For each itemenumerated, print out its information.
udev_list_entry_foreach is a macro which expands to
a loop. Theloop will be executed for each member in
devices,setting dev_list_entry to a list entry
which containsthe device's path in /sys. */
udev_list_entry_foreach(dev_list_entry,devices) {
constchar *path;
/* Get thefilename of the /sys entry for the device
and create audev_device object (dev) representing it */
path =udev_list_entry_get_name(dev_list_entry); /*获取到的是设备的地址*/
dev =udev_device_new_from_syspath(udev, path); /*获取相应的设备*/
/*usb_device_get_devnode() returns the path to the device node
itself in/dev. */ /*获取设备的相关信息*/
printf("DeviceNode Path: %s\n", udev_device_get_devnode(dev));
printf("Devicesysname: %s\n", udev_device_get_sysname(dev));
printf("Devicesyspath: %s\n", udev_device_get_syspath(dev));
printf("Devicedevtype: %s\n", udev_device_get_devtype(dev));
printf("Device subsystem:%s\n", udev_device_get_subsystem(dev));
printf("Device devpath:%s\n", udev_device_get_devpath(dev));
printf("Devicesysnum: %s\n", udev_device_get_sysnum(dev));
printf("Devicedriver: %s\n", udev_device_get_driver(dev));
printf("Deviceaction: %s\n", udev_device_get_action(dev));
printf("Device devnum: %ld\n",udev_device_get_devnum(dev));
printf(" VID/PID: %s%s\n",
udev_device_get_sysattr_value(dev,"idVendor"),
udev_device_get_sysattr_value(dev, "idProduct"));
printf(" serial: %s\n",
udev_device_get_sysattr_value(dev, "serial"));
/* The devicepointed to by dev contains information about
the hidrawdevice. In order to get information about the
USB device,get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". Thiswill
be severallevels up the tree, but the function will find
it.*/
//dev =udev_device_new_from_devnum (udev,'c',udev_device_get_devnum(dev));
dev =udev_device_new_from_subsystem_sysname(udev,"block","sdc");/*通过设备节点获取相应的设备信息*/
dev =udev_device_get_parent_with_subsystem_devtype(
dev,
"usb",
"usb_device");
if (!dev) {
printf("Unable to find parent usb device.");
exit(1);
}
/* From here, wecan call get_sysattr_value() for each file
in thedevice's /sys entry. The strings passed into these
functions(idProduct, idVendor, serial, etc.) correspond
directly tothe files in the directory which represents
the USBdevice. Note that USB strings are Unicode, UCS2
encoded, butthe strings returned from
udev_device_get_sysattr_value() are UTF-8encoded. */
printf(" VID/PID: %s%s\n",
udev_device_get_sysattr_value(dev,"idVendor"),
udev_device_get_sysattr_value(dev, "idProduct")); //获取到的是/dev/sdc的PID , VID
printf(" %s\n %s\n",
udev_device_get_sysattr_value(dev,"manufacturer"),
udev_device_get_sysattr_value(dev,"product"));
printf(" serial:%s\n",
udev_device_get_sysattr_value(dev, "serial"));
printf(" class: %s\n",
udev_device_get_sysattr_value(dev,"bInterfaceClass"));
printf(" bcdUSB: %d\n",
udev_device_get_sysattr_value(dev, "bcdUSB"));
udev_device_unref(dev);
}
/* Free theenumerator object */
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}