xref: /openbmc/phosphor-fan-presence/utility.hpp (revision b63aa09ef68f8bee70c613bfaac3ac0279a2b2d6)
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 constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
35 
36 class FileDescriptor
37 {
38   public:
39     FileDescriptor() = delete;
40     FileDescriptor(const FileDescriptor&) = delete;
41     FileDescriptor(FileDescriptor&&) = default;
42     FileDescriptor& operator=(const FileDescriptor&) = delete;
43     FileDescriptor& operator=(FileDescriptor&&) = default;
44 
45     explicit FileDescriptor(int fd) : fd(fd)
46     {}
47 
48     ~FileDescriptor()
49     {
50         if (fd != -1)
51         {
52             close(fd);
53         }
54     }
55 
56     int operator()()
57     {
58         return fd;
59     }
60 
61     void open(const std::string& pathname, int flags)
62     {
63         fd = ::open(pathname.c_str(), flags);
64         if (-1 == fd)
65         {
66             log<level::ERR>("Failed to open file device: ",
67                             entry("PATHNAME=%s", pathname.c_str()));
68             elog<InternalFailure>();
69         }
70     }
71 
72     bool is_open()
73     {
74         return fd != -1;
75     }
76 
77   private:
78     int fd = -1;
79 };
80 
81 /**
82  * @brief Get the object map for creating or updating an object property
83  *
84  * @param[in] path - The dbus object path name
85  * @param[in] intf - The dbus interface
86  * @param[in] prop - The dbus property
87  * @param[in] value - The property value
88  *
89  * @return - The full object path containing the property value
90  */
91 template <typename T>
92 auto getObjMap(const std::string& path, const std::string& intf,
93                const std::string& prop, const T& value)
94 {
95     using Property = std::string;
96     using Value = std::variant<T>;
97     using PropertyMap = std::map<Property, Value>;
98 
99     using Interface = std::string;
100     using InterfaceMap = std::map<Interface, PropertyMap>;
101 
102     using Object = sdbusplus::message::object_path;
103     using ObjectMap = std::map<Object, InterfaceMap>;
104 
105     ObjectMap objectMap;
106     InterfaceMap interfaceMap;
107     PropertyMap propertyMap;
108 
109     propertyMap.emplace(prop, value);
110     interfaceMap.emplace(intf, std::move(propertyMap));
111     objectMap.emplace(path, std::move(interfaceMap));
112 
113     return objectMap;
114 }
115 
116 } // namespace util
117 } // namespace fan
118 } // namespace phosphor
119