Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unique serial number of USB device mounted to /dev folder

I attach 2 webcam to computer and it was listed in /dev folder: /dev/video0; /dev/video1.

Can you help me write C code to get serial number of webcam with input: /dev/video[0;1]

like image 825
nhuanvd Avatar asked Sep 04 '13 04:09

nhuanvd


2 Answers

Just ran into this same problem and it took a bit to find the solution. Any solution which starts with "just use lsusb" is incorrect. You can figure out the devices serial, but none of the extra information it provides help you determine which /dev/video it links to.

Solution:

/bin/udevadm info --name=/dev/video1 | grep SERIAL_SHORT

Output:

E: ID_SERIAL_SHORT=256DEC57
like image 91
lessthanoptimal Avatar answered Oct 02 '22 08:10

lessthanoptimal


Based on the hint of using udevadm and the tutorial from http://www.signal11.us/oss/udev/ I got below code to get the serial info of my webcam.

#include "stdio.h"
#include <libudev.h>

int main(int argc, char **argv)
{
  struct udev *udev;
  struct udev_device *dev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *list, *node;
  const char *path;

  udev = udev_new();
  if (!udev) {
    printf("can not create udev");
    return 0;
  }

  enumerate = udev_enumerate_new(udev);
  udev_enumerate_add_match_subsystem(enumerate, "video4linux");
  udev_enumerate_scan_devices(enumerate);

  list = udev_enumerate_get_list_entry(enumerate);
  udev_list_entry_foreach(node, list) {
    path = udev_list_entry_get_name(node);
    dev = udev_device_new_from_syspath(udev, path);

    printf("Printing serial for %s\n", path);
    printf("ID_SERIAL=%s\n",
        udev_device_get_property_value(dev, "ID_SERIAL"));
    printf("ID_SERIAL_SHORT=%s\n",
        udev_device_get_property_value(dev, "ID_SERIAL_SHORT"));

    udev_device_unref(dev);
  }
  return 0;
}
like image 36
chouger Avatar answered Oct 02 '22 10:10

chouger