1953315d2SPatrick Williams #include "config.h" 2953315d2SPatrick Williams 3953315d2SPatrick Williams #include "json-config.hpp" 4953315d2SPatrick Williams #include "ledlayout.hpp" 5953315d2SPatrick Williams 6953315d2SPatrick Williams #include <nlohmann/json.hpp> 7953315d2SPatrick Williams #include <phosphor-logging/lg2.hpp> 8953315d2SPatrick Williams #include <sdbusplus/bus.hpp> 9953315d2SPatrick Williams #include <sdeventplus/event.hpp> 10953315d2SPatrick Williams 11953315d2SPatrick Williams #include <filesystem> 12953315d2SPatrick Williams #include <fstream> 13953315d2SPatrick Williams #include <iostream> 14953315d2SPatrick Williams 15953315d2SPatrick Williams namespace fs = std::filesystem; 16953315d2SPatrick Williams 17953315d2SPatrick Williams using Json = nlohmann::json; 18953315d2SPatrick Williams using LedAction = std::set<phosphor::led::Layout::LedAction>; 19953315d2SPatrick Williams using LedMap = std::map<std::string, LedAction>; 20953315d2SPatrick Williams 21953315d2SPatrick Williams // Priority for a particular LED needs to stay SAME across all groups 22953315d2SPatrick Williams // phosphor::led::Layout::Action can only be one of `Blink` and `On` 23953315d2SPatrick Williams using PriorityMap = std::map<std::string, phosphor::led::Layout::Action>; 24953315d2SPatrick Williams 25953315d2SPatrick Williams /** @brief Parse LED JSON file and output Json object 26953315d2SPatrick Williams * 27953315d2SPatrick Williams * @param[in] path - path of LED JSON file 28953315d2SPatrick Williams * 29953315d2SPatrick Williams * @return const Json - Json object 30953315d2SPatrick Williams */ 31*73ff9ad6SPatrick Williams Json readJson(const fs::path& path) 32953315d2SPatrick Williams { 33953315d2SPatrick Williams 34953315d2SPatrick Williams if (!fs::exists(path) || fs::is_empty(path)) 35953315d2SPatrick Williams { 36953315d2SPatrick Williams lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", 37953315d2SPatrick Williams "PATH", path); 38953315d2SPatrick Williams throw std::runtime_error("Incorrect File Path or empty file"); 39953315d2SPatrick Williams } 40953315d2SPatrick Williams 41953315d2SPatrick Williams try 42953315d2SPatrick Williams { 43953315d2SPatrick Williams std::ifstream jsonFile(path); 44953315d2SPatrick Williams return Json::parse(jsonFile); 45953315d2SPatrick Williams } 46953315d2SPatrick Williams catch (const std::exception& e) 47953315d2SPatrick Williams { 48953315d2SPatrick Williams lg2::error( 49953315d2SPatrick Williams "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", 50953315d2SPatrick Williams "ERROR", e, "PATH", path); 51953315d2SPatrick Williams throw std::runtime_error("Failed to parse config file"); 52953315d2SPatrick Williams } 53953315d2SPatrick Williams } 54953315d2SPatrick Williams 55953315d2SPatrick Williams /** @brief Returns action enum based on string 56953315d2SPatrick Williams * 57953315d2SPatrick Williams * @param[in] action - action string 58953315d2SPatrick Williams * 59953315d2SPatrick Williams * @return Action - action enum (On/Blink) 60953315d2SPatrick Williams */ 61953315d2SPatrick Williams phosphor::led::Layout::Action getAction(const std::string& action) 62953315d2SPatrick Williams { 63953315d2SPatrick Williams assert(action == "On" || action == "Blink"); 64953315d2SPatrick Williams 65953315d2SPatrick Williams return action == "Blink" ? phosphor::led::Layout::Blink 66953315d2SPatrick Williams : phosphor::led::Layout::On; 67953315d2SPatrick Williams } 68953315d2SPatrick Williams 69953315d2SPatrick Williams /** @brief Validate the Priority of an LED is same across ALL groups 70953315d2SPatrick Williams * 71953315d2SPatrick Williams * @param[in] name - led name member of each group 72953315d2SPatrick Williams * @param[in] priority - member priority of each group 73953315d2SPatrick Williams * @param[out] priorityMap - std::map, key:name, value:priority 74953315d2SPatrick Williams * 75953315d2SPatrick Williams * @return 76953315d2SPatrick Williams */ 77953315d2SPatrick Williams void validatePriority(const std::string& name, 78953315d2SPatrick Williams const phosphor::led::Layout::Action& priority, 79953315d2SPatrick Williams PriorityMap& priorityMap) 80953315d2SPatrick Williams { 81953315d2SPatrick Williams 82953315d2SPatrick Williams auto iter = priorityMap.find(name); 83953315d2SPatrick Williams if (iter == priorityMap.end()) 84953315d2SPatrick Williams { 85953315d2SPatrick Williams priorityMap.emplace(name, priority); 86953315d2SPatrick Williams return; 87953315d2SPatrick Williams } 88953315d2SPatrick Williams 89953315d2SPatrick Williams if (iter->second != priority) 90953315d2SPatrick Williams { 91953315d2SPatrick Williams lg2::error( 92953315d2SPatrick Williams "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", 93953315d2SPatrick Williams "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO", 94953315d2SPatrick Williams int(priority)); 95953315d2SPatrick Williams 96953315d2SPatrick Williams throw std::runtime_error( 97953315d2SPatrick Williams "Priority of at least one LED is not same across groups"); 98953315d2SPatrick Williams } 99953315d2SPatrick Williams } 100953315d2SPatrick Williams 101*73ff9ad6SPatrick Williams /** @brief Load JSON config and return led map (JSON version 1) 102953315d2SPatrick Williams * 103953315d2SPatrick Williams * @return LedMap - Generated an std::map of LedAction 104953315d2SPatrick Williams */ 105*73ff9ad6SPatrick Williams const LedMap loadJsonConfigV1(const Json& json) 106953315d2SPatrick Williams { 107953315d2SPatrick Williams LedMap ledMap{}; 108953315d2SPatrick Williams PriorityMap priorityMap{}; 109953315d2SPatrick Williams 110953315d2SPatrick Williams // define the default JSON as empty 111953315d2SPatrick Williams const Json empty{}; 112953315d2SPatrick Williams auto leds = json.value("leds", empty); 113953315d2SPatrick Williams 114953315d2SPatrick Williams for (const auto& entry : leds) 115953315d2SPatrick Williams { 116953315d2SPatrick Williams fs::path tmpPath(std::string{OBJPATH}); 117953315d2SPatrick Williams tmpPath /= entry.value("group", ""); 118953315d2SPatrick Williams auto objpath = tmpPath.string(); 119953315d2SPatrick Williams auto members = entry.value("members", empty); 120953315d2SPatrick Williams 121953315d2SPatrick Williams LedAction ledActions{}; 122953315d2SPatrick Williams for (const auto& member : members) 123953315d2SPatrick Williams { 124953315d2SPatrick Williams auto name = member.value("Name", ""); 125953315d2SPatrick Williams auto action = getAction(member.value("Action", "")); 126953315d2SPatrick Williams uint8_t dutyOn = member.value("DutyOn", 50); 127953315d2SPatrick Williams uint16_t period = member.value("Period", 0); 128953315d2SPatrick Williams 129953315d2SPatrick Williams // Since only have Blink/On and default priority is Blink 130953315d2SPatrick Williams auto priority = getAction(member.value("Priority", "Blink")); 131953315d2SPatrick Williams 132953315d2SPatrick Williams // Same LEDs can be part of multiple groups. However, their 133953315d2SPatrick Williams // priorities across groups need to match. 134953315d2SPatrick Williams validatePriority(name, priority, priorityMap); 135953315d2SPatrick Williams 136953315d2SPatrick Williams phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, 137953315d2SPatrick Williams period, priority}; 138953315d2SPatrick Williams ledActions.emplace(ledAction); 139953315d2SPatrick Williams } 140953315d2SPatrick Williams 141953315d2SPatrick Williams // Generated an std::map of LedGroupNames to std::set of LEDs 142953315d2SPatrick Williams // containing the name and properties. 143953315d2SPatrick Williams ledMap.emplace(objpath, ledActions); 144953315d2SPatrick Williams } 145953315d2SPatrick Williams 146953315d2SPatrick Williams return ledMap; 147953315d2SPatrick Williams } 148953315d2SPatrick Williams 149*73ff9ad6SPatrick Williams /** @brief Load JSON config and return led map 150*73ff9ad6SPatrick Williams * 151*73ff9ad6SPatrick Williams * @return LedMap - Generated an std::map of LedAction 152*73ff9ad6SPatrick Williams */ 153*73ff9ad6SPatrick Williams const LedMap loadJsonConfig(const fs::path& path) 154*73ff9ad6SPatrick Williams { 155*73ff9ad6SPatrick Williams auto json = readJson(path); 156*73ff9ad6SPatrick Williams 157*73ff9ad6SPatrick Williams auto version = json.value("version", 1); 158*73ff9ad6SPatrick Williams switch (version) 159*73ff9ad6SPatrick Williams { 160*73ff9ad6SPatrick Williams case 1: 161*73ff9ad6SPatrick Williams return loadJsonConfigV1(json); 162*73ff9ad6SPatrick Williams 163*73ff9ad6SPatrick Williams default: 164*73ff9ad6SPatrick Williams lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", 165*73ff9ad6SPatrick Williams version); 166*73ff9ad6SPatrick Williams throw std::runtime_error("Unsupported version"); 167*73ff9ad6SPatrick Williams } 168*73ff9ad6SPatrick Williams 169*73ff9ad6SPatrick Williams return LedMap{}; 170*73ff9ad6SPatrick Williams } 171*73ff9ad6SPatrick Williams 172953315d2SPatrick Williams /** @brief Get led map from LED groups JSON config 173953315d2SPatrick Williams * 1747217c035SPatrick Williams * @param[in] config - Path to the JSON config. 175953315d2SPatrick Williams * @return LedMap - Generated an std::map of LedAction 1767217c035SPatrick Williams * 1777217c035SPatrick Williams * @note if config is an empty string, daemon will interrogate dbus for 1787217c035SPatrick Williams * compatible strings. 179953315d2SPatrick Williams */ 1807217c035SPatrick Williams const LedMap getSystemLedMap(fs::path config) 181953315d2SPatrick Williams { 1827217c035SPatrick Williams if (config.empty()) 183953315d2SPatrick Williams { 1847217c035SPatrick Williams config = phosphor::led::getJsonConfig(); 185953315d2SPatrick Williams } 186953315d2SPatrick Williams 1877217c035SPatrick Williams return loadJsonConfig(config); 188953315d2SPatrick Williams } 189