blob: caab61bfa813a8b26953e17ecd27a210468f6f03 [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
21#include <phosphor-logging/log.hpp>
22
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 +053033using namespace phosphor::logging;
34
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053035// Callback handler when there is an activity on the FD
Patrick Venturedace6802018-11-01 16:52:10 -070036int Monitor::processEvents(sd_event_source* es, int fd, uint32_t revents,
37 void* userData)
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053038{
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053039 log<level::INFO>("GPIO line altered");
40 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053041
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053042 monitor->analyzeEvent();
43 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053044}
45
46// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053047void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053048{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053049 // Data returned
Patrick Venturedace6802018-11-01 16:52:10 -070050 struct input_event ev
51 {
52 };
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053053 int rc = 0;
54
55 // While testing, observed that not having a loop here was leading
56 // into events being missed.
57 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053058 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053059 // Wait until no more events are available on the device.
Patrick Venturedace6802018-11-01 16:52:10 -070060 rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL,
61 &ev);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053062 if (rc < 0)
63 {
64 // There was an error waiting for events, mostly that there are no
65 // events to be read.. So continue waiting...
66 return;
67 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053068
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053069 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
70 {
71 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
72 {
73 continue;
74 }
75 else if (ev.code == key && ev.value == polarity)
76 {
77 // If the code/value is what we are interested in, declare done.
78 // User supplied systemd unit
79 if (!target.empty())
80 {
81 auto bus = sdbusplus::bus::new_default();
Patrick Venturedace6802018-11-01 16:52:10 -070082 auto method =
83 bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
84 SYSTEMD_INTERFACE, "StartUnit");
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053085 method.append(target);
86 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053087
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053088 bus.call_noreply(method);
89 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053090
Lei YUbc4a4ff2018-04-11 13:33:25 +080091 if (!continueAfterKeyPress)
92 {
93 // This marks the completion of handling the gpio assertion
94 // and the app can exit
95 complete = true;
96 }
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053097 return;
98 }
99 }
100 };
101
102 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +0530103}
104
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +0530105} // namespace gpio
106} // namespace phosphor