aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Verkuil <hverkuil-cisco@xs4all.nl>2019-02-04 13:00:32 (GMT)
committerHans Verkuil <hverkuil-cisco@xs4all.nl>2019-02-04 13:00:32 (GMT)
commit4ee4b43b657f6ba951fb68d44ce5488f6f4d7fff (patch)
treef53d3748acee0a4c3a331328f8da7344038e2259
parent9b9c62e464fc97824d64dc6abc1ad33727f77003 (diff)
cec-info: add cec_device_find function
This function will find the first cec device whose driver and adapter names as returned by CEC_ADAP_G_CAPS matches the driver and adapter names given as arguments. This allows you to uniquely specify a cec device without having to rely on the device node numbering. Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
-rw-r--r--utils/common/cec-info.cpp40
-rw-r--r--utils/common/cec-info.h2
2 files changed, 42 insertions, 0 deletions
diff --git a/utils/common/cec-info.cpp b/utils/common/cec-info.cpp
index b91bdf3..31fb09f 100644
--- a/utils/common/cec-info.cpp
+++ b/utils/common/cec-info.cpp
@@ -7,6 +7,10 @@
#include <stdio.h>
#include <string>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <sys/ioctl.h>
#include <cec-info.h>
static std::string caps2s(unsigned caps)
@@ -438,3 +442,39 @@ void cec_driver_info(const struct cec_caps &caps,
}
}
}
+
+std::string cec_device_find(const char *driver, const char *adapter)
+{
+ DIR *dp;
+ struct dirent *ep;
+ std::string name;
+
+ dp = opendir("/dev");
+ if (dp == NULL) {
+ perror("Couldn't open the directory");
+ return name;
+ }
+ while ((ep = readdir(dp)))
+ if (!memcmp(ep->d_name, "cec", 3) && isdigit(ep->d_name[3])) {
+ std::string devname("/dev/");
+ struct cec_caps caps;
+ int fd;
+
+ devname += ep->d_name;
+ fd = open(devname.c_str(), O_RDWR);
+
+ if (fd < 0)
+ continue;
+ int err = ioctl(fd, CEC_ADAP_G_CAPS, &caps);
+ close(fd);
+ if (err)
+ continue;
+ if ((!driver || !strcmp(driver, caps.driver)) &&
+ (!adapter || !strcmp(adapter, caps.name))) {
+ name = devname;
+ break;
+ }
+ }
+ closedir(dp);
+ return name;
+}
diff --git a/utils/common/cec-info.h b/utils/common/cec-info.h
index a086ece..e979b5a 100644
--- a/utils/common/cec-info.h
+++ b/utils/common/cec-info.h
@@ -26,4 +26,6 @@ std::string status2s(const struct cec_msg &msg);
void cec_driver_info(const struct cec_caps &caps,
const struct cec_log_addrs &laddrs, __u16 phys_addr);
+std::string cec_device_find(const char *driver, const char *adapter);
+
#endif

Privacy Policy