blob: fd1fc8cb055280c686e5a34206029900a807db62 [file] [log] [blame]
Patrick Venturedace6802018-11-01 16:52:10 -07001#include "gpio_presence.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
Gunnar Mills5f101102017-06-29 13:07:39 -05005#include <fcntl.h>
Matt Spinler902d1c32017-09-01 11:03:02 -05006#include <libevdev/libevdev.h>
Patrick Venturedace6802018-11-01 16:52:10 -07007
Patrick Venturedace6802018-11-01 16:52:10 -07008#include <phosphor-logging/elog-errors.hpp>
Gunnar Mills5f101102017-06-29 13:07:39 -05009#include <phosphor-logging/elog.hpp>
George Liu2a8848c2023-08-01 13:49:28 +080010#include <phosphor-logging/lg2.hpp>
Gunnar Mills72639152017-06-22 15:06:21 -050011
Patrick Williams39084b42023-05-10 07:50:58 -050012#include <fstream>
13
Gunnar Mills72639152017-06-22 15:06:21 -050014namespace phosphor
15{
16namespace gpio
17{
18namespace presence
19{
20
Gunnar Mills5f101102017-06-29 13:07:39 -050021using namespace phosphor::logging;
22using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23
Gunnar Mills80292bb2017-07-05 16:34:51 -050024constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
25constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
26
27constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
28constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
29constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
30
Patrick Venturedace6802018-11-01 16:52:10 -070031std::string getService(const std::string& path, const std::string& interface,
Patrick Williamsbc5b3752022-07-22 19:26:56 -050032 sdbusplus::bus_t& bus)
Gunnar Mills80292bb2017-07-05 16:34:51 -050033{
Patrick Venturedace6802018-11-01 16:52:10 -070034 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
35 MAPPER_INTERFACE, "GetObject");
Gunnar Mills80292bb2017-07-05 16:34:51 -050036
37 mapperCall.append(path);
38 mapperCall.append(std::vector<std::string>({interface}));
39
George Liu7abda622023-08-01 13:54:11 +080040 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 Mills80292bb2017-07-05 16:34:51 -050047 {
George Liu2a8848c2023-08-01 13:49:28 +080048 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 Mills80292bb2017-07-05 16:34:51 -050051 elog<InternalFailure>();
52 }
53
54 return mapperResponse.begin()->first;
55}
56
Gunnar Mills5f101102017-06-29 13:07:39 -050057void Presence::determinePresence()
58{
Gunnar Mills80292bb2017-07-05 16:34:51 -050059 auto present = false;
Gunnar Mills5f101102017-06-29 13:07:39 -050060 auto value = static_cast<int>(0);
Patrick Williams39084b42023-05-10 07:50:58 -050061 auto fetch_rc = libevdev_fetch_event_value(devicePtr.get(), EV_KEY, key,
62 &value);
Gunnar Mills5f101102017-06-29 13:07:39 -050063 if (0 == fetch_rc)
64 {
George Liu2a8848c2023-08-01 13:49:28 +080065 lg2::error("Device does not support event type, key: {KEYCODE}",
66 "KEYCODE", key);
Gunnar Mills5f101102017-06-29 13:07:39 -050067 elog<InternalFailure>();
68 return;
69 }
Gunnar Mills80292bb2017-07-05 16:34:51 -050070 if (value > 0)
71 {
72 present = true;
73 }
74
75 updateInventory(present);
Gunnar Mills5f101102017-06-29 13:07:39 -050076}
77
Gunnar Mills765725e2017-07-06 14:17:44 -050078// Callback handler when there is an activity on the FD
Brad Bishop86d16f02019-09-19 16:07:33 -040079int Presence::processEvents(sd_event_source*, int, uint32_t, void* userData)
Gunnar Mills765725e2017-07-06 14:17:44 -050080{
81 auto presence = static_cast<Presence*>(userData);
82
83 presence->analyzeEvent();
84 return 0;
85}
86
Gunnar Mills765725e2017-07-06 14:17:44 -050087// Analyzes the GPIO event
88void Presence::analyzeEvent()
89{
Gunnar Mills765725e2017-07-06 14:17:44 -050090 // Data returned
Patrick Venturedace6802018-11-01 16:52:10 -070091 struct input_event ev
Patrick Williams39084b42023-05-10 07:50:58 -050092 {};
Gunnar Mills765725e2017-07-06 14:17:44 -050093 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 Venturedace6802018-11-01 16:52:10 -0700100 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
101 &ev);
Gunnar Mills765725e2017-07-06 14:17:44 -0500102 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 Wymanb08a0f62021-03-03 19:53:18 -0600121 std::this_thread::sleep_for(
122 std::chrono::milliseconds(delay));
123 bindOrUnbindDrivers(present);
124 updateInventory(present);
Gunnar Mills765725e2017-07-06 14:17:44 -0500125 }
Brandon Wymanb08a0f62021-03-03 19:53:18 -0600126 else
127 {
128 updateInventory(present);
129 bindOrUnbindDrivers(present);
130 }
Gunnar Mills765725e2017-07-06 14:17:44 -0500131 }
132 }
133 }
134
135 return;
136}
Gunnar Mills80292bb2017-07-05 16:34:51 -0500137
138Presence::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 Venturedace6802018-11-01 16:52:10 -0700146 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
Anthony Wilson206f0042019-05-02 00:02:23 -0500147 // 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 Mills80292bb2017-07-05 16:34:51 -0500152 invObj.emplace(std::move(inventory), std::move(invIntf));
153
154 return invObj;
155}
156
157void Presence::updateInventory(bool present)
158{
159 ObjectMap invObj = getObjectMap(present);
160
George Liu2a8848c2023-08-01 13:49:28 +0800161 lg2::info(
162 "Updating inventory present property value to {PRESENT}, path: {PATH}",
163 "PRESENT", present, "PATH", inventory);
Gunnar Mills765725e2017-07-06 14:17:44 -0500164
Gunnar Mills80292bb2017-07-05 16:34:51 -0500165 auto invService = getService(INVENTORY_PATH, INVENTORY_INTF, bus);
166
167 // Update inventory
Patrick Venturedace6802018-11-01 16:52:10 -0700168 auto invMsg = bus.new_method_call(invService.c_str(), INVENTORY_PATH,
169 INVENTORY_INTF, "Notify");
Gunnar Mills80292bb2017-07-05 16:34:51 -0500170 invMsg.append(std::move(invObj));
George Liu7abda622023-08-01 13:54:11 +0800171 try
Gunnar Mills80292bb2017-07-05 16:34:51 -0500172 {
George Liu7abda622023-08-01 13:54:11 +0800173 auto invMgrResponseMsg = bus.call(invMsg);
174 }
175 catch (const sdbusplus::exception_t& e)
176 {
George Liu2a8848c2023-08-01 13:49:28 +0800177 lg2::error(
178 "Error in inventory manager call to update inventory: {ERROR}",
179 "ERROR", e);
Gunnar Mills80292bb2017-07-05 16:34:51 -0500180 elog<InternalFailure>();
181 }
Matt Spinler902d1c32017-09-01 11:03:02 -0500182}
183
184void 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 Liu2a8848c2023-08-01 13:49:28 +0800195 lg2::info("Binding a {DEVICE} driver: {PATH}", "DEVICE", device,
196 "PATH", path);
Matt Spinler902d1c32017-09-01 11:03:02 -0500197 }
198 else
199 {
George Liu2a8848c2023-08-01 13:49:28 +0800200 lg2::info("Unbinding a {DEVICE} driver: {PATH}", "DEVICE", device,
201 "PATH", path);
Matt Spinler902d1c32017-09-01 11:03:02 -0500202 }
203
204 std::ofstream file;
205
Patrick Venturedace6802018-11-01 16:52:10 -0700206 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
207 std::ofstream::eofbit);
Matt Spinler902d1c32017-09-01 11:03:02 -0500208
209 try
210 {
211 file.open(path);
212 file << device;
213 file.close();
214 }
Patrick Williams67554142021-10-06 13:00:15 -0500215 catch (const std::exception& e)
Matt Spinler902d1c32017-09-01 11:03:02 -0500216 {
George Liu2a8848c2023-08-01 13:49:28 +0800217 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 Spinler902d1c32017-09-01 11:03:02 -0500220 }
221 }
Gunnar Mills80292bb2017-07-05 16:34:51 -0500222}
223
Gunnar Mills72639152017-06-22 15:06:21 -0500224} // namespace presence
225} // namespace gpio
226} // namespace phosphor