Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 1 | /** |
| 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 King | 3a78754 | 2020-04-14 13:45:01 +0800 | [diff] [blame] | 19 | #include "and_action.hpp" |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 20 | #include "chassis.hpp" |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 21 | #include "configuration.hpp" |
Bob King | 0e70113 | 2020-04-03 21:50:31 +0800 | [diff] [blame] | 22 | #include "device.hpp" |
Bob King | f09bfe0 | 2020-04-13 17:21:15 +0800 | [diff] [blame] | 23 | #include "i2c_compare_bit_action.hpp" |
| 24 | #include "i2c_compare_byte_action.hpp" |
| 25 | #include "i2c_compare_bytes_action.hpp" |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 26 | #include "i2c_interface.hpp" |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 27 | #include "i2c_write_bit_action.hpp" |
Bob King | 87ff9d7 | 2020-03-31 14:02:55 +0800 | [diff] [blame] | 28 | #include "i2c_write_byte_action.hpp" |
Bob King | bafcb86 | 2020-03-31 16:39:00 +0800 | [diff] [blame] | 29 | #include "i2c_write_bytes_action.hpp" |
Bob King | f1b58dc | 2020-04-14 14:53:10 +0800 | [diff] [blame^] | 30 | #include "not_action.hpp" |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 31 | #include "pmbus_write_vout_command_action.hpp" |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 32 | #include "presence_detection.hpp" |
| 33 | #include "rail.hpp" |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 34 | #include "rule.hpp" |
Bob King | 315b0b6 | 2020-04-03 21:47:58 +0800 | [diff] [blame] | 35 | #include "run_rule_action.hpp" |
Bob King | a2f2a0d | 2020-04-09 13:32:14 +0800 | [diff] [blame] | 36 | #include "sensor_monitoring.hpp" |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 37 | |
| 38 | #include <nlohmann/json.hpp> |
| 39 | |
| 40 | #include <cstdint> |
| 41 | #include <filesystem> |
| 42 | #include <memory> |
| 43 | #include <stdexcept> |
| 44 | #include <string> |
| 45 | #include <tuple> |
| 46 | #include <vector> |
| 47 | |
| 48 | namespace phosphor::power::regulators::config_file_parser |
| 49 | { |
| 50 | |
| 51 | /** |
| 52 | * Parses the specified JSON configuration file. |
| 53 | * |
| 54 | * Returns the corresponding C++ Rule and Chassis objects. |
| 55 | * |
| 56 | * Throws a ConfigFileParserError if an error occurs. |
| 57 | * |
| 58 | * @param pathName configuration file path name |
| 59 | * @return tuple containing vectors of Rule and Chassis objects |
| 60 | */ |
| 61 | std::tuple<std::vector<std::unique_ptr<Rule>>, |
| 62 | std::vector<std::unique_ptr<Chassis>>> |
| 63 | parse(const std::filesystem::path& pathName); |
| 64 | |
| 65 | /* |
| 66 | * Internal implementation details for parse() |
| 67 | */ |
| 68 | namespace internal |
| 69 | { |
| 70 | |
| 71 | /** |
| 72 | * Returns the specified property of the specified JSON element. |
| 73 | * |
| 74 | * Throws an invalid_argument exception if the property does not exist. |
| 75 | * |
| 76 | * @param element JSON element |
| 77 | * @param property property name |
| 78 | */ |
| 79 | inline const nlohmann::json& getRequiredProperty(const nlohmann::json& element, |
| 80 | const std::string& property) |
| 81 | { |
| 82 | auto it = element.find(property); |
| 83 | if (it == element.end()) |
| 84 | { |
| 85 | throw std::invalid_argument{"Required property missing: " + property}; |
| 86 | } |
| 87 | return *it; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Parses a JSON element containing an action. |
| 92 | * |
| 93 | * Returns the corresponding C++ Action object. |
| 94 | * |
| 95 | * Throws an exception if parsing fails. |
| 96 | * |
| 97 | * @param element JSON element |
| 98 | * @return Action object |
| 99 | */ |
| 100 | std::unique_ptr<Action> parseAction(const nlohmann::json& element); |
| 101 | |
| 102 | /** |
| 103 | * Parses a JSON element containing an array of actions. |
| 104 | * |
| 105 | * Returns the corresponding C++ Action objects. |
| 106 | * |
| 107 | * Throws an exception if parsing fails. |
| 108 | * |
| 109 | * @param element JSON element |
| 110 | * @return vector of Action objects |
| 111 | */ |
| 112 | std::vector<std::unique_ptr<Action>> |
| 113 | parseActionArray(const nlohmann::json& element); |
| 114 | |
| 115 | /** |
Bob King | 3a78754 | 2020-04-14 13:45:01 +0800 | [diff] [blame] | 116 | * Parses a JSON element containing an and action. |
| 117 | * |
| 118 | * Returns the corresponding C++ AndAction object. |
| 119 | * |
| 120 | * Throws an exception if parsing fails. |
| 121 | * |
| 122 | * @param element JSON element |
| 123 | * @return AndAction object |
| 124 | */ |
| 125 | std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element); |
| 126 | |
| 127 | /** |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 128 | * Parses a JSON element containing a bit position (from 0-7). |
| 129 | * |
| 130 | * Returns the corresponding C++ uint8_t value. |
| 131 | * |
| 132 | * Throws an exception if parsing fails. |
| 133 | * |
| 134 | * @param element JSON element |
| 135 | * @return uint8_t value |
| 136 | */ |
| 137 | inline uint8_t parseBitPosition(const nlohmann::json& element) |
| 138 | { |
| 139 | // Verify element contains an integer |
| 140 | if (!element.is_number_integer()) |
| 141 | { |
| 142 | throw std::invalid_argument{"Element is not an integer"}; |
| 143 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 144 | int value = element.get<int>(); |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 145 | if ((value < 0) || (value > 7)) |
| 146 | { |
| 147 | throw std::invalid_argument{"Element is not a bit position"}; |
| 148 | } |
| 149 | return static_cast<uint8_t>(value); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Parses a JSON element containing a bit value (0 or 1). |
| 154 | * |
| 155 | * Returns the corresponding C++ uint8_t value. |
| 156 | * |
| 157 | * Throws an exception if parsing fails. |
| 158 | * |
| 159 | * @param element JSON element |
| 160 | * @return uint8_t value |
| 161 | */ |
| 162 | inline uint8_t parseBitValue(const nlohmann::json& element) |
| 163 | { |
| 164 | // Verify element contains an integer |
| 165 | if (!element.is_number_integer()) |
| 166 | { |
| 167 | throw std::invalid_argument{"Element is not an integer"}; |
| 168 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 169 | int value = element.get<int>(); |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 170 | if ((value < 0) || (value > 1)) |
| 171 | { |
| 172 | throw std::invalid_argument{"Element is not a bit value"}; |
| 173 | } |
| 174 | return static_cast<uint8_t>(value); |
| 175 | } |
| 176 | |
| 177 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 178 | * Parses a JSON element containing a boolean. |
| 179 | * |
| 180 | * Returns the corresponding C++ boolean value. |
| 181 | * |
| 182 | * Throws an exception if parsing fails. |
| 183 | * |
| 184 | * @param element JSON element |
| 185 | * @return boolean value |
| 186 | */ |
| 187 | inline bool parseBoolean(const nlohmann::json& element) |
| 188 | { |
| 189 | // Verify element contains a boolean |
| 190 | if (!element.is_boolean()) |
| 191 | { |
| 192 | throw std::invalid_argument{"Element is not a boolean"}; |
| 193 | } |
| 194 | return element.get<bool>(); |
| 195 | } |
| 196 | |
| 197 | /** |
Bob King | 0e70113 | 2020-04-03 21:50:31 +0800 | [diff] [blame] | 198 | * Parses a JSON element containing a chassis. |
| 199 | * |
| 200 | * Returns the corresponding C++ Chassis object. |
| 201 | * |
| 202 | * Throws an exception if parsing fails. |
| 203 | * |
| 204 | * @param element JSON element |
| 205 | * @return Chassis object |
| 206 | */ |
| 207 | std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element); |
| 208 | |
| 209 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 210 | * Parses a JSON element containing an array of chassis. |
| 211 | * |
| 212 | * Returns the corresponding C++ Chassis objects. |
| 213 | * |
| 214 | * Throws an exception if parsing fails. |
| 215 | * |
| 216 | * @param element JSON element |
| 217 | * @return vector of Chassis objects |
| 218 | */ |
| 219 | std::vector<std::unique_ptr<Chassis>> |
| 220 | parseChassisArray(const nlohmann::json& element); |
| 221 | |
| 222 | /** |
Bob King | 33e7eaa | 2020-04-01 18:09:34 +0800 | [diff] [blame] | 223 | * Parses a JSON element containing a configuration. |
| 224 | * |
| 225 | * Returns the corresponding C++ Configuration object. |
| 226 | * |
| 227 | * Throws an exception if parsing fails. |
| 228 | * |
| 229 | * @param element JSON element |
| 230 | * @return Configuration object |
| 231 | */ |
| 232 | std::unique_ptr<Configuration> |
| 233 | parseConfiguration(const nlohmann::json& element); |
| 234 | |
| 235 | /** |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 236 | * Parses a JSON element containing a device. |
| 237 | * |
| 238 | * Returns the corresponding C++ Device object. |
| 239 | * |
| 240 | * Throws an exception if parsing fails. |
| 241 | * |
| 242 | * @param element JSON element |
| 243 | * @return Device object |
| 244 | */ |
| 245 | std::unique_ptr<Device> parseDevice(const nlohmann::json& element); |
| 246 | |
| 247 | /** |
Bob King | 0e70113 | 2020-04-03 21:50:31 +0800 | [diff] [blame] | 248 | * Parses a JSON element containing an array of devices. |
| 249 | * |
| 250 | * Returns the corresponding C++ Device objects. |
| 251 | * |
| 252 | * Throws an exception if parsing fails. |
| 253 | * |
| 254 | * @param element JSON element |
| 255 | * @return vector of Device objects |
| 256 | */ |
| 257 | std::vector<std::unique_ptr<Device>> |
| 258 | parseDeviceArray(const nlohmann::json& element); |
| 259 | |
| 260 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 261 | * Parses a JSON element containing a double (floating point number). |
| 262 | * |
| 263 | * Returns the corresponding C++ double value. |
| 264 | * |
| 265 | * Throws an exception if parsing fails. |
| 266 | * |
| 267 | * @param element JSON element |
| 268 | * @return double value |
| 269 | */ |
| 270 | inline double parseDouble(const nlohmann::json& element) |
| 271 | { |
| 272 | // Verify element contains a number (integer or floating point) |
| 273 | if (!element.is_number()) |
| 274 | { |
| 275 | throw std::invalid_argument{"Element is not a number"}; |
| 276 | } |
| 277 | return element.get<double>(); |
| 278 | } |
| 279 | |
| 280 | /** |
Bob King | bafcb86 | 2020-03-31 16:39:00 +0800 | [diff] [blame] | 281 | * Parses a JSON element containing a byte value expressed as a hexadecimal |
| 282 | * string. |
| 283 | * |
| 284 | * The JSON number data type does not support the hexadecimal format. For this |
| 285 | * reason, hexadecimal byte values are stored as strings in the configuration |
| 286 | * file. |
| 287 | * |
| 288 | * Returns the corresponding C++ uint8_t value. |
| 289 | * |
| 290 | * Throws an exception if parsing fails. |
| 291 | * |
| 292 | * @param element JSON element |
| 293 | * @return uint8_t value |
| 294 | */ |
| 295 | inline uint8_t parseHexByte(const nlohmann::json& element) |
| 296 | { |
| 297 | if (!element.is_string()) |
| 298 | { |
| 299 | throw std::invalid_argument{"Element is not a string"}; |
| 300 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 301 | std::string value = element.get<std::string>(); |
Bob King | bafcb86 | 2020-03-31 16:39:00 +0800 | [diff] [blame] | 302 | |
| 303 | bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) && |
| 304 | (value.size() < 5) && |
| 305 | (value.find_first_not_of("0123456789abcdefABCDEF", 2) == |
| 306 | std::string::npos); |
| 307 | if (!isHex) |
| 308 | { |
| 309 | throw std::invalid_argument{"Element is not hexadecimal string"}; |
| 310 | } |
| 311 | return static_cast<uint8_t>(std::stoul(value, 0, 0)); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Parses a JSON element containing an array of byte values expressed as a |
| 316 | * hexadecimal strings. |
| 317 | * |
| 318 | * Returns the corresponding C++ uint8_t values. |
| 319 | * |
| 320 | * Throws an exception if parsing fails. |
| 321 | * |
| 322 | * @param element JSON element |
| 323 | * @return vector of uint8_t |
| 324 | */ |
| 325 | std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element); |
| 326 | |
| 327 | /** |
Bob King | f09bfe0 | 2020-04-13 17:21:15 +0800 | [diff] [blame] | 328 | * Parses a JSON element containing an i2c_compare_bit action. |
| 329 | * |
| 330 | * Returns the corresponding C++ I2CCompareBitAction object. |
| 331 | * |
| 332 | * Throws an exception if parsing fails. |
| 333 | * |
| 334 | * @param element JSON element |
| 335 | * @return I2CCompareBitAction object |
| 336 | */ |
| 337 | std::unique_ptr<I2CCompareBitAction> |
| 338 | parseI2CCompareBit(const nlohmann::json& element); |
| 339 | |
| 340 | /** |
| 341 | * Parses a JSON element containing an i2c_compare_byte action. |
| 342 | * |
| 343 | * Returns the corresponding C++ I2CCompareByteAction object. |
| 344 | * |
| 345 | * Throws an exception if parsing fails. |
| 346 | * |
| 347 | * @param element JSON element |
| 348 | * @return I2CCompareByteAction object |
| 349 | */ |
| 350 | std::unique_ptr<I2CCompareByteAction> |
| 351 | parseI2CCompareByte(const nlohmann::json& element); |
| 352 | |
| 353 | /** |
| 354 | * Parses a JSON element containing an i2c_compare_bytes action. |
| 355 | * |
| 356 | * Returns the corresponding C++ I2CCompareBytesAction object. |
| 357 | * |
| 358 | * Throws an exception if parsing fails. |
| 359 | * |
| 360 | * @param element JSON element |
| 361 | * @return I2CCompareBytesAction object |
| 362 | */ |
| 363 | std::unique_ptr<I2CCompareBytesAction> |
| 364 | parseI2CCompareBytes(const nlohmann::json& element); |
| 365 | |
| 366 | /** |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 367 | * Parses a JSON element containing an i2c_interface. |
| 368 | * |
| 369 | * Returns the corresponding C++ i2c::I2CInterface object. |
| 370 | * |
| 371 | * Throws an exception if parsing fails. |
| 372 | * |
| 373 | * @param element JSON element |
| 374 | * @return i2c::I2CInterface object |
| 375 | */ |
| 376 | std::unique_ptr<i2c::I2CInterface> |
| 377 | parseI2CInterface(const nlohmann::json& element); |
| 378 | |
| 379 | /** |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 380 | * Parses a JSON element containing an i2c_write_bit action. |
| 381 | * |
| 382 | * Returns the corresponding C++ I2CWriteBitAction object. |
| 383 | * |
| 384 | * Throws an exception if parsing fails. |
| 385 | * |
| 386 | * @param element JSON element |
| 387 | * @return I2CWriteBitAction object |
| 388 | */ |
| 389 | std::unique_ptr<I2CWriteBitAction> |
| 390 | parseI2CWriteBit(const nlohmann::json& element); |
| 391 | |
| 392 | /** |
Bob King | 87ff9d7 | 2020-03-31 14:02:55 +0800 | [diff] [blame] | 393 | * Parses a JSON element containing an i2c_write_byte action. |
| 394 | * |
| 395 | * Returns the corresponding C++ I2CWriteByteAction object. |
| 396 | * |
| 397 | * Throws an exception if parsing fails. |
| 398 | * |
| 399 | * @param element JSON element |
| 400 | * @return I2CWriteByteAction object |
| 401 | */ |
| 402 | std::unique_ptr<I2CWriteByteAction> |
| 403 | parseI2CWriteByte(const nlohmann::json& element); |
| 404 | |
| 405 | /** |
Bob King | bafcb86 | 2020-03-31 16:39:00 +0800 | [diff] [blame] | 406 | * Parses a JSON element containing an i2c_write_bytes action. |
| 407 | * |
| 408 | * Returns the corresponding C++ I2CWriteBytesAction object. |
| 409 | * |
| 410 | * Throws an exception if parsing fails. |
| 411 | * |
| 412 | * @param element JSON element |
| 413 | * @return I2CWriteBytesAction object |
| 414 | */ |
| 415 | std::unique_ptr<I2CWriteBytesAction> |
| 416 | parseI2CWriteBytes(const nlohmann::json& element); |
| 417 | |
| 418 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 419 | * Parses a JSON element containing an 8-bit signed integer. |
| 420 | * |
| 421 | * Returns the corresponding C++ int8_t value. |
| 422 | * |
| 423 | * Throws an exception if parsing fails. |
| 424 | * |
| 425 | * @param element JSON element |
| 426 | * @return int8_t value |
| 427 | */ |
| 428 | inline int8_t parseInt8(const nlohmann::json& element) |
| 429 | { |
| 430 | // Verify element contains an integer |
| 431 | if (!element.is_number_integer()) |
| 432 | { |
| 433 | throw std::invalid_argument{"Element is not an integer"}; |
| 434 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 435 | int value = element.get<int>(); |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 436 | if ((value < INT8_MIN) || (value > INT8_MAX)) |
| 437 | { |
| 438 | throw std::invalid_argument{"Element is not an 8-bit signed integer"}; |
| 439 | } |
| 440 | return static_cast<int8_t>(value); |
| 441 | } |
| 442 | |
| 443 | /** |
Bob King | f1b58dc | 2020-04-14 14:53:10 +0800 | [diff] [blame^] | 444 | * Parses a JSON element containing a not action. |
| 445 | * |
| 446 | * Returns the corresponding C++ NotAction object. |
| 447 | * |
| 448 | * Throws an exception if parsing fails. |
| 449 | * |
| 450 | * @param element JSON element |
| 451 | * @return NotAction object |
| 452 | */ |
| 453 | std::unique_ptr<NotAction> parseNot(const nlohmann::json& element); |
| 454 | |
| 455 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 456 | * Parses a JSON element containing a pmbus_write_vout_command action. |
| 457 | * |
| 458 | * Returns the corresponding C++ PMBusWriteVoutCommandAction object. |
| 459 | * |
| 460 | * Throws an exception if parsing fails. |
| 461 | * |
| 462 | * @param element JSON element |
| 463 | * @return PMBusWriteVoutCommandAction object |
| 464 | */ |
| 465 | std::unique_ptr<PMBusWriteVoutCommandAction> |
| 466 | parsePMBusWriteVoutCommand(const nlohmann::json& element); |
| 467 | |
| 468 | /** |
Bob King | a2f2a0d | 2020-04-09 13:32:14 +0800 | [diff] [blame] | 469 | * Parses a JSON element containing a rail. |
| 470 | * |
| 471 | * Returns the corresponding C++ Rail object. |
| 472 | * |
| 473 | * Throws an exception if parsing fails. |
| 474 | * |
| 475 | * @param element JSON element |
| 476 | * @return Rail object |
| 477 | */ |
| 478 | std::unique_ptr<Rail> parseRail(const nlohmann::json& element); |
| 479 | |
| 480 | /** |
Bob King | 9c36c5f | 2020-04-06 11:34:09 +0800 | [diff] [blame] | 481 | * Parses a JSON element containing an array of rails. |
| 482 | * |
| 483 | * Returns the corresponding C++ Rail objects. |
| 484 | * |
| 485 | * Throws an exception if parsing fails. |
| 486 | * |
| 487 | * @param element JSON element |
| 488 | * @return vector of Rail objects |
| 489 | */ |
| 490 | std::vector<std::unique_ptr<Rail>> |
| 491 | parseRailArray(const nlohmann::json& element); |
| 492 | |
| 493 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 494 | * Parses the JSON root element of the entire configuration file. |
| 495 | * |
| 496 | * Returns the corresponding C++ Rule and Chassis objects. |
| 497 | * |
| 498 | * Throws an exception if parsing fails. |
| 499 | * |
| 500 | * @param element JSON element |
| 501 | * @return tuple containing vectors of Rule and Chassis objects |
| 502 | */ |
| 503 | std::tuple<std::vector<std::unique_ptr<Rule>>, |
| 504 | std::vector<std::unique_ptr<Chassis>>> |
| 505 | parseRoot(const nlohmann::json& element); |
| 506 | |
| 507 | /** |
| 508 | * Parses a JSON element containing a rule. |
| 509 | * |
| 510 | * Returns the corresponding C++ Rule object. |
| 511 | * |
| 512 | * Throws an exception if parsing fails. |
| 513 | * |
| 514 | * @param element JSON element |
| 515 | * @return Rule object |
| 516 | */ |
| 517 | std::unique_ptr<Rule> parseRule(const nlohmann::json& element); |
| 518 | |
| 519 | /** |
| 520 | * Parses a JSON element containing an array of rules. |
| 521 | * |
| 522 | * Returns the corresponding C++ Rule objects. |
| 523 | * |
| 524 | * Throws an exception if parsing fails. |
| 525 | * |
| 526 | * @param element JSON element |
| 527 | * @return vector of Rule objects |
| 528 | */ |
| 529 | std::vector<std::unique_ptr<Rule>> |
| 530 | parseRuleArray(const nlohmann::json& element); |
| 531 | |
| 532 | /** |
Bob King | 33e7eaa | 2020-04-01 18:09:34 +0800 | [diff] [blame] | 533 | * Parses the "rule_id" or "actions" property in a JSON element. |
| 534 | * |
| 535 | * The element must contain one property or the other but not both. |
| 536 | * |
| 537 | * If the element contains a "rule_id" property, the corresponding C++ |
| 538 | * RunRuleAction object is returned. |
| 539 | * |
| 540 | * If the element contains an "actions" property, the corresponding C++ Action |
| 541 | * objects are returned. |
| 542 | * |
| 543 | * Throws an exception if parsing fails. |
| 544 | * |
| 545 | * @param element JSON element |
| 546 | * @return vector of Action objects |
| 547 | */ |
| 548 | std::vector<std::unique_ptr<Action>> |
| 549 | parseRuleIDOrActionsProperty(const nlohmann::json& element); |
| 550 | |
| 551 | /** |
Bob King | 315b0b6 | 2020-04-03 21:47:58 +0800 | [diff] [blame] | 552 | * Parses a JSON element containing a run_rule action. |
| 553 | * |
| 554 | * Returns the corresponding C++ RunRuleAction object. |
| 555 | * |
| 556 | * Throws an exception if parsing fails. |
| 557 | * |
| 558 | * @param element JSON element |
| 559 | * @return RunRuleAction object |
| 560 | */ |
| 561 | std::unique_ptr<RunRuleAction> parseRunRule(const nlohmann::json& element); |
| 562 | |
| 563 | /** |
Bob King | a2f2a0d | 2020-04-09 13:32:14 +0800 | [diff] [blame] | 564 | * Parses a JSON element containing a sensor monitoring operation. |
| 565 | * |
| 566 | * Returns the corresponding C++ SensorMonitoring object. |
| 567 | * |
| 568 | * Throws an exception if parsing fails. |
| 569 | * |
| 570 | * @param element JSON element |
| 571 | * @return SensorMonitoring object |
| 572 | */ |
| 573 | std::unique_ptr<SensorMonitoring> |
| 574 | parseSensorMonitoring(const nlohmann::json& element); |
| 575 | |
| 576 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 577 | * Parses a JSON element containing a string. |
| 578 | * |
| 579 | * Returns the corresponding C++ string. |
| 580 | * |
| 581 | * Throws an exception if parsing fails. |
| 582 | * |
| 583 | * @param element JSON element |
| 584 | * @param isEmptyValid indicates whether an empty string value is valid |
| 585 | * @return string value |
| 586 | */ |
| 587 | inline std::string parseString(const nlohmann::json& element, |
| 588 | bool isEmptyValid = false) |
| 589 | { |
| 590 | if (!element.is_string()) |
| 591 | { |
| 592 | throw std::invalid_argument{"Element is not a string"}; |
| 593 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 594 | std::string value = element.get<std::string>(); |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 595 | if (value.empty() && !isEmptyValid) |
| 596 | { |
| 597 | throw std::invalid_argument{"Element contains an empty string"}; |
| 598 | } |
| 599 | return value; |
| 600 | } |
| 601 | |
| 602 | /** |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 603 | * Parses a JSON element containing an 8-bit unsigned integer. |
| 604 | * |
| 605 | * Returns the corresponding C++ uint8_t value. |
| 606 | * |
| 607 | * Throws an exception if parsing fails. |
| 608 | * |
| 609 | * @param element JSON element |
| 610 | * @return uint8_t value |
| 611 | */ |
| 612 | inline uint8_t parseUint8(const nlohmann::json& element) |
| 613 | { |
| 614 | // Verify element contains an integer |
| 615 | if (!element.is_number_integer()) |
| 616 | { |
| 617 | throw std::invalid_argument{"Element is not an integer"}; |
| 618 | } |
Bob King | 6afbf1a | 2020-04-06 17:19:01 +0800 | [diff] [blame] | 619 | int value = element.get<int>(); |
Bob King | f617f89 | 2020-03-30 19:03:35 +0800 | [diff] [blame] | 620 | if ((value < 0) || (value > UINT8_MAX)) |
| 621 | { |
| 622 | throw std::invalid_argument{"Element is not an 8-bit unsigned integer"}; |
| 623 | } |
| 624 | return static_cast<uint8_t>(value); |
| 625 | } |
| 626 | |
| 627 | /** |
Bob King | 0e70113 | 2020-04-03 21:50:31 +0800 | [diff] [blame] | 628 | * Parses a JSON element containing an unsigned integer. |
| 629 | * |
| 630 | * Returns the corresponding C++ unsigned int value. |
| 631 | * |
| 632 | * Throws an exception if parsing fails. |
| 633 | * |
| 634 | * @param element JSON element |
| 635 | * @return unsigned int value |
| 636 | */ |
| 637 | inline unsigned int parseUnsignedInteger(const nlohmann::json& element) |
| 638 | { |
| 639 | // Verify element contains an unsigned integer |
| 640 | if (!element.is_number_unsigned()) |
| 641 | { |
| 642 | throw std::invalid_argument{"Element is not an unsigned integer"}; |
| 643 | } |
| 644 | return element.get<unsigned int>(); |
| 645 | } |
| 646 | |
| 647 | /** |
Shawn McCarney | 0e8c68a | 2020-03-27 01:44:48 -0500 | [diff] [blame] | 648 | * Verifies that the specified JSON element is a JSON array. |
| 649 | * |
| 650 | * Throws an invalid_argument exception if the element is not an array. |
| 651 | * |
| 652 | * @param element JSON element |
| 653 | */ |
| 654 | inline void verifyIsArray(const nlohmann::json& element) |
| 655 | { |
| 656 | if (!element.is_array()) |
| 657 | { |
| 658 | throw std::invalid_argument{"Element is not an array"}; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Verifies that the specified JSON element is a JSON object. |
| 664 | * |
| 665 | * Throws an invalid_argument exception if the element is not an object. |
| 666 | * |
| 667 | * @param element JSON element |
| 668 | */ |
| 669 | inline void verifyIsObject(const nlohmann::json& element) |
| 670 | { |
| 671 | if (!element.is_object()) |
| 672 | { |
| 673 | throw std::invalid_argument{"Element is not an object"}; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Verifies that the specified JSON element contains the expected number of |
| 679 | * properties. |
| 680 | * |
| 681 | * Throws an invalid_argument exception if the element contains a different |
| 682 | * number of properties. This indicates the element contains an invalid |
| 683 | * property. |
| 684 | * |
| 685 | * @param element JSON element |
| 686 | * @param expectedCount expected number of properties in element |
| 687 | */ |
| 688 | inline void verifyPropertyCount(const nlohmann::json& element, |
| 689 | unsigned int expectedCount) |
| 690 | { |
| 691 | if (element.size() != expectedCount) |
| 692 | { |
| 693 | throw std::invalid_argument{"Element contains an invalid property"}; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | } // namespace internal |
| 698 | |
| 699 | } // namespace phosphor::power::regulators::config_file_parser |