How to use libudev in Linux to get USB device VID and PID

How to use libudev in Linux to get USB device VID and PID

In this article, we will use the libudev library to access the hidraw device. Through the libudev library, we can query the device's vendor ID (VID), product ID (PID), serial number, and device string without opening the device. Furthermore, libudev can tell us the specific location path of the device node in the /dev directory, providing a way for applications to access devices that is robust enough and independent of the system manufacturer. To use the libudev library, you need to include the libudev.h header file and add -ludev when compiling to tell the compiler to link the udev library.

All hidraw devices currently connected to the system will be listed, and their device node path, manufacturer, serial number and other information will be output.

To obtain this information, you need to create a udev_enumerate object with the "hidraw" string as the filter condition.

libudev will return all udev_device objects that match the filter string.

The steps for this example are as follows:

1. Initialize the library and get a struct udev handle

2. Enumerate devices

3. Output the node name of the matching device found, find the starting node of the actual USB device, print out the IDs and serial numbers of the USB device, and finally dereference the device object

4. Dereferencing enumeration objects

5. Dereference udev object

The specific code is as follows:

#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;
}

Compile the program:

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing Python to monitor USB device signals under Linux
  • Detailed explanation of Linux USB host driver writing
  • arm linux uses alsa driver and uses usb audio device
  • How to use USB mobile storage in Linux
  • Using USB storage in Linux environment
  • In-depth analysis of Linux NFS mechanism through cases
  • Working principle and example analysis of Linux NFS mechanism
  • IntelliJ IDEA remote debugging Linux Java program, find the problem do not just look at the log (recommended)
  • Use of Linux bzip2 command

<<:  Optimized record of using IN data volume in Mysql

>>:  Web interview frequently asked questions: the principles and differences between reflow and repaint

Recommend

MySQL 5.7.18 Installer installation download graphic tutorial

This article records the detailed installation tu...

Implementing a simple Christmas game with JavaScript

Table of contents Preface Achieve results Code CS...

Web design tips on form input boxes

This article lists some tips and codes about form...

Play and save WeChat public account recording files (convert amr files to mp3)

Table of contents Audio transcoding tools princip...

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

Basic tutorial on using explain statement in MySQL

Table of contents 1. Overview 1. Explain statemen...

Implementation of Docker data volume operations

Getting Started with Data Volumes In the previous...

MySQL sorting feature details

Table of contents 1. Problem scenario 2. Cause An...

Understanding and solutions of 1px line in mobile development

Reasons why the 1px line becomes thicker When wor...

How to change apt-get source in Ubuntu 18.04

When using apt-get to install, it will be very sl...

Basic JSON Operation Guide in MySQL 5.7

Preface Because of project needs, the storage fie...

Example of how to set automatic creation time and modification time in mysql

This article describes how to set the automatic c...