blob: 44a46c2f67df26da096ac495254a9dd0868e3603 [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 Subbanna4902a102017-04-04 14:05:09 +053017#include "monitor.hpp"
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053018
Patrick Venturedace6802018-11-01 16:52:10 -070019#include <systemd/sd-event.h>
20
George Liuae5e8a92023-08-01 17:30:34 +080021#include <CLI/CLI.hpp>
George Liu2a8848c2023-08-01 13:49:28 +080022#include <phosphor-logging/lg2.hpp>
Patrick Williams39084b42023-05-10 07:50:58 -050023
24#include <iostream>
Patrick Venturedace6802018-11-01 16:52:10 -070025#include <string>
26
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053027int main(int argc, char** argv)
28{
George Liuae5e8a92023-08-01 17:30:34 +080029 CLI::App app{"Monitor GPIO line for requested state change"};
30
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053031 // Read arguments.
George Liuae5e8a92023-08-01 17:30:34 +080032 std::string path{};
33 std::string key{};
34 std::string polarity{};
35 std::string target{};
36 bool continueRun = false;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053037
George Liuae5e8a92023-08-01 17:30:34 +080038 /* 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 Williams83546182023-08-09 11:19:07 -050049 app.add_flag("-c,--continue", continueRun,
50 "Whether or not to continue after key pressed");
George Liuae5e8a92023-08-01 17:30:34 +080051
Patrick Williams69e99302023-08-09 14:45:28 -050052 /* 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 Liuae5e8a92023-08-01 17:30:34 +080056 /* Parse input parameter */
57 try
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053058 {
George Liuae5e8a92023-08-01 17:30:34 +080059 app.parse(argc, argv);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053060 }
George Liuae5e8a92023-08-01 17:30:34 +080061 catch (const CLI::Error& e)
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053062 {
George Liuae5e8a92023-08-01 17:30:34 +080063 return app.exit(e);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053064 }
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053065
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053066 sd_event* event = nullptr;
67 auto r = sd_event_default(&event);
68 if (r < 0)
69 {
George Liu2a8848c2023-08-01 13:49:28 +080070 lg2::error("Error creating a default sd_event handler");
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053071 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 Venturedace6802018-11-01 16:52:10 -070077 phosphor::gpio::Monitor monitor(path, std::stoi(key), std::stoi(polarity),
78 target, eventP, continueRun);
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053079
80 // Wait for client requests until this application has processed
81 // at least one expected GPIO state change
Patrick Venturedace6802018-11-01 16:52:10 -070082 while (!monitor.completed())
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053083 {
84 // -1 denotes wait for ever
85 r = sd_event_run(eventP.get(), (uint64_t)-1);
86 if (r < 0)
87 {
George Liu2a8848c2023-08-01 13:49:28 +080088 lg2::error("Failure in processing request: {RC}", "RC", r);
George Liuae5e8a92023-08-01 17:30:34 +080089 return r;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053090 }
91 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053092
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053093 return 0;
94}