| Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 3 | #include <fcntl.h> | 
| Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 4 | #include <fmt/format.h> | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 5 | #include <unistd.h> | 
|  | 6 |  | 
| Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog.hpp> | 
|  | 9 | #include <phosphor-logging/log.hpp> | 
|  | 10 | #include <sdbusplus/bus.hpp> | 
| Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> | 
| Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 12 |  | 
| Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 13 | using namespace phosphor::logging; | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 14 | using InternalFailure = | 
|  | 15 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; | 
| Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 16 |  | 
| Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 17 | namespace phosphor | 
|  | 18 | { | 
|  | 19 | namespace fan | 
|  | 20 | { | 
| Matt Spinler | 5cfdf94 | 2017-04-10 14:25:47 -0500 | [diff] [blame] | 21 | namespace util | 
| Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 22 | { | 
|  | 23 |  | 
| Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 24 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; | 
|  | 25 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; | 
|  | 26 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; | 
|  | 27 |  | 
|  | 28 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; | 
|  | 29 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; | 
| Mike Capps | ab50925 | 2022-01-10 21:56:11 -0500 | [diff] [blame] | 30 | constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager"; | 
| Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 31 |  | 
|  | 32 | constexpr auto OPERATIONAL_STATUS_INTF = | 
|  | 33 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; | 
|  | 34 | constexpr auto FUNCTIONAL_PROPERTY = "Functional"; | 
|  | 35 |  | 
| Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 36 | constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item"; | 
| Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 37 | constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; | 
| Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 38 |  | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 39 | class FileDescriptor | 
|  | 40 | { | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 41 | public: | 
|  | 42 | FileDescriptor() = delete; | 
|  | 43 | FileDescriptor(const FileDescriptor&) = delete; | 
|  | 44 | FileDescriptor(FileDescriptor&&) = default; | 
|  | 45 | FileDescriptor& operator=(const FileDescriptor&) = delete; | 
|  | 46 | FileDescriptor& operator=(FileDescriptor&&) = default; | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 47 |  | 
| Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 48 | explicit FileDescriptor(int fd) : fd(fd) | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 49 | {} | 
|  | 50 |  | 
|  | 51 | ~FileDescriptor() | 
|  | 52 | { | 
|  | 53 | if (fd != -1) | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 54 | { | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 55 | close(fd); | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 56 | } | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 57 | } | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 58 |  | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 59 | 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 Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 68 | { | 
| Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 69 | log<level::ERR>( | 
|  | 70 | fmt::format("Failed to open file device path {}", pathname) | 
|  | 71 | .c_str()); | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 72 | elog<InternalFailure>(); | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 73 | } | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 74 | } | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 75 |  | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 76 | bool is_open() | 
|  | 77 | { | 
|  | 78 | return fd != -1; | 
|  | 79 | } | 
| Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 80 |  | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 81 | private: | 
|  | 82 | int fd = -1; | 
| Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
| Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 85 | /** | 
| Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 86 | * @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 | */ | 
|  | 95 | template <typename T> | 
| Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 96 | auto getObjMap(const std::string& path, const std::string& intf, | 
|  | 97 | const std::string& prop, const T& value) | 
| Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 98 | { | 
|  | 99 | using Property = std::string; | 
| Patrick Williams | c21d0b3 | 2020-05-13 17:55:14 -0500 | [diff] [blame] | 100 | using Value = std::variant<T>; | 
| Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 101 | 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 Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 120 | } // namespace util | 
|  | 121 | } // namespace fan | 
|  | 122 | } // namespace phosphor |