| 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 | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 18 | #include "power_sequencer_device.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 19 | #include "rail.hpp" |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 20 | #include "services.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 21 | |
| 22 | #include <nlohmann/json.hpp> |
| 23 | |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 24 | #include <cstdint> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 25 | #include <filesystem> |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 26 | #include <functional> // for reference_wrapper |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 27 | #include <map> |
| Shawn McCarney | 906cc3f | 2024-02-01 13:33:06 -0600 | [diff] [blame] | 28 | #include <memory> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 29 | #include <string> |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 30 | #include <tuple> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 33 | using json = nlohmann::json; |
| 34 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 35 | namespace phosphor::power::sequencer::config_file_parser |
| 36 | { |
| 37 | |
| 38 | /** |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 39 | * Standard JSON configuration file directory on the BMC. |
| 40 | */ |
| 41 | extern const std::filesystem::path standardConfigFileDirectory; |
| 42 | |
| 43 | /** |
| 44 | * Finds the JSON configuration file for the current system based on the |
| 45 | * specified compatible system types. |
| 46 | * |
| 47 | * This is required when a single BMC firmware image supports multiple system |
| 48 | * types and some system types require different configuration files. |
| 49 | * |
| 50 | * The compatible system types must be ordered from most to least specific. |
| 51 | * Example: |
| 52 | * - com.acme.Hardware.Chassis.Model.MegaServer4CPU |
| 53 | * - com.acme.Hardware.Chassis.Model.MegaServer |
| 54 | * - com.acme.Hardware.Chassis.Model.Server |
| 55 | * |
| 56 | * Throws an exception if an error occurs. |
| 57 | * |
| 58 | * @param compatibleSystemTypes compatible system types for the current system |
| 59 | * ordered from most to least specific |
| 60 | * @param configFileDir directory containing configuration files |
| 61 | * @return path to the JSON configuration file, or an empty path if none was |
| 62 | * found |
| 63 | */ |
| 64 | std::filesystem::path find( |
| 65 | const std::vector<std::string>& compatibleSystemTypes, |
| 66 | const std::filesystem::path& configFileDir = standardConfigFileDirectory); |
| 67 | |
| 68 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 69 | * Parses the specified JSON configuration file. |
| 70 | * |
| 71 | * Returns the corresponding C++ Rail objects. |
| 72 | * |
| 73 | * Throws a ConfigFileParserError if an error occurs. |
| 74 | * |
| 75 | * @param pathName configuration file path name |
| 76 | * @return vector of Rail objects |
| 77 | */ |
| 78 | std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName); |
| 79 | |
| 80 | /* |
| 81 | * Internal implementation details for parse() |
| 82 | */ |
| 83 | namespace internal |
| 84 | { |
| 85 | |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 86 | using JSONRefWrapper = std::reference_wrapper<const json>; |
| 87 | |
| 88 | /** |
| 89 | * Parses a JSON element containing a chassis_template object. |
| 90 | * |
| 91 | * Returns the template ID and a C++ reference_wrapper to the JSON element. |
| 92 | * |
| 93 | * Throws an exception if parsing fails. |
| 94 | * |
| 95 | * @param element JSON element |
| 96 | * @return template ID and reference_wrapper to JSON element |
| 97 | */ |
| 98 | std::tuple<std::string, JSONRefWrapper> parseChassisTemplate( |
| 99 | const json& element); |
| 100 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 101 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 102 | * Parses a JSON element containing a GPIO. |
| 103 | * |
| 104 | * Returns the corresponding C++ GPIO object. |
| 105 | * |
| 106 | * Throws an exception if parsing fails. |
| 107 | * |
| 108 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 109 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 110 | * @return GPIO object |
| 111 | */ |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 112 | GPIO parseGPIO(const json& element, |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 113 | const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 114 | |
| 115 | /** |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 116 | * Parses a JSON element containing an i2c_interface object. |
| 117 | * |
| 118 | * Returns the corresponding I2C bus and address. |
| 119 | * |
| 120 | * Throws an exception if parsing fails. |
| 121 | * |
| 122 | * @param element JSON element |
| 123 | * @param variables variables map used to expand variables in element value |
| 124 | * @return tuple containing bus and address |
| 125 | */ |
| 126 | std::tuple<uint8_t, uint16_t> parseI2CInterface( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 127 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 128 | |
| 129 | /** |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 130 | * Parses a JSON element containing a power_sequencer object. |
| 131 | * |
| 132 | * Returns the corresponding C++ PowerSequencerDevice object. |
| 133 | * |
| 134 | * Throws an exception if parsing fails. |
| 135 | * |
| 136 | * @param element JSON element |
| 137 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 138 | * @param services system services like hardware presence and the journal |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 139 | * @return PowerSequencerDevice object |
| 140 | */ |
| 141 | std::unique_ptr<PowerSequencerDevice> parsePowerSequencer( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 142 | const json& element, const std::map<std::string, std::string>& variables, |
| 143 | Services& services); |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Parses a JSON element containing an array of power_sequencer objects. |
| 147 | * |
| 148 | * Returns the corresponding C++ PowerSequencerDevice objects. |
| 149 | * |
| 150 | * Throws an exception if parsing fails. |
| 151 | * |
| 152 | * @param element JSON element |
| 153 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 154 | * @param services system services like hardware presence and the journal |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 155 | * @return vector of PowerSequencerDevice objects |
| 156 | */ |
| 157 | std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 158 | const json& element, const std::map<std::string, std::string>& variables, |
| 159 | Services& services); |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 160 | |
| 161 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 162 | * Parses a JSON element containing a rail. |
| 163 | * |
| 164 | * Returns the corresponding C++ Rail object. |
| 165 | * |
| 166 | * Throws an exception if parsing fails. |
| 167 | * |
| 168 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 169 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 170 | * @return Rail object |
| 171 | */ |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 172 | std::unique_ptr<Rail> parseRail( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 173 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Parses a JSON element containing an array of rails. |
| 177 | * |
| 178 | * Returns the corresponding C++ Rail objects. |
| 179 | * |
| 180 | * Throws an exception if parsing fails. |
| 181 | * |
| 182 | * @param element JSON element |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 183 | * @param variables variables map used to expand variables in element value |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 184 | * @return vector of Rail objects |
| 185 | */ |
| Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 186 | std::vector<std::unique_ptr<Rail>> parseRailArray( |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 187 | const json& element, const std::map<std::string, std::string>& variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Parses the JSON root element of the entire configuration file. |
| 191 | * |
| 192 | * Returns the corresponding C++ Rail objects. |
| 193 | * |
| 194 | * Throws an exception if parsing fails. |
| 195 | * |
| 196 | * @param element JSON element |
| 197 | * @return vector of Rail objects |
| 198 | */ |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame^] | 199 | std::vector<std::unique_ptr<Rail>> parseRoot(const json& element); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 200 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 201 | } // namespace internal |
| 202 | |
| 203 | } // namespace phosphor::power::sequencer::config_file_parser |