George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "json-config.hpp" |
| 4 | #include "ledlayout.hpp" |
| 5 | |
| 6 | #include <nlohmann/json.hpp> |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
| 9 | #include <sdeventplus/event.hpp> |
| 10 | |
| 11 | #include <filesystem> |
| 12 | #include <fstream> |
| 13 | #include <iostream> |
| 14 | |
| 15 | namespace fs = std::filesystem; |
| 16 | |
| 17 | using Json = nlohmann::json; |
| 18 | using LedAction = std::set<phosphor::led::Layout::LedAction>; |
| 19 | using 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` |
| 23 | using 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 | */ |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 31 | Json readJson(const fs::path& path) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 32 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 33 | |
| 34 | if (!fs::exists(path) || fs::is_empty(path)) |
| 35 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 36 | lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", |
| 37 | "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 48 | lg2::error( |
| 49 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 50 | "ERROR", e, "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame^] | 65 | return action == "Blink" ? phosphor::led::Layout::Action::Blink |
| 66 | : phosphor::led::Layout::Action::On; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | */ |
| 77 | void validatePriority(const std::string& name, |
| 78 | const phosphor::led::Layout::Action& priority, |
| 79 | PriorityMap& priorityMap) |
| 80 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 81 | |
| 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 Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 91 | 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 Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 95 | |
| 96 | throw std::runtime_error( |
| 97 | "Priority of at least one LED is not same across groups"); |
| 98 | } |
| 99 | } |
| 100 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 101 | /** @brief Load JSON config and return led map (JSON version 1) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 102 | * |
| 103 | * @return LedMap - Generated an std::map of LedAction |
| 104 | */ |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 105 | const LedMap loadJsonConfigV1(const Json& json) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 106 | { |
| 107 | LedMap ledMap{}; |
| 108 | PriorityMap priorityMap{}; |
| 109 | |
| 110 | // define the default JSON as empty |
| 111 | const Json empty{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 112 | auto leds = json.value("leds", empty); |
| 113 | |
| 114 | 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 | { |
| 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 | // Since only have Blink/On and default priority is Blink |
| 130 | auto priority = getAction(member.value("Priority", "Blink")); |
| 131 | |
| 132 | // Same LEDs can be part of multiple groups. However, their |
| 133 | // priorities across groups need to match. |
| 134 | validatePriority(name, priority, priorityMap); |
| 135 | |
| 136 | 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 | } |
| 148 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 149 | /** @brief Load JSON config and return led map |
| 150 | * |
| 151 | * @return LedMap - Generated an std::map of LedAction |
| 152 | */ |
| 153 | const LedMap loadJsonConfig(const fs::path& path) |
| 154 | { |
| 155 | auto json = readJson(path); |
| 156 | |
| 157 | auto version = json.value("version", 1); |
| 158 | switch (version) |
| 159 | { |
| 160 | case 1: |
| 161 | return loadJsonConfigV1(json); |
| 162 | |
| 163 | default: |
| 164 | lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", |
| 165 | version); |
| 166 | throw std::runtime_error("Unsupported version"); |
| 167 | } |
| 168 | |
| 169 | return LedMap{}; |
| 170 | } |
| 171 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 172 | /** @brief Get led map from LED groups JSON config |
| 173 | * |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 174 | * @param[in] config - Path to the JSON config. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 175 | * @return LedMap - Generated an std::map of LedAction |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 176 | * |
| 177 | * @note if config is an empty string, daemon will interrogate dbus for |
| 178 | * compatible strings. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 179 | */ |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 180 | const LedMap getSystemLedMap(fs::path config) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 181 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 182 | if (config.empty()) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 183 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 184 | config = phosphor::led::getJsonConfig(); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 187 | return loadJsonConfig(config); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 188 | } |