Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 2 | #include "evdev.hpp" |
| 3 | |
| 4 | #include <systemd/sd-event.h> |
| 5 | |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 6 | #include <experimental/filesystem> |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 7 | #include <string> |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace gpio |
| 12 | { |
| 13 | namespace presence |
| 14 | { |
| 15 | |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 16 | static constexpr auto deviceField = 0; |
| 17 | static constexpr auto pathField = 1; |
| 18 | using Device = std::string; |
| 19 | using Path = std::experimental::filesystem::path; |
| 20 | using Driver = std::tuple<Device, Path>; |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 21 | using Interface = std::string; |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 22 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 23 | /** @class Presence |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 24 | * @brief Responsible for determining and monitoring presence, |
| 25 | * by monitoring GPIO state changes, of inventory items and |
| 26 | * updating D-Bus accordingly. |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 27 | */ |
Gunnar Mills | 835dfb8 | 2017-07-26 16:15:21 -0500 | [diff] [blame] | 28 | class Presence : public Evdev |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 29 | { |
| 30 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 31 | using Property = std::string; |
| 32 | using Value = sdbusplus::message::variant<bool, std::string>; |
| 33 | // Association between property and its value |
| 34 | using PropertyMap = std::map<Property, Value>; |
| 35 | using Interface = std::string; |
| 36 | // Association between interface and the D-Bus property |
| 37 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 38 | using Object = sdbusplus::message::object_path; |
| 39 | // Association between object and the interface |
| 40 | using ObjectMap = std::map<Object, InterfaceMap>; |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 41 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 42 | public: |
| 43 | Presence() = delete; |
| 44 | ~Presence() = default; |
| 45 | Presence(const Presence&) = delete; |
| 46 | Presence& operator=(const Presence&) = delete; |
| 47 | Presence(Presence&&) = delete; |
| 48 | Presence& operator=(Presence&&) = delete; |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 49 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 50 | /** @brief Constructs Presence object. |
| 51 | * |
| 52 | * @param[in] bus - D-Bus bus Object |
| 53 | * @param[in] inventory - Object path under inventory |
| 54 | to display this inventory item |
| 55 | * @param[in] path - Device path to read for GPIO pin state |
| 56 | to determine presence of inventory item |
| 57 | * @param[in] key - GPIO key to monitor |
| 58 | * @param[in] name - Pretty name of the inventory item |
| 59 | * @param[in] event - sd_event handler |
| 60 | * @param[in] drivers - list of device drivers to bind and unbind |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 61 | * @param[in] ifaces - list of extra interfaces to associate with the |
| 62 | * inventory item |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 63 | * @param[in] handler - IO callback handler. Defaults to one in this |
| 64 | * class |
| 65 | */ |
| 66 | Presence(sdbusplus::bus::bus& bus, const std::string& inventory, |
| 67 | const std::string& path, const unsigned int key, |
| 68 | const std::string& name, EventPtr& event, |
| 69 | const std::vector<Driver>& drivers, |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 70 | const std::vector<Interface>& ifaces, |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 71 | sd_event_io_handler_t handler = Presence::processEvents) : |
| 72 | Evdev(path, key, event, handler, true), |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 73 | bus(bus), inventory(inventory), name(name), drivers(drivers), |
| 74 | ifaces(ifaces) |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 75 | { |
| 76 | determinePresence(); |
| 77 | } |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 78 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 79 | /** @brief Callback handler when the FD has some activity on it |
| 80 | * |
| 81 | * @param[in] es - Populated event source |
| 82 | * @param[in] fd - Associated File descriptor |
| 83 | * @param[in] revents - Type of event |
| 84 | * @param[in] userData - User data that was passed during registration |
| 85 | * |
| 86 | * @return - 0 or positive number on success and negative |
| 87 | * errno otherwise |
| 88 | */ |
| 89 | static int processEvents(sd_event_source* es, int fd, uint32_t revents, |
| 90 | void* userData); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 91 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 92 | private: |
| 93 | /** |
| 94 | * @brief Update the present property for the inventory item. |
| 95 | * |
| 96 | * @param[in] present - What the present property should be set to. |
| 97 | */ |
| 98 | void updateInventory(bool present); |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 99 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 100 | /** |
| 101 | * @brief Construct the inventory object map for the inventory item. |
| 102 | * |
| 103 | * @param[in] present - What the present property should be set to. |
| 104 | * |
| 105 | * @return The inventory object map to update inventory |
| 106 | */ |
| 107 | ObjectMap getObjectMap(bool present); |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 108 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 109 | /** @brief Connection for sdbusplus bus */ |
| 110 | sdbusplus::bus::bus& bus; |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 111 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 112 | /** |
| 113 | * @brief Read the GPIO device to determine initial presence and set |
| 114 | * present property at D-Bus path. |
| 115 | */ |
| 116 | void determinePresence(); |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 117 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 118 | /** @brief Object path under inventory to display this inventory item */ |
| 119 | const std::string inventory; |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 120 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 121 | /** @brief Pretty name of the inventory item*/ |
| 122 | const std::string name; |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 123 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 124 | /** @brief Analyzes the GPIO event and update present property*/ |
| 125 | void analyzeEvent(); |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 126 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 127 | /** @brief Vector of path and device tuples to bind/unbind*/ |
| 128 | const std::vector<Driver> drivers; |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 129 | |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 130 | /** @brief Vector of extra inventory interfaces to associate with the |
| 131 | * inventory item |
| 132 | */ |
| 133 | const std::vector<Interface> ifaces; |
| 134 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 135 | /** |
| 136 | * @brief Binds or unbinds drivers |
| 137 | * |
| 138 | * Called when a presence change is detected to either |
| 139 | * bind the drivers for the new card or unbind them for |
| 140 | * the just removed card. Operates on the drivers vector. |
| 141 | * |
| 142 | * Writes <device> to <path>/bind (or unbind) |
| 143 | * |
| 144 | * @param present - when true, will bind the drivers |
| 145 | * when false, will unbind them |
| 146 | */ |
| 147 | void bindOrUnbindDrivers(bool present); |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 150 | /** |
| 151 | * @brief Get the service name from the mapper for the |
| 152 | * interface and path passed in. |
| 153 | * |
| 154 | * @param[in] path - The D-Bus path name |
| 155 | * @param[in] interface - The D-Bus interface name |
| 156 | * @param[in] bus - The D-Bus bus object |
| 157 | * |
| 158 | * @return The service name |
| 159 | */ |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 160 | std::string getService(const std::string& path, const std::string& interface, |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 161 | sdbusplus::bus::bus& bus); |
| 162 | |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 163 | } // namespace presence |
| 164 | } // namespace gpio |
| 165 | } // namespace phosphor |