blob: 1608fb9054b00b1b0a5c100c3a54f6e0e83f768d [file] [log] [blame]
Shawn McCarney0e8c68a2020-03-27 01:44:48 -05001/**
2 * Copyright © 2020 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 "action.hpp"
Bob King3a787542020-04-14 13:45:01 +080019#include "and_action.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050020#include "chassis.hpp"
Bob Kingb267b7e2020-04-22 14:42:39 +080021#include "compare_presence_action.hpp"
Bob Kingf2134322020-04-27 14:14:56 +080022#include "compare_vpd_action.hpp"
Bob King9c36c5f2020-04-06 11:34:09 +080023#include "configuration.hpp"
Bob King0e701132020-04-03 21:50:31 +080024#include "device.hpp"
Shawn McCarney91f87a52021-09-07 09:59:57 -050025#include "i2c_capture_bytes_action.hpp"
Bob Kingf09bfe02020-04-13 17:21:15 +080026#include "i2c_compare_bit_action.hpp"
27#include "i2c_compare_byte_action.hpp"
28#include "i2c_compare_bytes_action.hpp"
Bob King9c36c5f2020-04-06 11:34:09 +080029#include "i2c_interface.hpp"
Bob Kingf617f892020-03-30 19:03:35 +080030#include "i2c_write_bit_action.hpp"
Bob King87ff9d72020-03-31 14:02:55 +080031#include "i2c_write_byte_action.hpp"
Bob Kingbafcb862020-03-31 16:39:00 +080032#include "i2c_write_bytes_action.hpp"
Bob King93a89d72020-04-15 15:11:11 +080033#include "if_action.hpp"
Shawn McCarney11157852021-09-07 14:04:36 -050034#include "log_phase_fault_action.hpp"
Bob Kingf1b58dc2020-04-14 14:53:10 +080035#include "not_action.hpp"
Bob King0b51a9b2020-04-15 13:24:18 +080036#include "or_action.hpp"
Shawn McCarneyb70370b2021-09-07 12:07:40 -050037#include "phase_fault.hpp"
Shawn McCarney39eb08a2021-09-07 16:34:49 -050038#include "phase_fault_detection.hpp"
Bob King84614882020-04-30 13:13:48 +080039#include "pmbus_read_sensor_action.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050040#include "pmbus_write_vout_command_action.hpp"
Bob King9c36c5f2020-04-06 11:34:09 +080041#include "presence_detection.hpp"
42#include "rail.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050043#include "rule.hpp"
Bob King315b0b62020-04-03 21:47:58 +080044#include "run_rule_action.hpp"
Bob Kinga2f2a0d2020-04-09 13:32:14 +080045#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050046#include "sensors.hpp"
Bob King18a68502020-04-17 14:19:56 +080047#include "set_device_action.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050048
49#include <nlohmann/json.hpp>
50
51#include <cstdint>
52#include <filesystem>
53#include <memory>
54#include <stdexcept>
55#include <string>
56#include <tuple>
57#include <vector>
58
59namespace phosphor::power::regulators::config_file_parser
60{
61
62/**
63 * Parses the specified JSON configuration file.
64 *
65 * Returns the corresponding C++ Rule and Chassis objects.
66 *
67 * Throws a ConfigFileParserError if an error occurs.
68 *
69 * @param pathName configuration file path name
70 * @return tuple containing vectors of Rule and Chassis objects
71 */
72std::tuple<std::vector<std::unique_ptr<Rule>>,
73 std::vector<std::unique_ptr<Chassis>>>
74 parse(const std::filesystem::path& pathName);
75
76/*
77 * Internal implementation details for parse()
78 */
79namespace internal
80{
81
82/**
83 * Returns the specified property of the specified JSON element.
84 *
85 * Throws an invalid_argument exception if the property does not exist.
86 *
87 * @param element JSON element
88 * @param property property name
89 */
Patrick Williams974a7942023-05-31 20:18:53 -050090#pragma GCC diagnostic push
Patrick Williams5a355ba2023-06-01 16:54:00 -050091#if __GNUC__ == 13
Patrick Williams974a7942023-05-31 20:18:53 -050092#pragma GCC diagnostic ignored "-Wdangling-reference"
Patrick Williams5a355ba2023-06-01 16:54:00 -050093#endif
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050094inline const nlohmann::json& getRequiredProperty(const nlohmann::json& element,
95 const std::string& property)
96{
97 auto it = element.find(property);
98 if (it == element.end())
99 {
100 throw std::invalid_argument{"Required property missing: " + property};
101 }
102 return *it;
103}
Patrick Williams974a7942023-05-31 20:18:53 -0500104#pragma GCC diagnostic pop
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500105
106/**
107 * Parses a JSON element containing an action.
108 *
109 * Returns the corresponding C++ Action object.
110 *
111 * Throws an exception if parsing fails.
112 *
113 * @param element JSON element
114 * @return Action object
115 */
116std::unique_ptr<Action> parseAction(const nlohmann::json& element);
117
118/**
119 * Parses a JSON element containing an array of actions.
120 *
121 * Returns the corresponding C++ Action objects.
122 *
123 * Throws an exception if parsing fails.
124 *
125 * @param element JSON element
126 * @return vector of Action objects
127 */
128std::vector<std::unique_ptr<Action>>
129 parseActionArray(const nlohmann::json& element);
130
131/**
Bob King3a787542020-04-14 13:45:01 +0800132 * Parses a JSON element containing an and action.
133 *
134 * Returns the corresponding C++ AndAction object.
135 *
136 * Throws an exception if parsing fails.
137 *
138 * @param element JSON element
139 * @return AndAction object
140 */
141std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element);
142
143/**
Bob Kingf617f892020-03-30 19:03:35 +0800144 * Parses a JSON element containing a bit position (from 0-7).
145 *
146 * Returns the corresponding C++ uint8_t value.
147 *
148 * Throws an exception if parsing fails.
149 *
150 * @param element JSON element
151 * @return uint8_t value
152 */
153inline uint8_t parseBitPosition(const nlohmann::json& element)
154{
155 // Verify element contains an integer
156 if (!element.is_number_integer())
157 {
158 throw std::invalid_argument{"Element is not an integer"};
159 }
Bob King6afbf1a2020-04-06 17:19:01 +0800160 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800161 if ((value < 0) || (value > 7))
162 {
163 throw std::invalid_argument{"Element is not a bit position"};
164 }
165 return static_cast<uint8_t>(value);
166}
167
168/**
169 * Parses a JSON element containing a bit value (0 or 1).
170 *
171 * Returns the corresponding C++ uint8_t value.
172 *
173 * Throws an exception if parsing fails.
174 *
175 * @param element JSON element
176 * @return uint8_t value
177 */
178inline uint8_t parseBitValue(const nlohmann::json& element)
179{
180 // Verify element contains an integer
181 if (!element.is_number_integer())
182 {
183 throw std::invalid_argument{"Element is not an integer"};
184 }
Bob King6afbf1a2020-04-06 17:19:01 +0800185 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800186 if ((value < 0) || (value > 1))
187 {
188 throw std::invalid_argument{"Element is not a bit value"};
189 }
190 return static_cast<uint8_t>(value);
191}
192
193/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500194 * Parses a JSON element containing a boolean.
195 *
196 * Returns the corresponding C++ boolean value.
197 *
198 * Throws an exception if parsing fails.
199 *
200 * @param element JSON element
201 * @return boolean value
202 */
203inline bool parseBoolean(const nlohmann::json& element)
204{
205 // Verify element contains a boolean
206 if (!element.is_boolean())
207 {
208 throw std::invalid_argument{"Element is not a boolean"};
209 }
210 return element.get<bool>();
211}
212
213/**
Bob King0e701132020-04-03 21:50:31 +0800214 * Parses a JSON element containing a chassis.
215 *
216 * Returns the corresponding C++ Chassis object.
217 *
218 * Throws an exception if parsing fails.
219 *
220 * @param element JSON element
221 * @return Chassis object
222 */
223std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element);
224
225/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500226 * Parses a JSON element containing an array of chassis.
227 *
228 * Returns the corresponding C++ Chassis objects.
229 *
230 * Throws an exception if parsing fails.
231 *
232 * @param element JSON element
233 * @return vector of Chassis objects
234 */
235std::vector<std::unique_ptr<Chassis>>
236 parseChassisArray(const nlohmann::json& element);
237
238/**
Bob Kingb267b7e2020-04-22 14:42:39 +0800239 * Parses a JSON element containing a compare_presence action.
240 *
241 * Returns the corresponding C++ ComparePresenceAction object.
242 *
243 * Throws an exception if parsing fails.
244 *
245 * @param element JSON element
246 * @return ComparePresenceAction object
247 */
248std::unique_ptr<ComparePresenceAction>
249 parseComparePresence(const nlohmann::json& element);
250
251/**
Bob Kingf2134322020-04-27 14:14:56 +0800252 * Parses a JSON element containing a compare_vpd action.
253 *
254 * Returns the corresponding C++ CompareVPDAction object.
255 *
256 * Throws an exception if parsing fails.
257 *
258 * @param element JSON element
259 * @return CompareVPDAction object
260 */
261std::unique_ptr<CompareVPDAction>
262 parseCompareVPD(const nlohmann::json& element);
263
264/**
Shawn McCarney39eb08a2021-09-07 16:34:49 -0500265 * Parses a JSON element containing a configuration object.
Bob King33e7eaa2020-04-01 18:09:34 +0800266 *
267 * Returns the corresponding C++ Configuration object.
268 *
269 * Throws an exception if parsing fails.
270 *
271 * @param element JSON element
272 * @return Configuration object
273 */
274std::unique_ptr<Configuration>
275 parseConfiguration(const nlohmann::json& element);
276
277/**
Bob King9c36c5f2020-04-06 11:34:09 +0800278 * Parses a JSON element containing a device.
279 *
280 * Returns the corresponding C++ Device object.
281 *
282 * Throws an exception if parsing fails.
283 *
284 * @param element JSON element
285 * @return Device object
286 */
287std::unique_ptr<Device> parseDevice(const nlohmann::json& element);
288
289/**
Bob King0e701132020-04-03 21:50:31 +0800290 * Parses a JSON element containing an array of devices.
291 *
292 * Returns the corresponding C++ Device objects.
293 *
294 * Throws an exception if parsing fails.
295 *
296 * @param element JSON element
297 * @return vector of Device objects
298 */
299std::vector<std::unique_ptr<Device>>
300 parseDeviceArray(const nlohmann::json& element);
301
302/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500303 * Parses a JSON element containing a double (floating point number).
304 *
305 * Returns the corresponding C++ double value.
306 *
307 * Throws an exception if parsing fails.
308 *
309 * @param element JSON element
310 * @return double value
311 */
312inline double parseDouble(const nlohmann::json& element)
313{
314 // Verify element contains a number (integer or floating point)
315 if (!element.is_number())
316 {
317 throw std::invalid_argument{"Element is not a number"};
318 }
319 return element.get<double>();
320}
321
322/**
Bob Kingbafcb862020-03-31 16:39:00 +0800323 * Parses a JSON element containing a byte value expressed as a hexadecimal
324 * string.
325 *
326 * The JSON number data type does not support the hexadecimal format. For this
327 * reason, hexadecimal byte values are stored as strings in the configuration
328 * file.
329 *
330 * Returns the corresponding C++ uint8_t value.
331 *
332 * Throws an exception if parsing fails.
333 *
334 * @param element JSON element
335 * @return uint8_t value
336 */
337inline uint8_t parseHexByte(const nlohmann::json& element)
338{
339 if (!element.is_string())
340 {
341 throw std::invalid_argument{"Element is not a string"};
342 }
Bob King6afbf1a2020-04-06 17:19:01 +0800343 std::string value = element.get<std::string>();
Bob Kingbafcb862020-03-31 16:39:00 +0800344
345 bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) &&
346 (value.size() < 5) &&
347 (value.find_first_not_of("0123456789abcdefABCDEF", 2) ==
348 std::string::npos);
349 if (!isHex)
350 {
351 throw std::invalid_argument{"Element is not hexadecimal string"};
352 }
353 return static_cast<uint8_t>(std::stoul(value, 0, 0));
354}
355
356/**
357 * Parses a JSON element containing an array of byte values expressed as a
358 * hexadecimal strings.
359 *
360 * Returns the corresponding C++ uint8_t values.
361 *
362 * Throws an exception if parsing fails.
363 *
364 * @param element JSON element
365 * @return vector of uint8_t
366 */
367std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
368
369/**
Shawn McCarney91f87a52021-09-07 09:59:57 -0500370 * Parses a JSON element containing an i2c_capture_bytes action.
371 *
372 * Returns the corresponding C++ I2CCaptureBytesAction object.
373 *
374 * Throws an exception if parsing fails.
375 *
376 * @param element JSON element
377 * @return I2CCaptureBytesAction object
378 */
379std::unique_ptr<I2CCaptureBytesAction>
380 parseI2CCaptureBytes(const nlohmann::json& element);
381
382/**
Bob Kingf09bfe02020-04-13 17:21:15 +0800383 * Parses a JSON element containing an i2c_compare_bit action.
384 *
385 * Returns the corresponding C++ I2CCompareBitAction object.
386 *
387 * Throws an exception if parsing fails.
388 *
389 * @param element JSON element
390 * @return I2CCompareBitAction object
391 */
392std::unique_ptr<I2CCompareBitAction>
393 parseI2CCompareBit(const nlohmann::json& element);
394
395/**
396 * Parses a JSON element containing an i2c_compare_byte action.
397 *
398 * Returns the corresponding C++ I2CCompareByteAction object.
399 *
400 * Throws an exception if parsing fails.
401 *
402 * @param element JSON element
403 * @return I2CCompareByteAction object
404 */
405std::unique_ptr<I2CCompareByteAction>
406 parseI2CCompareByte(const nlohmann::json& element);
407
408/**
409 * Parses a JSON element containing an i2c_compare_bytes action.
410 *
411 * Returns the corresponding C++ I2CCompareBytesAction object.
412 *
413 * Throws an exception if parsing fails.
414 *
415 * @param element JSON element
416 * @return I2CCompareBytesAction object
417 */
418std::unique_ptr<I2CCompareBytesAction>
419 parseI2CCompareBytes(const nlohmann::json& element);
420
421/**
Bob King9c36c5f2020-04-06 11:34:09 +0800422 * Parses a JSON element containing an i2c_interface.
423 *
424 * Returns the corresponding C++ i2c::I2CInterface object.
425 *
426 * Throws an exception if parsing fails.
427 *
428 * @param element JSON element
429 * @return i2c::I2CInterface object
430 */
431std::unique_ptr<i2c::I2CInterface>
432 parseI2CInterface(const nlohmann::json& element);
433
434/**
Bob Kingf617f892020-03-30 19:03:35 +0800435 * Parses a JSON element containing an i2c_write_bit action.
436 *
437 * Returns the corresponding C++ I2CWriteBitAction object.
438 *
439 * Throws an exception if parsing fails.
440 *
441 * @param element JSON element
442 * @return I2CWriteBitAction object
443 */
444std::unique_ptr<I2CWriteBitAction>
445 parseI2CWriteBit(const nlohmann::json& element);
446
447/**
Bob King87ff9d72020-03-31 14:02:55 +0800448 * Parses a JSON element containing an i2c_write_byte action.
449 *
450 * Returns the corresponding C++ I2CWriteByteAction object.
451 *
452 * Throws an exception if parsing fails.
453 *
454 * @param element JSON element
455 * @return I2CWriteByteAction object
456 */
457std::unique_ptr<I2CWriteByteAction>
458 parseI2CWriteByte(const nlohmann::json& element);
459
460/**
Bob Kingbafcb862020-03-31 16:39:00 +0800461 * Parses a JSON element containing an i2c_write_bytes action.
462 *
463 * Returns the corresponding C++ I2CWriteBytesAction object.
464 *
465 * Throws an exception if parsing fails.
466 *
467 * @param element JSON element
468 * @return I2CWriteBytesAction object
469 */
470std::unique_ptr<I2CWriteBytesAction>
471 parseI2CWriteBytes(const nlohmann::json& element);
472
473/**
Bob King93a89d72020-04-15 15:11:11 +0800474 * Parses a JSON element containing an if action.
475 *
476 * Returns the corresponding C++ IfAction object.
477 *
478 * Throws an exception if parsing fails.
479 *
480 * @param element JSON element
481 * @return IfAction object
482 */
483std::unique_ptr<IfAction> parseIf(const nlohmann::json& element);
484
485/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500486 * Parses a JSON element containing an 8-bit signed integer.
487 *
488 * Returns the corresponding C++ int8_t value.
489 *
490 * Throws an exception if parsing fails.
491 *
492 * @param element JSON element
493 * @return int8_t value
494 */
495inline int8_t parseInt8(const nlohmann::json& element)
496{
497 // Verify element contains an integer
498 if (!element.is_number_integer())
499 {
500 throw std::invalid_argument{"Element is not an integer"};
501 }
Bob King6afbf1a2020-04-06 17:19:01 +0800502 int value = element.get<int>();
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500503 if ((value < INT8_MIN) || (value > INT8_MAX))
504 {
505 throw std::invalid_argument{"Element is not an 8-bit signed integer"};
506 }
507 return static_cast<int8_t>(value);
508}
509
510/**
Bob Kinga76898f2020-10-13 15:08:33 +0800511 * Parses a JSON element containing a relative inventory path.
512 *
513 * Returns the corresponding C++ string containing the absolute inventory path.
514 *
515 * Inventory paths in the JSON configuration file are relative. Adds the
516 * necessary prefix to make the path absolute.
517 *
518 * Throws an exception if parsing fails.
519 *
520 * @param element JSON element
521 * @return absolute D-Bus inventory path
522 */
523std::string parseInventoryPath(const nlohmann::json& element);
524
525/**
Shawn McCarney11157852021-09-07 14:04:36 -0500526 * Parses a JSON element containing a log_phase_fault action.
527 *
528 * Returns the corresponding C++ LogPhaseFaultAction object.
529 *
530 * Throws an exception if parsing fails.
531 *
532 * @param element JSON element
533 * @return LogPhaseFaultAction object
534 */
535std::unique_ptr<LogPhaseFaultAction>
536 parseLogPhaseFault(const nlohmann::json& element);
537
538/**
Bob Kingf1b58dc2020-04-14 14:53:10 +0800539 * Parses a JSON element containing a not action.
540 *
541 * Returns the corresponding C++ NotAction object.
542 *
543 * Throws an exception if parsing fails.
544 *
545 * @param element JSON element
546 * @return NotAction object
547 */
548std::unique_ptr<NotAction> parseNot(const nlohmann::json& element);
549
550/**
Bob King0b51a9b2020-04-15 13:24:18 +0800551 * Parses a JSON element containing an or action.
552 *
553 * Returns the corresponding C++ OrAction object.
554 *
555 * Throws an exception if parsing fails.
556 *
557 * @param element JSON element
558 * @return OrAction object
559 */
560std::unique_ptr<OrAction> parseOr(const nlohmann::json& element);
561
562/**
Shawn McCarney39eb08a2021-09-07 16:34:49 -0500563 * Parses a JSON element containing a phase_fault_detection object.
564 *
565 * Returns the corresponding C++ PhaseFaultDetection object.
566 *
567 * Throws an exception if parsing fails.
568 *
569 * @param element JSON element
570 * @return PhaseFaultDetection object
571 */
572std::unique_ptr<PhaseFaultDetection>
573 parsePhaseFaultDetection(const nlohmann::json& element);
574
575/**
Shawn McCarneyb70370b2021-09-07 12:07:40 -0500576 * Parses a JSON element containing a PhaseFaultType expressed as a string.
577 *
578 * Returns the corresponding PhaseFaultType enum value.
579 *
580 * Throws an exception if parsing fails.
581 *
582 * @param element JSON element
583 * @return PhaseFaultType enum value
584 */
585PhaseFaultType parsePhaseFaultType(const nlohmann::json& element);
586
587/**
Bob King84614882020-04-30 13:13:48 +0800588 * Parses a JSON element containing a pmbus_read_sensor action.
589 *
590 * Returns the corresponding C++ PMBusReadSensorAction object.
591 *
592 * Throws an exception if parsing fails.
593 *
594 * @param element JSON element
595 * @return PMBusReadSensorAction object
596 */
597std::unique_ptr<PMBusReadSensorAction>
598 parsePMBusReadSensor(const nlohmann::json& element);
599
600/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500601 * Parses a JSON element containing a pmbus_write_vout_command action.
602 *
603 * Returns the corresponding C++ PMBusWriteVoutCommandAction object.
604 *
605 * Throws an exception if parsing fails.
606 *
607 * @param element JSON element
608 * @return PMBusWriteVoutCommandAction object
609 */
610std::unique_ptr<PMBusWriteVoutCommandAction>
611 parsePMBusWriteVoutCommand(const nlohmann::json& element);
612
613/**
Shawn McCarney39eb08a2021-09-07 16:34:49 -0500614 * Parses a JSON element containing a presence_detection object.
Bob King2aafb1c2020-04-16 15:24:32 +0800615 *
616 * Returns the corresponding C++ PresenceDetection object.
617 *
618 * Throws an exception if parsing fails.
619 *
620 * @param element JSON element
621 * @return PresenceDetection object
622 */
623std::unique_ptr<PresenceDetection>
624 parsePresenceDetection(const nlohmann::json& element);
625
626/**
Bob Kinga2f2a0d2020-04-09 13:32:14 +0800627 * Parses a JSON element containing a rail.
628 *
629 * Returns the corresponding C++ Rail object.
630 *
631 * Throws an exception if parsing fails.
632 *
633 * @param element JSON element
634 * @return Rail object
635 */
636std::unique_ptr<Rail> parseRail(const nlohmann::json& element);
637
638/**
Bob King9c36c5f2020-04-06 11:34:09 +0800639 * Parses a JSON element containing an array of rails.
640 *
641 * Returns the corresponding C++ Rail objects.
642 *
643 * Throws an exception if parsing fails.
644 *
645 * @param element JSON element
646 * @return vector of Rail objects
647 */
648std::vector<std::unique_ptr<Rail>>
649 parseRailArray(const nlohmann::json& element);
650
651/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500652 * Parses the JSON root element of the entire configuration file.
653 *
654 * Returns the corresponding C++ Rule and Chassis objects.
655 *
656 * Throws an exception if parsing fails.
657 *
658 * @param element JSON element
659 * @return tuple containing vectors of Rule and Chassis objects
660 */
661std::tuple<std::vector<std::unique_ptr<Rule>>,
662 std::vector<std::unique_ptr<Chassis>>>
663 parseRoot(const nlohmann::json& element);
664
665/**
666 * Parses a JSON element containing a rule.
667 *
668 * Returns the corresponding C++ Rule object.
669 *
670 * Throws an exception if parsing fails.
671 *
672 * @param element JSON element
673 * @return Rule object
674 */
675std::unique_ptr<Rule> parseRule(const nlohmann::json& element);
676
677/**
678 * Parses a JSON element containing an array of rules.
679 *
680 * Returns the corresponding C++ Rule objects.
681 *
682 * Throws an exception if parsing fails.
683 *
684 * @param element JSON element
685 * @return vector of Rule objects
686 */
687std::vector<std::unique_ptr<Rule>>
688 parseRuleArray(const nlohmann::json& element);
689
690/**
Bob King33e7eaa2020-04-01 18:09:34 +0800691 * Parses the "rule_id" or "actions" property in a JSON element.
692 *
693 * The element must contain one property or the other but not both.
694 *
695 * If the element contains a "rule_id" property, the corresponding C++
696 * RunRuleAction object is returned.
697 *
698 * If the element contains an "actions" property, the corresponding C++ Action
699 * objects are returned.
700 *
701 * Throws an exception if parsing fails.
702 *
703 * @param element JSON element
704 * @return vector of Action objects
705 */
706std::vector<std::unique_ptr<Action>>
707 parseRuleIDOrActionsProperty(const nlohmann::json& element);
708
709/**
Bob King315b0b62020-04-03 21:47:58 +0800710 * Parses a JSON element containing a run_rule action.
711 *
712 * Returns the corresponding C++ RunRuleAction object.
713 *
714 * Throws an exception if parsing fails.
715 *
716 * @param element JSON element
717 * @return RunRuleAction object
718 */
719std::unique_ptr<RunRuleAction> parseRunRule(const nlohmann::json& element);
720
721/**
Bob King84614882020-04-30 13:13:48 +0800722 * Parses a JSON element containing a SensorDataFormat expressed as a string.
723 *
724 * Returns the corresponding SensorDataFormat enum value.
725 *
726 * Throws an exception if parsing fails.
727 *
728 * @param element JSON element
729 * @return SensorDataFormat enum value
730 */
731pmbus_utils::SensorDataFormat
732 parseSensorDataFormat(const nlohmann::json& element);
733
734/**
Shawn McCarney39eb08a2021-09-07 16:34:49 -0500735 * Parses a JSON element containing a sensor_monitoring object.
Bob Kinga2f2a0d2020-04-09 13:32:14 +0800736 *
737 * Returns the corresponding C++ SensorMonitoring object.
738 *
739 * Throws an exception if parsing fails.
740 *
741 * @param element JSON element
742 * @return SensorMonitoring object
743 */
744std::unique_ptr<SensorMonitoring>
745 parseSensorMonitoring(const nlohmann::json& element);
746
747/**
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500748 * Parses a JSON element containing a SensorType expressed as a string.
Bob King84614882020-04-30 13:13:48 +0800749 *
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500750 * Returns the corresponding SensorType enum value.
Bob King84614882020-04-30 13:13:48 +0800751 *
752 * Throws an exception if parsing fails.
753 *
754 * @param element JSON element
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500755 * @return SensorType enum value
Bob King84614882020-04-30 13:13:48 +0800756 */
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500757SensorType parseSensorType(const nlohmann::json& element);
Bob King84614882020-04-30 13:13:48 +0800758
759/**
Bob King18a68502020-04-17 14:19:56 +0800760 * Parses a JSON element containing a set_device action.
761 *
762 * Returns the corresponding C++ SetDeviceAction object.
763 *
764 * Throws an exception if parsing fails.
765 *
766 * @param element JSON element
767 * @return SetDeviceAction object
768 */
769std::unique_ptr<SetDeviceAction> parseSetDevice(const nlohmann::json& element);
770
771/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500772 * Parses a JSON element containing a string.
773 *
774 * Returns the corresponding C++ string.
775 *
776 * Throws an exception if parsing fails.
777 *
778 * @param element JSON element
779 * @param isEmptyValid indicates whether an empty string value is valid
780 * @return string value
781 */
782inline std::string parseString(const nlohmann::json& element,
783 bool isEmptyValid = false)
784{
785 if (!element.is_string())
786 {
787 throw std::invalid_argument{"Element is not a string"};
788 }
Bob King6afbf1a2020-04-06 17:19:01 +0800789 std::string value = element.get<std::string>();
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500790 if (value.empty() && !isEmptyValid)
791 {
792 throw std::invalid_argument{"Element contains an empty string"};
793 }
794 return value;
795}
796
797/**
Bob Kingf617f892020-03-30 19:03:35 +0800798 * Parses a JSON element containing an 8-bit unsigned integer.
799 *
800 * Returns the corresponding C++ uint8_t value.
801 *
802 * Throws an exception if parsing fails.
803 *
804 * @param element JSON element
805 * @return uint8_t value
806 */
807inline uint8_t parseUint8(const nlohmann::json& element)
808{
809 // Verify element contains an integer
810 if (!element.is_number_integer())
811 {
812 throw std::invalid_argument{"Element is not an integer"};
813 }
Bob King6afbf1a2020-04-06 17:19:01 +0800814 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800815 if ((value < 0) || (value > UINT8_MAX))
816 {
817 throw std::invalid_argument{"Element is not an 8-bit unsigned integer"};
818 }
819 return static_cast<uint8_t>(value);
820}
821
822/**
Bob King0e701132020-04-03 21:50:31 +0800823 * Parses a JSON element containing an unsigned integer.
824 *
825 * Returns the corresponding C++ unsigned int value.
826 *
827 * Throws an exception if parsing fails.
828 *
829 * @param element JSON element
830 * @return unsigned int value
831 */
832inline unsigned int parseUnsignedInteger(const nlohmann::json& element)
833{
834 // Verify element contains an unsigned integer
835 if (!element.is_number_unsigned())
836 {
837 throw std::invalid_argument{"Element is not an unsigned integer"};
838 }
839 return element.get<unsigned int>();
840}
841
842/**
Bob King84614882020-04-30 13:13:48 +0800843 * Parses a JSON element containing a VoutDataFormat expressed as a string.
844 *
845 * Returns the corresponding VoutDataFormat enum value.
846 *
847 * Throws an exception if parsing fails.
848 *
849 * @param element JSON element
850 * @return VoutDataFormat enum value
851 */
852pmbus_utils::VoutDataFormat parseVoutDataFormat(const nlohmann::json& element);
853
854/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500855 * Verifies that the specified JSON element is a JSON array.
856 *
857 * Throws an invalid_argument exception if the element is not an array.
858 *
859 * @param element JSON element
860 */
861inline void verifyIsArray(const nlohmann::json& element)
862{
863 if (!element.is_array())
864 {
865 throw std::invalid_argument{"Element is not an array"};
866 }
867}
868
869/**
870 * Verifies that the specified JSON element is a JSON object.
871 *
872 * Throws an invalid_argument exception if the element is not an object.
873 *
874 * @param element JSON element
875 */
876inline void verifyIsObject(const nlohmann::json& element)
877{
878 if (!element.is_object())
879 {
880 throw std::invalid_argument{"Element is not an object"};
881 }
882}
883
884/**
885 * Verifies that the specified JSON element contains the expected number of
886 * properties.
887 *
888 * Throws an invalid_argument exception if the element contains a different
889 * number of properties. This indicates the element contains an invalid
890 * property.
891 *
892 * @param element JSON element
893 * @param expectedCount expected number of properties in element
894 */
895inline void verifyPropertyCount(const nlohmann::json& element,
896 unsigned int expectedCount)
897{
898 if (element.size() != expectedCount)
899 {
900 throw std::invalid_argument{"Element contains an invalid property"};
901 }
902}
903
904} // namespace internal
905
906} // namespace phosphor::power::regulators::config_file_parser