From patchwork Wed Aug 5 10:48:52 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: 9208 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 BDBC2BD86F for ; Wed, 5 Aug 2020 10:49:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 809F460595; Wed, 5 Aug 2020 12:49:13 +0200 (CEST) Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D4BF660492 for ; Wed, 5 Aug 2020 12:49:11 +0200 (CEST) X-Halon-ID: 4b0c393e-d709-11ea-a39b-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac52a8.dip0.t-ipconnect.de [84.172.82.168]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 4b0c393e-d709-11ea-a39b-005056917f90; Wed, 05 Aug 2020 12:49:10 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Wed, 5 Aug 2020 12:48:52 +0200 Message-Id: <20200805104900.2172763-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200805104900.2172763-1-niklas.soderlund@ragnatech.se> References: <20200805104900.2172763-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 1/9] libcamera: sysfs: Add helper to lookup sysfs path of a character device 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" Add a helper function to lookup the sysfs path of a character device. Store the function in a new libcamera::sysfs namespace as there is not class to host it. Suggested-by: Laurent Pinchart Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- * Changes since v7 - Rename parameter to deviceNode - Include sstream - Rename LOG category to SysFs - Update documentation - Print errno on failure ` --- include/libcamera/internal/meson.build | 1 + include/libcamera/internal/sysfs.h | 22 +++++++++++ src/libcamera/meson.build | 1 + src/libcamera/sysfs.cpp | 52 ++++++++++++++++++++++++++ 4 files changed, 76 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..247a376ab7e0e8ee --- /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 &deviceNode); + +} /* 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..398df2c2be114908 --- /dev/null +++ b/src/libcamera/sysfs.cpp @@ -0,0 +1,52 @@ +/* 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 + +#include "libcamera/internal/log.h" + +/** + * \file sysfs.h + * \brief Miscellaneous utility functions to access sysfs + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(SysFs); + +namespace sysfs { + +/** + * \brief Retrieve the sysfs path for a character device + * \param[in] deviceNode Path to character device node + * \return The sysfs path on success or an empty string on failure + */ +std::string charDevPath(const std::string &deviceNode) +{ + struct stat st; + int ret = stat(deviceNode.c_str(), &st); + if (ret < 0) { + ret = -errno; + LOG(SysFs, Error) + << "Unable to stat '" << deviceNode << "': " + << strerror(-ret); + 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 */