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"; |
| 30 | |
| 31 | constexpr auto OPERATIONAL_STATUS_INTF = |
| 32 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; |
| 33 | constexpr auto FUNCTIONAL_PROPERTY = "Functional"; |
| 34 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 35 | constexpr auto INV_ITEM_IFACE = "xyz.openbmc_project.Inventory.Item"; |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame^] | 36 | constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 37 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 38 | class FileDescriptor |
| 39 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 40 | 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 Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 46 | |
Matthew Barth | 9f93bd3 | 2020-05-28 16:57:14 -0500 | [diff] [blame] | 47 | explicit FileDescriptor(int fd) : fd(fd) |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 48 | {} |
| 49 | |
| 50 | ~FileDescriptor() |
| 51 | { |
| 52 | if (fd != -1) |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 53 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 54 | close(fd); |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 55 | } |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 56 | } |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 57 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 58 | int operator()() |
| 59 | { |
| 60 | return fd; |
| 61 | } |
| 62 | |
| 63 | void open(const std::string& pathname, int flags) |
| 64 | { |
| 65 | fd = ::open(pathname.c_str(), flags); |
| 66 | if (-1 == fd) |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 67 | { |
Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 68 | log<level::ERR>( |
| 69 | fmt::format("Failed to open file device path {}", pathname) |
| 70 | .c_str()); |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 71 | elog<InternalFailure>(); |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 72 | } |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 73 | } |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 74 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 75 | bool is_open() |
| 76 | { |
| 77 | return fd != -1; |
| 78 | } |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 79 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 80 | private: |
| 81 | int fd = -1; |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 84 | /** |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 85 | * @brief Get the object map for creating or updating an object property |
| 86 | * |
| 87 | * @param[in] path - The dbus object path name |
| 88 | * @param[in] intf - The dbus interface |
| 89 | * @param[in] prop - The dbus property |
| 90 | * @param[in] value - The property value |
| 91 | * |
| 92 | * @return - The full object path containing the property value |
| 93 | */ |
| 94 | template <typename T> |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 95 | auto getObjMap(const std::string& path, const std::string& intf, |
| 96 | const std::string& prop, const T& value) |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 97 | { |
| 98 | using Property = std::string; |
Patrick Williams | c21d0b3 | 2020-05-13 17:55:14 -0500 | [diff] [blame] | 99 | using Value = std::variant<T>; |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 100 | using PropertyMap = std::map<Property, Value>; |
| 101 | |
| 102 | using Interface = std::string; |
| 103 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 104 | |
| 105 | using Object = sdbusplus::message::object_path; |
| 106 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 107 | |
| 108 | ObjectMap objectMap; |
| 109 | InterfaceMap interfaceMap; |
| 110 | PropertyMap propertyMap; |
| 111 | |
| 112 | propertyMap.emplace(prop, value); |
| 113 | interfaceMap.emplace(intf, std::move(propertyMap)); |
| 114 | objectMap.emplace(path, std::move(interfaceMap)); |
| 115 | |
| 116 | return objectMap; |
| 117 | } |
| 118 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame] | 119 | } // namespace util |
| 120 | } // namespace fan |
| 121 | } // namespace phosphor |