blob: 56cf6f70930ec81983208f573bbb2f8f3f8fbe2b [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
George Liu2a8848c2023-08-01 13:49:28 +080019#include <phosphor-logging/lg2.hpp>
Vijay Khemka939a6432019-10-09 17:45:45 -070020#include <sdbusplus/bus.hpp>
21
22namespace phosphor
23{
24namespace gpio
25{
26
27/* systemd service to kick start a target. */
28constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
29constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
30constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
31
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080032constexpr auto falling = "FALLING";
33constexpr auto rising = "RISING";
34
Vijay Khemka939a6432019-10-09 17:45:45 -070035void GpioMonitor::scheduleEventHandler()
36{
Vijay Khemka939a6432019-10-09 17:45:45 -070037 gpioEventDescriptor.async_wait(
38 boost::asio::posix::stream_descriptor::wait_read,
39 [this](const boost::system::error_code& ec) {
Patrick Williams39084b42023-05-10 07:50:58 -050040 if (ec)
41 {
George Liu2a8848c2023-08-01 13:49:28 +080042 lg2::error("{GPIO} event handler error: {ERROR}", "GPIO",
43 gpioLineMsg, "ERROR", ec.message());
Patrick Williams39084b42023-05-10 07:50:58 -050044 return;
45 }
46 gpioEventHandler();
Patrick Williamsd3898c52023-10-20 11:19:20 -050047 });
Vijay Khemka939a6432019-10-09 17:45:45 -070048}
49
50void GpioMonitor::gpioEventHandler()
51{
52 gpiod_line_event gpioLineEvent;
53
54 if (gpiod_line_event_read_fd(gpioEventDescriptor.native_handle(),
55 &gpioLineEvent) < 0)
56 {
George Liu2a8848c2023-08-01 13:49:28 +080057 lg2::error("Failed to read {GPIO} from fd", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -070058 return;
59 }
60
George Liu2a8848c2023-08-01 13:49:28 +080061 if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
62 {
63 lg2::info("{GPIO} Asserted", "GPIO", gpioLineMsg);
64 }
65 else
66 {
67 lg2::info("{GPIO} Deasserted", "GPIO", gpioLineMsg);
68 }
Vijay Khemka939a6432019-10-09 17:45:45 -070069
70 /* Execute the target if it is defined. */
71 if (!target.empty())
72 {
73 auto bus = sdbusplus::bus::new_default();
74 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
75 SYSTEMD_INTERFACE, "StartUnit");
76 method.append(target);
77 method.append("replace");
78
79 bus.call_noreply(method);
80 }
81
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080082 std::vector<std::string> targetsToStart;
83 if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
84 {
85 auto risingFind = targets.find(rising);
86 if (risingFind != targets.end())
87 {
88 targetsToStart = risingFind->second;
89 }
90 }
91 else
92 {
93 auto fallingFind = targets.find(falling);
94 if (fallingFind != targets.end())
95 {
96 targetsToStart = fallingFind->second;
97 }
98 }
99
100 /* Execute the multi targets if it is defined. */
101 if (!targetsToStart.empty())
102 {
103 auto bus = sdbusplus::bus::new_default();
104 for (auto& tar : targetsToStart)
105 {
106 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
107 SYSTEMD_INTERFACE, "StartUnit");
108 method.append(tar, "replace");
109 bus.call_noreply(method);
110 }
111 }
112
Vijay Khemka939a6432019-10-09 17:45:45 -0700113 /* if not required to continue monitoring then return */
114 if (!continueAfterEvent)
115 {
116 return;
117 }
118
119 /* Schedule a wait event */
120 scheduleEventHandler();
121}
122
123int GpioMonitor::requestGPIOEvents()
124{
Vijay Khemka939a6432019-10-09 17:45:45 -0700125 /* Request an event to monitor for respected gpio line */
126 if (gpiod_line_request(gpioLine, &gpioConfig, 0) < 0)
127 {
George Liu2a8848c2023-08-01 13:49:28 +0800128 lg2::error("Failed to request {GPIO}", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700129 return -1;
130 }
131
132 int gpioLineFd = gpiod_line_event_get_fd(gpioLine);
133 if (gpioLineFd < 0)
134 {
George Liu2a8848c2023-08-01 13:49:28 +0800135 lg2::error("Failed to get fd for {GPIO}", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700136 return -1;
137 }
138
George Liu2a8848c2023-08-01 13:49:28 +0800139 lg2::info("{GPIO} monitoring started", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700140
141 /* Assign line fd to descriptor for monitoring */
142 gpioEventDescriptor.assign(gpioLineFd);
143
144 /* Schedule a wait event */
145 scheduleEventHandler();
146
147 return 0;
148}
149} // namespace gpio
150} // namespace phosphor