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> |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/elog-errors.hpp> |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog.hpp> |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <sdbusplus/bus.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 11 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 12 | #include <format> |
| 13 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 14 | using namespace phosphor::logging; |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 15 | using InternalFailure = |
| 16 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 17 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace fan |
| 21 | { |
Matt Spinler | 5cfdf94 | 2017-04-10 14:25:47 -0500 | [diff] [blame] | 22 | namespace util |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 23 | { |
| 24 | |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 25 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 26 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 27 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 28 | |
| 29 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 30 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
Mike Capps | ab50925 | 2022-01-10 21:56:11 -0500 | [diff] [blame] | 31 | constexpr auto INVENTORY_SVC = "xyz.openbmc_project.Inventory.Manager"; |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 32 | |
| 33 | constexpr auto OPERATIONAL_STATUS_INTF = |
| 34 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; |
| 35 | constexpr auto FUNCTIONAL_PROPERTY = "Functional"; |
| 36 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 37 | constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item"; |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 38 | constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 39 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 40 | class FileDescriptor |
| 41 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 42 | 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 Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 48 | |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 49 | explicit FileDescriptor(int fd) : fd(fd) {} |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 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>( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 70 | std::format("Failed to open file device path {}", pathname) |
Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 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 |