| 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 | #pragma once |
| 17 | |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame] | 18 | #include "chassis.hpp" |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 19 | #include "power_sequencer_device.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 20 | #include "rail.hpp" |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 21 | #include "services.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 22 | |
| 23 | #include <nlohmann/json.hpp> |
| 24 | |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 25 | #include <cstdint> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 26 | #include <filesystem> |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 27 | #include <functional> // for reference_wrapper |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 28 | #include <map> |
| Shawn McCarney | 906cc3f | 2024-02-01 13:33:06 -0600 | [diff] [blame] | 29 | #include <memory> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 30 | #include <string> |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 31 | #include <tuple> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 34 | using json = nlohmann::json; |
| 35 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 36 | namespace phosphor::power::sequencer::config_file_parser |
| 37 | { |
| 38 | |
| 39 | /** |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 40 | * Standard JSON configuration file directory on the BMC. |
| 41 | */ |
| 42 | extern const std::filesystem::path standardConfigFileDirectory; |
| 43 | |
| 44 | /** |
| 45 | * Finds the JSON configuration file for the current system based on the |
| 46 | * specified compatible system types. |
| 47 | * |
| 48 | * This is required when a single BMC firmware image supports multiple system |
| 49 | * types and some system types require different configuration files. |
| 50 | * |
| 51 | * The compatible system types must be ordered from most to least specific. |
| 52 | * Example: |
| 53 | * - com.acme.Hardware.Chassis.Model.MegaServer4CPU |
| 54 | * - com.acme.Hardware.Chassis.Model.MegaServer |
| 55 | * - com.acme.Hardware.Chassis.Model.Server |
| 56 | * |
| 57 | * Throws an exception if an error occurs. |
| 58 | * |
| 59 | * @param compatibleSystemTypes compatible system types for the current system |
| 60 | * ordered from most to least specific |
| 61 | * @param configFileDir directory containing configuration files |
| 62 | * @return path to the JSON configuration file, or an empty path if none was |
| 63 | * found |
| 64 | */ |
| 65 | std::filesystem::path find( |
| 66 | const std::vector<std::string>& compatibleSystemTypes, |
| 67 | const std::filesystem::path& configFileDir = standardConfigFileDirectory); |
| 68 | |
| 69 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 70 | * Parses the specified JSON configuration file. |
| 71 | * |
| 72 | * Returns the corresponding C++ Rail objects. |
| 73 | * |
| 74 | * Throws a ConfigFileParserError if an error occurs. |
| 75 | * |
| 76 | * @param pathName configuration file path name |
| 77 | * @return vector of Rail objects |
| 78 | */ |
| 79 | std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName); |
| 80 | |
| 81 | /* |
| 82 | * Internal implementation details for parse() |
| 83 | */ |
| 84 | namespace internal |
| 85 | { |
| 86 | |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 87 | using JSONRefWrapper = std::reference_wrapper<const json>; |
| 88 | |
| 89 | /** |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame] | 90 | * Parses a JSON element containing a chassis object. |
| 91 | * |
| 92 | * Returns the corresponding C++ Chassis object. |
| 93 | * |
| 94 | * Throws an exception if parsing fails. |
| 95 | * |
| 96 | * @param element JSON element |
| 97 | * @param chassisTemplates chassis templates map |
| 98 | * @param services system services like hardware presence and the journal |
| 99 | * @return Chassis object |
| 100 | */ |
| 101 | std::unique_ptr<Chassis> parseChassis( |
| Shawn McCarney | 1cd07b8 | 2025-11-12 13:29:26 -0600 | [diff] [blame^] | 102 | const json& element, |
| 103 | const std::map<std::string, JSONRefWrapper>& chassisTemplates, |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame] | 104 | Services& services); |
| 105 | |
| 106 | /** |
| 107 | * Parses a JSON element containing an array of chassis objects. |
| 108 | * |
| 109 | * Returns the corresponding C++ Chassis objects. |
| 110 | * |
| 111 | * Throws an exception if parsing fails. |
| 112 | * |
| 113 | * @param element JSON element |
| 114 | * @param chassisTemplates chassis templates map |
| 115 | * @param services system services like hardware presence and the journal |
| 116 | * @return vector of Chassis objects |
| 117 | */ |
| 118 | std::vector<std::unique_ptr<Chassis>> parseChassisArray( |
| Shawn McCarney | 1cd07b8 | 2025-11-12 13:29:26 -0600 | [diff] [blame^] | 119 | const json& element, |
| 120 | const std::map<std::string, JSONRefWrapper>& chassisTemplates, |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame] | 121 | Services& services); |
| 122 | |
| 123 | /** |
| 124 | * Parses a JSON element containing the properties of a chassis. |
| 125 | * |
| 126 | * The JSON element may be a chassis object or chassis_template object. |
| 127 | * |
| 128 | * Returns the corresponding C++ Chassis object. |
| 129 | * |
| 130 | * Throws an exception if parsing fails. |
| 131 | * |
| 132 | * @param element JSON element |
| 133 | * @param isChassisTemplate specifies whether element is a chassis_template |
| 134 | * @param variables variables map used to expand variables in element value |
| 135 | * @param services system services like hardware presence and the journal |
| 136 | * @return Chassis object |
| 137 | */ |
| 138 | std::unique_ptr<Chassis> parseChassisProperties( |
| 139 | const json& element, bool isChassisTemplate, |
| 140 | const std::map<std::string, std::string>& variables, Services& services); |
| 141 | |
| 142 | /** |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 143 | * Parses a JSON element containing a chassis_template object. |
| 144 | * |
| 145 | * Returns the template ID and a C++ reference_wrapper to the JSON element. |
| 146 | * |
| Shawn McCarney | 1cd07b8 | 2025-11-12 13:29:26 -0600 | [diff] [blame^] | 147 | * A chassis_template object cannot be fully parsed in isolation. It is a |
| 148 | * template that contains variables. |
| 149 | * |
| 150 | * The chassis_template object is used by one or more chassis objects to avoid |
| 151 | * duplicate JSON. The chassis objects define chassis-specific values for the |
| 152 | * template variables. |
| 153 | * |
| 154 | * When the chassis object is parsed, the chassis_template JSON will be |
| 155 | * re-parsed, and the template variables will be replaced with the |
| 156 | * chassis-specific values. |
| 157 | * |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 158 | * Throws an exception if parsing fails. |
| 159 | * |
| 160 | * @param element JSON element |
| 161 | * @return template ID and reference_wrapper to JSON element |
| 162 | */ |
| 163 | std::tuple<std::string, JSONRefWrapper> parseChassisTemplate( |
| 164 | const json& element); |
| 165 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 166 | /** |
| Shawn McCarney | 1cd07b8 | 2025-11-12 13:29:26 -0600 | [diff] [blame^] | 167 | * Parses a JSON element containing an array of chassis_template objects. |
| 168 | * |
| 169 | * Returns a map of template IDs to chassis_template JSON elements. |
| 170 | * |
| 171 | * Note that chassis_template objects cannot be fully parsed in isolation. See |
| 172 | * parseChassisTemplate() for more information. |
| 173 | * |
| 174 | * Throws an exception if parsing fails. |
| 175 | * |
| 176 | * @param element JSON element |
| 177 | * @return chassis templates map |
| 178 | */ |
| 179 | std::map<std::string, JSONRefWrapper> parseChassisTemplateArray( |
| 180 | const json& element); |
| 181 | |
| 182 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 183 | * Parses a JSON element containing a GPIO. |
| 184 | * |
| 185 | * Returns the corresponding C++ GPIO object. |
| 186 | * |
| 187 | * Throws an exception if parsing fails. |
| 188 | * |
| 189 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 190 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 191 | * @return GPIO object |
| 192 | */ |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 193 | GPIO parseGPIO(const json& element, |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 194 | const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 195 | |
| 196 | /** |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 197 | * Parses a JSON element containing an i2c_interface object. |
| 198 | * |
| 199 | * Returns the corresponding I2C bus and address. |
| 200 | * |
| 201 | * Throws an exception if parsing fails. |
| 202 | * |
| 203 | * @param element JSON element |
| 204 | * @param variables variables map used to expand variables in element value |
| 205 | * @return tuple containing bus and address |
| 206 | */ |
| 207 | std::tuple<uint8_t, uint16_t> parseI2CInterface( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 208 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 209 | |
| 210 | /** |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 211 | * Parses a JSON element containing a power_sequencer object. |
| 212 | * |
| 213 | * Returns the corresponding C++ PowerSequencerDevice object. |
| 214 | * |
| 215 | * Throws an exception if parsing fails. |
| 216 | * |
| 217 | * @param element JSON element |
| 218 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 219 | * @param services system services like hardware presence and the journal |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 220 | * @return PowerSequencerDevice object |
| 221 | */ |
| 222 | std::unique_ptr<PowerSequencerDevice> parsePowerSequencer( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 223 | const json& element, const std::map<std::string, std::string>& variables, |
| 224 | Services& services); |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Parses a JSON element containing an array of power_sequencer objects. |
| 228 | * |
| 229 | * Returns the corresponding C++ PowerSequencerDevice objects. |
| 230 | * |
| 231 | * Throws an exception if parsing fails. |
| 232 | * |
| 233 | * @param element JSON element |
| 234 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 235 | * @param services system services like hardware presence and the journal |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 236 | * @return vector of PowerSequencerDevice objects |
| 237 | */ |
| 238 | std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 239 | const json& element, const std::map<std::string, std::string>& variables, |
| 240 | Services& services); |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 241 | |
| 242 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 243 | * Parses a JSON element containing a rail. |
| 244 | * |
| 245 | * Returns the corresponding C++ Rail object. |
| 246 | * |
| 247 | * Throws an exception if parsing fails. |
| 248 | * |
| 249 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 250 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 251 | * @return Rail object |
| 252 | */ |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 253 | std::unique_ptr<Rail> parseRail( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 254 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Parses a JSON element containing an array of rails. |
| 258 | * |
| 259 | * Returns the corresponding C++ Rail objects. |
| 260 | * |
| 261 | * Throws an exception if parsing fails. |
| 262 | * |
| 263 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 264 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 265 | * @return vector of Rail objects |
| 266 | */ |
| Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 267 | std::vector<std::unique_ptr<Rail>> parseRailArray( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 268 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Parses the JSON root element of the entire configuration file. |
| 272 | * |
| 273 | * Returns the corresponding C++ Rail objects. |
| 274 | * |
| 275 | * Throws an exception if parsing fails. |
| 276 | * |
| 277 | * @param element JSON element |
| 278 | * @return vector of Rail objects |
| 279 | */ |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 280 | std::vector<std::unique_ptr<Rail>> parseRoot(const json& element); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 281 | |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame] | 282 | /** |
| 283 | * Parses a JSON element containing an object with variable names and values. |
| 284 | * |
| 285 | * Returns the corresponding C++ map of variable names and values. |
| 286 | * |
| 287 | * Throws an exception if parsing fails. |
| 288 | * |
| 289 | * @param element JSON element |
| 290 | * @return map of variable names and values |
| 291 | */ |
| 292 | std::map<std::string, std::string> parseVariables(const json& element); |
| 293 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 294 | } // namespace internal |
| 295 | |
| 296 | } // namespace phosphor::power::sequencer::config_file_parser |