承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

Linux基础:怎样使用libudev获得USB装备VID及PID

管理员 2023-08-31 08:04:08 互联网圈 0 ℃ 0 评论 8544字 收藏

Linux基础:怎样使用libudev获得USB装备VID及PID

在本文将使用libudev库来访问hidraw的装备。通过libudev库,我们可以查询装备的厂家ID(Vendor ID, VID),产品ID(Product ID, PID),序列号和装备字符串等而不需要打开装备。进一步,libudev可以告知我们在/dev目录下装备节点的具体位置路径,为利用程序提供一种具有足够鲁棒性而又和系统厂家独立的访问装备的方式。使用libudev库,需要包括libudev.h头文件,并且在编译时加上-ludev告知编译器去链接udev库。

将列出当前连接在系统中的所有hidraw装备,并且输出它们的装备节点路径、生产商、序列号等信息。

为了获得这些信息,需要创建一个udev_enumerate对象,其中“hidraw”字符串作为过滤条件,

libudev将返回所有匹配这个过滤字符串的udev_device对象。

这个列子的步骤以下:

1、 初始化库,获得一个struct udev句柄

2、枚举装备

3、对找到的匹配装备输出它的节点名称,找到实际USB装备的起始节点,打印出USB装备的IDs和序列号等,最后解援用装备对象

4、解援用枚举对象

5、解援用udev对象

具体代码以下:

#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>

int main (void)
{
  struct udev *udev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  struct udev_device *dev;

  /* Create the udev object */
  udev = udev_new();
  if (!udev) {
    printf("Can't create udev\n");
    exit(1);
  }

  /* Create a list of the devices in the 'hidraw' subsystem. */
  enumerate = udev_enumerate_new(udev);
  udev_enumerate_add_match_subsystem(enumerate, "hidraw");
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  /* For each item enumerated, print out its information.
    udev_list_entry_foreach is a macro which expands to
    a loop. The loop will be executed for each member in
    devices, setting dev_list_entry to a list entry
    which contains the device's path in /sys. */
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path;

    /* Get the filename of the /sys entry for the device
      and create a udev_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("Device Node Path: %s\n", udev_device_get_devnode(dev));

    /* The device pointed to by dev contains information about
      the hidraw device. In order to get information about the
      USB device, get the parent device with the
      subsystem/devtype pair of "usb"/"usb_device". This will
      be several levels up the tree, but the function will find
      it.*/
    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, we can call get_sysattr_value() for each file
      in the device's /sys entry. The strings passed into these
      functions (idProduct, idVendor, serial, etc.) correspond
      directly to the files in the directory which represents
      the USB device. Note that USB strings are Unicode, UCS2
      encoded, but the strings returned from
      udev_device_get_sysattr_value() are UTF⑻ encoded. */
    printf(" VID/PID: %s %s\n",
        udev_device_get_sysattr_value(dev,"idVendor"),
        udev_device_get_sysattr_value(dev, "idProduct"));
    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"));
    udev_device_unref(dev);
  }
  /* Free the enumerator object */
  udev_enumerate_unref(enumerate);

  udev_unref(udev);

  return 0;
}

编译程序:

gcc -Wall -g -o udev_example udev_example.c -ludev

在本文将使用libudev库来访问hidraw的装备。通过libudev库,我们可以查询装备的厂家ID(Vendor ID, VID),产品ID(Product ID, PID),序列号和装备字符串等而不需要打开装备。进一步,libudev可以告知我们在/dev目录下装备节点的具体位置路径,为利用程序提供一种具有足够鲁棒性而又和系统厂家独立的访问装备的方式。使用libudev库,需要包括libudev.h头文件,并且在编译时加上-ludev告知编译器去链接udev库。

将列出当前连接在系统中的所有hidraw装备,并且输出它们的装备节点路径、生产商、序列号等信息。

为了获得这些信息,需要创建一个udev_enumerate对象,其中“hidraw”字符串作为过滤条件,

libudev将返回所有匹配这个过滤字符串的udev_device对象。

这个列子的步骤以下:

1、 初始化库,获得一个struct udev句柄

2、枚举装备

3、对找到的匹配装备输出它的节点名称,找到实际USB装备的起始节点,打印出USB装备的IDs和序列号等,最后解援用装备对象

4、解援用枚举对象

5、解援用udev对象

具体代码以下:

#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>

int main (void)
{
  struct udev *udev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  struct udev_device *dev;

  /* Create the udev object */
  udev = udev_new();
  if (!udev) {
    printf("Can't create udev\n");
    exit(1);
  }

  /* Create a list of the devices in the 'hidraw' subsystem. */
  enumerate = udev_enumerate_new(udev);
  udev_enumerate_add_match_subsystem(enumerate, "hidraw");
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  /* For each item enumerated, print out its information.
    udev_list_entry_foreach is a macro which expands to
    a loop. The loop will be executed for each member in
    devices, setting dev_list_entry to a list entry
    which contains the device's path in /sys. */
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path;

    /* Get the filename of the /sys entry for the device
      and create a udev_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("Device Node Path: %s\n", udev_device_get_devnode(dev));

    /* The device pointed to by dev contains information about
      the hidraw device. In order to get information about the
      USB device, get the parent device with the
      subsystem/devtype pair of "usb"/"usb_device". This will
      be several levels up the tree, but the function will find
      it.*/
    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, we can call get_sysattr_value() for each file
      in the device's /sys entry. The strings passed into these
      functions (idProduct, idVendor, serial, etc.) correspond
      directly to the files in the directory which represents
      the USB device. Note that USB strings are Unicode, UCS2
      encoded, but the strings returned from
      udev_device_get_sysattr_value() are UTF⑻ encoded. */
    printf(" VID/PID: %s %s\n",
        udev_device_get_sysattr_value(dev,"idVendor"),
        udev_device_get_sysattr_value(dev, "idProduct"));
    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"));
    udev_device_unref(dev);
  }
  /* Free the enumerator object */
  udev_enumerate_unref(enumerate);

  udev_unref(udev);

  return 0;
}

编译程序:

gcc -Wall -g -o udev_example udev_example.c -ludev

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

文章来源:丸子建站

文章标题:Linux基础:怎样使用libudev获得USB装备VID及PID

https://www.wanzijz.com/view/75587.html

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信