blob: a57083e0a16ac1af7d92fe0cb7e5bb2d0ec0db8c [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
89 * @return GPIO object
90 */
91GPIO parseGPIO(const nlohmann::json& element);
92
93/**
Shawn McCarneye140ed92025-11-06 11:20:38 -060094 * Parses a JSON element containing an i2c_interface object.
95 *
96 * Returns the corresponding I2C bus and address.
97 *
98 * Throws an exception if parsing fails.
99 *
100 * @param element JSON element
101 * @param variables variables map used to expand variables in element value
102 * @return tuple containing bus and address
103 */
104std::tuple<uint8_t, uint16_t> parseI2CInterface(
105 const nlohmann::json& element,
106 const std::map<std::string, std::string>& variables);
107
108/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600109 * Parses a JSON element containing a rail.
110 *
111 * Returns the corresponding C++ Rail object.
112 *
113 * Throws an exception if parsing fails.
114 *
115 * @param element JSON element
116 * @return Rail object
117 */
118std::unique_ptr<Rail> parseRail(const nlohmann::json& element);
119
120/**
121 * Parses a JSON element containing an array of rails.
122 *
123 * Returns the corresponding C++ Rail objects.
124 *
125 * Throws an exception if parsing fails.
126 *
127 * @param element JSON element
128 * @return vector of Rail objects
129 */
Patrick Williams92261f82025-02-01 08:22:34 -0500130std::vector<std::unique_ptr<Rail>> parseRailArray(
131 const nlohmann::json& element);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600132
133/**
134 * Parses the JSON root element of the entire configuration file.
135 *
136 * Returns the corresponding C++ Rail objects.
137 *
138 * Throws an exception if parsing fails.
139 *
140 * @param element JSON element
141 * @return vector of Rail objects
142 */
143std::vector<std::unique_ptr<Rail>> parseRoot(const nlohmann::json& element);
144
Shawn McCarney6a957f62024-01-10 16:15:19 -0600145} // namespace internal
146
147} // namespace phosphor::power::sequencer::config_file_parser