blob: ac1ebbc0dc06d55303e81fc2ce795db4319f7182 [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";
Mike Cappsfdcd5db2021-05-20 12:47:10 -040036constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
Matt Spinlerb63aa092020-10-14 09:45:11 -050037
Brandon Wymanfef02952017-03-31 18:13:21 -050038class FileDescriptor
39{
Matthew Barth9e80c872020-05-26 10:50:29 -050040 public:
41 FileDescriptor() = delete;
42 FileDescriptor(const FileDescriptor&) = delete;
43 FileDescriptor(FileDescriptor&&) = default;
44 FileDescriptor& operator=(const FileDescriptor&) = delete;
45 FileDescriptor& operator=(FileDescriptor&&) = default;
Brandon Wymanfef02952017-03-31 18:13:21 -050046
Matthew Barth9f93bd32020-05-28 16:57:14 -050047 explicit FileDescriptor(int fd) : fd(fd)
Matthew Barth9e80c872020-05-26 10:50:29 -050048 {}
49
50 ~FileDescriptor()
51 {
52 if (fd != -1)
Brandon Wymanfef02952017-03-31 18:13:21 -050053 {
Matthew Barth9e80c872020-05-26 10:50:29 -050054 close(fd);
Brandon Wymanfef02952017-03-31 18:13:21 -050055 }
Matthew Barth9e80c872020-05-26 10:50:29 -050056 }
Brandon Wymanfef02952017-03-31 18:13:21 -050057
Matthew Barth9e80c872020-05-26 10:50:29 -050058 int operator()()
59 {
60 return fd;
61 }
62
63 void open(const std::string& pathname, int flags)
64 {
65 fd = ::open(pathname.c_str(), flags);
66 if (-1 == fd)
Brandon Wymanfef02952017-03-31 18:13:21 -050067 {
Jay Meyerae226192020-10-15 15:33:17 -050068 log<level::ERR>(
69 fmt::format("Failed to open file device path {}", pathname)
70 .c_str());
Matthew Barth9e80c872020-05-26 10:50:29 -050071 elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050072 }
Matthew Barth9e80c872020-05-26 10:50:29 -050073 }
Brandon Wymanfef02952017-03-31 18:13:21 -050074
Matthew Barth9e80c872020-05-26 10:50:29 -050075 bool is_open()
76 {
77 return fd != -1;
78 }
Brandon Wymance822442017-04-10 17:48:02 -050079
Matthew Barth9e80c872020-05-26 10:50:29 -050080 private:
81 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050082};
83
Brandon Wyman5914f652017-03-16 18:17:07 -050084/**
Matthew Barth51dd1852017-11-16 15:21:13 -060085 * @brief Get the object map for creating or updating an object property
86 *
87 * @param[in] path - The dbus object path name
88 * @param[in] intf - The dbus interface
89 * @param[in] prop - The dbus property
90 * @param[in] value - The property value
91 *
92 * @return - The full object path containing the property value
93 */
94template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050095auto getObjMap(const std::string& path, const std::string& intf,
96 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060097{
98 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050099 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -0600100 using PropertyMap = std::map<Property, Value>;
101
102 using Interface = std::string;
103 using InterfaceMap = std::map<Interface, PropertyMap>;
104
105 using Object = sdbusplus::message::object_path;
106 using ObjectMap = std::map<Object, InterfaceMap>;
107
108 ObjectMap objectMap;
109 InterfaceMap interfaceMap;
110 PropertyMap propertyMap;
111
112 propertyMap.emplace(prop, value);
113 interfaceMap.emplace(intf, std::move(propertyMap));
114 objectMap.emplace(path, std::move(interfaceMap));
115
116 return objectMap;
117}
118
Matthew Barth9e80c872020-05-26 10:50:29 -0500119} // namespace util
120} // namespace fan
121} // namespace phosphor