| 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 | |
| 18 | #include "rail.hpp" |
| 19 | |
| 20 | #include <nlohmann/json.hpp> |
| 21 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 22 | #include <filesystem> |
| Shawn McCarney | 906cc3f | 2024-02-01 13:33:06 -0600 | [diff] [blame] | 23 | #include <memory> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace phosphor::power::sequencer::config_file_parser |
| 28 | { |
| 29 | |
| 30 | /** |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 31 | * Standard JSON configuration file directory on the BMC. |
| 32 | */ |
| 33 | extern const std::filesystem::path standardConfigFileDirectory; |
| 34 | |
| 35 | /** |
| 36 | * Finds the JSON configuration file for the current system based on the |
| 37 | * specified compatible system types. |
| 38 | * |
| 39 | * This is required when a single BMC firmware image supports multiple system |
| 40 | * types and some system types require different configuration files. |
| 41 | * |
| 42 | * The compatible system types must be ordered from most to least specific. |
| 43 | * Example: |
| 44 | * - com.acme.Hardware.Chassis.Model.MegaServer4CPU |
| 45 | * - com.acme.Hardware.Chassis.Model.MegaServer |
| 46 | * - com.acme.Hardware.Chassis.Model.Server |
| 47 | * |
| 48 | * Throws an exception if an error occurs. |
| 49 | * |
| 50 | * @param compatibleSystemTypes compatible system types for the current system |
| 51 | * ordered from most to least specific |
| 52 | * @param configFileDir directory containing configuration files |
| 53 | * @return path to the JSON configuration file, or an empty path if none was |
| 54 | * found |
| 55 | */ |
| 56 | std::filesystem::path find( |
| 57 | const std::vector<std::string>& compatibleSystemTypes, |
| 58 | const std::filesystem::path& configFileDir = standardConfigFileDirectory); |
| 59 | |
| 60 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 61 | * Parses the specified JSON configuration file. |
| 62 | * |
| 63 | * Returns the corresponding C++ Rail objects. |
| 64 | * |
| 65 | * Throws a ConfigFileParserError if an error occurs. |
| 66 | * |
| 67 | * @param pathName configuration file path name |
| 68 | * @return vector of Rail objects |
| 69 | */ |
| 70 | std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName); |
| 71 | |
| 72 | /* |
| 73 | * Internal implementation details for parse() |
| 74 | */ |
| 75 | namespace internal |
| 76 | { |
| 77 | |
| 78 | /** |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 79 | * Parses a JSON element containing a GPIO. |
| 80 | * |
| 81 | * Returns the corresponding C++ GPIO object. |
| 82 | * |
| 83 | * Throws an exception if parsing fails. |
| 84 | * |
| 85 | * @param element JSON element |
| 86 | * @return GPIO object |
| 87 | */ |
| 88 | GPIO parseGPIO(const nlohmann::json& element); |
| 89 | |
| 90 | /** |
| 91 | * Parses a JSON element containing a rail. |
| 92 | * |
| 93 | * Returns the corresponding C++ Rail object. |
| 94 | * |
| 95 | * Throws an exception if parsing fails. |
| 96 | * |
| 97 | * @param element JSON element |
| 98 | * @return Rail object |
| 99 | */ |
| 100 | std::unique_ptr<Rail> parseRail(const nlohmann::json& element); |
| 101 | |
| 102 | /** |
| 103 | * Parses a JSON element containing an array of rails. |
| 104 | * |
| 105 | * Returns the corresponding C++ Rail objects. |
| 106 | * |
| 107 | * Throws an exception if parsing fails. |
| 108 | * |
| 109 | * @param element JSON element |
| 110 | * @return vector of Rail objects |
| 111 | */ |
| Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 112 | std::vector<std::unique_ptr<Rail>> parseRailArray( |
| 113 | const nlohmann::json& element); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Parses the JSON root element of the entire configuration file. |
| 117 | * |
| 118 | * Returns the corresponding C++ Rail objects. |
| 119 | * |
| 120 | * Throws an exception if parsing fails. |
| 121 | * |
| 122 | * @param element JSON element |
| 123 | * @return vector of Rail objects |
| 124 | */ |
| 125 | std::vector<std::unique_ptr<Rail>> parseRoot(const nlohmann::json& element); |
| 126 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 127 | } // namespace internal |
| 128 | |
| 129 | } // namespace phosphor::power::sequencer::config_file_parser |