blob: f43ad0eeeb8936e3ff4a7c18a8692c209da9da9a [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(
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600102 const json& element,
103 const std::map<std::string, JSONRefWrapper>& chassisTemplates,
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600104 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 */
118std::vector<std::unique_ptr<Chassis>> parseChassisArray(
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600119 const json& element,
120 const std::map<std::string, JSONRefWrapper>& chassisTemplates,
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600121 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 */
138std::unique_ptr<Chassis> parseChassisProperties(
139 const json& element, bool isChassisTemplate,
140 const std::map<std::string, std::string>& variables, Services& services);
141
142/**
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600143 * 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 McCarney1cd07b82025-11-12 13:29:26 -0600147 * 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 McCarneyfb7e0932025-11-10 10:23:05 -0600158 * Throws an exception if parsing fails.
159 *
160 * @param element JSON element
161 * @return template ID and reference_wrapper to JSON element
162 */
163std::tuple<std::string, JSONRefWrapper> parseChassisTemplate(
164 const json& element);
165
Shawn McCarney6a957f62024-01-10 16:15:19 -0600166/**
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600167 * 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 */
179std::map<std::string, JSONRefWrapper> parseChassisTemplateArray(
180 const json& element);
181
182/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600183 * 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 McCarney038f2ba2025-11-06 13:32:16 -0600190 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600191 * @return GPIO object
192 */
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600193GPIO parseGPIO(const json& element,
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600194 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600195
196/**
Shawn McCarneye140ed92025-11-06 11:20:38 -0600197 * 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 */
207std::tuple<uint8_t, uint16_t> parseI2CInterface(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600208 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarneye140ed92025-11-06 11:20:38 -0600209
210/**
Shawn McCarney4840b252025-11-06 16:06:40 -0600211 * 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 McCarneyfb7e0932025-11-10 10:23:05 -0600219 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600220 * @return PowerSequencerDevice object
221 */
222std::unique_ptr<PowerSequencerDevice> parsePowerSequencer(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600223 const json& element, const std::map<std::string, std::string>& variables,
224 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600225
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 McCarneyfb7e0932025-11-10 10:23:05 -0600235 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600236 * @return vector of PowerSequencerDevice objects
237 */
238std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600239 const json& element, const std::map<std::string, std::string>& variables,
240 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600241
242/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600243 * 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 McCarney038f2ba2025-11-06 13:32:16 -0600250 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600251 * @return Rail object
252 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600253std::unique_ptr<Rail> parseRail(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600254 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600255
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 McCarney038f2ba2025-11-06 13:32:16 -0600264 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600265 * @return vector of Rail objects
266 */
Patrick Williams92261f82025-02-01 08:22:34 -0500267std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600268 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600269
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 McCarneyfb7e0932025-11-10 10:23:05 -0600280std::vector<std::unique_ptr<Rail>> parseRoot(const json& element);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600281
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600282/**
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 */
292std::map<std::string, std::string> parseVariables(const json& element);
293
Shawn McCarney6a957f62024-01-10 16:15:19 -0600294} // namespace internal
295
296} // namespace phosphor::power::sequencer::config_file_parser