George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 3 | #include "grouplayout.hpp" |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 4 | #include "json-config.hpp" |
| 5 | #include "ledlayout.hpp" |
| 6 | |
| 7 | #include <nlohmann/json.hpp> |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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` |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 22 | using PriorityMap = |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame^] | 23 | std::unordered_map<std::string, |
| 24 | std::optional<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 | 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 | |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame^] | 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 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 92 | * @param[out] priorityMap - std::unordered_map, key:name, value:priority |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 93 | * |
| 94 | * @return |
| 95 | */ |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame^] | 96 | void validatePriority( |
| 97 | const std::string& name, |
| 98 | const std::optional<phosphor::led::Layout::Action>& priority, |
| 99 | PriorityMap& priorityMap) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 100 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 110 | lg2::error( |
| 111 | "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame^] | 112 | "NAME", name, "OLD_PRIO", priorityToString(iter->second), |
| 113 | "NEW_PRIO", priorityToString(priority)); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 114 | |
| 115 | throw std::runtime_error( |
| 116 | "Priority of at least one LED is not same across groups"); |
| 117 | } |
| 118 | } |
| 119 | |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 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 | |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame^] | 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 | } |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 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); |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 159 | int priority = entry.value("Priority", 0); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 160 | |
| 161 | lg2::debug("config for '{GROUP}'", "GROUP", groupName); |
| 162 | |
| 163 | phosphor::led::ActionSet ledActions{}; |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 164 | phosphor::led::Layout::GroupLayout groupLayout{}; |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 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. |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 172 | groupLayout.actionSet = ledActions; |
| 173 | groupLayout.priority = priority; |
| 174 | |
| 175 | ledMap.emplace(objpath, groupLayout); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 178 | /** @brief Load JSON config and return led map (JSON version 1) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 179 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 180 | * @return phosphor::led::GroupMap |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 181 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 182 | const phosphor::led::GroupMap loadJsonConfigV1(const Json& json) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 183 | { |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 184 | phosphor::led::GroupMap ledMap{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 185 | PriorityMap priorityMap{}; |
| 186 | |
| 187 | // define the default JSON as empty |
| 188 | const Json empty{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 189 | auto leds = json.value("leds", empty); |
| 190 | |
| 191 | for (const auto& entry : leds) |
| 192 | { |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 193 | loadJsonConfigV1Group(entry, ledMap, priorityMap); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return ledMap; |
| 197 | } |
| 198 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 199 | /** @brief Load JSON config and return led map |
| 200 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 201 | * @return phosphor::led::GroupMap |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 202 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 203 | const phosphor::led::GroupMap loadJsonConfig(const fs::path& path) |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 219 | return phosphor::led::GroupMap{}; |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 220 | } |
| 221 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 222 | /** @brief Get led map from LED groups JSON config |
| 223 | * |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 224 | * @param[in] config - Path to the JSON config. |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 225 | * @return phosphor::led::GroupMap |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 226 | * |
| 227 | * @note if config is an empty string, daemon will interrogate dbus for |
| 228 | * compatible strings. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 229 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 230 | const phosphor::led::GroupMap getSystemLedMap(fs::path config) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 231 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 232 | if (config.empty()) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 233 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 234 | config = phosphor::led::getJsonConfig(); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 235 | } |
| 236 | |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 237 | return loadJsonConfig(config); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 238 | } |