blob: c37df98022a5d0c8752843e965c27b943c6f2c84 [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>
Anwaar Hadi32c4fef2025-04-02 16:08:27 +00008#include <phosphor-logging/lg2.hpp>
Matthew Barth9e80c872020-05-26 10:50:29 -05009#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
Matthew Barth9e80c872020-05-26 10:50:29 -050012using InternalFailure =
13 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Dinesh Chinari618027a2017-06-26 23:26:50 -050014
Brandon Wyman5914f652017-03-16 18:17:07 -050015namespace phosphor
16{
17namespace fan
18{
Matt Spinler5cfdf942017-04-10 14:25:47 -050019namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050020{
21
Matthew Barth51dd1852017-11-16 15:21:13 -060022constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
23constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
24constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
25
26constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
27constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
Mike Cappsab509252022-01-10 21:56:11 -050028constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager";
Matthew Barth51dd1852017-11-16 15:21:13 -060029
30constexpr auto OPERATIONAL_STATUS_INTF =
31 "xyz.openbmc_project.State.Decorator.OperationalStatus";
32constexpr auto FUNCTIONAL_PROPERTY = "Functional";
33
Matt Spinlerb63aa092020-10-14 09:45:11 -050034constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item";
Mike Cappsfdcd5db2021-05-20 12:47:10 -040035constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
Matt Spinlerb63aa092020-10-14 09:45:11 -050036
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
Patrick Williams61b73292023-05-10 07:50:12 -050046 explicit FileDescriptor(int fd) : fd(fd) {}
Matthew Barth9e80c872020-05-26 10:50:29 -050047
48 ~FileDescriptor()
49 {
50 if (fd != -1)
Brandon Wymanfef02952017-03-31 18:13:21 -050051 {
Matthew Barth9e80c872020-05-26 10:50:29 -050052 close(fd);
Brandon Wymanfef02952017-03-31 18:13:21 -050053 }
Matthew Barth9e80c872020-05-26 10:50:29 -050054 }
Brandon Wymanfef02952017-03-31 18:13:21 -050055
Matthew Barth9e80c872020-05-26 10:50:29 -050056 int operator()()
57 {
58 return fd;
59 }
60
61 void open(const std::string& pathname, int flags)
62 {
63 fd = ::open(pathname.c_str(), flags);
64 if (-1 == fd)
Brandon Wymanfef02952017-03-31 18:13:21 -050065 {
Anwaar Hadi32c4fef2025-04-02 16:08:27 +000066 lg2::error("Failed to open file device path {PATH}", "PATH",
67 pathname);
Anwaar Hadi9d533802025-04-09 19:08:51 +000068 phosphor::logging::elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050069 }
Matthew Barth9e80c872020-05-26 10:50:29 -050070 }
Brandon Wymanfef02952017-03-31 18:13:21 -050071
Matthew Barth9e80c872020-05-26 10:50:29 -050072 bool is_open()
73 {
74 return fd != -1;
75 }
Brandon Wymance822442017-04-10 17:48:02 -050076
Matthew Barth9e80c872020-05-26 10:50:29 -050077 private:
78 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050079};
80
Brandon Wyman5914f652017-03-16 18:17:07 -050081/**
Matthew Barth51dd1852017-11-16 15:21:13 -060082 * @brief Get the object map for creating or updating an object property
83 *
84 * @param[in] path - The dbus object path name
85 * @param[in] intf - The dbus interface
86 * @param[in] prop - The dbus property
87 * @param[in] value - The property value
88 *
89 * @return - The full object path containing the property value
90 */
91template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050092auto getObjMap(const std::string& path, const std::string& intf,
93 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060094{
95 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050096 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -060097 using PropertyMap = std::map<Property, Value>;
98
99 using Interface = std::string;
100 using InterfaceMap = std::map<Interface, PropertyMap>;
101
102 using Object = sdbusplus::message::object_path;
103 using ObjectMap = std::map<Object, InterfaceMap>;
104
105 ObjectMap objectMap;
106 InterfaceMap interfaceMap;
107 PropertyMap propertyMap;
108
109 propertyMap.emplace(prop, value);
110 interfaceMap.emplace(intf, std::move(propertyMap));
111 objectMap.emplace(path, std::move(interfaceMap));
112
113 return objectMap;
114}
115
Matthew Barth9e80c872020-05-26 10:50:29 -0500116} // namespace util
117} // namespace fan
118} // namespace phosphor