blob: 65632437adb8bbffd61f7fab961a1c9e2102a6b5 [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>
Jay Meyerae226192020-10-15 15:33:17 -05004#include <fmt/format.h>
Matthew Barth9e80c872020-05-26 10:50:29 -05005#include <unistd.h>
6
Dinesh Chinari618027a2017-06-26 23:26:50 -05007#include <phosphor-logging/elog-errors.hpp>
Matthew Barth9e80c872020-05-26 10:50:29 -05008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
10#include <sdbusplus/bus.hpp>
Dinesh Chinari618027a2017-06-26 23:26:50 -050011#include <xyz/openbmc_project/Common/error.hpp>
Brandon Wymance822442017-04-10 17:48:02 -050012
Dinesh Chinari618027a2017-06-26 23:26:50 -050013using namespace phosphor::logging;
Matthew Barth9e80c872020-05-26 10:50:29 -050014using InternalFailure =
15 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Dinesh Chinari618027a2017-06-26 23:26:50 -050016
Brandon Wyman5914f652017-03-16 18:17:07 -050017namespace phosphor
18{
19namespace fan
20{
Matt Spinler5cfdf942017-04-10 14:25:47 -050021namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050022{
23
Matthew Barth51dd1852017-11-16 15:21:13 -060024constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
25constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
26constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
27
28constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
29constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
30
31constexpr auto OPERATIONAL_STATUS_INTF =
32 "xyz.openbmc_project.State.Decorator.OperationalStatus";
33constexpr auto FUNCTIONAL_PROPERTY = "Functional";
34
Matt Spinlerb63aa092020-10-14 09:45:11 -050035constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
36
Brandon Wymanfef02952017-03-31 18:13:21 -050037class FileDescriptor
38{
Matthew Barth9e80c872020-05-26 10:50:29 -050039 public:
40 FileDescriptor() = delete;
41 FileDescriptor(const FileDescriptor&) = delete;
42 FileDescriptor(FileDescriptor&&) = default;
43 FileDescriptor& operator=(const FileDescriptor&) = delete;
44 FileDescriptor& operator=(FileDescriptor&&) = default;
Brandon Wymanfef02952017-03-31 18:13:21 -050045
Matthew Barth9f93bd32020-05-28 16:57:14 -050046 explicit FileDescriptor(int fd) : fd(fd)
Matthew Barth9e80c872020-05-26 10:50:29 -050047 {}
48
49 ~FileDescriptor()
50 {
51 if (fd != -1)
Brandon Wymanfef02952017-03-31 18:13:21 -050052 {
Matthew Barth9e80c872020-05-26 10:50:29 -050053 close(fd);
Brandon Wymanfef02952017-03-31 18:13:21 -050054 }
Matthew Barth9e80c872020-05-26 10:50:29 -050055 }
Brandon Wymanfef02952017-03-31 18:13:21 -050056
Matthew Barth9e80c872020-05-26 10:50:29 -050057 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)
Brandon Wymanfef02952017-03-31 18:13:21 -050066 {
Jay Meyerae226192020-10-15 15:33:17 -050067 log<level::ERR>(
68 fmt::format("Failed to open file device path {}", pathname)
69 .c_str());
Matthew Barth9e80c872020-05-26 10:50:29 -050070 elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050071 }
Matthew Barth9e80c872020-05-26 10:50:29 -050072 }
Brandon Wymanfef02952017-03-31 18:13:21 -050073
Matthew Barth9e80c872020-05-26 10:50:29 -050074 bool is_open()
75 {
76 return fd != -1;
77 }
Brandon Wymance822442017-04-10 17:48:02 -050078
Matthew Barth9e80c872020-05-26 10:50:29 -050079 private:
80 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050081};
82
Brandon Wyman5914f652017-03-16 18:17:07 -050083/**
Matthew Barth51dd1852017-11-16 15:21:13 -060084 * @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 */
93template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050094auto getObjMap(const std::string& path, const std::string& intf,
95 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060096{
97 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050098 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -060099 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
Matthew Barth9e80c872020-05-26 10:50:29 -0500118} // namespace util
119} // namespace fan
120} // namespace phosphor