blob: 20805cde905baf85f3649917a24a0e7c8bad8351 [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";
Amithash Prasadba917cf2025-04-21 14:52:43 -070034constexpr auto init_high = "INIT_HIGH";
35constexpr auto init_low = "INIT_LOW";
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080036
Vijay Khemka939a6432019-10-09 17:45:45 -070037void GpioMonitor::scheduleEventHandler()
38{
Vijay Khemka939a6432019-10-09 17:45:45 -070039 gpioEventDescriptor.async_wait(
40 boost::asio::posix::stream_descriptor::wait_read,
41 [this](const boost::system::error_code& ec) {
Patrick Williams8377d592024-08-16 15:21:08 -040042 if (ec)
43 {
44 lg2::error("{GPIO} event handler error: {ERROR}", "GPIO",
45 gpioLineMsg, "ERROR", ec.message());
46 return;
47 }
48 gpioEventHandler();
49 });
Vijay Khemka939a6432019-10-09 17:45:45 -070050}
51
52void GpioMonitor::gpioEventHandler()
53{
54 gpiod_line_event gpioLineEvent;
55
56 if (gpiod_line_event_read_fd(gpioEventDescriptor.native_handle(),
57 &gpioLineEvent) < 0)
58 {
George Liu2a8848c2023-08-01 13:49:28 +080059 lg2::error("Failed to read {GPIO} from fd", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -070060 return;
61 }
62
George Liu2a8848c2023-08-01 13:49:28 +080063 if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
64 {
65 lg2::info("{GPIO} Asserted", "GPIO", gpioLineMsg);
66 }
67 else
68 {
69 lg2::info("{GPIO} Deasserted", "GPIO", gpioLineMsg);
70 }
Vijay Khemka939a6432019-10-09 17:45:45 -070071
72 /* Execute the target if it is defined. */
73 if (!target.empty())
74 {
75 auto bus = sdbusplus::bus::new_default();
76 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
77 SYSTEMD_INTERFACE, "StartUnit");
78 method.append(target);
79 method.append("replace");
80
81 bus.call_noreply(method);
82 }
83
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080084 std::vector<std::string> targetsToStart;
85 if (gpioLineEvent.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
86 {
87 auto risingFind = targets.find(rising);
88 if (risingFind != targets.end())
89 {
90 targetsToStart = risingFind->second;
91 }
92 }
93 else
94 {
95 auto fallingFind = targets.find(falling);
96 if (fallingFind != targets.end())
97 {
98 targetsToStart = fallingFind->second;
99 }
100 }
101
102 /* Execute the multi targets if it is defined. */
103 if (!targetsToStart.empty())
104 {
105 auto bus = sdbusplus::bus::new_default();
106 for (auto& tar : targetsToStart)
107 {
108 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
109 SYSTEMD_INTERFACE, "StartUnit");
110 method.append(tar, "replace");
111 bus.call_noreply(method);
112 }
113 }
114
Vijay Khemka939a6432019-10-09 17:45:45 -0700115 /* if not required to continue monitoring then return */
116 if (!continueAfterEvent)
117 {
118 return;
119 }
120
121 /* Schedule a wait event */
122 scheduleEventHandler();
123}
124
Amithash Prasadba917cf2025-04-21 14:52:43 -0700125void GpioMonitor::gpioHandleInitialState(bool value)
126{
127 if (auto itr = targets.find(value ? init_high : init_low);
128 itr != targets.end())
129 {
130 auto bus = sdbusplus::bus::new_default();
131 for (const auto& tar : itr->second)
132 {
133 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
134 SYSTEMD_INTERFACE, "StartUnit");
135 method.append(tar, "replace");
136 bus.call_noreply(method);
137 }
138 }
139}
140
Vijay Khemka939a6432019-10-09 17:45:45 -0700141int GpioMonitor::requestGPIOEvents()
142{
Vijay Khemka939a6432019-10-09 17:45:45 -0700143 /* Request an event to monitor for respected gpio line */
144 if (gpiod_line_request(gpioLine, &gpioConfig, 0) < 0)
145 {
George Liu2a8848c2023-08-01 13:49:28 +0800146 lg2::error("Failed to request {GPIO}", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700147 return -1;
148 }
149
150 int gpioLineFd = gpiod_line_event_get_fd(gpioLine);
151 if (gpioLineFd < 0)
152 {
George Liu2a8848c2023-08-01 13:49:28 +0800153 lg2::error("Failed to get fd for {GPIO}", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700154 return -1;
155 }
156
Amithash Prasadba917cf2025-04-21 14:52:43 -0700157 int value = gpiod_line_get_value(gpioLine);
158 if (value < 0)
159 {
160 lg2::error("Failed to get value for {GPIO} Error: {ERROR}", "GPIO",
161 gpioLineMsg, "ERROR", strerror(errno));
162 }
163 else
164 {
165 gpioHandleInitialState(value != 0);
166 }
167
George Liu2a8848c2023-08-01 13:49:28 +0800168 lg2::info("{GPIO} monitoring started", "GPIO", gpioLineMsg);
Vijay Khemka939a6432019-10-09 17:45:45 -0700169
170 /* Assign line fd to descriptor for monitoring */
171 gpioEventDescriptor.assign(gpioLineFd);
172
173 /* Schedule a wait event */
174 scheduleEventHandler();
175
176 return 0;
177}
178} // namespace gpio
179} // namespace phosphor