Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve device information in Linux from device file descriptor using c++

Tags:

c++

linux

How i can get device info including the device name from the device file descriptor?

For example, in SKYPE i can see that device "/dev/snd/pcmC2D0c" has name: "USB 2.0 Camera. USB Audio Default Audio Device (default:CARD=Camera)"

How i can get such device name using c++?

int file = open("/dev/snd/pcmC2D0c", O_RDWR | O_NONBLOCK, 0);
if (-1 == file) // check is error occurred during opening?
{
    perror("open");
    return -1;
}
if (-1 == ioctl(file, /* appropriate ioctl value to get the device info? */, /* appropriate structure to fill with device info? */))
{
    perror("ioctl");
    return -1;
}
like image 324
vdm Avatar asked Apr 24 '26 02:04

vdm


1 Answers

There are no ioctl values for getting device information. In this special case it's a USB device, which has other sources for device type information.

You can look into the sysfs file system, mounted at /sys. There is a directory /sys/bus/usb/devices/, which has symbolic link entries pointing to the real devices.

On my system, there is for example /sys/bus/usb/devices/3-4. This symbolic link leads to the directory /sys/devices/pci0000:00/0000:00:12.0/usb3/3-4/, which has several files identifying the device.

idVendor and idProduct contain the numbers 045e and 00cb. With these numbers, you can look into the file /usr/share/misc/usb.ids and will find

...
045e  Microsoft Corp.
...
        00ca  Fingerprint Reader
        00cb  Basic Optical Mouse v2.0
        00ce  Generic PPC Flash device
...

You have similar information for hard disk drives, for example. /sys/devices/pci0000:00/0000:00:11.0/host0/target0:0:0/0:0:0:0/block/sda/ is my hard disk and the entry device/model in this directory contains the string TOSHIBA DT01ACA1, identifying it as a THOSHIBA 1TB hard disk drive.

like image 118
Olaf Dietsche Avatar answered Apr 26 '26 17:04

Olaf Dietsche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!