blob: 665029eb6decc9387f5108af84b40514c0372077 [file] [log] [blame]
Shawn McCarney6a957f62024-01-10 16:15:19 -06001/**
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 McCarneye02aa3c2025-11-10 12:25:54 -060018#include "chassis.hpp"
Shawn McCarney4840b252025-11-06 16:06:40 -060019#include "power_sequencer_device.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060020#include "rail.hpp"
Shawn McCarney4840b252025-11-06 16:06:40 -060021#include "services.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060022
23#include <nlohmann/json.hpp>
24
Shawn McCarneye140ed92025-11-06 11:20:38 -060025#include <cstdint>
Shawn McCarney6a957f62024-01-10 16:15:19 -060026#include <filesystem>
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060027#include <functional> // for reference_wrapper
Shawn McCarneye140ed92025-11-06 11:20:38 -060028#include <map>
Shawn McCarney906cc3f2024-02-01 13:33:06 -060029#include <memory>
Shawn McCarney6a957f62024-01-10 16:15:19 -060030#include <string>
Shawn McCarneye140ed92025-11-06 11:20:38 -060031#include <tuple>
Shawn McCarney6a957f62024-01-10 16:15:19 -060032#include <vector>
33
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060034using json = nlohmann::json;
35
Shawn McCarney6a957f62024-01-10 16:15:19 -060036namespace phosphor::power::sequencer::config_file_parser
37{
38
39/**
Shawn McCarneye9144ab2024-05-22 12:34:18 -050040 * Standard JSON configuration file directory on the BMC.
41 */
42extern 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 */
65std::filesystem::path find(
66 const std::vector<std::string>& compatibleSystemTypes,
67 const std::filesystem::path& configFileDir = standardConfigFileDirectory);
68
69/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060070 * 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 */
79std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName);
80
81/*
82 * Internal implementation details for parse()
83 */
84namespace internal
85{
86
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060087using JSONRefWrapper = std::reference_wrapper<const json>;
88
89/**
Shawn McCarneye02aa3c2025-11-10 12:25:54 -060090 * 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 */
101std::unique_ptr<Chassis> parseChassis(
102 const json& element, std::map<std::string, JSONRefWrapper> chassisTemplates,
103 Services& services);
104
105/**
106 * Parses a JSON element containing an array of chassis objects.
107 *
108 * Returns the corresponding C++ Chassis objects.
109 *
110 * Throws an exception if parsing fails.
111 *
112 * @param element JSON element
113 * @param chassisTemplates chassis templates map
114 * @param services system services like hardware presence and the journal
115 * @return vector of Chassis objects
116 */
117std::vector<std::unique_ptr<Chassis>> parseChassisArray(
118 const json& element, std::map<std::string, JSONRefWrapper> chassisTemplates,
119 Services& services);
120
121/**
122 * Parses a JSON element containing the properties of a chassis.
123 *
124 * The JSON element may be a chassis object or chassis_template object.
125 *
126 * Returns the corresponding C++ Chassis object.
127 *
128 * Throws an exception if parsing fails.
129 *
130 * @param element JSON element
131 * @param isChassisTemplate specifies whether element is a chassis_template
132 * @param variables variables map used to expand variables in element value
133 * @param services system services like hardware presence and the journal
134 * @return Chassis object
135 */
136std::unique_ptr<Chassis> parseChassisProperties(
137 const json& element, bool isChassisTemplate,
138 const std::map<std::string, std::string>& variables, Services& services);
139
140/**
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600141 * Parses a JSON element containing a chassis_template object.
142 *
143 * Returns the template ID and a C++ reference_wrapper to the JSON element.
144 *
145 * Throws an exception if parsing fails.
146 *
147 * @param element JSON element
148 * @return template ID and reference_wrapper to JSON element
149 */
150std::tuple<std::string, JSONRefWrapper> parseChassisTemplate(
151 const json& element);
152
Shawn McCarney6a957f62024-01-10 16:15:19 -0600153/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600154 * Parses a JSON element containing a GPIO.
155 *
156 * Returns the corresponding C++ GPIO object.
157 *
158 * Throws an exception if parsing fails.
159 *
160 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600161 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600162 * @return GPIO object
163 */
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600164GPIO parseGPIO(const json& element,
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600165 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600166
167/**
Shawn McCarneye140ed92025-11-06 11:20:38 -0600168 * Parses a JSON element containing an i2c_interface object.
169 *
170 * Returns the corresponding I2C bus and address.
171 *
172 * Throws an exception if parsing fails.
173 *
174 * @param element JSON element
175 * @param variables variables map used to expand variables in element value
176 * @return tuple containing bus and address
177 */
178std::tuple<uint8_t, uint16_t> parseI2CInterface(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600179 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarneye140ed92025-11-06 11:20:38 -0600180
181/**
Shawn McCarney4840b252025-11-06 16:06:40 -0600182 * Parses a JSON element containing a power_sequencer object.
183 *
184 * Returns the corresponding C++ PowerSequencerDevice object.
185 *
186 * Throws an exception if parsing fails.
187 *
188 * @param element JSON element
189 * @param variables variables map used to expand variables in element value
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600190 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600191 * @return PowerSequencerDevice object
192 */
193std::unique_ptr<PowerSequencerDevice> parsePowerSequencer(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600194 const json& element, const std::map<std::string, std::string>& variables,
195 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600196
197/**
198 * Parses a JSON element containing an array of power_sequencer objects.
199 *
200 * Returns the corresponding C++ PowerSequencerDevice objects.
201 *
202 * Throws an exception if parsing fails.
203 *
204 * @param element JSON element
205 * @param variables variables map used to expand variables in element value
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600206 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600207 * @return vector of PowerSequencerDevice objects
208 */
209std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600210 const json& element, const std::map<std::string, std::string>& variables,
211 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600212
213/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600214 * Parses a JSON element containing a rail.
215 *
216 * Returns the corresponding C++ Rail object.
217 *
218 * Throws an exception if parsing fails.
219 *
220 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600221 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600222 * @return Rail object
223 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600224std::unique_ptr<Rail> parseRail(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600225 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600226
227/**
228 * Parses a JSON element containing an array of rails.
229 *
230 * Returns the corresponding C++ Rail objects.
231 *
232 * Throws an exception if parsing fails.
233 *
234 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600235 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600236 * @return vector of Rail objects
237 */
Patrick Williams92261f82025-02-01 08:22:34 -0500238std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600239 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600240
241/**
242 * Parses the JSON root element of the entire configuration file.
243 *
244 * Returns the corresponding C++ Rail objects.
245 *
246 * Throws an exception if parsing fails.
247 *
248 * @param element JSON element
249 * @return vector of Rail objects
250 */
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600251std::vector<std::unique_ptr<Rail>> parseRoot(const json& element);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600252
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600253/**
254 * Parses a JSON element containing an object with variable names and values.
255 *
256 * Returns the corresponding C++ map of variable names and values.
257 *
258 * Throws an exception if parsing fails.
259 *
260 * @param element JSON element
261 * @return map of variable names and values
262 */
263std::map<std::string, std::string> parseVariables(const json& element);
264
Shawn McCarney6a957f62024-01-10 16:15:19 -0600265} // namespace internal
266
267} // namespace phosphor::power::sequencer::config_file_parser