{"id":3545,"url":"https://patchwork.libcamera.org/api/patches/3545/?format=json","web_url":"https://patchwork.libcamera.org/patch/3545/","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":"<20200425231623.6505-2-laurent.pinchart@ideasonboard.com>","date":"2020-04-25T23:16:23","name":"[libcamera-devel,2/2] libcamera: v4l2_device: Simplify usage of getControls()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"f4626ec9301e9a625ddf45cdf37ad1b14f4e9f44","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/3545/mbox/","series":[{"id":826,"url":"https://patchwork.libcamera.org/api/series/826/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=826","date":"2020-04-25T23:16:22","name":"[libcamera-devel,1/2] libcamera: controls: Return ControlValue reference from ControlList::set()","version":1,"mbox":"https://patchwork.libcamera.org/series/826/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3545/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3545/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 21A93603FD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 26 Apr 2020 01:16:42 +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 A3D0B564;\n\tSun, 26 Apr 2020 01:16:41 +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=\"UkCGD9Gj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1587856601;\n\tbh=DpQsZoJDyr+ksVcMup3zK59+RrOfxnGAEz1BJfVPhA4=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=UkCGD9Gjzq2X1DY4aHT/kFdCulCmrqDQZVEZN5jsN6zhSiMpMOfX5kEKNjuedur0x\n\t8QOHpywXSxv3lXfBKiPlI1XNkWSL2ZG4bTG69/kateh95J0ukGTX/ziAtj/UcQAnuA\n\th7YUyAw1C5VokzDaMNgyNX+4CGLD7tfC5qGN28Zc=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun, 26 Apr 2020 02:16:23 +0300","Message-Id":"<20200425231623.6505-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.25.3","In-Reply-To":"<20200425231623.6505-1-laurent.pinchart@ideasonboard.com>","References":"<20200425231623.6505-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 2/2] libcamera: v4l2_device: Simplify\n\tusage of 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":"Sat, 25 Apr 2020 23:16:42 -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>\n---\n src/libcamera/camera_sensor.cpp       | 23 ++++++---------\n src/libcamera/include/camera_sensor.h |  2 +-\n src/libcamera/include/v4l2_device.h   |  2 +-\n src/libcamera/v4l2_device.cpp         | 42 ++++++++++++---------------\n test/v4l2_videodevice/controls.cpp    | 20 +++++++------\n 5 files changed, 41 insertions(+), 48 deletions(-)","diff":"diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 2219a4307436..ce585cab1a4f 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -299,30 +299,25 @@ 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+ * 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), 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+ * read and this 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+ * If an error occurs while reading the controls, an empty list is returned.\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\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 99cff98128dc..5277f7f7fe87 100644\n--- a/src/libcamera/include/camera_sensor.h\n+++ b/src/libcamera/include/camera_sensor.h\n@@ -43,7 +43,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 03e305165096..989ed05c9692 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -147,46 +147,42 @@ 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 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+ * 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), 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+ * read and this 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+ * If an error occurs while reading the controls, an empty list is returned.\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\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+\tControlList ctrls{ controls_ };\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+\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\tconst struct v4l2_query_ext_ctrl &info = controlInfo_[id];\n-\t\tControlValue &value = ctrl.second;\n+\t\tControlValue &value = ctrls.set(id, {});\n \n \t\tif (info.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {\n \t\t\tControlType type;\n@@ -200,7 +196,7 @@ 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\tvalue.reserve(type, true, info.elems);\n@@ -227,7 +223,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 +233,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","2/2"]}