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 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 23 | class FileDescriptor |
| 24 | { |
| 25 | public: |
| 26 | FileDescriptor() = delete; |
| 27 | FileDescriptor(const FileDescriptor&) = delete; |
| 28 | FileDescriptor(FileDescriptor&&) = default; |
| 29 | FileDescriptor& operator=(const FileDescriptor&) = delete; |
| 30 | FileDescriptor& operator=(FileDescriptor&&) = default; |
| 31 | |
| 32 | FileDescriptor(int fd) : fd(fd) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | ~FileDescriptor() |
| 37 | { |
| 38 | if (fd != -1) |
| 39 | { |
| 40 | close(fd); |
| 41 | } |
| 42 | } |
| 43 | |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 44 | int operator()() |
| 45 | { |
| 46 | return fd; |
| 47 | } |
| 48 | |
| 49 | void open(const std::string& pathname, int flags) |
| 50 | { |
| 51 | fd = ::open(pathname.c_str(), flags); |
| 52 | if (-1 == fd) |
| 53 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 54 | log<level::ERR>( |
| 55 | "Failed to open file device: ", |
| 56 | entry("PATHNAME=%s", pathname.c_str())); |
| 57 | elog<InternalFailure>(); |
Brandon Wyman | ce82244 | 2017-04-10 17:48:02 -0500 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Brandon Wyman | fef0295 | 2017-03-31 18:13:21 -0500 | [diff] [blame] | 61 | bool is_open() |
| 62 | { |
| 63 | return fd != -1; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | int fd = -1; |
| 68 | |
| 69 | }; |
| 70 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 71 | /** |
| 72 | * @brief Get the inventory service name from the mapper object |
| 73 | * |
| 74 | * @return The inventory manager service name |
| 75 | */ |
| 76 | std::string getInvService(sdbusplus::bus::bus& bus); |
| 77 | |
Matt Spinler | 5cfdf94 | 2017-04-10 14:25:47 -0500 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Get the service name from the mapper for the |
| 81 | * interface and path passed in. |
| 82 | * |
| 83 | * @param[in] path - the dbus path name |
| 84 | * @param[in] interface - the dbus interface name |
| 85 | * @param[in] bus - the dbus object |
| 86 | * |
| 87 | * @return The service name |
| 88 | */ |
| 89 | std::string getService(const std::string& path, |
| 90 | const std::string& interface, |
| 91 | sdbusplus::bus::bus& bus); |
| 92 | |
Brandon Wyman | 5914f65 | 2017-03-16 18:17:07 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |