blob: 0846a037bb2975a48e522fa88d4dedd0ac4b653e [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 Hansend0f80502024-07-23 12:12:54 +020079static void loadJsonConfigV1GroupMember(const Json& member,
Alexander Hansend0f80502024-07-23 12:12:54 +020080 phosphor::led::ActionSet& ledActions)
81{
82 auto name = member.value("Name", "");
83 auto action = getAction(member.value("Action", ""));
84 uint8_t dutyOn = member.value("DutyOn", 50);
85 uint16_t period = member.value("Period", 0);
86
Alexander Hansen55badf72024-07-24 14:35:13 +020087 const std::string priorityStr = member.value("Priority", "");
88 std::optional<phosphor::led::Layout::Action> priority = std::nullopt;
89
90 if (!priorityStr.empty())
91 {
92 priority = getAction(priorityStr);
93 }
Alexander Hansend0f80502024-07-23 12:12:54 +020094
Alexander Hansend0f80502024-07-23 12:12:54 +020095 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
96 priority};
97 ledActions.emplace(ledAction);
98}
99
100static void loadJsonConfigV1Group(const Json& entry,
Alexander Hansen638d1482024-08-21 17:39:57 +0200101 phosphor::led::GroupMap& ledMap)
Alexander Hansend0f80502024-07-23 12:12:54 +0200102{
103 const Json empty{};
104
105 fs::path tmpPath("/xyz/openbmc_project/led/groups");
106
107 const std::string groupName = entry.value("group", "");
108
109 tmpPath /= groupName;
110 auto objpath = tmpPath.string();
111 auto members = entry.value("members", empty);
112
113 lg2::debug("config for '{GROUP}'", "GROUP", groupName);
114
Alexander Hansen638d1482024-08-21 17:39:57 +0200115 int priority = entry.value("Priority", 0);
116
Alexander Hansend0f80502024-07-23 12:12:54 +0200117 phosphor::led::ActionSet ledActions{};
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200118 phosphor::led::Layout::GroupLayout groupLayout{};
Alexander Hansend0f80502024-07-23 12:12:54 +0200119 for (const auto& member : members)
120 {
Alexander Hansen638d1482024-08-21 17:39:57 +0200121 loadJsonConfigV1GroupMember(member, ledActions);
Alexander Hansend0f80502024-07-23 12:12:54 +0200122 }
123
124 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
125 // containing the name and properties.
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200126 groupLayout.actionSet = ledActions;
127 groupLayout.priority = priority;
128
129 ledMap.emplace(objpath, groupLayout);
Alexander Hansend0f80502024-07-23 12:12:54 +0200130}
131
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500132/** @brief Load JSON config and return led map (JSON version 1)
George Liu616a0712021-02-18 10:50:24 +0800133 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500134 * @return phosphor::led::GroupMap
George Liu616a0712021-02-18 10:50:24 +0800135 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500136const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
George Liu616a0712021-02-18 10:50:24 +0800137{
Patrick Williams158b2c12022-03-17 05:57:44 -0500138 phosphor::led::GroupMap ledMap{};
George Liu616a0712021-02-18 10:50:24 +0800139
140 // define the default JSON as empty
141 const Json empty{};
George Liu616a0712021-02-18 10:50:24 +0800142 auto leds = json.value("leds", empty);
143
144 for (const auto& entry : leds)
145 {
Alexander Hansen638d1482024-08-21 17:39:57 +0200146 loadJsonConfigV1Group(entry, ledMap);
George Liu616a0712021-02-18 10:50:24 +0800147 }
148
149 return ledMap;
150}
151
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500152/** @brief Load JSON config and return led map
153 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500154 * @return phosphor::led::GroupMap
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500155 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500156const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500157{
158 auto json = readJson(path);
159
160 auto version = json.value("version", 1);
161 switch (version)
162 {
163 case 1:
164 return loadJsonConfigV1(json);
165
166 default:
167 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
168 version);
169 throw std::runtime_error("Unsupported version");
170 }
171
Patrick Williams158b2c12022-03-17 05:57:44 -0500172 return phosphor::led::GroupMap{};
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500173}
174
George Liu616a0712021-02-18 10:50:24 +0800175/** @brief Get led map from LED groups JSON config
176 *
Patrick Williams7217c032022-03-16 16:26:09 -0500177 * @param[in] config - Path to the JSON config.
Patrick Williams158b2c12022-03-17 05:57:44 -0500178 * @return phosphor::led::GroupMap
Patrick Williams7217c032022-03-16 16:26:09 -0500179 *
180 * @note if config is an empty string, daemon will interrogate dbus for
181 * compatible strings.
George Liu616a0712021-02-18 10:50:24 +0800182 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500183const phosphor::led::GroupMap getSystemLedMap(fs::path config)
George Liu616a0712021-02-18 10:50:24 +0800184{
Patrick Williams7217c032022-03-16 16:26:09 -0500185 if (config.empty())
George Liu616a0712021-02-18 10:50:24 +0800186 {
Patrick Williams7217c032022-03-16 16:26:09 -0500187 config = phosphor::led::getJsonConfig();
George Liu616a0712021-02-18 10:50:24 +0800188 }
189
Patrick Williams7217c032022-03-16 16:26:09 -0500190 return loadJsonConfig(config);
George Liu616a0712021-02-18 10:50:24 +0800191}