blob: 4eb77db6dfdadab231fcd42e63fdc19cac3383e9 [file] [log] [blame]
George Liu616a0712021-02-18 10:50:24 +08001#include "utils.hpp"
George Liudef5f5a2020-04-10 11:23:52 +08002
George Liue9fb5c62021-07-01 14:05:32 +08003#include <phosphor-logging/lg2.hpp>
George Liu616a0712021-02-18 10:50:24 +08004#include <sdbusplus/exception.hpp>
5#include <sdeventplus/event.hpp>
George Liudef5f5a2020-04-10 11:23:52 +08006
7#include <filesystem>
8#include <fstream>
George Liudef5f5a2020-04-10 11:23:52 +08009
10namespace fs = std::filesystem;
11
George Liu616a0712021-02-18 10:50:24 +080012namespace phosphor
George Liudef5f5a2020-04-10 11:23:52 +080013{
George Liu616a0712021-02-18 10:50:24 +080014namespace led
15{
George Liudef5f5a2020-04-10 11:23:52 +080016
George Liu616a0712021-02-18 10:50:24 +080017static constexpr auto confFileName = "led-group-config.json";
18static constexpr auto confOverridePath = "/etc/phosphor-led-manager";
19static constexpr auto confBasePath = "/usr/share/phosphor-led-manager";
20static constexpr auto confCompatibleInterface =
21 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
22static constexpr auto confCompatibleProperty = "Names";
23
24class JsonConfig
25{
26 public:
27 /**
28 * @brief Constructor
29 *
30 * Looks for the JSON config file. If it can't find one, then it
31 * will watch entity-manager for the IBMCompatibleSystem interface
32 * to show up.
33 *
34 * @param[in] bus - The D-Bus object
35 * @param[in] event - sd event handler
36 */
Patrick Williams3e073ba2022-07-22 19:26:52 -050037 JsonConfig(sdbusplus::bus_t& bus, sdeventplus::Event& event) : event(event)
George Liudef5f5a2020-04-10 11:23:52 +080038 {
Patrick Williamsf178c0f2021-11-19 12:23:09 -060039 match = std::make_unique<sdbusplus::bus::match_t>(
George Liu616a0712021-02-18 10:50:24 +080040 bus,
41 sdbusplus::bus::match::rules::interfacesAdded() +
42 sdbusplus::bus::match::rules::sender(
43 "xyz.openbmc_project.EntityManager"),
44 std::bind(&JsonConfig::ifacesAddedCallback, this,
45 std::placeholders::_1));
46 getFilePath();
47
48 if (!confFile.empty())
49 {
50 match.reset();
51 }
George Liudef5f5a2020-04-10 11:23:52 +080052 }
53
George Liu616a0712021-02-18 10:50:24 +080054 /**
55 * @brief Get the configuration file
56 *
57 * @return filesystem path
58 */
59 inline const fs::path& getConfFile() const
George Liudef5f5a2020-04-10 11:23:52 +080060 {
George Liu616a0712021-02-18 10:50:24 +080061 return confFile;
George Liudef5f5a2020-04-10 11:23:52 +080062 }
George Liu616a0712021-02-18 10:50:24 +080063
64 private:
65 /** @brief Check the file path exists
66 *
67 * @param[in] names - Vector of the confCompatible Property
68 *
69 * @return - True or False
70 */
71 bool filePathExists(const std::vector<std::string>& names)
George Liudef5f5a2020-04-10 11:23:52 +080072 {
Patrick Williams857da382023-05-10 07:50:20 -050073 auto it = std::find_if(names.begin(), names.end(),
74 [this](const auto& name) {
75 auto tempConfFile = fs::path{confBasePath} / name / confFileName;
76 if (fs::exists(tempConfFile))
77 {
78 confFile = tempConfFile;
79 return true;
80 }
81 return false;
82 });
George Liu616a0712021-02-18 10:50:24 +080083 return it == names.end() ? false : true;
84 }
85
86 /**
87 * @brief The interfacesAdded callback function that looks for
88 * the IBMCompatibleSystem interface. If it finds it,
89 * it uses the Names property in the interface to find
90 * the JSON config file to use.
91 *
92 * @param[in] msg - The D-Bus message contents
93 */
Patrick Williams3e073ba2022-07-22 19:26:52 -050094 void ifacesAddedCallback(sdbusplus::message_t& msg)
George Liu616a0712021-02-18 10:50:24 +080095 {
96 sdbusplus::message::object_path path;
Patrick Williamsf2044032022-03-17 05:12:30 -050097 std::unordered_map<
98 std::string,
99 std::unordered_map<std::string,
100 std::variant<std::vector<std::string>>>>
George Liu616a0712021-02-18 10:50:24 +0800101 interfaces;
102
103 msg.read(path, interfaces);
104
George Liu7f53a032021-05-04 11:18:21 +0800105 if (!interfaces.contains(confCompatibleInterface))
George Liu616a0712021-02-18 10:50:24 +0800106 {
107 return;
108 }
109
110 // Get the "Name" property value of the
111 // "xyz.openbmc_project.Configuration.IBMCompatibleSystem" interface
112 const auto& properties = interfaces.at(confCompatibleInterface);
113
George Liu7f53a032021-05-04 11:18:21 +0800114 if (!properties.contains(confCompatibleProperty))
George Liu616a0712021-02-18 10:50:24 +0800115 {
116 return;
117 }
118 auto names = std::get<std::vector<std::string>>(
119 properties.at(confCompatibleProperty));
120
121 if (filePathExists(names))
122 {
123 match.reset();
124
125 // This results in event.loop() exiting in getSystemLedMap
126 event.exit(0);
127 }
128 }
129
130 /**
131 * Get the json configuration file. The first location found to contain the
132 * json config file from the following locations in order.
133 * confOverridePath: /etc/phosphor-led-manager/led-group-config.json
134 * confBasePath: /usr/shard/phosphor-led-manager/led-group-config.json
135 * the name property of the confCompatibleInterface:
136 * /usr/shard/phosphor-led-manager/${Name}/led-group-config.json
137 *
138 * @brief Get the configuration file to be used
139 *
140 * @return
141 */
George Liu4b062012020-10-13 15:26:58 +0800142 void getFilePath()
George Liu616a0712021-02-18 10:50:24 +0800143 {
144 // Check override location
145 confFile = fs::path{confOverridePath} / confFileName;
146 if (fs::exists(confFile))
147 {
148 return;
149 }
150
151 // If the default file is there, use it
152 confFile = fs::path{confBasePath} / confFileName;
153 if (fs::exists(confFile))
154 {
155 return;
156 }
157 confFile.clear();
158
159 try
160 {
161 // Get all objects implementing the compatible interface
Patrick Williams857da382023-05-10 07:50:20 -0500162 auto objects = dBusHandler.getSubTreePaths("/",
163 confCompatibleInterface);
George Liu616a0712021-02-18 10:50:24 +0800164 for (const auto& path : objects)
165 {
166 try
167 {
168 // Retrieve json config compatible relative path locations
169 auto value = dBusHandler.getProperty(
170 path, confCompatibleInterface, confCompatibleProperty);
171
172 auto confCompatValues =
173 std::get<std::vector<std::string>>(value);
174
175 // Look for a config file at each name relative to the base
176 // path and use the first one found
177 if (filePathExists(confCompatValues))
178 {
179 // Use the first config file found at a listed location
180 break;
181 }
182 confFile.clear();
183 }
Patrick Williams3e073ba2022-07-22 19:26:52 -0500184 catch (const sdbusplus::exception_t& e)
George Liu616a0712021-02-18 10:50:24 +0800185 {
186 // Property unavailable on object.
George Liue9fb5c62021-07-01 14:05:32 +0800187 lg2::error(
188 "Failed to get Names property, ERROR = {ERROR}, INTERFACES = {INTERFACES}, PATH = {PATH}",
189 "ERROR", e, "INTERFACE", confCompatibleInterface,
190 "PATH", path);
George Liudef5f5a2020-04-10 11:23:52 +0800191
George Liu616a0712021-02-18 10:50:24 +0800192 confFile.clear();
193 }
194 }
195 }
Patrick Williams3e073ba2022-07-22 19:26:52 -0500196 catch (const sdbusplus::exception_t& e)
George Liu616a0712021-02-18 10:50:24 +0800197 {
George Liue9fb5c62021-07-01 14:05:32 +0800198 lg2::error(
199 "Failed to call the SubTreePaths method, ERROR = {ERROR}, INTERFACE = {INTERFACE}",
200 "ERROR", e, "INTERFACE", confCompatibleInterface);
George Liu616a0712021-02-18 10:50:24 +0800201 }
George Liu17fcb4d2020-06-30 18:12:53 +0800202 return;
203 }
204
George Liu616a0712021-02-18 10:50:24 +0800205 private:
206 /**
207 * @brief sd event handler.
208 */
209 sdeventplus::Event& event;
George Liu17fcb4d2020-06-30 18:12:53 +0800210
George Liu616a0712021-02-18 10:50:24 +0800211 /**
212 * @brief The JSON config file
213 */
214 fs::path confFile;
George Liu17fcb4d2020-06-30 18:12:53 +0800215
George Liu616a0712021-02-18 10:50:24 +0800216 /**
217 * @brief The interfacesAdded match that is used to wait
218 * for the IBMCompatibleSystem interface to show up.
219 */
Patrick Williamsf178c0f2021-11-19 12:23:09 -0600220 std::unique_ptr<sdbusplus::bus::match_t> match;
George Liudef5f5a2020-04-10 11:23:52 +0800221
George Liu616a0712021-02-18 10:50:24 +0800222 /** DBusHandler class handles the D-Bus operations */
223 utils::DBusHandler dBusHandler;
224};
Patrick Williams7217c032022-03-16 16:26:09 -0500225
226/** Blocking call to find the JSON Config from DBus. */
227auto getJsonConfig()
228{
229 // Get a new Dbus
230 auto bus = sdbusplus::bus::new_bus();
231
232 // Get a new event loop
233 auto event = sdeventplus::Event::get_new();
234
235 // Attach the bus to sd_event to service user requests
236 bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
237 phosphor::led::JsonConfig jsonConfig(bus, event);
238
239 // The event loop will be terminated from inside of a function in JsonConfig
240 // after finding the configuration file
241 if (jsonConfig.getConfFile().empty())
242 {
243 event.loop();
244 }
245
246 // Detach the bus from its sd_event event loop object
247 bus.detach_event();
248
249 return jsonConfig.getConfFile();
250}
251
George Liu616a0712021-02-18 10:50:24 +0800252} // namespace led
253} // namespace phosphor