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