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 |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 21 | using PriorityMap = |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame] | 22 | std::unordered_map<std::string, |
| 23 | std::optional<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 | * |
Alexander Hansen | 4d44a55 | 2024-07-24 14:55:24 +0200 | [diff] [blame] | 58 | * @return Action - action enum (On/Off/Blink) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 59 | */ |
| 60 | phosphor::led::Layout::Action getAction(const std::string& action) |
| 61 | { |
Alexander Hansen | 4d44a55 | 2024-07-24 14:55:24 +0200 | [diff] [blame] | 62 | if (action == "On") |
| 63 | { |
| 64 | return phosphor::led::Layout::Action::On; |
| 65 | } |
| 66 | if (action == "Off") |
| 67 | { |
| 68 | return phosphor::led::Layout::Action::Off; |
| 69 | } |
| 70 | if (action == "Blink") |
| 71 | { |
| 72 | return phosphor::led::Layout::Action::Blink; |
| 73 | } |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 74 | |
Alexander Hansen | 4d44a55 | 2024-07-24 14:55:24 +0200 | [diff] [blame] | 75 | assert(false); |
| 76 | return phosphor::led::Layout::Action::Blink; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 79 | static void loadJsonConfigV1GroupMember(const Json& member, |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 80 | phosphor::led::ActionSet& ledActions) |
| 81 | { |
| 82 | auto name = member.value("Name", ""); |
| 83 | auto action = getAction(member.value("Action", "")); |
| 84 | uint8_t dutyOn = member.value("DutyOn", 50); |
| 85 | uint16_t period = member.value("Period", 0); |
| 86 | |
Alexander Hansen | 55badf7 | 2024-07-24 14:35:13 +0200 | [diff] [blame] | 87 | const std::string priorityStr = member.value("Priority", ""); |
| 88 | std::optional<phosphor::led::Layout::Action> priority = std::nullopt; |
| 89 | |
| 90 | if (!priorityStr.empty()) |
| 91 | { |
| 92 | priority = getAction(priorityStr); |
| 93 | } |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 94 | |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 95 | phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period, |
| 96 | priority}; |
| 97 | ledActions.emplace(ledAction); |
| 98 | } |
| 99 | |
| 100 | static void loadJsonConfigV1Group(const Json& entry, |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 101 | phosphor::led::GroupMap& ledMap) |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 102 | { |
| 103 | const Json empty{}; |
| 104 | |
| 105 | fs::path tmpPath("/xyz/openbmc_project/led/groups"); |
| 106 | |
| 107 | const std::string groupName = entry.value("group", ""); |
| 108 | |
| 109 | tmpPath /= groupName; |
| 110 | auto objpath = tmpPath.string(); |
| 111 | auto members = entry.value("members", empty); |
| 112 | |
| 113 | lg2::debug("config for '{GROUP}'", "GROUP", groupName); |
| 114 | |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 115 | int priority = entry.value("Priority", 0); |
| 116 | |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 117 | phosphor::led::ActionSet ledActions{}; |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 118 | phosphor::led::Layout::GroupLayout groupLayout{}; |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 119 | for (const auto& member : members) |
| 120 | { |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 121 | loadJsonConfigV1GroupMember(member, ledActions); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Generated an std::unordered_map of LedGroupNames to std::set of LEDs |
| 125 | // containing the name and properties. |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 126 | groupLayout.actionSet = ledActions; |
| 127 | groupLayout.priority = priority; |
| 128 | |
| 129 | ledMap.emplace(objpath, groupLayout); |
Alexander Hansen | d0f8050 | 2024-07-23 12:12:54 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 132 | /** @brief Load JSON config and return led map (JSON version 1) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 133 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 134 | * @return phosphor::led::GroupMap |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 135 | */ |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame^] | 136 | phosphor::led::GroupMap loadJsonConfigV1(const Json& json) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 137 | { |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 138 | phosphor::led::GroupMap ledMap{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 139 | |
| 140 | // define the default JSON as empty |
| 141 | const Json empty{}; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 142 | auto leds = json.value("leds", empty); |
| 143 | |
| 144 | for (const auto& entry : leds) |
| 145 | { |
Alexander Hansen | 638d148 | 2024-08-21 17:39:57 +0200 | [diff] [blame] | 146 | loadJsonConfigV1Group(entry, ledMap); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return ledMap; |
| 150 | } |
| 151 | |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 152 | /** @brief Load JSON config and return led map |
| 153 | * |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 154 | * @return phosphor::led::GroupMap |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 155 | */ |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame^] | 156 | phosphor::led::GroupMap loadJsonConfig(const fs::path& path) |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 157 | { |
| 158 | auto json = readJson(path); |
| 159 | |
| 160 | auto version = json.value("version", 1); |
| 161 | switch (version) |
| 162 | { |
| 163 | case 1: |
| 164 | return loadJsonConfigV1(json); |
| 165 | |
| 166 | default: |
| 167 | lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", |
| 168 | version); |
| 169 | throw std::runtime_error("Unsupported version"); |
| 170 | } |
| 171 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 172 | return phosphor::led::GroupMap{}; |
Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 173 | } |
| 174 | |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 175 | /** @brief Get led map from LED groups JSON config |
| 176 | * |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 177 | * @param[in] config - Path to the JSON config. |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 178 | * @return phosphor::led::GroupMap |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 179 | * |
| 180 | * @note if config is an empty string, daemon will interrogate dbus for |
| 181 | * compatible strings. |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 182 | */ |
George Liu | 405ea28 | 2024-08-22 19:36:41 +0800 | [diff] [blame^] | 183 | phosphor::led::GroupMap getSystemLedMap(fs::path config) |
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 | if (config.empty()) |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 186 | { |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 187 | config = phosphor::led::getJsonConfig(); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 190 | return loadJsonConfig(config); |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 191 | } |