blob: 3ec0c8a700e1c4900002454fe05e9c5f7d26c217 [file] [log] [blame]
Matthew Barth11547c92020-05-05 10:34:27 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "sdbusplus.hpp"
19
20#include <nlohmann/json.hpp>
21#include <phosphor-logging/log.hpp>
22#include <sdbusplus/bus.hpp>
23#include <sdeventplus/source/signal.hpp>
24
25#include <filesystem>
Patrick Williamsfbf47032023-07-17 12:27:34 -050026#include <format>
Matthew Barth11547c92020-05-05 10:34:27 -050027#include <fstream>
28
29namespace phosphor::fan
30{
31
32namespace fs = std::filesystem;
33using json = nlohmann::json;
34using namespace phosphor::logging;
35
36constexpr auto confOverridePath = "/etc/phosphor-fan-presence";
37constexpr auto confBasePath = "/usr/share/phosphor-fan-presence";
Matthew Barthb370ab32021-06-10 14:38:02 -050038constexpr auto confCompatServ = "xyz.openbmc_project.EntityManager";
Matthew Barthfcbdc0e2020-10-28 14:11:07 -050039constexpr auto confCompatIntf =
40 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
41constexpr auto confCompatProp = "Names";
Matthew Barth11547c92020-05-05 10:34:27 -050042
Matthew Barth6eb603e2021-09-01 09:01:57 -050043/**
44 * @class NoConfigFound - A no JSON configuration found exception
45 *
46 * A no JSON configuration found exception that is used to denote that a JSON
47 * configuration has not been found yet.
48 */
49class NoConfigFound : public std::runtime_error
50{
51 public:
52 NoConfigFound() = delete;
53 NoConfigFound(const NoConfigFound&) = delete;
54 NoConfigFound(NoConfigFound&&) = delete;
55 NoConfigFound& operator=(const NoConfigFound&) = delete;
56 NoConfigFound& operator=(NoConfigFound&&) = delete;
57 ~NoConfigFound() = default;
58
59 /**
60 * @brief No JSON configuration found exception object
61 *
62 * When attempting to find the JSON configuration file(s), a NoConfigFound
63 * exception can be thrown to denote that at that time finding/loading the
64 * JSON configuration file(s) for a fan application failed. Details on what
65 * application and JSON configuration file that failed to be found will be
66 * logged resulting in the application being terminated.
67 *
68 * @param[in] details - Additional details
69 */
70 NoConfigFound(const std::string& appName, const std::string& fileName) :
Patrick Williamsfbf47032023-07-17 12:27:34 -050071 std::runtime_error(std::format("JSON configuration not found [Could "
Matthew Barth6eb603e2021-09-01 09:01:57 -050072 "not find fan {} conf file {}]",
73 appName, fileName)
74 .c_str())
75 {}
76};
77
Matthew Barth11547c92020-05-05 10:34:27 -050078class JsonConfig
79{
80 public:
Matt Spinler59850df2021-01-06 16:56:06 -060081 /**
Matthew Barthd80c8752021-06-01 14:46:02 -050082 * @brief Get the object paths with the compatible interface
83 *
84 * Retrieve all the object paths implementing the compatible interface for
85 * configuration file loading.
86 */
87 static auto& getCompatObjPaths() __attribute__((pure))
88 {
89 static auto paths = util::SDBusPlus::getSubTreePathsRaw(
90 util::SDBusPlus::getBus(), "/", confCompatIntf, 0);
91 return paths;
92 }
93
94 /**
Matt Spinler59850df2021-01-06 16:56:06 -060095 * @brief Constructor
96 *
Matthew Barthb370ab32021-06-10 14:38:02 -050097 * Attempts to set the list of compatible values from the compatible
98 * interface and call the fan app's function to load its config file(s). If
99 * the compatible interface is not found, it subscribes to the
100 * interfacesAdded signal for that interface on the compatible service
101 * defined above.
102 *
103 * @param[in] func - Fan app function to call to load its config file(s)
104 */
105 JsonConfig(std::function<void()> func) : _loadFunc(func)
106 {
Matthew Barth8d1193c2021-09-14 10:36:06 -0500107 std::vector<std::string> compatObjPaths;
108
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600109 _match = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barthb370ab32021-06-10 14:38:02 -0500110 util::SDBusPlus::getBus(),
111 sdbusplus::bus::match::rules::interfacesAdded() +
112 sdbusplus::bus::match::rules::sender(confCompatServ),
113 std::bind(&JsonConfig::compatIntfAdded, this,
114 std::placeholders::_1));
Matthew Barth1689cb62021-08-27 12:34:36 -0500115
Matthew Barth8d1193c2021-09-14 10:36:06 -0500116 try
117 {
118 compatObjPaths = getCompatObjPaths();
119 }
120 catch (const util::DBusMethodError&)
121 {
122 // Compatible interface does not exist on any dbus object yet
123 }
124
Matthew Barth1689cb62021-08-27 12:34:36 -0500125 if (!compatObjPaths.empty())
Matthew Barthb370ab32021-06-10 14:38:02 -0500126 {
Matthew Barth1689cb62021-08-27 12:34:36 -0500127 for (auto& path : compatObjPaths)
Matthew Barthb370ab32021-06-10 14:38:02 -0500128 {
Matthew Barth1689cb62021-08-27 12:34:36 -0500129 try
Matthew Barthb370ab32021-06-10 14:38:02 -0500130 {
Matthew Barth1689cb62021-08-27 12:34:36 -0500131 // Retrieve json config compatible relative path
132 // locations (last one found will be what's used if more
133 // than one dbus object implementing the comptaible
134 // interface exists).
135 _confCompatValues =
136 util::SDBusPlus::getProperty<std::vector<std::string>>(
137 util::SDBusPlus::getBus(), path, confCompatIntf,
138 confCompatProp);
Matthew Barthb370ab32021-06-10 14:38:02 -0500139 }
Matthew Barth1689cb62021-08-27 12:34:36 -0500140 catch (const util::DBusError&)
141 {
142 // Compatible property unavailable on this dbus object
143 // path's compatible interface, ignore
144 }
145 }
146 _loadFunc();
Matthew Barth1689cb62021-08-27 12:34:36 -0500147 }
148 else
149 {
150 // Check if required config(s) are found not needing the
151 // compatible interface, otherwise this is intended to catch the
152 // exception thrown by the getConfFile function when the
153 // required config file was not found. This would then result in
154 // waiting for the compatible interfacesAdded signal
155 try
156 {
Matthew Barthb370ab32021-06-10 14:38:02 -0500157 _loadFunc();
Matthew Barthb370ab32021-06-10 14:38:02 -0500158 }
Matthew Barth6eb603e2021-09-01 09:01:57 -0500159 catch (const NoConfigFound&)
Matthew Barthb370ab32021-06-10 14:38:02 -0500160 {
Matthew Barth1689cb62021-08-27 12:34:36 -0500161 // Wait for compatible interfacesAdded signal
Matthew Barthb370ab32021-06-10 14:38:02 -0500162 }
163 }
Matthew Barthb370ab32021-06-10 14:38:02 -0500164 }
165
166 /**
Matthew Barthb370ab32021-06-10 14:38:02 -0500167 * @brief InterfacesAdded callback function for the compatible interface.
168 *
169 * @param[in] msg - The D-Bus message contents
170 *
171 * If the compatible interface is found, it uses the compatible property on
172 * the interface to set the list of compatible values to be used when
173 * attempting to get a configuration file. Once the list of compatible
174 * values has been updated, it calls the load function.
175 */
Patrick Williamscb356d42022-07-22 19:26:53 -0500176 void compatIntfAdded(sdbusplus::message_t& msg)
Matthew Barthb370ab32021-06-10 14:38:02 -0500177 {
178 sdbusplus::message::object_path op;
179 std::map<std::string,
180 std::map<std::string, std::variant<std::vector<std::string>>>>
181 intfProps;
182
183 msg.read(op, intfProps);
184
185 if (intfProps.find(confCompatIntf) == intfProps.end())
186 {
187 return;
188 }
189
190 const auto& props = intfProps.at(confCompatIntf);
191 // Only one dbus object with the compatible interface is used at a time
192 _confCompatValues =
193 std::get<std::vector<std::string>>(props.at(confCompatProp));
194 _loadFunc();
195 }
196
197 /**
Matthew Barth11547c92020-05-05 10:34:27 -0500198 * Get the json configuration file. The first location found to contain
199 * the json config file for the given fan application is used from the
200 * following locations in order.
201 * 1.) From the confOverridePath location
Matthew Barthb67089b2021-06-10 14:32:00 -0500202 * 2.) From the default confBasePath location
203 * 3.) From config file found using an entry from a list obtained from an
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500204 * interface's property as a relative path extension on the base path where:
205 * interface = Interface set in confCompatIntf with the property
206 * property = Property set in confCompatProp containing a list of
207 * subdirectories in priority order to find a config
Matthew Barth11547c92020-05-05 10:34:27 -0500208 *
209 * @brief Get the configuration file to be used
210 *
Matthew Barth11547c92020-05-05 10:34:27 -0500211 * @param[in] appName - The phosphor-fan-presence application name
212 * @param[in] fileName - Application's configuration file's name
Matthew Barthb5991022020-08-05 11:08:50 -0500213 * @param[in] isOptional - Config file is optional, default to 'false'
Matthew Barth11547c92020-05-05 10:34:27 -0500214 *
215 * @return filesystem path
216 * The filesystem path to the configuration file to use
217 */
Mike Capps808d7fe2022-06-13 10:12:16 -0400218 static const fs::path getConfFile(const std::string& appName,
Matthew Barthb5991022020-08-05 11:08:50 -0500219 const std::string& fileName,
220 bool isOptional = false)
Matthew Barth11547c92020-05-05 10:34:27 -0500221 {
222 // Check override location
223 fs::path confFile = fs::path{confOverridePath} / appName / fileName;
224 if (fs::exists(confFile))
225 {
226 return confFile;
227 }
228
Matt Spinler59850df2021-01-06 16:56:06 -0600229 // If the default file is there, use it
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500230 confFile = fs::path{confBasePath} / appName / fileName;
Matt Spinler59850df2021-01-06 16:56:06 -0600231 if (fs::exists(confFile))
232 {
233 return confFile;
234 }
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500235
Matthew Barthb67089b2021-06-10 14:32:00 -0500236 // Look for a config file at each entry relative to the base
237 // path and use the first one found
Patrick Williams61b73292023-05-10 07:50:12 -0500238 auto it =
239 std::find_if(_confCompatValues.begin(), _confCompatValues.end(),
240 [&confFile, &appName, &fileName](const auto& value) {
241 confFile = fs::path{confBasePath} / appName / value / fileName;
242 return fs::exists(confFile);
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500243 });
Matthew Barthb67089b2021-06-10 14:32:00 -0500244 if (it == _confCompatValues.end())
245 {
246 confFile.clear();
247 }
248
Matt Spinler59850df2021-01-06 16:56:06 -0600249 if (confFile.empty() && !isOptional)
Matthew Barth11547c92020-05-05 10:34:27 -0500250 {
Matthew Barth6eb603e2021-09-01 09:01:57 -0500251 throw NoConfigFound(appName, fileName);
Matthew Barthb5991022020-08-05 11:08:50 -0500252 }
Matthew Barth11547c92020-05-05 10:34:27 -0500253
254 return confFile;
255 }
256
257 /**
258 * @brief Load the JSON config file
259 *
260 * @param[in] confFile - File system path of the configuration file to load
261 *
262 * @return Parsed JSON object
263 * The parsed JSON configuration file object
264 */
265 static const json load(const fs::path& confFile)
266 {
267 std::ifstream file;
268 json jsonConf;
269
Matthew Barthb5991022020-08-05 11:08:50 -0500270 if (!confFile.empty() && fs::exists(confFile))
Matthew Barth11547c92020-05-05 10:34:27 -0500271 {
Matthew Barth1826c732020-08-28 08:40:59 -0500272 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500273 std::format("Loading configuration from {}", confFile.string())
Matthew Barth1826c732020-08-28 08:40:59 -0500274 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500275 file.open(confFile);
276 try
277 {
Matthew Barthde72d5d2021-07-16 10:39:42 -0500278 // Enable ignoring `//` or `/* */` comments
279 jsonConf = json::parse(file, nullptr, true, true);
Matthew Barth11547c92020-05-05 10:34:27 -0500280 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500281 catch (const std::exception& e)
Matthew Barth11547c92020-05-05 10:34:27 -0500282 {
Matthew Barth1826c732020-08-28 08:40:59 -0500283 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500284 std::format(
Matthew Barth1826c732020-08-28 08:40:59 -0500285 "Failed to parse JSON config file: {}, error: {}",
286 confFile.string(), e.what())
287 .c_str());
288 throw std::runtime_error(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500289 std::format(
Matthew Barth1826c732020-08-28 08:40:59 -0500290 "Failed to parse JSON config file: {}, error: {}",
291 confFile.string(), e.what())
292 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500293 }
294 }
295 else
296 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500297 log<level::ERR>(std::format("Unable to open JSON config file: {}",
Matthew Barth1826c732020-08-28 08:40:59 -0500298 confFile.string())
299 .c_str());
300 throw std::runtime_error(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500301 std::format("Unable to open JSON config file: {}",
Matthew Barth1826c732020-08-28 08:40:59 -0500302 confFile.string())
303 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500304 }
305
306 return jsonConf;
307 }
Matt Spinler59850df2021-01-06 16:56:06 -0600308
Matt Spinlerd4c7fb72021-11-09 16:03:50 -0600309 /**
310 * @brief Return the compatible values property
311 *
312 * @return const std::vector<std::string>& - The values
313 */
314 static const std::vector<std::string>& getCompatValues()
315 {
316 return _confCompatValues;
317 }
318
Matt Spinler59850df2021-01-06 16:56:06 -0600319 private:
Matthew Barthb370ab32021-06-10 14:38:02 -0500320 /* Load function to call for a fan app to load its config file(s). */
321 std::function<void()> _loadFunc;
322
Matt Spinler59850df2021-01-06 16:56:06 -0600323 /**
Matt Spinler59850df2021-01-06 16:56:06 -0600324 * @brief The interfacesAdded match that is used to wait
325 * for the IBMCompatibleSystem interface to show up.
326 */
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600327 std::unique_ptr<sdbusplus::bus::match_t> _match;
Matthew Barthb67089b2021-06-10 14:32:00 -0500328
329 /**
330 * @brief List of compatible values from the compatible interface
331 *
332 * Only supports a single instance of the compatible interface on a dbus
333 * object. If more than one dbus object exists with the compatible
334 * interface, the last one found will be the list of compatible values used.
335 */
336 inline static std::vector<std::string> _confCompatValues;
Matthew Barth11547c92020-05-05 10:34:27 -0500337};
338
339} // namespace phosphor::fan