Show a patch.

GET /api/patches/1476/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1476,
    "url": "https://patchwork.libcamera.org/api/patches/1476/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/1476/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20190619110858.20980-4-jacopo@jmondi.org>",
    "date": "2019-06-19T11:08:55",
    "name": "[libcamera-devel,v4,3/6] libcamera: v4l2_device: Implement get and set controls",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "851b3223d2e97f59a3d90764e1b7c8ce72680db7",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/1476/mbox/",
    "series": [
        {
            "id": 364,
            "url": "https://patchwork.libcamera.org/api/series/364/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=364",
            "date": "2019-06-19T11:08:52",
            "name": "Add support for V4L2 Controls",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/364/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/1476/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/1476/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<jacopo@jmondi.org>",
        "Received": [
            "from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net\n\t[217.70.183.199])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F0BBC61A3D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 19 Jun 2019 13:07:50 +0200 (CEST)",
            "from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 82E13FF803;\n\tWed, 19 Jun 2019 11:07:50 +0000 (UTC)"
        ],
        "X-Originating-IP": "2.224.242.101",
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Wed, 19 Jun 2019 13:08:55 +0200",
        "Message-Id": "<20190619110858.20980-4-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.21.0",
        "In-Reply-To": "<20190619110858.20980-1-jacopo@jmondi.org>",
        "References": "<20190619110858.20980-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 3/6] libcamera: v4l2_device: Implement\n\tget 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": "Wed, 19 Jun 2019 11:07:51 -0000"
    },
    "content": "Implement getControls() and setControls() operations in V4L2Device class.\n\nBoth operations take a V4L2Controls instance and read or write the V4L2\ncontrols on the V4L2 device.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/include/v4l2_device.h |   6 +\n src/libcamera/v4l2_device.cpp       | 189 ++++++++++++++++++++++++++++\n 2 files changed, 195 insertions(+)",
    "diff": "diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 563be151c257..e66cc7ebe737 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -15,6 +15,7 @@\n namespace libcamera {\n \n class V4L2ControlInfo;\n+class V4L2Controls;\n \n class V4L2Device : protected Loggable\n {\n@@ -23,6 +24,10 @@ public:\n \tbool isOpen() const { return fd_ != -1; }\n \tconst std::string &deviceNode() const { return deviceNode_; }\n \n+\tV4L2ControlInfo *getControlInfo(unsigned int id);\n+\tint getControls(V4L2Controls *ctrls);\n+\tint setControls(V4L2Controls *ctrls);\n+\n protected:\n \tV4L2Device(const std::string &deviceNode, const std::string &logTag);\n \t~V4L2Device();\n@@ -37,6 +42,7 @@ protected:\n \n private:\n \tvoid listControls();\n+\tint validateControlType(V4L2ControlInfo *info);\n \n \tstd::map<unsigned int, V4L2ControlInfo *> controls_;\n \tstd::string deviceNode_;\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 58dd94836686..5af0ddc468fe 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -66,6 +66,170 @@ void V4L2Device::close()\n  * \\return The device node path\n  */\n \n+/**\n+ * \\brief Get informations for control with \\a id\n+ * \\param[in] id The control id to search info for\n+ * \\return The V4L2ControlInfo associated to the V4L2 control with \\a id or\n+ * nullptr if the control is not supported by the V4L2Device\n+ */\n+V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id)\n+{\n+\tauto it = controls_.find(id);\n+\tif (it == controls_.end())\n+\t\treturn nullptr;\n+\n+\treturn it->second;\n+}\n+\n+/**\n+ * \\brief Read values of a list of controls from the device\n+ * \\param[inout] ctrls The list of controls to read\n+ *\n+ * Read the value of each V4L2Controls contained in \\a ctrls, overwriting\n+ * it's current value with the one returned by the device.\n+ *\n+ * Each V4L2Control instance in \\a ctrls should be supported by the device.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+int V4L2Device::getControls(V4L2Controls *ctrls)\n+{\n+\tunsigned int count = ctrls->size();\n+\tif (count == 0)\n+\t\treturn 0;\n+\n+\tstruct V4L2ControlInfo *controlInfo[count];\n+\tstruct v4l2_ext_control v4l2Ctrls[count];\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\t/* Validate the control. */\n+\t\tV4L2ControlInfo *info = getControlInfo(ctrl->id());\n+\t\tif (!info) {\n+\t\t\tLOG(V4L2, Error) << \"Control '\" << ctrl->id()\n+\t\t\t\t\t << \"' not valid\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tif (validateControlType(info))\n+\t\t\treturn -EINVAL;\n+\n+\t\tcontrolInfo[i] = info;\n+\n+\t\t/* Prepare the v4l2_ext_control entry for the read operation. */\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tv4l2Ctrl->id = info->id();\n+\t\tv4l2Ctrl->size = info->size();\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\tLOG(V4L2, Error) << \"Unable to read controls: \"\n+\t\t\t\t << strerror(ret);\n+\t\treturn ret;\n+\t}\n+\n+\t/*\n+\t * For each control read from the device, set the value in the\n+\t * V4L2ControlValue provided by the caller.\n+\t */\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tV4L2ControlInfo *info = controlInfo[i];\n+\t\tV4L2Control *ctrl = (*ctrls)[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\tcase V4L2_CTRL_TYPE_INTEGER:\n+\t\tcase V4L2_CTRL_TYPE_BOOLEAN:\n+\t\tcase V4L2_CTRL_TYPE_BUTTON:\n+\t\tcase V4L2_CTRL_TYPE_MENU:\n+\t\t\tctrl->setValue(static_cast<long int>(v4l2Ctrl->value));\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Write a list of controls to the device\n+ * \\param[in] ctrls The list of controls to apply\n+ *\n+ * Write the value of each V4L2Control contained in \\a ctrls. Each\n+ * control should be initialized by the caller with a value, otherwise\n+ * the result of the operation is undefined.\n+ *\n+ * The value of each control is not updated to reflect what has been\n+ * actually applied on the device. To read the actual value of a control\n+ * after a setControls(), users should read the control value back with\n+ * getControls().\n+ *\n+ * Each V4L2Control instance in \\a ctrls should be supported by the device.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+int V4L2Device::setControls(V4L2Controls *ctrls)\n+{\n+\tunsigned int count = ctrls->size();\n+\tif (count == 0)\n+\t\treturn 0;\n+\n+\tstruct v4l2_ext_control v4l2Ctrls[count];\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\t/* Validate the control. */\n+\t\tV4L2ControlInfo *info = getControlInfo(ctrl->id());\n+\t\tif (!info) {\n+\t\t\tLOG(V4L2, Error) << \"Control '\" << ctrl->id()\n+\t\t\t\t\t << \"' not valid\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tif (validateControlType(info))\n+\t\t\treturn -EINVAL;\n+\n+\t\t/* Prepare the v4l2_ext_control entry for the write operation. */\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tv4l2Ctrl->id = info->id();\n+\t\tv4l2Ctrl->size = info->size();\n+\n+\t\tswitch (info->type()) {\n+\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\tv4l2Ctrl->value64 = ctrl->value();\n+\t\t\tbreak;\n+\t\tcase V4L2_CTRL_TYPE_INTEGER:\n+\t\tcase V4L2_CTRL_TYPE_BOOLEAN:\n+\t\tcase V4L2_CTRL_TYPE_BUTTON:\n+\t\tcase V4L2_CTRL_TYPE_MENU:\n+\t\t\tv4l2Ctrl->value = static_cast<int32_t>(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\tLOG(V4L2, Error) << \"Unable to write controls: \"\n+\t\t\t\t << strerror(ret);\n+\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Construct a V4L2Device\n  * \\param[in] deviceNode The device node filesystem path\n@@ -171,4 +335,29 @@ void V4L2Device::listControls()\n \t}\n }\n \n+/*\n+ * \\brief Make sure the control type is supported\n+ * \\param[in] info The V4L2ControlInfo to inspect type of\n+ * \\return 0 on success or a negative error code otherwise\n+ * \\retval -EINVAL The control type is not supported\n+ */\n+int V4L2Device::validateControlType(V4L2ControlInfo *info)\n+{\n+\t/* \\todo support compound and menu controls. */\n+\tswitch (info->type()) {\n+\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\tcase V4L2_CTRL_TYPE_INTEGER:\n+\tcase V4L2_CTRL_TYPE_BOOLEAN:\n+\tcase V4L2_CTRL_TYPE_BUTTON:\n+\tcase V4L2_CTRL_TYPE_MENU:\n+\t\tbreak;\n+\tdefault:\n+\t\tLOG(V4L2, Error) << \"Control type '\" << info->type()\n+\t\t\t\t << \"' not supported\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn 0;\n+}\n+\n } /* namespace libcamera */\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "3/6"
    ]
}