Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 1 | /** |
| 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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 20 | #include "config.h" |
| 21 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace gpio |
| 25 | { |
| 26 | |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 27 | // systemd service to kick start a target. |
| 28 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 29 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
| 30 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 31 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 32 | using namespace phosphor::logging; |
| 33 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 34 | // Populate the file descriptor for passed in device |
| 35 | int 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 Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 49 | // Attaches the FD to event loop and registers the callback handler |
| 50 | void 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 |
| 66 | int Monitor::processEvents(sd_event_source* es, int fd, |
| 67 | uint32_t revents, void* userData) |
| 68 | { |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 69 | 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 |
| 76 | int 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 Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 98 | } // namespace gpio |
| 99 | } // namespace phosphor |