blob: 39c7ed6ed17a13cc0247cf5225a4d011f4d736d9 [file] [log] [blame]
#pragma once
#include <stdint.h>
#include <systemd/sd-bus.h>
#include <unistd.h>
#include <sdbusplus/server.hpp>
#include <string>
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;
};
} // 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 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;
}
} // namespace responder
} // namespace pldm