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