xref: /openbmc/phosphor-led-manager/manager/json-parser.hpp (revision 7ba70c820ed1ff5c19ea2af337ecad82732f383d)
1953315d2SPatrick Williams #include "config.h"
2953315d2SPatrick Williams 
3*7ba70c82SAlexander Hansen #include "grouplayout.hpp"
4953315d2SPatrick Williams #include "json-config.hpp"
5953315d2SPatrick Williams #include "ledlayout.hpp"
6953315d2SPatrick Williams 
7953315d2SPatrick Williams #include <nlohmann/json.hpp>
8953315d2SPatrick Williams #include <phosphor-logging/lg2.hpp>
9953315d2SPatrick Williams #include <sdbusplus/bus.hpp>
10953315d2SPatrick Williams #include <sdeventplus/event.hpp>
11953315d2SPatrick Williams 
12953315d2SPatrick Williams #include <filesystem>
13953315d2SPatrick Williams #include <fstream>
14953315d2SPatrick Williams #include <iostream>
15953315d2SPatrick Williams 
16953315d2SPatrick Williams namespace fs = std::filesystem;
17953315d2SPatrick Williams 
18953315d2SPatrick Williams using Json = nlohmann::json;
19953315d2SPatrick Williams 
20953315d2SPatrick Williams // Priority for a particular LED needs to stay SAME across all groups
21953315d2SPatrick Williams // phosphor::led::Layout::Action can only be one of `Blink` and `On`
22f2044037SPatrick Williams using PriorityMap =
23f2044037SPatrick Williams     std::unordered_map<std::string, phosphor::led::Layout::Action>;
24953315d2SPatrick Williams 
25953315d2SPatrick Williams /** @brief Parse LED JSON file and output Json object
26953315d2SPatrick Williams  *
27953315d2SPatrick Williams  *  @param[in] path - path of LED JSON file
28953315d2SPatrick Williams  *
29953315d2SPatrick Williams  *  @return const Json - Json object
30953315d2SPatrick Williams  */
3173ff9ad6SPatrick Williams Json readJson(const fs::path& path)
32953315d2SPatrick Williams {
33953315d2SPatrick Williams     if (!fs::exists(path) || fs::is_empty(path))
34953315d2SPatrick Williams     {
35953315d2SPatrick Williams         lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}",
36953315d2SPatrick Williams                    "PATH", path);
37953315d2SPatrick Williams         throw std::runtime_error("Incorrect File Path or empty file");
38953315d2SPatrick Williams     }
39953315d2SPatrick Williams 
40953315d2SPatrick Williams     try
41953315d2SPatrick Williams     {
42953315d2SPatrick Williams         std::ifstream jsonFile(path);
43953315d2SPatrick Williams         return Json::parse(jsonFile);
44953315d2SPatrick Williams     }
45953315d2SPatrick Williams     catch (const std::exception& e)
46953315d2SPatrick Williams     {
47953315d2SPatrick Williams         lg2::error(
48953315d2SPatrick Williams             "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}",
49953315d2SPatrick Williams             "ERROR", e, "PATH", path);
50953315d2SPatrick Williams         throw std::runtime_error("Failed to parse config file");
51953315d2SPatrick Williams     }
52953315d2SPatrick Williams }
53953315d2SPatrick Williams 
54953315d2SPatrick Williams /** @brief Returns action enum based on string
55953315d2SPatrick Williams  *
56953315d2SPatrick Williams  *  @param[in] action - action string
57953315d2SPatrick Williams  *
58953315d2SPatrick Williams  *  @return Action - action enum (On/Blink)
59953315d2SPatrick Williams  */
60953315d2SPatrick Williams phosphor::led::Layout::Action getAction(const std::string& action)
61953315d2SPatrick Williams {
62953315d2SPatrick Williams     assert(action == "On" || action == "Blink");
63953315d2SPatrick Williams 
64ed80e885SPatrick Williams     return action == "Blink" ? phosphor::led::Layout::Action::Blink
65ed80e885SPatrick Williams                              : phosphor::led::Layout::Action::On;
66953315d2SPatrick Williams }
67953315d2SPatrick Williams 
68953315d2SPatrick Williams /** @brief Validate the Priority of an LED is same across ALL groups
69953315d2SPatrick Williams  *
70953315d2SPatrick Williams  *  @param[in] name - led name member of each group
71953315d2SPatrick Williams  *  @param[in] priority - member priority of each group
72f2044037SPatrick Williams  *  @param[out] priorityMap - std::unordered_map, key:name, value:priority
73953315d2SPatrick Williams  *
74953315d2SPatrick Williams  *  @return
75953315d2SPatrick Williams  */
76953315d2SPatrick Williams void validatePriority(const std::string& name,
77953315d2SPatrick Williams                       const phosphor::led::Layout::Action& priority,
78953315d2SPatrick Williams                       PriorityMap& priorityMap)
79953315d2SPatrick Williams {
80953315d2SPatrick Williams     auto iter = priorityMap.find(name);
81953315d2SPatrick Williams     if (iter == priorityMap.end())
82953315d2SPatrick Williams     {
83953315d2SPatrick Williams         priorityMap.emplace(name, priority);
84953315d2SPatrick Williams         return;
85953315d2SPatrick Williams     }
86953315d2SPatrick Williams 
87953315d2SPatrick Williams     if (iter->second != priority)
88953315d2SPatrick Williams     {
89953315d2SPatrick Williams         lg2::error(
90953315d2SPatrick Williams             "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}",
91953315d2SPatrick Williams             "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO",
92953315d2SPatrick Williams             int(priority));
93953315d2SPatrick Williams 
94953315d2SPatrick Williams         throw std::runtime_error(
95953315d2SPatrick Williams             "Priority of at least one LED is not same across groups");
96953315d2SPatrick Williams     }
97953315d2SPatrick Williams }
98953315d2SPatrick Williams 
99d0f80506SAlexander Hansen static void loadJsonConfigV1GroupMember(const Json& member,
100d0f80506SAlexander Hansen                                         PriorityMap& priorityMap,
101d0f80506SAlexander Hansen                                         phosphor::led::ActionSet& ledActions)
102d0f80506SAlexander Hansen {
103d0f80506SAlexander Hansen     auto name = member.value("Name", "");
104d0f80506SAlexander Hansen     auto action = getAction(member.value("Action", ""));
105d0f80506SAlexander Hansen     uint8_t dutyOn = member.value("DutyOn", 50);
106d0f80506SAlexander Hansen     uint16_t period = member.value("Period", 0);
107d0f80506SAlexander Hansen 
108d0f80506SAlexander Hansen     // Since only have Blink/On and default priority is Blink
109d0f80506SAlexander Hansen     auto priority = getAction(member.value("Priority", "Blink"));
110d0f80506SAlexander Hansen 
111d0f80506SAlexander Hansen     // Same LEDs can be part of multiple groups. However, their
112d0f80506SAlexander Hansen     // priorities across groups need to match.
113d0f80506SAlexander Hansen     validatePriority(name, priority, priorityMap);
114d0f80506SAlexander Hansen 
115d0f80506SAlexander Hansen     phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period,
116d0f80506SAlexander Hansen                                                priority};
117d0f80506SAlexander Hansen     ledActions.emplace(ledAction);
118d0f80506SAlexander Hansen }
119d0f80506SAlexander Hansen 
120d0f80506SAlexander Hansen static void loadJsonConfigV1Group(const Json& entry,
121d0f80506SAlexander Hansen                                   phosphor::led::GroupMap& ledMap,
122d0f80506SAlexander Hansen                                   PriorityMap& priorityMap)
123d0f80506SAlexander Hansen {
124d0f80506SAlexander Hansen     const Json empty{};
125d0f80506SAlexander Hansen 
126d0f80506SAlexander Hansen     fs::path tmpPath("/xyz/openbmc_project/led/groups");
127d0f80506SAlexander Hansen 
128d0f80506SAlexander Hansen     const std::string groupName = entry.value("group", "");
129d0f80506SAlexander Hansen 
130d0f80506SAlexander Hansen     tmpPath /= groupName;
131d0f80506SAlexander Hansen     auto objpath = tmpPath.string();
132d0f80506SAlexander Hansen     auto members = entry.value("members", empty);
133*7ba70c82SAlexander Hansen     int priority = entry.value("Priority", 0);
134d0f80506SAlexander Hansen 
135d0f80506SAlexander Hansen     lg2::debug("config for '{GROUP}'", "GROUP", groupName);
136d0f80506SAlexander Hansen 
137d0f80506SAlexander Hansen     phosphor::led::ActionSet ledActions{};
138*7ba70c82SAlexander Hansen     phosphor::led::Layout::GroupLayout groupLayout{};
139d0f80506SAlexander Hansen     for (const auto& member : members)
140d0f80506SAlexander Hansen     {
141d0f80506SAlexander Hansen         loadJsonConfigV1GroupMember(member, priorityMap, ledActions);
142d0f80506SAlexander Hansen     }
143d0f80506SAlexander Hansen 
144d0f80506SAlexander Hansen     // Generated an std::unordered_map of LedGroupNames to std::set of LEDs
145d0f80506SAlexander Hansen     // containing the name and properties.
146*7ba70c82SAlexander Hansen     groupLayout.actionSet = ledActions;
147*7ba70c82SAlexander Hansen     groupLayout.priority = priority;
148*7ba70c82SAlexander Hansen 
149*7ba70c82SAlexander Hansen     ledMap.emplace(objpath, groupLayout);
150d0f80506SAlexander Hansen }
151d0f80506SAlexander Hansen 
15273ff9ad6SPatrick Williams /** @brief Load JSON config and return led map (JSON version 1)
153953315d2SPatrick Williams  *
154158b2c14SPatrick Williams  *  @return phosphor::led::GroupMap
155953315d2SPatrick Williams  */
156158b2c14SPatrick Williams const phosphor::led::GroupMap loadJsonConfigV1(const Json& json)
157953315d2SPatrick Williams {
158158b2c14SPatrick Williams     phosphor::led::GroupMap ledMap{};
159953315d2SPatrick Williams     PriorityMap priorityMap{};
160953315d2SPatrick Williams 
161953315d2SPatrick Williams     // define the default JSON as empty
162953315d2SPatrick Williams     const Json empty{};
163953315d2SPatrick Williams     auto leds = json.value("leds", empty);
164953315d2SPatrick Williams 
165953315d2SPatrick Williams     for (const auto& entry : leds)
166953315d2SPatrick Williams     {
167d0f80506SAlexander Hansen         loadJsonConfigV1Group(entry, ledMap, priorityMap);
168953315d2SPatrick Williams     }
169953315d2SPatrick Williams 
170953315d2SPatrick Williams     return ledMap;
171953315d2SPatrick Williams }
172953315d2SPatrick Williams 
17373ff9ad6SPatrick Williams /** @brief Load JSON config and return led map
17473ff9ad6SPatrick Williams  *
175158b2c14SPatrick Williams  *  @return phosphor::led::GroupMap
17673ff9ad6SPatrick Williams  */
177158b2c14SPatrick Williams const phosphor::led::GroupMap loadJsonConfig(const fs::path& path)
17873ff9ad6SPatrick Williams {
17973ff9ad6SPatrick Williams     auto json = readJson(path);
18073ff9ad6SPatrick Williams 
18173ff9ad6SPatrick Williams     auto version = json.value("version", 1);
18273ff9ad6SPatrick Williams     switch (version)
18373ff9ad6SPatrick Williams     {
18473ff9ad6SPatrick Williams         case 1:
18573ff9ad6SPatrick Williams             return loadJsonConfigV1(json);
18673ff9ad6SPatrick Williams 
18773ff9ad6SPatrick Williams         default:
18873ff9ad6SPatrick Williams             lg2::error("Unsupported JSON Version: {VERSION}", "VERSION",
18973ff9ad6SPatrick Williams                        version);
19073ff9ad6SPatrick Williams             throw std::runtime_error("Unsupported version");
19173ff9ad6SPatrick Williams     }
19273ff9ad6SPatrick Williams 
193158b2c14SPatrick Williams     return phosphor::led::GroupMap{};
19473ff9ad6SPatrick Williams }
19573ff9ad6SPatrick Williams 
196953315d2SPatrick Williams /** @brief Get led map from LED groups JSON config
197953315d2SPatrick Williams  *
1987217c035SPatrick Williams  *  @param[in] config - Path to the JSON config.
199158b2c14SPatrick Williams  *  @return phosphor::led::GroupMap
2007217c035SPatrick Williams  *
2017217c035SPatrick Williams  *  @note if config is an empty string, daemon will interrogate dbus for
2027217c035SPatrick Williams  *        compatible strings.
203953315d2SPatrick Williams  */
204158b2c14SPatrick Williams const phosphor::led::GroupMap getSystemLedMap(fs::path config)
205953315d2SPatrick Williams {
2067217c035SPatrick Williams     if (config.empty())
207953315d2SPatrick Williams     {
2087217c035SPatrick Williams         config = phosphor::led::getJsonConfig();
209953315d2SPatrick Williams     }
210953315d2SPatrick Williams 
2117217c035SPatrick Williams     return loadJsonConfig(config);
212953315d2SPatrick Williams }
213