30 const std::string & vendor,
31 const std::string & product,
32 const std::string & serial
35 typedef mx::verbose::vvv verboseT;
37 std::vector<std::string> devNames;
40 mx_error_check_rv(mx::ioutils::getFileNames(devNames,
"/sys/class/tty/",
"ttyUSB",
"",
""),-1);
51 for(
size_t i=0; i< devNames.size(); ++i)
53 struct udev_device *dev0;
55 dev0 = udev_device_new_from_syspath(udev, devNames[i].c_str());
59 std::cerr <<
"udev_device_new_from_syspath failed: " << strerror(errno) <<
"\n";
64 struct udev_device *dev;
66 dev = udev_device_get_parent_with_subsystem_devtype( dev0,
"usb",
"usb_device");
70 std::cerr <<
"udev_device_get_parent_with_subsystem_devtype failed: " << strerror(errno) <<
"\n";
72 udev_device_unref(dev0);
76 const char * idVendor = udev_device_get_sysattr_value( dev,
"idVendor" );
78 if(idVendor ==
nullptr)
80 udev_device_unref(dev0);
84 if( strcmp( idVendor, vendor.c_str()) != 0)
86 udev_device_unref(dev0);
90 const char * idProduct = udev_device_get_sysattr_value( dev,
"idProduct" );
92 if(idProduct ==
nullptr)
94 udev_device_unref(dev0);
98 if( strcmp( idProduct, product.c_str()) != 0)
100 udev_device_unref(dev0);
104 const char * dserial = udev_device_get_sysattr_value( dev,
"serial" );
106 if(dserial ==
nullptr)
110 udev_device_unref(dev0);
114 else if( strcmp( dserial, serial.c_str()) != 0 )
116 udev_device_unref(dev0);
121 std::filesystem::path p(devNames[i]);
122 devName =
"/dev/" + p.filename().string();
124 udev_device_unref(dev0);
139 const std::string & vendor,
140 const std::string & product
143 std::vector<std::string> pdevNames;
147 typedef mx::verbose::vvv verboseT;
148 mx_error_check_rv(mx::ioutils::getFileNames(pdevNames,
"/sys/class/tty/",
"ttyUSB",
"",
""), -1);
158 for(
size_t i=0; i< pdevNames.size(); ++i)
160 struct udev_device *dev0;
162 dev0 = udev_device_new_from_syspath(udev, pdevNames[i].c_str());
169 struct udev_device * dev;
170 dev = udev_device_get_parent_with_subsystem_devtype( dev0,
"usb",
"usb_device");
174 udev_device_unref(dev0);
178 const char * idVendor = udev_device_get_sysattr_value( dev,
"idVendor" );
180 if(idVendor ==
nullptr)
182 udev_device_unref(dev0);
186 if( strcmp( idVendor, vendor.c_str()) != 0)
188 udev_device_unref(dev0);
192 const char * idProduct = udev_device_get_sysattr_value( dev,
"idProduct" );
194 if(idProduct ==
nullptr)
196 udev_device_unref(dev0);
200 if( strcmp( idProduct, product.c_str()) != 0)
202 udev_device_unref(dev0);
207 std::filesystem::path p(pdevNames[i]);
208 devNames.push_back(
"/dev/" + p.filename().string());
210 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.