utils: remove DBus method
Since all DBus methods have been implemented in ipmid and
iei-ipmi-oem depends on ipmid, this commit removes utils and uses
ipmid's DBus methods.
Change-Id: Ibcf45292065da544632d8fdb1990c2935d819084
Signed-off-by: George Liu <liuxiwei@ieisystem.com>
diff --git a/src/iei_oem.cpp b/src/iei_oem.cpp
index bffab0f..625bbff 100644
--- a/src/iei_oem.cpp
+++ b/src/iei_oem.cpp
@@ -2,12 +2,10 @@
#include "iei_oem.hpp"
-#include "sdbus_wrapper.hpp"
-#include "utils.hpp"
-
-#include <ipmid/api.h>
-
+#include <ipmid/api.hpp>
+#include <ipmid/utils.hpp>
#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/bus.hpp>
#include <optional>
@@ -35,7 +33,6 @@
using namespace iei;
static void registerOEMFunctions() __attribute__((constructor));
-static auto& bus = getBus();
struct ParsedAssetInfo
{
@@ -134,10 +131,19 @@
data.data() + FIRMWARE_BUILDTIME_OFFSET),
FIRMWARE_BUILDTIME_SIZE);
- // Set BIOS version
- auto service = utils::getService(bus, BIOS_OBJPATH, VERSION_IFACE);
- utils::setProperty(bus, service.c_str(), BIOS_OBJPATH, VERSION_IFACE,
- VERSION, version);
+ try
+ {
+ // Set BIOS version
+ std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
+ auto service = getService(*dbus, VERSION_IFACE, BIOS_OBJPATH);
+ setDbusProperty(*dbus, service, BIOS_OBJPATH, VERSION_IFACE,
+ VERSION, version);
+ }
+ catch (const std::exception& e)
+ {
+ lg2::error("Failed to set BIOS version: {VERSION}, error: {ERROR}",
+ "VERSION", version, "ERROR", e);
+ }
}
printf("Dev %s, version %s, build time %s\n",
diff --git a/src/meson.build b/src/meson.build
index 8b38f31..d8c3c0b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -9,8 +9,6 @@
shared_library(
'iei-ipmi-oem',
'iei_oem.cpp',
- 'utils.cpp',
- 'sdbus_wrapper.cpp',
dependencies: [
phosphor_dbus_interfaces,
phosphor_logging,
diff --git a/src/sdbus_wrapper.cpp b/src/sdbus_wrapper.cpp
deleted file mode 100644
index a781036..0000000
--- a/src/sdbus_wrapper.cpp
+++ /dev/null
@@ -1,9 +0,0 @@
-#include "sdbus_wrapper.hpp"
-
-#include <ipmid/api.h>
-
-sdbusplus::bus::bus& getBus()
-{
- static sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
- return bus;
-}
diff --git a/src/sdbus_wrapper.hpp b/src/sdbus_wrapper.hpp
deleted file mode 100644
index d6273d1..0000000
--- a/src/sdbus_wrapper.hpp
+++ /dev/null
@@ -1,5 +0,0 @@
-#pragma once
-
-#include <sdbusplus/bus.hpp>
-
-sdbusplus::bus::bus& getBus();
diff --git a/src/utils.cpp b/src/utils.cpp
deleted file mode 100644
index 60117bb..0000000
--- a/src/utils.cpp
+++ /dev/null
@@ -1,113 +0,0 @@
-#include "utils.hpp"
-
-#include <phosphor-logging/lg2.hpp>
-
-#include <algorithm>
-
-namespace utils
-{
-
-namespace // anonymous
-{
-constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
-constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
-constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
-} // namespace
-
-const UtilsInterface& getUtils()
-{
- static Utils utils;
- return utils;
-}
-
-std::string Utils::getService(sdbusplus::bus::bus& bus, const char* path,
- const char* interface) const
-{
- auto services = getServices(bus, path, interface);
- if (services.empty())
- {
- return {};
- }
- return services[0];
-}
-
-std::vector<std::string> Utils::getServices(sdbusplus::bus::bus& bus,
- const char* path,
- const char* interface) const
-{
- auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
- MAPPER_INTERFACE, "GetObject");
-
- mapper.append(path, std::vector<std::string>({interface}));
- try
- {
- auto mapperResponseMsg = bus.call(mapper);
-
- std::vector<std::pair<std::string, std::vector<std::string>>>
- mapperResponse;
- mapperResponseMsg.read(mapperResponse);
- if (mapperResponse.empty())
- {
- lg2::error("Error reading mapper response");
- throw std::runtime_error("Error reading mapper response");
- }
- std::vector<std::string> ret;
- for (const auto& i : mapperResponse)
- {
- ret.emplace_back(i.first);
- }
- return ret;
- }
- catch (const sdbusplus::exception::SdBusError& ex)
- {
- lg2::error(
- "GetObject call failed, path: {PATH}, interface: {INTERFACE}",
- "PATH", path, "INTERFACE", interface);
- throw std::runtime_error("GetObject call failed");
- }
-}
-
-any Utils::getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName) const
-{
- auto method = bus.new_method_call(service, path,
- "org.freedesktop.DBus.Properties", "Get");
- method.append(interface, propertyName);
- try
- {
- PropertyType value{};
- auto reply = bus.call(method);
- reply.read(value);
- return any(value);
- }
- catch (const sdbusplus::exception::SdBusError& ex)
- {
- lg2::error(
- "GetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}",
- "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
- throw std::runtime_error("GetProperty call failed");
- }
-}
-
-void Utils::setPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName, ValueType&& value) const
-{
- auto method = bus.new_method_call(service, path,
- "org.freedesktop.DBus.Properties", "Set");
- method.append(interface, propertyName, value);
- try
- {
- bus.call_noreply(method);
- }
- catch (const sdbusplus::exception::SdBusError& ex)
- {
- lg2::error(
- "SetProperty call failed, path: {PATH}, interface: {INTERFACE}, property: {PROPERTY}",
- "PATH", path, "INTERFACE", interface, "PROPERTY", propertyName);
- throw std::runtime_error("SetProperty call failed");
- }
-}
-
-} // namespace utils
diff --git a/src/utils.hpp b/src/utils.hpp
deleted file mode 100644
index 43e1f0d..0000000
--- a/src/utils.hpp
+++ /dev/null
@@ -1,168 +0,0 @@
-#pragma once
-
-#include <sdbusplus/bus.hpp>
-
-#include <experimental/any>
-#include <string>
-#include <vector>
-
-namespace utils
-{
-
-class UtilsInterface;
-
-// Due to a libstdc++ bug, we got compile error using std::any with gmock.
-// A temporary workaround is to use std::experimental::any.
-// See details in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90415
-using std::experimental::any;
-using std::experimental::any_cast;
-
-using ValueType =
- std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
- uint64_t, double, std::string>;
-/**
- * @brief Get the implementation of UtilsInterface
- */
-const UtilsInterface& getUtils();
-
-/** @brief Get service name from object path and interface
- *
- * @param[in] bus - The Dbus bus object
- * @param[in] path - The Dbus object path
- * @param[in] interface - The Dbus interface
- *
- * @return The name of the service
- */
-std::string getService(sdbusplus::bus::bus& bus, const char* path,
- const char* interface);
-
-/** @brief Get all the service names from object path and interface
- *
- * @param[in] bus - The Dbus bus object
- * @param[in] path - The Dbus object path
- * @param[in] interface - The Dbus interface
- *
- * @return The name of the services
- */
-std::vector<std::string> getServices(sdbusplus::bus::bus& bus, const char* path,
- const char* interface);
-
-/** @brief The template function to get property from the requested dbus path
- *
- * @param[in] bus - The Dbus bus object
- * @param[in] service - The Dbus service name
- * @param[in] path - The Dbus object path
- * @param[in] interface - The Dbus interface
- * @param[in] propertyName - The property name to get
- *
- * @return The value of the property
- */
-template <typename T>
-T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
- const char* interface, const char* propertyName);
-
-template <typename T>
-void setProperty(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName, const T& value);
-
-/**
- * @brief The interface for utils
- */
-class UtilsInterface
-{
- public:
- // For now the code needs to get property for Present and Version
- using PropertyType = std::variant<std::string, bool>;
-
- virtual ~UtilsInterface() = default;
-
- virtual std::string getService(sdbusplus::bus::bus& bus, const char* path,
- const char* interface) const = 0;
-
- virtual std::vector<std::string>
- getServices(sdbusplus::bus::bus& bus, const char* path,
- const char* interface) const = 0;
-
- virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName) const = 0;
-
- virtual void setPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName,
- ValueType&& value) const = 0;
-
- template <typename T>
- T getProperty(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName) const
- {
- any result = getPropertyImpl(bus, service, path, interface,
- propertyName);
- auto value = any_cast<PropertyType>(result);
- return std::get<T>(value);
- }
-
- template <typename T>
- void setProperty(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName, const T& value) const
- {
- ValueType v(value);
- setPropertyImpl(bus, service, path, interface, propertyName,
- std::move(v));
- }
-};
-
-class Utils : public UtilsInterface
-{
- public:
- std::string getService(sdbusplus::bus::bus& bus, const char* path,
- const char* interface) const override;
-
- std::vector<std::string> getServices(sdbusplus::bus::bus& bus,
- const char* path,
- const char* interface) const override;
-
- any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName) const override;
-
- void setPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName,
- ValueType&& value) const override;
-};
-
-inline std::string getService(sdbusplus::bus::bus& bus, const char* path,
- const char* interface)
-{
- return getUtils().getService(bus, path, interface);
-}
-
-inline std::vector<std::string> getServices(sdbusplus::bus::bus& bus,
- const char* path,
- const char* interface)
-{
- return getUtils().getServices(bus, path, interface);
-}
-
-template <typename T>
-T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
- const char* interface, const char* propertyName)
-{
- return getUtils().getProperty<T>(bus, service, path, interface,
- propertyName);
-}
-
-template <typename T>
-void setProperty(sdbusplus::bus::bus& bus, const char* service,
- const char* path, const char* interface,
- const char* propertyName, const T& value)
-{
- return getUtils().setProperty<T>(bus, service, path, interface,
- propertyName, value);
-}
-
-} // namespace utils