@@ -13,12 +13,17 @@
namespace libcamera {
class V4L2ControlInfo;
+class V4L2Controls;
class V4L2Device
{
public:
virtual ~V4L2Device();
+ V4L2ControlInfo *getControlInfo(unsigned int id);
+ int getControls(V4L2Controls *ctrls);
+ int setControls(V4L2Controls *ctrls);
+
protected:
V4L2Device();
@@ -32,6 +37,7 @@ protected:
private:
int listControls();
+ int validateControlType(V4L2ControlInfo *info);
int fd_;
std::map<unsigned int, V4L2ControlInfo *> controls_;
@@ -159,4 +159,192 @@ int V4L2Device::ioctl(unsigned long request, void *argp)
return 0;
}
+/**
+ * \brief Get informations for control with \a id
+ * \param id The control id to search info for
+ * \return The V4L2ControlInfo associated to the V4L2 control with \a id or
+ * nullptr if the control is not supported by the V4L2Device
+ */
+V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id)
+{
+ auto it = controls_.find(id);
+ if (it == controls_.end())
+ return nullptr;
+
+ return it->second;
+}
+
+/**
+ * \brief Read values of a list of controls from the device
+ * \param ctrls The list of controls to read
+ *
+ * Read the value of each V4L2Controls contained in \a ctrls, overwriting
+ * it's current value with the one returned by the device.
+ *
+ * Each V4L2Control instance in \a ctrls should be supported by the device.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2Device::getControls(V4L2Controls *ctrls)
+{
+ unsigned int count = ctrls->size();
+ if (count == 0)
+ return 0;
+
+ struct V4L2ControlInfo *controlInfo[count];
+ struct v4l2_ext_control v4l2Ctrls[count];
+ for (unsigned int i = 0; i < count; ++i) {
+ V4L2Control *ctrl = (*ctrls)[i];
+
+ /* Validate the control. */
+ V4L2ControlInfo *info = getControlInfo(ctrl->id());
+ if (!info) {
+ LOG(V4L2, Error) << "Control '" << ctrl->id()
+ << "' not valid";
+ return -EINVAL;
+ }
+
+ if (validateControlType(info))
+ return -EINVAL;
+
+ controlInfo[i] = info;
+
+ /* Prepare the v4l2_ext_control entry for the read operation. */
+ struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
+ v4l2Ctrl->id = info->id();
+ v4l2Ctrl->size = info->size();
+ }
+
+ struct v4l2_ext_controls v4l2ExtCtrls = {};
+ v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;
+ v4l2ExtCtrls.controls = v4l2Ctrls;
+ v4l2ExtCtrls.count = count;
+
+ int ret = ioctl(VIDIOC_G_EXT_CTRLS, &v4l2ExtCtrls);
+ if (ret) {
+ LOG(V4L2, Error) << "Unable to read controls: "
+ << strerror(ret);
+ return ret;
+ }
+
+ /*
+ * For each control read from the device, set the value in the
+ * V4L2ControlValue provided by the caller.
+ */
+ for (unsigned int i = 0; i < count; ++i) {
+ struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
+ V4L2ControlInfo *info = controlInfo[i];
+ V4L2Control *ctrl = (*ctrls)[i];
+
+ switch (info->type()) {
+ case V4L2_CTRL_TYPE_INTEGER64:
+ ctrl->setValue(v4l2Ctrl->value64);
+ break;
+ case V4L2_CTRL_TYPE_INTEGER:
+ case V4L2_CTRL_TYPE_BOOLEAN:
+ case V4L2_CTRL_TYPE_BUTTON:
+ case V4L2_CTRL_TYPE_MENU:
+ ctrl->setValue(static_cast<long int>(v4l2Ctrl->value));
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * \brief Write a list of controls to the device
+ * \param ctrls The list of controls to apply
+ *
+ * Write the value of each V4L2Control contained in \a ctrls. Each
+ * control should be initialized by the caller with a value, otherwise
+ * the result of the operation is undefined.
+ *
+ * The value of each control is not updated to reflect what has been
+ * actually applied on the device. To read the actual value of a control
+ * after a setControls(), users should read the control value back with
+ * getControls().
+ *
+ * Each V4L2Control instance in \a ctrls should be supported by the device.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2Device::setControls(V4L2Controls *ctrls)
+{
+ unsigned int count = ctrls->size();
+ if (count == 0)
+ return 0;
+
+ struct v4l2_ext_control v4l2Ctrls[count];
+ for (unsigned int i = 0; i < count; ++i) {
+ V4L2Control *ctrl = (*ctrls)[i];
+
+ /* Validate the control. */
+ V4L2ControlInfo *info = getControlInfo(ctrl->id());
+ if (!info) {
+ LOG(V4L2, Error) << "Control '" << ctrl->id()
+ << "' not valid";
+ return -EINVAL;
+ }
+
+ if (validateControlType(info))
+ return -EINVAL;
+
+ /* Prepare the v4l2_ext_control entry for the write operation. */
+ struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
+ v4l2Ctrl->id = info->id();
+ v4l2Ctrl->size = info->size();
+
+ switch (info->type()) {
+ case V4L2_CTRL_TYPE_INTEGER64:
+ v4l2Ctrl->value64 = ctrl->value();
+ break;
+ case V4L2_CTRL_TYPE_INTEGER:
+ case V4L2_CTRL_TYPE_BOOLEAN:
+ case V4L2_CTRL_TYPE_BUTTON:
+ case V4L2_CTRL_TYPE_MENU:
+ v4l2Ctrl->value = static_cast<int32_t>(ctrl->value());
+ break;
+ }
+ }
+
+ struct v4l2_ext_controls v4l2ExtCtrls = {};
+ v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;
+ v4l2ExtCtrls.controls = v4l2Ctrls;
+ v4l2ExtCtrls.count = count;
+
+ int ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);
+ if (ret) {
+ LOG(V4L2, Error) << "Unable to write controls: "
+ << strerror(ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+/*
+ * \brief Make sure the control type is supported
+ * \param info The V4L2ControlInfo to inspect type of
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2Device::validateControlType(V4L2ControlInfo *info)
+{
+ /* \todo support compound and menu controls. */
+ switch (info->type()) {
+ case V4L2_CTRL_TYPE_INTEGER64:
+ case V4L2_CTRL_TYPE_INTEGER:
+ case V4L2_CTRL_TYPE_BOOLEAN:
+ case V4L2_CTRL_TYPE_BUTTON:
+ case V4L2_CTRL_TYPE_MENU:
+ break;
+ default:
+ LOG(V4L2, Error) << "Control type '" << info->type()
+ << "' not supported";
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
} /* namespace libcamera */
Implement getControls() and setControls() operations in V4L2Device class. Both operations take a V4L2Controls instance and read or write the V4L2 controls on the V4L2 device. Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> --- src/libcamera/include/v4l2_device.h | 6 + src/libcamera/v4l2_device.cpp | 188 ++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+)