Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 1 | #pragma once |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 2 | #include <string> |
| 3 | #include <libevdev/libevdev.h> |
| 4 | #include "file.hpp" |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace gpio |
| 9 | { |
| 10 | namespace presence |
| 11 | { |
| 12 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 13 | /* Need a custom deleter for freeing up evdev struct */ |
| 14 | struct FreeEvDev |
| 15 | { |
| 16 | void operator()(struct libevdev* device) const |
| 17 | { |
| 18 | libevdev_free(device); |
| 19 | } |
| 20 | }; |
| 21 | using EvdevPtr = std::unique_ptr<struct libevdev, FreeEvDev>; |
| 22 | |
| 23 | /** @class Presence |
| 24 | * @brief Responsible for determining and monitoring presence of |
| 25 | * inventory items and updating D-Bus accordingly. |
| 26 | */ |
| 27 | class Presence |
| 28 | { |
| 29 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 30 | using Property = std::string; |
| 31 | using Value = sdbusplus::message::variant<bool, std::string>; |
| 32 | // Association between property and its value |
| 33 | using PropertyMap = std::map<Property, Value>; |
| 34 | using Interface = std::string; |
| 35 | // Association between interface and the D-Bus property |
| 36 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 37 | using Object = sdbusplus::message::object_path; |
| 38 | // Association between object and the interface |
| 39 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 40 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 41 | public: |
| 42 | Presence() = delete; |
| 43 | ~Presence() = default; |
| 44 | Presence(const Presence&) = delete; |
| 45 | Presence& operator=(const Presence&) = delete; |
| 46 | Presence(Presence&&) = delete; |
| 47 | Presence& operator=(Presence&&) = delete; |
| 48 | |
| 49 | /** @brief Constructs Presence object. |
| 50 | * |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 51 | * @param[in] bus - D-Bus bus Object |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 52 | * @param[in] inventory - Object path under inventory |
| 53 | to display this inventory item |
| 54 | * @param[in] path - Device path to read for GPIO pin state |
| 55 | to determine presence of inventory item |
| 56 | * @param[in] key - GPIO key to monitor |
| 57 | * @param[in] name - Pretty name of the inventory item |
| 58 | */ |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 59 | Presence(sdbusplus::bus::bus& bus, |
| 60 | const std::string& inventory, |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 61 | const std::string& path, |
| 62 | const unsigned int key, |
| 63 | const std::string& name) : |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 64 | bus(bus), |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 65 | inventory(inventory), |
| 66 | path(path), |
| 67 | key(key), |
| 68 | name(name), |
| 69 | fd(openDevice()) |
| 70 | { |
| 71 | initEvDev(); |
| 72 | determinePresence(); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | /** |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 77 | * @brief Update the present property for the inventory item. |
| 78 | * |
| 79 | * @param[in] present - What the present property should be set to. |
| 80 | */ |
| 81 | void updateInventory(bool present); |
| 82 | |
| 83 | /** |
| 84 | * @brief Construct the inventory object map for the inventory item. |
| 85 | * |
| 86 | * @param[in] present - What the present property should be set to. |
| 87 | * |
| 88 | * @return The inventory object map to update inventory |
| 89 | */ |
| 90 | ObjectMap getObjectMap(bool present); |
| 91 | |
| 92 | /** @brief Connection for sdbusplus bus */ |
| 93 | sdbusplus::bus::bus& bus; |
| 94 | |
| 95 | /** |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 96 | * @brief Read the GPIO device to determine initial presence and set |
| 97 | * present property at D-Bus path. |
| 98 | **/ |
| 99 | void determinePresence(); |
| 100 | |
| 101 | /** @brief Object path under inventory to display this inventory item */ |
| 102 | const std::string inventory; |
| 103 | |
| 104 | /** @brief Device path to read for GPIO pin state |
| 105 | to determine presence of inventory item */ |
| 106 | const std::string path; |
| 107 | |
| 108 | /** @brief GPIO key to monitor */ |
| 109 | const unsigned int key; |
| 110 | |
| 111 | /** @brief Pretty name of the inventory item*/ |
| 112 | const std::string name; |
| 113 | |
| 114 | /** @brief Event structure */ |
| 115 | EvdevPtr devicePtr; |
| 116 | |
| 117 | /** @brief Opens the device and populates the descriptor */ |
| 118 | int openDevice(); |
| 119 | |
| 120 | /** @brief File descriptor manager */ |
| 121 | FileDescriptor fd; |
| 122 | |
| 123 | /** @brief Initializes evdev handle with the fd */ |
| 124 | void initEvDev(); |
| 125 | }; |
| 126 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame^] | 127 | /** |
| 128 | * @brief Get the service name from the mapper for the |
| 129 | * interface and path passed in. |
| 130 | * |
| 131 | * @param[in] path - The D-Bus path name |
| 132 | * @param[in] interface - The D-Bus interface name |
| 133 | * @param[in] bus - The D-Bus bus object |
| 134 | * |
| 135 | * @return The service name |
| 136 | */ |
| 137 | std::string getService(const std::string& path, |
| 138 | const std::string& interface, |
| 139 | sdbusplus::bus::bus& bus); |
| 140 | |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 141 | } // namespace presence |
| 142 | } // namespace gpio |
| 143 | } // namespace phosphor |
| 144 | |