Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <sdbusplus/bus.hpp> |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 4 | #include <unistd.h> |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 5 | #include <fcntl.h> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include <phosphor-logging/elog.hpp> |
| 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 10 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 11 | |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 12 | using namespace phosphor::logging; |
| 13 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 14 | Error::InternalFailure; |
| 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 | { |
| 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; |
| 42 | |
| 43 | FileDescriptor(int fd) : fd(fd) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | ~FileDescriptor() |
| 48 | { |
| 49 | if (fd != -1) |
| 50 | { |
| 51 | close(fd); |
| 52 | } |
| 53 | } |
| 54 | |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 55 | int operator()() |
| 56 | { |
| 57 | return fd; |
| 58 | } |
| 59 | |
| 60 | void open(const std::string& pathname, int flags) |
| 61 | { |
| 62 | fd = ::open(pathname.c_str(), flags); |
| 63 | if (-1 == fd) |
| 64 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 65 | log<level::ERR>( |
| 66 | "Failed to open file device: ", |
| 67 | entry("PATHNAME=%s", pathname.c_str())); |
| 68 | elog<InternalFailure>(); |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 72 | bool is_open() |
| 73 | { |
| 74 | return fd != -1; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | int fd = -1; |
| 79 | |
| 80 | }; |
| 81 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 82 | /** |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 83 | * @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 | */ |
| 92 | template <typename T> |
| 93 | auto getObjMap(const std::string& path, |
| 94 | const std::string& intf, |
| 95 | const std::string& prop, |
| 96 | const T& value) |
| 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 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |