| 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 | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 3 | |
| Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 4 | #include "monitor.hpp" |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 5 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 6 | #include <systemd/sd-event.h> |
| 7 | |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 8 | #include <CLI/CLI.hpp> |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
| Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 10 | |
| 11 | #include <iostream> |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 14 | int main(int argc, char** argv) |
| 15 | { |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 16 | CLI::App app{"Monitor GPIO line for requested state change"}; |
| 17 | |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 18 | // Read arguments. |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 19 | std::string path{}; |
| 20 | std::string key{}; |
| 21 | std::string polarity{}; |
| 22 | std::string target{}; |
| 23 | bool continueRun = false; |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 24 | |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 25 | /* Add an input option */ |
| 26 | app.add_option("-p,--path", path, |
| 27 | "Path of input device. Ex: /dev/input/event2") |
| 28 | ->required(); |
| 29 | app.add_option("-k,--key", key, "Input GPIO key number")->required(); |
| 30 | app.add_option("-r,--polarity", polarity, |
| 31 | "Asertion polarity to look for. This is 0 / 1") |
| 32 | ->required(); |
| 33 | app.add_option("-t,--target", target, |
| 34 | "Systemd unit to be called on GPIO state change") |
| 35 | ->required(); |
| Patrick Williams | 8354618 | 2023-08-09 11:19:07 -0500 | [diff] [blame] | 36 | app.add_flag("-c,--continue", continueRun, |
| 37 | "Whether or not to continue after key pressed"); |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 38 | |
| Patrick Williams | 69e9930 | 2023-08-09 14:45:28 -0500 | [diff] [blame] | 39 | /* Due to the way this process is loaded from systemd, we can end up with |
| 40 | * an empty extra parameter. Go ahead and ignore it. */ |
| 41 | app.allow_extras(); |
| 42 | |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 43 | /* Parse input parameter */ |
| 44 | try |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 45 | { |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 46 | app.parse(argc, argv); |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 47 | } |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 48 | catch (const CLI::Error& e) |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 49 | { |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 50 | return app.exit(e); |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 51 | } |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 52 | |
| Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 53 | sd_event* event = nullptr; |
| 54 | auto r = sd_event_default(&event); |
| 55 | if (r < 0) |
| 56 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 57 | lg2::error("Error creating a default sd_event handler"); |
| Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 58 | return r; |
| 59 | } |
| 60 | phosphor::gpio::EventPtr eventP{event}; |
| 61 | event = nullptr; |
| 62 | |
| 63 | // Create a monitor object and let it do all the rest |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 64 | phosphor::gpio::Monitor monitor(path, std::stoi(key), std::stoi(polarity), |
| 65 | target, eventP, continueRun); |
| Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 66 | |
| 67 | // Wait for client requests until this application has processed |
| 68 | // at least one expected GPIO state change |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 69 | while (!monitor.completed()) |
| Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 70 | { |
| 71 | // -1 denotes wait for ever |
| 72 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 73 | if (r < 0) |
| 74 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 75 | lg2::error("Failure in processing request: {RC}", "RC", r); |
| George Liu | ae5e8a9 | 2023-08-01 17:30:34 +0800 | [diff] [blame] | 76 | return r; |
| Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 77 | } |
| 78 | } |
| Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 79 | |
| Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 80 | return 0; |
| 81 | } |