-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgpioMonMain.cpp
182 lines (150 loc) · 5.17 KB
/
gpioMonMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright © 2019 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gpioMon.hpp"
#include <CLI/CLI.hpp>
#include <boost/asio/io_context.hpp>
#include <nlohmann/json.hpp>
#include <phosphor-logging/lg2.hpp>
#include <fstream>
namespace phosphor
{
namespace gpio
{
std::map<std::string, int> polarityMap = {
/**< Only watch falling edge events. */
{"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE},
/**< Only watch rising edge events. */
{"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE},
/**< Monitor both types of events. */
{"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}};
}
} // namespace phosphor
int main(int argc, char** argv)
{
boost::asio::io_context io;
CLI::App app{"Monitor GPIO line for requested state change"};
std::string gpioFileName;
/* Add an input option */
app.add_option("-c,--config", gpioFileName, "Name of config json file")
->required()
->check(CLI::ExistingFile);
/* Parse input parameter */
try
{
app.parse(argc, argv);
}
catch (const CLI::Error& e)
{
return app.exit(e);
}
/* Get list of gpio config details from json file */
std::ifstream file(gpioFileName);
if (!file)
{
lg2::error("GPIO monitor config file not found: {FILE}", "FILE",
gpioFileName);
return -1;
}
nlohmann::json gpioMonObj;
file >> gpioMonObj;
file.close();
std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios;
for (auto& obj : gpioMonObj)
{
/* GPIO Line message */
std::string lineMsg = "GPIO Line ";
/* GPIO line */
gpiod_line* line = nullptr;
/* GPIO line configuration, default to monitor both edge */
struct gpiod_line_request_config config{
"gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0};
/* flag to monitor */
bool flag = false;
/* target to start */
std::string target;
/* multi targets to start */
std::map<std::string, std::vector<std::string>> targets;
if (obj.find("LineName") == obj.end())
{
/* If there is no line Name defined then gpio num nd chip
* id must be defined. GpioNum is integer mapping to the
* GPIO key configured by the kernel
*/
if (obj.find("GpioNum") == obj.end() ||
obj.find("ChipId") == obj.end())
{
lg2::error("Failed to find line name or gpio number: {FILE}",
"FILE", gpioFileName);
return -1;
}
std::string chipIdStr = obj["ChipId"];
int gpioNum = obj["GpioNum"];
lineMsg += std::to_string(gpioNum);
/* Get the GPIO line */
line = gpiod_line_get(chipIdStr.c_str(), gpioNum);
}
else
{
/* Find the GPIO line */
std::string lineName = obj["LineName"];
lineMsg += lineName;
line = gpiod_line_find(lineName.c_str());
}
if (line == nullptr)
{
lg2::error("Failed to find the {GPIO}", "GPIO", lineMsg);
continue;
}
/* Get event to be monitored, if it is not defined then
* Both rising falling edge will be monitored.
*/
if (obj.find("EventMon") != obj.end())
{
std::string eventStr = obj["EventMon"];
auto findEvent = phosphor::gpio::polarityMap.find(eventStr);
if (findEvent == phosphor::gpio::polarityMap.end())
{
lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg,
"EVENT", eventStr);
return -1;
}
config.request_type = findEvent->second;
}
/* Get flag if monitoring needs to continue after first event */
if (obj.find("Continue") != obj.end())
{
flag = obj["Continue"];
}
/* Parse out target argument. It is fine if the user does not
* pass this if they are not interested in calling into any target
* on meeting a condition.
*/
if (obj.find("Target") != obj.end())
{
target = obj["Target"];
}
/* Parse out the targets argument if multi-targets are needed.*/
if (obj.find("Targets") != obj.end())
{
obj.at("Targets").get_to(targets);
}
/* Create a monitor object and let it do all the rest */
gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>(
line, config, io, target, targets, lineMsg, flag));
}
io.run();
return 0;
}