| 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 |  | 
 | 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 |  | 
 | 81 |     auto iter = priorityMap.find(name); | 
 | 82 |     if (iter == priorityMap.end()) | 
 | 83 |     { | 
 | 84 |         priorityMap.emplace(name, priority); | 
 | 85 |         return; | 
 | 86 |     } | 
 | 87 |  | 
 | 88 |     if (iter->second != priority) | 
 | 89 |     { | 
| George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 90 |         lg2::error( | 
 | 91 |             "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", | 
 | 92 |             "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO", | 
 | 93 |             int(priority)); | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 94 |  | 
 | 95 |         throw std::runtime_error( | 
 | 96 |             "Priority of at least one LED is not same across groups"); | 
 | 97 |     } | 
 | 98 | } | 
 | 99 |  | 
| Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 100 | /** @brief Load JSON config and return led map (JSON version 1) | 
| 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 |  *  @return phosphor::led::GroupMap | 
| 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 | const phosphor::led::GroupMap loadJsonConfigV1(const Json& json) | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 105 | { | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 106 |     phosphor::led::GroupMap ledMap{}; | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 107 |     PriorityMap priorityMap{}; | 
 | 108 |  | 
 | 109 |     // define the default JSON as empty | 
 | 110 |     const Json empty{}; | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 111 |     auto leds = json.value("leds", empty); | 
 | 112 |  | 
 | 113 |     for (const auto& entry : leds) | 
 | 114 |     { | 
 | 115 |         fs::path tmpPath(std::string{OBJPATH}); | 
 | 116 |         tmpPath /= entry.value("group", ""); | 
 | 117 |         auto objpath = tmpPath.string(); | 
 | 118 |         auto members = entry.value("members", empty); | 
 | 119 |  | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 120 |         phosphor::led::ActionSet ledActions{}; | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 121 |         for (const auto& member : members) | 
 | 122 |         { | 
 | 123 |             auto name = member.value("Name", ""); | 
 | 124 |             auto action = getAction(member.value("Action", "")); | 
 | 125 |             uint8_t dutyOn = member.value("DutyOn", 50); | 
 | 126 |             uint16_t period = member.value("Period", 0); | 
 | 127 |  | 
 | 128 |             // Since only have Blink/On and default priority is Blink | 
 | 129 |             auto priority = getAction(member.value("Priority", "Blink")); | 
 | 130 |  | 
 | 131 |             // Same LEDs can be part of multiple groups. However, their | 
 | 132 |             // priorities across groups need to match. | 
 | 133 |             validatePriority(name, priority, priorityMap); | 
 | 134 |  | 
 | 135 |             phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, | 
 | 136 |                                                        period, priority}; | 
 | 137 |             ledActions.emplace(ledAction); | 
 | 138 |         } | 
 | 139 |  | 
| Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 140 |         // Generated an std::unordered_map of LedGroupNames to std::set of LEDs | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 141 |         // containing the name and properties. | 
 | 142 |         ledMap.emplace(objpath, ledActions); | 
 | 143 |     } | 
 | 144 |  | 
 | 145 |     return ledMap; | 
 | 146 | } | 
 | 147 |  | 
| Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 148 | /** @brief Load JSON config and return led map | 
 | 149 |  * | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 150 |  *  @return phosphor::led::GroupMap | 
| Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 151 |  */ | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 152 | const phosphor::led::GroupMap loadJsonConfig(const fs::path& path) | 
| Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 153 | { | 
 | 154 |     auto json = readJson(path); | 
 | 155 |  | 
 | 156 |     auto version = json.value("version", 1); | 
 | 157 |     switch (version) | 
 | 158 |     { | 
 | 159 |         case 1: | 
 | 160 |             return loadJsonConfigV1(json); | 
 | 161 |  | 
 | 162 |         default: | 
 | 163 |             lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", | 
 | 164 |                        version); | 
 | 165 |             throw std::runtime_error("Unsupported version"); | 
 | 166 |     } | 
 | 167 |  | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 168 |     return phosphor::led::GroupMap{}; | 
| Patrick Williams | 73ff9ad | 2022-03-16 16:27:21 -0500 | [diff] [blame] | 169 | } | 
 | 170 |  | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 171 | /** @brief Get led map from LED groups JSON config | 
 | 172 |  * | 
| Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 173 |  *  @param[in] config - Path to the JSON config. | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 174 |  *  @return phosphor::led::GroupMap | 
| Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 175 |  * | 
 | 176 |  *  @note if config is an empty string, daemon will interrogate dbus for | 
 | 177 |  *        compatible strings. | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 178 |  */ | 
| Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame^] | 179 | const phosphor::led::GroupMap getSystemLedMap(fs::path config) | 
| 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 |     if (config.empty()) | 
| 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 |         config = phosphor::led::getJsonConfig(); | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 184 |     } | 
 | 185 |  | 
| Patrick Williams | 7217c03 | 2022-03-16 16:26:09 -0500 | [diff] [blame] | 186 |     return loadJsonConfig(config); | 
| George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 187 | } |