blob: bb23dea83a091da0a16d7a874083c34c058d09a7 [file] [log] [blame]
Brandon Wyman5914f652017-03-16 18:17:07 -05001#pragma once
2
3#include <sdbusplus/bus.hpp>
Brandon Wymanfef02952017-03-31 18:13:21 -05004#include <unistd.h>
Brandon Wymance822442017-04-10 17:48:02 -05005#include <fcntl.h>
Dinesh Chinari618027a2017-06-26 23:26:50 -05006#include <phosphor-logging/log.hpp>
7#include <phosphor-logging/elog.hpp>
8#include <phosphor-logging/elog-errors.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
Brandon Wymance822442017-04-10 17:48:02 -050010
Brandon Wyman5914f652017-03-16 18:17:07 -050011
Dinesh Chinari618027a2017-06-26 23:26:50 -050012using namespace phosphor::logging;
13using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
14 Error::InternalFailure;
15
Brandon Wyman5914f652017-03-16 18:17:07 -050016namespace phosphor
17{
18namespace fan
19{
Matt Spinler5cfdf942017-04-10 14:25:47 -050020namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050021{
22
Matthew Barth51dd1852017-11-16 15:21:13 -060023constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
24constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
25constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
26
27constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
28constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
29
30constexpr auto OPERATIONAL_STATUS_INTF =
31 "xyz.openbmc_project.State.Decorator.OperationalStatus";
32constexpr auto FUNCTIONAL_PROPERTY = "Functional";
33
Brandon Wymanfef02952017-03-31 18:13:21 -050034class 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
47 ~FileDescriptor()
48 {
49 if (fd != -1)
50 {
51 close(fd);
52 }
53 }
54
Brandon Wymance822442017-04-10 17:48:02 -050055 int operator()()
56 {
57 return fd;
58 }
59
60 void open(const std::string& pathname, int flags)
61 {
62 fd = ::open(pathname.c_str(), flags);
63 if (-1 == fd)
64 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050065 log<level::ERR>(
66 "Failed to open file device: ",
67 entry("PATHNAME=%s", pathname.c_str()));
68 elog<InternalFailure>();
Brandon Wymance822442017-04-10 17:48:02 -050069 }
70 }
71
Brandon Wymanfef02952017-03-31 18:13:21 -050072 bool is_open()
73 {
74 return fd != -1;
75 }
76
77 private:
78 int fd = -1;
79
80};
81
Brandon Wyman5914f652017-03-16 18:17:07 -050082/**
83 * @brief Get the inventory service name from the mapper object
84 *
85 * @return The inventory manager service name
86 */
87std::string getInvService(sdbusplus::bus::bus& bus);
88
Matt Spinler5cfdf942017-04-10 14:25:47 -050089
90/**
91 * @brief Get the service name from the mapper for the
92 * interface and path passed in.
93 *
94 * @param[in] path - the dbus path name
95 * @param[in] interface - the dbus interface name
96 * @param[in] bus - the dbus object
97 *
98 * @return The service name
99 */
100std::string getService(const std::string& path,
101 const std::string& interface,
102 sdbusplus::bus::bus& bus);
103
Matthew Barth51dd1852017-11-16 15:21:13 -0600104/**
105 * @brief Get the object map for creating or updating an object property
106 *
107 * @param[in] path - The dbus object path name
108 * @param[in] intf - The dbus interface
109 * @param[in] prop - The dbus property
110 * @param[in] value - The property value
111 *
112 * @return - The full object path containing the property value
113 */
114template <typename T>
115auto getObjMap(const std::string& path,
116 const std::string& intf,
117 const std::string& prop,
118 const T& value)
119{
120 using Property = std::string;
121 using Value = sdbusplus::message::variant<T>;
122 using PropertyMap = std::map<Property, Value>;
123
124 using Interface = std::string;
125 using InterfaceMap = std::map<Interface, PropertyMap>;
126
127 using Object = sdbusplus::message::object_path;
128 using ObjectMap = std::map<Object, InterfaceMap>;
129
130 ObjectMap objectMap;
131 InterfaceMap interfaceMap;
132 PropertyMap propertyMap;
133
134 propertyMap.emplace(prop, value);
135 interfaceMap.emplace(intf, std::move(propertyMap));
136 objectMap.emplace(path, std::move(interfaceMap));
137
138 return objectMap;
139}
140
Brandon Wyman5914f652017-03-16 18:17:07 -0500141}
142}
143}