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 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 12 | using namespace phosphor::logging; |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 13 | using InternalFailure = |
| 14 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 15 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace fan |
| 19 | { |
Matt Spinler | 5cfdf94 | 2017-04-10 14:25:47 -0500 | [diff] [blame] | 20 | namespace util |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 21 | { |
| 22 | |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 23 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 24 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 25 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 26 | |
| 27 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 28 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 29 | |
| 30 | constexpr auto OPERATIONAL_STATUS_INTF = |
| 31 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; |
| 32 | constexpr auto FUNCTIONAL_PROPERTY = "Functional"; |
| 33 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 34 | class FileDescriptor |
| 35 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 36 | public: |
| 37 | FileDescriptor() = delete; |
| 38 | FileDescriptor(const FileDescriptor&) = delete; |
| 39 | FileDescriptor(FileDescriptor&&) = default; |
| 40 | FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 41 | FileDescriptor& operator=(FileDescriptor&&) = default; |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 42 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 43 | FileDescriptor(int fd) : fd(fd) |
| 44 | {} |
| 45 | |
| 46 | ~FileDescriptor() |
| 47 | { |
| 48 | if (fd != -1) |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 49 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 50 | close(fd); |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 51 | } |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 52 | } |
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 | int operator()() |
| 55 | { |
| 56 | return fd; |
| 57 | } |
| 58 | |
| 59 | void open(const std::string& pathname, int flags) |
| 60 | { |
| 61 | fd = ::open(pathname.c_str(), flags); |
| 62 | if (-1 == fd) |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 63 | { |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 64 | log<level::ERR>("Failed to open file device: ", |
| 65 | entry("PATHNAME=%s", pathname.c_str())); |
| 66 | elog<InternalFailure>(); |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 67 | } |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 68 | } |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 69 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 70 | bool is_open() |
| 71 | { |
| 72 | return fd != -1; |
| 73 | } |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 74 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 75 | private: |
| 76 | int fd = -1; |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 79 | /** |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 80 | * @brief Get the object map for creating or updating an object property |
| 81 | * |
| 82 | * @param[in] path - The dbus object path name |
| 83 | * @param[in] intf - The dbus interface |
| 84 | * @param[in] prop - The dbus property |
| 85 | * @param[in] value - The property value |
| 86 | * |
| 87 | * @return - The full object path containing the property value |
| 88 | */ |
| 89 | template <typename T> |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 90 | auto getObjMap(const std::string& path, const std::string& intf, |
| 91 | const std::string& prop, const T& value) |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 92 | { |
| 93 | using Property = std::string; |
Patrick Williams | c21d0b3 | 2020-05-13 17:55:14 -0500 | [diff] [blame] | 94 | using Value = std::variant<T>; |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 95 | using PropertyMap = std::map<Property, Value>; |
| 96 | |
| 97 | using Interface = std::string; |
| 98 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 99 | |
| 100 | using Object = sdbusplus::message::object_path; |
| 101 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 102 | |
| 103 | ObjectMap objectMap; |
| 104 | InterfaceMap interfaceMap; |
| 105 | PropertyMap propertyMap; |
| 106 | |
| 107 | propertyMap.emplace(prop, value); |
| 108 | interfaceMap.emplace(intf, std::move(propertyMap)); |
| 109 | objectMap.emplace(path, std::move(interfaceMap)); |
| 110 | |
| 111 | return objectMap; |
| 112 | } |
| 113 | |
Matthew Barth | 9e80c87 | 2020-05-26 10:50:29 -0500 | [diff] [blame^] | 114 | } // namespace util |
| 115 | } // namespace fan |
| 116 | } // namespace phosphor |