regulators: Implements support for device
Enhance the configuration file parser to support the device and
i2c_interface elements.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I8cf55d4fc793357113cdfcb6c543556b5eb09a8d
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 99a2d15..3d56629 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -225,15 +225,91 @@
return chassis;
}
+std::unique_ptr<Device> parseDevice(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Optional comments property; value not stored
+ if (element.contains("comments"))
+ {
+ ++propertyCount;
+ }
+
+ // Required id property
+ const json& idElement = getRequiredProperty(element, "id");
+ std::string id = parseString(idElement);
+ ++propertyCount;
+
+ // Required is_regulator property
+ const json& isRegulatorElement =
+ getRequiredProperty(element, "is_regulator");
+ bool isRegulator = parseBoolean(isRegulatorElement);
+ ++propertyCount;
+
+ // Required fru property
+ const json& fruElement = getRequiredProperty(element, "fru");
+ std::string fru = parseString(fruElement);
+ ++propertyCount;
+
+ // Required i2c_interface property
+ const json& i2cInterfaceElement =
+ getRequiredProperty(element, "i2c_interface");
+ std::unique_ptr<i2c::I2CInterface> i2cInterface =
+ parseI2CInterface(i2cInterfaceElement);
+ ++propertyCount;
+
+ // Optional presence_detection property
+ // TODO: Not implemented yet
+ std::unique_ptr<PresenceDetection> presenceDetection{};
+ // auto presenceDetectionIt = element.find("presence_detection");
+ // if (presenceDetectionIt != element.end())
+ // {
+ // presenceDetection = parsePresenceDetection(*presenceDetectionIt);
+ // ++propertyCount;
+ // }
+
+ // Optional configuration property
+ // TODO: Not implemented yet
+ std::unique_ptr<Configuration> configuration{};
+ // auto configurationIt = element.find("configuration");
+ // if (configurationIt != element.end())
+ // {
+ // configuration = parseConfiguration(*configurationIt);
+ // ++propertyCount;
+ // }
+
+ // Optional rails property
+ std::vector<std::unique_ptr<Rail>> rails{};
+ auto railsIt = element.find("rails");
+ if (railsIt != element.end())
+ {
+ if (!isRegulator)
+ {
+ throw std::invalid_argument{
+ "Invalid rails property when is_regulator is false"};
+ }
+ rails = parseRailArray(*railsIt);
+ ++propertyCount;
+ }
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ return std::make_unique<Device>(id, isRegulator, fru,
+ std::move(i2cInterface),
+ std::move(presenceDetection),
+ std::move(configuration), std::move(rails));
+}
+
std::vector<std::unique_ptr<Device>> parseDeviceArray(const json& element)
{
verifyIsArray(element);
std::vector<std::unique_ptr<Device>> devices;
- // TODO: Not implemented yet
- // for (auto& deviceElement : element)
- // {
- // devices.emplace_back(parseDevice(deviceElement));
- // }
+ for (auto& deviceElement : element)
+ {
+ devices.emplace_back(parseDevice(deviceElement));
+ }
return devices;
}
@@ -248,6 +324,25 @@
return values;
}
+std::unique_ptr<i2c::I2CInterface> parseI2CInterface(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Required bus property
+ const json& busElement = getRequiredProperty(element, "bus");
+ uint8_t bus = parseUint8(busElement);
+ ++propertyCount;
+
+ // Required address property
+ const json& addressElement = getRequiredProperty(element, "address");
+ uint8_t address = parseHexByte(addressElement);
+ ++propertyCount;
+
+ verifyPropertyCount(element, propertyCount);
+ return i2c::create(bus, address, i2c::I2CInterface::InitialState::CLOSED);
+}
+
std::unique_ptr<I2CWriteBitAction> parseI2CWriteBit(const json& element)
{
verifyIsObject(element);
@@ -394,6 +489,18 @@
exponent, isVerified);
}
+std::vector<std::unique_ptr<Rail>> parseRailArray(const json& element)
+{
+ verifyIsArray(element);
+ std::vector<std::unique_ptr<Rail>> rails;
+ // TODO: Not implemented yet
+ // for (auto& railElement : element)
+ // {
+ // rails.emplace_back(parseRail(railElement));
+ // }
+ return rails;
+}
+
std::tuple<std::vector<std::unique_ptr<Rule>>,
std::vector<std::unique_ptr<Chassis>>>
parseRoot(const json& element)
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 3ebab4b..35178ea 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -17,11 +17,15 @@
#include "action.hpp"
#include "chassis.hpp"
+#include "configuration.hpp"
#include "device.hpp"
+#include "i2c_interface.hpp"
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
#include "i2c_write_bytes_action.hpp"
#include "pmbus_write_vout_command_action.hpp"
+#include "presence_detection.hpp"
+#include "rail.hpp"
#include "rule.hpp"
#include "run_rule_action.hpp"
@@ -198,6 +202,18 @@
parseChassisArray(const nlohmann::json& element);
/**
+ * Parses a JSON element containing a device.
+ *
+ * Returns the corresponding C++ Device object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return Device object
+ */
+std::unique_ptr<Device> parseDevice(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an array of devices.
*
* Returns the corresponding C++ Device objects.
@@ -278,6 +294,19 @@
std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an i2c_interface.
+ *
+ * Returns the corresponding C++ i2c::I2CInterface object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return i2c::I2CInterface object
+ */
+std::unique_ptr<i2c::I2CInterface>
+ parseI2CInterface(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an i2c_write_bit action.
*
* Returns the corresponding C++ I2CWriteBitAction object.
@@ -355,6 +384,19 @@
parsePMBusWriteVoutCommand(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an array of rails.
+ *
+ * Returns the corresponding C++ Rail objects.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return vector of Rail objects
+ */
+std::vector<std::unique_ptr<Rail>>
+ parseRailArray(const nlohmann::json& element);
+
+/**
* Parses the JSON root element of the entire configuration file.
*
* Returns the corresponding C++ Rule and Chassis objects.