1 /**
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>
20 #include <boost/asio/io_context.hpp>
21 #include <nlohmann/json.hpp>
22 #include <phosphor-logging/lg2.hpp>
23
24 #include <fstream>
25
26 namespace phosphor
27 {
28 namespace gpio
29 {
30
31 std::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
main(int argc,char ** argv)42 int main(int argc, char** argv)
43 {
44 boost::asio::io_context io;
45
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 }
60 catch (const CLI::Error& e)
61 {
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 {
69 lg2::error("GPIO monitor config file not found: {FILE}", "FILE",
70 gpioFileName);
71 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 {
82 /* GPIO Line message */
83 std::string lineMsg = "GPIO Line ";
84
85 /* GPIO line */
86 gpiod_line* line = nullptr;
87
88 /* GPIO line configuration, default to monitor both edge */
89 struct gpiod_line_request_config config{
90 "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0};
91
92 /* flag to monitor */
93 bool flag = false;
94
95 /* target to start */
96 std::string target;
97
98 /* multi targets to start */
99 std::map<std::string, std::vector<std::string>> targets;
100
101 if (obj.find("LineName") == obj.end())
102 {
103 /* If there is no line Name defined then gpio num nd chip
104 * id must be defined. GpioNum is integer mapping to the
105 * GPIO key configured by the kernel
106 */
107 if (obj.find("GpioNum") == obj.end() ||
108 obj.find("ChipId") == obj.end())
109 {
110 lg2::error("Failed to find line name or gpio number: {FILE}",
111 "FILE", gpioFileName);
112 return -1;
113 }
114
115 std::string chipIdStr = obj["ChipId"];
116 int gpioNum = obj["GpioNum"];
117
118 lineMsg += std::to_string(gpioNum);
119
120 /* Get the GPIO line */
121 line = gpiod_line_get(chipIdStr.c_str(), gpioNum);
122 }
123 else
124 {
125 /* Find the GPIO line */
126 std::string lineName = obj["LineName"];
127 lineMsg += lineName;
128 line = gpiod_line_find(lineName.c_str());
129 }
130
131 if (line == nullptr)
132 {
133 lg2::error("Failed to find the {GPIO}", "GPIO", lineMsg);
134 continue;
135 }
136
137 /* Get event to be monitored, if it is not defined then
138 * Both rising falling edge will be monitored.
139 */
140 if (obj.find("EventMon") != obj.end())
141 {
142 std::string eventStr = obj["EventMon"];
143 auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
144 if (findEvent == phosphor::gpio::polarityMap.end())
145 {
146 lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg,
147 "EVENT", eventStr);
148 return -1;
149 }
150
151 config.request_type = findEvent->second;
152 }
153
154 /* Get flag if monitoring needs to continue after first event */
155 if (obj.find("Continue") != obj.end())
156 {
157 flag = obj["Continue"];
158 }
159
160 /* Parse out target argument. It is fine if the user does not
161 * pass this if they are not interested in calling into any target
162 * on meeting a condition.
163 */
164 if (obj.find("Target") != obj.end())
165 {
166 target = obj["Target"];
167 }
168
169 /* Parse out the targets argument if multi-targets are needed.*/
170 if (obj.find("Targets") != obj.end())
171 {
172 obj.at("Targets").get_to(targets);
173 }
174
175 /* Create a monitor object and let it do all the rest */
176 gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>(
177 line, config, io, target, targets, lineMsg, flag));
178 }
179 io.run();
180
181 return 0;
182 }
183