blob: dd96fe9552b0db276cbb0666b462c0f41cc28019 [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
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053017#include "config.h"
18
Patrick Venturedace6802018-11-01 16:52:10 -070019#include "monitor.hpp"
20
21#include <fcntl.h>
22
23#include <phosphor-logging/log.hpp>
24
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053025namespace phosphor
26{
27namespace gpio
28{
29
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053030// systemd service to kick start a target.
Patrick Venturedace6802018-11-01 16:52:10 -070031constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
32constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
33constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053034
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053035using namespace phosphor::logging;
36
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053037// Callback handler when there is an activity on the FD
Patrick Venturedace6802018-11-01 16:52:10 -070038int Monitor::processEvents(sd_event_source* es, int fd, uint32_t revents,
39 void* userData)
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053040{
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053041 log<level::INFO>("GPIO line altered");
42 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053043
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053044 monitor->analyzeEvent();
45 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053046}
47
48// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053049void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053050{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053051 // Data returned
Patrick Venturedace6802018-11-01 16:52:10 -070052 struct input_event ev
53 {
54 };
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053055 int rc = 0;
56
57 // While testing, observed that not having a loop here was leading
58 // into events being missed.
59 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053060 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053061 // Wait until no more events are available on the device.
Patrick Venturedace6802018-11-01 16:52:10 -070062 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
63 &ev);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053064 if (rc < 0)
65 {
66 // There was an error waiting for events, mostly that there are no
67 // events to be read.. So continue waiting...
68 return;
69 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053070
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053071 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
72 {
73 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
74 {
75 continue;
76 }
77 else if (ev.code == key && ev.value == polarity)
78 {
79 // If the code/value is what we are interested in, declare done.
80 // User supplied systemd unit
81 if (!target.empty())
82 {
83 auto bus = sdbusplus::bus::new_default();
Patrick Venturedace6802018-11-01 16:52:10 -070084 auto method =
85 bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
86 SYSTEMD_INTERFACE, "StartUnit");
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053087 method.append(target);
88 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053089
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053090 bus.call_noreply(method);
91 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053092
Lei YUbc4a4ff2018-04-11 13:33:25 +080093 if (!continueAfterKeyPress)
94 {
95 // This marks the completion of handling the gpio assertion
96 // and the app can exit
97 complete = true;
98 }
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053099 return;
100 }
101 }
102 };
103
104 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +0530105}
106
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +0530107} // namespace gpio
108} // namespace phosphor