blob: 09f92cb6ad45bdb815d6d788ef209dae27ef402c [file] [log] [blame]
Vijay Khemka939a6432019-10-09 17:45:45 -07001/**
2 * Copyright © 2019 Facebook
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 "gpioMon.hpp"
18
19#include <CLI/CLI.hpp>
Ed Tanous854404e2023-02-28 13:37:51 -080020#include <boost/asio/io_context.hpp>
Vijay Khemka939a6432019-10-09 17:45:45 -070021#include <nlohmann/json.hpp>
George Liu2a8848c2023-08-01 13:49:28 +080022#include <phosphor-logging/lg2.hpp>
Vijay Khemka939a6432019-10-09 17:45:45 -070023
Patrick Williams39084b42023-05-10 07:50:58 -050024#include <fstream>
25
Vijay Khemka939a6432019-10-09 17:45:45 -070026namespace phosphor
27{
28namespace gpio
29{
30
31std::map<std::string, int> polarityMap = {
32 /**< Only watch falling edge events. */
33 {"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE},
34 /**< Only watch rising edge events. */
35 {"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE},
36 /**< Monitor both types of events. */
37 {"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}};
38
39}
40} // namespace phosphor
41
42int main(int argc, char** argv)
43{
Ed Tanous854404e2023-02-28 13:37:51 -080044 boost::asio::io_context io;
Vijay Khemka939a6432019-10-09 17:45:45 -070045
46 CLI::App app{"Monitor GPIO line for requested state change"};
47
48 std::string gpioFileName;
49
50 /* Add an input option */
51 app.add_option("-c,--config", gpioFileName, "Name of config json file")
52 ->required()
53 ->check(CLI::ExistingFile);
54
55 /* Parse input parameter */
56 try
57 {
58 app.parse(argc, argv);
59 }
Patrick Williams67554142021-10-06 13:00:15 -050060 catch (const CLI::Error& e)
Vijay Khemka939a6432019-10-09 17:45:45 -070061 {
62 return app.exit(e);
63 }
64
65 /* Get list of gpio config details from json file */
66 std::ifstream file(gpioFileName);
67 if (!file)
68 {
George Liu2a8848c2023-08-01 13:49:28 +080069 lg2::error("GPIO monitor config file not found: {FILE}", "FILE",
70 gpioFileName);
Vijay Khemka939a6432019-10-09 17:45:45 -070071 return -1;
72 }
73
74 nlohmann::json gpioMonObj;
75 file >> gpioMonObj;
76 file.close();
77
78 std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios;
79
80 for (auto& obj : gpioMonObj)
81 {
Vijay Khemka939a6432019-10-09 17:45:45 -070082 /* GPIO Line message */
83 std::string lineMsg = "GPIO Line ";
84
85 /* GPIO line */
86 gpiod_line* line = NULL;
87
88 /* Log message string */
89 std::string errMsg;
90
91 /* GPIO line configuration, default to monitor both edge */
92 struct gpiod_line_request_config config
93 {
94 "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0
95 };
96
97 /* flag to monitor */
98 bool flag = false;
99
100 /* target to start */
101 std::string target;
102
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800103 /* multi targets to start */
104 std::map<std::string, std::vector<std::string>> targets;
105
Vijay Khemka939a6432019-10-09 17:45:45 -0700106 if (obj.find("LineName") == obj.end())
107 {
108 /* If there is no line Name defined then gpio num nd chip
109 * id must be defined. GpioNum is integer mapping to the
110 * GPIO key configured by the kernel
111 */
112 if (obj.find("GpioNum") == obj.end() ||
113 obj.find("ChipId") == obj.end())
114 {
George Liu2a8848c2023-08-01 13:49:28 +0800115 lg2::error("Failed to find line name or gpio number: {FILE}",
116 "FILE", gpioFileName);
Vijay Khemka939a6432019-10-09 17:45:45 -0700117 return -1;
118 }
119
120 std::string chipIdStr = obj["ChipId"];
121 int gpioNum = obj["GpioNum"];
122
123 lineMsg += std::to_string(gpioNum);
124
125 /* Get the GPIO line */
126 line = gpiod_line_get(chipIdStr.c_str(), gpioNum);
127 }
128 else
129 {
130 /* Find the GPIO line */
131 std::string lineName = obj["LineName"];
132 lineMsg += lineName;
133 line = gpiod_line_find(lineName.c_str());
134 }
135
136 if (line == NULL)
137 {
George Liu2a8848c2023-08-01 13:49:28 +0800138 lg2::error("Failed to find the {GPIO}", "GPIO", errMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700139 return -1;
140 }
141
142 /* Get event to be monitored, if it is not defined then
143 * Both rising falling edge will be monitored.
144 */
145 if (obj.find("EventMon") != obj.end())
146 {
147 std::string eventStr = obj["EventMon"];
148 auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
149 if (findEvent == phosphor::gpio::polarityMap.end())
150 {
George Liu2a8848c2023-08-01 13:49:28 +0800151 lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg,
152 "EVENT", eventStr);
Vijay Khemka939a6432019-10-09 17:45:45 -0700153 return -1;
154 }
155
156 config.request_type = findEvent->second;
157 }
158
159 /* Get flag if monitoring needs to continue after first event */
160 if (obj.find("Continue") != obj.end())
161 {
162 flag = obj["Continue"];
163 }
164
165 /* Parse out target argument. It is fine if the user does not
166 * pass this if they are not interested in calling into any target
167 * on meeting a condition.
168 */
169 if (obj.find("Target") != obj.end())
170 {
171 target = obj["Target"];
172 }
173
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800174 /* Parse out the targets argument if multi-targets are needed.*/
175 if (obj.find("Targets") != obj.end())
176 {
177 obj.at("Targets").get_to(targets);
178 }
179
Vijay Khemka939a6432019-10-09 17:45:45 -0700180 /* Create a monitor object and let it do all the rest */
181 gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>(
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800182 line, config, io, target, targets, lineMsg, flag));
Vijay Khemka939a6432019-10-09 17:45:45 -0700183 }
184 io.run();
185
186 return 0;
187}