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/meson.build b/src/meson.build
index d183f6d..65ecd02 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -8,6 +8,7 @@
sources = [
'asset_info.cpp',
+ 'utils.cpp',
]
deps = [
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
diff --git a/src/utils.hpp b/src/utils.hpp
new file mode 100644
index 0000000..231c1c9
--- /dev/null
+++ b/src/utils.hpp
@@ -0,0 +1,104 @@
+#include <ipmid/api-types.hpp>
+#include <ipmid/utils.hpp>
+#include <phosphor-logging/lg2.hpp>
+
+namespace ipmi
+{
+namespace iei
+{
+
+Cc getAllDbusObjects(ipmi::Context::ptr ctx, const std::string& serviceRoot,
+ const std::string& interface, ObjectTree& objectTree);
+
+Cc getService(ipmi::Context::ptr ctx, const std::string& interface,
+ const std::string& path, std::string& service);
+
+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);
+
+Cc setDbusProperty(ipmi::Context::ptr ctx, const std::string& objPath,
+ const std::string interface, const std::string property,
+ const Value& value);
+
+Cc getAllDbusProperties(Context::ptr ctx, const std::string& service,
+ const std::string& objPath,
+ const std::string& interface, PropertyMap& properties);
+
+template <typename Type>
+Cc getDbusProperty(Context::ptr ctx, const std::string& service,
+ const std::string& objPath, const std::string& interface,
+ const std::string& property, Type& propertyValue)
+{
+ auto ec = ipmi::getDbusProperty(ctx, service, objPath, interface, property,
+ propertyValue);
+ if (ec)
+ {
+ lg2::error(
+ "getDbusProperty for (service:{SERVICE}, path:{PATH}, interface:{INTERFACE}, property:{PROPERTY}) failed with {ERRMSG}",
+ "SERVICE", service, "PATH", objPath, "INTERFACE", interface,
+ "PROPERTY", property, "ERRMSG", ec.message());
+ return ccUnspecifiedError;
+ }
+ return ccSuccess;
+}
+
+template <typename Type>
+Cc getDbusProperty(Context::ptr ctx, const std::string& objPath,
+ const std::string& interface, const std::string& property,
+ Type& propertyValue)
+{
+ std::string service;
+ auto cc = iei::getService(ctx, interface, objPath, service);
+ if (cc != ccSuccess)
+ {
+ return cc;
+ }
+
+ return iei::getDbusProperty(ctx, service, objPath, interface, property,
+ propertyValue);
+}
+
+template <typename... InputArgs>
+void callDbusMethod(ipmi::Context::ptr ctx, Cc& cc, const std::string& service,
+ const std::string& objPath, const std::string& interface,
+ const std::string& method, const InputArgs&... a)
+{
+ boost::system::error_code ec;
+ cc = ccSuccess;
+ ctx->bus->yield_method_call(ctx->yield, ec, service, objPath, interface,
+ method, a...);
+ if (ec)
+ {
+ lg2::error(
+ "callDbusMethod for (service:{SERVICE}, path:{PATH}, interface:{INTERFACE}, method:{METHOD}) failed with {ERRMSG}",
+ "SERVICE", service, "PATH", objPath, "INTERFACE", interface,
+ "METHOD", method, "ERRMSG", ec.message());
+ cc = ccUnspecifiedError;
+ }
+ return;
+}
+
+template <typename RetType, typename... InputArgs>
+RetType callDbusMethod(ipmi::Context::ptr ctx, Cc& cc,
+ const std::string& service, const std::string& objPath,
+ const std::string& interface, const std::string& method,
+ const InputArgs&... a)
+{
+ boost::system::error_code ec;
+ cc = ccSuccess;
+ auto rc = ctx->bus->yield_method_call<RetType>(
+ ctx->yield, ec, service, objPath, interface, method, a...);
+ if (ec)
+ {
+ lg2::error(
+ "callDbusMethod for (service:{SERVICE}, path:{PATH}, interface:{INTERFACE}, method:{METHOD}) failed with {ERRMSG}",
+ "SERVICE", service, "PATH", objPath, "INTERFACE", interface,
+ "METHOD", method, "ERRMSG", ec.message());
+ cc = ccUnspecifiedError;
+ }
+ return rc;
+}
+
+} // namespace iei
+} // namespace ipmi