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 #include <format>
13
14 using namespace phosphor::logging;
15 using InternalFailure =
16 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17
18 namespace phosphor
19 {
20 namespace fan
21 {
22 namespace util
23 {
24
25 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
28
29 constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
30 constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
31 constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager";
32
33 constexpr auto OPERATIONAL_STATUS_INTF =
34 "xyz.openbmc_project.State.Decorator.OperationalStatus";
35 constexpr auto FUNCTIONAL_PROPERTY = "Functional";
36
37 constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
38 constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
39
40 class FileDescriptor
41 {
42 public:
43 FileDescriptor() = delete;
44 FileDescriptor(const FileDescriptor&) = delete;
45 FileDescriptor(FileDescriptor&&) = default;
46 FileDescriptor& operator=(const FileDescriptor&) = delete;
47 FileDescriptor& operator=(FileDescriptor&&) = default;
48
FileDescriptor(int fd)49 explicit FileDescriptor(int fd) : fd(fd) {}
50
~FileDescriptor()51 ~FileDescriptor()
52 {
53 if (fd != -1)
54 {
55 close(fd);
56 }
57 }
58
operator ()()59 int operator()()
60 {
61 return fd;
62 }
63
open(const std::string & pathname,int flags)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 std::format("Failed to open file device path {}", pathname)
71 .c_str());
72 elog<InternalFailure>();
73 }
74 }
75
is_open()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>
getObjMap(const std::string & path,const std::string & intf,const std::string & prop,const T & value)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