blob: b2a18ca0cb784deb345d7bb1a847d81d101be00b [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 McCarneyfb7e0932025-11-10 10:23:05 -060026#include <functional> // for reference_wrapper
Shawn McCarneye140ed92025-11-06 11:20:38 -060027#include <map>
Shawn McCarney906cc3f2024-02-01 13:33:06 -060028#include <memory>
Shawn McCarney6a957f62024-01-10 16:15:19 -060029#include <string>
Shawn McCarneye140ed92025-11-06 11:20:38 -060030#include <tuple>
Shawn McCarney6a957f62024-01-10 16:15:19 -060031#include <vector>
32
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060033using json = nlohmann::json;
34
Shawn McCarney6a957f62024-01-10 16:15:19 -060035namespace phosphor::power::sequencer::config_file_parser
36{
37
38/**
Shawn McCarneye9144ab2024-05-22 12:34:18 -050039 * Standard JSON configuration file directory on the BMC.
40 */
41extern 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 */
64std::filesystem::path find(
65 const std::vector<std::string>& compatibleSystemTypes,
66 const std::filesystem::path& configFileDir = standardConfigFileDirectory);
67
68/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060069 * 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 */
78std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName);
79
80/*
81 * Internal implementation details for parse()
82 */
83namespace internal
84{
85
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060086using 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 */
98std::tuple<std::string, JSONRefWrapper> parseChassisTemplate(
99 const json& element);
100
Shawn McCarney6a957f62024-01-10 16:15:19 -0600101/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600102 * 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 McCarney038f2ba2025-11-06 13:32:16 -0600109 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600110 * @return GPIO object
111 */
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600112GPIO parseGPIO(const json& element,
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600113 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600114
115/**
Shawn McCarneye140ed92025-11-06 11:20:38 -0600116 * 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 */
126std::tuple<uint8_t, uint16_t> parseI2CInterface(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600127 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarneye140ed92025-11-06 11:20:38 -0600128
129/**
Shawn McCarney4840b252025-11-06 16:06:40 -0600130 * 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 McCarneyfb7e0932025-11-10 10:23:05 -0600138 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600139 * @return PowerSequencerDevice object
140 */
141std::unique_ptr<PowerSequencerDevice> parsePowerSequencer(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600142 const json& element, const std::map<std::string, std::string>& variables,
143 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600144
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 McCarneyfb7e0932025-11-10 10:23:05 -0600154 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600155 * @return vector of PowerSequencerDevice objects
156 */
157std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600158 const json& element, const std::map<std::string, std::string>& variables,
159 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600160
161/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600162 * 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 McCarney038f2ba2025-11-06 13:32:16 -0600169 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600170 * @return Rail object
171 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600172std::unique_ptr<Rail> parseRail(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600173 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600174
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 McCarney038f2ba2025-11-06 13:32:16 -0600183 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600184 * @return vector of Rail objects
185 */
Patrick Williams92261f82025-02-01 08:22:34 -0500186std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600187 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600188
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 McCarneyfb7e0932025-11-10 10:23:05 -0600199std::vector<std::unique_ptr<Rail>> parseRoot(const json& element);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600200
Shawn McCarney6a957f62024-01-10 16:15:19 -0600201} // namespace internal
202
203} // namespace phosphor::power::sequencer::config_file_parser