欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux如何使用libudev獲取USB設(shè)備VID及PID

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

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

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

為了獲取這些信息,需要?jiǎng)?chuàng)建一個(gè)udev_enumerate對(duì)象,其中“hidraw”字符串作為過(guò)濾條件,

libudev將返回所有匹配這個(gè)過(guò)濾字符串的udev_device對(duì)象。

這個(gè)列子的步驟如下:

1、 初始化庫(kù),獲取一個(gè)struct udev句柄

2、枚舉設(shè)備

3、對(duì)找到的匹配設(shè)備輸出它的節(jié)點(diǎn)名稱(chēng),找到實(shí)際USB設(shè)備的起始節(jié)點(diǎn),打印出USB設(shè)備的IDs和序列號(hào)等,最后解引用設(shè)備對(duì)象

4、解引用枚舉對(duì)象

5、解引用udev對(duì)象

具體代碼如下:

#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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 自制Linux終端鎖屏工具

    自制Linux終端鎖屏工具

    這篇文章主要為大家詳細(xì)介紹了如何自制Linux終端鎖屏工具,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Linux SecureCRT顯示亂碼解決方案

    Linux SecureCRT顯示亂碼解決方案

    這篇文章主要介紹了Linux SecureCRT顯示亂碼解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • ubuntu18.04獲取root權(quán)限并用root用戶(hù)登錄的實(shí)現(xiàn)

    ubuntu18.04獲取root權(quán)限并用root用戶(hù)登錄的實(shí)現(xiàn)

    這篇文章主要介紹了ubuntu18.04獲取root權(quán)限并用root用戶(hù)登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Angular.JS中指令的命名規(guī)則詳解

    Angular.JS中指令的命名規(guī)則詳解

    這篇文章主要給大家介紹了關(guān)于Angular.JS中指令命名規(guī)則的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • PHP程序員玩轉(zhuǎn)Linux系列 nginx初學(xué)者引導(dǎo)

    PHP程序員玩轉(zhuǎn)Linux系列 nginx初學(xué)者引導(dǎo)

    這篇文章主要為大家詳細(xì)介紹了PHP程序員玩轉(zhuǎn)Linux系列文章,帶大家初步認(rèn)識(shí)一下nginx,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 制作centos基礎(chǔ)鏡像的方法

    制作centos基礎(chǔ)鏡像的方法

    這篇文章主要介紹了制作centos基礎(chǔ)鏡像的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • linux查看匹配內(nèi)容的前后幾行方法

    linux查看匹配內(nèi)容的前后幾行方法

    這篇文章給大家分享了linux查看匹配內(nèi)容的前后幾行的方法以及實(shí)例代碼,有興趣的朋友可以參考學(xué)習(xí)下。
    2018-07-07
  • centos 6.9 升級(jí)glibc動(dòng)態(tài)庫(kù)的詳細(xì)過(guò)程

    centos 6.9 升級(jí)glibc動(dòng)態(tài)庫(kù)的詳細(xì)過(guò)程

    glibc是gnu發(fā)布的libc庫(kù),即c運(yùn)行庫(kù),glibc是linux系統(tǒng)中最底層的api,幾乎其它任何運(yùn)行庫(kù)都會(huì)依賴(lài)于glibc。這篇文章主要介紹了centos 6.9 升級(jí)glibc動(dòng)態(tài)庫(kù)的詳細(xì)過(guò)程,需要的朋友可以參考下
    2019-11-11
  • linux輸入yum后提示: -bash: /usr/bin/yum: No such file or directory的解決方法

    linux輸入yum后提示: -bash: /usr/bin/yum: No such file or director

    在本篇文章里小編給大家整理的是關(guān)于linux輸入yum后提示: -bash: /usr/bin/yum: No such file or directory的解決方法,有需要的朋友們參考下。
    2019-11-11
  • Linux?CentOS7系統(tǒng)中如何添加用戶(hù)

    Linux?CentOS7系統(tǒng)中如何添加用戶(hù)

    這篇文章主要介紹了Linux?CentOS7系統(tǒng)中如何添加用戶(hù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評(píng)論