Skip to content
Snippets Groups Projects
Unverified Commit afda24d6 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

v4l2: use serial id

Change-Id: I7f4d6e5baef04b99a2cf1435aa6848aff7bd84df
parent 8cce5301
No related branches found
No related tags found
No related merge requests found
...@@ -82,33 +82,11 @@ private: ...@@ -82,33 +82,11 @@ private:
}; };
std::string std::string
getDeviceString(udev* udev, const std::string& dev_path) getDeviceString(struct udev_device* udev_device)
{ {
std::string unique_device_string; if (auto serial = udev_device_get_property_value(udev_device, "ID_SERIAL"))
struct stat statbuf; return serial;
if (stat(dev_path.c_str(), &statbuf) < 0) { throw std::invalid_argument("No ID_SERIAL detected");
return {};
}
auto type = S_ISBLK(statbuf.st_mode) ? 'b' : S_ISCHR(statbuf.st_mode) ? 'c' : 0;
auto opened_dev = udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
auto dev = opened_dev;
while (dev != nullptr) {
auto serial = udev_device_get_sysattr_value(dev, "serial");
if (nullptr == serial) {
dev = udev_device_get_parent(dev);
} else {
unique_device_string += udev_device_get_sysattr_value(dev, "idVendor");
unique_device_string += udev_device_get_sysattr_value(dev, "idProduct");
unique_device_string += serial;
break;
}
}
if (opened_dev) {
udev_device_unref(opened_dev);
}
return unique_device_string;
} }
static int static int
...@@ -167,14 +145,14 @@ VideoDeviceMonitorImpl::VideoDeviceMonitorImpl(VideoDeviceMonitor* monitor) ...@@ -167,14 +145,14 @@ VideoDeviceMonitorImpl::VideoDeviceMonitorImpl(VideoDeviceMonitor* monitor)
// udev_device_get_devnode will fail // udev_device_get_devnode will fail
continue; continue;
} }
auto unique_name = getDeviceString(udev_, path); try {
auto unique_name = getDeviceString(dev);
JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); JAMI_DBG("udev: adding device with id %s", unique_name.c_str());
std::map<std::string, std::string> info = {{"devPath", path}}; std::map<std::string, std::string> info = {{"devPath", path}};
std::vector<std::map<std::string, std::string>> devInfo = {info}; std::vector<std::map<std::string, std::string>> devInfo = {info};
try {
monitor_->addDevice(unique_name, &devInfo); monitor_->addDevice(unique_name, &devInfo);
currentPathToId_.emplace(path, unique_name); currentPathToId_.emplace(path, unique_name);
} catch (const std::runtime_error& e) { } catch (const std::exception& e) {
JAMI_ERR("%s", e.what()); JAMI_ERR("%s", e.what());
} }
} }
...@@ -253,19 +231,16 @@ VideoDeviceMonitorImpl::run() ...@@ -253,19 +231,16 @@ VideoDeviceMonitorImpl::run()
// udev_device_get_devnode will fail // udev_device_get_devnode will fail
break; break;
} }
auto unique_name = getDeviceString(udev_, path); try {
auto unique_name = getDeviceString(dev);
const char* action = udev_device_get_action(dev); const char* action = udev_device_get_action(dev);
if (!strcmp(action, "add")) { if (!strcmp(action, "add")) {
JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); JAMI_DBG("udev: adding device with id %s", unique_name.c_str());
std::map<std::string, std::string> info = {{"devPath", path}}; std::map<std::string, std::string> info = {{"devPath", path}};
std::vector<std::map<std::string, std::string>> devInfo = {info}; std::vector<std::map<std::string, std::string>> devInfo = {info};
try {
monitor_->addDevice(unique_name, &devInfo); monitor_->addDevice(unique_name, &devInfo);
currentPathToId_.emplace(path, unique_name); currentPathToId_.emplace(path, unique_name);
} catch (const std::runtime_error& e) {
JAMI_ERR("%s", e.what());
}
} else if (!strcmp(action, "remove")) { } else if (!strcmp(action, "remove")) {
auto it = currentPathToId_.find(path); auto it = currentPathToId_.find(path);
if (it != currentPathToId_.end()) { if (it != currentPathToId_.end()) {
...@@ -278,6 +253,9 @@ VideoDeviceMonitorImpl::run() ...@@ -278,6 +253,9 @@ VideoDeviceMonitorImpl::run()
monitor_->removeDevice(path); monitor_->removeDevice(path);
} }
} }
} catch (const std::exception& e) {
JAMI_ERR("%s", e.what());
}
} }
udev_device_unref(dev); udev_device_unref(dev);
break; break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment