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>; |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 19 | using LedMap = std::unordered_map<std::string, LedAction>; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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` |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 23 | using PriorityMap = |
| 24 | std::unordered_map<std::string, phosphor::led::Layout::Action>; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | */ |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 32 | Json readJson(const fs::path& path) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 33 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 34 | |
| 35 | if (!fs::exists(path) || fs::is_empty(path)) |
| 36 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 37 | lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", |
| 38 | "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 39 | throw std::runtime_error("Incorrect File Path or empty file"); |
| 40 | } |
| 41 | |
| 42 | try |
| 43 | { |
| 44 | std::ifstream jsonFile(path); |
| 45 | return Json::parse(jsonFile); |
| 46 | } |
| 47 | catch (const std::exception& e) |
| 48 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 49 | lg2::error( |
| 50 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 51 | "ERROR", e, "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 52 | throw std::runtime_error("Failed to parse config file"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** @brief Returns action enum based on string |
| 57 | * |
| 58 | * @param[in] action - action string |
| 59 | * |
| 60 | * @return Action - action enum (On/Blink) |
| 61 | */ |
| 62 | phosphor::led::Layout::Action getAction(const std::string& action) |
| 63 | { |
| 64 | assert(action == "On" || action == "Blink"); |
| 65 | |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 66 | return action == "Blink" ? phosphor::led::Layout::Action::Blink |
| 67 | : phosphor::led::Layout::Action::On; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** @brief Validate the Priority of an LED is same across ALL groups |
| 71 | * |
| 72 | * @param[in] name - led name member of each group |
| 73 | * @param[in] priority - member priority of each group |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 74 | * @param[out] priorityMap - std::unordered_map, key:name, value:priority |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 75 | * |
| 76 | * @return |
| 77 | */ |
| 78 | void validatePriority(const std::string& name, |
| 79 | const phosphor::led::Layout::Action& priority, |
| 80 | PriorityMap& priorityMap) |
| 81 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 82 | |
| 83 | auto iter = priorityMap.find(name); |
| 84 | if (iter == priorityMap.end()) |
| 85 | { |
| 86 | priorityMap.emplace(name, priority); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (iter->second != priority) |
| 91 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 92 | lg2::error( |
| 93 | "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", |
| 94 | "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO", |
| 95 | int(priority)); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 96 | |
| 97 | throw std::runtime_error( |
| 98 | "Priority of at least one LED is not same across groups"); |
| 99 | } |
| 100 | } |
| 101 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 102 | /** @brief Load JSON config and return led map (JSON version 1) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 103 | * |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 104 | * @return LedMap - Generated an std::unordered_map of LedAction |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 105 | */ |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 106 | const LedMap loadJsonConfigV1(const Json& json) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 107 | { |
| 108 | LedMap ledMap{}; |
| 109 | PriorityMap priorityMap{}; |
| 110 | |
| 111 | // define the default JSON as empty |
| 112 | const Json empty{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 142 | // Generated an std::unordered_map of LedGroupNames to std::set of LEDs |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 143 | // containing the name and properties. |
| 144 | ledMap.emplace(objpath, ledActions); |
| 145 | } |
| 146 | |
| 147 | return ledMap; |
| 148 | } |
| 149 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 150 | /** @brief Load JSON config and return led map |
| 151 | * |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 152 | * @return LedMap - Generated an std::unordered_map of LedAction |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 153 | */ |
| 154 | const LedMap loadJsonConfig(const fs::path& path) |
| 155 | { |
| 156 | auto json = readJson(path); |
| 157 | |
| 158 | auto version = json.value("version", 1); |
| 159 | switch (version) |
| 160 | { |
| 161 | case 1: |
| 162 | return loadJsonConfigV1(json); |
| 163 | |
| 164 | default: |
| 165 | lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", |
| 166 | version); |
| 167 | throw std::runtime_error("Unsupported version"); |
| 168 | } |
| 169 | |
| 170 | return LedMap{}; |
| 171 | } |
| 172 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 173 | /** @brief Get led map from LED groups JSON config |
| 174 | * |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 175 | * @param[in] config - Path to the JSON config. |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame^] | 176 | * @return LedMap - Generated an std::unordered_map of LedAction |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 177 | * |
| 178 | * @note if config is an empty string, daemon will interrogate dbus for |
| 179 | * compatible strings. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 180 | */ |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 181 | const LedMap getSystemLedMap(fs::path config) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 182 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 183 | if (config.empty()) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 184 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 185 | config = phosphor::led::getJsonConfig(); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 188 | return loadJsonConfig(config); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 189 | } |