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 | |
| 18 | /** @brief Parse LED JSON file and output Json object |
| 19 | * |
| 20 | * @param[in] path - path of LED JSON file |
| 21 | * |
| 22 | * @return const Json - Json object |
| 23 | */ |
| 24 | const Json readJson(const fs::path& path) |
| 25 | { |
| 26 | using namespace phosphor::logging; |
| 27 | |
| 28 | if (!fs::exists(path) || fs::is_empty(path)) |
| 29 | { |
| 30 | log<level::ERR>("Incorrect File Path or empty file", |
| 31 | entry("FILE_PATH=%s", path.c_str())); |
| 32 | throw std::runtime_error("Incorrect File Path or empty file"); |
| 33 | } |
| 34 | |
| 35 | try |
| 36 | { |
| 37 | std::ifstream jsonFile(path); |
| 38 | return Json::parse(jsonFile); |
| 39 | } |
| 40 | catch (const std::exception& e) |
| 41 | { |
| 42 | log<level::ERR>("Failed to parse config file", |
| 43 | entry("ERROR=%s", e.what()), |
| 44 | entry("FILE_PATH=%s", path.c_str())); |
| 45 | throw std::runtime_error("Failed to parse config file"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** @brief Returns action enum based on string |
| 50 | * |
| 51 | * @param[in] action - action string |
| 52 | * |
| 53 | * @return Action - action enum (On/Blink) |
| 54 | */ |
| 55 | phosphor::led::Layout::Action getAction(const std::string& action) |
| 56 | { |
| 57 | assert(action == "On" || action == "Blink"); |
| 58 | |
| 59 | return action == "Blink" ? phosphor::led::Layout::Blink |
| 60 | : phosphor::led::Layout::On; |
| 61 | } |
| 62 | |
| 63 | /** @brief Load JSON config and return led map |
| 64 | * |
| 65 | * @return LedMap - Generated an std::map of LedAction |
| 66 | */ |
| 67 | const LedMap loadJsonConfig(const fs::path& path) |
| 68 | { |
| 69 | LedMap ledMap{}; |
| 70 | |
| 71 | // define the default JSON as empty |
| 72 | const Json empty{}; |
| 73 | auto json = readJson(path); |
| 74 | |
| 75 | auto leds = json.value("leds", empty); |
| 76 | for (const auto& entry : leds) |
| 77 | { |
| 78 | fs::path tmpPath(std::string{OBJPATH}); |
| 79 | tmpPath /= entry.value("group", ""); |
| 80 | auto objpath = tmpPath.string(); |
| 81 | auto members = entry.value("members", empty); |
| 82 | |
| 83 | LedAction ledActions{}; |
| 84 | for (const auto& member : members) |
| 85 | { |
George Liu | e5c366f | 2020-07-02 18:04:05 +0800 | [diff] [blame] | 86 | auto name = member.value("Name", ""); |
George Liu | def5f5a | 2020-04-10 11:23:52 +0800 | [diff] [blame] | 87 | auto action = getAction(member.value("Action", "")); |
| 88 | uint8_t dutyOn = member.value("DutyOn", 50); |
| 89 | uint16_t period = member.value("Period", 0); |
| 90 | |
| 91 | // Since only have Blink/On and default priority is Blink |
| 92 | auto priority = getAction(member.value("Priority", "Blink")); |
| 93 | phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, |
| 94 | period, priority}; |
| 95 | ledActions.emplace(ledAction); |
| 96 | } |
| 97 | |
| 98 | // Generated an std::map of LedGroupNames to std::set of LEDs |
| 99 | // containing the name and properties. |
| 100 | ledMap.emplace(objpath, ledActions); |
| 101 | } |
| 102 | |
| 103 | return ledMap; |
| 104 | } |