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 | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 17 | #include "argument.hpp" |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 18 | #include "monitor.hpp" |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 19 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 20 | #include <systemd/sd-event.h> |
| 21 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/log.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 | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 28 | static void exitWithError(const char* err, char** argv) |
| 29 | { |
| 30 | phosphor::gpio::ArgumentParser::usage(argv); |
| 31 | std::cerr << "ERROR: " << err << "\n"; |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | |
| 35 | int main(int argc, char** argv) |
| 36 | { |
| 37 | // Read arguments. |
| 38 | auto options = phosphor::gpio::ArgumentParser(argc, argv); |
| 39 | |
| 40 | // Parse out path argument. |
| 41 | auto path = (options)["path"]; |
| 42 | if (path == phosphor::gpio::ArgumentParser::emptyString) |
| 43 | { |
| 44 | exitWithError("path not specified.", argv); |
| 45 | } |
| 46 | |
| 47 | // Parse out key number that we are interested in |
| 48 | // Its integer mapping to the GPIO key configured by the kernel |
| 49 | auto key = (options)["key"]; |
| 50 | if (key == phosphor::gpio::ArgumentParser::emptyString) |
| 51 | { |
| 52 | exitWithError("Key not specified.", argv); |
| 53 | } |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 54 | |
| 55 | // Parse out assertion polarity interested in |
| 56 | // Its either 1 or 0 for press / release |
| 57 | auto polarity = (options)["polarity"]; |
| 58 | if (polarity == phosphor::gpio::ArgumentParser::emptyString) |
| 59 | { |
| 60 | exitWithError("Polarity not specified.", argv); |
| 61 | } |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 62 | |
| 63 | // Parse out target argument. It is fine if the caller does not |
| 64 | // pass this if they are not interested in calling into any target |
| 65 | // on meeting a condition. |
| 66 | auto target = (options)["target"]; |
| 67 | |
Lei YU | bc4a4ff | 2018-04-11 13:33:25 +0800 | [diff] [blame] | 68 | bool continueRun = |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 69 | (options["continue"] == phosphor::gpio::ArgumentParser::trueString); |
Lei YU | bc4a4ff | 2018-04-11 13:33:25 +0800 | [diff] [blame] | 70 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 71 | sd_event* event = nullptr; |
| 72 | auto r = sd_event_default(&event); |
| 73 | if (r < 0) |
| 74 | { |
| 75 | log<level::ERR>("Error creating a default sd_event handler"); |
| 76 | return r; |
| 77 | } |
| 78 | phosphor::gpio::EventPtr eventP{event}; |
| 79 | event = nullptr; |
| 80 | |
| 81 | // Create a monitor object and let it do all the rest |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 82 | phosphor::gpio::Monitor monitor(path, std::stoi(key), std::stoi(polarity), |
| 83 | target, eventP, continueRun); |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 84 | |
| 85 | // Wait for client requests until this application has processed |
| 86 | // at least one expected GPIO state change |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 87 | while (!monitor.completed()) |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 88 | { |
| 89 | // -1 denotes wait for ever |
| 90 | r = sd_event_run(eventP.get(), (uint64_t)-1); |
| 91 | if (r < 0) |
| 92 | { |
| 93 | log<level::ERR>("Failure in processing request", |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 94 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | } |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 98 | |
Vishwanatha Subbanna | affea8b | 2017-04-04 14:02:16 +0530 | [diff] [blame] | 99 | return 0; |
| 100 | } |