blob: e0448c56f5841cd4984f1f8c50e639a007d8a206 [file] [log] [blame]
George Liudef5f5a2020-04-10 11:23:52 +08001#include "config.h"
2
3#include "ledlayout.hpp"
4
5#include <nlohmann/json.hpp>
6#include <phosphor-logging/log.hpp>
7
8#include <filesystem>
9#include <fstream>
10#include <iostream>
11
12namespace fs = std::filesystem;
13
14using Json = nlohmann::json;
15using LedAction = std::set<phosphor::led::Layout::LedAction>;
16using LedMap = std::map<std::string, LedAction>;
17
George Liu17fcb4d2020-06-30 18:12:53 +080018// Priority for a particular LED needs to stay SAME across all groups
19// phosphor::led::Layout::Action can only be one of `Blink` and `On`
20using PriorityMap = std::map<std::string, phosphor::led::Layout::Action>;
21
George Liudef5f5a2020-04-10 11:23:52 +080022/** @brief Parse LED JSON file and output Json object
23 *
24 * @param[in] path - path of LED JSON file
25 *
26 * @return const Json - Json object
27 */
28const Json readJson(const fs::path& path)
29{
30 using namespace phosphor::logging;
31
32 if (!fs::exists(path) || fs::is_empty(path))
33 {
34 log<level::ERR>("Incorrect File Path or empty file",
35 entry("FILE_PATH=%s", path.c_str()));
36 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 {
46 log<level::ERR>("Failed to parse config file",
47 entry("ERROR=%s", e.what()),
48 entry("FILE_PATH=%s", path.c_str()));
49 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
63 return action == "Blink" ? phosphor::led::Layout::Blink
64 : phosphor::led::Layout::On;
65}
66
George Liu17fcb4d2020-06-30 18:12:53 +080067/** @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
71 * @param[out] priorityMap - std::map, key:name, value:priority
72 *
73 * @return
74 */
75void validatePriority(const std::string& name,
76 const phosphor::led::Layout::Action& priority,
77 PriorityMap& priorityMap)
78{
79 using namespace phosphor::logging;
80
81 auto iter = priorityMap.find(name);
82 if (iter == priorityMap.end())
83 {
84 priorityMap.emplace(name, priority);
85 return;
86 }
87
88 if (iter->second != priority)
89 {
90 log<level::ERR>("Priority of LED is not same across all",
91 entry("Name=%s", name.c_str()),
92 entry(" Old Priority=%d", iter->second),
93 entry(" New priority=%d", priority));
94
95 throw std::runtime_error(
96 "Priority of at least one LED is not same across groups");
97 }
98}
99
George Liudef5f5a2020-04-10 11:23:52 +0800100/** @brief Load JSON config and return led map
101 *
102 * @return LedMap - Generated an std::map of LedAction
103 */
104const LedMap loadJsonConfig(const fs::path& path)
105{
106 LedMap ledMap{};
George Liu17fcb4d2020-06-30 18:12:53 +0800107 PriorityMap priorityMap{};
George Liudef5f5a2020-04-10 11:23:52 +0800108
109 // define the default JSON as empty
110 const Json empty{};
111 auto json = readJson(path);
George Liudef5f5a2020-04-10 11:23:52 +0800112 auto leds = json.value("leds", empty);
George Liu17fcb4d2020-06-30 18:12:53 +0800113
George Liudef5f5a2020-04-10 11:23:52 +0800114 for (const auto& entry : leds)
115 {
116 fs::path tmpPath(std::string{OBJPATH});
117 tmpPath /= entry.value("group", "");
118 auto objpath = tmpPath.string();
119 auto members = entry.value("members", empty);
120
121 LedAction ledActions{};
122 for (const auto& member : members)
123 {
George Liue5c366f2020-07-02 18:04:05 +0800124 auto name = member.value("Name", "");
George Liudef5f5a2020-04-10 11:23:52 +0800125 auto action = getAction(member.value("Action", ""));
126 uint8_t dutyOn = member.value("DutyOn", 50);
127 uint16_t period = member.value("Period", 0);
128
129 // Since only have Blink/On and default priority is Blink
130 auto priority = getAction(member.value("Priority", "Blink"));
George Liu17fcb4d2020-06-30 18:12:53 +0800131
132 // Same LEDs can be part of multiple groups. However, their
133 // priorities across groups need to match.
134 validatePriority(name, priority, priorityMap);
135
George Liudef5f5a2020-04-10 11:23:52 +0800136 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn,
137 period, priority};
138 ledActions.emplace(ledAction);
139 }
140
141 // Generated an std::map of LedGroupNames to std::set of LEDs
142 // containing the name and properties.
143 ledMap.emplace(objpath, ledActions);
144 }
145
146 return ledMap;
147}