| 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 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 4 | #include "gpio_presence.hpp" |
| 5 | |
| 6 | #include "xyz/openbmc_project/Common/error.hpp" |
| 7 | |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 8 | #include <fcntl.h> |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 9 | #include <libevdev/libevdev.h> |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 10 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/elog.hpp> |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 13 | #include <phosphor-logging/lg2.hpp> |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 14 | |
| Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 15 | #include <fstream> |
| 16 | |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace gpio |
| 20 | { |
| 21 | namespace presence |
| 22 | { |
| 23 | |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 24 | using namespace phosphor::logging; |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 26 | |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 27 | constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"; |
| 28 | constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 29 | |
| 30 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 31 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 32 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 33 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 34 | std::string getService(const std::string& path, const std::string& interface, |
| Patrick Williams | bc5b375 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 35 | sdbusplus::bus_t& bus) |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 36 | { |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 37 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 38 | MAPPER_INTERFACE, "GetObject"); |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 39 | |
| 40 | mapperCall.append(path); |
| 41 | mapperCall.append(std::vector<std::string>({interface})); |
| 42 | |
| George Liu | 7abda62 | 2023-08-01 13:54:11 +0800 | [diff] [blame] | 43 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 44 | try |
| 45 | { |
| 46 | auto mapperResponseMsg = bus.call(mapperCall); |
| 47 | mapperResponseMsg.read(mapperResponse); |
| 48 | } |
| 49 | catch (const sdbusplus::exception_t& e) |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 50 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 51 | lg2::error( |
| 52 | "Error in mapper call to get service name, path: {PATH}, interface: {INTERFACE}, error: {ERROR}", |
| 53 | "PATH", path, "INTERFACE", interface, "ERROR", e); |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 54 | elog<InternalFailure>(); |
| 55 | } |
| 56 | |
| 57 | return mapperResponse.begin()->first; |
| 58 | } |
| 59 | |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 60 | void Presence::determinePresence() |
| 61 | { |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 62 | auto present = false; |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 63 | auto value = static_cast<int>(0); |
| Patrick Williams | 8377d59 | 2024-08-16 15:21:08 -0400 | [diff] [blame] | 64 | auto fetch_rc = |
| 65 | libevdev_fetch_event_value(devicePtr.get(), EV_KEY, key, &value); |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 66 | if (0 == fetch_rc) |
| 67 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 68 | lg2::error("Device does not support event type, key: {KEYCODE}", |
| 69 | "KEYCODE", key); |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 70 | elog<InternalFailure>(); |
| 71 | return; |
| 72 | } |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 73 | if (value > 0) |
| 74 | { |
| 75 | present = true; |
| 76 | } |
| 77 | |
| 78 | updateInventory(present); |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 81 | // Callback handler when there is an activity on the FD |
| Brad Bishop | 86d16f0 | 2019-09-19 16:07:33 -0400 | [diff] [blame] | 82 | int Presence::processEvents(sd_event_source*, int, uint32_t, void* userData) |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 83 | { |
| 84 | auto presence = static_cast<Presence*>(userData); |
| 85 | |
| 86 | presence->analyzeEvent(); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 90 | // Analyzes the GPIO event |
| 91 | void Presence::analyzeEvent() |
| 92 | { |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 93 | // Data returned |
| Patrick Williams | 1c88803 | 2024-12-18 11:21:41 -0500 | [diff] [blame] | 94 | struct input_event ev{}; |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 95 | int rc = 0; |
| 96 | |
| 97 | // While testing, observed that not having a loop here was leading |
| 98 | // into events being missed. |
| 99 | while (rc >= 0) |
| 100 | { |
| 101 | // Wait until no more events are available on the device. |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 102 | rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL, |
| 103 | &ev); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 104 | if (rc < 0) |
| 105 | { |
| 106 | // There was an error waiting for events, mostly that there are no |
| 107 | // events to be read.. So continue waiting... |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (rc == LIBEVDEV_READ_STATUS_SUCCESS) |
| 112 | { |
| 113 | if (ev.type == EV_SYN && ev.code == SYN_REPORT) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | else if (ev.code == key) |
| 118 | { |
| 119 | auto present = false; |
| 120 | if (ev.value > 0) |
| 121 | { |
| 122 | present = true; |
| Brandon Wyman | b08a0f6 | 2021-03-03 19:53:18 -0600 | [diff] [blame] | 123 | std::this_thread::sleep_for( |
| 124 | std::chrono::milliseconds(delay)); |
| 125 | bindOrUnbindDrivers(present); |
| 126 | updateInventory(present); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 127 | } |
| Brandon Wyman | b08a0f6 | 2021-03-03 19:53:18 -0600 | [diff] [blame] | 128 | else |
| 129 | { |
| 130 | updateInventory(present); |
| 131 | bindOrUnbindDrivers(present); |
| 132 | } |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return; |
| 138 | } |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 139 | |
| 140 | Presence::ObjectMap Presence::getObjectMap(bool present) |
| 141 | { |
| 142 | ObjectMap invObj; |
| 143 | InterfaceMap invIntf; |
| 144 | PropertyMap invProp; |
| 145 | |
| 146 | invProp.emplace("Present", present); |
| 147 | invProp.emplace("PrettyName", name); |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 148 | invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp)); |
| Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 149 | // Add any extra interfaces we want to associate with the inventory item |
| 150 | for (auto& iface : ifaces) |
| 151 | { |
| 152 | invIntf.emplace(iface, PropertyMap()); |
| 153 | } |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 154 | invObj.emplace(std::move(inventory), std::move(invIntf)); |
| 155 | |
| 156 | return invObj; |
| 157 | } |
| 158 | |
| 159 | void Presence::updateInventory(bool present) |
| 160 | { |
| 161 | ObjectMap invObj = getObjectMap(present); |
| 162 | |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 163 | lg2::info( |
| 164 | "Updating inventory present property value to {PRESENT}, path: {PATH}", |
| 165 | "PRESENT", present, "PATH", inventory); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 166 | |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 167 | auto invService = getService(INVENTORY_PATH, INVENTORY_INTF, bus); |
| 168 | |
| 169 | // Update inventory |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 170 | auto invMsg = bus.new_method_call(invService.c_str(), INVENTORY_PATH, |
| 171 | INVENTORY_INTF, "Notify"); |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 172 | invMsg.append(std::move(invObj)); |
| George Liu | 7abda62 | 2023-08-01 13:54:11 +0800 | [diff] [blame] | 173 | try |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 174 | { |
| George Liu | 7abda62 | 2023-08-01 13:54:11 +0800 | [diff] [blame] | 175 | auto invMgrResponseMsg = bus.call(invMsg); |
| 176 | } |
| 177 | catch (const sdbusplus::exception_t& e) |
| 178 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 179 | lg2::error( |
| 180 | "Error in inventory manager call to update inventory: {ERROR}", |
| 181 | "ERROR", e); |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 182 | elog<InternalFailure>(); |
| 183 | } |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void Presence::bindOrUnbindDrivers(bool present) |
| 187 | { |
| 188 | auto action = (present) ? "bind" : "unbind"; |
| 189 | |
| 190 | for (auto& driver : drivers) |
| 191 | { |
| 192 | auto path = std::get<pathField>(driver) / action; |
| 193 | auto device = std::get<deviceField>(driver); |
| 194 | |
| 195 | if (present) |
| 196 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 197 | lg2::info("Binding a {DEVICE} driver: {PATH}", "DEVICE", device, |
| 198 | "PATH", path); |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 199 | } |
| 200 | else |
| 201 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 202 | lg2::info("Unbinding a {DEVICE} driver: {PATH}", "DEVICE", device, |
| 203 | "PATH", path); |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | std::ofstream file; |
| 207 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 208 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
| 209 | std::ofstream::eofbit); |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 210 | |
| 211 | try |
| 212 | { |
| 213 | file.open(path); |
| 214 | file << device; |
| 215 | file.close(); |
| 216 | } |
| Patrick Williams | 6755414 | 2021-10-06 13:00:15 -0500 | [diff] [blame] | 217 | catch (const std::exception& e) |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 218 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 219 | lg2::error( |
| 220 | "Failed binding or unbinding a {DEVICE} after a card was removed or added, path: {PATH}, error: {ERROR}", |
| 221 | "DEVICE", device, "PATH", path, "ERROR", e); |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 222 | } |
| 223 | } |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 226 | } // namespace presence |
| 227 | } // namespace gpio |
| 228 | } // namespace phosphor |