From patchwork Fri Dec 21 12:37:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 74 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A393F60B2C for ; Fri, 21 Dec 2018 13:37:29 +0100 (CET) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2255E565; Fri, 21 Dec 2018 13:37:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545395849; bh=8xEsQc8ihblPTAYPmXBim7y8o7afV9hgZiRsnkMRTlk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vzlxinuxlmnVU1P3OL2AUIK3dmztcqCtsnPPrdzEkPPZ2LqCp4g/1WRrvubLZmEHr 9DsoYKTy33kzvdtnuT1/K367QcpeKcfF23HoqwfwfobyWrvKdoOLjtvaAti93dWLJW 2qP0ElFQnYiT5Nfa9Ju/F2oc3Jt6IHQWIlhxzfb8= From: Kieran Bingham To: LibCamera Devel Date: Fri, 21 Dec 2018 12:37:23 +0000 Message-Id: <20181221123724.27290-2-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 1/2] test: libtest: Add path_exists helper X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Dec 2018 12:37:36 -0000 We will frequently need to validate the existance of a target for testing. Add a helper to wrap this. This could be extended or replaced to ensure we have access to the path as well as it existing. Signed-off-by: Kieran Bingham --- test/libtest/test.cpp | 11 +++++++++++ test/libtest/test.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/test/libtest/test.cpp b/test/libtest/test.cpp index 9d537ea08698..ff0c9fc02e15 100644 --- a/test/libtest/test.cpp +++ b/test/libtest/test.cpp @@ -5,6 +5,7 @@ * test.cpp - libcamera test base class */ +#include #include "test.h" Test::Test() @@ -29,3 +30,13 @@ int Test::execute() return ret; } + +bool Test::path_exists(const char *p) +{ + struct stat sb; + + if (stat(p, &sb) == 0) + return true; + + return false; +} diff --git a/test/libtest/test.h b/test/libtest/test.h index 18b430f428c7..f21cc15e8743 100644 --- a/test/libtest/test.h +++ b/test/libtest/test.h @@ -21,6 +21,8 @@ public: int execute(); + bool path_exists(const char *p); + protected: virtual int init() { return 0; } virtual int run() = 0; From patchwork Fri Dec 21 12:37:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 75 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 03B6760B2D for ; Fri, 21 Dec 2018 13:37:36 +0100 (CET) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6E7DE597; Fri, 21 Dec 2018 13:37:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545395849; bh=54Fyp/sWlGLaQ1zL6H/oIlsZo8xZ9W/dYCEUCOIB8Gg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DXvmFPWBQrJOS24VErnzuEL0fTNuxlusbpQvi/Eq+Z02NgOc9ankBS6QnEKKeqhJE YN+JC9GwbkT0lSOkyDUeRWHYz/jhtYJCdmy8yd70M/WtAP1EbCNX/Eg5oBidVswXX7 3O9ltZdbecKxXUlFeRQi898M2VFZMClwFJUXJ3G4= From: Kieran Bingham To: LibCamera Devel 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Dec 2018 12:37:36 -0000 Provide a helper V4L2 device object capable of interacting with the V4L2 Linux Kernel APIs. A test suite is added at test/v4l2_device/ to utilise and validate the code base. Signed-off-by: Kieran Bingham --- src/libcamera/include/v4l2_device.h | 36 +++++++ src/libcamera/meson.build | 2 + src/libcamera/v4l2_device.cpp | 137 ++++++++++++++++++++++++++ test/meson.build | 2 + test/v4l2_device/double_open.cpp | 32 ++++++ test/v4l2_device/meson.build | 12 +++ test/v4l2_device/v4l2_device_test.cpp | 36 +++++++ test/v4l2_device/v4l2_device_test.h | 31 ++++++ 8 files changed, 288 insertions(+) create mode 100644 src/libcamera/include/v4l2_device.h create mode 100644 src/libcamera/v4l2_device.cpp create mode 100644 test/v4l2_device/double_open.cpp create mode 100644 test/v4l2_device/meson.build create mode 100644 test/v4l2_device/v4l2_device_test.cpp create mode 100644 test/v4l2_device/v4l2_device_test.h diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h new file mode 100644 index 000000000000..619d932d3c82 --- /dev/null +++ b/src/libcamera/include/v4l2_device.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * v4l2_device.h - V4L2 Device API Abstractions + */ +#ifndef __LIBCAMERA_V4L2_DEVICE_H__ +#define __LIBCAMERA_V4L2_DEVICE_H__ + +#include + +#include + +namespace libcamera { + +class V4L2Device +{ +public: + V4L2Device(const std::string &); + ~V4L2Device(); + + bool isOpen(); + int open(); + void close(); + + int queryCap(); + +private: + std::string device_; + int fd_; + int capabilities_; +}; + +} + +#endif /* __LIBCAMERA_V4L2_DEVICE_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index f632eb5dd779..bbaf9a05ec2c 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -1,11 +1,13 @@ libcamera_sources = files([ 'log.cpp', 'main.cpp', + 'v4l2_device.cpp', ]) libcamera_headers = files([ 'include/log.h', 'include/utils.h', + 'include/v4l2_device.h', ]) libcamera_internal_includes = include_directories('include') diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp new file mode 100644 index 000000000000..eea2514f343c --- /dev/null +++ b/src/libcamera/v4l2_device.cpp @@ -0,0 +1,137 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * v4l2_device.cpp - V4L2 Device API + */ + +#include +#include +#include + +#include +#include + +#include "log.h" +#include "v4l2_device.h" + +/** + * \file v4l2_device.cpp + * \brief V4L2 Device API + */ +namespace libcamera { + +/** + * \class V4L2Device + * \brief V4L2Device object and API. + * + * The V4L2 Device API class models a single instance of a v4l2 device node. + */ + +/** + * \fn V4L2Device::V4L2Device(device) + * + * Default constructor for a V4L2Device object. The \a device specifies a + * string to representation of the device object. + */ +V4L2Device::V4L2Device(const std::string &device) +{ + device_ = device; + fd_ = -1; + capabilities_ = 0; + + LOG(Debug) << "V4L2Device Constructed for " << device_; +} + +V4L2Device::~V4L2Device() +{ + close(); + + LOG(Debug) << "V4L2Device Destroyed"; +} + +/** + * \fn V4L2Device::isOpen(()) + * + * Checks to see if we have successfully opened a v4l2 video device. + */ +bool V4L2Device::isOpen() +{ + return (fd_ != -1); +} + +/** + * \fn V4L2Device::open() + * + * Opens a v4l2 device and queries properties from the device. + */ +int V4L2Device::open() +{ + int ret; + + if (isOpen()) { + LOG(Error) << "Device already open"; + return -EBADF; + } + + fd_ = ::open(device_.c_str(), O_RDWR); + if (fd_ < 0) { + LOG(Error) << "Failed to open V4L2 device " << device_ + << " : " << strerror(errno); + return -errno; + } + + ret = queryCap(); + if (ret) + return ret; + + if (!(capabilities_ & V4L2_CAP_STREAMING)) { + LOG(Error) << "Device does not support streaming IO"; + return -EINVAL; + } + + return 0; +} + +/** + * \fn V4L2Device::close() + * + * Close a v4l2 device handle. + */ +void V4L2Device::close() +{ + if (fd_ < 0) + return; + + ::close(fd_); + fd_ = -1; +} + +/** + * \fn V4L2Device::queryCap() + * + * Obtains the capabilities information from the V4L2 Device. + */ +int V4L2Device::queryCap() +{ + struct v4l2_capability cap = {}; + int ret; + + if (!isOpen()) { + LOG(Error) << "Attempt to query unopened device"; + return -EBADF; + } + + ret = ioctl(fd_, VIDIOC_QUERYCAP, &cap); + if (ret < 0) { + LOG(Error) << "Failed to query device capabilities"; + return -1; + } + + capabilities_ = cap.capabilities & V4L2_CAP_DEVICE_CAPS + ? cap.device_caps : cap.capabilities; + + return 0; +} + +} /* namespace libcamera */ diff --git a/test/meson.build b/test/meson.build index 754527324c7d..2bc76c289eea 100644 --- a/test/meson.build +++ b/test/meson.build @@ -12,6 +12,8 @@ test_includes_internal = [ libcamera_internal_includes, ] +subdir('v4l2_device') + public_tests = [ [ 'test_init', 'init.cpp' ], ] diff --git a/test/v4l2_device/double_open.cpp b/test/v4l2_device/double_open.cpp new file mode 100644 index 000000000000..1b7c7bfe14b8 --- /dev/null +++ b/test/v4l2_device/double_open.cpp @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include "v4l2_device_test.h" + +class DoubleOpen : public V4L2DeviceTest +{ +protected: + int run() + { + int ret; + + /* + * Expect failure: The device has already been opened by the + * V4L2DeviceTest base class + */ + ret = dev->open(); + if (!ret) { + fprintf(stderr, "Double open erroneously succeeded\n"); + dev->close(); + return TEST_FAIL; + } + + return TEST_PASS; + } +}; + +TEST_REGISTER(DoubleOpen); diff --git a/test/v4l2_device/meson.build b/test/v4l2_device/meson.build new file mode 100644 index 000000000000..41675a303498 --- /dev/null +++ b/test/v4l2_device/meson.build @@ -0,0 +1,12 @@ +# Tests are listed in order of complexity. +# They are not alphabetically sorted. +v4l2_device_tests = [ + [ 'double_open', 'double_open.cpp' ], +] + +foreach t : v4l2_device_tests + exe = executable(t[0], [t[1], 'v4l2_device_test.cpp'], + link_with : test_libraries, + include_directories : test_includes_internal) + test(t[0], exe, suite: 'v4l2_device', is_parallel: false) +endforeach diff --git a/test/v4l2_device/v4l2_device_test.cpp b/test/v4l2_device/v4l2_device_test.cpp new file mode 100644 index 000000000000..ae317a9519c5 --- /dev/null +++ b/test/v4l2_device/v4l2_device_test.cpp @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include "v4l2_device_test.h" + +using namespace libcamera; + +V4L2DeviceTest::V4L2DeviceTest() +{ + dev = nullptr; +} + +int V4L2DeviceTest::init() +{ + const char *device = "/dev/video0"; + + /* Validate the device node exists. */ + if (!path_exists(device)) + return TEST_SKIP; + + dev = new V4L2Device(device); + if (!dev) + return TEST_FAIL; + + return dev->open(); +} + +void V4L2DeviceTest::cleanup() +{ + if (dev) + delete dev; +}; diff --git a/test/v4l2_device/v4l2_device_test.h b/test/v4l2_device/v4l2_device_test.h new file mode 100644 index 000000000000..4374ddc7e932 --- /dev/null +++ b/test/v4l2_device/v4l2_device_test.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * vl42device_test.h - libcamera v4l2device test base class + */ + +#include "test.h" +#include "v4l2_device.h" + +#ifndef __LIBCAMERA_V4L2_DEVICE_TEST_H_ +#define __LIBCAMERA_V4L2_DEVICE_TEST_H_ + +using namespace libcamera; + +class V4L2DeviceTest : public Test +{ +public: + V4L2DeviceTest(); + virtual ~V4L2DeviceTest() {}; + +protected: + int init(); + void cleanup(); + + virtual int run() = 0; + + V4L2Device *dev; +}; + +#endif /* __LIBCAMERA_V4L2_DEVICE_TEST_H_ */