Get device path based on USB VID:PID in Linux

試著忘記壹切 提交于 2021-02-07 19:39:16

问题


If I plug in a device, say /dev/ttyUSB0 and I want to get the number 0 based on its VID:PID (found with lsusb), how could I do that in C++ Linux? I have this code to find one printer device, if it's helpful at all:

int printer_open (void)
{    
    char printer_location[] = "/dev/usb/lpX";
    struct stat buf;

    // continuously try all numbers until stat returns true for the connected printer
    for (int i = 0; i < 10; i++)
    {
        printer_location[11] = '0' + i;
        if (!stat (printer_location, &buf))
            break;
    }

    return 0;
}

回答1:


You could use libusb
apt-get install build-essential libudev-dev
Here is a good example:
http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
and here is the lib description:
http://libusb.sourceforge.net/api-1.0/

int main() {
    libusb_context *context = NULL;
    libusb_device **list = NULL;
    int rc = 0;
    ssize_t count = 0;

    rc = libusb_init(&context);
    assert(rc == 0);

    count = libusb_get_device_list(context, &list);
    assert(count > 0);

    for (size_t idx = 0; idx < count; ++idx) {
        libusb_device *device = list[idx];
        libusb_device_descriptor desc = {0};

        rc = libusb_get_device_descriptor(device, &desc);
        assert(rc == 0);

        printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
    }
}

And if you compile your code don't forget to add the lib reference -I/usr/include/libusb-1.0/ and - lusb-1.0




回答2:


libusb can't get it actually. So look at this file instead: /proc/bus/input/devices

Example line from the file:

I: Bus=0003 Vendor=1a2c Product=0c23 Version=0110
N: Name="USB USB Keyboard"
P: Phys=usb-0000:00:14.0-3/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/0003:1A2C:0C23.0015/input/input30
U: Uniq=
H: Handlers=sysrq kbd event10 leds
B: PROP=0
B: EV=120013
B: KEY=1000000000007 ff800000000007ff febeffdff3cfffff fffffffffffffffe
B: MSC=10
B: LED=7 

This function gets the event number from the device with the matching VID:PID:

#include <string>
#include <iostream>
#include <fstream>

void open_device (std::string device_vid, std::string device_pid)
{       
    try
    {
        std::ifstream file_input;
        std::size_t pos;
        std::string device_path, current_line, search_str, event_str;
        std::string device_list_file = "/proc/bus/input/devices";
        bool vid_pid_found = false;
        int fd = 0;
        bool debug = true;

        // 1. open device list file
        file_input.open(device_list_file.c_str());
        if (!file_input.is_open())
        {
            std::cerr << "file_input.open >> " << std::strerror(errno) << std::endl;
            throw -2;
        }

        // 2. search for first VID:PID and get event number
        search_str = "Vendor=" + device_vid + " Product=" + device_pid;
        while (getline(file_input, current_line))
        {
            if (!vid_pid_found)
            {
                pos = current_line.find(search_str, 0);
                if (pos != std::string::npos)
                {
                    vid_pid_found = true;
                    search_str = "event";
                }               
            }
            else
            {
                pos = current_line.find(search_str, 0);
                if (pos != std::string::npos)
                {
                    event_str = current_line.substr(pos);
                    // find space and substring event##
                    pos = event_str.find(' ', 0);
                    event_str = event_str.substr(0, pos);
                    break;
                }
            }
        }

        // 3.  build device path
        device_path = "/dev/input/" + event_str;
        if (debug) std::cout << "device_path = " << device_path << std::endl;   

        // 4.  connect to device
        fd = open (device_path.c_str(), O_RDONLY);
        if (fd < 0)
        {
            std::cerr << "open >> errno = " << std::strerror(errno) << std::endl;       
            throw -3;
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << "e.what() = " << e.what() << std::endl;
        throw -1;
    }

    return;
}


来源:https://stackoverflow.com/questions/37417618/get-device-path-based-on-usb-vidpid-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!