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