[libcamera-devel,v2,01/10] libcamera: pipeline: Add a platform configuration file helper
diff mbox series

Message ID 20221129134534.2933-2-naush@raspberrypi.com
State Superseded
Headers show
Series
  • Raspberry Pi: Platform configuration and buffer allocation improvements
Related show

Commit Message

Naushir Patuck Nov. 29, 2022, 1:45 p.m. UTC
Add a new helper function PipelineHandler::configurationFile() that returns
the full path of a named configuration file. This configuration file may be read
by pipeline handlers for platform specific configuration parameters on
initialisation.

The mechanism for searching for the configuration file is similar to the IPA
configuration file:

- In the source tree if libcamera is not installed
- Otherwise in standard system locations (etc and share directories).

When stored in the source tree, configuration files shall be located in a 'data'
subdirectory of their respective pipeline handler directory.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/internal/pipeline_handler.h |  3 +
 src/libcamera/pipeline_handler.cpp            | 60 +++++++++++++++++++
 2 files changed, 63 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h
index ec4f662d7399..cd27bcf944d6 100644
--- a/include/libcamera/internal/pipeline_handler.h
+++ b/include/libcamera/internal/pipeline_handler.h
@@ -76,6 +76,9 @@  protected:
 
 	virtual void releaseDevice(Camera *camera);
 
+	std::string configurationFile(const std::string &subdir,
+				      const std::string &name) const;
+
 	CameraManager *manager_;
 
 private:
diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp
index cfade4908118..b4579671ba5a 100644
--- a/src/libcamera/pipeline_handler.cpp
+++ b/src/libcamera/pipeline_handler.cpp
@@ -8,6 +8,7 @@ 
 #include "libcamera/internal/pipeline_handler.h"
 
 #include <chrono>
+#include <sys/stat.h>
 #include <sys/sysmacros.h>
 
 #include <libcamera/base/log.h>
@@ -638,6 +639,65 @@  void PipelineHandler::disconnect()
 	}
 }
 
+/**
+ * \brief Retrieve the absolute path to a platform configuration file
+ * \param[in] subdir The pipeline handler specific subdirectory name
+ * \param[in] name The configuration file name
+ *
+ * This function locates a named platform configuration file and returns
+ * its absolute path to the pipeline handler. It searches the following
+ * directories, in order:
+ *
+ * - If libcamera is not installed, the src/libcamera/pipeline/<subdir>/data/
+ *   directory within the source tree ; otherwise
+ * - The system data (share/libcamera/pipeline/<subdir>) directory.
+ *
+ * The system directories are not searched if libcamera is not installed.
+ *
+ * \return The full path to the pipeline handler configuration file, or an empty
+ * string if no configuration file can be found
+ */
+std::string PipelineHandler::configurationFile(const std::string &subdir,
+					       const std::string &name) const
+{
+	std::string confPath;
+	struct stat statbuf;
+	int ret;
+
+	std::string root = utils::libcameraSourcePath();
+	if (!root.empty()) {
+		/*
+		 * When libcamera is used before it is installed, load
+		 * configuration files from the source directory. The
+		 * configuration files are then located in the 'data'
+		 * subdirectory of the corresponding pipeline handler.
+		 */
+		std::string confDir = root + "src/libcamera/pipeline/";
+		confPath = confDir + subdir + "/data/" + name;
+
+		LOG(Pipeline, Info)
+			<< "libcamera is not installed. Loading platform configuration file from '"
+			<< confPath << "'";
+
+		ret = stat(confPath.c_str(), &statbuf);
+		if (ret == 0 && (statbuf.st_mode & S_IFMT) == S_IFREG)
+			return confPath;
+	} else {
+		/* Else look in the system locations. */
+		confPath = std::string(LIBCAMERA_DATA_DIR)
+				+ "/pipeline/" + subdir + '/' + name;
+		ret = stat(confPath.c_str(), &statbuf);
+		if (ret == 0 && (statbuf.st_mode & S_IFMT) == S_IFREG)
+			return confPath;
+	}
+
+	LOG(Pipeline, Error)
+		<< "Configuration file '" << confPath
+		<< "' not found for pipeline handler '" << PipelineHandler::name() << "'";
+
+	return std::string();
+}
+
 /**
  * \var PipelineHandler::manager_
  * \brief The Camera manager associated with the pipeline handler