[libcamera-devel,v3,1/5] libcamera: v4l2_device: Add method to lookup device path

Message ID 20200728234225.3505868-1-niklas.soderlund@ragnatech.se
State Superseded
Headers show
Series
  • libcamera: Generate unique and stable camera names
Related show

Commit Message

Niklas Söderlund July 28, 2020, 11:42 p.m. UTC
Add a method to lookup a V4L2 devices path in sysfs.

Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
---
 include/libcamera/internal/v4l2_device.h |  1 +
 src/libcamera/v4l2_device.cpp            | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

Comments

Jacopo Mondi July 29, 2020, 7:28 a.m. UTC | #1
Hi Niklas,

On Wed, Jul 29, 2020 at 01:42:21AM +0200, Niklas Söderlund wrote:
> Add a method to lookup a V4L2 devices path in sysfs.
>
> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
> ---
>  include/libcamera/internal/v4l2_device.h |  1 +
>  src/libcamera/v4l2_device.cpp            | 24 ++++++++++++++++++++++++
>  2 files changed, 25 insertions(+)
>
> diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h
> index bf643f2ec966bb33..3b605aab343b3b94 100644
> --- a/include/libcamera/internal/v4l2_device.h
> +++ b/include/libcamera/internal/v4l2_device.h
> @@ -30,6 +30,7 @@ public:
>  	int setControls(ControlList *ctrls);
>
>  	const std::string &deviceNode() const { return deviceNode_; }
> +	std::string devicePath() const;
>
>  protected:
>  	V4L2Device(const std::string &deviceNode);
> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> index 56ea1ddda2c1425f..c186f246dbfaff66 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -9,6 +9,7 @@
>
>  #include <fcntl.h>
>  #include <iomanip>
> +#include <limits.h>
>  #include <string.h>
>  #include <sys/ioctl.h>
>  #include <sys/syscall.h>
> @@ -350,6 +351,29 @@ int V4L2Device::setControls(ControlList *ctrls)
>  	return ret;
>  }
>
> +/**
> + * \brief Retrieve the device path

"the device path in sysfs" or "under /sys" not sure which one is the
most correct one. But I think it's worth pointing it out as when I
read "the device path" I thought about the device node path in /dev/

> + *
> + * The device path describes the device backing the V4L2 device.

same

> + *
> + * \todo When switching to C++17 use std::filesystem:: in the implementation.
> + * \todo Query udev for this information as the path created for vdev here
> + * migh not be correct.
> + *
> + * \return The device path

same here

> + */
> +std::string V4L2Device::devicePath() const
> +{
> +	std::string vdev = basename(deviceNode_.c_str());
> +	std::string sysfs = "/sys/class/video4linux/" + vdev + "/device";
> +
> +	char path[PATH_MAX];
> +	if (!realpath(sysfs.c_str(), path))
> +		LOG(V4L2, Fatal) << "Can not resolve path for " << deviceNode_;
> +
> +	return path;
> +}
> +

I think Laurent had a comment suggesting to have this go through the
device enumerator sub-classes. I can't tell why both him and you say
that "the path here might not be correct", so I'll just re-state his
comment here.

From my side
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

>  /**
>   * \brief Perform an IOCTL system call on the device node
>   * \param[in] request The IOCTL request code
> --
> 2.27.0
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h
index bf643f2ec966bb33..3b605aab343b3b94 100644
--- a/include/libcamera/internal/v4l2_device.h
+++ b/include/libcamera/internal/v4l2_device.h
@@ -30,6 +30,7 @@  public:
 	int setControls(ControlList *ctrls);
 
 	const std::string &deviceNode() const { return deviceNode_; }
+	std::string devicePath() const;
 
 protected:
 	V4L2Device(const std::string &deviceNode);
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index 56ea1ddda2c1425f..c186f246dbfaff66 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -9,6 +9,7 @@ 
 
 #include <fcntl.h>
 #include <iomanip>
+#include <limits.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/syscall.h>
@@ -350,6 +351,29 @@  int V4L2Device::setControls(ControlList *ctrls)
 	return ret;
 }
 
+/**
+ * \brief Retrieve the device path
+ *
+ * The device path describes the device backing the V4L2 device.
+ *
+ * \todo When switching to C++17 use std::filesystem:: in the implementation.
+ * \todo Query udev for this information as the path created for vdev here
+ * migh not be correct.
+ *
+ * \return The device path
+ */
+std::string V4L2Device::devicePath() const
+{
+	std::string vdev = basename(deviceNode_.c_str());
+	std::string sysfs = "/sys/class/video4linux/" + vdev + "/device";
+
+	char path[PATH_MAX];
+	if (!realpath(sysfs.c_str(), path))
+		LOG(V4L2, Fatal) << "Can not resolve path for " << deviceNode_;
+
+	return path;
+}
+
 /**
  * \brief Perform an IOCTL system call on the device node
  * \param[in] request The IOCTL request code