blob: 2663f8fa2071ead6cbd400e6d77ae5f47a293367 [file] [log] [blame]
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05301/**
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 Subbannaaffea8b2017-04-04 14:02:16 +053017#include "argument.hpp"
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053018#include "monitor.hpp"
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053019
Patrick Venturedace6802018-11-01 16:52:10 -070020#include <systemd/sd-event.h>
21
22#include <iostream>
23#include <phosphor-logging/log.hpp>
24#include <string>
25
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053026using namespace phosphor::logging;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053027static void exitWithError(const char* err, char** argv)
28{
29 phosphor::gpio::ArgumentParser::usage(argv);
30 std::cerr << "ERROR: " << err << "\n";
31 exit(EXIT_FAILURE);
32}
33
34int main(int argc, char** argv)
35{
36 // Read arguments.
37 auto options = phosphor::gpio::ArgumentParser(argc, argv);
38
39 // Parse out path argument.
40 auto path = (options)["path"];
41 if (path == phosphor::gpio::ArgumentParser::emptyString)
42 {
43 exitWithError("path not specified.", argv);
44 }
45
46 // Parse out key number that we are interested in
47 // Its integer mapping to the GPIO key configured by the kernel
48 auto key = (options)["key"];
49 if (key == phosphor::gpio::ArgumentParser::emptyString)
50 {
51 exitWithError("Key not specified.", argv);
52 }
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053053
54 // Parse out assertion polarity interested in
55 // Its either 1 or 0 for press / release
56 auto polarity = (options)["polarity"];
57 if (polarity == phosphor::gpio::ArgumentParser::emptyString)
58 {
59 exitWithError("Polarity not specified.", argv);
60 }
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053061
62 // Parse out target argument. It is fine if the caller does not
63 // pass this if they are not interested in calling into any target
64 // on meeting a condition.
65 auto target = (options)["target"];
66
Lei YUbc4a4ff2018-04-11 13:33:25 +080067 bool continueRun =
Patrick Venturedace6802018-11-01 16:52:10 -070068 (options["continue"] == phosphor::gpio::ArgumentParser::trueString);
Lei YUbc4a4ff2018-04-11 13:33:25 +080069
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053070 sd_event* event = nullptr;
71 auto r = sd_event_default(&event);
72 if (r < 0)
73 {
74 log<level::ERR>("Error creating a default sd_event handler");
75 return r;
76 }
77 phosphor::gpio::EventPtr eventP{event};
78 event = nullptr;
79
80 // Create a monitor object and let it do all the rest
Patrick Venturedace6802018-11-01 16:52:10 -070081 phosphor::gpio::Monitor monitor(path, std::stoi(key), std::stoi(polarity),
82 target, eventP, continueRun);
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053083
84 // Wait for client requests until this application has processed
85 // at least one expected GPIO state change
Patrick Venturedace6802018-11-01 16:52:10 -070086 while (!monitor.completed())
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053087 {
88 // -1 denotes wait for ever
89 r = sd_event_run(eventP.get(), (uint64_t)-1);
90 if (r < 0)
91 {
92 log<level::ERR>("Failure in processing request",
Patrick Venturedace6802018-11-01 16:52:10 -070093 entry("ERROR=%s", strerror(-r)));
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053094 break;
95 }
96 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053097
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053098 return 0;
99}