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 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 65 | // Initializes the event device with the fd |
| 66 | void 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 Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 87 | // Callback handler when there is an activity on the FD |
| 88 | int Monitor::processEvents(sd_event_source* es, int fd, |
| 89 | uint32_t revents, void* userData) |
| 90 | { |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 91 | log<level::INFO>("GPIO line altered"); |
| 92 | auto monitor = static_cast<Monitor*>(userData); |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 93 | |
| 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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Analyzes the GPIO event |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 103 | void Monitor::analyzeEvent() |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 104 | { |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 105 | // 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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 112 | { |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 113 | // 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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 122 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 123 | 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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 142 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 143 | bus.call_noreply(method); |
| 144 | } |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 145 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 146 | // 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 Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 157 | } // namespace gpio |
| 158 | } // namespace phosphor |