blob: fcae99affc8ec8598e76921e08733ced8dcae253 [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 McCarney4840b252025-11-06 16:06:40 -060018#include "power_sequencer_device.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060019#include "rail.hpp"
Shawn McCarney4840b252025-11-06 16:06:40 -060020#include "services.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060021
22#include <nlohmann/json.hpp>
23
Shawn McCarneye140ed92025-11-06 11:20:38 -060024#include <cstdint>
Shawn McCarney6a957f62024-01-10 16:15:19 -060025#include <filesystem>
Shawn McCarneye140ed92025-11-06 11:20:38 -060026#include <map>
Shawn McCarney906cc3f2024-02-01 13:33:06 -060027#include <memory>
Shawn McCarney6a957f62024-01-10 16:15:19 -060028#include <string>
Shawn McCarneye140ed92025-11-06 11:20:38 -060029#include <tuple>
Shawn McCarney6a957f62024-01-10 16:15:19 -060030#include <vector>
31
32namespace phosphor::power::sequencer::config_file_parser
33{
34
35/**
Shawn McCarneye9144ab2024-05-22 12:34:18 -050036 * Standard JSON configuration file directory on the BMC.
37 */
38extern const std::filesystem::path standardConfigFileDirectory;
39
40/**
41 * Finds the JSON configuration file for the current system based on the
42 * specified compatible system types.
43 *
44 * This is required when a single BMC firmware image supports multiple system
45 * types and some system types require different configuration files.
46 *
47 * The compatible system types must be ordered from most to least specific.
48 * Example:
49 * - com.acme.Hardware.Chassis.Model.MegaServer4CPU
50 * - com.acme.Hardware.Chassis.Model.MegaServer
51 * - com.acme.Hardware.Chassis.Model.Server
52 *
53 * Throws an exception if an error occurs.
54 *
55 * @param compatibleSystemTypes compatible system types for the current system
56 * ordered from most to least specific
57 * @param configFileDir directory containing configuration files
58 * @return path to the JSON configuration file, or an empty path if none was
59 * found
60 */
61std::filesystem::path find(
62 const std::vector<std::string>& compatibleSystemTypes,
63 const std::filesystem::path& configFileDir = standardConfigFileDirectory);
64
65/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060066 * Parses the specified JSON configuration file.
67 *
68 * Returns the corresponding C++ Rail objects.
69 *
70 * Throws a ConfigFileParserError if an error occurs.
71 *
72 * @param pathName configuration file path name
73 * @return vector of Rail objects
74 */
75std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName);
76
77/*
78 * Internal implementation details for parse()
79 */
80namespace internal
81{
82
83/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060084 * Parses a JSON element containing a GPIO.
85 *
86 * Returns the corresponding C++ GPIO object.
87 *
88 * Throws an exception if parsing fails.
89 *
90 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -060091 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -060092 * @return GPIO object
93 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -060094GPIO parseGPIO(const nlohmann::json& element,
95 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -060096
97/**
Shawn McCarneye140ed92025-11-06 11:20:38 -060098 * Parses a JSON element containing an i2c_interface object.
99 *
100 * Returns the corresponding I2C bus and address.
101 *
102 * Throws an exception if parsing fails.
103 *
104 * @param element JSON element
105 * @param variables variables map used to expand variables in element value
106 * @return tuple containing bus and address
107 */
108std::tuple<uint8_t, uint16_t> parseI2CInterface(
109 const nlohmann::json& element,
110 const std::map<std::string, std::string>& variables);
111
112/**
Shawn McCarney4840b252025-11-06 16:06:40 -0600113 * Parses a JSON element containing a power_sequencer object.
114 *
115 * Returns the corresponding C++ PowerSequencerDevice object.
116 *
117 * Throws an exception if parsing fails.
118 *
119 * @param element JSON element
120 * @param variables variables map used to expand variables in element value
121 * @param services System services like hardware presence and the journal
122 * @return PowerSequencerDevice object
123 */
124std::unique_ptr<PowerSequencerDevice> parsePowerSequencer(
125 const nlohmann::json& element,
126 const std::map<std::string, std::string>& variables, Services& services);
127
128/**
129 * Parses a JSON element containing an array of power_sequencer objects.
130 *
131 * Returns the corresponding C++ PowerSequencerDevice objects.
132 *
133 * Throws an exception if parsing fails.
134 *
135 * @param element JSON element
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 vector of PowerSequencerDevice objects
139 */
140std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray(
141 const nlohmann::json& element,
142 const std::map<std::string, std::string>& variables, Services& services);
143
144/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600145 * Parses a JSON element containing a rail.
146 *
147 * Returns the corresponding C++ Rail object.
148 *
149 * Throws an exception if parsing fails.
150 *
151 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600152 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600153 * @return Rail object
154 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600155std::unique_ptr<Rail> parseRail(
156 const nlohmann::json& element,
157 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600158
159/**
160 * Parses a JSON element containing an array of rails.
161 *
162 * Returns the corresponding C++ Rail objects.
163 *
164 * Throws an exception if parsing fails.
165 *
166 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600167 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600168 * @return vector of Rail objects
169 */
Patrick Williams92261f82025-02-01 08:22:34 -0500170std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600171 const nlohmann::json& element,
172 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600173
174/**
175 * Parses the JSON root element of the entire configuration file.
176 *
177 * Returns the corresponding C++ Rail objects.
178 *
179 * Throws an exception if parsing fails.
180 *
181 * @param element JSON element
182 * @return vector of Rail objects
183 */
184std::vector<std::unique_ptr<Rail>> parseRoot(const nlohmann::json& element);
185
Shawn McCarney6a957f62024-01-10 16:15:19 -0600186} // namespace internal
187
188} // namespace phosphor::power::sequencer::config_file_parser