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