{"id":3595,"url":"https://patchwork.libcamera.org/api/patches/3595/?format=json","web_url":"https://patchwork.libcamera.org/patch/3595/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200428170732.22123-1-laurent.pinchart@ideasonboard.com>","date":"2020-04-28T17:07:32","name":"[libcamera-devel,v3] libcamera: v4l2_device: Simplify usage of getControls()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d27a0246a17846c06adb9b994fcadc8ab31031e9","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3595/mbox/","series":[{"id":844,"url":"https://patchwork.libcamera.org/api/series/844/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=844","date":"2020-04-28T17:07:32","name":"[libcamera-devel,v3] libcamera: v4l2_device: Simplify usage of getControls()","version":3,"mbox":"https://patchwork.libcamera.org/series/844/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3595/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3595/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A96D860AF5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Apr 2020 19:07:51 +0200 (CEST)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3F1E472C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Apr 2020 19:07:51 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ApK+jLub\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1588093671;\n\tbh=kfC2g0rq8WezqDczaFcOHzk0u8QUwg3Ce9jabeWOmuI=;\n\th=From:To:Subject:Date:From;\n\tb=ApK+jLub9+1fKb5FOiB3umsTlfIy/hKj98K6f30Q7pj9tF1X4BpEDchnWWSWDSav8\n\tNmpfuAZAi8hlp6OAXFpXON5zFrnO8kk01Iyv2Sb5m6E8gopgaHU0h4bFh9yHY9ppBJ\n\tDZRrvIliNg4WNAH38dnWGtFPhhy9jTNucPVhGKp4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 28 Apr 2020 20:07:32 +0300","Message-Id":"<20200428170732.22123-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.25.3","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3] libcamera: v4l2_device: Simplify usage\n\tof getControls()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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":"Tue, 28 Apr 2020 17:07:51 -0000"},"content":"The V4L2Device::getControls() function takes a ControlList that needs to\nbe pre-populated with dummy entries for the controls that need to be\nread. This is a cumbersome API, especially when reading a single\ncontrol. Make it nicer by passing the list of V4L2 controls as a vector\nof control IDs, and returning a ControlList.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\nChanges since v2:\n\n- Minor documentation updates\n\nChanges since v1:\n\n- Fill the ControlList and v4l2_ext_control arrays separately\n---\n src/libcamera/camera_sensor.cpp       | 28 +++++--------\n src/libcamera/include/camera_sensor.h |  2 +-\n src/libcamera/include/v4l2_device.h   |  2 +-\n src/libcamera/v4l2_device.cpp         | 60 +++++++++++++++------------\n test/v4l2_videodevice/controls.cpp    | 20 +++++----\n 5 files changed, 57 insertions(+), 55 deletions(-)","diff":"diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 140186b79f6f..7289cc773034 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -323,30 +323,24 @@ const ControlInfoMap &CameraSensor::controls() const\n \n /**\n  * \\brief Read controls from the sensor\n- * \\param[inout] ctrls The list of controls to read\n+ * \\param[in] ids The list of control to read, specified by their ID\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+ * This method reads the value of all controls contained in \\a ids, and returns\n+ * their values as a ControlList.\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+ * If any control in \\a ids is not supported by the device, is disabled (i.e.\n+ * has the V4L2_CTRL_FLAG_DISABLED flag set), or if any other error occurs\n+ * during validation of the requested controls, no control is read and this\n+ * method returns an empty control list.\n  *\n  * \\sa V4L2Device::getControls()\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+ * \\return The control values in a ControlList on success, or an empty list on\n+ * error\n  */\n-int CameraSensor::getControls(ControlList *ctrls)\n+ControlList CameraSensor::getControls(const std::vector<uint32_t> &ids)\n {\n-\treturn subdev_->getControls(ctrls);\n+\treturn subdev_->getControls(ids);\n }\n \n /**\ndiff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h\nindex 7dc4ebc79051..e68185370eb2 100644\n--- a/src/libcamera/include/camera_sensor.h\n+++ b/src/libcamera/include/camera_sensor.h\n@@ -44,7 +44,7 @@ public:\n \tint setFormat(V4L2SubdeviceFormat *format);\n \n \tconst ControlInfoMap &controls() const;\n-\tint getControls(ControlList *ctrls);\n+\tControlList getControls(const std::vector<uint32_t> &ids);\n \tint setControls(ControlList *ctrls);\n \n \tconst ControlList &properties() const { return properties_; }\ndiff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex ce8edd98a01d..e604a40df4c9 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -26,7 +26,7 @@ public:\n \n \tconst ControlInfoMap &controls() const { return controls_; }\n \n-\tint getControls(ControlList *ctrls);\n+\tControlList getControls(const std::vector<uint32_t> &ids);\n \tint setControls(ControlList *ctrls);\n \n \tconst std::string &deviceNode() const { return deviceNode_; }\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex c134266e5fd7..d08ab531f034 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -147,46 +147,51 @@ void V4L2Device::close()\n \n /**\n  * \\brief Read controls from the device\n- * \\param[inout] ctrls The list of controls to read\n+ * \\param[in] ids The list of controls to read, specified by their ID\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+ * This method reads the value of all controls contained in \\a ids, and returns\n+ * their values as a ControlList.\n  *\n- * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n+ * If any control in \\a ids is not supported by the device, is disabled (i.e.\n  * has the V4L2_CTRL_FLAG_DISABLED flag set), or if any other error occurs\n  * during validation of the requested controls, no control is read and this\n- * method returns -EINVAL.\n+ * method returns an empty control list.\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- * \\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+ * \\return The control values in a ControlList on success, or an empty list on\n+ * error\n  */\n-int V4L2Device::getControls(ControlList *ctrls)\n+ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)\n {\n-\tunsigned int count = ctrls->size();\n+\tunsigned int count = ids.size();\n \tif (count == 0)\n-\t\treturn 0;\n+\t\treturn {};\n \n-\tstruct v4l2_ext_control v4l2Ctrls[count];\n-\tmemset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));\n+\tControlList ctrls{ controls_ };\n \n-\tunsigned int i = 0;\n-\tfor (auto &ctrl : *ctrls) {\n-\t\tunsigned int id = ctrl.first;\n+\t/*\n+\t * Start by filling the ControlList. This can't be combined with filling\n+\t * v4l2Ctrls, as updateControls() relies on both containers having the\n+\t * same order, and the control list is based on a map, which is not\n+\t * sorted by insertion order.\n+\t */\n+\tfor (uint32_t id : ids) {\n \t\tconst auto iter = controls_.find(id);\n \t\tif (iter == controls_.end()) {\n \t\t\tLOG(V4L2, Error)\n \t\t\t\t<< \"Control \" << utils::hex(id) << \" not found\";\n-\t\t\treturn -EINVAL;\n+\t\t\treturn {};\n \t\t}\n \n+\t\tctrls.set(id, {});\n+\t}\n+\n+\tstruct v4l2_ext_control v4l2Ctrls[count];\n+\tmemset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));\n+\n+\tunsigned int i = 0;\n+\tfor (auto &ctrl : ctrls) {\n+\t\tunsigned int id = ctrl.first;\n \t\tconst struct v4l2_query_ext_ctrl &info = controlInfo_[id];\n-\t\tControlValue &value = ctrl.second;\n \n \t\tif (info.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {\n \t\t\tControlType type;\n@@ -200,9 +205,10 @@ int V4L2Device::getControls(ControlList *ctrls)\n \t\t\t\tLOG(V4L2, Error)\n \t\t\t\t\t<< \"Unsupported payload control type \"\n \t\t\t\t\t<< info.type;\n-\t\t\t\treturn -EINVAL;\n+\t\t\t\treturn {};\n \t\t\t}\n \n+\t\t\tControlValue &value = ctrl.second;\n \t\t\tvalue.reserve(type, true, info.elems);\n \t\t\tSpan<uint8_t> data = value.data();\n \n@@ -227,7 +233,7 @@ int V4L2Device::getControls(ControlList *ctrls)\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\treturn {};\n \t\t}\n \n \t\t/* A specific control failed. */\n@@ -237,9 +243,9 @@ int V4L2Device::getControls(ControlList *ctrls)\n \t\tret = errorIdx;\n \t}\n \n-\tupdateControls(ctrls, v4l2Ctrls, count);\n+\tupdateControls(&ctrls, v4l2Ctrls, count);\n \n-\treturn ret;\n+\treturn ctrls;\n }\n \n /**\ndiff --git a/test/v4l2_videodevice/controls.cpp b/test/v4l2_videodevice/controls.cpp\nindex da9e0111e221..347af2112f1a 100644\n--- a/test/v4l2_videodevice/controls.cpp\n+++ b/test/v4l2_videodevice/controls.cpp\n@@ -57,18 +57,20 @@ protected:\n \t\tconst ControlInfo &u8 = infoMap.find(VIVID_CID_U8_4D_ARRAY)->second;\n \n \t\t/* Test getting controls. */\n-\t\tControlList ctrls(infoMap);\n-\t\tctrls.set(V4L2_CID_BRIGHTNESS, -1);\n-\t\tctrls.set(V4L2_CID_CONTRAST, -1);\n-\t\tctrls.set(V4L2_CID_SATURATION, -1);\n-\t\tctrls.set(VIVID_CID_U8_4D_ARRAY, 0);\n-\n-\t\tint ret = capture_->getControls(&ctrls);\n-\t\tif (ret) {\n+\t\tControlList ctrls = capture_->getControls({ V4L2_CID_BRIGHTNESS,\n+\t\t\t\t\t\t\t    V4L2_CID_CONTRAST,\n+\t\t\t\t\t\t\t    V4L2_CID_SATURATION,\n+\t\t\t\t\t\t\t    VIVID_CID_U8_4D_ARRAY });\n+\t\tif (ctrls.empty()) {\n \t\t\tcerr << \"Failed to get controls\" << endl;\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\tif (ctrls.infoMap() != &infoMap) {\n+\t\t\tcerr << \"Incorrect infoMap for retrieved controls\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\tif (ctrls.get(V4L2_CID_BRIGHTNESS).get<int32_t>() == -1 ||\n \t\t    ctrls.get(V4L2_CID_CONTRAST).get<int32_t>() == -1 ||\n \t\t    ctrls.get(V4L2_CID_SATURATION).get<int32_t>() == -1) {\n@@ -97,7 +99,7 @@ protected:\n \t\tstd::fill(u8Values.begin(), u8Values.end(), u8.min().get<uint8_t>());\n \t\tctrls.set(VIVID_CID_U8_4D_ARRAY, Span<const uint8_t>(u8Values));\n \n-\t\tret = capture_->setControls(&ctrls);\n+\t\tint ret = capture_->setControls(&ctrls);\n \t\tif (ret) {\n \t\t\tcerr << \"Failed to set controls\" << endl;\n \t\t\treturn TestFail;\n","prefixes":["libcamera-devel","v3"]}