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 19953315d2SPatrick Williams // Priority for a particular LED needs to stay SAME across all groups 20953315d2SPatrick Williams // phosphor::led::Layout::Action can only be one of `Blink` and `On` 21f2044037SPatrick Williams using PriorityMap = 22f2044037SPatrick Williams std::unordered_map<std::string, phosphor::led::Layout::Action>; 23953315d2SPatrick Williams 24953315d2SPatrick Williams /** @brief Parse LED JSON file and output Json object 25953315d2SPatrick Williams * 26953315d2SPatrick Williams * @param[in] path - path of LED JSON file 27953315d2SPatrick Williams * 28953315d2SPatrick Williams * @return const Json - Json object 29953315d2SPatrick Williams */ 3073ff9ad6SPatrick Williams Json readJson(const fs::path& path) 31953315d2SPatrick Williams { 32953315d2SPatrick Williams if (!fs::exists(path) || fs::is_empty(path)) 33953315d2SPatrick Williams { 34953315d2SPatrick Williams lg2::error("Incorrect File Path or empty file, FILE_PATH = {PATH}", 35953315d2SPatrick Williams "PATH", path); 36953315d2SPatrick Williams throw std::runtime_error("Incorrect File Path or empty file"); 37953315d2SPatrick Williams } 38953315d2SPatrick Williams 39953315d2SPatrick Williams try 40953315d2SPatrick Williams { 41953315d2SPatrick Williams std::ifstream jsonFile(path); 42953315d2SPatrick Williams return Json::parse(jsonFile); 43953315d2SPatrick Williams } 44953315d2SPatrick Williams catch (const std::exception& e) 45953315d2SPatrick Williams { 46953315d2SPatrick Williams lg2::error( 47953315d2SPatrick Williams "Failed to parse config file, ERROR = {ERROR}, FILE_PATH = {PATH}", 48953315d2SPatrick Williams "ERROR", e, "PATH", path); 49953315d2SPatrick Williams throw std::runtime_error("Failed to parse config file"); 50953315d2SPatrick Williams } 51953315d2SPatrick Williams } 52953315d2SPatrick Williams 53953315d2SPatrick Williams /** @brief Returns action enum based on string 54953315d2SPatrick Williams * 55953315d2SPatrick Williams * @param[in] action - action string 56953315d2SPatrick Williams * 57953315d2SPatrick Williams * @return Action - action enum (On/Blink) 58953315d2SPatrick Williams */ 59953315d2SPatrick Williams phosphor::led::Layout::Action getAction(const std::string& action) 60953315d2SPatrick Williams { 61953315d2SPatrick Williams assert(action == "On" || action == "Blink"); 62953315d2SPatrick Williams 63ed80e885SPatrick Williams return action == "Blink" ? phosphor::led::Layout::Action::Blink 64ed80e885SPatrick Williams : phosphor::led::Layout::Action::On; 65953315d2SPatrick Williams } 66953315d2SPatrick Williams 67953315d2SPatrick Williams /** @brief Validate the Priority of an LED is same across ALL groups 68953315d2SPatrick Williams * 69953315d2SPatrick Williams * @param[in] name - led name member of each group 70953315d2SPatrick Williams * @param[in] priority - member priority of each group 71f2044037SPatrick Williams * @param[out] priorityMap - std::unordered_map, key:name, value:priority 72953315d2SPatrick Williams * 73953315d2SPatrick Williams * @return 74953315d2SPatrick Williams */ 75953315d2SPatrick Williams void validatePriority(const std::string& name, 76953315d2SPatrick Williams const phosphor::led::Layout::Action& priority, 77953315d2SPatrick Williams PriorityMap& priorityMap) 78953315d2SPatrick Williams { 79953315d2SPatrick Williams auto iter = priorityMap.find(name); 80953315d2SPatrick Williams if (iter == priorityMap.end()) 81953315d2SPatrick Williams { 82953315d2SPatrick Williams priorityMap.emplace(name, priority); 83953315d2SPatrick Williams return; 84953315d2SPatrick Williams } 85953315d2SPatrick Williams 86953315d2SPatrick Williams if (iter->second != priority) 87953315d2SPatrick Williams { 88953315d2SPatrick Williams lg2::error( 89953315d2SPatrick Williams "Priority of LED is not same across all, Name = {NAME}, Old Priority = {OLD_PRIO}, New Priority = {NEW_PRIO}", 90953315d2SPatrick Williams "NAME", name, "OLD_PRIO", int(iter->second), "NEW_PRIO", 91953315d2SPatrick Williams int(priority)); 92953315d2SPatrick Williams 93953315d2SPatrick Williams throw std::runtime_error( 94953315d2SPatrick Williams "Priority of at least one LED is not same across groups"); 95953315d2SPatrick Williams } 96953315d2SPatrick Williams } 97953315d2SPatrick Williams 98*d0f80506SAlexander Hansen static void loadJsonConfigV1GroupMember(const Json& member, 99*d0f80506SAlexander Hansen PriorityMap& priorityMap, 100*d0f80506SAlexander Hansen phosphor::led::ActionSet& ledActions) 101*d0f80506SAlexander Hansen { 102*d0f80506SAlexander Hansen auto name = member.value("Name", ""); 103*d0f80506SAlexander Hansen auto action = getAction(member.value("Action", "")); 104*d0f80506SAlexander Hansen uint8_t dutyOn = member.value("DutyOn", 50); 105*d0f80506SAlexander Hansen uint16_t period = member.value("Period", 0); 106*d0f80506SAlexander Hansen 107*d0f80506SAlexander Hansen // Since only have Blink/On and default priority is Blink 108*d0f80506SAlexander Hansen auto priority = getAction(member.value("Priority", "Blink")); 109*d0f80506SAlexander Hansen 110*d0f80506SAlexander Hansen // Same LEDs can be part of multiple groups. However, their 111*d0f80506SAlexander Hansen // priorities across groups need to match. 112*d0f80506SAlexander Hansen validatePriority(name, priority, priorityMap); 113*d0f80506SAlexander Hansen 114*d0f80506SAlexander Hansen phosphor::led::Layout::LedAction ledAction{name, action, dutyOn, period, 115*d0f80506SAlexander Hansen priority}; 116*d0f80506SAlexander Hansen ledActions.emplace(ledAction); 117*d0f80506SAlexander Hansen } 118*d0f80506SAlexander Hansen 119*d0f80506SAlexander Hansen static void loadJsonConfigV1Group(const Json& entry, 120*d0f80506SAlexander Hansen phosphor::led::GroupMap& ledMap, 121*d0f80506SAlexander Hansen PriorityMap& priorityMap) 122*d0f80506SAlexander Hansen { 123*d0f80506SAlexander Hansen const Json empty{}; 124*d0f80506SAlexander Hansen 125*d0f80506SAlexander Hansen fs::path tmpPath("/xyz/openbmc_project/led/groups"); 126*d0f80506SAlexander Hansen 127*d0f80506SAlexander Hansen const std::string groupName = entry.value("group", ""); 128*d0f80506SAlexander Hansen 129*d0f80506SAlexander Hansen tmpPath /= groupName; 130*d0f80506SAlexander Hansen auto objpath = tmpPath.string(); 131*d0f80506SAlexander Hansen auto members = entry.value("members", empty); 132*d0f80506SAlexander Hansen 133*d0f80506SAlexander Hansen lg2::debug("config for '{GROUP}'", "GROUP", groupName); 134*d0f80506SAlexander Hansen 135*d0f80506SAlexander Hansen phosphor::led::ActionSet ledActions{}; 136*d0f80506SAlexander Hansen for (const auto& member : members) 137*d0f80506SAlexander Hansen { 138*d0f80506SAlexander Hansen loadJsonConfigV1GroupMember(member, priorityMap, ledActions); 139*d0f80506SAlexander Hansen } 140*d0f80506SAlexander Hansen 141*d0f80506SAlexander Hansen // Generated an std::unordered_map of LedGroupNames to std::set of LEDs 142*d0f80506SAlexander Hansen // containing the name and properties. 143*d0f80506SAlexander Hansen ledMap.emplace(objpath, ledActions); 144*d0f80506SAlexander Hansen } 145*d0f80506SAlexander Hansen 14673ff9ad6SPatrick Williams /** @brief Load JSON config and return led map (JSON version 1) 147953315d2SPatrick Williams * 148158b2c14SPatrick Williams * @return phosphor::led::GroupMap 149953315d2SPatrick Williams */ 150158b2c14SPatrick Williams const phosphor::led::GroupMap loadJsonConfigV1(const Json& json) 151953315d2SPatrick Williams { 152158b2c14SPatrick Williams phosphor::led::GroupMap ledMap{}; 153953315d2SPatrick Williams PriorityMap priorityMap{}; 154953315d2SPatrick Williams 155953315d2SPatrick Williams // define the default JSON as empty 156953315d2SPatrick Williams const Json empty{}; 157953315d2SPatrick Williams auto leds = json.value("leds", empty); 158953315d2SPatrick Williams 159953315d2SPatrick Williams for (const auto& entry : leds) 160953315d2SPatrick Williams { 161*d0f80506SAlexander Hansen loadJsonConfigV1Group(entry, ledMap, priorityMap); 162953315d2SPatrick Williams } 163953315d2SPatrick Williams 164953315d2SPatrick Williams return ledMap; 165953315d2SPatrick Williams } 166953315d2SPatrick Williams 16773ff9ad6SPatrick Williams /** @brief Load JSON config and return led map 16873ff9ad6SPatrick Williams * 169158b2c14SPatrick Williams * @return phosphor::led::GroupMap 17073ff9ad6SPatrick Williams */ 171158b2c14SPatrick Williams const phosphor::led::GroupMap loadJsonConfig(const fs::path& path) 17273ff9ad6SPatrick Williams { 17373ff9ad6SPatrick Williams auto json = readJson(path); 17473ff9ad6SPatrick Williams 17573ff9ad6SPatrick Williams auto version = json.value("version", 1); 17673ff9ad6SPatrick Williams switch (version) 17773ff9ad6SPatrick Williams { 17873ff9ad6SPatrick Williams case 1: 17973ff9ad6SPatrick Williams return loadJsonConfigV1(json); 18073ff9ad6SPatrick Williams 18173ff9ad6SPatrick Williams default: 18273ff9ad6SPatrick Williams lg2::error("Unsupported JSON Version: {VERSION}", "VERSION", 18373ff9ad6SPatrick Williams version); 18473ff9ad6SPatrick Williams throw std::runtime_error("Unsupported version"); 18573ff9ad6SPatrick Williams } 18673ff9ad6SPatrick Williams 187158b2c14SPatrick Williams return phosphor::led::GroupMap{}; 18873ff9ad6SPatrick Williams } 18973ff9ad6SPatrick Williams 190953315d2SPatrick Williams /** @brief Get led map from LED groups JSON config 191953315d2SPatrick Williams * 1927217c035SPatrick Williams * @param[in] config - Path to the JSON config. 193158b2c14SPatrick Williams * @return phosphor::led::GroupMap 1947217c035SPatrick Williams * 1957217c035SPatrick Williams * @note if config is an empty string, daemon will interrogate dbus for 1967217c035SPatrick Williams * compatible strings. 197953315d2SPatrick Williams */ 198158b2c14SPatrick Williams const phosphor::led::GroupMap getSystemLedMap(fs::path config) 199953315d2SPatrick Williams { 2007217c035SPatrick Williams if (config.empty()) 201953315d2SPatrick Williams { 2027217c035SPatrick Williams config = phosphor::led::getJsonConfig(); 203953315d2SPatrick Williams } 204953315d2SPatrick Williams 2057217c035SPatrick Williams return loadJsonConfig(config); 206953315d2SPatrick Williams } 207