linux设置lib目录(Linux如何使用libudev获取USB设备VID及PID)
类别:服务器 浏览量:1253
时间:2022-04-01 21:36:46 linux设置lib目录
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-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宝塔面板安装后怎么打开(Linux宝塔面板7.30推出文件外链分享功能)
- kafka 单机安装(在Linux环境下安装Kafka)
- centos7docker部署(CentOS 7下设置Docker代理Linux下Systemd服务的环境变量配置)
- linux安装phpstudy(PHPStudy下如何为Apache安装SSL证书的方法步骤)
- zabbixagent开发环境(Linux zabbix agent部署及配置方法详解)
- linux有哪些ftp服务器软件(Linux系统 改善FTP服务器的安全性)
- php系统化框架教程(PHP+swoole+linux实现系统监控和性能优化操作示例)
- linux如何查看apache日志(限制 Apache日志文件大小的方法)
- linux下安装nginx常见问题(Linux安装Nginx步骤详解)
- linux rsync 迁移数据(如何使用Linux的rsync)
- apache 配置域名(apache 二级域名解析 window与linux)
- mysql和mycat半主从复制项目(Linux如何使用 MyCat 实现 MySQL 主从读写分离)
- linux压缩文件夹zip命令(Linux gzip命令压缩文件实现原理及代码实例)
- linux下python安装pip(在Docker中的ubuntu中安装Python3和Pip的问题)
- linux中apache是否启动怎么查看(Linux apache取消300错误页面Multiple Choices简单步骤)
- linux中dir命令怎么用(Linux sar命令使用方法及代码实例解析)
- 这8种耐阴植物,营造阴生植物花境,也是一个不错的选择(营造阴生植物花境)
- 览邦G08 Plus SMART WATCH 测评⑱ 全独立这才是智能手表该有的样子(览邦G08PlusSMART)
- 荣耀手表 GS 3 真机亮相 不支持无线充电(荣耀手表GS3)
- 通过体温就能为智能手表充电 原来是用NASA在空间站用的黑科技(通过体温就能为智能手表充电)
- 智能手表兼容Windows和Android 无需充电挑战苹果(智能手表兼容Windows和Android)
- 一天一冲也算表 麦步,一款待机 21 天的智能手表体验评测(一天一冲也算表)
热门推荐
- 云服务器怎么上传16g文件(云服务器怎么上传下载文件?)
- vuetable表格合并(vue-table实现添加和删除)
- tomcat在windows下内存配置(浅谈Tomcat内存配置的正确姿势)
- LINQ TO SQL 中join
- vue组件方法里如何修改data(vue项目中使用rem替换px的实现示例)
- php的底层分析(PHP反射原理与用法深入分析)
- python怎么导入xlsx(用Python将结果保存为xlsx的方法)
- vue怎么引入axios(如何用vue封装axios请求)
- pythonselenium隐藏浏览器窗口(Python Selenium 之关闭窗口close与quit的方法)
- html语法规范解释(浅析HTML5 Landmark)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9