regulators: Implement ComparePresenceAction class
The ComparePresenceAction::execute() method reads the presence value
using the Services->getPresenceService()->isPresent() method.
Then it compares the actual value to the expected value.
It returns true if they match and false if they don't.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ie6a17191313b2c80a0ecd7a7d7357448985d0673
diff --git a/phosphor-regulators/src/actions/compare_presence_action.cpp b/phosphor-regulators/src/actions/compare_presence_action.cpp
new file mode 100644
index 0000000..0d3b28d
--- /dev/null
+++ b/phosphor-regulators/src/actions/compare_presence_action.cpp
@@ -0,0 +1,59 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "compare_presence_action.hpp"
+
+#include "action_error.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool ComparePresenceAction::execute(ActionEnvironment& environment)
+{
+ bool isEqual{false};
+ try
+ {
+ // Get actual presence value for FRU.
+ bool isPresent =
+ environment.getServices().getPresenceService().isPresent(fru);
+
+ // Check if actual presence value equals expected presence value.
+ isEqual = (isPresent == value);
+ }
+ catch (const std::exception& e)
+ {
+ // Nest exception within an ActionError so the caller will have both the
+ // low level error information and the action information.
+ std::throw_with_nested(ActionError(*this));
+ }
+ return isEqual;
+}
+
+std::string ComparePresenceAction::toString() const
+{
+ std::ostringstream ss;
+ ss << "compare_presence: { ";
+ ss << "fru: " << fru << ", ";
+ ss << "value: " << std::boolalpha << value << " }";
+
+ return ss.str();
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/compare_presence_action.hpp b/phosphor-regulators/src/actions/compare_presence_action.hpp
index b2c7880..b16c98f 100644
--- a/phosphor-regulators/src/actions/compare_presence_action.hpp
+++ b/phosphor-regulators/src/actions/compare_presence_action.hpp
@@ -18,8 +18,6 @@
#include "action.hpp"
#include "action_environment.hpp"
-#include <ios>
-#include <sstream>
#include <string>
namespace phosphor::power::regulators
@@ -57,16 +55,10 @@
/**
* Executes this action.
*
- * TODO: Not implemented yet
- *
* @param environment Action execution environment.
* @return true
*/
- virtual bool execute(ActionEnvironment& /* environment */) override
- {
- // TODO: Not implemented yet
- return true;
- }
+ virtual bool execute(ActionEnvironment& environment) override;
/**
* Returns the Field-Replaceable Unit (FRU).
@@ -93,17 +85,7 @@
*
* @return description of action
*/
- virtual std::string toString() const override
- {
- std::ostringstream ss;
- ss << "compare_presence: { ";
-
- ss << "fru: " << fru << ", ";
-
- ss << "value: " << std::boolalpha << value << " }";
-
- return ss.str();
- }
+ virtual std::string toString() const override;
private:
/**
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index bfcacb1..8238d93 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -21,6 +21,7 @@
'system.cpp',
'temporary_file.cpp',
+ 'actions/compare_presence_action.cpp',
'actions/if_action.cpp',
'actions/i2c_compare_bit_action.cpp',
'actions/i2c_compare_byte_action.cpp',