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