blob: 7db904706f230fdaeda68ded652bd78701e94279 [file] [log] [blame]
Brandon Wyman5914f652017-03-16 18:17:07 -05001#pragma once
2
Brandon Wymance822442017-04-10 17:48:02 -05003#include <fcntl.h>
Matthew Barth9e80c872020-05-26 10:50:29 -05004#include <unistd.h>
5
Dinesh Chinari618027a2017-06-26 23:26:50 -05006#include <phosphor-logging/elog-errors.hpp>
Matthew Barth9e80c872020-05-26 10:50:29 -05007#include <phosphor-logging/elog.hpp>
8#include <phosphor-logging/log.hpp>
9#include <sdbusplus/bus.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050010#include <xyz/openbmc_project/Common/error.hpp>
Brandon Wymance822442017-04-10 17:48:02 -050011
Dinesh Chinari618027a2017-06-26 23:26:50 -050012using namespace phosphor::logging;
Matthew Barth9e80c872020-05-26 10:50:29 -050013using InternalFailure =
14 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Dinesh Chinari618027a2017-06-26 23:26:50 -050015
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{
Matthew Barth9e80c872020-05-26 10:50:29 -050036 public:
37 FileDescriptor() = delete;
38 FileDescriptor(const FileDescriptor&) = delete;
39 FileDescriptor(FileDescriptor&&) = default;
40 FileDescriptor& operator=(const FileDescriptor&) = delete;
41 FileDescriptor& operator=(FileDescriptor&&) = default;
Brandon Wymanfef02952017-03-31 18:13:21 -050042
Matthew Barth9e80c872020-05-26 10:50:29 -050043 FileDescriptor(int fd) : fd(fd)
44 {}
45
46 ~FileDescriptor()
47 {
48 if (fd != -1)
Brandon Wymanfef02952017-03-31 18:13:21 -050049 {
Matthew Barth9e80c872020-05-26 10:50:29 -050050 close(fd);
Brandon Wymanfef02952017-03-31 18:13:21 -050051 }
Matthew Barth9e80c872020-05-26 10:50:29 -050052 }
Brandon Wymanfef02952017-03-31 18:13:21 -050053
Matthew Barth9e80c872020-05-26 10:50:29 -050054 int operator()()
55 {
56 return fd;
57 }
58
59 void open(const std::string& pathname, int flags)
60 {
61 fd = ::open(pathname.c_str(), flags);
62 if (-1 == fd)
Brandon Wymanfef02952017-03-31 18:13:21 -050063 {
Matthew Barth9e80c872020-05-26 10:50:29 -050064 log<level::ERR>("Failed to open file device: ",
65 entry("PATHNAME=%s", pathname.c_str()));
66 elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050067 }
Matthew Barth9e80c872020-05-26 10:50:29 -050068 }
Brandon Wymanfef02952017-03-31 18:13:21 -050069
Matthew Barth9e80c872020-05-26 10:50:29 -050070 bool is_open()
71 {
72 return fd != -1;
73 }
Brandon Wymance822442017-04-10 17:48:02 -050074
Matthew Barth9e80c872020-05-26 10:50:29 -050075 private:
76 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050077};
78
Brandon Wyman5914f652017-03-16 18:17:07 -050079/**
Matthew Barth51dd1852017-11-16 15:21:13 -060080 * @brief Get the object map for creating or updating an object property
81 *
82 * @param[in] path - The dbus object path name
83 * @param[in] intf - The dbus interface
84 * @param[in] prop - The dbus property
85 * @param[in] value - The property value
86 *
87 * @return - The full object path containing the property value
88 */
89template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050090auto getObjMap(const std::string& path, const std::string& intf,
91 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060092{
93 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050094 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -060095 using PropertyMap = std::map<Property, Value>;
96
97 using Interface = std::string;
98 using InterfaceMap = std::map<Interface, PropertyMap>;
99
100 using Object = sdbusplus::message::object_path;
101 using ObjectMap = std::map<Object, InterfaceMap>;
102
103 ObjectMap objectMap;
104 InterfaceMap interfaceMap;
105 PropertyMap propertyMap;
106
107 propertyMap.emplace(prop, value);
108 interfaceMap.emplace(intf, std::move(propertyMap));
109 objectMap.emplace(path, std::move(interfaceMap));
110
111 return objectMap;
112}
113
Matthew Barth9e80c872020-05-26 10:50:29 -0500114} // namespace util
115} // namespace fan
116} // namespace phosphor