Patch Detail
Show a patch.
GET /api/patches/75/?format=api
{ "id": 75, "url": "https://patchwork.libcamera.org/api/patches/75/?format=api", "web_url": "https://patchwork.libcamera.org/patch/75/", "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": "<20181221123724.27290-3-kieran.bingham@ideasonboard.com>", "date": "2018-12-21T12:37:24", "name": "[libcamera-devel,2/2] lib: Add V4L2 Device object", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "2105e13d1f1fc750b69de4d0d26fc4442b5d98a3", "submitter": { "id": 4, "url": "https://patchwork.libcamera.org/api/people/4/?format=api", "name": "Kieran Bingham", "email": "kieran.bingham@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/75/mbox/", "series": [ { "id": 37, "url": "https://patchwork.libcamera.org/api/series/37/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=37", "date": "2018-12-21T12:37:22", "name": "libcamera: Initial v4l2device object", "version": 1, "mbox": "https://patchwork.libcamera.org/series/37/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/75/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/75/checks/", "tags": {}, "headers": { "Return-Path": "<kieran.bingham@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 03B6760B2D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 21 Dec 2018 13:37:36 +0100 (CET)", "from localhost.localdomain\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 6E7DE597;\n\tFri, 21 Dec 2018 13:37:29 +0100 (CET)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1545395849;\n\tbh=54Fyp/sWlGLaQ1zL6H/oIlsZo8xZ9W/dYCEUCOIB8Gg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=DXvmFPWBQrJOS24VErnzuEL0fTNuxlusbpQvi/Eq+Z02NgOc9ankBS6QnEKKeqhJE\n\tYN+JC9GwbkT0lSOkyDUeRWHYz/jhtYJCdmy8yd70M/WtAP1EbCNX/Eg5oBidVswXX7\n\t3O9ltZdbecKxXUlFeRQi898M2VFZMClwFJUXJ3G4=", "From": "Kieran Bingham <kieran.bingham@ideasonboard.com>", "To": "LibCamera Devel <libcamera-devel@lists.libcamera.org>", "Date": "Fri, 21 Dec 2018 12:37:24 +0000", "Message-Id": "<20181221123724.27290-3-kieran.bingham@ideasonboard.com>", "X-Mailer": "git-send-email 2.17.1", "In-Reply-To": "<20181221123724.27290-1-kieran.bingham@ideasonboard.com>", "References": "<20181221123724.27290-1-kieran.bingham@ideasonboard.com>", "Subject": "[libcamera-devel] [PATCH 2/2] lib: Add V4L2 Device object", "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": "Fri, 21 Dec 2018 12:37:36 -0000" }, "content": "Provide a helper V4L2 device object capable of interacting with the\nV4L2 Linux Kernel APIs.\n\nA test suite is added at test/v4l2_device/ to utilise and validate the\ncode base.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/libcamera/include/v4l2_device.h | 36 +++++++\n src/libcamera/meson.build | 2 +\n src/libcamera/v4l2_device.cpp | 137 ++++++++++++++++++++++++++\n test/meson.build | 2 +\n test/v4l2_device/double_open.cpp | 32 ++++++\n test/v4l2_device/meson.build | 12 +++\n test/v4l2_device/v4l2_device_test.cpp | 36 +++++++\n test/v4l2_device/v4l2_device_test.h | 31 ++++++\n 8 files changed, 288 insertions(+)\n create mode 100644 src/libcamera/include/v4l2_device.h\n create mode 100644 src/libcamera/v4l2_device.cpp\n create mode 100644 test/v4l2_device/double_open.cpp\n create mode 100644 test/v4l2_device/meson.build\n create mode 100644 test/v4l2_device/v4l2_device_test.cpp\n create mode 100644 test/v4l2_device/v4l2_device_test.h", "diff": "diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nnew file mode 100644\nindex 000000000000..619d932d3c82\n--- /dev/null\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -0,0 +1,36 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * v4l2_device.h - V4L2 Device API Abstractions\n+ */\n+#ifndef __LIBCAMERA_V4L2_DEVICE_H__\n+#define __LIBCAMERA_V4L2_DEVICE_H__\n+\n+#include <string>\n+\n+#include <linux/videodev2.h>\n+\n+namespace libcamera {\n+\n+class V4L2Device\n+{\n+public:\n+\tV4L2Device(const std::string &);\n+\t~V4L2Device();\n+\n+\tbool isOpen();\n+\tint open();\n+\tvoid close();\n+\n+\tint queryCap();\n+\n+private:\n+\tstd::string device_;\n+\tint fd_;\n+\tint capabilities_;\n+};\n+\n+}\n+\n+#endif /* __LIBCAMERA_V4L2_DEVICE_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex f632eb5dd779..bbaf9a05ec2c 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -1,11 +1,13 @@\n libcamera_sources = files([\n 'log.cpp',\n 'main.cpp',\n+ 'v4l2_device.cpp',\n ])\n \n libcamera_headers = files([\n 'include/log.h',\n 'include/utils.h',\n+ 'include/v4l2_device.h',\n ])\n \n libcamera_internal_includes = include_directories('include')\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nnew file mode 100644\nindex 000000000000..eea2514f343c\n--- /dev/null\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -0,0 +1,137 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * v4l2_device.cpp - V4L2 Device API\n+ */\n+\n+#include <fcntl.h>\n+#include <string.h>\n+#include <unistd.h>\n+\n+#include <sys/ioctl.h>\n+#include <sys/mman.h>\n+\n+#include \"log.h\"\n+#include \"v4l2_device.h\"\n+\n+/**\n+ * \\file v4l2_device.cpp\n+ * \\brief V4L2 Device API\n+ */\n+namespace libcamera {\n+\n+/**\n+ * \\class V4L2Device\n+ * \\brief V4L2Device object and API.\n+ *\n+ * The V4L2 Device API class models a single instance of a v4l2 device node.\n+ */\n+\n+/**\n+ * \\fn V4L2Device::V4L2Device(device)\n+ *\n+ * Default constructor for a V4L2Device object. The \\a device specifies a\n+ * string to representation of the device object.\n+ */\n+V4L2Device::V4L2Device(const std::string &device)\n+{\n+\tdevice_ = device;\n+\tfd_ = -1;\n+\tcapabilities_ = 0;\n+\n+\tLOG(Debug) << \"V4L2Device Constructed for \" << device_;\n+}\n+\n+V4L2Device::~V4L2Device()\n+{\n+\tclose();\n+\n+\tLOG(Debug) << \"V4L2Device Destroyed\";\n+}\n+\n+/**\n+ * \\fn V4L2Device::isOpen(())\n+ *\n+ * Checks to see if we have successfully opened a v4l2 video device.\n+ */\n+bool V4L2Device::isOpen()\n+{\n+\treturn (fd_ != -1);\n+}\n+\n+/**\n+ * \\fn V4L2Device::open()\n+ *\n+ * Opens a v4l2 device and queries properties from the device.\n+ */\n+int V4L2Device::open()\n+{\n+\tint ret;\n+\n+\tif (isOpen()) {\n+\t\tLOG(Error) << \"Device already open\";\n+\t\treturn -EBADF;\n+\t}\n+\n+\tfd_ = ::open(device_.c_str(), O_RDWR);\n+\tif (fd_ < 0) {\n+\t\tLOG(Error) << \"Failed to open V4L2 device \" << device_\n+\t\t\t << \" : \" << strerror(errno);\n+\t\treturn -errno;\n+\t}\n+\n+\tret = queryCap();\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (!(capabilities_ & V4L2_CAP_STREAMING)) {\n+\t\tLOG(Error) << \"Device does not support streaming IO\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\fn V4L2Device::close()\n+ *\n+ * Close a v4l2 device handle.\n+ */\n+void V4L2Device::close()\n+{\n+\tif (fd_ < 0)\n+\t\treturn;\n+\n+\t::close(fd_);\n+\tfd_ = -1;\n+}\n+\n+/**\n+ * \\fn V4L2Device::queryCap()\n+ *\n+ * Obtains the capabilities information from the V4L2 Device.\n+ */\n+int V4L2Device::queryCap()\n+{\n+\tstruct v4l2_capability cap = {};\n+\tint ret;\n+\n+\tif (!isOpen()) {\n+\t\tLOG(Error) << \"Attempt to query unopened device\";\n+\t\treturn -EBADF;\n+\t}\n+\n+\tret = ioctl(fd_, VIDIOC_QUERYCAP, &cap);\n+\tif (ret < 0) {\n+\t\tLOG(Error) << \"Failed to query device capabilities\";\n+\t\treturn -1;\n+\t}\n+\n+\tcapabilities_ = cap.capabilities & V4L2_CAP_DEVICE_CAPS\n+\t\t ? cap.device_caps : cap.capabilities;\n+\n+\treturn 0;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/test/meson.build b/test/meson.build\nindex 754527324c7d..2bc76c289eea 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -12,6 +12,8 @@ test_includes_internal = [\n libcamera_internal_includes,\n ]\n \n+subdir('v4l2_device')\n+\n public_tests = [\n [ 'test_init', 'init.cpp' ],\n ]\ndiff --git a/test/v4l2_device/double_open.cpp b/test/v4l2_device/double_open.cpp\nnew file mode 100644\nindex 000000000000..1b7c7bfe14b8\n--- /dev/null\n+++ b/test/v4l2_device/double_open.cpp\n@@ -0,0 +1,32 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * libcamera V4L2 API tests\n+ */\n+\n+#include \"v4l2_device_test.h\"\n+\n+class DoubleOpen : public V4L2DeviceTest\n+{\n+protected:\n+\tint run()\n+\t{\n+\t\tint ret;\n+\n+\t\t/*\n+\t\t * Expect failure: The device has already been opened by the\n+\t\t * V4L2DeviceTest base class\n+\t\t */\n+\t\tret = dev->open();\n+\t\tif (!ret) {\n+\t\t\tfprintf(stderr, \"Double open erroneously succeeded\\n\");\n+\t\t\tdev->close();\n+\t\t\treturn TEST_FAIL;\n+\t\t}\n+\n+\t\treturn TEST_PASS;\n+\t}\n+};\n+\n+TEST_REGISTER(DoubleOpen);\ndiff --git a/test/v4l2_device/meson.build b/test/v4l2_device/meson.build\nnew file mode 100644\nindex 000000000000..41675a303498\n--- /dev/null\n+++ b/test/v4l2_device/meson.build\n@@ -0,0 +1,12 @@\n+# Tests are listed in order of complexity.\n+# They are not alphabetically sorted.\n+v4l2_device_tests = [\n+ [ 'double_open', 'double_open.cpp' ],\n+]\n+\n+foreach t : v4l2_device_tests\n+ exe = executable(t[0], [t[1], 'v4l2_device_test.cpp'],\n+\t\t link_with : test_libraries,\n+\t\t include_directories : test_includes_internal)\n+ test(t[0], exe, suite: 'v4l2_device', is_parallel: false)\n+endforeach\ndiff --git a/test/v4l2_device/v4l2_device_test.cpp b/test/v4l2_device/v4l2_device_test.cpp\nnew file mode 100644\nindex 000000000000..ae317a9519c5\n--- /dev/null\n+++ b/test/v4l2_device/v4l2_device_test.cpp\n@@ -0,0 +1,36 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * libcamera V4L2 API tests\n+ */\n+\n+#include \"v4l2_device_test.h\"\n+\n+using namespace libcamera;\n+\n+V4L2DeviceTest::V4L2DeviceTest()\n+{\n+\tdev = nullptr;\n+}\n+\n+int V4L2DeviceTest::init()\n+{\n+\tconst char *device = \"/dev/video0\";\n+\n+\t/* Validate the device node exists. */\n+\tif (!path_exists(device))\n+\t\treturn TEST_SKIP;\n+\n+\tdev = new V4L2Device(device);\n+\tif (!dev)\n+\t\treturn TEST_FAIL;\n+\n+\treturn dev->open();\n+}\n+\n+void V4L2DeviceTest::cleanup()\n+{\n+\tif (dev)\n+\t\tdelete dev;\n+};\ndiff --git a/test/v4l2_device/v4l2_device_test.h b/test/v4l2_device/v4l2_device_test.h\nnew file mode 100644\nindex 000000000000..4374ddc7e932\n--- /dev/null\n+++ b/test/v4l2_device/v4l2_device_test.h\n@@ -0,0 +1,31 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * vl42device_test.h - libcamera v4l2device test base class\n+ */\n+\n+#include \"test.h\"\n+#include \"v4l2_device.h\"\n+\n+#ifndef __LIBCAMERA_V4L2_DEVICE_TEST_H_\n+#define __LIBCAMERA_V4L2_DEVICE_TEST_H_\n+\n+using namespace libcamera;\n+\n+class V4L2DeviceTest : public Test\n+{\n+public:\n+\tV4L2DeviceTest();\n+\tvirtual ~V4L2DeviceTest() {};\n+\n+protected:\n+\tint init();\n+\tvoid cleanup();\n+\n+\tvirtual int run() = 0;\n+\n+\tV4L2Device *dev;\n+};\n+\n+#endif /* __LIBCAMERA_V4L2_DEVICE_TEST_H_ */\n", "prefixes": [ "libcamera-devel", "2/2" ] }