blob: 35326a9838710d091f27c1036e5d944cdd73b46c [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>
10#include <phosphor-logging/log.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 {
48 log<level::ERR>("Error in mapper call to get service name",
49 entry("PATH=%s", path.c_str()),
George Liu7abda622023-08-01 13:54:11 +080050 entry("INTERFACE=%s", interface.c_str()),
51 entry("ERROR=%s", e.what()));
Gunnar Mills80292bb2017-07-05 16:34:51 -050052 elog<InternalFailure>();
53 }
54
55 return mapperResponse.begin()->first;
56}
57
Gunnar Mills5f101102017-06-29 13:07:39 -050058void Presence::determinePresence()
59{
Gunnar Mills80292bb2017-07-05 16:34:51 -050060 auto present = false;
Gunnar Mills5f101102017-06-29 13:07:39 -050061 auto value = static_cast<int>(0);
Patrick Williams39084b42023-05-10 07:50:58 -050062 auto fetch_rc = libevdev_fetch_event_value(devicePtr.get(), EV_KEY, key,
63 &value);
Gunnar Mills5f101102017-06-29 13:07:39 -050064 if (0 == fetch_rc)
65 {
66 log<level::ERR>("Device does not support event type",
67 entry("KEYCODE=%d", key));
68 elog<InternalFailure>();
69 return;
70 }
Gunnar Mills80292bb2017-07-05 16:34:51 -050071 if (value > 0)
72 {
73 present = true;
74 }
75
76 updateInventory(present);
Gunnar Mills5f101102017-06-29 13:07:39 -050077}
78
Gunnar Mills765725e2017-07-06 14:17:44 -050079// Callback handler when there is an activity on the FD
Brad Bishop86d16f02019-09-19 16:07:33 -040080int Presence::processEvents(sd_event_source*, int, uint32_t, void* userData)
Gunnar Mills765725e2017-07-06 14:17:44 -050081{
82 auto presence = static_cast<Presence*>(userData);
83
84 presence->analyzeEvent();
85 return 0;
86}
87
Gunnar Mills765725e2017-07-06 14:17:44 -050088// Analyzes the GPIO event
89void Presence::analyzeEvent()
90{
Gunnar Mills765725e2017-07-06 14:17:44 -050091 // Data returned
Patrick Venturedace6802018-11-01 16:52:10 -070092 struct input_event ev
Patrick Williams39084b42023-05-10 07:50:58 -050093 {};
Gunnar Mills765725e2017-07-06 14:17:44 -050094 int rc = 0;
95
96 // While testing, observed that not having a loop here was leading
97 // into events being missed.
98 while (rc >= 0)
99 {
100 // Wait until no more events are available on the device.
Patrick Venturedace6802018-11-01 16:52:10 -0700101 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
102 &ev);
Gunnar Mills765725e2017-07-06 14:17:44 -0500103 if (rc < 0)
104 {
105 // There was an error waiting for events, mostly that there are no
106 // events to be read.. So continue waiting...
107 return;
108 }
109
110 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
111 {
112 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
113 {
114 continue;
115 }
116 else if (ev.code == key)
117 {
118 auto present = false;
119 if (ev.value > 0)
120 {
121 present = true;
Brandon Wymanb08a0f62021-03-03 19:53:18 -0600122 std::this_thread::sleep_for(
123 std::chrono::milliseconds(delay));
124 bindOrUnbindDrivers(present);
125 updateInventory(present);
Gunnar Mills765725e2017-07-06 14:17:44 -0500126 }
Brandon Wymanb08a0f62021-03-03 19:53:18 -0600127 else
128 {
129 updateInventory(present);
130 bindOrUnbindDrivers(present);
131 }
Gunnar Mills765725e2017-07-06 14:17:44 -0500132 }
133 }
134 }
135
136 return;
137}
Gunnar Mills80292bb2017-07-05 16:34:51 -0500138
139Presence::ObjectMap Presence::getObjectMap(bool present)
140{
141 ObjectMap invObj;
142 InterfaceMap invIntf;
143 PropertyMap invProp;
144
145 invProp.emplace("Present", present);
146 invProp.emplace("PrettyName", name);
Patrick Venturedace6802018-11-01 16:52:10 -0700147 invIntf.emplace("xyz.openbmc_project.Inventory.Item", std::move(invProp));
Anthony Wilson206f0042019-05-02 00:02:23 -0500148 // Add any extra interfaces we want to associate with the inventory item
149 for (auto& iface : ifaces)
150 {
151 invIntf.emplace(iface, PropertyMap());
152 }
Gunnar Mills80292bb2017-07-05 16:34:51 -0500153 invObj.emplace(std::move(inventory), std::move(invIntf));
154
155 return invObj;
156}
157
158void Presence::updateInventory(bool present)
159{
160 ObjectMap invObj = getObjectMap(present);
161
Gunnar Mills765725e2017-07-06 14:17:44 -0500162 log<level::INFO>("Updating inventory present property",
163 entry("PRESENT=%d", present),
Joseph Reynolds32c3f632018-05-17 12:11:18 -0500164 entry("PATH=%s", inventory.c_str()));
Gunnar Mills765725e2017-07-06 14:17:44 -0500165
Gunnar Mills80292bb2017-07-05 16:34:51 -0500166 auto invService = getService(INVENTORY_PATH, INVENTORY_INTF, bus);
167
168 // Update inventory
Patrick Venturedace6802018-11-01 16:52:10 -0700169 auto invMsg = bus.new_method_call(invService.c_str(), INVENTORY_PATH,
170 INVENTORY_INTF, "Notify");
Gunnar Mills80292bb2017-07-05 16:34:51 -0500171 invMsg.append(std::move(invObj));
George Liu7abda622023-08-01 13:54:11 +0800172 try
Gunnar Mills80292bb2017-07-05 16:34:51 -0500173 {
George Liu7abda622023-08-01 13:54:11 +0800174 auto invMgrResponseMsg = bus.call(invMsg);
175 }
176 catch (const sdbusplus::exception_t& e)
177 {
178 log<level::ERR>("Error in inventory manager call to update inventory",
179 entry("ERROR=%s", e.what()));
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 {
Patrick Venturedace6802018-11-01 16:52:10 -0700195 log<level::INFO>("Binding a device driver",
196 entry("PATH=%s", path.c_str()),
197 entry("DEVICE=%s", device.c_str()));
Matt Spinler902d1c32017-09-01 11:03:02 -0500198 }
199 else
200 {
Patrick Venturedace6802018-11-01 16:52:10 -0700201 log<level::INFO>("Unbinding a device driver",
202 entry("PATH=%s", path.c_str()),
203 entry("DEVICE=%s", device.c_str()));
Matt Spinler902d1c32017-09-01 11:03:02 -0500204 }
205
206 std::ofstream file;
207
Patrick Venturedace6802018-11-01 16:52:10 -0700208 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
209 std::ofstream::eofbit);
Matt Spinler902d1c32017-09-01 11:03:02 -0500210
211 try
212 {
213 file.open(path);
214 file << device;
215 file.close();
216 }
Patrick Williams67554142021-10-06 13:00:15 -0500217 catch (const std::exception& e)
Matt Spinler902d1c32017-09-01 11:03:02 -0500218 {
219 auto err = errno;
220
221 log<level::ERR>("Failed binding or unbinding a device "
222 "after a card was removed or added",
223 entry("PATH=%s", path.c_str()),
224 entry("DEVICE=%s", device.c_str()),
225 entry("ERRNO=%d", err));
226 }
227 }
Gunnar Mills80292bb2017-07-05 16:34:51 -0500228}
229
Gunnar Mills72639152017-06-22 15:06:21 -0500230} // namespace presence
231} // namespace gpio
232} // namespace phosphor