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/lg2.hpp>
9 #include <sdbusplus/bus.hpp>
10 #include <xyz/openbmc_project/Common/error.hpp>
11
12 using InternalFailure =
13 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
14
15 namespace phosphor
16 {
17 namespace fan
18 {
19 namespace util
20 {
21
22 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
23 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
24 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
25
26 constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
27 constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
28 constexpr auto INVENTORY_SVC = "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 constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
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
FileDescriptor(int fd)46 explicit FileDescriptor(int fd) : fd(fd) {}
47
~FileDescriptor()48 ~FileDescriptor()
49 {
50 if (fd != -1)
51 {
52 close(fd);
53 }
54 }
55
operator ()()56 int operator()()
57 {
58 return fd;
59 }
60
open(const std::string & pathname,int flags)61 void open(const std::string& pathname, int flags)
62 {
63 fd = ::open(pathname.c_str(), flags);
64 if (-1 == fd)
65 {
66 lg2::error("Failed to open file device path {PATH}", "PATH",
67 pathname);
68 phosphor::logging::elog<InternalFailure>();
69 }
70 }
71
is_open()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>
getObjMap(const std::string & path,const std::string & intf,const std::string & prop,const T & value)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