George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "ledlayout.hpp" |
| 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | |
| 8 | #include <filesystem> |
| 9 | #include <fstream> |
| 10 | #include <iostream> |
| 11 | |
| 12 | namespace fs = std::filesystem; |
| 13 | |
| 14 | using Json = nlohmann::json; |
| 15 | using LedAction = std::set<phosphor::led::Layout::LedAction>; |
| 16 | using LedMap = std::map<std::string, LedAction>; |
| 17 | |
George Liu | 17fcb4d | 2020-06-30 18:12:53 +0800 | [diff] [blame] | 18 | // Priority for a particular LED needs to stay SAME across all groups |
| 19 | // phosphor::led::Layout::Action can only be one of `Blink` and `On` |
| 20 | using PriorityMap = std::map<std::string, phosphor::led::Layout::Action>; |
| 21 | |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 22 | /** @brief Parse LED JSON file and output Json object |
| 23 | * |
| 24 | * @param[in] path - path of LED JSON file |
| 25 | * |
| 26 | * @return const Json - Json object |
| 27 | */ |
| 28 | const Json readJson(const fs::path& path) |
| 29 | { |
| 30 | using namespace phosphor::logging; |
| 31 | |
| 32 | if (!fs::exists(path) || fs::is_empty(path)) |
| 33 | { |
| 34 | log<level::ERR>("Incorrect File Path or empty file", |
| 35 | entry("FILE_PATH=%s", path.c_str())); |
| 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 | { |
| 46 | log<level::ERR>("Failed to parse config file", |
| 47 | entry("ERROR=%s", e.what()), |
| 48 | entry("FILE_PATH=%s", path.c_str())); |
| 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 | |
| 63 | return action == "Blink" ? phosphor::led::Layout::Blink |
| 64 | : phosphor::led::Layout::On; |
| 65 | } |
| 66 | |
George Liu | 17fcb4d | 2020-06-30 18:12:53 +0800 | [diff] [blame] | 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 |
| 71 | * @param[out] priorityMap - std::map, key:name, value:priority |
| 72 | * |
| 73 | * @return |
| 74 | */ |
| 75 | void validatePriority(const std::string& name, |
| 76 | const phosphor::led::Layout::Action& priority, |
| 77 | PriorityMap& priorityMap) |
| 78 | { |
| 79 | using namespace phosphor::logging; |
| 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 | { |
| 90 | log<level::ERR>("Priority of LED is not same across all", |
| 91 | entry("Name=%s", name.c_str()), |
| 92 | entry(" Old Priority=%d", iter->second), |
| 93 | entry(" New priority=%d", priority)); |
| 94 | |
| 95 | throw std::runtime_error( |
| 96 | "Priority of at least one LED is not same across groups"); |
| 97 | } |
| 98 | } |
| 99 | |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 100 | /** @brief Load JSON config and return led map |
| 101 | * |
| 102 | * @return LedMap - Generated an std::map of LedAction |
| 103 | */ |
| 104 | const LedMap loadJsonConfig(const fs::path& path) |
| 105 | { |
| 106 | LedMap ledMap{}; |
George Liu | 17fcb4d | 2020-06-30 18:12:53 +0800 | [diff] [blame] | 107 | PriorityMap priorityMap{}; |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 108 | |
| 109 | // define the default JSON as empty |
| 110 | const Json empty{}; |
| 111 | auto json = readJson(path); |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 112 | auto leds = json.value("leds", empty); |
George Liu | 17fcb4d | 2020-06-30 18:12:53 +0800 | [diff] [blame] | 113 | |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 114 | for (const auto& entry : leds) |
| 115 | { |
| 116 | fs::path tmpPath(std::string{OBJPATH}); |
| 117 | tmpPath /= entry.value("group", ""); |
| 118 | auto objpath = tmpPath.string(); |
| 119 | auto members = entry.value("members", empty); |
| 120 | |
| 121 | LedAction ledActions{}; |
| 122 | for (const auto& member : members) |
| 123 | { |
George Liu | e5c366f | 2020-07-02 18:04:05 +0800 | [diff] [blame] | 124 | auto name = member.value("Name", ""); |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 125 | auto action = getAction(member.value("Action", "")); |
| 126 | uint8_t dutyOn = member.value("DutyOn", 50); |
| 127 | uint16_t period = member.value("Period", 0); |
| 128 | |
| 129 | // Since only have Blink/On and default priority is Blink |
| 130 | auto priority = getAction(member.value("Priority", "Blink")); |
George Liu | 17fcb4d | 2020-06-30 18:12:53 +0800 | [diff] [blame] | 131 | |
| 132 | // Same LEDs can be part of multiple groups. However, their |
| 133 | // priorities across groups need to match. |
| 134 | validatePriority(name, priority, priorityMap); |
| 135 | |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 136 | phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, |
| 137 | period, priority}; |
| 138 | ledActions.emplace(ledAction); |
| 139 | } |
| 140 | |
| 141 | // Generated an std::map of LedGroupNames to std::set of LEDs |
| 142 | // containing the name and properties. |
| 143 | ledMap.emplace(objpath, ledActions); |
| 144 | } |
| 145 | |
| 146 | return ledMap; |
| 147 | } |