[libcamera-devel] [PATCH v6 3/6] libcamera: v4l2_device: Implement get and set controls
Jacopo Mondi
jacopo at jmondi.org
Mon Jun 24 18:40:42 CEST 2019
Hi Laurent,
On Mon, Jun 24, 2019 at 07:16:48PM +0300, Laurent Pinchart wrote:
> Hi Jacopo,
>
> Thank you for the patch.
>
> On Mon, Jun 24, 2019 at 04:28:56PM +0200, Jacopo Mondi wrote:
> > 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 at jmondi.org>
> > ---
> > src/libcamera/include/v4l2_device.h | 9 ++
> > src/libcamera/v4l2_device.cpp | 186 ++++++++++++++++++++++++++++
> > 2 files changed, 195 insertions(+)
> >
> > diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h
> > index 556960467e10..39a47f55acb8 100644
> > --- a/src/libcamera/include/v4l2_device.h
> > +++ b/src/libcamera/include/v4l2_device.h
> > @@ -10,11 +10,14 @@
> > #include <map>
> > #include <string>
> >
> > +#include <linux/videodev2.h>
> > +
> > #include "log.h"
> >
> > namespace libcamera {
> >
> > class V4L2ControlInfo;
> > +class V4L2ControlList;
> >
> > class V4L2Device : protected Loggable
> > {
> > @@ -23,6 +26,8 @@ public:
> > bool isOpen() const { return fd_ != -1; }
> >
> > const V4L2ControlInfo *getControlInfo(unsigned int id) const;
> > + int getControls(V4L2ControlList *ctrls);
> > + int setControls(V4L2ControlList *ctrls);
> >
> > const std::string &deviceNode() const { return deviceNode_; }
> >
> > @@ -38,6 +43,10 @@ protected:
> >
> > private:
> > void listControls();
> > + void updateControls(V4L2ControlList *ctrls,
> > + const V4L2ControlInfo **controlInfo,
> > + struct v4l2_ext_control *v4l2Ctrls,
> > + unsigned int count);
> >
> > std::map<unsigned int, V4L2ControlInfo> controls_;
> > std::string deviceNode_;
> > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> > index 51764b441f92..6319d1cfaaa1 100644
> > --- a/src/libcamera/v4l2_device.cpp
> > +++ b/src/libcamera/v4l2_device.cpp
> > @@ -125,6 +125,156 @@ const V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id) const
> > return &it->second;
> > }
> >
> > +/**
> > + * \brief Read controls from the device
> > + * \param[inout] ctrls The list of controls to read
> > + *
> > + * This method reads the value of all controls contained in \a ctrls, and stores
> > + * their values in the corresponding \a ctrls entry.
> > + *
> > + * If any control in \a ctrls is not supported by the device, is disabled (i.e.
> > + * has the V4L2_CTRL_FLAG_DISABLED flag set), is a compound control, or if any
> > + * other error occurs during validation of the requested controls, no control is
> > + * read and this method returns -EINVAL.
> > + *
> > + * If an error occurs while reading the controls, the index of the first control
> > + * that couldn't be read is returned. The value of all controls below that index
> > + * are updated in \a ctrls, while the value of all the other controls are not
> > + * changed.
>
> There was no need to drop the \return block :-)
>
> > + */
> > +int V4L2Device::getControls(V4L2ControlList *ctrls)
> > +{
> > + unsigned int count = ctrls->size();
> > + if (count == 0)
> > + return 0;
> > +
> > + const V4L2ControlInfo *controlInfo[count] = {};
> > + struct v4l2_ext_control v4l2Ctrls[count] = {};
> > + for (V4L2Control &ctrl : *ctrls) {
>
> const V4L2Control &ctrl
>
> > + const V4L2ControlInfo *info = getControlInfo(ctrl.id());
> > + if (!info) {
> > + LOG(V4L2, Error)
> > + << "Control '" << ctrl.id() << "' not found";
> > + return -EINVAL;
> > + }
> > +
> > + controlInfo[i] = info;
> > + v4l2Ctrls[i].id = info->id();
> > + }
> > +
> > + 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) {
> > + unsigned int errorIdx = v4l2ExtCtrls.error_idx;
> > +
> > + /* Generic validation error. */
> > + if (errorIdx == 0 || errorIdx >= count) {
> > + LOG(V4L2, Error) << "Unable to read controls: "
> > + << strerror(ret);
> > + return -EINVAL;
> > + }
> > +
> > + /* A specific control failed. */
> > + LOG(V4L2, Error) << "Unable to read control " << errorIdx
> > + << ": " << strerror(ret);
> > + count = errorIdx - 1;
> > + ret = errorIdx;
> > + }
> > +
> > + updateControls(ctrls, controlInfo, v4l2Ctrls, count);
> > +
> > + return ret;
> > +}
> > +
> > +/**
> > + * \brief Write controls to the device
> > + * \param[in] ctrls The list of controls to write
> > + *
> > + * This method writes the value of all controls contained in \a ctrls, and
> > + * stores the values actually applied to the device in the corresponding
> > + * \a ctrls entry.
> > + *
> > + * If any control in \a ctrls is not supported by the device, is disabled (i.e.
> > + * has the V4L2_CTRL_FLAG_DISABLED flag set), is read-only, is a
> > + * compound control, or if any other error occurs during validation of
> > + * the requested controls, no control is written and this method returns
> > + * -EINVAL.
> > + *
> > + * If an error occurs while writing the controls, the index of the first
> > + * control that couldn't be written is returned. All controls below that index
> > + * are written and their values are updated in \a ctrls, while all other
> > + * controls are not written and their values are not changed.
> > + *
> > + * \return 0 on success or an error code otherwise
> > + * \retval -EINVAL One of the control is not supported or not accessible
> > + * \retval i The index of the control that failed
> > + */
> > +int V4L2Device::setControls(V4L2ControlList *ctrls)
> > +{
> > + unsigned int count = ctrls->size();
> > + if (count == 0)
> > + return 0;
> > +
> > + const V4L2ControlInfo *controlInfo[count] = {};
> > + struct v4l2_ext_control v4l2Ctrls[count] = {};
> > + for (V4L2Control &ctrl : *ctrls) {
>
> const V4L2Control &ctrl
>
> > + const V4L2ControlInfo *info = getControlInfo(ctrl.id());
> > + if (!info) {
> > + LOG(V4L2, Error)
> > + << "Control '" << ctrl.id() << "' not found";
> > + return -EINVAL;
> > + }
> > +
> > + controlInfo[i] = info;
> > + v4l2Ctrls[i].id = info->id();
> > +
> > + /* Set the v4l2_ext_control value for the write operation. */
> > + switch (info->type()) {
> > + case V4L2_CTRL_TYPE_INTEGER64:
> > + v4l2Ctrls[i].value64 = ctrl.value();
> > + break;
> > + default:
> > + /*
> > + * \todo To be changed when support for string and
> > + * compound controls will be added.
> > + */
> > + v4l2Ctrls[i].value = 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) {
> > + unsigned int errorIdx = v4l2ExtCtrls.error_idx;
> > +
> > + /* Generic validation error. */
> > + if (errorIdx == 0 || errorIdx >= count) {
> > + LOG(V4L2, Error) << "Unable to read controls: "
> > + << strerror(ret);
> > + return -EINVAL;
> > + }
> > +
> > + /* A specific control failed. */
> > + LOG(V4L2, Error) << "Unable to read control " << errorIdx
> > + << ": " << strerror(ret);
> > + count = errorIdx - 1;
> > + ret = errorIdx;
> > + }
> > +
> > + updateControls(ctrls, controlInfo, v4l2Ctrls, count);
> > +
> > + return ret;
> > +}
> > +
> > /**
> > * \brief Perform an IOCTL system call on the device node
> > * \param[in] request The IOCTL request code
> > @@ -194,4 +344,40 @@ void V4L2Device::listControls()
> > }
> > }
> >
> > +/*
> > + * \brief Update the value of \a count V4L2 controls in \a ctrls using values
>
> s/value of/value of the first/
>
> > + * in \a v4l2Ctrls
> > + * \param[inout] ctrls List of V4L2 controls to update
> > + * \param[in] controlInfo List of V4L2 control information
> > + * \param[in] v4l2Ctrls List of V4L2 extended controls as returned by the driver
> > + * \param[in] count The number of controls to update
> > + */
> > +void V4L2Device::updateControls(V4L2ControlList *ctrls,
> > + const V4L2ControlInfo **controlInfo,
> > + struct v4l2_ext_control *v4l2Ctrls,
>
> This can be const.
>
> > + unsigned int count)
> > +{
> > + unsigned int i = 0;
> > + for (V4L2Control &ctrl : *ctrls) {
> > + struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
> > + const V4L2ControlInfo *info = controlInfo[i];
> > +
> > + switch (info->type()) {
> > + case V4L2_CTRL_TYPE_INTEGER64:
> > + ctrl.setValue(v4l2Ctrl->value64);
> > + break;
> > + default:
> > + /*
> > + * \todo To be changed when support for string and
> > + * compound controls will be added.
> > + */
> > + ctrl.setValue(v4l2Ctrl->value);
> > + break;
> > + }
> > +
> > + if (++i == count)
> > + break;
>
> I would move the test at the beginning of the loop, so that in the
> unlikely case where count == 0 the function will not loop forever. You
> could write
This doesn't get called if count == 0
>
> struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
> const V4L2ControlInfo *info = controlInfo[i];
>
> if (i++ == count)
> break;
>
... for the moment at least.
Better safe than sorry, I'll move it up
> ...
>
> With these small issues addressed,
>
> Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
>
Thanks
j
> > + }
> > +}
> > +
> > } /* namespace libcamera */
>
> --
> Regards,
>
> Laurent Pinchart
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.libcamera.org/pipermail/libcamera-devel/attachments/20190624/cae08c62/attachment-0001.sig>
More information about the libcamera-devel
mailing list