blob: 0f5fe3cb87dddb2deae0823d192d7f15c4190714 [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
18#include "rail.hpp"
19
20#include <nlohmann/json.hpp>
21
Shawn McCarneye140ed92025-11-06 11:20:38 -060022#include <cstdint>
Shawn McCarney6a957f62024-01-10 16:15:19 -060023#include <filesystem>
Shawn McCarneye140ed92025-11-06 11:20:38 -060024#include <map>
Shawn McCarney906cc3f2024-02-01 13:33:06 -060025#include <memory>
Shawn McCarney6a957f62024-01-10 16:15:19 -060026#include <string>
Shawn McCarneye140ed92025-11-06 11:20:38 -060027#include <tuple>
Shawn McCarney6a957f62024-01-10 16:15:19 -060028#include <vector>
29
30namespace phosphor::power::sequencer::config_file_parser
31{
32
33/**
Shawn McCarneye9144ab2024-05-22 12:34:18 -050034 * Standard JSON configuration file directory on the BMC.
35 */
36extern const std::filesystem::path standardConfigFileDirectory;
37
38/**
39 * Finds the JSON configuration file for the current system based on the
40 * specified compatible system types.
41 *
42 * This is required when a single BMC firmware image supports multiple system
43 * types and some system types require different configuration files.
44 *
45 * The compatible system types must be ordered from most to least specific.
46 * Example:
47 * - com.acme.Hardware.Chassis.Model.MegaServer4CPU
48 * - com.acme.Hardware.Chassis.Model.MegaServer
49 * - com.acme.Hardware.Chassis.Model.Server
50 *
51 * Throws an exception if an error occurs.
52 *
53 * @param compatibleSystemTypes compatible system types for the current system
54 * ordered from most to least specific
55 * @param configFileDir directory containing configuration files
56 * @return path to the JSON configuration file, or an empty path if none was
57 * found
58 */
59std::filesystem::path find(
60 const std::vector<std::string>& compatibleSystemTypes,
61 const std::filesystem::path& configFileDir = standardConfigFileDirectory);
62
63/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060064 * Parses the specified JSON configuration file.
65 *
66 * Returns the corresponding C++ Rail objects.
67 *
68 * Throws a ConfigFileParserError if an error occurs.
69 *
70 * @param pathName configuration file path name
71 * @return vector of Rail objects
72 */
73std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName);
74
75/*
76 * Internal implementation details for parse()
77 */
78namespace internal
79{
80
81/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060082 * Parses a JSON element containing a GPIO.
83 *
84 * Returns the corresponding C++ GPIO object.
85 *
86 * Throws an exception if parsing fails.
87 *
88 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -060089 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -060090 * @return GPIO object
91 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -060092GPIO parseGPIO(const nlohmann::json& element,
93 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -060094
95/**
Shawn McCarneye140ed92025-11-06 11:20:38 -060096 * Parses a JSON element containing an i2c_interface object.
97 *
98 * Returns the corresponding I2C bus and address.
99 *
100 * Throws an exception if parsing fails.
101 *
102 * @param element JSON element
103 * @param variables variables map used to expand variables in element value
104 * @return tuple containing bus and address
105 */
106std::tuple<uint8_t, uint16_t> parseI2CInterface(
107 const nlohmann::json& element,
108 const std::map<std::string, std::string>& variables);
109
110/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600111 * Parses a JSON element containing a rail.
112 *
113 * Returns the corresponding C++ Rail object.
114 *
115 * Throws an exception if parsing fails.
116 *
117 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600118 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600119 * @return Rail object
120 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600121std::unique_ptr<Rail> parseRail(
122 const nlohmann::json& element,
123 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600124
125/**
126 * Parses a JSON element containing an array of rails.
127 *
128 * Returns the corresponding C++ Rail objects.
129 *
130 * Throws an exception if parsing fails.
131 *
132 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600133 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600134 * @return vector of Rail objects
135 */
Patrick Williams92261f82025-02-01 08:22:34 -0500136std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600137 const nlohmann::json& element,
138 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600139
140/**
141 * Parses the JSON root element of the entire configuration file.
142 *
143 * Returns the corresponding C++ Rail objects.
144 *
145 * Throws an exception if parsing fails.
146 *
147 * @param element JSON element
148 * @return vector of Rail objects
149 */
150std::vector<std::unique_ptr<Rail>> parseRoot(const nlohmann::json& element);
151
Shawn McCarney6a957f62024-01-10 16:15:19 -0600152} // namespace internal
153
154} // namespace phosphor::power::sequencer::config_file_parser