blob: 8530ffcf149a28d0a7928780a06aac7363c4d5f0 [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 <fstream>
22#include <nlohmann/json.hpp>
23#include <phosphor-logging/log.hpp>
24
25using namespace phosphor::logging;
26
27namespace phosphor
28{
29namespace gpio
30{
31
32std::map<std::string, int> polarityMap = {
33 /**< Only watch falling edge events. */
34 {"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE},
35 /**< Only watch rising edge events. */
36 {"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE},
37 /**< Monitor both types of events. */
38 {"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}};
39
40}
41} // namespace phosphor
42
43int main(int argc, char** argv)
44{
45
Ed Tanous854404e2023-02-28 13:37:51 -080046 boost::asio::io_context io;
Vijay Khemka939a6432019-10-09 17:45:45 -070047
48 CLI::App app{"Monitor GPIO line for requested state change"};
49
50 std::string gpioFileName;
51
52 /* Add an input option */
53 app.add_option("-c,--config", gpioFileName, "Name of config json file")
54 ->required()
55 ->check(CLI::ExistingFile);
56
57 /* Parse input parameter */
58 try
59 {
60 app.parse(argc, argv);
61 }
Patrick Williams67554142021-10-06 13:00:15 -050062 catch (const CLI::Error& e)
Vijay Khemka939a6432019-10-09 17:45:45 -070063 {
64 return app.exit(e);
65 }
66
67 /* Get list of gpio config details from json file */
68 std::ifstream file(gpioFileName);
69 if (!file)
70 {
71 log<level::ERR>("GPIO monitor config file not found",
72 entry("GPIO_MON_FILE=%s", gpioFileName.c_str()));
73 return -1;
74 }
75
76 nlohmann::json gpioMonObj;
77 file >> gpioMonObj;
78 file.close();
79
80 std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios;
81
82 for (auto& obj : gpioMonObj)
83 {
84
85 /* GPIO Line message */
86 std::string lineMsg = "GPIO Line ";
87
88 /* GPIO line */
89 gpiod_line* line = NULL;
90
91 /* Log message string */
92 std::string errMsg;
93
94 /* GPIO line configuration, default to monitor both edge */
95 struct gpiod_line_request_config config
96 {
97 "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0
98 };
99
100 /* flag to monitor */
101 bool flag = false;
102
103 /* target to start */
104 std::string target;
105
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800106 /* multi targets to start */
107 std::map<std::string, std::vector<std::string>> targets;
108
Vijay Khemka939a6432019-10-09 17:45:45 -0700109 if (obj.find("LineName") == obj.end())
110 {
111 /* If there is no line Name defined then gpio num nd chip
112 * id must be defined. GpioNum is integer mapping to the
113 * GPIO key configured by the kernel
114 */
115 if (obj.find("GpioNum") == obj.end() ||
116 obj.find("ChipId") == obj.end())
117 {
118 log<level::ERR>(
119 "Failed to find line name or gpio number",
120 entry("GPIO_JSON_FILE_NAME=%s", gpioFileName.c_str()));
121 return -1;
122 }
123
124 std::string chipIdStr = obj["ChipId"];
125 int gpioNum = obj["GpioNum"];
126
127 lineMsg += std::to_string(gpioNum);
128
129 /* Get the GPIO line */
130 line = gpiod_line_get(chipIdStr.c_str(), gpioNum);
131 }
132 else
133 {
134 /* Find the GPIO line */
135 std::string lineName = obj["LineName"];
136 lineMsg += lineName;
137 line = gpiod_line_find(lineName.c_str());
138 }
139
140 if (line == NULL)
141 {
142 errMsg = "Failed to find the " + lineMsg;
143 log<level::ERR>(errMsg.c_str());
144 return -1;
145 }
146
147 /* Get event to be monitored, if it is not defined then
148 * Both rising falling edge will be monitored.
149 */
150 if (obj.find("EventMon") != obj.end())
151 {
152 std::string eventStr = obj["EventMon"];
153 auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
154 if (findEvent == phosphor::gpio::polarityMap.end())
155 {
156 errMsg = "Incorrect GPIO monitor event defined " + lineMsg;
157 log<level::ERR>(errMsg.c_str());
158 return -1;
159 }
160
161 config.request_type = findEvent->second;
162 }
163
164 /* Get flag if monitoring needs to continue after first event */
165 if (obj.find("Continue") != obj.end())
166 {
167 flag = obj["Continue"];
168 }
169
170 /* Parse out target argument. It is fine if the user does not
171 * pass this if they are not interested in calling into any target
172 * on meeting a condition.
173 */
174 if (obj.find("Target") != obj.end())
175 {
176 target = obj["Target"];
177 }
178
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800179 /* Parse out the targets argument if multi-targets are needed.*/
180 if (obj.find("Targets") != obj.end())
181 {
182 obj.at("Targets").get_to(targets);
183 }
184
Vijay Khemka939a6432019-10-09 17:45:45 -0700185 /* Create a monitor object and let it do all the rest */
186 gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>(
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +0800187 line, config, io, target, targets, lineMsg, flag));
Vijay Khemka939a6432019-10-09 17:45:45 -0700188 }
189 io.run();
190
191 return 0;
192}