-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjson-config.hpp
271 lines (241 loc) · 8.5 KB
/
json-config.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "utils.hpp"
#include <nlohmann/json.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/exception.hpp>
#include <sdeventplus/event.hpp>
#include <filesystem>
#include <fstream>
#include <unordered_map>
namespace fs = std::filesystem;
namespace phosphor
{
namespace led
{
static constexpr auto confFileName = "led-group-config.json";
static constexpr auto confOverridePath = "/etc/phosphor-led-manager";
static constexpr auto confBasePath = "/usr/share/phosphor-led-manager";
static constexpr auto modelFilePath = "/sys/firmware/devicetree/base/model";
static constexpr auto confCompatibleInterface =
"xyz.openbmc_project.Configuration.IBMCompatibleSystem";
static constexpr auto confCompatibleProperty = "Names";
static constexpr auto configMapJson = "config_map.json";
class JsonConfig
{
public:
/**
* @brief Constructor
*
* Looks for the JSON config file. If it can't find one, then it
* will watch entity-manager for the IBMCompatibleSystem interface
* to show up.
*
* @param[in] bus - The D-Bus object
* @param[in] event - sd event handler
*/
JsonConfig(sdbusplus::bus::bus& bus, sdeventplus::Event& event) :
event(event)
{
match = std::make_unique<sdbusplus::bus::match_t>(
bus,
sdbusplus::bus::match::rules::interfacesAdded() +
sdbusplus::bus::match::rules::sender(
"xyz.openbmc_project.EntityManager"),
std::bind(&JsonConfig::ifacesAddedCallback, this,
std::placeholders::_1));
getFilePath();
if (!confFile.empty())
{
match.reset();
}
}
/**
* @brief Get the configuration file
*
* @return filesystem path
*/
inline const fs::path& getConfFile() const
{
return confFile;
}
private:
/** @brief Check the file path exists
*
* @param[in] names - Vector of the confCompatible Property
*
* @return - True or False
*/
bool filePathExists(const std::vector<std::string>& names)
{
auto it =
std::find_if(names.begin(), names.end(), [this](const auto& name) {
auto tempConfFile =
fs::path{confBasePath} / name / confFileName;
if (fs::exists(tempConfFile))
{
confFile = tempConfFile;
return true;
}
return false;
});
return it == names.end() ? false : true;
}
/**
* @brief The interfacesAdded callback function that looks for
* the IBMCompatibleSystem interface. If it finds it,
* it uses the Names property in the interface to find
* the JSON config file to use.
*
* @param[in] msg - The D-Bus message contents
*/
void ifacesAddedCallback(sdbusplus::message::message& msg)
{
sdbusplus::message::object_path path;
std::map<std::string,
std::map<std::string, std::variant<std::vector<std::string>>>>
interfaces;
msg.read(path, interfaces);
if (!interfaces.contains(confCompatibleInterface))
{
return;
}
// Get the "Name" property value of the
// "xyz.openbmc_project.Configuration.IBMCompatibleSystem" interface
const auto& properties = interfaces.at(confCompatibleInterface);
if (!properties.contains(confCompatibleProperty))
{
return;
}
auto names = std::get<std::vector<std::string>>(
properties.at(confCompatibleProperty));
if (filePathExists(names))
{
match.reset();
// This results in event.loop() exiting in getSystemLedMap
event.exit(0);
}
}
/**
* Get the json configuration file. The first location found to contain the
* json config file from the following locations in order.
* confOverridePath: /etc/phosphor-led-manager/led-group-config.json
* confBasePath: /usr/shard/phosphor-led-manager/led-group-config.json
* the name property of the confCompatibleInterface:
* /usr/shard/phosphor-led-manager/${Name}/led-group-config.json
*
* @brief Get the configuration file to be used
*
* @return
*/
void getFilePath()
{
// Check override location
confFile = fs::path{confOverridePath} / confFileName;
if (fs::exists(confFile))
{
return;
}
// If the default file is there, use it
confFile = fs::path{confBasePath} / confFileName;
if (fs::exists(confFile))
{
return;
}
confFile.clear();
try
{
auto configMapJsonPath = fs::path{confBasePath} / configMapJson;
// based on device tree check if config file exists for the model.
if (fs::exists(modelFilePath) && fs::exists(configMapJsonPath))
{
std::ifstream modelFile(modelFilePath);
std::ifstream configMapFile(configMapJsonPath);
if (modelFile.is_open() && configMapFile.is_open())
{
std::string model;
std::getline(modelFile, model, '\0');
auto configMap = nlohmann::json::parse(configMapFile);
if (configMap.contains("CompatibleSystems"))
{
if (configMap["CompatibleSystems"].contains(model))
{
std::vector<std::string> name;
name.push_back(
configMap["CompatibleSystems"][model]);
if (filePathExists(name))
{
// Use the first config file found at a listed
// location
return;
}
}
}
}
}
}
catch (const std::exception& ex)
{
lg2::error(
"Failed to fetch config file using model data. ERROR = {ERROR}",
"ERROR", ex.what());
}
try
{
// Get all objects implementing the compatible interface
auto objects =
dBusHandler.getSubTreePaths("/", confCompatibleInterface);
for (const auto& path : objects)
{
try
{
// Retrieve json config compatible relative path locations
auto value = dBusHandler.getProperty(
path, confCompatibleInterface, confCompatibleProperty);
auto confCompatValues =
std::get<std::vector<std::string>>(value);
// Look for a config file at each name relative to the base
// path and use the first one found
if (filePathExists(confCompatValues))
{
// Use the first config file found at a listed location
break;
}
confFile.clear();
}
catch (const sdbusplus::exception::exception& e)
{
// Property unavailable on object.
lg2::error(
"Failed to get Names property, ERROR = {ERROR}, INTERFACES = {INTERFACES}, PATH = {PATH}",
"ERROR", e, "INTERFACE", confCompatibleInterface,
"PATH", path);
confFile.clear();
}
}
}
catch (const sdbusplus::exception::exception& e)
{
lg2::error(
"Failed to call the SubTreePaths method, ERROR = {ERROR}, INTERFACE = {INTERFACE}",
"ERROR", e, "INTERFACE", confCompatibleInterface);
}
return;
}
private:
/**
* @brief sd event handler.
*/
sdeventplus::Event& event;
/**
* @brief The JSON config file
*/
fs::path confFile;
/**
* @brief The interfacesAdded match that is used to wait
* for the IBMCompatibleSystem interface to show up.
*/
std::unique_ptr<sdbusplus::bus::match_t> match;
/** DBusHandler class handles the D-Bus operations */
utils::DBusHandler dBusHandler;
};
} // namespace led
} // namespace phosphor