1939a6431SVijay Khemka /**
2939a6431SVijay Khemka * Copyright © 2019 Facebook
3939a6431SVijay Khemka *
4939a6431SVijay Khemka * Licensed under the Apache License, Version 2.0 (the "License");
5939a6431SVijay Khemka * you may not use this file except in compliance with the License.
6939a6431SVijay Khemka * You may obtain a copy of the License at
7939a6431SVijay Khemka *
8939a6431SVijay Khemka * http://www.apache.org/licenses/LICENSE-2.0
9939a6431SVijay Khemka *
10939a6431SVijay Khemka * Unless required by applicable law or agreed to in writing, software
11939a6431SVijay Khemka * distributed under the License is distributed on an "AS IS" BASIS,
12939a6431SVijay Khemka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13939a6431SVijay Khemka * See the License for the specific language governing permissions and
14939a6431SVijay Khemka * limitations under the License.
15939a6431SVijay Khemka */
16939a6431SVijay Khemka
17939a6431SVijay Khemka #include "gpioMon.hpp"
18939a6431SVijay Khemka
19939a6431SVijay Khemka #include <CLI/CLI.hpp>
20854404e0SEd Tanous #include <boost/asio/io_context.hpp>
21939a6431SVijay Khemka #include <nlohmann/json.hpp>
222a8848c6SGeorge Liu #include <phosphor-logging/lg2.hpp>
23939a6431SVijay Khemka
2439084b4aSPatrick Williams #include <fstream>
2539084b4aSPatrick Williams
26939a6431SVijay Khemka namespace phosphor
27939a6431SVijay Khemka {
28939a6431SVijay Khemka namespace gpio
29939a6431SVijay Khemka {
30939a6431SVijay Khemka
31939a6431SVijay Khemka std::map<std::string, int> polarityMap = {
32939a6431SVijay Khemka /**< Only watch falling edge events. */
33939a6431SVijay Khemka {"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE},
34939a6431SVijay Khemka /**< Only watch rising edge events. */
35939a6431SVijay Khemka {"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE},
36939a6431SVijay Khemka /**< Monitor both types of events. */
37939a6431SVijay Khemka {"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}};
38939a6431SVijay Khemka
39939a6431SVijay Khemka }
40939a6431SVijay Khemka } // namespace phosphor
41939a6431SVijay Khemka
main(int argc,char ** argv)42939a6431SVijay Khemka int main(int argc, char** argv)
43939a6431SVijay Khemka {
44854404e0SEd Tanous boost::asio::io_context io;
45939a6431SVijay Khemka
46939a6431SVijay Khemka CLI::App app{"Monitor GPIO line for requested state change"};
47939a6431SVijay Khemka
48939a6431SVijay Khemka std::string gpioFileName;
49939a6431SVijay Khemka
50939a6431SVijay Khemka /* Add an input option */
51939a6431SVijay Khemka app.add_option("-c,--config", gpioFileName, "Name of config json file")
52939a6431SVijay Khemka ->required()
53939a6431SVijay Khemka ->check(CLI::ExistingFile);
54939a6431SVijay Khemka
55939a6431SVijay Khemka /* Parse input parameter */
56939a6431SVijay Khemka try
57939a6431SVijay Khemka {
58939a6431SVijay Khemka app.parse(argc, argv);
59939a6431SVijay Khemka }
6067554144SPatrick Williams catch (const CLI::Error& e)
61939a6431SVijay Khemka {
62939a6431SVijay Khemka return app.exit(e);
63939a6431SVijay Khemka }
64939a6431SVijay Khemka
65939a6431SVijay Khemka /* Get list of gpio config details from json file */
66939a6431SVijay Khemka std::ifstream file(gpioFileName);
67939a6431SVijay Khemka if (!file)
68939a6431SVijay Khemka {
692a8848c6SGeorge Liu lg2::error("GPIO monitor config file not found: {FILE}", "FILE",
702a8848c6SGeorge Liu gpioFileName);
71939a6431SVijay Khemka return -1;
72939a6431SVijay Khemka }
73939a6431SVijay Khemka
74939a6431SVijay Khemka nlohmann::json gpioMonObj;
75939a6431SVijay Khemka file >> gpioMonObj;
76939a6431SVijay Khemka file.close();
77939a6431SVijay Khemka
78939a6431SVijay Khemka std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios;
79939a6431SVijay Khemka
80939a6431SVijay Khemka for (auto& obj : gpioMonObj)
81939a6431SVijay Khemka {
82939a6431SVijay Khemka /* GPIO Line message */
83939a6431SVijay Khemka std::string lineMsg = "GPIO Line ";
84939a6431SVijay Khemka
85939a6431SVijay Khemka /* GPIO line */
86cf33c593SJayanth Othayoth gpiod_line* line = nullptr;
87939a6431SVijay Khemka
88939a6431SVijay Khemka /* GPIO line configuration, default to monitor both edge */
89*1c88803fSPatrick Williams struct gpiod_line_request_config config{
90*1c88803fSPatrick Williams "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0};
91939a6431SVijay Khemka
92939a6431SVijay Khemka /* flag to monitor */
93939a6431SVijay Khemka bool flag = false;
94939a6431SVijay Khemka
95939a6431SVijay Khemka /* target to start */
96939a6431SVijay Khemka std::string target;
97939a6431SVijay Khemka
98a66ac0fcSDelphine CC Chiu /* multi targets to start */
99a66ac0fcSDelphine CC Chiu std::map<std::string, std::vector<std::string>> targets;
100a66ac0fcSDelphine CC Chiu
101939a6431SVijay Khemka if (obj.find("LineName") == obj.end())
102939a6431SVijay Khemka {
103939a6431SVijay Khemka /* If there is no line Name defined then gpio num nd chip
104939a6431SVijay Khemka * id must be defined. GpioNum is integer mapping to the
105939a6431SVijay Khemka * GPIO key configured by the kernel
106939a6431SVijay Khemka */
107939a6431SVijay Khemka if (obj.find("GpioNum") == obj.end() ||
108939a6431SVijay Khemka obj.find("ChipId") == obj.end())
109939a6431SVijay Khemka {
1102a8848c6SGeorge Liu lg2::error("Failed to find line name or gpio number: {FILE}",
1112a8848c6SGeorge Liu "FILE", gpioFileName);
112939a6431SVijay Khemka return -1;
113939a6431SVijay Khemka }
114939a6431SVijay Khemka
115939a6431SVijay Khemka std::string chipIdStr = obj["ChipId"];
116939a6431SVijay Khemka int gpioNum = obj["GpioNum"];
117939a6431SVijay Khemka
118939a6431SVijay Khemka lineMsg += std::to_string(gpioNum);
119939a6431SVijay Khemka
120939a6431SVijay Khemka /* Get the GPIO line */
121939a6431SVijay Khemka line = gpiod_line_get(chipIdStr.c_str(), gpioNum);
122939a6431SVijay Khemka }
123939a6431SVijay Khemka else
124939a6431SVijay Khemka {
125939a6431SVijay Khemka /* Find the GPIO line */
126939a6431SVijay Khemka std::string lineName = obj["LineName"];
127939a6431SVijay Khemka lineMsg += lineName;
128939a6431SVijay Khemka line = gpiod_line_find(lineName.c_str());
129939a6431SVijay Khemka }
130939a6431SVijay Khemka
131cf33c593SJayanth Othayoth if (line == nullptr)
132939a6431SVijay Khemka {
13338cd74c8SYang Chen lg2::error("Failed to find the {GPIO}", "GPIO", lineMsg);
13438cd74c8SYang Chen continue;
135939a6431SVijay Khemka }
136939a6431SVijay Khemka
137939a6431SVijay Khemka /* Get event to be monitored, if it is not defined then
138939a6431SVijay Khemka * Both rising falling edge will be monitored.
139939a6431SVijay Khemka */
140939a6431SVijay Khemka if (obj.find("EventMon") != obj.end())
141939a6431SVijay Khemka {
142939a6431SVijay Khemka std::string eventStr = obj["EventMon"];
143939a6431SVijay Khemka auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
144939a6431SVijay Khemka if (findEvent == phosphor::gpio::polarityMap.end())
145939a6431SVijay Khemka {
1462a8848c6SGeorge Liu lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg,
1472a8848c6SGeorge Liu "EVENT", eventStr);
148939a6431SVijay Khemka return -1;
149939a6431SVijay Khemka }
150939a6431SVijay Khemka
151939a6431SVijay Khemka config.request_type = findEvent->second;
152939a6431SVijay Khemka }
153939a6431SVijay Khemka
154939a6431SVijay Khemka /* Get flag if monitoring needs to continue after first event */
155939a6431SVijay Khemka if (obj.find("Continue") != obj.end())
156939a6431SVijay Khemka {
157939a6431SVijay Khemka flag = obj["Continue"];
158939a6431SVijay Khemka }
159939a6431SVijay Khemka
160939a6431SVijay Khemka /* Parse out target argument. It is fine if the user does not
161939a6431SVijay Khemka * pass this if they are not interested in calling into any target
162939a6431SVijay Khemka * on meeting a condition.
163939a6431SVijay Khemka */
164939a6431SVijay Khemka if (obj.find("Target") != obj.end())
165939a6431SVijay Khemka {
166939a6431SVijay Khemka target = obj["Target"];
167939a6431SVijay Khemka }
168939a6431SVijay Khemka
169a66ac0fcSDelphine CC Chiu /* Parse out the targets argument if multi-targets are needed.*/
170a66ac0fcSDelphine CC Chiu if (obj.find("Targets") != obj.end())
171a66ac0fcSDelphine CC Chiu {
172a66ac0fcSDelphine CC Chiu obj.at("Targets").get_to(targets);
173a66ac0fcSDelphine CC Chiu }
174a66ac0fcSDelphine CC Chiu
175939a6431SVijay Khemka /* Create a monitor object and let it do all the rest */
176939a6431SVijay Khemka gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>(
177a66ac0fcSDelphine CC Chiu line, config, io, target, targets, lineMsg, flag));
178939a6431SVijay Khemka }
179939a6431SVijay Khemka io.run();
180939a6431SVijay Khemka
181939a6431SVijay Khemka return 0;
182939a6431SVijay Khemka }
183