libudev库接口的一些相关资料
2015-04-24 16:25
375 查看
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;
}
链接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;
}
相关文章推荐
- 基于HBase Thrift接口的一些使用问题及相关注意事项的详解
- SATA 接口相关资料。
- 一些和ajax相关的资料
- CIM Provider 的一些相关资料
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- 【计算机】收集的一些FLASH相关的技术资料
- [置顶] 第二篇 KinectV2结合opencv入门开发以及一些相关的学习资料
- linux 路由表 的一些相关资料
- 一些有关sharepoint WebServices相关资料
- 设计模似和F#相关资料的一些链接
- 虚幻4相关的一些资料
- 收集的一些FLASH相关的技术资料(补)
- Asp.net/C#相关一些学习资料, 非常好!!! 进来看一下.
- pyqt4的一些相关资料整理
- 一些相关资料
- 移植uboot过程中 nand flash 驱动相关的一些资料分享
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- 基于HBase Thrift接口的一些使用问题及相关注意事项的详解
- smartClient的一些相关资料.
- 今天开始要详细的记录学习sharepoint 的进度和相关的一些资料