pldm: Moving utils function into the common directory
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I4fbd0da4bc6f27e9a9ed711962bd8737d8b55ebf
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index d815ce4..6776038 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -1,6 +1,6 @@
#include "bios.hpp"
-#include "libpldmresponder/utils.hpp"
+#include "utils.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include <array>
@@ -48,14 +48,15 @@
auto t = time_t(timeSec);
auto time = localtime(&t);
- seconds = decimalToBcd(time->tm_sec);
- minutes = decimalToBcd(time->tm_min);
- hours = decimalToBcd(time->tm_hour);
- day = decimalToBcd(time->tm_mday);
- month =
- decimalToBcd(time->tm_mon + 1); // The number of months in the range
- // 0 to 11.PLDM expects range 1 to 12
- year = decimalToBcd(time->tm_year + 1900); // The number of years since 1900
+ seconds = pldm::utils::decimalToBcd(time->tm_sec);
+ minutes = pldm::utils::decimalToBcd(time->tm_min);
+ hours = pldm::utils::decimalToBcd(time->tm_hour);
+ day = pldm::utils::decimalToBcd(time->tm_mday);
+ month = pldm::utils::decimalToBcd(time->tm_mon +
+ 1); // The number of months in the range
+ // 0 to 11.PLDM expects range 1 to 12
+ year = pldm::utils::decimalToBcd(time->tm_year +
+ 1900); // The number of years since 1900
}
size_t getTableTotalsize(size_t sizeWithoutPad)
@@ -126,7 +127,9 @@
auto bus = sdbusplus::bus::new_default();
try
{
- auto service = getService(bus, hostTimePath, timeInterface);
+
+ auto service =
+ pldm::utils::getService(bus, hostTimePath, timeInterface);
auto method = bus.new_method_call(service.c_str(), hostTimePath,
dbusProperties, "Get");
diff --git a/libpldmresponder/bios_parser.cpp b/libpldmresponder/bios_parser.cpp
index 86c07c6..062a342 100644
--- a/libpldmresponder/bios_parser.cpp
+++ b/libpldmresponder/bios_parser.cpp
@@ -1,6 +1,6 @@
#include "bios_parser.hpp"
-#include "libpldmresponder/utils.hpp"
+#include "utils.hpp"
#include <filesystem>
#include <fstream>
@@ -230,7 +230,7 @@
const auto& dbusValToValMap = internal::dbusValToValMaps.at(attrName);
propValue =
- pldm::responder::DBusHandler()
+ pldm::utils::DBusHandler()
.getDbusPropertyVariant<internal::PropertyValue>(
dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
dBusMap->interface.c_str());
@@ -345,7 +345,7 @@
return std::get<DefaultStr>(valueEntry);
}
- return pldm::responder::DBusHandler().getDbusProperty<std::string>(
+ return pldm::utils::DBusHandler().getDbusProperty<std::string>(
dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
dBusMap->interface.c_str());
}
@@ -409,7 +409,7 @@
return std::get<AttrDefaultValue>(valueEntry);
}
- return pldm::responder::DBusHandler().getDbusProperty<uint64_t>(
+ return pldm::utils::DBusHandler().getDbusProperty<uint64_t>(
dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
dBusMap->interface.c_str());
}
diff --git a/libpldmresponder/meson.build b/libpldmresponder/meson.build
index 2fc0f76..fc47454 100644
--- a/libpldmresponder/meson.build
+++ b/libpldmresponder/meson.build
@@ -11,8 +11,8 @@
'bios_parser.cpp',
'pdr.cpp',
'effecters.cpp',
- 'utils.cpp',
'platform.cpp',
+ '../utils.cpp'
]
if get_option('oem-ibm').enabled()
diff --git a/libpldmresponder/pdr.hpp b/libpldmresponder/pdr.hpp
index 87143a8..b32f878 100644
--- a/libpldmresponder/pdr.hpp
+++ b/libpldmresponder/pdr.hpp
@@ -298,13 +298,15 @@
{
std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
<< " ERROR=" << e.what() << "\n";
- reportError("xyz.openbmc_project.bmc.pldm.InternalFailure");
+ pldm::utils::reportError(
+ "xyz.openbmc_project.bmc.pldm.InternalFailure");
}
catch (const std::exception& e)
{
std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
<< " ERROR=" << e.what() << "\n";
- reportError("xyz.openbmc_project.bmc.pldm.InternalFailure");
+ pldm::utils::reportError(
+ "xyz.openbmc_project.bmc.pldm.InternalFailure");
}
}
}
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 1d6d042..9c5b688 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -1,6 +1,8 @@
#include "platform.hpp"
+#include "utils.hpp"
+
namespace pldm
{
namespace responder
@@ -106,9 +108,10 @@
if (rc == PLDM_SUCCESS)
{
stateField.resize(compEffecterCnt);
- const DBusHandler dBusIntf;
- rc = setStateEffecterStatesHandler<DBusHandler>(dBusIntf, effecterId,
- stateField);
+
+ const pldm::utils::DBusHandler dBusIntf;
+ rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>(
+ dBusIntf, effecterId, stateField);
}
encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index 1c6c733..3de0e1f 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -4,7 +4,7 @@
#include "handler.hpp"
#include "libpldmresponder/pdr.hpp"
-#include "libpldmresponder/utils.hpp"
+#include "utils.hpp"
#include <stdint.h>
diff --git a/libpldmresponder/utils.cpp b/libpldmresponder/utils.cpp
deleted file mode 100644
index 8290675..0000000
--- a/libpldmresponder/utils.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-#include "utils.hpp"
-
-#include "xyz/openbmc_project/Common/error.hpp"
-
-#include <array>
-#include <ctime>
-#include <iostream>
-#include <map>
-#include <stdexcept>
-#include <string>
-#include <vector>
-
-namespace pldm
-{
-
-constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
-constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
-constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
-
-namespace responder
-{
-
-std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
- const std::string& interface)
-{
- using DbusInterfaceList = std::vector<std::string>;
- std::map<std::string, std::vector<std::string>> mapperResponse;
-
- try
- {
- auto mapper = bus.new_method_call(mapperBusName, mapperPath,
- mapperInterface, "GetObject");
- mapper.append(path, DbusInterfaceList({interface}));
-
- auto mapperResponseMsg = bus.call(mapper);
- mapperResponseMsg.read(mapperResponse);
- }
- catch (std::exception& e)
- {
- std::cerr << "Error in mapper call, ERROR=" << e.what()
- << " PATH=" << path.c_str()
- << " INTERFACE=" << interface.c_str() << "\n";
- throw;
- }
- return mapperResponse.begin()->first;
-}
-
-void reportError(const char* errorMsg)
-{
- static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
- static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
-
- static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
-
- try
- {
- auto service = getService(bus, logObjPath, logInterface);
- using namespace sdbusplus::xyz::openbmc_project::Logging::server;
- auto severity =
- sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
- sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
- Error);
- auto method = bus.new_method_call(service.c_str(), logObjPath,
- logInterface, "Create");
- std::map<std::string, std::string> addlData{};
- method.append(errorMsg, severity, addlData);
- bus.call_noreply(method);
- }
- catch (const std::exception& e)
- {
- std::cerr << "failed to make a d-bus call to create error log, ERROR="
- << e.what() << "\n";
- }
-}
-
-namespace utils
-{
-
-uint8_t getNumPadBytes(uint32_t data)
-{
- uint8_t pad;
- pad = ((data % 4) ? (4 - data % 4) : 0);
- return pad;
-} // end getNumPadBytes
-
-} // end namespace utils
-} // namespace responder
-} // namespace pldm
diff --git a/libpldmresponder/utils.hpp b/libpldmresponder/utils.hpp
deleted file mode 100644
index 453f6d9..0000000
--- a/libpldmresponder/utils.hpp
+++ /dev/null
@@ -1,175 +0,0 @@
-#pragma once
-
-#include <stdint.h>
-#include <systemd/sd-bus.h>
-#include <unistd.h>
-
-#include <exception>
-#include <iostream>
-#include <sdbusplus/server.hpp>
-#include <string>
-#include <variant>
-#include <vector>
-#include <xyz/openbmc_project/Logging/Entry/server.hpp>
-
-#include "libpldm/base.h"
-
-namespace pldm
-{
-namespace responder
-{
-namespace utils
-{
-
-/** @struct CustomFD
- *
- * RAII wrapper for file descriptor.
- */
-struct CustomFD
-{
- CustomFD(const CustomFD&) = delete;
- CustomFD& operator=(const CustomFD&) = delete;
- CustomFD(CustomFD&&) = delete;
- CustomFD& operator=(CustomFD&&) = delete;
-
- CustomFD(int fd) : fd(fd)
- {
- }
-
- ~CustomFD()
- {
- if (fd >= 0)
- {
- close(fd);
- }
- }
-
- int operator()() const
- {
- return fd;
- }
-
- private:
- int fd = -1;
-};
-
-/** @brief Calculate the pad for PLDM data
- *
- * @param[in] data - Length of the data
- * @return - uint8_t - number of pad bytes
- */
-uint8_t getNumPadBytes(uint32_t data);
-
-} // namespace utils
-
-/**
- * @brief Get the DBUS Service name for the input dbus path
- * @param[in] bus - DBUS Bus Object
- * @param[in] path - DBUS object path
- * @param[in] interface - DBUS Interface
- * @return std::string - the dbus service name
- */
-std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
- const std::string& interface);
-
-/**
- * @brief creates an error log
- * @param[in] errorMsg - the error message
- */
-void reportError(const char* errorMsg);
-
-/** @brief Convert any Decimal number to BCD
- *
- * @tparam[in] decimal - Decimal number
- * @return Corresponding BCD number
- */
-template <typename T>
-T decimalToBcd(T decimal)
-{
- T bcd = 0;
- T rem = 0;
- auto cnt = 0;
-
- while (decimal)
- {
- rem = decimal % 10;
- bcd = bcd + (rem << cnt);
- decimal = decimal / 10;
- cnt += 4;
- }
-
- return bcd;
-}
-
-constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
-
-/**
- * @class DBusHandler
- *
- * Wrapper class to handle the D-Bus calls
- *
- * This class contains the APIs to handle the D-Bus calls
- * to cater the request from pldm requester.
- * A class is created to mock the apis in the test cases
- */
-class DBusHandler
-{
- public:
- /** @brief API to set a D-Bus property
- *
- * @param[in] objPath - Object path for the D-Bus object
- * @param[in] dbusProp - The D-Bus property
- * @param[in] dbusInterface - The D-Bus interface
- * @param[in] value - The value to be set
- * failure
- */
- template <typename T>
- void setDbusProperty(const char* objPath, const char* dbusProp,
- const char* dbusInterface,
- const std::variant<T>& value) const
- {
- auto bus = sdbusplus::bus::new_default();
- auto service = getService(bus, objPath, dbusInterface);
- auto method = bus.new_method_call(service.c_str(), objPath,
- dbusProperties, "Set");
- method.append(dbusInterface, dbusProp, value);
- bus.call_noreply(method);
- }
-
- template <typename Variant>
- auto getDbusPropertyVariant(const char* objPath, const char* dbusProp,
- const char* dbusInterface)
- {
- Variant value;
- auto bus = sdbusplus::bus::new_default();
- auto service = getService(bus, objPath, dbusInterface);
- auto method = bus.new_method_call(service.c_str(), objPath,
- dbusProperties, "Get");
- method.append(dbusInterface, dbusProp);
- try
- {
- auto reply = bus.call(method);
- reply.read(value);
- }
- catch (const sdbusplus::exception::SdBusError& e)
- {
- std::cerr << "dbus call exception, OBJPATH=" << objPath
- << " INTERFACE=" << dbusInterface
- << " PROPERTY=" << dbusProp << " EXCEPTION=" << e.what()
- << "\n";
- }
- return value;
- }
-
- template <typename Property>
- auto getDbusProperty(const char* objPath, const char* dbusProp,
- const char* dbusInterface)
- {
- auto VariantValue = getDbusPropertyVariant<std::variant<Property>>(
- objPath, dbusProp, dbusInterface);
- return std::get<Property>(VariantValue);
- }
-};
-
-} // namespace responder
-} // namespace pldm