blob: dfd9987f26a3ce24480c5bd87004e76a185ee773 [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 Subbanna4902a102017-04-04 14:05:09 +053034// Populate the file descriptor for passed in device
35int Monitor::openDevice()
36{
37 using namespace phosphor::logging;
38
39 int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
40 if (fd < 0)
41 {
42 log<level::ERR>("Failed to open device",
43 entry("PATH=%s", path.c_str()));
44 throw std::runtime_error("Failed to open device");
45 }
46 return fd;
47}
48
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053049// Attaches the FD to event loop and registers the callback handler
50void Monitor::registerCallback()
51{
52 decltype(eventSource.get()) sourcePtr = nullptr;
53 auto r = sd_event_add_io(event.get(), &sourcePtr, (fd)(),
54 EPOLLIN, callbackHandler, this);
55 eventSource.reset(sourcePtr);
56
57 if (r < 0)
58 {
59 log<level::ERR>("Failed to register callback handler",
60 entry("ERROR=%s", strerror(-r)));
61 throw std::runtime_error("Failed to register callback handler");
62 }
63}
64
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053065// Initializes the event device with the fd
66void Monitor::initEvDev()
67{
68 if (device)
69 {
70 // Init can be done only once per device
71 return;
72 }
73
74 struct libevdev* evdev = nullptr;
75 auto rc = libevdev_new_from_fd((fd)(), &evdev);
76 if (rc < 0)
77 {
78 log<level::ERR>("Failed to initialize evdev");
79 throw std::runtime_error("Failed to initialize evdev");
80 }
81
82 // Packing in the unique_ptr
83 device.reset(evdev);
84 evdev = nullptr;
85}
86
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053087// Callback handler when there is an activity on the FD
88int Monitor::processEvents(sd_event_source* es, int fd,
89 uint32_t revents, void* userData)
90{
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053091 log<level::INFO>("GPIO line altered");
92 auto monitor = static_cast<Monitor*>(userData);
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053093
94 // Initialize libevdev for this. Doing it here enables
95 // gtest to use this infrastructure on arbitrary device
96 // than /dev/input/
97 monitor->initEvDev();
98 monitor->analyzeEvent();
99 return 0;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530100}
101
102// Analyzes the GPIO event
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530103void Monitor::analyzeEvent()
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530104{
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530105 // Data returned
106 struct input_event ev{};
107 int rc = 0;
108
109 // While testing, observed that not having a loop here was leading
110 // into events being missed.
111 while (rc >= 0)
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530112 {
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530113 // Wait until no more events are available on the device.
114 rc = libevdev_next_event(device.get(),
115 LIBEVDEV_READ_FLAG_NORMAL, &ev);
116 if (rc < 0)
117 {
118 // There was an error waiting for events, mostly that there are no
119 // events to be read.. So continue waiting...
120 return;
121 };
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530122
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530123 if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
124 {
125 if (ev.type == EV_SYN && ev.code == SYN_REPORT)
126 {
127 continue;
128 }
129 else if (ev.code == key && ev.value == polarity)
130 {
131 // If the code/value is what we are interested in, declare done.
132 // User supplied systemd unit
133 if (!target.empty())
134 {
135 auto bus = sdbusplus::bus::new_default();
136 auto method = bus.new_method_call(SYSTEMD_SERVICE,
137 SYSTEMD_ROOT,
138 SYSTEMD_INTERFACE,
139 "StartUnit");
140 method.append(target);
141 method.append("replace");
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530142
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530143 bus.call_noreply(method);
144 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +0530145
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +0530146 // This marks the completion of handling the gpio assertion
147 // and the app can exit
148 complete = true;
149 return;
150 }
151 }
152 };
153
154 return;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +0530155}
156
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +0530157} // namespace gpio
158} // namespace phosphor