blob: 727b557ed4c62e278408ffc92b4a112747d16bb6 [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
Patrick Williamsfbf47032023-07-17 12:27:34 -050012#include <format>
13
Dinesh Chinari618027a2017-06-26 23:26:50 -050014using namespace phosphor::logging;
Matthew Barth9e80c872020-05-26 10:50:29 -050015using InternalFailure =
16 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Dinesh Chinari618027a2017-06-26 23:26:50 -050017
Brandon Wyman5914f652017-03-16 18:17:07 -050018namespace phosphor
19{
20namespace fan
21{
Matt Spinler5cfdf942017-04-10 14:25:47 -050022namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050023{
24
Matthew Barth51dd1852017-11-16 15:21:13 -060025constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
28
29constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
30constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
Mike Cappsab509252022-01-10 21:56:11 -050031constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager";
Matthew Barth51dd1852017-11-16 15:21:13 -060032
33constexpr auto OPERATIONAL_STATUS_INTF =
34 "xyz.openbmc_project.State.Decorator.OperationalStatus";
35constexpr auto FUNCTIONAL_PROPERTY = "Functional";
36
Matt Spinlerb63aa092020-10-14 09:45:11 -050037constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
Mike Cappsfdcd5db2021-05-20 12:47:10 -040038constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
Matt Spinlerb63aa092020-10-14 09:45:11 -050039
Brandon Wymanfef02952017-03-31 18:13:21 -050040class FileDescriptor
41{
Matthew Barth9e80c872020-05-26 10:50:29 -050042 public:
43 FileDescriptor() = delete;
44 FileDescriptor(const FileDescriptor&) = delete;
45 FileDescriptor(FileDescriptor&&) = default;
46 FileDescriptor& operator=(const FileDescriptor&) = delete;
47 FileDescriptor& operator=(FileDescriptor&&) = default;
Brandon Wymanfef02952017-03-31 18:13:21 -050048
Patrick Williams61b73292023-05-10 07:50:12 -050049 explicit FileDescriptor(int fd) : fd(fd) {}
Matthew Barth9e80c872020-05-26 10:50:29 -050050
51 ~FileDescriptor()
52 {
53 if (fd != -1)
Brandon Wymanfef02952017-03-31 18:13:21 -050054 {
Matthew Barth9e80c872020-05-26 10:50:29 -050055 close(fd);
Brandon Wymanfef02952017-03-31 18:13:21 -050056 }
Matthew Barth9e80c872020-05-26 10:50:29 -050057 }
Brandon Wymanfef02952017-03-31 18:13:21 -050058
Matthew Barth9e80c872020-05-26 10:50:29 -050059 int operator()()
60 {
61 return fd;
62 }
63
64 void open(const std::string& pathname, int flags)
65 {
66 fd = ::open(pathname.c_str(), flags);
67 if (-1 == fd)
Brandon Wymanfef02952017-03-31 18:13:21 -050068 {
Jay Meyerae226192020-10-15 15:33:17 -050069 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050070 std::format("Failed to open file device path {}", pathname)
Jay Meyerae226192020-10-15 15:33:17 -050071 .c_str());
Matthew Barth9e80c872020-05-26 10:50:29 -050072 elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050073 }
Matthew Barth9e80c872020-05-26 10:50:29 -050074 }
Brandon Wymanfef02952017-03-31 18:13:21 -050075
Matthew Barth9e80c872020-05-26 10:50:29 -050076 bool is_open()
77 {
78 return fd != -1;
79 }
Brandon Wymance822442017-04-10 17:48:02 -050080
Matthew Barth9e80c872020-05-26 10:50:29 -050081 private:
82 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050083};
84
Brandon Wyman5914f652017-03-16 18:17:07 -050085/**
Matthew Barth51dd1852017-11-16 15:21:13 -060086 * @brief Get the object map for creating or updating an object property
87 *
88 * @param[in] path - The dbus object path name
89 * @param[in] intf - The dbus interface
90 * @param[in] prop - The dbus property
91 * @param[in] value - The property value
92 *
93 * @return - The full object path containing the property value
94 */
95template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050096auto getObjMap(const std::string& path, const std::string& intf,
97 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060098{
99 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -0500100 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -0600101 using PropertyMap = std::map<Property, Value>;
102
103 using Interface = std::string;
104 using InterfaceMap = std::map<Interface, PropertyMap>;
105
106 using Object = sdbusplus::message::object_path;
107 using ObjectMap = std::map<Object, InterfaceMap>;
108
109 ObjectMap objectMap;
110 InterfaceMap interfaceMap;
111 PropertyMap propertyMap;
112
113 propertyMap.emplace(prop, value);
114 interfaceMap.emplace(intf, std::move(propertyMap));
115 objectMap.emplace(path, std::move(interfaceMap));
116
117 return objectMap;
118}
119
Matthew Barth9e80c872020-05-26 10:50:29 -0500120} // namespace util
121} // namespace fan
122} // namespace phosphor