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