regulators: Implements support for not action
Enhance the configuration file parser to support the not action element.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ia88eee705a9fae28a816aeb6dff9e0b6f0deba39
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index e458ba7..5f0181e 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -120,9 +120,8 @@
}
else if (element.contains("not"))
{
- // TODO: Not implemented yet
- // action = parseNot(element["not"]);
- // ++propertyCount;
+ action = parseNot(element["not"]);
+ ++propertyCount;
}
else if (element.contains("or"))
{
@@ -576,6 +575,14 @@
return std::make_unique<I2CWriteBytesAction>(reg, values, masks);
}
+std::unique_ptr<NotAction> parseNot(const json& element)
+{
+ // Required action to execute
+ std::unique_ptr<Action> action = parseAction(element);
+
+ return std::make_unique<NotAction>(std::move(action));
+}
+
std::unique_ptr<PMBusWriteVoutCommandAction>
parsePMBusWriteVoutCommand(const json& element)
{
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index d81e403..e2476a2 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -27,6 +27,7 @@
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
#include "i2c_write_bytes_action.hpp"
+#include "not_action.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "presence_detection.hpp"
#include "rail.hpp"
@@ -440,6 +441,18 @@
}
/**
+ * Parses a JSON element containing a not action.
+ *
+ * Returns the corresponding C++ NotAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return NotAction object
+ */
+std::unique_ptr<NotAction> parseNot(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing a pmbus_write_vout_command action.
*
* Returns the corresponding C++ PMBusWriteVoutCommandAction object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index ed3c271..4dfc45e 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -27,6 +27,7 @@
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
#include "i2c_write_bytes_action.hpp"
+#include "not_action.hpp"
#include "pmbus_utils.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "presence_detection.hpp"
@@ -350,7 +351,16 @@
// TODO: Not implemented yet
// Test where works: not action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "not":
+ { "i2c_compare_byte": { "register": "0xA0", "value": "0xFF" } }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: or action type specified
// TODO: Not implemented yet
@@ -2547,6 +2557,30 @@
}
}
+TEST(ConfigFileParserTests, ParseNot)
+{
+ // Test where works
+ {
+ const json element = R"(
+ { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } }
+ )"_json;
+ std::unique_ptr<NotAction> action = parseNot(element);
+ EXPECT_NE(action->getAction().get(), nullptr);
+ }
+
+ // Test where fails: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseNot(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an object");
+ }
+}
+
TEST(ConfigFileParserTests, ParsePMBusWriteVoutCommand)
{
// Test where works: Only required properties specified