最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Linux如何使用libudev獲取USB設備VID及PID

 更新時間:2020年09月11日 09:50:19   作者:陌鉎こ城sHi  
這篇文章主要介紹了Linux如何使用libudev獲取USB設備VID及PID,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在本文將使用libudev庫來訪問hidraw的設備。通過libudev庫,我們可以查詢設備的廠家ID(Vendor ID, VID),產(chǎn)品ID(Product ID, PID),序列號和設備字符串等而不需要打開設備。進一步,libudev可以告訴我們在/dev目錄下設備節(jié)點的具體位置路徑,為應用程序提供一種具有足夠魯棒性而又和系統(tǒng)廠家獨立的訪問設備的方式。使用libudev庫,需要包含libudev.h頭文件,并且在編譯時加上-ludev告訴編譯器去鏈接udev庫。

將列出當前連接在系統(tǒng)中的所有hidraw設備,并且輸出它們的設備節(jié)點路徑、生產(chǎn)商、序列號等信息。

為了獲取這些信息,需要創(chuàng)建一個udev_enumerate對象,其中“hidraw”字符串作為過濾條件,

libudev將返回所有匹配這個過濾字符串的udev_device對象。

這個列子的步驟如下:

1、 初始化庫,獲取一個struct udev句柄

2、枚舉設備

3、對找到的匹配設備輸出它的節(jié)點名稱,找到實際USB設備的起始節(jié)點,打印出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-8 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Apache訪問日志的配置與使用

    Apache訪問日志的配置與使用

    本文主要是在linux下設置apache的httpd.conf配置文件,實現(xiàn)記錄自己想要記錄的web訪問日志,如客戶機IP、連接的日期和時間、響應請求的狀態(tài)代碼等等。
    2018-05-05
  • linux之硬鏈接和軟鏈接解讀

    linux之硬鏈接和軟鏈接解讀

    這篇文章主要介紹了linux之硬鏈接和軟鏈接的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Linux文件查找命令總結(下篇)

    Linux文件查找命令總結(下篇)

    這篇文章主要介紹了Linux文件查找命令總結(下篇),本文章內容詳細,通過案例可以更好的掌握文件查找的相關命令,本篇為下篇,需要的朋友可以參考下
    2023-01-01
  • Linux下nginx配置https協(xié)議訪問的方法

    Linux下nginx配置https協(xié)議訪問的方法

    這篇文章主要介紹了Linux下nginx配置https協(xié)議訪問的方法,需要的朋友可以參考下
    2016-07-07
  • 詳解Linux iptables 命令

    詳解Linux iptables 命令

    iptables 是 Linux 管理員用來設置 IPv4 數(shù)據(jù)包過濾條件和 NAT 的命令行工具。這篇文章較詳細的給大家介紹了Linux iptables 命令,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-07-07
  • 三種方法實現(xiàn)Linux系統(tǒng)調用

    三種方法實現(xiàn)Linux系統(tǒng)調用

    這篇文章主要介紹了三種方法實現(xiàn)Linux系統(tǒng)調用,感興趣的朋友可以參考一下
    2016-01-01
  • Centos7.5配置IP地址的實現(xiàn)

    Centos7.5配置IP地址的實現(xiàn)

    這篇文章主要介紹了Centos7.5配置IP地址的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • 淺析Linux中crontab任務調度

    淺析Linux中crontab任務調度

    這篇文章主要介紹了Linux中crontab任務調度的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • linux 安裝配置lamp v2

    linux 安裝配置lamp v2

    距離第一個版本已經(jīng)有一年了。修正了幾個錯誤的地方,還有取消了某些lib的安裝,因為centos有,所以相關的lib安裝我都沒有去查找錯誤。
    2009-02-02
  • Linux 相對路徑和絕對路徑的使用

    Linux 相對路徑和絕對路徑的使用

    這篇文章主要介紹了Linux 相對路徑和絕對路徑的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02

最新評論

奎屯市| 平度市| 会同县| 中山市| 满城县| 九江市| 洱源县| 泾源县| 明星| 黎川县| 宜黄县| 肥西县| 恩施市| 平舆县| 金乡县| 全南县| 望奎县| 香河县| 吉林市| 松原市| 乌什县| 古蔺县| 吉水县| 凌源市| 临夏县| 蓬莱市| 化隆| 克拉玛依市| 西乌珠穆沁旗| 萍乡市| 大庆市| 普格县| 济源市| 贺兰县| 莆田市| 临潭县| 七台河市| 绥德县| 赣州市| 湘阴县| 任丘市|