[libcamera-devel,v7,3/9] libcamera: v4l2_device: Add method to lookup device path

Message ID 20200804161358.1628962-4-niklas.soderlund@ragnatech.se
State Superseded
Headers show
Series
  • libcamera: Generate unique and stable camera IDs
Related show

Commit Message

Niklas Söderlund Aug. 4, 2020, 4:13 p.m. UTC
Add a method to lookup a V4L2 devices path in sysfs.

Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
---
* Changes since v6
- Use sysfs:: helper.
- Do not try to return std::string { nullptr } on error.

* Changes since v5
- Expand documentation
- Use DeviceEnumerator interface to lookup device path
- Include stdlib.h
- Use realpath(..., nullptr); ...; free();

* Changes since v3
- s/the device path/the device path in sysfs/
---
 include/libcamera/internal/v4l2_device.h |  1 +
 src/libcamera/v4l2_device.cpp            | 28 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

Comments

Laurent Pinchart Aug. 4, 2020, 6:19 p.m. UTC | #1
Hi Niklas,

Thank you for the patch.

On Tue, Aug 04, 2020 at 06:13:52PM +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>
> ---
> * Changes since v6
> - Use sysfs:: helper.
> - Do not try to return std::string { nullptr } on error.
> 
> * Changes since v5
> - Expand documentation
> - Use DeviceEnumerator interface to lookup device path
> - Include stdlib.h
> - Use realpath(..., nullptr); ...; free();
> 
> * Changes since v3
> - s/the device path/the device path in sysfs/
> ---
>  include/libcamera/internal/v4l2_device.h |  1 +
>  src/libcamera/v4l2_device.cpp            | 28 ++++++++++++++++++++++++
>  2 files changed, 29 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..e55deabed46e30d4 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -9,12 +9,15 @@
>  
>  #include <fcntl.h>
>  #include <iomanip>
> +#include <limits.h>
> +#include <stdlib.h>
>  #include <string.h>
>  #include <sys/ioctl.h>
>  #include <sys/syscall.h>
>  #include <unistd.h>
>  
>  #include "libcamera/internal/log.h"
> +#include "libcamera/internal/sysfs.h"
>  #include "libcamera/internal/utils.h"
>  #include "libcamera/internal/v4l2_controls.h"
>  
> @@ -350,6 +353,31 @@ int V4L2Device::setControls(ControlList *ctrls)
>  	return ret;
>  }
>  
> +/**
> + * \brief Retrieve the device path in sysfs
> + *
> + * This function returns the sysfs path to the physical device backing the V4L2
> + * device. The path is guaranteed to be an absolute path, without any symbolic
> + * link.

"It includes the sysfs mount point prefix."

> + *
> + * \return The device path in sysfs
> + */
> +std::string V4L2Device::devicePath() const
> +{
> +	std::string devicePath = sysfs::charDevPath(deviceNode_) + "/device";
> +
> +	char *realPath = realpath(devicePath.c_str(), nullptr);
> +	if (!realPath) {
> +		LOG(V4L2, Fatal) << "Can not resolve path for " << devicePath;

s/path/device path/ ?

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +		return {};
> +	}
> +
> +	std::string path{ realPath };
> +	free(realPath);
> +
> +	return path;
> +}
> +
>  /**
>   * \brief Perform an IOCTL system call on the device node
>   * \param[in] request The IOCTL request code
Jacopo Mondi Aug. 5, 2020, 7:25 a.m. UTC | #2
Hi Niklas,

On Tue, Aug 04, 2020 at 06:13:52PM +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>
> ---
> * Changes since v6
> - Use sysfs:: helper.
> - Do not try to return std::string { nullptr } on error.
>
> * Changes since v5
> - Expand documentation
> - Use DeviceEnumerator interface to lookup device path
> - Include stdlib.h
> - Use realpath(..., nullptr); ...; free();
>
> * Changes since v3
> - s/the device path/the device path in sysfs/
> ---
>  include/libcamera/internal/v4l2_device.h |  1 +
>  src/libcamera/v4l2_device.cpp            | 28 ++++++++++++++++++++++++
>  2 files changed, 29 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..e55deabed46e30d4 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -9,12 +9,15 @@
>
>  #include <fcntl.h>
>  #include <iomanip>
> +#include <limits.h>

Is limits.h a leftover from when you used MAX_PATH ?

> +#include <stdlib.h>
>  #include <string.h>
>  #include <sys/ioctl.h>
>  #include <sys/syscall.h>
>  #include <unistd.h>
>
>  #include "libcamera/internal/log.h"
> +#include "libcamera/internal/sysfs.h"
>  #include "libcamera/internal/utils.h"
>  #include "libcamera/internal/v4l2_controls.h"
>
> @@ -350,6 +353,31 @@ int V4L2Device::setControls(ControlList *ctrls)
>  	return ret;
>  }
>
> +/**
> + * \brief Retrieve the device path in sysfs
> + *
> + * This function returns the sysfs path to the physical device backing the V4L2
> + * device. The path is guaranteed to be an absolute path, without any symbolic
> + * link.
> + *
> + * \return The device path in sysfs
> + */
> +std::string V4L2Device::devicePath() const
> +{
> +	std::string devicePath = sysfs::charDevPath(deviceNode_) + "/device";
> +
> +	char *realPath = realpath(devicePath.c_str(), nullptr);
> +	if (!realPath) {
> +		LOG(V4L2, Fatal) << "Can not resolve path for " << devicePath;
> +		return {};
> +	}
> +
> +	std::string path{ realPath };
> +	free(realPath);
> +
> +	return path;
> +}
> +

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.28.0
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Niklas Söderlund Aug. 5, 2020, 9:08 a.m. UTC | #3
Hi Jacopo,

Thanks for your feedback.

On 2020-08-05 09:25:40 +0200, Jacopo Mondi wrote:
> Hi Niklas,
> 
> On Tue, Aug 04, 2020 at 06:13:52PM +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>
> > ---
> > * Changes since v6
> > - Use sysfs:: helper.
> > - Do not try to return std::string { nullptr } on error.
> >
> > * Changes since v5
> > - Expand documentation
> > - Use DeviceEnumerator interface to lookup device path
> > - Include stdlib.h
> > - Use realpath(..., nullptr); ...; free();
> >
> > * Changes since v3
> > - s/the device path/the device path in sysfs/
> > ---
> >  include/libcamera/internal/v4l2_device.h |  1 +
> >  src/libcamera/v4l2_device.cpp            | 28 ++++++++++++++++++++++++
> >  2 files changed, 29 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..e55deabed46e30d4 100644
> > --- a/src/libcamera/v4l2_device.cpp
> > +++ b/src/libcamera/v4l2_device.cpp
> > @@ -9,12 +9,15 @@
> >
> >  #include <fcntl.h>
> >  #include <iomanip>
> > +#include <limits.h>
> 
> Is limits.h a leftover from when you used MAX_PATH ?

No, it's required for realpath();

> 
> > +#include <stdlib.h>
> >  #include <string.h>
> >  #include <sys/ioctl.h>
> >  #include <sys/syscall.h>
> >  #include <unistd.h>
> >
> >  #include "libcamera/internal/log.h"
> > +#include "libcamera/internal/sysfs.h"
> >  #include "libcamera/internal/utils.h"
> >  #include "libcamera/internal/v4l2_controls.h"
> >
> > @@ -350,6 +353,31 @@ int V4L2Device::setControls(ControlList *ctrls)
> >  	return ret;
> >  }
> >
> > +/**
> > + * \brief Retrieve the device path in sysfs
> > + *
> > + * This function returns the sysfs path to the physical device backing the V4L2
> > + * device. The path is guaranteed to be an absolute path, without any symbolic
> > + * link.
> > + *
> > + * \return The device path in sysfs
> > + */
> > +std::string V4L2Device::devicePath() const
> > +{
> > +	std::string devicePath = sysfs::charDevPath(deviceNode_) + "/device";
> > +
> > +	char *realPath = realpath(devicePath.c_str(), nullptr);
> > +	if (!realPath) {
> > +		LOG(V4L2, Fatal) << "Can not resolve path for " << devicePath;
> > +		return {};
> > +	}
> > +
> > +	std::string path{ realPath };
> > +	free(realPath);
> > +
> > +	return path;
> > +}
> > +
> 
> 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.28.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..e55deabed46e30d4 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -9,12 +9,15 @@ 
 
 #include <fcntl.h>
 #include <iomanip>
+#include <limits.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/syscall.h>
 #include <unistd.h>
 
 #include "libcamera/internal/log.h"
+#include "libcamera/internal/sysfs.h"
 #include "libcamera/internal/utils.h"
 #include "libcamera/internal/v4l2_controls.h"
 
@@ -350,6 +353,31 @@  int V4L2Device::setControls(ControlList *ctrls)
 	return ret;
 }
 
+/**
+ * \brief Retrieve the device path in sysfs
+ *
+ * This function returns the sysfs path to the physical device backing the V4L2
+ * device. The path is guaranteed to be an absolute path, without any symbolic
+ * link.
+ *
+ * \return The device path in sysfs
+ */
+std::string V4L2Device::devicePath() const
+{
+	std::string devicePath = sysfs::charDevPath(deviceNode_) + "/device";
+
+	char *realPath = realpath(devicePath.c_str(), nullptr);
+	if (!realPath) {
+		LOG(V4L2, Fatal) << "Can not resolve path for " << devicePath;
+		return {};
+	}
+
+	std::string path{ realPath };
+	free(realPath);
+
+	return path;
+}
+
 /**
  * \brief Perform an IOCTL system call on the device node
  * \param[in] request The IOCTL request code