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