blob: a18db5de39b45f385b23e628ba7d8731187a099b [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;
18using LedAction = std::set<phosphor::led::Layout::LedAction>;
19using LedMap = std::map<std::string, LedAction>;
20
21// Priority for a particular LED needs to stay SAME across all groups
22// phosphor::led::Layout::Action can only be one of `Blink` and `On`
23using PriorityMap = std::map<std::string, phosphor::led::Layout::Action>;
24
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 */
31const Json readJson(const fs::path& path)
32{
George Liu616a0712021-02-18 10:50:24 +080033
34 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
65 return action == "Blink" ? phosphor::led::Layout::Blink
66 : phosphor::led::Layout::On;
67}
68
69/** @brief Validate the Priority of an LED is same across ALL groups
70 *
71 * @param[in] name - led name member of each group
72 * @param[in] priority - member priority of each group
73 * @param[out] priorityMap - std::map, key:name, value:priority
74 *
75 * @return
76 */
77void validatePriority(const std::string& name,
78 const phosphor::led::Layout::Action& priority,
79 PriorityMap& priorityMap)
80{
George Liu616a0712021-02-18 10:50:24 +080081
82 auto iter = priorityMap.find(name);
83 if (iter == priorityMap.end())
84 {
85 priorityMap.emplace(name, priority);
86 return;
87 }
88
89 if (iter->second != priority)
90 {
George Liue9fb5c62021-07-01 14:05:32 +080091 lg2::error(
92 "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
93 "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO",
94 int(priority));
George Liu616a0712021-02-18 10:50:24 +080095
96 throw std::runtime_error(
97 "Priority of at least one LED is not same across groups");
98 }
99}
100
101/** @brief Load JSON config and return led map
102 *
103 * @return LedMap - Generated an std::map of LedAction
104 */
105const LedMap loadJsonConfig(const fs::path& path)
106{
107 LedMap ledMap{};
108 PriorityMap priorityMap{};
109
110 // define the default JSON as empty
111 const Json empty{};
112 auto json = readJson(path);
113 auto leds = json.value("leds", empty);
114
115 for (const auto& entry : leds)
116 {
117 fs::path tmpPath(std::string{OBJPATH});
118 tmpPath /= entry.value("group", "");
119 auto objpath = tmpPath.string();
120 auto members = entry.value("members", empty);
121
122 LedAction ledActions{};
123 for (const auto& member : members)
124 {
125 auto name = member.value("Name", "");
126 auto action = getAction(member.value("Action", ""));
127 uint8_t dutyOn = member.value("DutyOn", 50);
128 uint16_t period = member.value("Period", 0);
129
130 // Since only have Blink/On and default priority is Blink
131 auto priority = getAction(member.value("Priority", "Blink"));
132
133 // Same LEDs can be part of multiple groups. However, their
134 // priorities across groups need to match.
135 validatePriority(name, priority, priorityMap);
136
137 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn,
138 period, priority};
139 ledActions.emplace(ledAction);
140 }
141
142 // Generated an std::map of LedGroupNames to std::set of LEDs
143 // containing the name and properties.
144 ledMap.emplace(objpath, ledActions);
145 }
146
147 return ledMap;
148}
149
150/** @brief Get led map from LED groups JSON config
151 *
152 * @return LedMap - Generated an std::map of LedAction
153 */
154const LedMap getSystemLedMap()
155{
156 // Get a new Dbus
157 auto bus = sdbusplus::bus::new_bus();
158
159 // Get a new event loop
160 auto event = sdeventplus::Event::get_new();
161
162 // Attach the bus to sd_event to service user requests
163 bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
164 phosphor::led::JsonConfig jsonConfig(bus, event);
165
166 // The event loop will be terminated from inside of a function in JsonConfig
167 // after finding the configuration file
168 if (jsonConfig.getConfFile().empty())
169 {
170 event.loop();
171 }
172
173 // Detach the bus from its sd_event event loop object
174 bus.detach_event();
175
176 return loadJsonConfig(jsonConfig.getConfFile());
177}