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