[libcamera-devel] [PATCH v8 1/9] libcamera: sysfs: Add helper to lookup sysfs path of a character device
Niklas Söderlund
niklas.soderlund at ragnatech.se
Wed Aug 5 12:48:52 CEST 2020
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 <laurent.pinchart at ideasonboard.com>
Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>
---
* 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 <string>
+
+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 <sstream>
+#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 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 */
--
2.28.0
More information about the libcamera-devel
mailing list