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