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

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

In-depth analysis of MySQL lock blocking

In daily maintenance, threads are often blocked, ...

Tutorial on how to install htop on CentOS 8

If you are looking to monitor your system interac...

Learn Hyperlink A Tag

ask: I have styled the hyperlink using CSS, but i...

Drop-down menu and sliding menu design examples

I found a lot of websites that use drop-down or sl...

How to strike a balance between ease of use and security in the login interface

Whether you are a web designer or a UI designer, ...

Detailed explanation of MySQL multi-table join query

Table of contents Multi-table join query Inner Jo...

Detailed explanation of MySql view trigger stored procedure

view: When a temporary table is used repeatedly, ...

Node script realizes automatic sign-in and lottery function

Table of contents 1. Introduction 2. Preparation ...

Detailed explanation of building MySQL master-slave environment with Docker

Preface This article records how I use docker-com...

A brief discussion on the problem of Docker run container being in created state

In a recent problem, there is such a phenomenon: ...