blob: 47f0d000e5f47091470075d3a682956f77ba0c8a [file] [log] [blame]
George Liu616a0712021-02-18 10:50:24 +08001#include "config.h"
2
3#include "json-config.hpp"
4#include "ledlayout.hpp"
5
6#include <nlohmann/json.hpp>
George Liue9fb5c62021-07-01 14:05:32 +08007#include <phosphor-logging/lg2.hpp>
George Liu616a0712021-02-18 10:50:24 +08008#include <sdbusplus/bus.hpp>
9#include <sdeventplus/event.hpp>
10
11#include <filesystem>
12#include <fstream>
13#include <iostream>
14
15namespace fs = std::filesystem;
16
17using Json = nlohmann::json;
George Liu616a0712021-02-18 10:50:24 +080018
19// Priority for a particular LED needs to stay SAME across all groups
20// phosphor::led::Layout::Action can only be one of `Blink` and `On`
Patrick Williamsf2044032022-03-17 05:12:30 -050021using PriorityMap =
22 std::unordered_map<std::string, phosphor::led::Layout::Action>;
George Liu616a0712021-02-18 10:50:24 +080023
24/** @brief Parse LED JSON file and output Json object
25 *
26 * @param[in] path - path of LED JSON file
27 *
28 * @return const Json - Json object
29 */
Patrick Williams73ff9ad2022-03-16 16:27:21 -050030Json readJson(const fs::path& path)
George Liu616a0712021-02-18 10:50:24 +080031{
George Liu616a0712021-02-18 10:50:24 +080032 if (!fs::exists(path) || fs::is_empty(path))
33 {
George Liue9fb5c62021-07-01 14:05:32 +080034 lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}",
35 "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080036 throw std::runtime_error("Incorrect File Path or empty file");
37 }
38
39 try
40 {
41 std::ifstream jsonFile(path);
42 return Json::parse(jsonFile);
43 }
44 catch (const std::exception& e)
45 {
George Liue9fb5c62021-07-01 14:05:32 +080046 lg2::error(
47 "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
48 "ERROR", e, "PATH", path);
George Liu616a0712021-02-18 10:50:24 +080049 throw std::runtime_error("Failed to parse config file");
50 }
51}
52
53/** @brief Returns action enum based on string
54 *
55 * @param[in] action - action string
56 *
57 * @return Action - action enum (On/Blink)
58 */
59phosphor::led::Layout::Action getAction(const std::string& action)
60{
61 assert(action == "On" || action == "Blink");
62
Patrick Williamsed80e882022-03-17 05:03:51 -050063 return action == "Blink" ? phosphor::led::Layout::Action::Blink
64 : phosphor::led::Layout::Action::On;
George Liu616a0712021-02-18 10:50:24 +080065}
66
67/** @brief Validate the Priority of an LED is same across ALL groups
68 *
69 * @param[in] name - led name member of each group
70 * @param[in] priority - member priority of each group
Patrick Williamsf2044032022-03-17 05:12:30 -050071 * @param[out] priorityMap - std::unordered_map, key:name, value:priority
George Liu616a0712021-02-18 10:50:24 +080072 *
73 * @return
74 */
75void validatePriority(const std::string& name,
76 const phosphor::led::Layout::Action& priority,
77 PriorityMap& priorityMap)
78{
George Liu616a0712021-02-18 10:50:24 +080079 auto iter = priorityMap.find(name);
80 if (iter == priorityMap.end())
81 {
82 priorityMap.emplace(name, priority);
83 return;
84 }
85
86 if (iter->second != priority)
87 {
George Liue9fb5c62021-07-01 14:05:32 +080088 lg2::error(
89 "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
90 "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO",
91 int(priority));
George Liu616a0712021-02-18 10:50:24 +080092
93 throw std::runtime_error(
94 "Priority of at least one LED is not same across groups");
95 }
96}
97
Alexander Hansend0f80502024-07-23 12:12:54 +020098static void loadJsonConfigV1GroupMember(const Json& member,
99 PriorityMap& priorityMap,
100 phosphor::led::ActionSet& ledActions)
101{
102 auto name = member.value("Name", "");
103 auto action = getAction(member.value("Action", ""));
104 uint8_t dutyOn = member.value("DutyOn", 50);
105 uint16_t period = member.value("Period", 0);
106
107 // Since only have Blink/On and default priority is Blink
108 auto priority = getAction(member.value("Priority", "Blink"));
109
110 // Same LEDs can be part of multiple groups. However, their
111 // priorities across groups need to match.
112 validatePriority(name, priority, priorityMap);
113
114 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
115 priority};
116 ledActions.emplace(ledAction);
117}
118
119static void loadJsonConfigV1Group(const Json& entry,
120 phosphor::led::GroupMap& ledMap,
121 PriorityMap& priorityMap)
122{
123 const Json empty{};
124
125 fs::path tmpPath("/xyz/openbmc_project/led/groups");
126
127 const std::string groupName = entry.value("group", "");
128
129 tmpPath /= groupName;
130 auto objpath = tmpPath.string();
131 auto members = entry.value("members", empty);
132
133 lg2::debug("config for '{GROUP}'", "GROUP", groupName);
134
135 phosphor::led::ActionSet ledActions{};
136 for (const auto& member : members)
137 {
138 loadJsonConfigV1GroupMember(member, priorityMap, ledActions);
139 }
140
141 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
142 // containing the name and properties.
143 ledMap.emplace(objpath, ledActions);
144}
145
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500146/** @brief Load JSON config and return led map (JSON version 1)
George Liu616a0712021-02-18 10:50:24 +0800147 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500148 * @return phosphor::led::GroupMap
George Liu616a0712021-02-18 10:50:24 +0800149 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500150const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
George Liu616a0712021-02-18 10:50:24 +0800151{
Patrick Williams158b2c12022-03-17 05:57:44 -0500152 phosphor::led::GroupMap ledMap{};
George Liu616a0712021-02-18 10:50:24 +0800153 PriorityMap priorityMap{};
154
155 // define the default JSON as empty
156 const Json empty{};
George Liu616a0712021-02-18 10:50:24 +0800157 auto leds = json.value("leds", empty);
158
159 for (const auto& entry : leds)
160 {
Alexander Hansend0f80502024-07-23 12:12:54 +0200161 loadJsonConfigV1Group(entry, ledMap, priorityMap);
George Liu616a0712021-02-18 10:50:24 +0800162 }
163
164 return ledMap;
165}
166
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500167/** @brief Load JSON config and return led map
168 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500169 * @return phosphor::led::GroupMap
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500170 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500171const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500172{
173 auto json = readJson(path);
174
175 auto version = json.value("version", 1);
176 switch (version)
177 {
178 case 1:
179 return loadJsonConfigV1(json);
180
181 default:
182 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
183 version);
184 throw std::runtime_error("Unsupported version");
185 }
186
Patrick Williams158b2c12022-03-17 05:57:44 -0500187 return phosphor::led::GroupMap{};
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500188}
189
George Liu616a0712021-02-18 10:50:24 +0800190/** @brief Get led map from LED groups JSON config
191 *
Patrick Williams7217c032022-03-16 16:26:09 -0500192 * @param[in] config - Path to the JSON config.
Patrick Williams158b2c12022-03-17 05:57:44 -0500193 * @return phosphor::led::GroupMap
Patrick Williams7217c032022-03-16 16:26:09 -0500194 *
195 * @note if config is an empty string, daemon will interrogate dbus for
196 * compatible strings.
George Liu616a0712021-02-18 10:50:24 +0800197 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500198const phosphor::led::GroupMap getSystemLedMap(fs::path config)
George Liu616a0712021-02-18 10:50:24 +0800199{
Patrick Williams7217c032022-03-16 16:26:09 -0500200 if (config.empty())
George Liu616a0712021-02-18 10:50:24 +0800201 {
Patrick Williams7217c032022-03-16 16:26:09 -0500202 config = phosphor::led::getJsonConfig();
George Liu616a0712021-02-18 10:50:24 +0800203 }
204
Patrick Williams7217c032022-03-16 16:26:09 -0500205 return loadJsonConfig(config);
George Liu616a0712021-02-18 10:50:24 +0800206}