1313
1414#include < cmath>
1515#include < fcntl.h>
16+ #include < fstream>
1617#include < stdlib.h>
1718
1819#include < sys/ioctl.h>
2526#include < libcamera/orientation.h>
2627
2728#include " logging_tools.h"
29+ #include " gs_config.h"
2830
2931
3032unsigned int RPiCamApp::verbosity = 1 ;
@@ -53,34 +55,52 @@ static libcamera::PixelFormat mode_to_pixel_format(Mode const &mode)
5355 return libcamera::formats::SBGGR12_CSI2P;
5456}
5557
56- static void set_pipeline_configuration (Platform platform)
58+ static bool set_pipeline_configuration (Platform platform)
5759{
5860 GS_LOG_TRACE_MSG (trace, " set_pipeline_configuration called for platform: " + std::to_string ((int )platform));
5961
62+ bool found_valid_file = false ;
63+ std::string configure_file_name;
64+
6065 // Respect any pre-existing value in the environment variable.
61- char const *existing_config = getenv (" LIBCAMERA_RPI_CONFIG_FILE" );
62- if (existing_config && existing_config[0 ])
63- return ;
66+ std::string existing_config = golf_sim::GolfSimConfiguration::safe_getenv (" LIBCAMERA_RPI_CONFIG_FILE" );
67+ if (!existing_config.empty ()) {
68+ found_valid_file = true ;
69+ configure_file_name = existing_config;
70+ }
6471
65- // Otherwise point it at whichever of these we find first (if any) for the given platform.
66- static const std::vector<std::pair<Platform, std::string>> config_files = {
67- { Platform::VC4, " /usr/local/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml" },
68- { Platform::VC4, " /usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml" },
69- { Platform::PISP, " /usr/local/share/libcamera/pipeline/rpi/pisp/rpi_apps.yaml" },
70- { Platform::PISP, " /usr/share/libcamera/pipeline/rpi/pisp/rpi_apps.yaml" },
71- };
72+ if (!found_valid_file) {
73+ // Otherwise point it at whichever of these we find first (if any) for the given platform.
74+ static const std::vector<std::pair<Platform, std::string>> config_files = {
75+ { Platform::VC4, " /usr/local/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml" },
76+ { Platform::VC4, " /usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml" },
77+ { Platform::PISP, " /usr/local/share/libcamera/pipeline/rpi/pisp/rpi_apps.yaml" },
78+ { Platform::PISP, " /usr/share/libcamera/pipeline/rpi/pisp/rpi_apps.yaml" },
79+ };
7280
73- for (auto &config_file : config_files)
74- {
75- struct stat info;
76- if (config_file.first == platform && stat (config_file.second .c_str (), &info) == 0 )
81+ for (auto & config_file : config_files)
7782 {
78- GS_LOG_TRACE_MSG (trace, " set_pipeline_configuration setting yaml pipeline file to : " + config_file.second );
83+ struct stat info;
84+ if (config_file.first == platform && stat (config_file.second .c_str (), &info) == 0 )
85+ {
86+ GS_LOG_TRACE_MSG (trace, " set_pipeline_configuration setting yaml pipeline file to : " + config_file.second );
7987
80- setenv (" LIBCAMERA_RPI_CONFIG_FILE" , config_file.second .c_str (), 1 );
81- break ;
88+ configure_file_name = config_file.second ;
89+ setenv (" LIBCAMERA_RPI_CONFIG_FILE" , config_file.second .c_str (), 1 );
90+ break ;
91+ }
8292 }
8393 }
94+
95+ // Make sure the environment actually has the file available
96+ std::ifstream file (configure_file_name);
97+ if (!file.good ()) {
98+ GS_LOG_MSG (error, " set_pipeline_configuration failed to open file: " + configure_file_name);
99+ GS_LOG_MSG (error, " If necessary, find the example.yaml file, copy it to the expected file name, and ensure the timeout value is set correctly." );
100+ return false ;
101+ }
102+
103+ return true ;
84104}
85105
86106RPiCamApp::RPiCamApp (std::unique_ptr<Options> opts)
@@ -105,7 +125,10 @@ RPiCamApp::RPiCamApp(std::unique_ptr<Options> opts)
105125 exit (-1 );
106126 }
107127
108- set_pipeline_configuration (platform);
128+ if (!set_pipeline_configuration (platform)) {
129+ fprintf (stderr, " ERROR: failed to set the libcamera pipeline configuration. Exiting Program.\n " );
130+ exit (-1 );
131+ }
109132}
110133
111134RPiCamApp::~RPiCamApp ()
0 commit comments