[libcamera-devel,v3,09/13] libcamera: v4l2_device: Add method to read a control

Message ID 20200424215304.558317-10-jacopo@jmondi.org
State Superseded
Headers show
Series
  • libcamera: Add CameraSensorInfo
Related show

Commit Message

Jacopo Mondi April 24, 2020, 9:53 p.m. UTC
Add a method to the V4L2Device class to read a single control from a
device.

The current procedure to read a V4L2 Control from a device requires the
user to setup a list and populate it using ControlList::set(), with a
value that is ignored if the only interest is reading the control out.

Provide a method to read a single control by just providing its id.
The newly added method only supports non-span e non-string control but
could be easily expanded later.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/include/v4l2_device.h | 18 ++++++++++++++++++
 src/libcamera/v4l2_device.cpp       | 11 +++++++++++
 2 files changed, 29 insertions(+)

Patch

diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h
index ce8edd98a01d..23cff08bbfd0 100644
--- a/src/libcamera/include/v4l2_device.h
+++ b/src/libcamera/include/v4l2_device.h
@@ -11,6 +11,7 @@ 
 #include <memory>
 #include <vector>
 
+#include <libcamera/controls.h>
 #include <linux/videodev2.h>
 
 #include "log.h"
@@ -27,6 +28,23 @@  public:
 	const ControlInfoMap &controls() const { return controls_; }
 
 	int getControls(ControlList *ctrls);
+	template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
+						       !std::is_same<std::string, std::remove_cv_t<T>>::value,
+						       std::nullptr_t> = nullptr>
+	int getControl(unsigned int id, T *val)
+	{
+		ControlList list(controls_);
+		/* Reuse 'val' as we're only interested in reading. */
+		list.set(id, *val);
+
+		int ret = getControls(&list);
+		if (ret)
+			return ret;
+		*val = list.get(id).get<T>();
+
+		return 0;
+	}
+
 	int setControls(ControlList *ctrls);
 
 	const std::string &deviceNode() const { return deviceNode_; }
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index 03e305165096..9d67e0f63b1e 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -242,6 +242,17 @@  int V4L2Device::getControls(ControlList *ctrls)
 	return ret;
 }
 
+/**
+ * \fn int V4L2Device::getControl(unsigned int id, T *val) const
+ * \brief Get the value of the V4L2 control \a id from the device
+ * \param[in] id The V4L2 control ID
+ * \param[out] val The control value
+ *
+ * \todo This only supports non-span and non-string controls.
+ *
+ * \return 0 on success, a negative error code otherwise
+ */
+
 /**
  * \brief Write controls to the device
  * \param[in] ctrls The list of controls to write