1 #pragma once 2 3 #include <fcntl.h> 4 #include <fmt/format.h> 5 #include <unistd.h> 6 7 #include <phosphor-logging/elog-errors.hpp> 8 #include <phosphor-logging/elog.hpp> 9 #include <phosphor-logging/log.hpp> 10 #include <sdbusplus/bus.hpp> 11 #include <xyz/openbmc_project/Common/error.hpp> 12 13 using namespace phosphor::logging; 14 using InternalFailure = 15 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 16 17 namespace phosphor 18 { 19 namespace fan 20 { 21 namespace util 22 { 23 24 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; 25 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; 26 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; 27 28 constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; 29 constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; 30 constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager"; 31 32 constexpr auto OPERATIONAL_STATUS_INTF = 33 "xyz.openbmc_project.State.Decorator.OperationalStatus"; 34 constexpr auto FUNCTIONAL_PROPERTY = "Functional"; 35 36 constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item"; 37 constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; 38 39 class FileDescriptor 40 { 41 public: 42 FileDescriptor() = delete; 43 FileDescriptor(const FileDescriptor&) = delete; 44 FileDescriptor(FileDescriptor&&) = default; 45 FileDescriptor& operator=(const FileDescriptor&) = delete; 46 FileDescriptor& operator=(FileDescriptor&&) = default; 47 48 explicit FileDescriptor(int fd) : fd(fd) 49 {} 50 51 ~FileDescriptor() 52 { 53 if (fd != -1) 54 { 55 close(fd); 56 } 57 } 58 59 int operator()() 60 { 61 return fd; 62 } 63 64 void open(const std::string& pathname, int flags) 65 { 66 fd = ::open(pathname.c_str(), flags); 67 if (-1 == fd) 68 { 69 log<level::ERR>( 70 fmt::format("Failed to open file device path {}", pathname) 71 .c_str()); 72 elog<InternalFailure>(); 73 } 74 } 75 76 bool is_open() 77 { 78 return fd != -1; 79 } 80 81 private: 82 int fd = -1; 83 }; 84 85 /** 86 * @brief Get the object map for creating or updating an object property 87 * 88 * @param[in] path - The dbus object path name 89 * @param[in] intf - The dbus interface 90 * @param[in] prop - The dbus property 91 * @param[in] value - The property value 92 * 93 * @return - The full object path containing the property value 94 */ 95 template <typename T> 96 auto getObjMap(const std::string& path, const std::string& intf, 97 const std::string& prop, const T& value) 98 { 99 using Property = std::string; 100 using Value = std::variant<T>; 101 using PropertyMap = std::map<Property, Value>; 102 103 using Interface = std::string; 104 using InterfaceMap = std::map<Interface, PropertyMap>; 105 106 using Object = sdbusplus::message::object_path; 107 using ObjectMap = std::map<Object, InterfaceMap>; 108 109 ObjectMap objectMap; 110 InterfaceMap interfaceMap; 111 PropertyMap propertyMap; 112 113 propertyMap.emplace(prop, value); 114 interfaceMap.emplace(intf, std::move(propertyMap)); 115 objectMap.emplace(path, std::move(interfaceMap)); 116 117 return objectMap; 118 } 119 120 } // namespace util 121 } // namespace fan 122 } // namespace phosphor 123