blob: 727b557ed4c62e278408ffc92b4a112747d16bb6 [file] [log] [blame]
#pragma once
#include <fcntl.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <format>
using namespace phosphor::logging;
using InternalFailure =
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
namespace phosphor
{
namespace fan
{
namespace util
{
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";
constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager";
constexpr auto OPERATIONAL_STATUS_INTF =
"xyz.openbmc_project.State.Decorator.OperationalStatus";
constexpr auto FUNCTIONAL_PROPERTY = "Functional";
constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
class FileDescriptor
{
public:
FileDescriptor() = delete;
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor(FileDescriptor&&) = default;
FileDescriptor& operator=(const FileDescriptor&) = delete;
FileDescriptor& operator=(FileDescriptor&&) = default;
explicit FileDescriptor(int fd) : fd(fd) {}
~FileDescriptor()
{
if (fd != -1)
{
close(fd);
}
}
int operator()()
{
return fd;
}
void open(const std::string& pathname, int flags)
{
fd = ::open(pathname.c_str(), flags);
if (-1 == fd)
{
log<level::ERR>(
std::format("Failed to open file device path {}", pathname)
.c_str());
elog<InternalFailure>();
}
}
bool is_open()
{
return fd != -1;
}
private:
int fd = -1;
};
/**
* @brief Get the object map for creating or updating an object property
*
* @param[in] path - The dbus object path name
* @param[in] intf - The dbus interface
* @param[in] prop - The dbus property
* @param[in] value - The property value
*
* @return - The full object path containing the property value
*/
template <typename T>
auto getObjMap(const std::string& path, const std::string& intf,
const std::string& prop, const T& value)
{
using Property = std::string;
using Value = std::variant<T>;
using PropertyMap = std::map<Property, Value>;
using Interface = std::string;
using InterfaceMap = std::map<Interface, PropertyMap>;
using Object = sdbusplus::message::object_path;
using ObjectMap = std::map<Object, InterfaceMap>;
ObjectMap objectMap;
InterfaceMap interfaceMap;
PropertyMap propertyMap;
propertyMap.emplace(prop, value);
interfaceMap.emplace(intf, std::move(propertyMap));
objectMap.emplace(path, std::move(interfaceMap));
return objectMap;
}
} // namespace util
} // namespace fan
} // namespace phosphor