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