blob: d65a393c5b6d49c6e4b3d41d35de8d54f10ecee2 [file] [log] [blame]
Christopher Meis75ff1672025-09-23 11:40:06 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2016 IBM Corporation
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05303
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05304#include "monitor.hpp"
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05305
Patrick Venturedace6802018-11-01 16:52:10 -07006#include <systemd/sd-event.h>
7
George Liuae5e8a92023-08-01 17:30:34 +08008#include <CLI/CLI.hpp>
George Liu2a8848c2023-08-01 13:49:28 +08009#include <phosphor-logging/lg2.hpp>
Patrick Williams39084b42023-05-10 07:50:58 -050010
11#include <iostream>
Patrick Venturedace6802018-11-01 16:52:10 -070012#include <string>
13
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053014int main(int argc, char** argv)
15{
George Liuae5e8a92023-08-01 17:30:34 +080016 CLI::App app{"Monitor GPIO line for requested state change"};
17
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053018 // Read arguments.
George Liuae5e8a92023-08-01 17:30:34 +080019 std::string path{};
20 std::string key{};
21 std::string polarity{};
22 std::string target{};
23 bool continueRun = false;
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053024
George Liuae5e8a92023-08-01 17:30:34 +080025 /* Add an input option */
26 app.add_option("-p,--path", path,
27 "Path of input device. Ex: /dev/input/event2")
28 ->required();
29 app.add_option("-k,--key", key, "Input GPIO key number")->required();
30 app.add_option("-r,--polarity", polarity,
31 "Asertion polarity to look for. This is 0 / 1")
32 ->required();
33 app.add_option("-t,--target", target,
34 "Systemd unit to be called on GPIO state change")
35 ->required();
Patrick Williams83546182023-08-09 11:19:07 -050036 app.add_flag("-c,--continue", continueRun,
37 "Whether or not to continue after key pressed");
George Liuae5e8a92023-08-01 17:30:34 +080038
Patrick Williams69e99302023-08-09 14:45:28 -050039 /* Due to the way this process is loaded from systemd, we can end up with
40 * an empty extra parameter. Go ahead and ignore it. */
41 app.allow_extras();
42
George Liuae5e8a92023-08-01 17:30:34 +080043 /* Parse input parameter */
44 try
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053045 {
George Liuae5e8a92023-08-01 17:30:34 +080046 app.parse(argc, argv);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053047 }
George Liuae5e8a92023-08-01 17:30:34 +080048 catch (const CLI::Error& e)
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053049 {
George Liuae5e8a92023-08-01 17:30:34 +080050 return app.exit(e);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053051 }
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053052
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053053 sd_event* event = nullptr;
54 auto r = sd_event_default(&event);
55 if (r < 0)
56 {
George Liu2a8848c2023-08-01 13:49:28 +080057 lg2::error("Error creating a default sd_event handler");
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053058 return r;
59 }
60 phosphor::gpio::EventPtr eventP{event};
61 event = nullptr;
62
63 // Create a monitor object and let it do all the rest
Patrick Venturedace6802018-11-01 16:52:10 -070064 phosphor::gpio::Monitor monitor(path, std::stoi(key), std::stoi(polarity),
65 target, eventP, continueRun);
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053066
67 // Wait for client requests until this application has processed
68 // at least one expected GPIO state change
Patrick Venturedace6802018-11-01 16:52:10 -070069 while (!monitor.completed())
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053070 {
71 // -1 denotes wait for ever
72 r = sd_event_run(eventP.get(), (uint64_t)-1);
73 if (r < 0)
74 {
George Liu2a8848c2023-08-01 13:49:28 +080075 lg2::error("Failure in processing request: {RC}", "RC", r);
George Liuae5e8a92023-08-01 17:30:34 +080076 return r;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053077 }
78 }
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053079
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053080 return 0;
81}