blob: e17568b8103a8ee93f560ed5ae1cfeb857987dff [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
21// phosphor::led::Layout::Action can only be one of `Blink` and `On`
Patrick Williamsf2044032022-03-17 05:12:30 -050022using PriorityMap =
Alexander Hansen55badf72024-07-24 14:35:13 +020023 std::unordered_map<std::string,
24 std::optional<phosphor::led::Layout::Action>>;
George Liu616a0712021-02-18 10:50:24 +080025
26/** @brief Parse LED JSON file and output Json object
27 *
28 * @param[in] path - path of LED JSON file
29 *
30 * @return const Json - Json object
31 */
Patrick Williams73ff9ad2022-03-16 16:27:21 -050032Json readJson(const fs::path& path)
George Liu616a0712021-02-18 10:50:24 +080033{
George Liu616a0712021-02-18 10:50:24 +080034 if (!fs::exists(path) || fs::is_empty(path))
35 {
George Liue9fb5c62021-07-01 14:05:32 +080036 lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}",
37 "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080038 throw std::runtime_error("Incorrect File Path or empty file");
39 }
40
41 try
42 {
43 std::ifstream jsonFile(path);
44 return Json::parse(jsonFile);
45 }
46 catch (const std::exception& e)
47 {
George Liue9fb5c62021-07-01 14:05:32 +080048 lg2::error(
49 "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
50 "ERROR", e, "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080051 throw std::runtime_error("Failed to parse config file");
52 }
53}
54
55/** @brief Returns action enum based on string
56 *
57 * @param[in] action - action string
58 *
59 * @return Action - action enum (On/Blink)
60 */
61phosphor::led::Layout::Action getAction(const std::string& action)
62{
63 assert(action == "On" || action == "Blink");
64
Patrick Williamsed80e882022-03-17 05:03:51 -050065 return action == "Blink" ? phosphor::led::Layout::Action::Blink
66 : phosphor::led::Layout::Action::On;
George Liu616a0712021-02-18 10:50:24 +080067}
68
Alexander Hansen55badf72024-07-24 14:35:13 +020069static std::string priorityToString(
70 const std::optional<phosphor::led::Layout::Action>& priority)
71{
72 if (!priority.has_value())
73 {
74 return "none";
75 }
76 switch (priority.value())
77 {
78 case phosphor::led::Layout::Action::Off:
79 return "Off";
80 case phosphor::led::Layout::Action::On:
81 return "On";
82 case phosphor::led::Layout::Action::Blink:
83 return "Blink";
84 }
85 return "?";
86}
87
George Liu616a0712021-02-18 10:50:24 +080088/** @brief Validate the Priority of an LED is same across ALL groups
89 *
90 * @param[in] name - led name member of each group
91 * @param[in] priority - member priority of each group
Patrick Williamsf2044032022-03-17 05:12:30 -050092 * @param[out] priorityMap - std::unordered_map, key:name, value:priority
George Liu616a0712021-02-18 10:50:24 +080093 *
94 * @return
95 */
Alexander Hansen55badf72024-07-24 14:35:13 +020096void validatePriority(
97 const std::string& name,
98 const std::optional<phosphor::led::Layout::Action>& priority,
99 PriorityMap& priorityMap)
George Liu616a0712021-02-18 10:50:24 +0800100{
George Liu616a0712021-02-18 10:50:24 +0800101 auto iter = priorityMap.find(name);
102 if (iter == priorityMap.end())
103 {
104 priorityMap.emplace(name, priority);
105 return;
106 }
107
108 if (iter->second != priority)
109 {
George Liue9fb5c62021-07-01 14:05:32 +0800110 lg2::error(
111 "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 +0200112 "NAME", name, "OLD_PRIO", priorityToString(iter->second),
113 "NEW_PRIO", priorityToString(priority));
George Liu616a0712021-02-18 10:50:24 +0800114
115 throw std::runtime_error(
116 "Priority of at least one LED is not same across groups");
117 }
118}
119
Alexander Hansend0f80502024-07-23 12:12:54 +0200120static void loadJsonConfigV1GroupMember(const Json& member,
121 PriorityMap& priorityMap,
122 phosphor::led::ActionSet& ledActions)
123{
124 auto name = member.value("Name", "");
125 auto action = getAction(member.value("Action", ""));
126 uint8_t dutyOn = member.value("DutyOn", 50);
127 uint16_t period = member.value("Period", 0);
128
Alexander Hansen55badf72024-07-24 14:35:13 +0200129 const std::string priorityStr = member.value("Priority", "");
130 std::optional<phosphor::led::Layout::Action> priority = std::nullopt;
131
132 if (!priorityStr.empty())
133 {
134 priority = getAction(priorityStr);
135 }
Alexander Hansend0f80502024-07-23 12:12:54 +0200136
137 // Same LEDs can be part of multiple groups. However, their
138 // priorities across groups need to match.
139 validatePriority(name, priority, priorityMap);
140
141 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
142 priority};
143 ledActions.emplace(ledAction);
144}
145
146static void loadJsonConfigV1Group(const Json& entry,
147 phosphor::led::GroupMap& ledMap,
148 PriorityMap& priorityMap)
149{
150 const Json empty{};
151
152 fs::path tmpPath("/xyz/openbmc_project/led/groups");
153
154 const std::string groupName = entry.value("group", "");
155
156 tmpPath /= groupName;
157 auto objpath = tmpPath.string();
158 auto members = entry.value("members", empty);
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200159 int priority = entry.value("Priority", 0);
Alexander Hansend0f80502024-07-23 12:12:54 +0200160
161 lg2::debug("config for '{GROUP}'", "GROUP", groupName);
162
163 phosphor::led::ActionSet ledActions{};
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200164 phosphor::led::Layout::GroupLayout groupLayout{};
Alexander Hansend0f80502024-07-23 12:12:54 +0200165 for (const auto& member : members)
166 {
167 loadJsonConfigV1GroupMember(member, priorityMap, ledActions);
168 }
169
170 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
171 // containing the name and properties.
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200172 groupLayout.actionSet = ledActions;
173 groupLayout.priority = priority;
174
175 ledMap.emplace(objpath, groupLayout);
Alexander Hansend0f80502024-07-23 12:12:54 +0200176}
177
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500178/** @brief Load JSON config and return led map (JSON version 1)
George Liu616a0712021-02-18 10:50:24 +0800179 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500180 * @return phosphor::led::GroupMap
George Liu616a0712021-02-18 10:50:24 +0800181 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500182const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
George Liu616a0712021-02-18 10:50:24 +0800183{
Patrick Williams158b2c12022-03-17 05:57:44 -0500184 phosphor::led::GroupMap ledMap{};
George Liu616a0712021-02-18 10:50:24 +0800185 PriorityMap priorityMap{};
186
187 // define the default JSON as empty
188 const Json empty{};
George Liu616a0712021-02-18 10:50:24 +0800189 auto leds = json.value("leds", empty);
190
191 for (const auto& entry : leds)
192 {
Alexander Hansend0f80502024-07-23 12:12:54 +0200193 loadJsonConfigV1Group(entry, ledMap, priorityMap);
George Liu616a0712021-02-18 10:50:24 +0800194 }
195
196 return ledMap;
197}
198
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500199/** @brief Load JSON config and return led map
200 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500201 * @return phosphor::led::GroupMap
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500202 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500203const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500204{
205 auto json = readJson(path);
206
207 auto version = json.value("version", 1);
208 switch (version)
209 {
210 case 1:
211 return loadJsonConfigV1(json);
212
213 default:
214 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
215 version);
216 throw std::runtime_error("Unsupported version");
217 }
218
Patrick Williams158b2c12022-03-17 05:57:44 -0500219 return phosphor::led::GroupMap{};
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500220}
221
George Liu616a0712021-02-18 10:50:24 +0800222/** @brief Get led map from LED groups JSON config
223 *
Patrick Williams7217c032022-03-16 16:26:09 -0500224 * @param[in] config - Path to the JSON config.
Patrick Williams158b2c12022-03-17 05:57:44 -0500225 * @return phosphor::led::GroupMap
Patrick Williams7217c032022-03-16 16:26:09 -0500226 *
227 * @note if config is an empty string, daemon will interrogate dbus for
228 * compatible strings.
George Liu616a0712021-02-18 10:50:24 +0800229 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500230const phosphor::led::GroupMap getSystemLedMap(fs::path config)
George Liu616a0712021-02-18 10:50:24 +0800231{
Patrick Williams7217c032022-03-16 16:26:09 -0500232 if (config.empty())
George Liu616a0712021-02-18 10:50:24 +0800233 {
Patrick Williams7217c032022-03-16 16:26:09 -0500234 config = phosphor::led::getJsonConfig();
George Liu616a0712021-02-18 10:50:24 +0800235 }
236
Patrick Williams7217c032022-03-16 16:26:09 -0500237 return loadJsonConfig(config);
George Liu616a0712021-02-18 10:50:24 +0800238}