From patchwork Tue Aug 4 16:13:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 9184 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C9F28BD87A for ; Tue, 4 Aug 2020 16:14:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9CD9F6055A; Tue, 4 Aug 2020 18:14:21 +0200 (CEST) Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9AC8E60547 for ; Tue, 4 Aug 2020 18:14:20 +0200 (CEST) X-Halon-ID: 8c9998c5-d66d-11ea-b48b-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac52a8.dip0.t-ipconnect.de [84.172.82.168]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id 8c9998c5-d66d-11ea-b48b-0050569116f7; Tue, 04 Aug 2020 18:14:19 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Tue, 4 Aug 2020 18:13:50 +0200 Message-Id: <20200804161358.1628962-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200804161358.1628962-1-niklas.soderlund@ragnatech.se> References: <20200804161358.1628962-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 1/9] libcamera: sysfs: Add new namespace to interact with sysfs X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" To support stable and unique camera IDs access to information in sysfs is needed. Add a new libcamera::sysfs namespace to contain such sysfs helpers. As a first helper add a method to lookup a character device sysfs path. Suggested-by: Laurent Pinchart Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- include/libcamera/internal/meson.build | 1 + include/libcamera/internal/sysfs.h | 22 ++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/sysfs.cpp | 48 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 include/libcamera/internal/sysfs.h create mode 100644 src/libcamera/sysfs.cpp diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index d868eff47f920da0..150103388fdb219d 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -25,6 +25,7 @@ libcamera_internal_headers = files([ 'process.h', 'pub_key.h', 'semaphore.h', + 'sysfs.h', 'thread.h', 'utils.h', 'v4l2_controls.h', diff --git a/include/libcamera/internal/sysfs.h b/include/libcamera/internal/sysfs.h new file mode 100644 index 0000000000000000..72f436205d8d30e8 --- /dev/null +++ b/include/libcamera/internal/sysfs.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * sysfs.h - Miscellaneous utility functions to access sysfs + */ +#ifndef __LIBCAMERA_INTERNAL_SYSFS_H__ +#define __LIBCAMERA_INTERNAL_SYSFS_H__ + +#include + +namespace libcamera { + +namespace sysfs { + +std::string charDevPath(const std::string &devicePath); + +} /* namespace sysfs */ + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_INTERNAL_SYSFS_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 3aad4386ffc296db..bada45bcb11e9e88 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -41,6 +41,7 @@ libcamera_sources = files([ 'semaphore.cpp', 'signal.cpp', 'stream.cpp', + 'sysfs.cpp', 'thread.cpp', 'timer.cpp', 'utils.cpp', diff --git a/src/libcamera/sysfs.cpp b/src/libcamera/sysfs.cpp new file mode 100644 index 0000000000000000..3b2920663e9c3bcc --- /dev/null +++ b/src/libcamera/sysfs.cpp @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * sysfs.cpp - Miscellaneous utility functions to access sysfs + */ + +#include "libcamera/internal/sysfs.h" + +#include +#include + +#include "libcamera/internal/log.h" + +/** + * \file sysfs.h + * \brief Miscellaneous utility functions to access sysfs + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(SYSFS); + +namespace sysfs { + +/** + * \brief Retrive the sysfs path for a characther device + * \param[in] devicePath Path to charachter device + * \return The sysfs path on success or empty string on failure + */ +std::string charDevPath(const std::string &devicePath) +{ + struct stat st; + int ret = stat(devicePath.c_str(), &st); + if (ret < 0) { + LOG(SYSFS, Error) + << "Unable to stat '" << devicePath << "'"; + return {}; + } + + std::ostringstream dev("/sys/dev/char/", std::ios_base::ate); + dev << major(st.st_rdev) << ":" << minor(st.st_rdev); + return dev.str(); +} + +} /* namespace sysfs */ + +} /* namespace libcamera */