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