blob: 159127d799035831016298897fe80f49b281d014 [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
17#include <fcntl.h>
18#include <phosphor-logging/log.hpp>
19#include "monitor.hpp"
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053020#include "config.h"
21
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053022namespace phosphor
23{
24namespace gpio
25{
26
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053027// systemd service to kick start a target.
28constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
29constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
30constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
31
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053032using namespace phosphor::logging;
33
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053034// Callback handler when there is an activity on the FD
35int Monitor::processEvents(sd_event_source* es, int fd,
36 uint32_t revents, void* userData)
37{
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053038 log<level::INFO>("GPIO line altered");
39 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053040
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053041 monitor->analyzeEvent();
42 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053043}
44
45// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053046void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053047{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053048 // Data returned
49 struct input_event ev{};
50 int rc = 0;
51
52 // While testing, observed that not having a loop here was leading
53 // into events being missed.
54 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053055 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053056 // Wait until no more events are available on the device.
Gunnar Mills99258572017-07-27 15:22:42 -050057 rc = libevdev_next_event(devicePtr.get(),
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053058 LIBEVDEV_READ_FLAG_NORMAL, &ev);
59 if (rc < 0)
60 {
61 // There was an error waiting for events, mostly that there are no
62 // events to be read.. So continue waiting...
63 return;
64 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053065
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053066 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
67 {
68 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
69 {
70 continue;
71 }
72 else if (ev.code == key && ev.value == polarity)
73 {
74 // If the code/value is what we are interested in, declare done.
75 // User supplied systemd unit
76 if (!target.empty())
77 {
78 auto bus = sdbusplus::bus::new_default();
79 auto method = bus.new_method_call(SYSTEMD_SERVICE,
80 SYSTEMD_ROOT,
81 SYSTEMD_INTERFACE,
82 "StartUnit");
83 method.append(target);
84 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053085
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053086 bus.call_noreply(method);
87 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053088
Lei YUbc4a4ff2018-04-11 13:33:25 +080089 if (!continueAfterKeyPress)
90 {
91 // This marks the completion of handling the gpio assertion
92 // and the app can exit
93 complete = true;
94 }
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053095 return;
96 }
97 }
98 };
99
100 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +0530101}
102
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +0530103} // namespace gpio
104} // namespace phosphor