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