-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgpioMon.cpp
150 lines (129 loc) · 4.12 KB
/
gpioMon.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
/**
* 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 <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
namespace phosphor
{
namespace gpio
{
/* systemd service to kick start a target. */
constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
constexpr auto falling = "FALLING";
constexpr auto rising = "RISING";
void GpioMonitor::scheduleEventHandler()
{
gpioEventDescriptor.async_wait(
boost::asio::posix::stream_descriptor::wait_read,
[this](const boost::system::error_code& ec) {
if (ec)
{
lg2::error("{GPIO} event handler error: {ERROR}", "GPIO",
gpioLineMsg, "ERROR", ec.message());
return;
}
gpioEventHandler();
});
}
void GpioMonitor::gpioEventHandler()
{
gpiod_line_event gpioLineEvent;
if (gpiod_line_event_read_fd(gpioEventDescriptor.native_handle(),
&gpioLineEvent) < 0)
{
lg2::error("Failed to read {GPIO} from fd", "GPIO", gpioLineMsg);
return;
}
if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
{
lg2::info("{GPIO} Asserted", "GPIO", gpioLineMsg);
}
else
{
lg2::info("{GPIO} Deasserted", "GPIO", gpioLineMsg);
}
/* Execute the target if it is defined. */
if (!target.empty())
{
auto bus = sdbusplus::bus::new_default();
auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
SYSTEMD_INTERFACE, "StartUnit");
method.append(target);
method.append("replace");
bus.call_noreply(method);
}
std::vector<std::string> targetsToStart;
if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
{
auto risingFind = targets.find(rising);
if (risingFind != targets.end())
{
targetsToStart = risingFind->second;
}
}
else
{
auto fallingFind = targets.find(falling);
if (fallingFind != targets.end())
{
targetsToStart = fallingFind->second;
}
}
/* Execute the multi targets if it is defined. */
if (!targetsToStart.empty())
{
auto bus = sdbusplus::bus::new_default();
for (auto& tar : targetsToStart)
{
auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
SYSTEMD_INTERFACE, "StartUnit");
method.append(tar, "replace");
bus.call_noreply(method);
}
}
/* if not required to continue monitoring then return */
if (!continueAfterEvent)
{
return;
}
/* Schedule a wait event */
scheduleEventHandler();
}
int GpioMonitor::requestGPIOEvents()
{
/* Request an event to monitor for respected gpio line */
if (gpiod_line_request(gpioLine, &gpioConfig, 0) < 0)
{
lg2::error("Failed to request {GPIO}", "GPIO", gpioLineMsg);
return -1;
}
int gpioLineFd = gpiod_line_event_get_fd(gpioLine);
if (gpioLineFd < 0)
{
lg2::error("Failed to get fd for {GPIO}", "GPIO", gpioLineMsg);
return -1;
}
lg2::info("{GPIO} monitoring started", "GPIO", gpioLineMsg);
/* Assign line fd to descriptor for monitoring */
gpioEventDescriptor.assign(gpioLineFd);
/* Schedule a wait event */
scheduleEventHandler();
return 0;
}
} // namespace gpio
} // namespace phosphor