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 = |
| 23 | std::unordered_map<std::string, phosphor::led::Layout::Action>; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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 | if (!fs::exists(path) || fs::is_empty(path)) |
| 34 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 35 | lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", |
| 36 | "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 37 | throw std::runtime_error("Incorrect File Path or empty file"); |
| 38 | } |
| 39 | |
| 40 | try |
| 41 | { |
| 42 | std::ifstream jsonFile(path); |
| 43 | return Json::parse(jsonFile); |
| 44 | } |
| 45 | catch (const std::exception& e) |
| 46 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 47 | lg2::error( |
| 48 | "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", |
| 49 | "ERROR", e, "PATH", path); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 50 | throw std::runtime_error("Failed to parse config file"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** @brief Returns action enum based on string |
| 55 | * |
| 56 | * @param[in] action - action string |
| 57 | * |
| 58 | * @return Action - action enum (On/Blink) |
| 59 | */ |
| 60 | phosphor::led::Layout::Action getAction(const std::string& action) |
| 61 | { |
| 62 | assert(action == "On" || action == "Blink"); |
| 63 | |
Patrick Williams | ed80e88 | 2022-03-17 05:03:51 -0500 | [diff] [blame] | 64 | return action == "Blink" ? phosphor::led::Layout::Action::Blink |
| 65 | : phosphor::led::Layout::Action::On; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** @brief Validate the Priority of an LED is same across ALL groups |
| 69 | * |
| 70 | * @param[in] name - led name member of each group |
| 71 | * @param[in] priority - member priority of each group |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 72 | * @param[out] priorityMap - std::unordered_map, key:name, value:priority |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 73 | * |
| 74 | * @return |
| 75 | */ |
| 76 | void validatePriority(const std::string& name, |
| 77 | const phosphor::led::Layout::Action& priority, |
| 78 | PriorityMap& priorityMap) |
| 79 | { |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 80 | auto iter = priorityMap.find(name); |
| 81 | if (iter == priorityMap.end()) |
| 82 | { |
| 83 | priorityMap.emplace(name, priority); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (iter->second != priority) |
| 88 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 89 | lg2::error( |
| 90 | "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", |
| 91 | "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO", |
| 92 | int(priority)); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 93 | |
| 94 | throw std::runtime_error( |
| 95 | "Priority of at least one LED is not same across groups"); |
| 96 | } |
| 97 | } |
| 98 | |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 99 | static void loadJsonConfigV1GroupMember(const Json& member, |
| 100 | PriorityMap& priorityMap, |
| 101 | phosphor::led::ActionSet& ledActions) |
| 102 | { |
| 103 | auto name = member.value("Name", ""); |
| 104 | auto action = getAction(member.value("Action", "")); |
| 105 | uint8_t dutyOn = member.value("DutyOn", 50); |
| 106 | uint16_t period = member.value("Period", 0); |
| 107 | |
| 108 | // Since only have Blink/On and default priority is Blink |
| 109 | auto priority = getAction(member.value("Priority", "Blink")); |
| 110 | |
| 111 | // Same LEDs can be part of multiple groups. However, their |
| 112 | // priorities across groups need to match. |
| 113 | validatePriority(name, priority, priorityMap); |
| 114 | |
| 115 | phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period, |
| 116 | priority}; |
| 117 | ledActions.emplace(ledAction); |
| 118 | } |
| 119 | |
| 120 | static void loadJsonConfigV1Group(const Json& entry, |
| 121 | phosphor::led::GroupMap& ledMap, |
| 122 | PriorityMap& priorityMap) |
| 123 | { |
| 124 | const Json empty{}; |
| 125 | |
| 126 | fs::path tmpPath("/xyz/openbmc_project/led/groups"); |
| 127 | |
| 128 | const std::string groupName = entry.value("group", ""); |
| 129 | |
| 130 | tmpPath /= groupName; |
| 131 | auto objpath = tmpPath.string(); |
| 132 | auto members = entry.value("members", empty); |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame^] | 133 | int priority = entry.value("Priority", 0); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 134 | |
| 135 | lg2::debug("config for '{GROUP}'", "GROUP", groupName); |
| 136 | |
| 137 | phosphor::led::ActionSet ledActions{}; |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame^] | 138 | phosphor::led::Layout::GroupLayout groupLayout{}; |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 139 | for (const auto& member : members) |
| 140 | { |
| 141 | loadJsonConfigV1GroupMember(member, priorityMap, ledActions); |
| 142 | } |
| 143 | |
| 144 | // Generated an std::unordered_map of LedGroupNames to std::set of LEDs |
| 145 | // containing the name and properties. |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame^] | 146 | groupLayout.actionSet = ledActions; |
| 147 | groupLayout.priority = priority; |
| 148 | |
| 149 | ledMap.emplace(objpath, groupLayout); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 152 | /** @brief Load JSON config and return led map (JSON version 1) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 153 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 154 | * @return phosphor::led::GroupMap |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 155 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 156 | const phosphor::led::GroupMap loadJsonConfigV1(const Json& json) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 157 | { |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 158 | phosphor::led::GroupMap ledMap{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 159 | PriorityMap priorityMap{}; |
| 160 | |
| 161 | // define the default JSON as empty |
| 162 | const Json empty{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 163 | auto leds = json.value("leds", empty); |
| 164 | |
| 165 | for (const auto& entry : leds) |
| 166 | { |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 167 | loadJsonConfigV1Group(entry, ledMap, priorityMap); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return ledMap; |
| 171 | } |
| 172 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 173 | /** @brief Load JSON config and return led map |
| 174 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 175 | * @return phosphor::led::GroupMap |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 176 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 177 | const phosphor::led::GroupMap loadJsonConfig(const fs::path& path) |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 178 | { |
| 179 | auto json = readJson(path); |
| 180 | |
| 181 | auto version = json.value("version", 1); |
| 182 | switch (version) |
| 183 | { |
| 184 | case 1: |
| 185 | return loadJsonConfigV1(json); |
| 186 | |
| 187 | default: |
| 188 | lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", |
| 189 | version); |
| 190 | throw std::runtime_error("Unsupported version"); |
| 191 | } |
| 192 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 193 | return phosphor::led::GroupMap{}; |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 194 | } |
| 195 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 196 | /** @brief Get led map from LED groups JSON config |
| 197 | * |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 198 | * @param[in] config - Path to the JSON config. |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 199 | * @return phosphor::led::GroupMap |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 200 | * |
| 201 | * @note if config is an empty string, daemon will interrogate dbus for |
| 202 | * compatible strings. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 203 | */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 204 | const phosphor::led::GroupMap getSystemLedMap(fs::path config) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 205 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 206 | if (config.empty()) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 207 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 208 | config = phosphor::led::getJsonConfig(); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 209 | } |
| 210 | |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 211 | return loadJsonConfig(config); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 212 | } |