[{"id":2010,"web_url":"https://patchwork.libcamera.org/comment/2010/","msgid":"<20190624161648.GO5737@pendragon.ideasonboard.com>","date":"2019-06-24T16:16:48","subject":"Re: [libcamera-devel] [PATCH v6 3/6] libcamera: v4l2_device:\n\tImplement get and set controls","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Mon, Jun 24, 2019 at 04:28:56PM +0200, Jacopo Mondi wrote:\n> Implement getControls() and setControls() operations in V4L2Device class.\n> \n> Both operations take a V4L2Controls instance and read or write the V4L2\n> controls on the V4L2 device.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  src/libcamera/include/v4l2_device.h |   9 ++\n>  src/libcamera/v4l2_device.cpp       | 186 ++++++++++++++++++++++++++++\n>  2 files changed, 195 insertions(+)\n> \n> diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\n> index 556960467e10..39a47f55acb8 100644\n> --- a/src/libcamera/include/v4l2_device.h\n> +++ b/src/libcamera/include/v4l2_device.h\n> @@ -10,11 +10,14 @@\n>  #include <map>\n>  #include <string>\n>  \n> +#include <linux/videodev2.h>\n> +\n>  #include \"log.h\"\n>  \n>  namespace libcamera {\n>  \n>  class V4L2ControlInfo;\n> +class V4L2ControlList;\n>  \n>  class V4L2Device : protected Loggable\n>  {\n> @@ -23,6 +26,8 @@ public:\n>  \tbool isOpen() const { return fd_ != -1; }\n>  \n>  \tconst V4L2ControlInfo *getControlInfo(unsigned int id) const;\n> +\tint getControls(V4L2ControlList *ctrls);\n> +\tint setControls(V4L2ControlList *ctrls);\n>  \n>  \tconst std::string &deviceNode() const { return deviceNode_; }\n>  \n> @@ -38,6 +43,10 @@ protected:\n>  \n>  private:\n>  \tvoid listControls();\n> +\tvoid updateControls(V4L2ControlList *ctrls,\n> +\t\t\t    const V4L2ControlInfo **controlInfo,\n> +\t\t\t    struct v4l2_ext_control *v4l2Ctrls,\n> +\t\t\t    unsigned int count);\n>  \n>  \tstd::map<unsigned int, V4L2ControlInfo> controls_;\n>  \tstd::string deviceNode_;\n> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> index 51764b441f92..6319d1cfaaa1 100644\n> --- a/src/libcamera/v4l2_device.cpp\n> +++ b/src/libcamera/v4l2_device.cpp\n> @@ -125,6 +125,156 @@ const V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id) const\n>  \treturn &it->second;\n>  }\n>  \n> +/**\n> + * \\brief Read controls from the device\n> + * \\param[inout] ctrls The list of controls to read\n> + *\n> + * This method reads the value of all controls contained in \\a ctrls, and stores\n> + * their values in the corresponding \\a ctrls entry.\n> + *\n> + * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n> + * has the V4L2_CTRL_FLAG_DISABLED flag set), is a compound control, or if any\n> + * other error occurs during validation of the requested controls, no control is\n> + * read and this method returns -EINVAL.\n> + *\n> + * If an error occurs while reading the controls, the index of the first control\n> + * that couldn't be read is returned. The value of all controls below that index\n> + * are updated in \\a ctrls, while the value of all the other controls are not\n> + * changed.\n\nThere was no need to drop the \\return block :-)\n\n> + */\n> +int V4L2Device::getControls(V4L2ControlList *ctrls)\n> +{\n> +\tunsigned int count = ctrls->size();\n> +\tif (count == 0)\n> +\t\treturn 0;\n> +\n> +\tconst V4L2ControlInfo *controlInfo[count] = {};\n> +\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n> +\tfor (V4L2Control &ctrl : *ctrls) {\n\nconst V4L2Control &ctrl\n\n> +\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl.id());\n> +\t\tif (!info) {\n> +\t\t\tLOG(V4L2, Error)\n> +\t\t\t\t<< \"Control '\" << ctrl.id() << \"' not found\";\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\tcontrolInfo[i] = info;\n> +\t\tv4l2Ctrls[i].id = info->id();\n> +\t}\n> +\n> +\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n> +\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> +\tv4l2ExtCtrls.controls = v4l2Ctrls;\n> +\tv4l2ExtCtrls.count = count;\n> +\n> +\tint ret = ioctl(VIDIOC_G_EXT_CTRLS, &v4l2ExtCtrls);\n> +\tif (ret) {\n> +\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> +\n> +\t\t/* Generic validation error. */\n> +\t\tif (errorIdx == 0 || errorIdx >= count) {\n> +\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n> +\t\t\t\t\t << strerror(ret);\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\t/* A specific control failed. */\n> +\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n> +\t\t\t\t << \": \" << strerror(ret);\n> +\t\tcount = errorIdx - 1;\n> +\t\tret = errorIdx;\n> +\t}\n> +\n> +\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n> +\n> +\treturn ret;\n> +}\n> +\n> +/**\n> + * \\brief Write controls to the device\n> + * \\param[in] ctrls The list of controls to write\n> + *\n> + * This method writes the value of all controls contained in \\a ctrls, and\n> + * stores the values actually applied to the device in the corresponding\n> + * \\a ctrls entry.\n> + *\n> + * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n> + * has the V4L2_CTRL_FLAG_DISABLED flag set), is read-only, is a\n> + * compound control, or if any other error occurs during validation of\n> + * the requested controls, no control is written and this method returns\n> + * -EINVAL.\n> + *\n> + * If an error occurs while writing the controls, the index of the first\n> + * control that couldn't be written is returned. All controls below that index\n> + * are written and their values are updated in \\a ctrls, while all other\n> + * controls are not written and their values are not changed.\n> + *\n> + * \\return 0 on success or an error code otherwise\n> + * \\retval -EINVAL One of the control is not supported or not accessible\n> + * \\retval i The index of the control that failed\n> + */\n> +int V4L2Device::setControls(V4L2ControlList *ctrls)\n> +{\n> +\tunsigned int count = ctrls->size();\n> +\tif (count == 0)\n> +\t\treturn 0;\n> +\n> +\tconst V4L2ControlInfo *controlInfo[count] = {};\n> +\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n> +\tfor (V4L2Control &ctrl : *ctrls) {\n\nconst V4L2Control &ctrl\n\n> +\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl.id());\n> +\t\tif (!info) {\n> +\t\t\tLOG(V4L2, Error)\n> +\t\t\t\t<< \"Control '\" << ctrl.id() << \"' not found\";\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\tcontrolInfo[i] = info;\n> +\t\tv4l2Ctrls[i].id = info->id();\n> +\n> +\t\t/* Set the v4l2_ext_control value for the write operation. */\n> +\t\tswitch (info->type()) {\n> +\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n> +\t\t\tv4l2Ctrls[i].value64 = ctrl.value();\n> +\t\t\tbreak;\n> +\t\tdefault:\n> +\t\t\t/*\n> +\t\t\t * \\todo To be changed when support for string and\n> +\t\t\t * compound controls will be added.\n> +\t\t\t */\n> +\t\t\tv4l2Ctrls[i].value = ctrl.value();\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n> +\n> +\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n> +\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> +\tv4l2ExtCtrls.controls = v4l2Ctrls;\n> +\tv4l2ExtCtrls.count = count;\n> +\n> +\tint ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n> +\tif (ret) {\n> +\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> +\n> +\t\t/* Generic validation error. */\n> +\t\tif (errorIdx == 0 || errorIdx >= count) {\n> +\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n> +\t\t\t\t\t << strerror(ret);\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\t/* A specific control failed. */\n> +\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n> +\t\t\t\t << \": \" << strerror(ret);\n> +\t\tcount = errorIdx - 1;\n> +\t\tret = errorIdx;\n> +\t}\n> +\n> +\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n> +\n> +\treturn ret;\n> +}\n> +\n>  /**\n>   * \\brief Perform an IOCTL system call on the device node\n>   * \\param[in] request The IOCTL request code\n> @@ -194,4 +344,40 @@ void V4L2Device::listControls()\n>  \t}\n>  }\n>  \n> +/*\n> + * \\brief Update the value of \\a count V4L2 controls in \\a ctrls using values\n\ns/value of/value of the first/\n\n> + * in \\a v4l2Ctrls\n> + * \\param[inout] ctrls List of V4L2 controls to update\n> + * \\param[in] controlInfo List of V4L2 control information\n> + * \\param[in] v4l2Ctrls List of V4L2 extended controls as returned by the driver\n> + * \\param[in] count The number of controls to update\n> + */\n> +void V4L2Device::updateControls(V4L2ControlList *ctrls,\n> +\t\t\t\tconst V4L2ControlInfo **controlInfo,\n> +\t\t\t\tstruct v4l2_ext_control *v4l2Ctrls,\n\nThis can be const.\n\n> +\t\t\t\tunsigned int count)\n> +{\n> +\tunsigned int i = 0;\n> +\tfor (V4L2Control &ctrl : *ctrls) {\n> +\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n> +\t\tconst V4L2ControlInfo *info = controlInfo[i];\n> +\n> +\t\tswitch (info->type()) {\n> +\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n> +\t\t\tctrl.setValue(v4l2Ctrl->value64);\n> +\t\t\tbreak;\n> +\t\tdefault:\n> +\t\t\t/*\n> +\t\t\t * \\todo To be changed when support for string and\n> +\t\t\t * compound controls will be added.\n> +\t\t\t */\n> +\t\t\tctrl.setValue(v4l2Ctrl->value);\n> +\t\t\tbreak;\n> +\t\t}\n> +\n> +\t\tif (++i == count)\n> +\t\t\tbreak;\n\nI would move the test at the beginning of the loop, so that in the\nunlikely case where count == 0 the function will not loop forever. You\ncould write\n\n\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n\t\tconst V4L2ControlInfo *info = controlInfo[i];\n\n\t\tif (i++ == count)\n\t\t\tbreak;\n\n\t\t...\n\nWith these small issues addressed,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t}\n> +}\n> +\n>  } /* namespace libcamera */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A130961580\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Jun 2019 18:19:19 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 092462E7;\n\tMon, 24 Jun 2019 18:19:18 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1561393159;\n\tbh=3hA37DU9YT1sFiDVw0vEIrgqSPSWRJXuen3IsRdW9ik=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=S/5dC/kIz+365cNsNqUmHST7McUrmXrL778k7XxZKsb69rvdhYvsgwcR++VPMOUDU\n\tEzK1wYgq1dnl8l7ZuXj49y01nQw6913MKq/32cuuMObQIApLskxOm0I0CIYglrKHir\n\tXfJDqs28Fic4HDlTjSGtGvkYF0hd/J3f9E1HqcNQ=","Date":"Mon, 24 Jun 2019 19:16:48 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190624161648.GO5737@pendragon.ideasonboard.com>","References":"<20190624142859.19313-1-jacopo@jmondi.org>\n\t<20190624142859.19313-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190624142859.19313-4-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v6 3/6] libcamera: v4l2_device:\n\tImplement get and set controls","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 24 Jun 2019 16:19:19 -0000"}},{"id":2014,"web_url":"https://patchwork.libcamera.org/comment/2014/","msgid":"<20190624164042.kpbagv5im3vwf65z@uno.localdomain>","date":"2019-06-24T16:40:42","subject":"Re: [libcamera-devel] [PATCH v6 3/6] libcamera: v4l2_device:\n\tImplement get and set controls","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent,\n\nOn Mon, Jun 24, 2019 at 07:16:48PM +0300, Laurent Pinchart wrote:\n> Hi Jacopo,\n>\n> Thank you for the patch.\n>\n> On Mon, Jun 24, 2019 at 04:28:56PM +0200, Jacopo Mondi wrote:\n> > Implement getControls() and setControls() operations in V4L2Device class.\n> >\n> > Both operations take a V4L2Controls instance and read or write the V4L2\n> > controls on the V4L2 device.\n> >\n> > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> > ---\n> >  src/libcamera/include/v4l2_device.h |   9 ++\n> >  src/libcamera/v4l2_device.cpp       | 186 ++++++++++++++++++++++++++++\n> >  2 files changed, 195 insertions(+)\n> >\n> > diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\n> > index 556960467e10..39a47f55acb8 100644\n> > --- a/src/libcamera/include/v4l2_device.h\n> > +++ b/src/libcamera/include/v4l2_device.h\n> > @@ -10,11 +10,14 @@\n> >  #include <map>\n> >  #include <string>\n> >\n> > +#include <linux/videodev2.h>\n> > +\n> >  #include \"log.h\"\n> >\n> >  namespace libcamera {\n> >\n> >  class V4L2ControlInfo;\n> > +class V4L2ControlList;\n> >\n> >  class V4L2Device : protected Loggable\n> >  {\n> > @@ -23,6 +26,8 @@ public:\n> >  \tbool isOpen() const { return fd_ != -1; }\n> >\n> >  \tconst V4L2ControlInfo *getControlInfo(unsigned int id) const;\n> > +\tint getControls(V4L2ControlList *ctrls);\n> > +\tint setControls(V4L2ControlList *ctrls);\n> >\n> >  \tconst std::string &deviceNode() const { return deviceNode_; }\n> >\n> > @@ -38,6 +43,10 @@ protected:\n> >\n> >  private:\n> >  \tvoid listControls();\n> > +\tvoid updateControls(V4L2ControlList *ctrls,\n> > +\t\t\t    const V4L2ControlInfo **controlInfo,\n> > +\t\t\t    struct v4l2_ext_control *v4l2Ctrls,\n> > +\t\t\t    unsigned int count);\n> >\n> >  \tstd::map<unsigned int, V4L2ControlInfo> controls_;\n> >  \tstd::string deviceNode_;\n> > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> > index 51764b441f92..6319d1cfaaa1 100644\n> > --- a/src/libcamera/v4l2_device.cpp\n> > +++ b/src/libcamera/v4l2_device.cpp\n> > @@ -125,6 +125,156 @@ const V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id) const\n> >  \treturn &it->second;\n> >  }\n> >\n> > +/**\n> > + * \\brief Read controls from the device\n> > + * \\param[inout] ctrls The list of controls to read\n> > + *\n> > + * This method reads the value of all controls contained in \\a ctrls, and stores\n> > + * their values in the corresponding \\a ctrls entry.\n> > + *\n> > + * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n> > + * has the V4L2_CTRL_FLAG_DISABLED flag set), is a compound control, or if any\n> > + * other error occurs during validation of the requested controls, no control is\n> > + * read and this method returns -EINVAL.\n> > + *\n> > + * If an error occurs while reading the controls, the index of the first control\n> > + * that couldn't be read is returned. The value of all controls below that index\n> > + * are updated in \\a ctrls, while the value of all the other controls are not\n> > + * changed.\n>\n> There was no need to drop the \\return block :-)\n>\n> > + */\n> > +int V4L2Device::getControls(V4L2ControlList *ctrls)\n> > +{\n> > +\tunsigned int count = ctrls->size();\n> > +\tif (count == 0)\n> > +\t\treturn 0;\n> > +\n> > +\tconst V4L2ControlInfo *controlInfo[count] = {};\n> > +\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n> > +\tfor (V4L2Control &ctrl : *ctrls) {\n>\n> const V4L2Control &ctrl\n>\n> > +\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl.id());\n> > +\t\tif (!info) {\n> > +\t\t\tLOG(V4L2, Error)\n> > +\t\t\t\t<< \"Control '\" << ctrl.id() << \"' not found\";\n> > +\t\t\treturn -EINVAL;\n> > +\t\t}\n> > +\n> > +\t\tcontrolInfo[i] = info;\n> > +\t\tv4l2Ctrls[i].id = info->id();\n> > +\t}\n> > +\n> > +\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n> > +\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> > +\tv4l2ExtCtrls.controls = v4l2Ctrls;\n> > +\tv4l2ExtCtrls.count = count;\n> > +\n> > +\tint ret = ioctl(VIDIOC_G_EXT_CTRLS, &v4l2ExtCtrls);\n> > +\tif (ret) {\n> > +\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> > +\n> > +\t\t/* Generic validation error. */\n> > +\t\tif (errorIdx == 0 || errorIdx >= count) {\n> > +\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n> > +\t\t\t\t\t << strerror(ret);\n> > +\t\t\treturn -EINVAL;\n> > +\t\t}\n> > +\n> > +\t\t/* A specific control failed. */\n> > +\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n> > +\t\t\t\t << \": \" << strerror(ret);\n> > +\t\tcount = errorIdx - 1;\n> > +\t\tret = errorIdx;\n> > +\t}\n> > +\n> > +\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n> > +\n> > +\treturn ret;\n> > +}\n> > +\n> > +/**\n> > + * \\brief Write controls to the device\n> > + * \\param[in] ctrls The list of controls to write\n> > + *\n> > + * This method writes the value of all controls contained in \\a ctrls, and\n> > + * stores the values actually applied to the device in the corresponding\n> > + * \\a ctrls entry.\n> > + *\n> > + * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n> > + * has the V4L2_CTRL_FLAG_DISABLED flag set), is read-only, is a\n> > + * compound control, or if any other error occurs during validation of\n> > + * the requested controls, no control is written and this method returns\n> > + * -EINVAL.\n> > + *\n> > + * If an error occurs while writing the controls, the index of the first\n> > + * control that couldn't be written is returned. All controls below that index\n> > + * are written and their values are updated in \\a ctrls, while all other\n> > + * controls are not written and their values are not changed.\n> > + *\n> > + * \\return 0 on success or an error code otherwise\n> > + * \\retval -EINVAL One of the control is not supported or not accessible\n> > + * \\retval i The index of the control that failed\n> > + */\n> > +int V4L2Device::setControls(V4L2ControlList *ctrls)\n> > +{\n> > +\tunsigned int count = ctrls->size();\n> > +\tif (count == 0)\n> > +\t\treturn 0;\n> > +\n> > +\tconst V4L2ControlInfo *controlInfo[count] = {};\n> > +\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n> > +\tfor (V4L2Control &ctrl : *ctrls) {\n>\n> const V4L2Control &ctrl\n>\n> > +\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl.id());\n> > +\t\tif (!info) {\n> > +\t\t\tLOG(V4L2, Error)\n> > +\t\t\t\t<< \"Control '\" << ctrl.id() << \"' not found\";\n> > +\t\t\treturn -EINVAL;\n> > +\t\t}\n> > +\n> > +\t\tcontrolInfo[i] = info;\n> > +\t\tv4l2Ctrls[i].id = info->id();\n> > +\n> > +\t\t/* Set the v4l2_ext_control value for the write operation. */\n> > +\t\tswitch (info->type()) {\n> > +\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n> > +\t\t\tv4l2Ctrls[i].value64 = ctrl.value();\n> > +\t\t\tbreak;\n> > +\t\tdefault:\n> > +\t\t\t/*\n> > +\t\t\t * \\todo To be changed when support for string and\n> > +\t\t\t * compound controls will be added.\n> > +\t\t\t */\n> > +\t\t\tv4l2Ctrls[i].value = ctrl.value();\n> > +\t\t\tbreak;\n> > +\t\t}\n> > +\t}\n> > +\n> > +\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n> > +\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> > +\tv4l2ExtCtrls.controls = v4l2Ctrls;\n> > +\tv4l2ExtCtrls.count = count;\n> > +\n> > +\tint ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n> > +\tif (ret) {\n> > +\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> > +\n> > +\t\t/* Generic validation error. */\n> > +\t\tif (errorIdx == 0 || errorIdx >= count) {\n> > +\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n> > +\t\t\t\t\t << strerror(ret);\n> > +\t\t\treturn -EINVAL;\n> > +\t\t}\n> > +\n> > +\t\t/* A specific control failed. */\n> > +\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n> > +\t\t\t\t << \": \" << strerror(ret);\n> > +\t\tcount = errorIdx - 1;\n> > +\t\tret = errorIdx;\n> > +\t}\n> > +\n> > +\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n> > +\n> > +\treturn ret;\n> > +}\n> > +\n> >  /**\n> >   * \\brief Perform an IOCTL system call on the device node\n> >   * \\param[in] request The IOCTL request code\n> > @@ -194,4 +344,40 @@ void V4L2Device::listControls()\n> >  \t}\n> >  }\n> >\n> > +/*\n> > + * \\brief Update the value of \\a count V4L2 controls in \\a ctrls using values\n>\n> s/value of/value of the first/\n>\n> > + * in \\a v4l2Ctrls\n> > + * \\param[inout] ctrls List of V4L2 controls to update\n> > + * \\param[in] controlInfo List of V4L2 control information\n> > + * \\param[in] v4l2Ctrls List of V4L2 extended controls as returned by the driver\n> > + * \\param[in] count The number of controls to update\n> > + */\n> > +void V4L2Device::updateControls(V4L2ControlList *ctrls,\n> > +\t\t\t\tconst V4L2ControlInfo **controlInfo,\n> > +\t\t\t\tstruct v4l2_ext_control *v4l2Ctrls,\n>\n> This can be const.\n>\n> > +\t\t\t\tunsigned int count)\n> > +{\n> > +\tunsigned int i = 0;\n> > +\tfor (V4L2Control &ctrl : *ctrls) {\n> > +\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n> > +\t\tconst V4L2ControlInfo *info = controlInfo[i];\n> > +\n> > +\t\tswitch (info->type()) {\n> > +\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n> > +\t\t\tctrl.setValue(v4l2Ctrl->value64);\n> > +\t\t\tbreak;\n> > +\t\tdefault:\n> > +\t\t\t/*\n> > +\t\t\t * \\todo To be changed when support for string and\n> > +\t\t\t * compound controls will be added.\n> > +\t\t\t */\n> > +\t\t\tctrl.setValue(v4l2Ctrl->value);\n> > +\t\t\tbreak;\n> > +\t\t}\n> > +\n> > +\t\tif (++i == count)\n> > +\t\t\tbreak;\n>\n> I would move the test at the beginning of the loop, so that in the\n> unlikely case where count == 0 the function will not loop forever. You\n> could write\n\nThis doesn't get called if count == 0\n>\n> \t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n> \t\tconst V4L2ControlInfo *info = controlInfo[i];\n>\n> \t\tif (i++ == count)\n> \t\t\tbreak;\n>\n... for the moment at least.\n\nBetter safe than sorry, I'll move it up\n\n> \t\t...\n>\n> With these small issues addressed,\n>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n>\n\nThanks\n   j\n\n> > +\t}\n> > +}\n> > +\n> >  } /* namespace libcamera */\n>\n> --\n> Regards,\n>\n> Laurent Pinchart","headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay10.mail.gandi.net (relay10.mail.gandi.net\n\t[217.70.178.230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 30A4361580\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Jun 2019 18:39:29 +0200 (CEST)","from uno.localdomain (2-224-242-101.ip172.fastwebnet.it\n\t[2.224.242.101]) (Authenticated sender: jacopo@jmondi.org)\n\tby relay10.mail.gandi.net (Postfix) with ESMTPSA id B552C240009;\n\tMon, 24 Jun 2019 16:39:28 +0000 (UTC)"],"Date":"Mon, 24 Jun 2019 18:40:42 +0200","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190624164042.kpbagv5im3vwf65z@uno.localdomain>","References":"<20190624142859.19313-1-jacopo@jmondi.org>\n\t<20190624142859.19313-4-jacopo@jmondi.org>\n\t<20190624161648.GO5737@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha256;\n\tprotocol=\"application/pgp-signature\"; boundary=\"a5m7wir5rkfb2sc4\"","Content-Disposition":"inline","In-Reply-To":"<20190624161648.GO5737@pendragon.ideasonboard.com>","User-Agent":"NeoMutt/20180716","Subject":"Re: [libcamera-devel] [PATCH v6 3/6] libcamera: v4l2_device:\n\tImplement get and set controls","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 24 Jun 2019 16:39:29 -0000"}}]