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 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 17 | #include "monitor.hpp" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <phosphor-logging/log.hpp> |
| 22 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace gpio |
| 26 | { |
| 27 | |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 28 | // systemd service to kick start a target. |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 29 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 30 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
| 31 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 32 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 33 | using namespace phosphor::logging; |
| 34 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 35 | // Callback handler when there is an activity on the FD |
Brad Bishop | 86d16f0 | 2019-09-19 16:07:33 -0400 | [diff] [blame] | 36 | int Monitor::processEvents(sd_event_source*, int, uint32_t, void* userData) |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 37 | { |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 38 | log<level::INFO>("GPIO line altered"); |
| 39 | auto monitor = static_cast<Monitor*>(userData); |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 40 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 41 | monitor->analyzeEvent(); |
| 42 | return 0; |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Analyzes the GPIO event |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 46 | void Monitor::analyzeEvent() |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 47 | { |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 48 | // Data returned |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 49 | struct input_event ev |
| 50 | { |
| 51 | }; |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 52 | int rc = 0; |
| 53 | |
| 54 | // While testing, observed that not having a loop here was leading |
| 55 | // into events being missed. |
| 56 | while (rc >= 0) |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 57 | { |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 58 | // Wait until no more events are available on the device. |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 59 | rc = libevdev_next_event(devicePtr.get(), LIBEVDEV_READ_FLAG_NORMAL, |
| 60 | &ev); |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 61 | if (rc < 0) |
| 62 | { |
| 63 | // There was an error waiting for events, mostly that there are no |
| 64 | // events to be read.. So continue waiting... |
| 65 | return; |
| 66 | }; |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 67 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 68 | if (rc == LIBEVDEV_READ_STATUS_SUCCESS) |
| 69 | { |
| 70 | if (ev.type == EV_SYN && ev.code == SYN_REPORT) |
| 71 | { |
| 72 | continue; |
| 73 | } |
| 74 | else if (ev.code == key && ev.value == polarity) |
| 75 | { |
| 76 | // If the code/value is what we are interested in, declare done. |
| 77 | // User supplied systemd unit |
| 78 | if (!target.empty()) |
| 79 | { |
| 80 | auto bus = sdbusplus::bus::new_default(); |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 81 | auto method = |
| 82 | bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 83 | SYSTEMD_INTERFACE, "StartUnit"); |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 84 | method.append(target); |
| 85 | method.append("replace"); |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 86 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 87 | bus.call_noreply(method); |
| 88 | } |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 89 | |
Lei YU | bc4a4ff | 2018-04-11 13:33:25 +0800 | [diff] [blame] | 90 | if (!continueAfterKeyPress) |
| 91 | { |
| 92 | // This marks the completion of handling the gpio assertion |
| 93 | // and the app can exit |
| 94 | complete = true; |
| 95 | } |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | return; |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 104 | } // namespace gpio |
| 105 | } // namespace phosphor |