Add utils.cpp file
In the IPMI handling logic, there are many checks to determine if DBus
communication is successful. If not, it logs an error and returns an
error code. This logic is now placed in utils to avoid code duplication.
Change-Id: Ie14ba31130fa5476ffc1840f076aa139879f4ff6
Signed-off-by: John Wang <wangzhiqiang02@ieisystem.com>
diff --git a/src/utils.cpp b/src/utils.cpp
new file mode 100644
index 0000000..35556b6
--- /dev/null
+++ b/src/utils.cpp
@@ -0,0 +1,100 @@
+#include "utils.hpp"
+
+namespace ipmi
+{
+namespace iei
+{
+
+Cc getAllDbusObjects(ipmi::Context::ptr ctx, const std::string& serviceRoot,
+ const std::string& interface, ObjectTree& objectTree)
+{
+ auto ec = ipmi::getAllDbusObjects(ctx, serviceRoot, interface, objectTree);
+ if (ec)
+ {
+ lg2::error(
+ "getAllDbusObjects for (path:{ROOTPATH}, intf:{INTF}) failed with {ERRMSG}",
+ "ROOTPATH", serviceRoot, "INTF", interface, "ERRMSG", ec.message());
+ return ccUnspecifiedError;
+ }
+
+ return ccSuccess;
+}
+
+Cc getService(ipmi::Context::ptr ctx, const std::string& interface,
+ const std::string& path, std::string& service)
+{
+ auto ec = ipmi::getService(ctx, interface, path, service);
+ if (ec)
+ {
+ lg2::error(
+ "getService for (path:{PATH}, interface:{INTERFACE}) failed with {ERRMSG}",
+ "PATH", path, "INTERFACE", interface, "ERRMSG", ec.message());
+ return ccUnspecifiedError;
+ }
+
+ return ccSuccess;
+}
+
+Cc setDbusProperty(ipmi::Context::ptr ctx, const std::string& service,
+ const std::string& objPath, const std::string interface,
+ const std::string property, const Value& value)
+{
+ auto ec = ipmi::setDbusProperty(ctx, service, objPath, interface, property,
+ value);
+ if (ec)
+ {
+ lg2::error(
+ "setDbusProperty for (service:{SERVICE}, path:{PATH}, interface:{INTERFACE}) failed with {ERRMSG}",
+ "SERVICE", service, "PATH", objPath, "INTERFACE", interface,
+ "ERRMSG", ec.message());
+ if (ec == boost::system::errc::make_error_code(
+ boost::system::errc::invalid_argument))
+ {
+ return ccInvalidFieldRequest;
+ }
+ return ccUnspecifiedError;
+ }
+
+ return ccSuccess;
+}
+
+Cc setDbusProperty(ipmi::Context::ptr ctx, const std::string& objPath,
+ const std::string interface, const std::string property,
+ const Value& value)
+{
+ std::string service;
+ auto cc = iei::getService(ctx, interface, objPath, service);
+ if (cc != ccSuccess)
+ {
+ return cc;
+ }
+
+ return iei::setDbusProperty(ctx, service, objPath, interface, property,
+ value);
+}
+
+Cc getAllDbusProperties(Context::ptr ctx, const std::string& service,
+ const std::string& objPath,
+ const std::string& interface, PropertyMap& properties)
+{
+ auto ec = ipmi::getAllDbusProperties(ctx, service, objPath, interface,
+ properties);
+ if (ec)
+ {
+ lg2::error(
+ "getAllDbusProperties for (service:{SERVICE}, path:{PATH}, interface:{INTERFACE}) failed with {ERRMSG}",
+ "SERVICE", service, "PATH", objPath, "INTERFACE", interface,
+ "ERRMSG", ec.message());
+ if (ec == boost::system::errc::make_error_code(
+ boost::system::errc::invalid_argument))
+ {
+ return ccInvalidFieldRequest;
+ }
+ return ccUnspecifiedError;
+ }
+
+ return ccSuccess;
+}
+
+} // namespace iei
+} // namespace ipmi