blob: 30a06a424531809ec62eb401e3ab0b1bb26f0d96 [file] [log] [blame]
George Liu616a0712021-02-18 10:50:24 +08001#include "config.h"
2
Alexander Hansen638d1482024-08-21 17:39:57 +02003#include "config-validator.hpp"
Alexander Hansen7ba70c82024-07-23 13:46:25 +02004#include "grouplayout.hpp"
George Liu616a0712021-02-18 10:50:24 +08005#include "json-config.hpp"
6#include "ledlayout.hpp"
7
8#include <nlohmann/json.hpp>
George Liue9fb5c62021-07-01 14:05:32 +08009#include <phosphor-logging/lg2.hpp>
George Liu616a0712021-02-18 10:50:24 +080010#include <sdbusplus/bus.hpp>
11#include <sdeventplus/event.hpp>
12
13#include <filesystem>
14#include <fstream>
15#include <iostream>
16
17namespace fs = std::filesystem;
18
19using Json = nlohmann::json;
George Liu616a0712021-02-18 10:50:24 +080020
21// Priority for a particular LED needs to stay SAME across all groups
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 *
Alexander Hansen4d44a552024-07-24 14:55:24 +020059 * @return Action - action enum (On/Off/Blink)
George Liu616a0712021-02-18 10:50:24 +080060 */
61phosphor::led::Layout::Action getAction(const std::string& action)
62{
Alexander Hansen4d44a552024-07-24 14:55:24 +020063 if (action == "On")
64 {
65 return phosphor::led::Layout::Action::On;
66 }
67 if (action == "Off")
68 {
69 return phosphor::led::Layout::Action::Off;
70 }
71 if (action == "Blink")
72 {
73 return phosphor::led::Layout::Action::Blink;
74 }
George Liu616a0712021-02-18 10:50:24 +080075
Alexander Hansen4d44a552024-07-24 14:55:24 +020076 assert(false);
77 return phosphor::led::Layout::Action::Blink;
George Liu616a0712021-02-18 10:50:24 +080078}
79
Alexander Hansend0f80502024-07-23 12:12:54 +020080static void loadJsonConfigV1GroupMember(const Json& member,
Alexander Hansend0f80502024-07-23 12:12:54 +020081 phosphor::led::ActionSet& ledActions)
82{
83 auto name = member.value("Name", "");
84 auto action = getAction(member.value("Action", ""));
85 uint8_t dutyOn = member.value("DutyOn", 50);
86 uint16_t period = member.value("Period", 0);
87
Alexander Hansen55badf72024-07-24 14:35:13 +020088 const std::string priorityStr = member.value("Priority", "");
89 std::optional<phosphor::led::Layout::Action> priority = std::nullopt;
90
91 if (!priorityStr.empty())
92 {
93 priority = getAction(priorityStr);
94 }
Alexander Hansend0f80502024-07-23 12:12:54 +020095
Alexander Hansend0f80502024-07-23 12:12:54 +020096 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
97 priority};
98 ledActions.emplace(ledAction);
99}
100
101static void loadJsonConfigV1Group(const Json& entry,
Alexander Hansen638d1482024-08-21 17:39:57 +0200102 phosphor::led::GroupMap& ledMap)
Alexander Hansend0f80502024-07-23 12:12:54 +0200103{
104 const Json empty{};
105
106 fs::path tmpPath("/xyz/openbmc_project/led/groups");
107
108 const std::string groupName = entry.value("group", "");
109
110 tmpPath /= groupName;
111 auto objpath = tmpPath.string();
112 auto members = entry.value("members", empty);
113
114 lg2::debug("config for '{GROUP}'", "GROUP", groupName);
115
Alexander Hansen638d1482024-08-21 17:39:57 +0200116 int priority = entry.value("Priority", 0);
117
Alexander Hansend0f80502024-07-23 12:12:54 +0200118 phosphor::led::ActionSet ledActions{};
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200119 phosphor::led::Layout::GroupLayout groupLayout{};
Alexander Hansend0f80502024-07-23 12:12:54 +0200120 for (const auto& member : members)
121 {
Alexander Hansen638d1482024-08-21 17:39:57 +0200122 loadJsonConfigV1GroupMember(member, ledActions);
Alexander Hansend0f80502024-07-23 12:12:54 +0200123 }
124
125 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
126 // containing the name and properties.
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200127 groupLayout.actionSet = ledActions;
128 groupLayout.priority = priority;
129
130 ledMap.emplace(objpath, groupLayout);
Alexander Hansend0f80502024-07-23 12:12:54 +0200131}
132
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500133/** @brief Load JSON config and return led map (JSON version 1)
George Liu616a0712021-02-18 10:50:24 +0800134 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500135 * @return phosphor::led::GroupMap
George Liu616a0712021-02-18 10:50:24 +0800136 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500137const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
George Liu616a0712021-02-18 10:50:24 +0800138{
Patrick Williams158b2c12022-03-17 05:57:44 -0500139 phosphor::led::GroupMap ledMap{};
George Liu616a0712021-02-18 10:50:24 +0800140
141 // define the default JSON as empty
142 const Json empty{};
George Liu616a0712021-02-18 10:50:24 +0800143 auto leds = json.value("leds", empty);
144
145 for (const auto& entry : leds)
146 {
Alexander Hansen638d1482024-08-21 17:39:57 +0200147 loadJsonConfigV1Group(entry, ledMap);
George Liu616a0712021-02-18 10:50:24 +0800148 }
149
150 return ledMap;
151}
152
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500153/** @brief Load JSON config and return led map
154 *
Patrick Williams158b2c12022-03-17 05:57:44 -0500155 * @return phosphor::led::GroupMap
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500156 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500157const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500158{
159 auto json = readJson(path);
160
161 auto version = json.value("version", 1);
162 switch (version)
163 {
164 case 1:
165 return loadJsonConfigV1(json);
166
167 default:
168 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
169 version);
170 throw std::runtime_error("Unsupported version");
171 }
172
Patrick Williams158b2c12022-03-17 05:57:44 -0500173 return phosphor::led::GroupMap{};
Patrick Williams73ff9ad2022-03-16 16:27:21 -0500174}
175
George Liu616a0712021-02-18 10:50:24 +0800176/** @brief Get led map from LED groups JSON config
177 *
Patrick Williams7217c032022-03-16 16:26:09 -0500178 * @param[in] config - Path to the JSON config.
Patrick Williams158b2c12022-03-17 05:57:44 -0500179 * @return phosphor::led::GroupMap
Patrick Williams7217c032022-03-16 16:26:09 -0500180 *
181 * @note if config is an empty string, daemon will interrogate dbus for
182 * compatible strings.
George Liu616a0712021-02-18 10:50:24 +0800183 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500184const phosphor::led::GroupMap getSystemLedMap(fs::path config)
George Liu616a0712021-02-18 10:50:24 +0800185{
Patrick Williams7217c032022-03-16 16:26:09 -0500186 if (config.empty())
George Liu616a0712021-02-18 10:50:24 +0800187 {
Patrick Williams7217c032022-03-16 16:26:09 -0500188 config = phosphor::led::getJsonConfig();
George Liu616a0712021-02-18 10:50:24 +0800189 }
190
Patrick Williams7217c032022-03-16 16:26:09 -0500191 return loadJsonConfig(config);
George Liu616a0712021-02-18 10:50:24 +0800192}