17 #include <boost/filesystem.hpp>
18 #include <mx/ioutils/fileUtils.hpp>
31 const std::string & vendor,
32 const std::string & product,
33 const std::string & serial
36 std::vector<std::string> devNames;
39 devNames = mx::ioutils::getFileNames(
"/sys/class/tty/",
"ttyUSB",
"",
"");
50 for(
size_t i=0; i< devNames.size(); ++i)
52 struct udev_device *dev0;
54 dev0 = udev_device_new_from_syspath(udev, devNames[i].c_str());
58 std::cerr <<
"udev_device_new_from_syspath failed: " << strerror(errno) <<
"\n";
63 struct udev_device *dev;
65 dev = udev_device_get_parent_with_subsystem_devtype( dev0,
"usb",
"usb_device");
69 std::cerr <<
"udev_device_get_parent_with_subsystem_devtype failed: " << strerror(errno) <<
"\n";
71 udev_device_unref(dev0);
75 const char * idVendor = udev_device_get_sysattr_value( dev,
"idVendor" );
77 if(idVendor ==
nullptr)
79 udev_device_unref(dev0);
83 if( strcmp( idVendor, vendor.c_str()) != 0)
85 udev_device_unref(dev0);
89 const char * idProduct = udev_device_get_sysattr_value( dev,
"idProduct" );
91 if(idProduct ==
nullptr)
93 udev_device_unref(dev0);
97 if( strcmp( idProduct, product.c_str()) != 0)
99 udev_device_unref(dev0);
103 const char * dserial = udev_device_get_sysattr_value( dev,
"serial" );
105 if(dserial ==
nullptr)
109 udev_device_unref(dev0);
113 else if( strcmp( dserial, serial.c_str()) != 0 )
115 udev_device_unref(dev0);
120 boost::filesystem::path p(devNames[i]);
121 devName =
"/dev/" + p.filename().string();
123 udev_device_unref(dev0);
138 const std::string & vendor,
139 const std::string & product
142 std::vector<std::string> pdevNames;
146 pdevNames = mx::ioutils::getFileNames(
"/sys/class/tty/",
"ttyUSB",
"",
"");
156 for(
size_t i=0; i< pdevNames.size(); ++i)
158 struct udev_device *dev0;
160 dev0 = udev_device_new_from_syspath(udev, pdevNames[i].c_str());
167 struct udev_device * dev;
168 dev = udev_device_get_parent_with_subsystem_devtype( dev0,
"usb",
"usb_device");
172 udev_device_unref(dev0);
176 const char * idVendor = udev_device_get_sysattr_value( dev,
"idVendor" );
178 if(idVendor ==
nullptr)
180 udev_device_unref(dev0);
184 if( strcmp( idVendor, vendor.c_str()) != 0)
186 udev_device_unref(dev0);
190 const char * idProduct = udev_device_get_sysattr_value( dev,
"idProduct" );
192 if(idProduct ==
nullptr)
194 udev_device_unref(dev0);
198 if( strcmp( idProduct, product.c_str()) != 0)
200 udev_device_unref(dev0);
205 boost::filesystem::path p(pdevNames[i]);
206 devNames.push_back(
"/dev/" + p.filename().string());
208 udev_device_unref(dev0);
int ttyUSBDevName(std::string &devName, const std::string &vendor, const std::string &product, const std::string &serial)
Get the ttyUSB device name for a specific device.
int ttyUSBDevNames(std::vector< std::string > &devNames, const std::string &vendor, const std::string &product)
Get the ttyUSB device name for a set of devices specified by their vendor and product ids.
Error numbers for the tty utilities.
#define TTY_E_UDEVNEWFAILED
#define TTY_E_DEVNOTFOUND
Find the details for USB serial devices.