好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Linux如何使用libudev獲取USB設備VID及PID

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

熱門標簽:江蘇自動外呼系統一般多少錢 信貸電銷機器人系統 400 電話 申請費用 鸚鵡螺號航海地圖標注時間 山東電信外呼系統靠譜嗎 ai電話機器人營銷 比較穩定的外呼系統 長沙回撥外呼系統 云南云電銷機器人招商

在本文將使用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-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

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

標簽:衡陽 運城 烏海 澳門 嘉興 齊齊哈爾 拉薩 亳州

巨人網絡通訊聲明:本文標題《Linux如何使用libudev獲取USB設備VID及PID》,本文關鍵詞  Linux,如何,使用,libudev,獲取,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Linux如何使用libudev獲取USB設備VID及PID》相關的同類信息!
  • 本頁收集關于Linux如何使用libudev獲取USB設備VID及PID的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 美女脱了内衣内裤| 日本高清xxxxxxxxxx不卡免费| 中国女人大白屁股| 91精品国产免费久久久久久| 精品视频一区二区观看| 欧美黑人巨大xxxxxfreexxxxx| 欧美办公室狂躁大交XXOO| 同性男男A片免费播放| 毛片女女女女女女女女女| 美国美女黄色片| 2021videos18xxxx中国| japanesefree成熟老师| 在公交车上强弄到高c的说说| 日韩精品亚洲一级在线观看| 欧美深到肚子的全臂交| 香港十大色禁片观看| ??男生???男生??里| 91肉色超薄丝袜脚交一区二区| 四虎一区二区三区不卡在线| 肥胖粗壮农村妇女毛片| 免费一级乱子伦片| 黄色成人在线观看| 国产精品国产三级国产普通话在线| 麻花星空无限传媒有限公司| 三级电影中文| 后入式射精快| 神院午夜达达兔| 人人爽久久涩噜噜噜网站| 我的放荡丝袜美腿老师漫画| 乱人伦视频免费观看| 免费观看黄| 蜜桃影像传媒文化有限股份公司| 麻豆国产精品无码视频桃花园| 伊人久久中文字幕久久cm| 日韩精品午夜| 麻麻下面好紧怀孕生子小说| 干女人小说| 国产精品99精品免费无码视频 | 国产美女视频免费看网站| 色综合欧美色综合七久久| 邻居让我夜夜爽|