blob: cbc0c1c9f3cf54c866a2ae0c25aae2712e6a0ee9 [file] [log] [blame]
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05301/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Venturedace6802018-11-01 16:52:10 -070017#include "monitor.hpp"
18
19#include <fcntl.h>
20
George Liu2a8848c2023-08-01 13:49:28 +080021#include <phosphor-logging/lg2.hpp>
Patrick Venturedace6802018-11-01 16:52:10 -070022
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053023namespace phosphor
24{
25namespace gpio
26{
27
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053028// systemd service to kick start a target.
Patrick Venturedace6802018-11-01 16:52:10 -070029constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
30constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
31constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053032
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053033// Callback handler when there is an activity on the FD
Brad Bishop86d16f02019-09-19 16:07:33 -040034int Monitor::processEvents(sd_event_source*, int, uint32_t, void* userData)
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053035{
George Liu2a8848c2023-08-01 13:49:28 +080036 lg2::info("GPIO line altered");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053037 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053038
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053039 monitor->analyzeEvent();
40 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053041}
42
43// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053044void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053045{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053046 // Data returned
Patrick Williams1c888032024-12-18 11:21:41 -050047 struct input_event ev{};
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053048 int rc = 0;
49
50 // While testing, observed that not having a loop here was leading
51 // into events being missed.
52 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053053 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053054 // Wait until no more events are available on the device.
Patrick Venturedace6802018-11-01 16:52:10 -070055 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
56 &ev);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053057 if (rc < 0)
58 {
59 // There was an error waiting for events, mostly that there are no
60 // events to be read.. So continue waiting...
61 return;
62 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053063
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053064 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
65 {
66 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
67 {
68 continue;
69 }
70 else if (ev.code == key && ev.value == polarity)
71 {
72 // If the code/value is what we are interested in, declare done.
73 // User supplied systemd unit
74 if (!target.empty())
75 {
76 auto bus = sdbusplus::bus::new_default();
Patrick Venturedace6802018-11-01 16:52:10 -070077 auto method =
78 bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
79 SYSTEMD_INTERFACE, "StartUnit");
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053080 method.append(target);
81 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053082
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053083 bus.call_noreply(method);
84 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053085
Lei YUbc4a4ff2018-04-11 13:33:25 +080086 if (!continueAfterKeyPress)
87 {
88 // This marks the completion of handling the gpio assertion
89 // and the app can exit
90 complete = true;
91 }
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053092 return;
93 }
94 }
95 };
96
97 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053098}
99
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +0530100} // namespace gpio
101} // namespace phosphor