Linux:试图通过ioctl获取操纵杆供应商和产品ID,请改为获取EINVAL

Linux:试图通过ioctl获取操纵杆供应商和产品ID,请改为获取EINVAL,linux,ubuntu,usb,ioctl,joystick,Linux,Ubuntu,Usb,Ioctl,Joystick,我正在尝试读取Ubuntu上USB操纵杆的名称、供应商ID和产品ID(具体来说,我正在使用Ubuntu 13.10 x64上的有线Xbox 360键盘)。我可以读取名称,但在尝试读取供应商和产品ID时,我会出现EINVAL错误。代码如下: if (plugged[index]) { char name[32]; std::snprintf(name, sizeof(name), "/dev/input/js%u", index); // Open the joystic

我正在尝试读取Ubuntu上USB操纵杆的名称、供应商ID和产品ID(具体来说,我正在使用Ubuntu 13.10 x64上的有线Xbox 360键盘)。我可以读取名称,但在尝试读取供应商和产品ID时,我会出现
EINVAL
错误。代码如下:

if (plugged[index])
{
    char name[32];
    std::snprintf(name, sizeof(name), "/dev/input/js%u", index);

    // Open the joystick's file descriptor (read-only and non-blocking)
    m_file = ::open(name, O_RDONLY | O_NONBLOCK);
    if (m_file >= 0)
    {
        // Retrieve the axes mapping
        ioctl(m_file, JSIOCGAXMAP, m_mapping);

        // Get the name
        char joyname[128];
        if (ioctl(m_file, JSIOCGNAME(128), joyname) < 0) {
            m_name = "Unknown Joystick";
        } else {
            m_name = joyname;
        }

        // Get vendor and product IDs
        input_id inpid;
        if (ioctl(m_file, EVIOCGID, &inpid) < 0) {
            if (errno == EBADF) printf("EBADF\n");
            if (errno == EFAULT) printf("EFAULT\n");
            if (errno == ENOTTY) printf("ENOTTY\n");
            if (errno == EINVAL) printf("EINVAL\n");
            m_manufacturerID = 0;
            m_productID = 0;
        } else {
            m_manufacturerID = inpid.vendor;
            m_productID = inpid.product;
        }

        // Reset the joystick state
        m_state = JoystickState();

        return true;
    }
    else
    {
        return false;
    }
}
else
{
    return false;
}
根据,
EINVAL
请求(
EVIOCGID
)或argp(
inpid
)无效


如何确定哪个无效?

在进一步挖掘之后,我发现ioctl(m_文件、EVIOCGID和inpid)失败的原因是我打开的设备是一个操纵杆(
/dev/input/js
),而
EVIOCGID
ioctl是事件设备(
/dev/input/event
),因此失败。不幸的是,没有
JSIOCGID
ioctl,所以我不得不改变策略。相反,我使用访问操纵杆的供应商和产品ID。以下是我使用的代码:

// Use udev to look up the product and manufacturer IDs
struct udev *udev = udev_new();
if (udev)
{
    char sysname[32];
    std::snprintf(sysname, sizeof(sysname), "js%u", index);
    struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
    dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
    if (!dev)
    {
        err() << "Unable to find parent USB device" << std::endl;
    }

    std::stringstream ss;
    ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
    ss >> m_manufacturerID;

    ss.clear();
    ss.str("");
    ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
    ss >> m_productID;

    udev_device_unref(dev);
    udev_unref(udev);
}
else
{
    err() << "Cannot create udev" << std::endl;
}
//使用udev查找产品和制造商ID
结构udev*udev=udev_new();
if(udev)
{
字符系统名[32];
std::snprintf(sysname,sizeof(sysname),“js%u”,索引);
struct udev_device*dev=udev_device_new_from_subsystem_sysname(udev,“input”,sysname);
dev=udev_device_get_parent_与_subsystem_devtype(dev,“usb”,“usb_device”);
如果(!dev)
{

err()只是一个更清晰的示例,带有小错误修复:

#include <iostream>
#include <libudev.h>
#include <sstream>

bool getJoystickInfo(int index, std::string& manufacturerID, std::string& productID, std::string& message)
{
    // Use udev to look up the product and manufacturer IDs
    struct udev *udev = udev_new();
    if (udev) {
        char sysname[32];
        std::snprintf(sysname, sizeof(sysname), "js%u", index);
        struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
        dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
        if (!dev)
        {
            message = "Unable to find parent USB device";
            return false;
        }

        std::stringstream ss;
        ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
        ss >> manufacturerID;

        ss.clear();
        ss.str("");
        ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
        ss >> productID;

        udev_device_unref(dev);
    }
    else
    {
        message = "Cannot create udev";
        return false;
    }
    udev_unref(udev);
    return true;
}

int main() {
    std::string manufacturerID, productID, message;
    if (getJoystickInfo(1, manufacturerID, productID, message))
        std::cout << manufacturerID << "\t" << productID << std::endl;
    else
        std::cerr << message << std::endl;
    return 0;
}
#include <iostream>
#include <libudev.h>
#include <sstream>

bool getJoystickInfo(int index, std::string& manufacturerID, std::string& productID, std::string& message)
{
    // Use udev to look up the product and manufacturer IDs
    struct udev *udev = udev_new();
    if (udev) {
        char sysname[32];
        std::snprintf(sysname, sizeof(sysname), "js%u", index);
        struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
        dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
        if (!dev)
        {
            message = "Unable to find parent USB device";
            return false;
        }

        std::stringstream ss;
        ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
        ss >> manufacturerID;

        ss.clear();
        ss.str("");
        ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
        ss >> productID;

        udev_device_unref(dev);
    }
    else
    {
        message = "Cannot create udev";
        return false;
    }
    udev_unref(udev);
    return true;
}

int main() {
    std::string manufacturerID, productID, message;
    if (getJoystickInfo(1, manufacturerID, productID, message))
        std::cout << manufacturerID << "\t" << productID << std::endl;
    else
        std::cerr << message << std::endl;
    return 0;
}
target_link_libraries(MyExecutable udev)