From patchwork Wed Aug 5 10:48:54 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: 9210 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 228A1BD87B for ; Wed, 5 Aug 2020 10:49:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CFDE4605A8; Wed, 5 Aug 2020 12:49:16 +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 555A560555 for ; Wed, 5 Aug 2020 12:49:13 +0200 (CEST) X-Halon-ID: 4c3c6404-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 4c3c6404-d709-11ea-a39b-005056917f90; Wed, 05 Aug 2020 12:49:12 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Wed, 5 Aug 2020 12:48:54 +0200 Message-Id: <20200805104900.2172763-4-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 3/9] libcamera: v4l2_device: Add method to lookup device path 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 method to lookup a V4L2 devices path in sysfs. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- * Changes since v7 - Expand comment - Update error message * Changes since v6 - Use sysfs:: helper. - Do not try to return std::string { nullptr } on error. * Changes since v5 - Expand documentation - Use DeviceEnumerator interface to lookup device path - Include stdlib.h - Use realpath(..., nullptr); ...; free(); * Changes since v3 - s/the device path/the device path in sysfs/ --- include/libcamera/internal/v4l2_device.h | 1 + src/libcamera/v4l2_device.cpp | 31 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index bf643f2ec966bb33..3b605aab343b3b94 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -30,6 +30,7 @@ public: int setControls(ControlList *ctrls); const std::string &deviceNode() const { return deviceNode_; } + std::string devicePath() const; protected: V4L2Device(const std::string &deviceNode); diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index 56ea1ddda2c1425f..65830d41f3cb6e9d 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -9,12 +9,15 @@ #include #include +#include +#include #include #include #include #include #include "libcamera/internal/log.h" +#include "libcamera/internal/sysfs.h" #include "libcamera/internal/utils.h" #include "libcamera/internal/v4l2_controls.h" @@ -350,6 +353,34 @@ int V4L2Device::setControls(ControlList *ctrls) return ret; } +/** + * \brief Retrieve the device path in sysfs + * + * This function returns the sysfs path to the physical device backing the V4L2 + * device. The path is guaranteed to be an absolute path, without any symbolic + * link. + * + * It includes the sysfs mount point prefix + * + * \return The device path in sysfs + */ +std::string V4L2Device::devicePath() const +{ + std::string devicePath = sysfs::charDevPath(deviceNode_) + "/device"; + + char *realPath = realpath(devicePath.c_str(), nullptr); + if (!realPath) { + LOG(V4L2, Fatal) + << "Can not resolve device path for " << devicePath; + return {}; + } + + std::string path{ realPath }; + free(realPath); + + return path; +} + /** * \brief Perform an IOCTL system call on the device node * \param[in] request The IOCTL request code