blob: ef6f115b1c901b0e609151cd2bdbad111b775356 [file] [log] [blame]
Christopher Meis75ff1672025-09-23 11:40:06 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2016 IBM Corporation
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05303
Patrick Venturedace6802018-11-01 16:52:10 -07004#include "monitor.hpp"
5
6#include <fcntl.h>
7
George Liu2a8848c2023-08-01 13:49:28 +08008#include <phosphor-logging/lg2.hpp>
Patrick Venturedace6802018-11-01 16:52:10 -07009
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053010namespace phosphor
11{
12namespace gpio
13{
14
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053015// systemd service to kick start a target.
Patrick Venturedace6802018-11-01 16:52:10 -070016constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
17constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
18constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053019
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053020// Callback handler when there is an activity on the FD
Brad Bishop86d16f02019-09-19 16:07:33 -040021int Monitor::processEvents(sd_event_source*, int, uint32_t, void* userData)
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053022{
George Liu2a8848c2023-08-01 13:49:28 +080023 lg2::info("GPIO line altered");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053024 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053025
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053026 monitor->analyzeEvent();
27 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053028}
29
30// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053031void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053032{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053033 // Data returned
Patrick Williams1c888032024-12-18 11:21:41 -050034 struct input_event ev{};
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053035 int rc = 0;
36
37 // While testing, observed that not having a loop here was leading
38 // into events being missed.
39 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053040 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053041 // Wait until no more events are available on the device.
Patrick Venturedace6802018-11-01 16:52:10 -070042 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
43 &ev);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053044 if (rc < 0)
45 {
46 // There was an error waiting for events, mostly that there are no
47 // events to be read.. So continue waiting...
48 return;
49 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053050
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053051 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
52 {
53 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
54 {
55 continue;
56 }
57 else if (ev.code == key && ev.value == polarity)
58 {
59 // If the code/value is what we are interested in, declare done.
60 // User supplied systemd unit
61 if (!target.empty())
62 {
63 auto bus = sdbusplus::bus::new_default();
Patrick Venturedace6802018-11-01 16:52:10 -070064 auto method =
65 bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
66 SYSTEMD_INTERFACE, "StartUnit");
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053067 method.append(target);
68 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053069
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053070 bus.call_noreply(method);
71 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053072
Lei YUbc4a4ff2018-04-11 13:33:25 +080073 if (!continueAfterKeyPress)
74 {
75 // This marks the completion of handling the gpio assertion
76 // and the app can exit
77 complete = true;
78 }
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053079 return;
80 }
81 }
82 };
83
84 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053085}
86
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053087} // namespace gpio
88} // namespace phosphor