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