blob: 63c8bf8d04f569293ceadf56d3dca5754cf59af9 [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"
Bob King84614882020-04-30 13:13:48 +080038#include "pmbus_read_sensor_action.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050039#include "pmbus_write_vout_command_action.hpp"
Bob King9c36c5f2020-04-06 11:34:09 +080040#include "presence_detection.hpp"
41#include "rail.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050042#include "rule.hpp"
Bob King315b0b62020-04-03 21:47:58 +080043#include "run_rule_action.hpp"
Bob Kinga2f2a0d2020-04-09 13:32:14 +080044#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050045#include "sensors.hpp"
Bob King18a68502020-04-17 14:19:56 +080046#include "set_device_action.hpp"
Shawn McCarney0e8c68a2020-03-27 01:44:48 -050047
48#include <nlohmann/json.hpp>
49
50#include <cstdint>
51#include <filesystem>
52#include <memory>
53#include <stdexcept>
54#include <string>
55#include <tuple>
56#include <vector>
57
58namespace phosphor::power::regulators::config_file_parser
59{
60
61/**
62 * Parses the specified JSON configuration file.
63 *
64 * Returns the corresponding C++ Rule and Chassis objects.
65 *
66 * Throws a ConfigFileParserError if an error occurs.
67 *
68 * @param pathName configuration file path name
69 * @return tuple containing vectors of Rule and Chassis objects
70 */
71std::tuple<std::vector<std::unique_ptr<Rule>>,
72 std::vector<std::unique_ptr<Chassis>>>
73 parse(const std::filesystem::path& pathName);
74
75/*
76 * Internal implementation details for parse()
77 */
78namespace internal
79{
80
81/**
82 * Returns the specified property of the specified JSON element.
83 *
84 * Throws an invalid_argument exception if the property does not exist.
85 *
86 * @param element JSON element
87 * @param property property name
88 */
89inline const nlohmann::json& getRequiredProperty(const nlohmann::json& element,
90 const std::string& property)
91{
92 auto it = element.find(property);
93 if (it == element.end())
94 {
95 throw std::invalid_argument{"Required property missing: " + property};
96 }
97 return *it;
98}
99
100/**
101 * Parses a JSON element containing an action.
102 *
103 * Returns the corresponding C++ Action object.
104 *
105 * Throws an exception if parsing fails.
106 *
107 * @param element JSON element
108 * @return Action object
109 */
110std::unique_ptr<Action> parseAction(const nlohmann::json& element);
111
112/**
113 * Parses a JSON element containing an array of actions.
114 *
115 * Returns the corresponding C++ Action objects.
116 *
117 * Throws an exception if parsing fails.
118 *
119 * @param element JSON element
120 * @return vector of Action objects
121 */
122std::vector<std::unique_ptr<Action>>
123 parseActionArray(const nlohmann::json& element);
124
125/**
Bob King3a787542020-04-14 13:45:01 +0800126 * Parses a JSON element containing an and action.
127 *
128 * Returns the corresponding C++ AndAction object.
129 *
130 * Throws an exception if parsing fails.
131 *
132 * @param element JSON element
133 * @return AndAction object
134 */
135std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element);
136
137/**
Bob Kingf617f892020-03-30 19:03:35 +0800138 * Parses a JSON element containing a bit position (from 0-7).
139 *
140 * Returns the corresponding C++ uint8_t value.
141 *
142 * Throws an exception if parsing fails.
143 *
144 * @param element JSON element
145 * @return uint8_t value
146 */
147inline uint8_t parseBitPosition(const nlohmann::json& element)
148{
149 // Verify element contains an integer
150 if (!element.is_number_integer())
151 {
152 throw std::invalid_argument{"Element is not an integer"};
153 }
Bob King6afbf1a2020-04-06 17:19:01 +0800154 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800155 if ((value < 0) || (value > 7))
156 {
157 throw std::invalid_argument{"Element is not a bit position"};
158 }
159 return static_cast<uint8_t>(value);
160}
161
162/**
163 * Parses a JSON element containing a bit value (0 or 1).
164 *
165 * Returns the corresponding C++ uint8_t value.
166 *
167 * Throws an exception if parsing fails.
168 *
169 * @param element JSON element
170 * @return uint8_t value
171 */
172inline uint8_t parseBitValue(const nlohmann::json& element)
173{
174 // Verify element contains an integer
175 if (!element.is_number_integer())
176 {
177 throw std::invalid_argument{"Element is not an integer"};
178 }
Bob King6afbf1a2020-04-06 17:19:01 +0800179 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800180 if ((value < 0) || (value > 1))
181 {
182 throw std::invalid_argument{"Element is not a bit value"};
183 }
184 return static_cast<uint8_t>(value);
185}
186
187/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500188 * Parses a JSON element containing a boolean.
189 *
190 * Returns the corresponding C++ boolean value.
191 *
192 * Throws an exception if parsing fails.
193 *
194 * @param element JSON element
195 * @return boolean value
196 */
197inline bool parseBoolean(const nlohmann::json& element)
198{
199 // Verify element contains a boolean
200 if (!element.is_boolean())
201 {
202 throw std::invalid_argument{"Element is not a boolean"};
203 }
204 return element.get<bool>();
205}
206
207/**
Bob King0e701132020-04-03 21:50:31 +0800208 * Parses a JSON element containing a chassis.
209 *
210 * Returns the corresponding C++ Chassis object.
211 *
212 * Throws an exception if parsing fails.
213 *
214 * @param element JSON element
215 * @return Chassis object
216 */
217std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element);
218
219/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500220 * Parses a JSON element containing an array of chassis.
221 *
222 * Returns the corresponding C++ Chassis objects.
223 *
224 * Throws an exception if parsing fails.
225 *
226 * @param element JSON element
227 * @return vector of Chassis objects
228 */
229std::vector<std::unique_ptr<Chassis>>
230 parseChassisArray(const nlohmann::json& element);
231
232/**
Bob Kingb267b7e2020-04-22 14:42:39 +0800233 * Parses a JSON element containing a compare_presence action.
234 *
235 * Returns the corresponding C++ ComparePresenceAction object.
236 *
237 * Throws an exception if parsing fails.
238 *
239 * @param element JSON element
240 * @return ComparePresenceAction object
241 */
242std::unique_ptr<ComparePresenceAction>
243 parseComparePresence(const nlohmann::json& element);
244
245/**
Bob Kingf2134322020-04-27 14:14:56 +0800246 * Parses a JSON element containing a compare_vpd action.
247 *
248 * Returns the corresponding C++ CompareVPDAction object.
249 *
250 * Throws an exception if parsing fails.
251 *
252 * @param element JSON element
253 * @return CompareVPDAction object
254 */
255std::unique_ptr<CompareVPDAction>
256 parseCompareVPD(const nlohmann::json& element);
257
258/**
Bob King33e7eaa2020-04-01 18:09:34 +0800259 * Parses a JSON element containing a configuration.
260 *
261 * Returns the corresponding C++ Configuration object.
262 *
263 * Throws an exception if parsing fails.
264 *
265 * @param element JSON element
266 * @return Configuration object
267 */
268std::unique_ptr<Configuration>
269 parseConfiguration(const nlohmann::json& element);
270
271/**
Bob King9c36c5f2020-04-06 11:34:09 +0800272 * Parses a JSON element containing a device.
273 *
274 * Returns the corresponding C++ Device object.
275 *
276 * Throws an exception if parsing fails.
277 *
278 * @param element JSON element
279 * @return Device object
280 */
281std::unique_ptr<Device> parseDevice(const nlohmann::json& element);
282
283/**
Bob King0e701132020-04-03 21:50:31 +0800284 * Parses a JSON element containing an array of devices.
285 *
286 * Returns the corresponding C++ Device objects.
287 *
288 * Throws an exception if parsing fails.
289 *
290 * @param element JSON element
291 * @return vector of Device objects
292 */
293std::vector<std::unique_ptr<Device>>
294 parseDeviceArray(const nlohmann::json& element);
295
296/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500297 * Parses a JSON element containing a double (floating point number).
298 *
299 * Returns the corresponding C++ double value.
300 *
301 * Throws an exception if parsing fails.
302 *
303 * @param element JSON element
304 * @return double value
305 */
306inline double parseDouble(const nlohmann::json& element)
307{
308 // Verify element contains a number (integer or floating point)
309 if (!element.is_number())
310 {
311 throw std::invalid_argument{"Element is not a number"};
312 }
313 return element.get<double>();
314}
315
316/**
Bob Kingbafcb862020-03-31 16:39:00 +0800317 * Parses a JSON element containing a byte value expressed as a hexadecimal
318 * string.
319 *
320 * The JSON number data type does not support the hexadecimal format. For this
321 * reason, hexadecimal byte values are stored as strings in the configuration
322 * file.
323 *
324 * Returns the corresponding C++ uint8_t value.
325 *
326 * Throws an exception if parsing fails.
327 *
328 * @param element JSON element
329 * @return uint8_t value
330 */
331inline uint8_t parseHexByte(const nlohmann::json& element)
332{
333 if (!element.is_string())
334 {
335 throw std::invalid_argument{"Element is not a string"};
336 }
Bob King6afbf1a2020-04-06 17:19:01 +0800337 std::string value = element.get<std::string>();
Bob Kingbafcb862020-03-31 16:39:00 +0800338
339 bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) &&
340 (value.size() < 5) &&
341 (value.find_first_not_of("0123456789abcdefABCDEF", 2) ==
342 std::string::npos);
343 if (!isHex)
344 {
345 throw std::invalid_argument{"Element is not hexadecimal string"};
346 }
347 return static_cast<uint8_t>(std::stoul(value, 0, 0));
348}
349
350/**
351 * Parses a JSON element containing an array of byte values expressed as a
352 * hexadecimal strings.
353 *
354 * Returns the corresponding C++ uint8_t values.
355 *
356 * Throws an exception if parsing fails.
357 *
358 * @param element JSON element
359 * @return vector of uint8_t
360 */
361std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
362
363/**
Shawn McCarney91f87a52021-09-07 09:59:57 -0500364 * Parses a JSON element containing an i2c_capture_bytes action.
365 *
366 * Returns the corresponding C++ I2CCaptureBytesAction object.
367 *
368 * Throws an exception if parsing fails.
369 *
370 * @param element JSON element
371 * @return I2CCaptureBytesAction object
372 */
373std::unique_ptr<I2CCaptureBytesAction>
374 parseI2CCaptureBytes(const nlohmann::json& element);
375
376/**
Bob Kingf09bfe02020-04-13 17:21:15 +0800377 * Parses a JSON element containing an i2c_compare_bit action.
378 *
379 * Returns the corresponding C++ I2CCompareBitAction object.
380 *
381 * Throws an exception if parsing fails.
382 *
383 * @param element JSON element
384 * @return I2CCompareBitAction object
385 */
386std::unique_ptr<I2CCompareBitAction>
387 parseI2CCompareBit(const nlohmann::json& element);
388
389/**
390 * Parses a JSON element containing an i2c_compare_byte action.
391 *
392 * Returns the corresponding C++ I2CCompareByteAction object.
393 *
394 * Throws an exception if parsing fails.
395 *
396 * @param element JSON element
397 * @return I2CCompareByteAction object
398 */
399std::unique_ptr<I2CCompareByteAction>
400 parseI2CCompareByte(const nlohmann::json& element);
401
402/**
403 * Parses a JSON element containing an i2c_compare_bytes action.
404 *
405 * Returns the corresponding C++ I2CCompareBytesAction object.
406 *
407 * Throws an exception if parsing fails.
408 *
409 * @param element JSON element
410 * @return I2CCompareBytesAction object
411 */
412std::unique_ptr<I2CCompareBytesAction>
413 parseI2CCompareBytes(const nlohmann::json& element);
414
415/**
Bob King9c36c5f2020-04-06 11:34:09 +0800416 * Parses a JSON element containing an i2c_interface.
417 *
418 * Returns the corresponding C++ i2c::I2CInterface object.
419 *
420 * Throws an exception if parsing fails.
421 *
422 * @param element JSON element
423 * @return i2c::I2CInterface object
424 */
425std::unique_ptr<i2c::I2CInterface>
426 parseI2CInterface(const nlohmann::json& element);
427
428/**
Bob Kingf617f892020-03-30 19:03:35 +0800429 * Parses a JSON element containing an i2c_write_bit action.
430 *
431 * Returns the corresponding C++ I2CWriteBitAction object.
432 *
433 * Throws an exception if parsing fails.
434 *
435 * @param element JSON element
436 * @return I2CWriteBitAction object
437 */
438std::unique_ptr<I2CWriteBitAction>
439 parseI2CWriteBit(const nlohmann::json& element);
440
441/**
Bob King87ff9d72020-03-31 14:02:55 +0800442 * Parses a JSON element containing an i2c_write_byte action.
443 *
444 * Returns the corresponding C++ I2CWriteByteAction object.
445 *
446 * Throws an exception if parsing fails.
447 *
448 * @param element JSON element
449 * @return I2CWriteByteAction object
450 */
451std::unique_ptr<I2CWriteByteAction>
452 parseI2CWriteByte(const nlohmann::json& element);
453
454/**
Bob Kingbafcb862020-03-31 16:39:00 +0800455 * Parses a JSON element containing an i2c_write_bytes action.
456 *
457 * Returns the corresponding C++ I2CWriteBytesAction object.
458 *
459 * Throws an exception if parsing fails.
460 *
461 * @param element JSON element
462 * @return I2CWriteBytesAction object
463 */
464std::unique_ptr<I2CWriteBytesAction>
465 parseI2CWriteBytes(const nlohmann::json& element);
466
467/**
Bob King93a89d72020-04-15 15:11:11 +0800468 * Parses a JSON element containing an if action.
469 *
470 * Returns the corresponding C++ IfAction object.
471 *
472 * Throws an exception if parsing fails.
473 *
474 * @param element JSON element
475 * @return IfAction object
476 */
477std::unique_ptr<IfAction> parseIf(const nlohmann::json& element);
478
479/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500480 * Parses a JSON element containing an 8-bit signed integer.
481 *
482 * Returns the corresponding C++ int8_t value.
483 *
484 * Throws an exception if parsing fails.
485 *
486 * @param element JSON element
487 * @return int8_t value
488 */
489inline int8_t parseInt8(const nlohmann::json& element)
490{
491 // Verify element contains an integer
492 if (!element.is_number_integer())
493 {
494 throw std::invalid_argument{"Element is not an integer"};
495 }
Bob King6afbf1a2020-04-06 17:19:01 +0800496 int value = element.get<int>();
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500497 if ((value < INT8_MIN) || (value > INT8_MAX))
498 {
499 throw std::invalid_argument{"Element is not an 8-bit signed integer"};
500 }
501 return static_cast<int8_t>(value);
502}
503
504/**
Bob Kinga76898f2020-10-13 15:08:33 +0800505 * Parses a JSON element containing a relative inventory path.
506 *
507 * Returns the corresponding C++ string containing the absolute inventory path.
508 *
509 * Inventory paths in the JSON configuration file are relative. Adds the
510 * necessary prefix to make the path absolute.
511 *
512 * Throws an exception if parsing fails.
513 *
514 * @param element JSON element
515 * @return absolute D-Bus inventory path
516 */
517std::string parseInventoryPath(const nlohmann::json& element);
518
519/**
Shawn McCarney11157852021-09-07 14:04:36 -0500520 * Parses a JSON element containing a log_phase_fault action.
521 *
522 * Returns the corresponding C++ LogPhaseFaultAction object.
523 *
524 * Throws an exception if parsing fails.
525 *
526 * @param element JSON element
527 * @return LogPhaseFaultAction object
528 */
529std::unique_ptr<LogPhaseFaultAction>
530 parseLogPhaseFault(const nlohmann::json& element);
531
532/**
Bob Kingf1b58dc2020-04-14 14:53:10 +0800533 * Parses a JSON element containing a not action.
534 *
535 * Returns the corresponding C++ NotAction object.
536 *
537 * Throws an exception if parsing fails.
538 *
539 * @param element JSON element
540 * @return NotAction object
541 */
542std::unique_ptr<NotAction> parseNot(const nlohmann::json& element);
543
544/**
Bob King0b51a9b2020-04-15 13:24:18 +0800545 * Parses a JSON element containing an or action.
546 *
547 * Returns the corresponding C++ OrAction object.
548 *
549 * Throws an exception if parsing fails.
550 *
551 * @param element JSON element
552 * @return OrAction object
553 */
554std::unique_ptr<OrAction> parseOr(const nlohmann::json& element);
555
556/**
Shawn McCarneyb70370b2021-09-07 12:07:40 -0500557 * Parses a JSON element containing a PhaseFaultType expressed as a string.
558 *
559 * Returns the corresponding PhaseFaultType enum value.
560 *
561 * Throws an exception if parsing fails.
562 *
563 * @param element JSON element
564 * @return PhaseFaultType enum value
565 */
566PhaseFaultType parsePhaseFaultType(const nlohmann::json& element);
567
568/**
Bob King84614882020-04-30 13:13:48 +0800569 * Parses a JSON element containing a pmbus_read_sensor action.
570 *
571 * Returns the corresponding C++ PMBusReadSensorAction object.
572 *
573 * Throws an exception if parsing fails.
574 *
575 * @param element JSON element
576 * @return PMBusReadSensorAction object
577 */
578std::unique_ptr<PMBusReadSensorAction>
579 parsePMBusReadSensor(const nlohmann::json& element);
580
581/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500582 * Parses a JSON element containing a pmbus_write_vout_command action.
583 *
584 * Returns the corresponding C++ PMBusWriteVoutCommandAction object.
585 *
586 * Throws an exception if parsing fails.
587 *
588 * @param element JSON element
589 * @return PMBusWriteVoutCommandAction object
590 */
591std::unique_ptr<PMBusWriteVoutCommandAction>
592 parsePMBusWriteVoutCommand(const nlohmann::json& element);
593
594/**
Bob King2aafb1c2020-04-16 15:24:32 +0800595 * Parses a JSON element containing a presence detection operation.
596 *
597 * Returns the corresponding C++ PresenceDetection object.
598 *
599 * Throws an exception if parsing fails.
600 *
601 * @param element JSON element
602 * @return PresenceDetection object
603 */
604std::unique_ptr<PresenceDetection>
605 parsePresenceDetection(const nlohmann::json& element);
606
607/**
Bob Kinga2f2a0d2020-04-09 13:32:14 +0800608 * Parses a JSON element containing a rail.
609 *
610 * Returns the corresponding C++ Rail object.
611 *
612 * Throws an exception if parsing fails.
613 *
614 * @param element JSON element
615 * @return Rail object
616 */
617std::unique_ptr<Rail> parseRail(const nlohmann::json& element);
618
619/**
Bob King9c36c5f2020-04-06 11:34:09 +0800620 * Parses a JSON element containing an array of rails.
621 *
622 * Returns the corresponding C++ Rail objects.
623 *
624 * Throws an exception if parsing fails.
625 *
626 * @param element JSON element
627 * @return vector of Rail objects
628 */
629std::vector<std::unique_ptr<Rail>>
630 parseRailArray(const nlohmann::json& element);
631
632/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500633 * Parses the JSON root element of the entire configuration file.
634 *
635 * Returns the corresponding C++ Rule and Chassis objects.
636 *
637 * Throws an exception if parsing fails.
638 *
639 * @param element JSON element
640 * @return tuple containing vectors of Rule and Chassis objects
641 */
642std::tuple<std::vector<std::unique_ptr<Rule>>,
643 std::vector<std::unique_ptr<Chassis>>>
644 parseRoot(const nlohmann::json& element);
645
646/**
647 * Parses a JSON element containing a rule.
648 *
649 * Returns the corresponding C++ Rule object.
650 *
651 * Throws an exception if parsing fails.
652 *
653 * @param element JSON element
654 * @return Rule object
655 */
656std::unique_ptr<Rule> parseRule(const nlohmann::json& element);
657
658/**
659 * Parses a JSON element containing an array of rules.
660 *
661 * Returns the corresponding C++ Rule objects.
662 *
663 * Throws an exception if parsing fails.
664 *
665 * @param element JSON element
666 * @return vector of Rule objects
667 */
668std::vector<std::unique_ptr<Rule>>
669 parseRuleArray(const nlohmann::json& element);
670
671/**
Bob King33e7eaa2020-04-01 18:09:34 +0800672 * Parses the "rule_id" or "actions" property in a JSON element.
673 *
674 * The element must contain one property or the other but not both.
675 *
676 * If the element contains a "rule_id" property, the corresponding C++
677 * RunRuleAction object is returned.
678 *
679 * If the element contains an "actions" property, the corresponding C++ Action
680 * objects are returned.
681 *
682 * Throws an exception if parsing fails.
683 *
684 * @param element JSON element
685 * @return vector of Action objects
686 */
687std::vector<std::unique_ptr<Action>>
688 parseRuleIDOrActionsProperty(const nlohmann::json& element);
689
690/**
Bob King315b0b62020-04-03 21:47:58 +0800691 * Parses a JSON element containing a run_rule action.
692 *
693 * Returns the corresponding C++ RunRuleAction object.
694 *
695 * Throws an exception if parsing fails.
696 *
697 * @param element JSON element
698 * @return RunRuleAction object
699 */
700std::unique_ptr<RunRuleAction> parseRunRule(const nlohmann::json& element);
701
702/**
Bob King84614882020-04-30 13:13:48 +0800703 * Parses a JSON element containing a SensorDataFormat expressed as a string.
704 *
705 * Returns the corresponding SensorDataFormat enum value.
706 *
707 * Throws an exception if parsing fails.
708 *
709 * @param element JSON element
710 * @return SensorDataFormat enum value
711 */
712pmbus_utils::SensorDataFormat
713 parseSensorDataFormat(const nlohmann::json& element);
714
715/**
Bob Kinga2f2a0d2020-04-09 13:32:14 +0800716 * Parses a JSON element containing a sensor monitoring operation.
717 *
718 * Returns the corresponding C++ SensorMonitoring object.
719 *
720 * Throws an exception if parsing fails.
721 *
722 * @param element JSON element
723 * @return SensorMonitoring object
724 */
725std::unique_ptr<SensorMonitoring>
726 parseSensorMonitoring(const nlohmann::json& element);
727
728/**
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500729 * Parses a JSON element containing a SensorType expressed as a string.
Bob King84614882020-04-30 13:13:48 +0800730 *
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500731 * Returns the corresponding SensorType enum value.
Bob King84614882020-04-30 13:13:48 +0800732 *
733 * Throws an exception if parsing fails.
734 *
735 * @param element JSON element
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500736 * @return SensorType enum value
Bob King84614882020-04-30 13:13:48 +0800737 */
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500738SensorType parseSensorType(const nlohmann::json& element);
Bob King84614882020-04-30 13:13:48 +0800739
740/**
Bob King18a68502020-04-17 14:19:56 +0800741 * Parses a JSON element containing a set_device action.
742 *
743 * Returns the corresponding C++ SetDeviceAction object.
744 *
745 * Throws an exception if parsing fails.
746 *
747 * @param element JSON element
748 * @return SetDeviceAction object
749 */
750std::unique_ptr<SetDeviceAction> parseSetDevice(const nlohmann::json& element);
751
752/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500753 * Parses a JSON element containing a string.
754 *
755 * Returns the corresponding C++ string.
756 *
757 * Throws an exception if parsing fails.
758 *
759 * @param element JSON element
760 * @param isEmptyValid indicates whether an empty string value is valid
761 * @return string value
762 */
763inline std::string parseString(const nlohmann::json& element,
764 bool isEmptyValid = false)
765{
766 if (!element.is_string())
767 {
768 throw std::invalid_argument{"Element is not a string"};
769 }
Bob King6afbf1a2020-04-06 17:19:01 +0800770 std::string value = element.get<std::string>();
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500771 if (value.empty() && !isEmptyValid)
772 {
773 throw std::invalid_argument{"Element contains an empty string"};
774 }
775 return value;
776}
777
778/**
Bob Kingf617f892020-03-30 19:03:35 +0800779 * Parses a JSON element containing an 8-bit unsigned integer.
780 *
781 * Returns the corresponding C++ uint8_t value.
782 *
783 * Throws an exception if parsing fails.
784 *
785 * @param element JSON element
786 * @return uint8_t value
787 */
788inline uint8_t parseUint8(const nlohmann::json& element)
789{
790 // Verify element contains an integer
791 if (!element.is_number_integer())
792 {
793 throw std::invalid_argument{"Element is not an integer"};
794 }
Bob King6afbf1a2020-04-06 17:19:01 +0800795 int value = element.get<int>();
Bob Kingf617f892020-03-30 19:03:35 +0800796 if ((value < 0) || (value > UINT8_MAX))
797 {
798 throw std::invalid_argument{"Element is not an 8-bit unsigned integer"};
799 }
800 return static_cast<uint8_t>(value);
801}
802
803/**
Bob King0e701132020-04-03 21:50:31 +0800804 * Parses a JSON element containing an unsigned integer.
805 *
806 * Returns the corresponding C++ unsigned int value.
807 *
808 * Throws an exception if parsing fails.
809 *
810 * @param element JSON element
811 * @return unsigned int value
812 */
813inline unsigned int parseUnsignedInteger(const nlohmann::json& element)
814{
815 // Verify element contains an unsigned integer
816 if (!element.is_number_unsigned())
817 {
818 throw std::invalid_argument{"Element is not an unsigned integer"};
819 }
820 return element.get<unsigned int>();
821}
822
823/**
Bob King84614882020-04-30 13:13:48 +0800824 * Parses a JSON element containing a VoutDataFormat expressed as a string.
825 *
826 * Returns the corresponding VoutDataFormat enum value.
827 *
828 * Throws an exception if parsing fails.
829 *
830 * @param element JSON element
831 * @return VoutDataFormat enum value
832 */
833pmbus_utils::VoutDataFormat parseVoutDataFormat(const nlohmann::json& element);
834
835/**
Shawn McCarney0e8c68a2020-03-27 01:44:48 -0500836 * Verifies that the specified JSON element is a JSON array.
837 *
838 * Throws an invalid_argument exception if the element is not an array.
839 *
840 * @param element JSON element
841 */
842inline void verifyIsArray(const nlohmann::json& element)
843{
844 if (!element.is_array())
845 {
846 throw std::invalid_argument{"Element is not an array"};
847 }
848}
849
850/**
851 * Verifies that the specified JSON element is a JSON object.
852 *
853 * Throws an invalid_argument exception if the element is not an object.
854 *
855 * @param element JSON element
856 */
857inline void verifyIsObject(const nlohmann::json& element)
858{
859 if (!element.is_object())
860 {
861 throw std::invalid_argument{"Element is not an object"};
862 }
863}
864
865/**
866 * Verifies that the specified JSON element contains the expected number of
867 * properties.
868 *
869 * Throws an invalid_argument exception if the element contains a different
870 * number of properties. This indicates the element contains an invalid
871 * property.
872 *
873 * @param element JSON element
874 * @param expectedCount expected number of properties in element
875 */
876inline void verifyPropertyCount(const nlohmann::json& element,
877 unsigned int expectedCount)
878{
879 if (element.size() != expectedCount)
880 {
881 throw std::invalid_argument{"Element contains an invalid property"};
882 }
883}
884
885} // namespace internal
886
887} // namespace phosphor::power::regulators::config_file_parser