blob: 99eed038a7b1086ef16741dafc8c19f9021f0976 [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
65// Callback handler when there is an activity on the FD
66int Monitor::processEvents(sd_event_source* es, int fd,
67 uint32_t revents, void* userData)
68{
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053069 log<level::INFO>("GPIO line altered");
70 auto monitor = static_cast<Monitor*>(userData);
71 // TODO : Need a way to check if the GPIO state change is what we wanted
72 return monitor->analyzeEvent();
73}
74
75// Analyzes the GPIO event
76int Monitor::analyzeEvent()
77{
78 if(!target.empty())
79 {
80 auto bus = sdbusplus::bus::new_default();
81 auto method = bus.new_method_call(SYSTEMD_SERVICE,
82 SYSTEMD_ROOT,
83 SYSTEMD_INTERFACE,
84 "StartUnit");
85 method.append(target);
86 method.append("replace");
87
88 // If there is any error, an exception would be thrown from here.
89 bus.call_noreply(method);
90 }
91
92 // This marks the completion of handling the checkstop and app can exit
93 complete = true;
94
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053095 return 0;
96}
97
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053098} // namespace gpio
99} // namespace phosphor