blob: 3cb7df2752425abbe593ccca0451165a723c1457 [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
Matthew Barth1826c732020-08-28 08:40:59 -050020#include <fmt/format.h>
21
Matthew Barth11547c92020-05-05 10:34:27 -050022#include <nlohmann/json.hpp>
23#include <phosphor-logging/log.hpp>
24#include <sdbusplus/bus.hpp>
25#include <sdeventplus/source/signal.hpp>
26
27#include <filesystem>
28#include <fstream>
29
30namespace phosphor::fan
31{
32
33namespace fs = std::filesystem;
34using json = nlohmann::json;
35using namespace phosphor::logging;
36
37constexpr auto confOverridePath = "/etc/phosphor-fan-presence";
38constexpr auto confBasePath = "/usr/share/phosphor-fan-presence";
Matthew Barthb370ab32021-06-10 14:38:02 -050039constexpr auto confCompatServ = "xyz.openbmc_project.EntityManager";
Matthew Barthfcbdc0e2020-10-28 14:11:07 -050040constexpr auto confCompatIntf =
41 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
42constexpr auto confCompatProp = "Names";
Matthew Barth11547c92020-05-05 10:34:27 -050043
44class JsonConfig
45{
46 public:
Matt Spinler59850df2021-01-06 16:56:06 -060047 /**
Matthew Barthd80c8752021-06-01 14:46:02 -050048 * @brief Get the object paths with the compatible interface
49 *
50 * Retrieve all the object paths implementing the compatible interface for
51 * configuration file loading.
52 */
53 static auto& getCompatObjPaths() __attribute__((pure))
54 {
55 static auto paths = util::SDBusPlus::getSubTreePathsRaw(
56 util::SDBusPlus::getBus(), "/", confCompatIntf, 0);
57 return paths;
58 }
59
60 /**
Matt Spinler59850df2021-01-06 16:56:06 -060061 * @brief Constructor
62 *
Matthew Barthb370ab32021-06-10 14:38:02 -050063 * Attempts to set the list of compatible values from the compatible
64 * interface and call the fan app's function to load its config file(s). If
65 * the compatible interface is not found, it subscribes to the
66 * interfacesAdded signal for that interface on the compatible service
67 * defined above.
68 *
69 * @param[in] func - Fan app function to call to load its config file(s)
70 */
71 JsonConfig(std::function<void()> func) : _loadFunc(func)
72 {
73 _match = std::make_unique<sdbusplus::server::match::match>(
74 util::SDBusPlus::getBus(),
75 sdbusplus::bus::match::rules::interfacesAdded() +
76 sdbusplus::bus::match::rules::sender(confCompatServ),
77 std::bind(&JsonConfig::compatIntfAdded, this,
78 std::placeholders::_1));
Matthew Barth1689cb62021-08-27 12:34:36 -050079
80 auto compatObjPaths = getCompatObjPaths();
81 if (!compatObjPaths.empty())
Matthew Barthb370ab32021-06-10 14:38:02 -050082 {
Matthew Barth1689cb62021-08-27 12:34:36 -050083 for (auto& path : compatObjPaths)
Matthew Barthb370ab32021-06-10 14:38:02 -050084 {
Matthew Barth1689cb62021-08-27 12:34:36 -050085 try
Matthew Barthb370ab32021-06-10 14:38:02 -050086 {
Matthew Barth1689cb62021-08-27 12:34:36 -050087 // Retrieve json config compatible relative path
88 // locations (last one found will be what's used if more
89 // than one dbus object implementing the comptaible
90 // interface exists).
91 _confCompatValues =
92 util::SDBusPlus::getProperty<std::vector<std::string>>(
93 util::SDBusPlus::getBus(), path, confCompatIntf,
94 confCompatProp);
Matthew Barthb370ab32021-06-10 14:38:02 -050095 }
Matthew Barth1689cb62021-08-27 12:34:36 -050096 catch (const util::DBusError&)
97 {
98 // Compatible property unavailable on this dbus object
99 // path's compatible interface, ignore
100 }
101 }
102 _loadFunc();
103 _match.reset();
104 }
105 else
106 {
107 // Check if required config(s) are found not needing the
108 // compatible interface, otherwise this is intended to catch the
109 // exception thrown by the getConfFile function when the
110 // required config file was not found. This would then result in
111 // waiting for the compatible interfacesAdded signal
112 try
113 {
Matthew Barthb370ab32021-06-10 14:38:02 -0500114 _loadFunc();
115 _match.reset();
116 }
Matthew Barth1689cb62021-08-27 12:34:36 -0500117 catch (const std::runtime_error&)
Matthew Barthb370ab32021-06-10 14:38:02 -0500118 {
Matthew Barth1689cb62021-08-27 12:34:36 -0500119 // Wait for compatible interfacesAdded signal
Matthew Barthb370ab32021-06-10 14:38:02 -0500120 }
121 }
Matthew Barthb370ab32021-06-10 14:38:02 -0500122 }
123
124 /**
Matthew Barthb370ab32021-06-10 14:38:02 -0500125 * @brief InterfacesAdded callback function for the compatible interface.
126 *
127 * @param[in] msg - The D-Bus message contents
128 *
129 * If the compatible interface is found, it uses the compatible property on
130 * the interface to set the list of compatible values to be used when
131 * attempting to get a configuration file. Once the list of compatible
132 * values has been updated, it calls the load function.
133 */
134 void compatIntfAdded(sdbusplus::message::message& msg)
135 {
136 sdbusplus::message::object_path op;
137 std::map<std::string,
138 std::map<std::string, std::variant<std::vector<std::string>>>>
139 intfProps;
140
141 msg.read(op, intfProps);
142
143 if (intfProps.find(confCompatIntf) == intfProps.end())
144 {
145 return;
146 }
147
148 const auto& props = intfProps.at(confCompatIntf);
149 // Only one dbus object with the compatible interface is used at a time
150 _confCompatValues =
151 std::get<std::vector<std::string>>(props.at(confCompatProp));
152 _loadFunc();
153 }
154
155 /**
Matthew Barth11547c92020-05-05 10:34:27 -0500156 * Get the json configuration file. The first location found to contain
157 * the json config file for the given fan application is used from the
158 * following locations in order.
159 * 1.) From the confOverridePath location
Matthew Barthb67089b2021-06-10 14:32:00 -0500160 * 2.) From the default confBasePath location
161 * 3.) From config file found using an entry from a list obtained from an
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500162 * interface's property as a relative path extension on the base path where:
163 * interface = Interface set in confCompatIntf with the property
164 * property = Property set in confCompatProp containing a list of
165 * subdirectories in priority order to find a config
Matthew Barth11547c92020-05-05 10:34:27 -0500166 *
167 * @brief Get the configuration file to be used
168 *
169 * @param[in] bus - The dbus bus object
170 * @param[in] appName - The phosphor-fan-presence application name
171 * @param[in] fileName - Application's configuration file's name
Matthew Barthb5991022020-08-05 11:08:50 -0500172 * @param[in] isOptional - Config file is optional, default to 'false'
Matthew Barth11547c92020-05-05 10:34:27 -0500173 *
174 * @return filesystem path
175 * The filesystem path to the configuration file to use
176 */
177 static const fs::path getConfFile(sdbusplus::bus::bus& bus,
178 const std::string& appName,
Matthew Barthb5991022020-08-05 11:08:50 -0500179 const std::string& fileName,
180 bool isOptional = false)
Matthew Barth11547c92020-05-05 10:34:27 -0500181 {
182 // Check override location
183 fs::path confFile = fs::path{confOverridePath} / appName / fileName;
184 if (fs::exists(confFile))
185 {
186 return confFile;
187 }
188
Matt Spinler59850df2021-01-06 16:56:06 -0600189 // If the default file is there, use it
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500190 confFile = fs::path{confBasePath} / appName / fileName;
Matt Spinler59850df2021-01-06 16:56:06 -0600191 if (fs::exists(confFile))
192 {
193 return confFile;
194 }
Matthew Barthfcbdc0e2020-10-28 14:11:07 -0500195
Matthew Barthb67089b2021-06-10 14:32:00 -0500196 // Look for a config file at each entry relative to the base
197 // path and use the first one found
198 auto it = std::find_if(
199 _confCompatValues.begin(), _confCompatValues.end(),
200 [&confFile, &appName, &fileName](const auto& value) {
201 confFile = fs::path{confBasePath} / appName / value / fileName;
202 return fs::exists(confFile);
203 });
204 if (it == _confCompatValues.end())
205 {
206 confFile.clear();
207 }
208
209 if (!isOptional && confFile.empty() && !_confCompatValues.empty())
Matt Spinler4031f102021-04-22 16:42:20 -0500210 {
211 log<level::ERR>(fmt::format("Could not find fan {} conf file {}",
212 appName, fileName)
213 .c_str());
214 }
215
Matt Spinler59850df2021-01-06 16:56:06 -0600216 if (confFile.empty() && !isOptional)
Matthew Barth11547c92020-05-05 10:34:27 -0500217 {
Matt Spinler59850df2021-01-06 16:56:06 -0600218 throw std::runtime_error("No JSON config file found");
Matthew Barthb5991022020-08-05 11:08:50 -0500219 }
Matthew Barth11547c92020-05-05 10:34:27 -0500220
221 return confFile;
222 }
223
224 /**
225 * @brief Load the JSON config file
226 *
227 * @param[in] confFile - File system path of the configuration file to load
228 *
229 * @return Parsed JSON object
230 * The parsed JSON configuration file object
231 */
232 static const json load(const fs::path& confFile)
233 {
234 std::ifstream file;
235 json jsonConf;
236
Matthew Barthb5991022020-08-05 11:08:50 -0500237 if (!confFile.empty() && fs::exists(confFile))
Matthew Barth11547c92020-05-05 10:34:27 -0500238 {
Matthew Barth1826c732020-08-28 08:40:59 -0500239 log<level::INFO>(
240 fmt::format("Loading configuration from {}", confFile.string())
241 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500242 file.open(confFile);
243 try
244 {
Matthew Barthde72d5d2021-07-16 10:39:42 -0500245 // Enable ignoring `//` or `/* */` comments
246 jsonConf = json::parse(file, nullptr, true, true);
Matthew Barth11547c92020-05-05 10:34:27 -0500247 }
248 catch (std::exception& e)
249 {
Matthew Barth1826c732020-08-28 08:40:59 -0500250 log<level::ERR>(
251 fmt::format(
252 "Failed to parse JSON config file: {}, error: {}",
253 confFile.string(), e.what())
254 .c_str());
255 throw std::runtime_error(
256 fmt::format(
257 "Failed to parse JSON config file: {}, error: {}",
258 confFile.string(), e.what())
259 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500260 }
261 }
262 else
263 {
Matthew Barth1826c732020-08-28 08:40:59 -0500264 log<level::ERR>(fmt::format("Unable to open JSON config file: {}",
265 confFile.string())
266 .c_str());
267 throw std::runtime_error(
268 fmt::format("Unable to open JSON config file: {}",
269 confFile.string())
270 .c_str());
Matthew Barth11547c92020-05-05 10:34:27 -0500271 }
272
273 return jsonConf;
274 }
Matt Spinler59850df2021-01-06 16:56:06 -0600275
276 private:
Matthew Barthb370ab32021-06-10 14:38:02 -0500277 /* Load function to call for a fan app to load its config file(s). */
278 std::function<void()> _loadFunc;
279
Matt Spinler59850df2021-01-06 16:56:06 -0600280 /**
Matt Spinler59850df2021-01-06 16:56:06 -0600281 * @brief The interfacesAdded match that is used to wait
282 * for the IBMCompatibleSystem interface to show up.
283 */
284 std::unique_ptr<sdbusplus::server::match::match> _match;
Matthew Barthb67089b2021-06-10 14:32:00 -0500285
286 /**
287 * @brief List of compatible values from the compatible interface
288 *
289 * Only supports a single instance of the compatible interface on a dbus
290 * object. If more than one dbus object exists with the compatible
291 * interface, the last one found will be the list of compatible values used.
292 */
293 inline static std::vector<std::string> _confCompatValues;
Matthew Barth11547c92020-05-05 10:34:27 -0500294};
295
296} // namespace phosphor::fan