[libcamera-devel] [PATCH v7 1/9] libcamera: sysfs: Add new namespace to interact with sysfs

Niklas Söderlund niklas.soderlund at ragnatech.se
Tue Aug 4 18:13:50 CEST 2020


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 <laurent.pinchart at ideasonboard.com>
Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
---
 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 <string>
+
+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 <sys/stat.h>
+#include <sys/sysmacros.h>
+
+#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 */
-- 
2.28.0



More information about the libcamera-devel mailing list