{"id":242,"url":"https://patchwork.libcamera.org/api/patches/242/?format=json","web_url":"https://patchwork.libcamera.org/patch/242/","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":"<20190115160212.30100-2-kieran.bingham@ideasonboard.com>","date":"2019-01-15T16:02:11","name":"[libcamera-devel,v2,1/2] lib: Add V4L2 Device object","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"62ad90f8439e434db9dd14adfeac74bfb982bc72","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/242/mbox/","series":[{"id":82,"url":"https://patchwork.libcamera.org/api/series/82/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=82","date":"2019-01-15T16:02:10","name":"V4L2Device: Add basic V4L2 support class","version":2,"mbox":"https://patchwork.libcamera.org/series/82/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/242/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/242/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 AB43660C87\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 15 Jan 2019 17:02:16 +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 2582D55C;\n\tTue, 15 Jan 2019 17:02:16 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1547568136;\n\tbh=t4B78EX19LzPc5m017d2sAynJs8ktdwFz+pGjbGWmsE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=nnq0d21F8y2zb+r5FuN0QLD1fD62BzX96tF+g8NSbG28FoWPOG0Y+9RCXxH8aYN5H\n\tfj3A7P6qgQURSF1hwRSFCBqRWkoGn7T85B3EE20+MGnM4zzs0KHpasixash66VhsRj\n\tlcJkbMhM6ajRe2m8iShhbhT6YNb5j23ytjChcm3Y=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"LibCamera Devel <libcamera-devel@lists.libcamera.org>","Date":"Tue, 15 Jan 2019 16:02:11 +0000","Message-Id":"<20190115160212.30100-2-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.17.1","In-Reply-To":"<20190115160212.30100-1-kieran.bingham@ideasonboard.com>","References":"<20190115160212.30100-1-kieran.bingham@ideasonboard.com>","Subject":"[libcamera-devel] [PATCH v2 1/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":"Tue, 15 Jan 2019 16:02:16 -0000"},"content":"Provide a helper V4L2 device object capable of interacting with the\nV4L2 Linux Kernel APIs.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/libcamera/include/v4l2_device.h |  43 ++++++++++\n src/libcamera/meson.build           |   2 +\n src/libcamera/v4l2_device.cpp       | 127 ++++++++++++++++++++++++++++\n 3 files changed, 172 insertions(+)\n create mode 100644 src/libcamera/include/v4l2_device.h\n create mode 100644 src/libcamera/v4l2_device.cpp","diff":"diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nnew file mode 100644\nindex 000000000000..ddcb17af2187\n--- /dev/null\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -0,0 +1,43 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, 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 V4L2Capability : public v4l2_capability\n+{\n+public:\n+\tbool isCapture() { return capabilities & V4L2_CAP_VIDEO_CAPTURE; }\n+\tbool isMplane() { return capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE; }\n+\tbool hasStreaming() { return capabilities & V4L2_CAP_STREAMING; }\n+};\n+\n+class V4L2Device\n+{\n+public:\n+\tV4L2Device() = delete;\n+\tV4L2Device(const std::string &device);\n+\t~V4L2Device();\n+\n+\tint open();\n+\tbool isOpen() const;\n+\tvoid close();\n+\n+private:\n+\tstd::string device_;\n+\tint fd_;\n+\tV4L2Capability caps_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_V4L2_DEVICE_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex abf9a71d4172..f9f25c0ecf15 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -11,6 +11,7 @@ libcamera_sources = files([\n     'pipeline_handler.cpp',\n     'signal.cpp',\n     'timer.cpp',\n+    'v4l2_device.cpp',\n ])\n \n libcamera_headers = files([\n@@ -21,6 +22,7 @@ libcamera_headers = files([\n     'include/media_object.h',\n     'include/pipeline_handler.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..3133bfe4ffb0\n--- /dev/null\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -0,0 +1,127 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * v4l2_device.cpp - V4L2 Device API\n+ */\n+\n+#include <fcntl.h>\n+#include <string.h>\n+#include <sys/ioctl.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+\n+#include \"log.h\"\n+#include \"v4l2_device.h\"\n+\n+/**\n+ * \\file v4l2_device.h\n+ * \\brief V4L2 Device API\n+ */\n+namespace libcamera {\n+\n+/**\n+ * \\class V4L2Capability\n+ * \\brief struct v4l2_capability object wrapper and helpers\n+ *\n+ * The V4L2Capability wraps the V4L2 API interactions for capabilities and\n+ * device flag parsing.\n+ */\n+\n+/**\n+ * \\fn bool V4L2Capability::isCapture()\n+ * \\brief Identify if the device is capable of capturing video\n+ * \\return boolean true if the device provides video frames\n+ */\n+\n+/**\n+ * \\fn bool V4L2Capability::isMplane()\n+ * \\brief Identify if the device uses MPLANE formats\n+ * \\return boolean true if the device uses multiplanar buffer formats\n+ */\n+\n+/**\n+ * \\fn bool V4L2Capability::hasStreaming()\n+ * \\brief Determine if the device can perform Streaming IO\n+ * \\return boolean true if the device provides Streaming IO IOCTLs\n+ */\n+\n+/**\n+ * \\class V4L2Device\n+ * \\brief V4L2Device object and API.\n+ *\n+ * The V4L2 Device API class models an instance of a v4l2 device node.\n+ */\n+\n+/**\n+ * \\brief Constructor for a V4L2Device object. The \\a device specifies the path\n+ * to the video device node.\n+ * \\param device The file-system path to the video device node\n+ */\n+V4L2Device::V4L2Device(const std::string &device)\n+{\n+\tdevice_ = device;\n+\tfd_ = -1;\n+\tcaps_ = { };\n+}\n+\n+V4L2Device::~V4L2Device()\n+{\n+\tclose();\n+}\n+\n+/**\n+ * \\brief Opens a v4l2 device and queries properties from the device.\n+ * \\return 0 on success, or a negative errno\n+ */\n+int V4L2Device::open()\n+{\n+\tint ret;\n+\n+\tif (isOpen()) {\n+\t\tLOG(Error) << \"Device already open\";\n+\t\treturn -EBUSY;\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 = ioctl(fd_, VIDIOC_QUERYCAP, &caps_);\n+\tif (ret < 0) {\n+\t\tLOG(Error) << \"Failed to query device capabilities: \" << strerror(errno);\n+\t\treturn -errno;\n+\t}\n+\n+\tif (!(caps_.hasStreaming())) {\n+\t\tLOG(Error) << \"Device does not support streaming IO\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Checks to see if we have successfully opened a v4l2 video device.\n+ */\n+bool V4L2Device::isOpen() const\n+{\n+\treturn (fd_ != -1);\n+}\n+\n+/**\n+ * \\brief Close the device, releasing any resources acquired by \\a open().\n+ */\n+void V4L2Device::close()\n+{\n+\tif (fd_ < 0)\n+\t\treturn;\n+\n+\t::close(fd_);\n+\tfd_ = -1;\n+}\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v2","1/2"]}