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> |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 4 | #include "evdev.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 | /** @class Presence |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 14 | * @brief Responsible for determining and monitoring presence, |
| 15 | * by monitoring GPIO state changes, of inventory items and |
| 16 | * updating D-Bus accordingly. |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 17 | */ |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 18 | class Presence : public Evdev |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 19 | { |
| 20 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 21 | using Property = std::string; |
| 22 | using Value = sdbusplus::message::variant<bool, std::string>; |
| 23 | // Association between property and its value |
| 24 | using PropertyMap = std::map<Property, Value>; |
| 25 | using Interface = std::string; |
| 26 | // Association between interface and the D-Bus property |
| 27 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 28 | using Object = sdbusplus::message::object_path; |
| 29 | // Association between object and the interface |
| 30 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 31 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 32 | public: |
| 33 | Presence() = delete; |
| 34 | ~Presence() = default; |
| 35 | Presence(const Presence&) = delete; |
| 36 | Presence& operator=(const Presence&) = delete; |
| 37 | Presence(Presence&&) = delete; |
| 38 | Presence& operator=(Presence&&) = delete; |
| 39 | |
| 40 | /** @brief Constructs Presence object. |
| 41 | * |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 42 | * @param[in] bus - D-Bus bus Object |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 43 | * @param[in] inventory - Object path under inventory |
| 44 | to display this inventory item |
| 45 | * @param[in] path - Device path to read for GPIO pin state |
| 46 | to determine presence of inventory item |
| 47 | * @param[in] key - GPIO key to monitor |
| 48 | * @param[in] name - Pretty name of the inventory item |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 49 | * @param[in] event - sd_event handler |
| 50 | * @param[in] handler - IO callback handler. Defaults to one in this |
| 51 | * class |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 52 | */ |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 53 | Presence(sdbusplus::bus::bus& bus, |
| 54 | const std::string& inventory, |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 55 | const std::string& path, |
| 56 | const unsigned int key, |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 57 | const std::string& name, |
| 58 | EventPtr& event, |
| 59 | sd_event_io_handler_t handler = Presence::processEvents) : |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 60 | Evdev(path, key, event, handler, true), |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 61 | bus(bus), |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 62 | inventory(inventory), |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 63 | name(name) |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 64 | { |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 65 | determinePresence(); |
| 66 | } |
| 67 | |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 68 | /** @brief Callback handler when the FD has some activity on it |
| 69 | * |
| 70 | * @param[in] es - Populated event source |
| 71 | * @param[in] fd - Associated File descriptor |
| 72 | * @param[in] revents - Type of event |
| 73 | * @param[in] userData - User data that was passed during registration |
| 74 | * |
| 75 | * @return - 0 or positive number on success and negative |
| 76 | * errno otherwise |
| 77 | */ |
| 78 | static int processEvents(sd_event_source* es, int fd, |
| 79 | uint32_t revents, void* userData); |
| 80 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 81 | private: |
| 82 | /** |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 83 | * @brief Update the present property for the inventory item. |
| 84 | * |
| 85 | * @param[in] present - What the present property should be set to. |
| 86 | */ |
| 87 | void updateInventory(bool present); |
| 88 | |
| 89 | /** |
| 90 | * @brief Construct the inventory object map for the inventory item. |
| 91 | * |
| 92 | * @param[in] present - What the present property should be set to. |
| 93 | * |
| 94 | * @return The inventory object map to update inventory |
| 95 | */ |
| 96 | ObjectMap getObjectMap(bool present); |
| 97 | |
| 98 | /** @brief Connection for sdbusplus bus */ |
| 99 | sdbusplus::bus::bus& bus; |
| 100 | |
| 101 | /** |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 102 | * @brief Read the GPIO device to determine initial presence and set |
| 103 | * present property at D-Bus path. |
| 104 | **/ |
| 105 | void determinePresence(); |
| 106 | |
| 107 | /** @brief Object path under inventory to display this inventory item */ |
| 108 | const std::string inventory; |
| 109 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 110 | /** @brief Pretty name of the inventory item*/ |
| 111 | const std::string name; |
| 112 | |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 113 | /** @brief Analyzes the GPIO event and update present property*/ |
| 114 | void analyzeEvent(); |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 115 | }; |
| 116 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 117 | /** |
| 118 | * @brief Get the service name from the mapper for the |
| 119 | * interface and path passed in. |
| 120 | * |
| 121 | * @param[in] path - The D-Bus path name |
| 122 | * @param[in] interface - The D-Bus interface name |
| 123 | * @param[in] bus - The D-Bus bus object |
| 124 | * |
| 125 | * @return The service name |
| 126 | */ |
| 127 | std::string getService(const std::string& path, |
| 128 | const std::string& interface, |
| 129 | sdbusplus::bus::bus& bus); |
| 130 | |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 131 | } // namespace presence |
| 132 | } // namespace gpio |
| 133 | } // namespace phosphor |
| 134 | |