blob: 8f2832baaaf4aeda1084717afbe1be2bcb9de532 [file] [log] [blame]
George Liu616a0712021-02-18 10:50:24 +08001#include "config.h"
2
Alexander Hansen7ba70c82024-07-23 13:46:25 +02003#include "grouplayout.hpp"
George Liu616a0712021-02-18 10:50:24 +08004#include "json-config.hpp"
5#include "ledlayout.hpp"
6
7#include <nlohmann/json.hpp>
George Liue9fb5c62021-07-01 14:05:32 +08008#include <phosphor-logging/lg2.hpp>
George Liu616a0712021-02-18 10:50:24 +08009#include <sdbusplus/bus.hpp>
10#include <sdeventplus/event.hpp>
11
12#include <filesystem>
13#include <fstream>
14#include <iostream>
15
16namespace fs = std::filesystem;
17
18using Json = nlohmann::json;
George Liu616a0712021-02-18 10:50:24 +080019
20// Priority for a particular LED needs to stay SAME across all groups
Patrick Williamsf2044032022-03-17 05:12:30 -050021using PriorityMap =
Alexander Hansen55badf72024-07-24 14:35:13 +020022 std::unordered_map<std::string,
23 std::optional<phosphor::led::Layout::Action>>;
George Liu616a0712021-02-18 10:50:24 +080024
25/** @brief Parse LED JSON file and output Json object
26 *
27 * @param[in] path - path of LED JSON file
28 *
29 * @return const Json - Json object
30 */
Patrick Williams73ff9ad2022-03-16 16:27:21 -050031Json readJson(const fs::path& path)
George Liu616a0712021-02-18 10:50:24 +080032{
George Liu616a0712021-02-18 10:50:24 +080033 if (!fs::exists(path) || fs::is_empty(path))
34 {
George Liue9fb5c62021-07-01 14:05:32 +080035 lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}",
36 "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080037 throw std::runtime_error("Incorrect File Path or empty file");
38 }
39
40 try
41 {
42 std::ifstream jsonFile(path);
43 return Json::parse(jsonFile);
44 }
45 catch (const std::exception& e)
46 {
George Liue9fb5c62021-07-01 14:05:32 +080047 lg2::error(
48 "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
49 "ERROR", e, "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080050 throw std::runtime_error("Failed to parse config file");
51 }
52}
53
54/** @brief Returns action enum based on string
55 *
56 * @param[in] action - action string
57 *
Alexander Hansen4d44a552024-07-24 14:55:24 +020058 * @return Action - action enum (On/Off/Blink)
George Liu616a0712021-02-18 10:50:24 +080059 */
60phosphor::led::Layout::Action getAction(const std::string& action)
61{
Alexander Hansen4d44a552024-07-24 14:55:24 +020062 if (action == "On")
63 {
64 return phosphor::led::Layout::Action::On;
65 }
66 if (action == "Off")
67 {
68 return phosphor::led::Layout::Action::Off;
69 }
70 if (action == "Blink")
71 {
72 return phosphor::led::Layout::Action::Blink;
73 }
George Liu616a0712021-02-18 10:50:24 +080074
Alexander Hansen4d44a552024-07-24 14:55:24 +020075 assert(false);
76 return phosphor::led::Layout::Action::Blink;
George Liu616a0712021-02-18 10:50:24 +080077}
78
Alexander Hansen55badf72024-07-24 14:35:13 +020079static std::string priorityToString(
80 const std::optional<phosphor::led::Layout::Action>& priority)
81{
82 if (!priority.has_value())
83 {
84 return "none";
85 }
86 switch (priority.value())
87 {
88 case phosphor::led::Layout::Action::Off:
89 return "Off";
90 case phosphor::led::Layout::Action::On:
91 return "On";
92 case phosphor::led::Layout::Action::Blink:
93 return "Blink";
94 }
95 return "?";
96}
97
George Liu616a0712021-02-18 10:50:24 +080098/** @brief Validate the Priority of an LED is same across ALL groups
99 *
100 * @param[in] name - led name member of each group
101 * @param[in] priority - member priority of each group
Patrick Williamsf2044032022-03-17 05:12:30 -0500102 * @param[out] priorityMap - std::unordered_map, key:name, value:priority
George Liu616a0712021-02-18 10:50:24 +0800103 *
104 * @return
105 */
Alexander Hansen55badf72024-07-24 14:35:13 +0200106void validatePriority(
107 const std::string& name,
108 const std::optional<phosphor::led::Layout::Action>& priority,
109 PriorityMap& priorityMap)
George Liu616a0712021-02-18 10:50:24 +0800110{
George Liu616a0712021-02-18 10:50:24 +0800111 auto iter = priorityMap.find(name);
112 if (iter == priorityMap.end())
113 {
114 priorityMap.emplace(name, priority);
115 return;
116 }
117
118 if (iter->second != priority)
119 {
George Liue9fb5c62021-07-01 14:05:32 +0800120 lg2::error(
121 "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
Alexander Hansen55badf72024-07-24 14:35:13 +0200122 "NAME", name, "OLD_PRIO", priorityToString(iter->second),
123 "NEW_PRIO", priorityToString(priority));
George Liu616a0712021-02-18 10:50:24 +0800124
125 throw std::runtime_error(
126 "Priority of at least one LED is not same across groups");
127 }
128}
129
Alexander Hansend0f80502024-07-23 12:12:54 +0200130static void loadJsonConfigV1GroupMember(const Json& member,
131 PriorityMap& priorityMap,
132 phosphor::led::ActionSet& ledActions)
133{
134 auto name = member.value("Name", "");
135 auto action = getAction(member.value("Action", ""));
136 uint8_t dutyOn = member.value("DutyOn", 50);
137 uint16_t period = member.value("Period", 0);
138
Alexander Hansen55badf72024-07-24 14:35:13 +0200139 const std::string priorityStr = member.value("Priority", "");
140 std::optional<phosphor::led::Layout::Action> priority = std::nullopt;
141
142 if (!priorityStr.empty())
143 {
144 priority = getAction(priorityStr);
145 }
Alexander Hansend0f80502024-07-23 12:12:54 +0200146
147 // Same LEDs can be part of multiple groups. However, their
148 // priorities across groups need to match.
149 validatePriority(name, priority, priorityMap);
150
151 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
152 priority};
153 ledActions.emplace(ledAction);
154}
155
156static void loadJsonConfigV1Group(const Json& entry,
157 phosphor::led::GroupMap& ledMap,
158 PriorityMap& priorityMap)
159{
160 const Json empty{};
161
162 fs::path tmpPath("/xyz/openbmc_project/led/groups");
163
164 const std::string groupName = entry.value("group", "");
165
166 tmpPath /= groupName;
167 auto objpath = tmpPath.string();
168 auto members = entry.value("members", empty);
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200169 int priority = entry.value("Priority", 0);
Alexander Hansend0f80502024-07-23 12:12:54 +0200170
171 lg2::debug("config for '{GROUP}'", "GROUP", groupName);
172
173 phosphor::led::ActionSet ledActions{};
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200174 phosphor::led::Layout::GroupLayout groupLayout{};
Alexander Hansend0f80502024-07-23 12:12:54 +0200175 for (const auto& member : members)
176 {
177 loadJsonConfigV1GroupMember(member, priorityMap, ledActions);
178 }
179
180 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
181 // containing the name and properties.
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200182 groupLayout.actionSet = ledActions;
183 groupLayout.priority = priority;
184
185 ledMap.emplace(objpath, groupLayout);
Alexander Hansend0f80502024-07-23 12:12:54 +0200186}
187
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500188/** @brief Load JSON config and return led map (JSON version 1)
George Liu616a0712021-02-18 10:50:24 +0800189 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500190 * @return phosphor::led::GroupMap
George Liu616a0712021-02-18 10:50:24 +0800191 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500192const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
George Liu616a0712021-02-18 10:50:24 +0800193{
Patrick Williams158b2c12022-03-17 05:57:44 -0500194 phosphor::led::GroupMap ledMap{};
George Liu616a0712021-02-18 10:50:24 +0800195 PriorityMap priorityMap{};
196
197 // define the default JSON as empty
198 const Json empty{};
George Liu616a0712021-02-18 10:50:24 +0800199 auto leds = json.value("leds", empty);
200
201 for (const auto& entry : leds)
202 {
Alexander Hansend0f80502024-07-23 12:12:54 +0200203 loadJsonConfigV1Group(entry, ledMap, priorityMap);
George Liu616a0712021-02-18 10:50:24 +0800204 }
205
206 return ledMap;
207}
208
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500209/** @brief Load JSON config and return led map
210 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500211 * @return phosphor::led::GroupMap
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500212 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500213const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500214{
215 auto json = readJson(path);
216
217 auto version = json.value("version", 1);
218 switch (version)
219 {
220 case 1:
221 return loadJsonConfigV1(json);
222
223 default:
224 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
225 version);
226 throw std::runtime_error("Unsupported version");
227 }
228
Patrick Williams158b2c12022-03-17 05:57:44 -0500229 return phosphor::led::GroupMap{};
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500230}
231
George Liu616a0712021-02-18 10:50:24 +0800232/** @brief Get led map from LED groups JSON config
233 *
Patrick Williams7217c032022-03-16 16:26:09 -0500234 * @param[in] config - Path to the JSON config.
Patrick Williams158b2c12022-03-17 05:57:44 -0500235 * @return phosphor::led::GroupMap
Patrick Williams7217c032022-03-16 16:26:09 -0500236 *
237 * @note if config is an empty string, daemon will interrogate dbus for
238 * compatible strings.
George Liu616a0712021-02-18 10:50:24 +0800239 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500240const phosphor::led::GroupMap getSystemLedMap(fs::path config)
George Liu616a0712021-02-18 10:50:24 +0800241{
Patrick Williams7217c032022-03-16 16:26:09 -0500242 if (config.empty())
George Liu616a0712021-02-18 10:50:24 +0800243 {
Patrick Williams7217c032022-03-16 16:26:09 -0500244 config = phosphor::led::getJsonConfig();
George Liu616a0712021-02-18 10:50:24 +0800245 }
246
Patrick Williams7217c032022-03-16 16:26:09 -0500247 return loadJsonConfig(config);
George Liu616a0712021-02-18 10:50:24 +0800248}