1 #include "config.h" 2 3 #include "grouplayout.hpp" 4 #include "json-config.hpp" 5 #include "ledlayout.hpp" 6 7 #include <nlohmann/json.hpp> 8 #include <phosphor-logging/lg2.hpp> 9 #include <sdbusplus/bus.hpp> 10 #include <sdeventplus/event.hpp> 11 12 #include <filesystem> 13 #include <fstream> 14 #include <iostream> 15 16 namespace fs = std::filesystem; 17 18 using Json = nlohmann::json; 19 20 // Priority for a particular LED needs to stay SAME across all groups 21 // phosphor::led::Layout::Action can only be one of `Blink` and `On` 22 using PriorityMap = 23 std::unordered_map<std::string, 24 std::optional<phosphor::led::Layout::Action>>; 25 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 */ 32 Json readJson(const fs::path& path) 33 { 34 if (!fs::exists(path) || fs::is_empty(path)) 35 { 36 lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", 37 "PATH", path); 38 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 { 48 lg2::error( 49 "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", 50 "ERROR", e, "PATH", path); 51 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 */ 61 phosphor::led::Layout::Action getAction(const std::string& action) 62 { 63 assert(action == "On" || action == "Blink"); 64 65 return action == "Blink" ? phosphor::led::Layout::Action::Blink 66 : phosphor::led::Layout::Action::On; 67 } 68 69 static std::string priorityToString( 70 const std::optional<phosphor::led::Layout::Action>& priority) 71 { 72 if (!priority.has_value()) 73 { 74 return "none"; 75 } 76 switch (priority.value()) 77 { 78 case phosphor::led::Layout::Action::Off: 79 return "Off"; 80 case phosphor::led::Layout::Action::On: 81 return "On"; 82 case phosphor::led::Layout::Action::Blink: 83 return "Blink"; 84 } 85 return "?"; 86 } 87 88 /** @brief Validate the Priority of an LED is same across ALL groups 89 * 90 * @param[in] name - led name member of each group 91 * @param[in] priority - member priority of each group 92 * @param[out] priorityMap - std::unordered_map, key:name, value:priority 93 * 94 * @return 95 */ 96 void validatePriority( 97 const std::string& name, 98 const std::optional<phosphor::led::Layout::Action>& priority, 99 PriorityMap& priorityMap) 100 { 101 auto iter = priorityMap.find(name); 102 if (iter == priorityMap.end()) 103 { 104 priorityMap.emplace(name, priority); 105 return; 106 } 107 108 if (iter->second != priority) 109 { 110 lg2::error( 111 "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", 112 "NAME", name, "OLD_PRIO", priorityToString(iter->second), 113 "NEW_PRIO", priorityToString(priority)); 114 115 throw std::runtime_error( 116 "Priority of at least one LED is not same across groups"); 117 } 118 } 119 120 static void loadJsonConfigV1GroupMember(const Json& member, 121 PriorityMap& priorityMap, 122 phosphor::led::ActionSet& ledActions) 123 { 124 auto name = member.value("Name", ""); 125 auto action = getAction(member.value("Action", "")); 126 uint8_t dutyOn = member.value("DutyOn", 50); 127 uint16_t period = member.value("Period", 0); 128 129 const std::string priorityStr = member.value("Priority", ""); 130 std::optional<phosphor::led::Layout::Action> priority = std::nullopt; 131 132 if (!priorityStr.empty()) 133 { 134 priority = getAction(priorityStr); 135 } 136 137 // Same LEDs can be part of multiple groups. However, their 138 // priorities across groups need to match. 139 validatePriority(name, priority, priorityMap); 140 141 phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period, 142 priority}; 143 ledActions.emplace(ledAction); 144 } 145 146 static void loadJsonConfigV1Group(const Json& entry, 147 phosphor::led::GroupMap& ledMap, 148 PriorityMap& priorityMap) 149 { 150 const Json empty{}; 151 152 fs::path tmpPath("/xyz/openbmc_project/led/groups"); 153 154 const std::string groupName = entry.value("group", ""); 155 156 tmpPath /= groupName; 157 auto objpath = tmpPath.string(); 158 auto members = entry.value("members", empty); 159 int priority = entry.value("Priority", 0); 160 161 lg2::debug("config for '{GROUP}'", "GROUP", groupName); 162 163 phosphor::led::ActionSet ledActions{}; 164 phosphor::led::Layout::GroupLayout groupLayout{}; 165 for (const auto& member : members) 166 { 167 loadJsonConfigV1GroupMember(member, priorityMap, ledActions); 168 } 169 170 // Generated an std::unordered_map of LedGroupNames to std::set of LEDs 171 // containing the name and properties. 172 groupLayout.actionSet = ledActions; 173 groupLayout.priority = priority; 174 175 ledMap.emplace(objpath, groupLayout); 176 } 177 178 /** @brief Load JSON config and return led map (JSON version 1) 179 * 180 * @return phosphor::led::GroupMap 181 */ 182 const phosphor::led::GroupMap loadJsonConfigV1(const Json& json) 183 { 184 phosphor::led::GroupMap ledMap{}; 185 PriorityMap priorityMap{}; 186 187 // define the default JSON as empty 188 const Json empty{}; 189 auto leds = json.value("leds", empty); 190 191 for (const auto& entry : leds) 192 { 193 loadJsonConfigV1Group(entry, ledMap, priorityMap); 194 } 195 196 return ledMap; 197 } 198 199 /** @brief Load JSON config and return led map 200 * 201 * @return phosphor::led::GroupMap 202 */ 203 const phosphor::led::GroupMap loadJsonConfig(const fs::path& path) 204 { 205 auto json = readJson(path); 206 207 auto version = json.value("version", 1); 208 switch (version) 209 { 210 case 1: 211 return loadJsonConfigV1(json); 212 213 default: 214 lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", 215 version); 216 throw std::runtime_error("Unsupported version"); 217 } 218 219 return phosphor::led::GroupMap{}; 220 } 221 222 /** @brief Get led map from LED groups JSON config 223 * 224 * @param[in] config - Path to the JSON config. 225 * @return phosphor::led::GroupMap 226 * 227 * @note if config is an empty string, daemon will interrogate dbus for 228 * compatible strings. 229 */ 230 const phosphor::led::GroupMap getSystemLedMap(fs::path config) 231 { 232 if (config.empty()) 233 { 234 config = phosphor::led::getJsonConfig(); 235 } 236 237 return loadJsonConfig(config); 238 } 239