xref: /openbmc/phosphor-power/phosphor-regulators/src/config_file_parser.hpp (revision 92261f88729b618b1c31d20f28328a8aff73b83b)
10e8c68abSShawn McCarney /**
20e8c68abSShawn McCarney  * Copyright © 2020 IBM Corporation
30e8c68abSShawn McCarney  *
40e8c68abSShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
50e8c68abSShawn McCarney  * you may not use this file except in compliance with the License.
60e8c68abSShawn McCarney  * You may obtain a copy of the License at
70e8c68abSShawn McCarney  *
80e8c68abSShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
90e8c68abSShawn McCarney  *
100e8c68abSShawn McCarney  * Unless required by applicable law or agreed to in writing, software
110e8c68abSShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
120e8c68abSShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130e8c68abSShawn McCarney  * See the License for the specific language governing permissions and
140e8c68abSShawn McCarney  * limitations under the License.
150e8c68abSShawn McCarney  */
160e8c68abSShawn McCarney #pragma once
170e8c68abSShawn McCarney 
180e8c68abSShawn McCarney #include "action.hpp"
193a787540SBob King #include "and_action.hpp"
200e8c68abSShawn McCarney #include "chassis.hpp"
21b267b7ebSBob King #include "compare_presence_action.hpp"
22f2134320SBob King #include "compare_vpd_action.hpp"
239c36c5fbSBob King #include "configuration.hpp"
240e70113dSBob King #include "device.hpp"
2591f87a56SShawn McCarney #include "i2c_capture_bytes_action.hpp"
26f09bfe07SBob King #include "i2c_compare_bit_action.hpp"
27f09bfe07SBob King #include "i2c_compare_byte_action.hpp"
28f09bfe07SBob King #include "i2c_compare_bytes_action.hpp"
299c36c5fbSBob King #include "i2c_interface.hpp"
30f617f893SBob King #include "i2c_write_bit_action.hpp"
3187ff9d7dSBob King #include "i2c_write_byte_action.hpp"
32bafcb86cSBob King #include "i2c_write_bytes_action.hpp"
3393a89d72SBob King #include "if_action.hpp"
341115785fSShawn McCarney #include "log_phase_fault_action.hpp"
35f1b58dc4SBob King #include "not_action.hpp"
360b51a9b2SBob King #include "or_action.hpp"
37b70370b7SShawn McCarney #include "phase_fault.hpp"
3839eb08a9SShawn McCarney #include "phase_fault_detection.hpp"
3984614882SBob King #include "pmbus_read_sensor_action.hpp"
400e8c68abSShawn McCarney #include "pmbus_write_vout_command_action.hpp"
419c36c5fbSBob King #include "presence_detection.hpp"
429c36c5fbSBob King #include "rail.hpp"
430e8c68abSShawn McCarney #include "rule.hpp"
44315b0b62SBob King #include "run_rule_action.hpp"
45a2f2a0dcSBob King #include "sensor_monitoring.hpp"
462f9e14f6SShawn McCarney #include "sensors.hpp"
4718a68505SBob King #include "set_device_action.hpp"
480e8c68abSShawn McCarney 
490e8c68abSShawn McCarney #include <nlohmann/json.hpp>
500e8c68abSShawn McCarney 
510e8c68abSShawn McCarney #include <cstdint>
520e8c68abSShawn McCarney #include <filesystem>
530e8c68abSShawn McCarney #include <memory>
540e8c68abSShawn McCarney #include <stdexcept>
550e8c68abSShawn McCarney #include <string>
560e8c68abSShawn McCarney #include <tuple>
570e8c68abSShawn McCarney #include <vector>
580e8c68abSShawn McCarney 
590e8c68abSShawn McCarney namespace phosphor::power::regulators::config_file_parser
600e8c68abSShawn McCarney {
610e8c68abSShawn McCarney 
620e8c68abSShawn McCarney /**
630e8c68abSShawn McCarney  * Parses the specified JSON configuration file.
640e8c68abSShawn McCarney  *
650e8c68abSShawn McCarney  * Returns the corresponding C++ Rule and Chassis objects.
660e8c68abSShawn McCarney  *
670e8c68abSShawn McCarney  * Throws a ConfigFileParserError if an error occurs.
680e8c68abSShawn McCarney  *
690e8c68abSShawn McCarney  * @param pathName configuration file path name
700e8c68abSShawn McCarney  * @return tuple containing vectors of Rule and Chassis objects
710e8c68abSShawn McCarney  */
720e8c68abSShawn McCarney std::tuple<std::vector<std::unique_ptr<Rule>>,
730e8c68abSShawn McCarney            std::vector<std::unique_ptr<Chassis>>>
740e8c68abSShawn McCarney     parse(const std::filesystem::path& pathName);
750e8c68abSShawn McCarney 
760e8c68abSShawn McCarney /*
770e8c68abSShawn McCarney  * Internal implementation details for parse()
780e8c68abSShawn McCarney  */
790e8c68abSShawn McCarney namespace internal
800e8c68abSShawn McCarney {
810e8c68abSShawn McCarney 
820e8c68abSShawn McCarney /**
830e8c68abSShawn McCarney  * Returns the specified property of the specified JSON element.
840e8c68abSShawn McCarney  *
850e8c68abSShawn McCarney  * Throws an invalid_argument exception if the property does not exist.
860e8c68abSShawn McCarney  *
870e8c68abSShawn McCarney  * @param element JSON element
880e8c68abSShawn McCarney  * @param property property name
890e8c68abSShawn McCarney  */
90974a7940SPatrick Williams #pragma GCC diagnostic push
91b9d4fb71SAndrew Geissler #if __GNUC__ >= 13
92974a7940SPatrick Williams #pragma GCC diagnostic ignored "-Wdangling-reference"
935a355ba3SPatrick Williams #endif
getRequiredProperty(const nlohmann::json & element,const std::string & property)940e8c68abSShawn McCarney inline const nlohmann::json& getRequiredProperty(const nlohmann::json& element,
950e8c68abSShawn McCarney                                                  const std::string& property)
960e8c68abSShawn McCarney {
970e8c68abSShawn McCarney     auto it = element.find(property);
980e8c68abSShawn McCarney     if (it == element.end())
990e8c68abSShawn McCarney     {
1000e8c68abSShawn McCarney         throw std::invalid_argument{"Required property missing: " + property};
1010e8c68abSShawn McCarney     }
1020e8c68abSShawn McCarney     return *it;
1030e8c68abSShawn McCarney }
104974a7940SPatrick Williams #pragma GCC diagnostic pop
1050e8c68abSShawn McCarney 
1060e8c68abSShawn McCarney /**
1070e8c68abSShawn McCarney  * Parses a JSON element containing an action.
1080e8c68abSShawn McCarney  *
1090e8c68abSShawn McCarney  * Returns the corresponding C++ Action object.
1100e8c68abSShawn McCarney  *
1110e8c68abSShawn McCarney  * Throws an exception if parsing fails.
1120e8c68abSShawn McCarney  *
1130e8c68abSShawn McCarney  * @param element JSON element
1140e8c68abSShawn McCarney  * @return Action object
1150e8c68abSShawn McCarney  */
1160e8c68abSShawn McCarney std::unique_ptr<Action> parseAction(const nlohmann::json& element);
1170e8c68abSShawn McCarney 
1180e8c68abSShawn McCarney /**
1190e8c68abSShawn McCarney  * Parses a JSON element containing an array of actions.
1200e8c68abSShawn McCarney  *
1210e8c68abSShawn McCarney  * Returns the corresponding C++ Action objects.
1220e8c68abSShawn McCarney  *
1230e8c68abSShawn McCarney  * Throws an exception if parsing fails.
1240e8c68abSShawn McCarney  *
1250e8c68abSShawn McCarney  * @param element JSON element
1260e8c68abSShawn McCarney  * @return vector of Action objects
1270e8c68abSShawn McCarney  */
128*92261f88SPatrick Williams std::vector<std::unique_ptr<Action>> parseActionArray(
129*92261f88SPatrick Williams     const nlohmann::json& element);
1300e8c68abSShawn McCarney 
1310e8c68abSShawn McCarney /**
1323a787540SBob King  * Parses a JSON element containing an and action.
1333a787540SBob King  *
1343a787540SBob King  * Returns the corresponding C++ AndAction object.
1353a787540SBob King  *
1363a787540SBob King  * Throws an exception if parsing fails.
1373a787540SBob King  *
1383a787540SBob King  * @param element JSON element
1393a787540SBob King  * @return AndAction object
1403a787540SBob King  */
1413a787540SBob King std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element);
1423a787540SBob King 
1433a787540SBob King /**
144f617f893SBob King  * Parses a JSON element containing a bit position (from 0-7).
145f617f893SBob King  *
146f617f893SBob King  * Returns the corresponding C++ uint8_t value.
147f617f893SBob King  *
148f617f893SBob King  * Throws an exception if parsing fails.
149f617f893SBob King  *
150f617f893SBob King  * @param element JSON element
151f617f893SBob King  * @return uint8_t value
152f617f893SBob King  */
parseBitPosition(const nlohmann::json & element)153f617f893SBob King inline uint8_t parseBitPosition(const nlohmann::json& element)
154f617f893SBob King {
155f617f893SBob King     // Verify element contains an integer
156f617f893SBob King     if (!element.is_number_integer())
157f617f893SBob King     {
158f617f893SBob King         throw std::invalid_argument{"Element is not an integer"};
159f617f893SBob King     }
1606afbf1a5SBob King     int value = element.get<int>();
161f617f893SBob King     if ((value < 0) || (value > 7))
162f617f893SBob King     {
163f617f893SBob King         throw std::invalid_argument{"Element is not a bit position"};
164f617f893SBob King     }
165f617f893SBob King     return static_cast<uint8_t>(value);
166f617f893SBob King }
167f617f893SBob King 
168f617f893SBob King /**
169f617f893SBob King  * Parses a JSON element containing a bit value (0 or 1).
170f617f893SBob King  *
171f617f893SBob King  * Returns the corresponding C++ uint8_t value.
172f617f893SBob King  *
173f617f893SBob King  * Throws an exception if parsing fails.
174f617f893SBob King  *
175f617f893SBob King  * @param element JSON element
176f617f893SBob King  * @return uint8_t value
177f617f893SBob King  */
parseBitValue(const nlohmann::json & element)178f617f893SBob King inline uint8_t parseBitValue(const nlohmann::json& element)
179f617f893SBob King {
180f617f893SBob King     // Verify element contains an integer
181f617f893SBob King     if (!element.is_number_integer())
182f617f893SBob King     {
183f617f893SBob King         throw std::invalid_argument{"Element is not an integer"};
184f617f893SBob King     }
1856afbf1a5SBob King     int value = element.get<int>();
186f617f893SBob King     if ((value < 0) || (value > 1))
187f617f893SBob King     {
188f617f893SBob King         throw std::invalid_argument{"Element is not a bit value"};
189f617f893SBob King     }
190f617f893SBob King     return static_cast<uint8_t>(value);
191f617f893SBob King }
192f617f893SBob King 
193f617f893SBob King /**
1940e8c68abSShawn McCarney  * Parses a JSON element containing a boolean.
1950e8c68abSShawn McCarney  *
1960e8c68abSShawn McCarney  * Returns the corresponding C++ boolean value.
1970e8c68abSShawn McCarney  *
1980e8c68abSShawn McCarney  * Throws an exception if parsing fails.
1990e8c68abSShawn McCarney  *
2000e8c68abSShawn McCarney  * @param element JSON element
2010e8c68abSShawn McCarney  * @return boolean value
2020e8c68abSShawn McCarney  */
parseBoolean(const nlohmann::json & element)2030e8c68abSShawn McCarney inline bool parseBoolean(const nlohmann::json& element)
2040e8c68abSShawn McCarney {
2050e8c68abSShawn McCarney     // Verify element contains a boolean
2060e8c68abSShawn McCarney     if (!element.is_boolean())
2070e8c68abSShawn McCarney     {
2080e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not a boolean"};
2090e8c68abSShawn McCarney     }
2100e8c68abSShawn McCarney     return element.get<bool>();
2110e8c68abSShawn McCarney }
2120e8c68abSShawn McCarney 
2130e8c68abSShawn McCarney /**
2140e70113dSBob King  * Parses a JSON element containing a chassis.
2150e70113dSBob King  *
2160e70113dSBob King  * Returns the corresponding C++ Chassis object.
2170e70113dSBob King  *
2180e70113dSBob King  * Throws an exception if parsing fails.
2190e70113dSBob King  *
2200e70113dSBob King  * @param element JSON element
2210e70113dSBob King  * @return Chassis object
2220e70113dSBob King  */
2230e70113dSBob King std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element);
2240e70113dSBob King 
2250e70113dSBob King /**
2260e8c68abSShawn McCarney  * Parses a JSON element containing an array of chassis.
2270e8c68abSShawn McCarney  *
2280e8c68abSShawn McCarney  * Returns the corresponding C++ Chassis objects.
2290e8c68abSShawn McCarney  *
2300e8c68abSShawn McCarney  * Throws an exception if parsing fails.
2310e8c68abSShawn McCarney  *
2320e8c68abSShawn McCarney  * @param element JSON element
2330e8c68abSShawn McCarney  * @return vector of Chassis objects
2340e8c68abSShawn McCarney  */
235*92261f88SPatrick Williams std::vector<std::unique_ptr<Chassis>> parseChassisArray(
236*92261f88SPatrick Williams     const nlohmann::json& element);
2370e8c68abSShawn McCarney 
2380e8c68abSShawn McCarney /**
239b267b7ebSBob King  * Parses a JSON element containing a compare_presence action.
240b267b7ebSBob King  *
241b267b7ebSBob King  * Returns the corresponding C++ ComparePresenceAction object.
242b267b7ebSBob King  *
243b267b7ebSBob King  * Throws an exception if parsing fails.
244b267b7ebSBob King  *
245b267b7ebSBob King  * @param element JSON element
246b267b7ebSBob King  * @return ComparePresenceAction object
247b267b7ebSBob King  */
248*92261f88SPatrick Williams std::unique_ptr<ComparePresenceAction> parseComparePresence(
249*92261f88SPatrick Williams     const nlohmann::json& element);
250b267b7ebSBob King 
251b267b7ebSBob King /**
252f2134320SBob King  * Parses a JSON element containing a compare_vpd action.
253f2134320SBob King  *
254f2134320SBob King  * Returns the corresponding C++ CompareVPDAction object.
255f2134320SBob King  *
256f2134320SBob King  * Throws an exception if parsing fails.
257f2134320SBob King  *
258f2134320SBob King  * @param element JSON element
259f2134320SBob King  * @return CompareVPDAction object
260f2134320SBob King  */
261*92261f88SPatrick Williams std::unique_ptr<CompareVPDAction> parseCompareVPD(
262*92261f88SPatrick Williams     const nlohmann::json& element);
263f2134320SBob King 
264f2134320SBob King /**
26539eb08a9SShawn McCarney  * Parses a JSON element containing a configuration object.
26633e7eaa5SBob King  *
26733e7eaa5SBob King  * Returns the corresponding C++ Configuration object.
26833e7eaa5SBob King  *
26933e7eaa5SBob King  * Throws an exception if parsing fails.
27033e7eaa5SBob King  *
27133e7eaa5SBob King  * @param element JSON element
27233e7eaa5SBob King  * @return Configuration object
27333e7eaa5SBob King  */
274*92261f88SPatrick Williams std::unique_ptr<Configuration> parseConfiguration(
275*92261f88SPatrick Williams     const nlohmann::json& element);
27633e7eaa5SBob King 
27733e7eaa5SBob King /**
2789c36c5fbSBob King  * Parses a JSON element containing a device.
2799c36c5fbSBob King  *
2809c36c5fbSBob King  * Returns the corresponding C++ Device object.
2819c36c5fbSBob King  *
2829c36c5fbSBob King  * Throws an exception if parsing fails.
2839c36c5fbSBob King  *
2849c36c5fbSBob King  * @param element JSON element
2859c36c5fbSBob King  * @return Device object
2869c36c5fbSBob King  */
2879c36c5fbSBob King std::unique_ptr<Device> parseDevice(const nlohmann::json& element);
2889c36c5fbSBob King 
2899c36c5fbSBob King /**
2900e70113dSBob King  * Parses a JSON element containing an array of devices.
2910e70113dSBob King  *
2920e70113dSBob King  * Returns the corresponding C++ Device objects.
2930e70113dSBob King  *
2940e70113dSBob King  * Throws an exception if parsing fails.
2950e70113dSBob King  *
2960e70113dSBob King  * @param element JSON element
2970e70113dSBob King  * @return vector of Device objects
2980e70113dSBob King  */
299*92261f88SPatrick Williams std::vector<std::unique_ptr<Device>> parseDeviceArray(
300*92261f88SPatrick Williams     const nlohmann::json& element);
3010e70113dSBob King 
3020e70113dSBob King /**
3030e8c68abSShawn McCarney  * Parses a JSON element containing a double (floating point number).
3040e8c68abSShawn McCarney  *
3050e8c68abSShawn McCarney  * Returns the corresponding C++ double value.
3060e8c68abSShawn McCarney  *
3070e8c68abSShawn McCarney  * Throws an exception if parsing fails.
3080e8c68abSShawn McCarney  *
3090e8c68abSShawn McCarney  * @param element JSON element
3100e8c68abSShawn McCarney  * @return double value
3110e8c68abSShawn McCarney  */
parseDouble(const nlohmann::json & element)3120e8c68abSShawn McCarney inline double parseDouble(const nlohmann::json& element)
3130e8c68abSShawn McCarney {
3140e8c68abSShawn McCarney     // Verify element contains a number (integer or floating point)
3150e8c68abSShawn McCarney     if (!element.is_number())
3160e8c68abSShawn McCarney     {
3170e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not a number"};
3180e8c68abSShawn McCarney     }
3190e8c68abSShawn McCarney     return element.get<double>();
3200e8c68abSShawn McCarney }
3210e8c68abSShawn McCarney 
3220e8c68abSShawn McCarney /**
323bafcb86cSBob King  * Parses a JSON element containing a byte value expressed as a hexadecimal
324bafcb86cSBob King  * string.
325bafcb86cSBob King  *
326bafcb86cSBob King  * The JSON number data type does not support the hexadecimal format.  For this
327bafcb86cSBob King  * reason, hexadecimal byte values are stored as strings in the configuration
328bafcb86cSBob King  * file.
329bafcb86cSBob King  *
330bafcb86cSBob King  * Returns the corresponding C++ uint8_t value.
331bafcb86cSBob King  *
332bafcb86cSBob King  * Throws an exception if parsing fails.
333bafcb86cSBob King  *
334bafcb86cSBob King  * @param element JSON element
335bafcb86cSBob King  * @return uint8_t value
336bafcb86cSBob King  */
parseHexByte(const nlohmann::json & element)337bafcb86cSBob King inline uint8_t parseHexByte(const nlohmann::json& element)
338bafcb86cSBob King {
339bafcb86cSBob King     if (!element.is_string())
340bafcb86cSBob King     {
341bafcb86cSBob King         throw std::invalid_argument{"Element is not a string"};
342bafcb86cSBob King     }
3436afbf1a5SBob King     std::string value = element.get<std::string>();
344bafcb86cSBob King 
345bafcb86cSBob King     bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) &&
346bafcb86cSBob King                  (value.size() < 5) &&
347bafcb86cSBob King                  (value.find_first_not_of("0123456789abcdefABCDEF", 2) ==
348bafcb86cSBob King                   std::string::npos);
349bafcb86cSBob King     if (!isHex)
350bafcb86cSBob King     {
351bafcb86cSBob King         throw std::invalid_argument{"Element is not hexadecimal string"};
352bafcb86cSBob King     }
353757ad6a9SJayanth Othayoth     return static_cast<uint8_t>(std::stoul(value, nullptr, 0));
354bafcb86cSBob King }
355bafcb86cSBob King 
356bafcb86cSBob King /**
357bafcb86cSBob King  * Parses a JSON element containing an array of byte values expressed as a
358bafcb86cSBob King  * hexadecimal strings.
359bafcb86cSBob King  *
360bafcb86cSBob King  * Returns the corresponding C++ uint8_t values.
361bafcb86cSBob King  *
362bafcb86cSBob King  * Throws an exception if parsing fails.
363bafcb86cSBob King  *
364bafcb86cSBob King  * @param element JSON element
365bafcb86cSBob King  * @return vector of uint8_t
366bafcb86cSBob King  */
367bafcb86cSBob King std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
368bafcb86cSBob King 
369bafcb86cSBob King /**
37091f87a56SShawn McCarney  * Parses a JSON element containing an i2c_capture_bytes action.
37191f87a56SShawn McCarney  *
37291f87a56SShawn McCarney  * Returns the corresponding C++ I2CCaptureBytesAction object.
37391f87a56SShawn McCarney  *
37491f87a56SShawn McCarney  * Throws an exception if parsing fails.
37591f87a56SShawn McCarney  *
37691f87a56SShawn McCarney  * @param element JSON element
37791f87a56SShawn McCarney  * @return I2CCaptureBytesAction object
37891f87a56SShawn McCarney  */
379*92261f88SPatrick Williams std::unique_ptr<I2CCaptureBytesAction> parseI2CCaptureBytes(
380*92261f88SPatrick Williams     const nlohmann::json& element);
38191f87a56SShawn McCarney 
38291f87a56SShawn McCarney /**
383f09bfe07SBob King  * Parses a JSON element containing an i2c_compare_bit action.
384f09bfe07SBob King  *
385f09bfe07SBob King  * Returns the corresponding C++ I2CCompareBitAction object.
386f09bfe07SBob King  *
387f09bfe07SBob King  * Throws an exception if parsing fails.
388f09bfe07SBob King  *
389f09bfe07SBob King  * @param element JSON element
390f09bfe07SBob King  * @return I2CCompareBitAction object
391f09bfe07SBob King  */
392*92261f88SPatrick Williams std::unique_ptr<I2CCompareBitAction> parseI2CCompareBit(
393*92261f88SPatrick Williams     const nlohmann::json& element);
394f09bfe07SBob King 
395f09bfe07SBob King /**
396f09bfe07SBob King  * Parses a JSON element containing an i2c_compare_byte action.
397f09bfe07SBob King  *
398f09bfe07SBob King  * Returns the corresponding C++ I2CCompareByteAction object.
399f09bfe07SBob King  *
400f09bfe07SBob King  * Throws an exception if parsing fails.
401f09bfe07SBob King  *
402f09bfe07SBob King  * @param element JSON element
403f09bfe07SBob King  * @return I2CCompareByteAction object
404f09bfe07SBob King  */
405*92261f88SPatrick Williams std::unique_ptr<I2CCompareByteAction> parseI2CCompareByte(
406*92261f88SPatrick Williams     const nlohmann::json& element);
407f09bfe07SBob King 
408f09bfe07SBob King /**
409f09bfe07SBob King  * Parses a JSON element containing an i2c_compare_bytes action.
410f09bfe07SBob King  *
411f09bfe07SBob King  * Returns the corresponding C++ I2CCompareBytesAction object.
412f09bfe07SBob King  *
413f09bfe07SBob King  * Throws an exception if parsing fails.
414f09bfe07SBob King  *
415f09bfe07SBob King  * @param element JSON element
416f09bfe07SBob King  * @return I2CCompareBytesAction object
417f09bfe07SBob King  */
418*92261f88SPatrick Williams std::unique_ptr<I2CCompareBytesAction> parseI2CCompareBytes(
419*92261f88SPatrick Williams     const nlohmann::json& element);
420f09bfe07SBob King 
421f09bfe07SBob King /**
4229c36c5fbSBob King  * Parses a JSON element containing an i2c_interface.
4239c36c5fbSBob King  *
4249c36c5fbSBob King  * Returns the corresponding C++ i2c::I2CInterface object.
4259c36c5fbSBob King  *
4269c36c5fbSBob King  * Throws an exception if parsing fails.
4279c36c5fbSBob King  *
4289c36c5fbSBob King  * @param element JSON element
4299c36c5fbSBob King  * @return i2c::I2CInterface object
4309c36c5fbSBob King  */
431*92261f88SPatrick Williams std::unique_ptr<i2c::I2CInterface> parseI2CInterface(
432*92261f88SPatrick Williams     const nlohmann::json& element);
4339c36c5fbSBob King 
4349c36c5fbSBob King /**
435f617f893SBob King  * Parses a JSON element containing an i2c_write_bit action.
436f617f893SBob King  *
437f617f893SBob King  * Returns the corresponding C++ I2CWriteBitAction object.
438f617f893SBob King  *
439f617f893SBob King  * Throws an exception if parsing fails.
440f617f893SBob King  *
441f617f893SBob King  * @param element JSON element
442f617f893SBob King  * @return I2CWriteBitAction object
443f617f893SBob King  */
444*92261f88SPatrick Williams std::unique_ptr<I2CWriteBitAction> parseI2CWriteBit(
445*92261f88SPatrick Williams     const nlohmann::json& element);
446f617f893SBob King 
447f617f893SBob King /**
44887ff9d7dSBob King  * Parses a JSON element containing an i2c_write_byte action.
44987ff9d7dSBob King  *
45087ff9d7dSBob King  * Returns the corresponding C++ I2CWriteByteAction object.
45187ff9d7dSBob King  *
45287ff9d7dSBob King  * Throws an exception if parsing fails.
45387ff9d7dSBob King  *
45487ff9d7dSBob King  * @param element JSON element
45587ff9d7dSBob King  * @return I2CWriteByteAction object
45687ff9d7dSBob King  */
457*92261f88SPatrick Williams std::unique_ptr<I2CWriteByteAction> parseI2CWriteByte(
458*92261f88SPatrick Williams     const nlohmann::json& element);
45987ff9d7dSBob King 
46087ff9d7dSBob King /**
461bafcb86cSBob King  * Parses a JSON element containing an i2c_write_bytes action.
462bafcb86cSBob King  *
463bafcb86cSBob King  * Returns the corresponding C++ I2CWriteBytesAction object.
464bafcb86cSBob King  *
465bafcb86cSBob King  * Throws an exception if parsing fails.
466bafcb86cSBob King  *
467bafcb86cSBob King  * @param element JSON element
468bafcb86cSBob King  * @return I2CWriteBytesAction object
469bafcb86cSBob King  */
470*92261f88SPatrick Williams std::unique_ptr<I2CWriteBytesAction> parseI2CWriteBytes(
471*92261f88SPatrick Williams     const nlohmann::json& element);
472bafcb86cSBob King 
473bafcb86cSBob King /**
47493a89d72SBob King  * Parses a JSON element containing an if action.
47593a89d72SBob King  *
47693a89d72SBob King  * Returns the corresponding C++ IfAction object.
47793a89d72SBob King  *
47893a89d72SBob King  * Throws an exception if parsing fails.
47993a89d72SBob King  *
48093a89d72SBob King  * @param element JSON element
48193a89d72SBob King  * @return IfAction object
48293a89d72SBob King  */
48393a89d72SBob King std::unique_ptr<IfAction> parseIf(const nlohmann::json& element);
48493a89d72SBob King 
48593a89d72SBob King /**
4860e8c68abSShawn McCarney  * Parses a JSON element containing an 8-bit signed integer.
4870e8c68abSShawn McCarney  *
4880e8c68abSShawn McCarney  * Returns the corresponding C++ int8_t value.
4890e8c68abSShawn McCarney  *
4900e8c68abSShawn McCarney  * Throws an exception if parsing fails.
4910e8c68abSShawn McCarney  *
4920e8c68abSShawn McCarney  * @param element JSON element
4930e8c68abSShawn McCarney  * @return int8_t value
4940e8c68abSShawn McCarney  */
parseInt8(const nlohmann::json & element)4950e8c68abSShawn McCarney inline int8_t parseInt8(const nlohmann::json& element)
4960e8c68abSShawn McCarney {
4970e8c68abSShawn McCarney     // Verify element contains an integer
4980e8c68abSShawn McCarney     if (!element.is_number_integer())
4990e8c68abSShawn McCarney     {
5000e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not an integer"};
5010e8c68abSShawn McCarney     }
5026afbf1a5SBob King     int value = element.get<int>();
5030e8c68abSShawn McCarney     if ((value < INT8_MIN) || (value > INT8_MAX))
5040e8c68abSShawn McCarney     {
5050e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not an 8-bit signed integer"};
5060e8c68abSShawn McCarney     }
5070e8c68abSShawn McCarney     return static_cast<int8_t>(value);
5080e8c68abSShawn McCarney }
5090e8c68abSShawn McCarney 
5100e8c68abSShawn McCarney /**
511a76898f1SBob King  * Parses a JSON element containing a relative inventory path.
512a76898f1SBob King  *
513a76898f1SBob King  * Returns the corresponding C++ string containing the absolute inventory path.
514a76898f1SBob King  *
515a76898f1SBob King  * Inventory paths in the JSON configuration file are relative.  Adds the
516a76898f1SBob King  * necessary prefix to make the path absolute.
517a76898f1SBob King  *
518a76898f1SBob King  * Throws an exception if parsing fails.
519a76898f1SBob King  *
520a76898f1SBob King  * @param element JSON element
521a76898f1SBob King  * @return absolute D-Bus inventory path
522a76898f1SBob King  */
523a76898f1SBob King std::string parseInventoryPath(const nlohmann::json& element);
524a76898f1SBob King 
525a76898f1SBob King /**
5261115785fSShawn McCarney  * Parses a JSON element containing a log_phase_fault action.
5271115785fSShawn McCarney  *
5281115785fSShawn McCarney  * Returns the corresponding C++ LogPhaseFaultAction object.
5291115785fSShawn McCarney  *
5301115785fSShawn McCarney  * Throws an exception if parsing fails.
5311115785fSShawn McCarney  *
5321115785fSShawn McCarney  * @param element JSON element
5331115785fSShawn McCarney  * @return LogPhaseFaultAction object
5341115785fSShawn McCarney  */
535*92261f88SPatrick Williams std::unique_ptr<LogPhaseFaultAction> parseLogPhaseFault(
536*92261f88SPatrick Williams     const nlohmann::json& element);
5371115785fSShawn McCarney 
5381115785fSShawn McCarney /**
539f1b58dc4SBob King  * Parses a JSON element containing a not action.
540f1b58dc4SBob King  *
541f1b58dc4SBob King  * Returns the corresponding C++ NotAction object.
542f1b58dc4SBob King  *
543f1b58dc4SBob King  * Throws an exception if parsing fails.
544f1b58dc4SBob King  *
545f1b58dc4SBob King  * @param element JSON element
546f1b58dc4SBob King  * @return NotAction object
547f1b58dc4SBob King  */
548f1b58dc4SBob King std::unique_ptr<NotAction> parseNot(const nlohmann::json& element);
549f1b58dc4SBob King 
550f1b58dc4SBob King /**
5510b51a9b2SBob King  * Parses a JSON element containing an or action.
5520b51a9b2SBob King  *
5530b51a9b2SBob King  * Returns the corresponding C++ OrAction object.
5540b51a9b2SBob King  *
5550b51a9b2SBob King  * Throws an exception if parsing fails.
5560b51a9b2SBob King  *
5570b51a9b2SBob King  * @param element JSON element
5580b51a9b2SBob King  * @return OrAction object
5590b51a9b2SBob King  */
5600b51a9b2SBob King std::unique_ptr<OrAction> parseOr(const nlohmann::json& element);
5610b51a9b2SBob King 
5620b51a9b2SBob King /**
56339eb08a9SShawn McCarney  * Parses a JSON element containing a phase_fault_detection object.
56439eb08a9SShawn McCarney  *
56539eb08a9SShawn McCarney  * Returns the corresponding C++ PhaseFaultDetection object.
56639eb08a9SShawn McCarney  *
56739eb08a9SShawn McCarney  * Throws an exception if parsing fails.
56839eb08a9SShawn McCarney  *
56939eb08a9SShawn McCarney  * @param element JSON element
57039eb08a9SShawn McCarney  * @return PhaseFaultDetection object
57139eb08a9SShawn McCarney  */
572*92261f88SPatrick Williams std::unique_ptr<PhaseFaultDetection> parsePhaseFaultDetection(
573*92261f88SPatrick Williams     const nlohmann::json& element);
57439eb08a9SShawn McCarney 
57539eb08a9SShawn McCarney /**
576b70370b7SShawn McCarney  * Parses a JSON element containing a PhaseFaultType expressed as a string.
577b70370b7SShawn McCarney  *
578b70370b7SShawn McCarney  * Returns the corresponding PhaseFaultType enum value.
579b70370b7SShawn McCarney  *
580b70370b7SShawn McCarney  * Throws an exception if parsing fails.
581b70370b7SShawn McCarney  *
582b70370b7SShawn McCarney  * @param element JSON element
583b70370b7SShawn McCarney  * @return PhaseFaultType enum value
584b70370b7SShawn McCarney  */
585b70370b7SShawn McCarney PhaseFaultType parsePhaseFaultType(const nlohmann::json& element);
586b70370b7SShawn McCarney 
587b70370b7SShawn McCarney /**
58884614882SBob King  * Parses a JSON element containing a pmbus_read_sensor action.
58984614882SBob King  *
59084614882SBob King  * Returns the corresponding C++ PMBusReadSensorAction object.
59184614882SBob King  *
59284614882SBob King  * Throws an exception if parsing fails.
59384614882SBob King  *
59484614882SBob King  * @param element JSON element
59584614882SBob King  * @return PMBusReadSensorAction object
59684614882SBob King  */
597*92261f88SPatrick Williams std::unique_ptr<PMBusReadSensorAction> parsePMBusReadSensor(
598*92261f88SPatrick Williams     const nlohmann::json& element);
59984614882SBob King 
60084614882SBob King /**
6010e8c68abSShawn McCarney  * Parses a JSON element containing a pmbus_write_vout_command action.
6020e8c68abSShawn McCarney  *
6030e8c68abSShawn McCarney  * Returns the corresponding C++ PMBusWriteVoutCommandAction object.
6040e8c68abSShawn McCarney  *
6050e8c68abSShawn McCarney  * Throws an exception if parsing fails.
6060e8c68abSShawn McCarney  *
6070e8c68abSShawn McCarney  * @param element JSON element
6080e8c68abSShawn McCarney  * @return PMBusWriteVoutCommandAction object
6090e8c68abSShawn McCarney  */
610*92261f88SPatrick Williams std::unique_ptr<PMBusWriteVoutCommandAction> parsePMBusWriteVoutCommand(
611*92261f88SPatrick Williams     const nlohmann::json& element);
6120e8c68abSShawn McCarney 
6130e8c68abSShawn McCarney /**
61439eb08a9SShawn McCarney  * Parses a JSON element containing a presence_detection object.
6152aafb1c7SBob King  *
6162aafb1c7SBob King  * Returns the corresponding C++ PresenceDetection object.
6172aafb1c7SBob King  *
6182aafb1c7SBob King  * Throws an exception if parsing fails.
6192aafb1c7SBob King  *
6202aafb1c7SBob King  * @param element JSON element
6212aafb1c7SBob King  * @return PresenceDetection object
6222aafb1c7SBob King  */
623*92261f88SPatrick Williams std::unique_ptr<PresenceDetection> parsePresenceDetection(
624*92261f88SPatrick Williams     const nlohmann::json& element);
6252aafb1c7SBob King 
6262aafb1c7SBob King /**
627a2f2a0dcSBob King  * Parses a JSON element containing a rail.
628a2f2a0dcSBob King  *
629a2f2a0dcSBob King  * Returns the corresponding C++ Rail object.
630a2f2a0dcSBob King  *
631a2f2a0dcSBob King  * Throws an exception if parsing fails.
632a2f2a0dcSBob King  *
633a2f2a0dcSBob King  * @param element JSON element
634a2f2a0dcSBob King  * @return Rail object
635a2f2a0dcSBob King  */
636a2f2a0dcSBob King std::unique_ptr<Rail> parseRail(const nlohmann::json& element);
637a2f2a0dcSBob King 
638a2f2a0dcSBob King /**
6399c36c5fbSBob King  * Parses a JSON element containing an array of rails.
6409c36c5fbSBob King  *
6419c36c5fbSBob King  * Returns the corresponding C++ Rail objects.
6429c36c5fbSBob King  *
6439c36c5fbSBob King  * Throws an exception if parsing fails.
6449c36c5fbSBob King  *
6459c36c5fbSBob King  * @param element JSON element
6469c36c5fbSBob King  * @return vector of Rail objects
6479c36c5fbSBob King  */
648*92261f88SPatrick Williams std::vector<std::unique_ptr<Rail>> parseRailArray(
649*92261f88SPatrick Williams     const nlohmann::json& element);
6509c36c5fbSBob King 
6519c36c5fbSBob King /**
6520e8c68abSShawn McCarney  * Parses the JSON root element of the entire configuration file.
6530e8c68abSShawn McCarney  *
6540e8c68abSShawn McCarney  * Returns the corresponding C++ Rule and Chassis objects.
6550e8c68abSShawn McCarney  *
6560e8c68abSShawn McCarney  * Throws an exception if parsing fails.
6570e8c68abSShawn McCarney  *
6580e8c68abSShawn McCarney  * @param element JSON element
6590e8c68abSShawn McCarney  * @return tuple containing vectors of Rule and Chassis objects
6600e8c68abSShawn McCarney  */
6610e8c68abSShawn McCarney std::tuple<std::vector<std::unique_ptr<Rule>>,
6620e8c68abSShawn McCarney            std::vector<std::unique_ptr<Chassis>>>
6630e8c68abSShawn McCarney     parseRoot(const nlohmann::json& element);
6640e8c68abSShawn McCarney 
6650e8c68abSShawn McCarney /**
6660e8c68abSShawn McCarney  * Parses a JSON element containing a rule.
6670e8c68abSShawn McCarney  *
6680e8c68abSShawn McCarney  * Returns the corresponding C++ Rule object.
6690e8c68abSShawn McCarney  *
6700e8c68abSShawn McCarney  * Throws an exception if parsing fails.
6710e8c68abSShawn McCarney  *
6720e8c68abSShawn McCarney  * @param element JSON element
6730e8c68abSShawn McCarney  * @return Rule object
6740e8c68abSShawn McCarney  */
6750e8c68abSShawn McCarney std::unique_ptr<Rule> parseRule(const nlohmann::json& element);
6760e8c68abSShawn McCarney 
6770e8c68abSShawn McCarney /**
6780e8c68abSShawn McCarney  * Parses a JSON element containing an array of rules.
6790e8c68abSShawn McCarney  *
6800e8c68abSShawn McCarney  * Returns the corresponding C++ Rule objects.
6810e8c68abSShawn McCarney  *
6820e8c68abSShawn McCarney  * Throws an exception if parsing fails.
6830e8c68abSShawn McCarney  *
6840e8c68abSShawn McCarney  * @param element JSON element
6850e8c68abSShawn McCarney  * @return vector of Rule objects
6860e8c68abSShawn McCarney  */
687*92261f88SPatrick Williams std::vector<std::unique_ptr<Rule>> parseRuleArray(
688*92261f88SPatrick Williams     const nlohmann::json& element);
6890e8c68abSShawn McCarney 
6900e8c68abSShawn McCarney /**
69133e7eaa5SBob King  * Parses the "rule_id" or "actions" property in a JSON element.
69233e7eaa5SBob King  *
69333e7eaa5SBob King  * The element must contain one property or the other but not both.
69433e7eaa5SBob King  *
69533e7eaa5SBob King  * If the element contains a "rule_id" property, the corresponding C++
69633e7eaa5SBob King  * RunRuleAction object is returned.
69733e7eaa5SBob King  *
69833e7eaa5SBob King  * If the element contains an "actions" property, the corresponding C++ Action
69933e7eaa5SBob King  * objects are returned.
70033e7eaa5SBob King  *
70133e7eaa5SBob King  * Throws an exception if parsing fails.
70233e7eaa5SBob King  *
70333e7eaa5SBob King  * @param element JSON element
70433e7eaa5SBob King  * @return vector of Action objects
70533e7eaa5SBob King  */
706*92261f88SPatrick Williams std::vector<std::unique_ptr<Action>> parseRuleIDOrActionsProperty(
707*92261f88SPatrick Williams     const nlohmann::json& element);
70833e7eaa5SBob King 
70933e7eaa5SBob King /**
710315b0b62SBob King  * Parses a JSON element containing a run_rule action.
711315b0b62SBob King  *
712315b0b62SBob King  * Returns the corresponding C++ RunRuleAction object.
713315b0b62SBob King  *
714315b0b62SBob King  * Throws an exception if parsing fails.
715315b0b62SBob King  *
716315b0b62SBob King  * @param element JSON element
717315b0b62SBob King  * @return RunRuleAction object
718315b0b62SBob King  */
719315b0b62SBob King std::unique_ptr<RunRuleAction> parseRunRule(const nlohmann::json& element);
720315b0b62SBob King 
721315b0b62SBob King /**
72284614882SBob King  * Parses a JSON element containing a SensorDataFormat expressed as a string.
72384614882SBob King  *
72484614882SBob King  * Returns the corresponding SensorDataFormat enum value.
72584614882SBob King  *
72684614882SBob King  * Throws an exception if parsing fails.
72784614882SBob King  *
72884614882SBob King  * @param element JSON element
72984614882SBob King  * @return SensorDataFormat enum value
73084614882SBob King  */
731*92261f88SPatrick Williams pmbus_utils::SensorDataFormat parseSensorDataFormat(
732*92261f88SPatrick Williams     const nlohmann::json& element);
73384614882SBob King 
73484614882SBob King /**
73539eb08a9SShawn McCarney  * Parses a JSON element containing a sensor_monitoring object.
736a2f2a0dcSBob King  *
737a2f2a0dcSBob King  * Returns the corresponding C++ SensorMonitoring object.
738a2f2a0dcSBob King  *
739a2f2a0dcSBob King  * Throws an exception if parsing fails.
740a2f2a0dcSBob King  *
741a2f2a0dcSBob King  * @param element JSON element
742a2f2a0dcSBob King  * @return SensorMonitoring object
743a2f2a0dcSBob King  */
744*92261f88SPatrick Williams std::unique_ptr<SensorMonitoring> parseSensorMonitoring(
745*92261f88SPatrick Williams     const nlohmann::json& element);
746a2f2a0dcSBob King 
747a2f2a0dcSBob King /**
7482f9e14f6SShawn McCarney  * Parses a JSON element containing a SensorType expressed as a string.
74984614882SBob King  *
7502f9e14f6SShawn McCarney  * Returns the corresponding SensorType enum value.
75184614882SBob King  *
75284614882SBob King  * Throws an exception if parsing fails.
75384614882SBob King  *
75484614882SBob King  * @param element JSON element
7552f9e14f6SShawn McCarney  * @return SensorType enum value
75684614882SBob King  */
7572f9e14f6SShawn McCarney SensorType parseSensorType(const nlohmann::json& element);
75884614882SBob King 
75984614882SBob King /**
76018a68505SBob King  * Parses a JSON element containing a set_device action.
76118a68505SBob King  *
76218a68505SBob King  * Returns the corresponding C++ SetDeviceAction object.
76318a68505SBob King  *
76418a68505SBob King  * Throws an exception if parsing fails.
76518a68505SBob King  *
76618a68505SBob King  * @param element JSON element
76718a68505SBob King  * @return SetDeviceAction object
76818a68505SBob King  */
76918a68505SBob King std::unique_ptr<SetDeviceAction> parseSetDevice(const nlohmann::json& element);
77018a68505SBob King 
77118a68505SBob King /**
7720e8c68abSShawn McCarney  * Parses a JSON element containing a string.
7730e8c68abSShawn McCarney  *
7740e8c68abSShawn McCarney  * Returns the corresponding C++ string.
7750e8c68abSShawn McCarney  *
7760e8c68abSShawn McCarney  * Throws an exception if parsing fails.
7770e8c68abSShawn McCarney  *
7780e8c68abSShawn McCarney  * @param element JSON element
7790e8c68abSShawn McCarney  * @param isEmptyValid indicates whether an empty string value is valid
7800e8c68abSShawn McCarney  * @return string value
7810e8c68abSShawn McCarney  */
parseString(const nlohmann::json & element,bool isEmptyValid=false)7820e8c68abSShawn McCarney inline std::string parseString(const nlohmann::json& element,
7830e8c68abSShawn McCarney                                bool isEmptyValid = false)
7840e8c68abSShawn McCarney {
7850e8c68abSShawn McCarney     if (!element.is_string())
7860e8c68abSShawn McCarney     {
7870e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not a string"};
7880e8c68abSShawn McCarney     }
7896afbf1a5SBob King     std::string value = element.get<std::string>();
7900e8c68abSShawn McCarney     if (value.empty() && !isEmptyValid)
7910e8c68abSShawn McCarney     {
7920e8c68abSShawn McCarney         throw std::invalid_argument{"Element contains an empty string"};
7930e8c68abSShawn McCarney     }
7940e8c68abSShawn McCarney     return value;
7950e8c68abSShawn McCarney }
7960e8c68abSShawn McCarney 
7970e8c68abSShawn McCarney /**
798f617f893SBob King  * Parses a JSON element containing an 8-bit unsigned integer.
799f617f893SBob King  *
800f617f893SBob King  * Returns the corresponding C++ uint8_t value.
801f617f893SBob King  *
802f617f893SBob King  * Throws an exception if parsing fails.
803f617f893SBob King  *
804f617f893SBob King  * @param element JSON element
805f617f893SBob King  * @return uint8_t value
806f617f893SBob King  */
parseUint8(const nlohmann::json & element)807f617f893SBob King inline uint8_t parseUint8(const nlohmann::json& element)
808f617f893SBob King {
809f617f893SBob King     // Verify element contains an integer
810f617f893SBob King     if (!element.is_number_integer())
811f617f893SBob King     {
812f617f893SBob King         throw std::invalid_argument{"Element is not an integer"};
813f617f893SBob King     }
8146afbf1a5SBob King     int value = element.get<int>();
815f617f893SBob King     if ((value < 0) || (value > UINT8_MAX))
816f617f893SBob King     {
817f617f893SBob King         throw std::invalid_argument{"Element is not an 8-bit unsigned integer"};
818f617f893SBob King     }
819f617f893SBob King     return static_cast<uint8_t>(value);
820f617f893SBob King }
821f617f893SBob King 
822f617f893SBob King /**
8230e70113dSBob King  * Parses a JSON element containing an unsigned integer.
8240e70113dSBob King  *
8250e70113dSBob King  * Returns the corresponding C++ unsigned int value.
8260e70113dSBob King  *
8270e70113dSBob King  * Throws an exception if parsing fails.
8280e70113dSBob King  *
8290e70113dSBob King  * @param element JSON element
8300e70113dSBob King  * @return unsigned int value
8310e70113dSBob King  */
parseUnsignedInteger(const nlohmann::json & element)8320e70113dSBob King inline unsigned int parseUnsignedInteger(const nlohmann::json& element)
8330e70113dSBob King {
8340e70113dSBob King     // Verify element contains an unsigned integer
8350e70113dSBob King     if (!element.is_number_unsigned())
8360e70113dSBob King     {
8370e70113dSBob King         throw std::invalid_argument{"Element is not an unsigned integer"};
8380e70113dSBob King     }
8390e70113dSBob King     return element.get<unsigned int>();
8400e70113dSBob King }
8410e70113dSBob King 
8420e70113dSBob King /**
84384614882SBob King  * Parses a JSON element containing a VoutDataFormat expressed as a string.
84484614882SBob King  *
84584614882SBob King  * Returns the corresponding VoutDataFormat enum value.
84684614882SBob King  *
84784614882SBob King  * Throws an exception if parsing fails.
84884614882SBob King  *
84984614882SBob King  * @param element JSON element
85084614882SBob King  * @return VoutDataFormat enum value
85184614882SBob King  */
85284614882SBob King pmbus_utils::VoutDataFormat parseVoutDataFormat(const nlohmann::json& element);
85384614882SBob King 
85484614882SBob King /**
8550e8c68abSShawn McCarney  * Verifies that the specified JSON element is a JSON array.
8560e8c68abSShawn McCarney  *
8570e8c68abSShawn McCarney  * Throws an invalid_argument exception if the element is not an array.
8580e8c68abSShawn McCarney  *
8590e8c68abSShawn McCarney  * @param element JSON element
8600e8c68abSShawn McCarney  */
verifyIsArray(const nlohmann::json & element)8610e8c68abSShawn McCarney inline void verifyIsArray(const nlohmann::json& element)
8620e8c68abSShawn McCarney {
8630e8c68abSShawn McCarney     if (!element.is_array())
8640e8c68abSShawn McCarney     {
8650e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not an array"};
8660e8c68abSShawn McCarney     }
8670e8c68abSShawn McCarney }
8680e8c68abSShawn McCarney 
8690e8c68abSShawn McCarney /**
8700e8c68abSShawn McCarney  * Verifies that the specified JSON element is a JSON object.
8710e8c68abSShawn McCarney  *
8720e8c68abSShawn McCarney  * Throws an invalid_argument exception if the element is not an object.
8730e8c68abSShawn McCarney  *
8740e8c68abSShawn McCarney  * @param element JSON element
8750e8c68abSShawn McCarney  */
verifyIsObject(const nlohmann::json & element)8760e8c68abSShawn McCarney inline void verifyIsObject(const nlohmann::json& element)
8770e8c68abSShawn McCarney {
8780e8c68abSShawn McCarney     if (!element.is_object())
8790e8c68abSShawn McCarney     {
8800e8c68abSShawn McCarney         throw std::invalid_argument{"Element is not an object"};
8810e8c68abSShawn McCarney     }
8820e8c68abSShawn McCarney }
8830e8c68abSShawn McCarney 
8840e8c68abSShawn McCarney /**
8850e8c68abSShawn McCarney  * Verifies that the specified JSON element contains the expected number of
8860e8c68abSShawn McCarney  * properties.
8870e8c68abSShawn McCarney  *
8880e8c68abSShawn McCarney  * Throws an invalid_argument exception if the element contains a different
8890e8c68abSShawn McCarney  * number of properties.  This indicates the element contains an invalid
8900e8c68abSShawn McCarney  * property.
8910e8c68abSShawn McCarney  *
8920e8c68abSShawn McCarney  * @param element JSON element
8930e8c68abSShawn McCarney  * @param expectedCount expected number of properties in element
8940e8c68abSShawn McCarney  */
verifyPropertyCount(const nlohmann::json & element,unsigned int expectedCount)8950e8c68abSShawn McCarney inline void verifyPropertyCount(const nlohmann::json& element,
8960e8c68abSShawn McCarney                                 unsigned int expectedCount)
8970e8c68abSShawn McCarney {
8980e8c68abSShawn McCarney     if (element.size() != expectedCount)
8990e8c68abSShawn McCarney     {
9000e8c68abSShawn McCarney         throw std::invalid_argument{"Element contains an invalid property"};
9010e8c68abSShawn McCarney     }
9020e8c68abSShawn McCarney }
9030e8c68abSShawn McCarney 
9040e8c68abSShawn McCarney } // namespace internal
9050e8c68abSShawn McCarney 
9060e8c68abSShawn McCarney } // namespace phosphor::power::regulators::config_file_parser
907