blob: a55a52f3812b972b4549bb88e98f6658bf926409 [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
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";
Mike Cappsab509252022-01-10 21:56:11 -050029constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager";
Matthew Barth51dd1852017-11-16 15:21:13 -060030
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
Patrick Williams61b73292023-05-10 07:50:12 -050047 explicit FileDescriptor(int fd) : fd(fd) {}
Matthew Barth9e80c872020-05-26 10:50:29 -050048
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 {
Anwaar Hadi32c4fef2025-04-02 16:08:27 +000067 lg2::error("Failed to open file device path {PATH}", "PATH",
68 pathname);
Matthew Barth9e80c872020-05-26 10:50:29 -050069 elog<InternalFailure>();
Brandon Wymanfef02952017-03-31 18:13:21 -050070 }
Matthew Barth9e80c872020-05-26 10:50:29 -050071 }
Brandon Wymanfef02952017-03-31 18:13:21 -050072
Matthew Barth9e80c872020-05-26 10:50:29 -050073 bool is_open()
74 {
75 return fd != -1;
76 }
Brandon Wymance822442017-04-10 17:48:02 -050077
Matthew Barth9e80c872020-05-26 10:50:29 -050078 private:
79 int fd = -1;
Brandon Wymanfef02952017-03-31 18:13:21 -050080};
81
Brandon Wyman5914f652017-03-16 18:17:07 -050082/**
Matthew Barth51dd1852017-11-16 15:21:13 -060083 * @brief Get the object map for creating or updating an object property
84 *
85 * @param[in] path - The dbus object path name
86 * @param[in] intf - The dbus interface
87 * @param[in] prop - The dbus property
88 * @param[in] value - The property value
89 *
90 * @return - The full object path containing the property value
91 */
92template <typename T>
Matthew Barth9e80c872020-05-26 10:50:29 -050093auto getObjMap(const std::string& path, const std::string& intf,
94 const std::string& prop, const T& value)
Matthew Barth51dd1852017-11-16 15:21:13 -060095{
96 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050097 using Value = std::variant<T>;
Matthew Barth51dd1852017-11-16 15:21:13 -060098 using PropertyMap = std::map<Property, Value>;
99
100 using Interface = std::string;
101 using InterfaceMap = std::map<Interface, PropertyMap>;
102
103 using Object = sdbusplus::message::object_path;
104 using ObjectMap = std::map<Object, InterfaceMap>;
105
106 ObjectMap objectMap;
107 InterfaceMap interfaceMap;
108 PropertyMap propertyMap;
109
110 propertyMap.emplace(prop, value);
111 interfaceMap.emplace(intf, std::move(propertyMap));
112 objectMap.emplace(path, std::move(interfaceMap));
113
114 return objectMap;
115}
116
Matthew Barth9e80c872020-05-26 10:50:29 -0500117} // namespace util
118} // namespace fan
119} // namespace phosphor