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