| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2024 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "config_file_parser.hpp" |
| 18 | |
| 19 | #include "config_file_parser_error.hpp" |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 20 | #include "json_parser_utils.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 21 | |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 22 | #include <cstdint> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 23 | #include <exception> |
| 24 | #include <fstream> |
| 25 | #include <optional> |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 26 | #include <stdexcept> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 27 | |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 28 | using namespace phosphor::power::json_parser_utils; |
| 29 | using ConfigFileParserError = phosphor::power::util::ConfigFileParserError; |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 30 | using json = nlohmann::json; |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 31 | namespace fs = std::filesystem; |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 32 | |
| 33 | namespace phosphor::power::sequencer::config_file_parser |
| 34 | { |
| 35 | |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 36 | const std::filesystem::path standardConfigFileDirectory{ |
| 37 | "/usr/share/phosphor-power-sequencer"}; |
| 38 | |
| Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 39 | std::filesystem::path find( |
| 40 | const std::vector<std::string>& compatibleSystemTypes, |
| 41 | const std::filesystem::path& configFileDir) |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 42 | { |
| 43 | fs::path pathName, possiblePath; |
| 44 | std::string fileName; |
| 45 | |
| 46 | for (const std::string& systemType : compatibleSystemTypes) |
| 47 | { |
| 48 | // Look for file name that is entire system type + ".json" |
| 49 | // Example: com.acme.Hardware.Chassis.Model.MegaServer.json |
| 50 | fileName = systemType + ".json"; |
| 51 | possiblePath = configFileDir / fileName; |
| 52 | if (fs::is_regular_file(possiblePath)) |
| 53 | { |
| 54 | pathName = possiblePath; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | // Look for file name that is last node of system type + ".json" |
| 59 | // Example: MegaServer.json |
| 60 | std::string::size_type pos = systemType.rfind('.'); |
| 61 | if ((pos != std::string::npos) && ((systemType.size() - pos) > 1)) |
| 62 | { |
| 63 | fileName = systemType.substr(pos + 1) + ".json"; |
| 64 | possiblePath = configFileDir / fileName; |
| 65 | if (fs::is_regular_file(possiblePath)) |
| 66 | { |
| 67 | pathName = possiblePath; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return pathName; |
| 74 | } |
| 75 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 76 | std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName) |
| 77 | { |
| 78 | try |
| 79 | { |
| 80 | // Use standard JSON parser to create tree of JSON elements |
| 81 | std::ifstream file{pathName}; |
| 82 | json rootElement = json::parse(file); |
| 83 | |
| 84 | // Parse tree of JSON elements and return corresponding C++ objects |
| 85 | return internal::parseRoot(rootElement); |
| 86 | } |
| 87 | catch (const std::exception& e) |
| 88 | { |
| 89 | throw ConfigFileParserError{pathName, e.what()}; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | namespace internal |
| 94 | { |
| 95 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 96 | GPIO parseGPIO(const json& element, |
| 97 | const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 98 | { |
| 99 | verifyIsObject(element); |
| 100 | unsigned int propertyCount{0}; |
| 101 | |
| 102 | // Required line property |
| 103 | const json& lineElement = getRequiredProperty(element, "line"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 104 | unsigned int line = parseUnsignedInteger(lineElement, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 105 | ++propertyCount; |
| 106 | |
| 107 | // Optional active_low property |
| 108 | bool activeLow{false}; |
| 109 | auto activeLowIt = element.find("active_low"); |
| 110 | if (activeLowIt != element.end()) |
| 111 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 112 | activeLow = parseBoolean(*activeLowIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 113 | ++propertyCount; |
| 114 | } |
| 115 | |
| 116 | // Verify no invalid properties exist |
| 117 | verifyPropertyCount(element, propertyCount); |
| 118 | |
| 119 | return GPIO(line, activeLow); |
| 120 | } |
| 121 | |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 122 | std::tuple<uint8_t, uint16_t> parseI2CInterface( |
| 123 | const nlohmann::json& element, |
| 124 | const std::map<std::string, std::string>& variables) |
| 125 | { |
| 126 | verifyIsObject(element); |
| 127 | unsigned int propertyCount{0}; |
| 128 | |
| 129 | // Required bus property |
| 130 | const json& busElement = getRequiredProperty(element, "bus"); |
| 131 | uint8_t bus = parseUint8(busElement, variables); |
| 132 | ++propertyCount; |
| 133 | |
| 134 | // Required address property |
| 135 | const json& addressElement = getRequiredProperty(element, "address"); |
| 136 | uint16_t address = parseHexByte(addressElement, variables); |
| 137 | ++propertyCount; |
| 138 | |
| 139 | // Verify no invalid properties exist |
| 140 | verifyPropertyCount(element, propertyCount); |
| 141 | |
| 142 | return {bus, address}; |
| 143 | } |
| 144 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 145 | std::unique_ptr<Rail> parseRail( |
| 146 | const json& element, const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 147 | { |
| 148 | verifyIsObject(element); |
| 149 | unsigned int propertyCount{0}; |
| 150 | |
| 151 | // Required name property |
| 152 | const json& nameElement = getRequiredProperty(element, "name"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 153 | std::string name = parseString(nameElement, false, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 154 | ++propertyCount; |
| 155 | |
| 156 | // Optional presence property |
| 157 | std::optional<std::string> presence{}; |
| 158 | auto presenceIt = element.find("presence"); |
| 159 | if (presenceIt != element.end()) |
| 160 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 161 | presence = parseString(*presenceIt, false, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 162 | ++propertyCount; |
| 163 | } |
| 164 | |
| 165 | // Optional page property |
| 166 | std::optional<uint8_t> page{}; |
| 167 | auto pageIt = element.find("page"); |
| 168 | if (pageIt != element.end()) |
| 169 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 170 | page = parseUint8(*pageIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 171 | ++propertyCount; |
| 172 | } |
| 173 | |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 174 | // Optional is_power_supply_rail property |
| 175 | bool isPowerSupplyRail{false}; |
| 176 | auto isPowerSupplyRailIt = element.find("is_power_supply_rail"); |
| 177 | if (isPowerSupplyRailIt != element.end()) |
| 178 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 179 | isPowerSupplyRail = parseBoolean(*isPowerSupplyRailIt, variables); |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 180 | ++propertyCount; |
| 181 | } |
| 182 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 183 | // Optional check_status_vout property |
| 184 | bool checkStatusVout{false}; |
| 185 | auto checkStatusVoutIt = element.find("check_status_vout"); |
| 186 | if (checkStatusVoutIt != element.end()) |
| 187 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 188 | checkStatusVout = parseBoolean(*checkStatusVoutIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 189 | ++propertyCount; |
| 190 | } |
| 191 | |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 192 | // Optional compare_voltage_to_limit property |
| 193 | bool compareVoltageToLimit{false}; |
| 194 | auto compareVoltageToLimitIt = element.find("compare_voltage_to_limit"); |
| 195 | if (compareVoltageToLimitIt != element.end()) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 196 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 197 | compareVoltageToLimit = |
| 198 | parseBoolean(*compareVoltageToLimitIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 199 | ++propertyCount; |
| 200 | } |
| 201 | |
| 202 | // Optional gpio property |
| 203 | std::optional<GPIO> gpio{}; |
| 204 | auto gpioIt = element.find("gpio"); |
| 205 | if (gpioIt != element.end()) |
| 206 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 207 | gpio = parseGPIO(*gpioIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 208 | ++propertyCount; |
| 209 | } |
| 210 | |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 211 | // If check_status_vout or compare_voltage_to_limit property is true, the |
| 212 | // page property is required; verify page was specified |
| 213 | if ((checkStatusVout || compareVoltageToLimit) && !page.has_value()) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 214 | { |
| 215 | throw std::invalid_argument{"Required property missing: page"}; |
| 216 | } |
| 217 | |
| 218 | // Verify no invalid properties exist |
| 219 | verifyPropertyCount(element, propertyCount); |
| 220 | |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 221 | return std::make_unique<Rail>(name, presence, page, isPowerSupplyRail, |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 222 | checkStatusVout, compareVoltageToLimit, gpio); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 225 | std::vector<std::unique_ptr<Rail>> parseRailArray( |
| 226 | const json& element, const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 227 | { |
| 228 | verifyIsArray(element); |
| 229 | std::vector<std::unique_ptr<Rail>> rails; |
| 230 | for (auto& railElement : element) |
| 231 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 232 | rails.emplace_back(parseRail(railElement, variables)); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 233 | } |
| 234 | return rails; |
| 235 | } |
| 236 | |
| 237 | std::vector<std::unique_ptr<Rail>> parseRoot(const json& element) |
| 238 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 239 | std::map<std::string, std::string> variables{}; |
| 240 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 241 | verifyIsObject(element); |
| 242 | unsigned int propertyCount{0}; |
| 243 | |
| 244 | // Required rails property |
| 245 | const json& railsElement = getRequiredProperty(element, "rails"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame^] | 246 | std::vector<std::unique_ptr<Rail>> rails = |
| 247 | parseRailArray(railsElement, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 248 | ++propertyCount; |
| 249 | |
| 250 | // Verify no invalid properties exist |
| 251 | verifyPropertyCount(element, propertyCount); |
| 252 | |
| 253 | return rails; |
| 254 | } |
| 255 | |
| 256 | } // namespace internal |
| 257 | |
| 258 | } // namespace phosphor::power::sequencer::config_file_parser |