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