blob: 395e520ca78461925a68fe6e1886e5830cfdd5b8 [file] [log] [blame]
Jim Wright19920832021-08-25 11:13:56 -05001/**
2 * Copyright © 2021 IBM Corporation
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 "power_interface.hpp"
18
19#include "types.hpp"
20
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +000021#include <phosphor-logging/lg2.hpp>
Jim Wright19920832021-08-25 11:13:56 -050022#include <sdbusplus/exception.hpp>
23#include <sdbusplus/sdbus.hpp>
24#include <sdbusplus/server.hpp>
25
26#include <string>
27#include <tuple>
28
Jim Wright19920832021-08-25 11:13:56 -050029namespace phosphor::power::sequencer
30{
31
Patrick Williams7354ce62022-07-22 19:26:56 -050032PowerInterface::PowerInterface(sdbusplus::bus_t& bus, const char* path) :
Jim Wright213ffe92022-06-03 08:54:30 -050033 serverInterface(bus, path, POWER_IFACE, vtable, this)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000034{}
Jim Wright19920832021-08-25 11:13:56 -050035
Patrick Williamsf5402192024-08-16 15:20:53 -040036int PowerInterface::callbackGetPgood(
37 sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
38 const char* /*property*/, sd_bus_message* msg, void* context,
39 sd_bus_error* error)
Jim Wright19920832021-08-25 11:13:56 -050040{
41 if (msg != nullptr && context != nullptr)
42 {
43 try
44 {
45 auto pwrObj = static_cast<PowerInterface*>(context);
46 int pgood = pwrObj->getPgood();
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +000047 lg2::debug("callbackGetPgood: {PGOOD}", "PGOOD", pgood);
Jim Wright19920832021-08-25 11:13:56 -050048
Patrick Williams7354ce62022-07-22 19:26:56 -050049 sdbusplus::message_t(msg).append(pgood);
Jim Wright19920832021-08-25 11:13:56 -050050 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050051 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -050052 {
53 return sd_bus_error_set(error, e.name(), e.description());
54 }
55 }
56 else
57 {
58 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +000059 lg2::error("Unable to service get pgood property callback");
Jim Wright19920832021-08-25 11:13:56 -050060 return -1;
61 }
62
63 return 1;
64}
65
Patrick Williamsf5402192024-08-16 15:20:53 -040066int PowerInterface::callbackGetPgoodTimeout(
67 sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
68 const char* /*property*/, sd_bus_message* msg, void* context,
69 sd_bus_error* error)
Jim Wright19920832021-08-25 11:13:56 -050070{
71 if (msg != nullptr && context != nullptr)
72 {
73 try
74 {
75 auto pwrObj = static_cast<PowerInterface*>(context);
76 int timeout = pwrObj->getPgoodTimeout();
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +000077 lg2::debug("callbackGetPgoodTimeout: {TIMEOUT}", "TIMEOUT",
78 timeout);
Jim Wright19920832021-08-25 11:13:56 -050079
Patrick Williams7354ce62022-07-22 19:26:56 -050080 sdbusplus::message_t(msg).append(timeout);
Jim Wright19920832021-08-25 11:13:56 -050081 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050082 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -050083 {
84 return sd_bus_error_set(error, e.name(), e.description());
85 }
86 }
87 else
88 {
89 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +000090 lg2::error("Unable to service get pgood timeout property callback");
Jim Wright19920832021-08-25 11:13:56 -050091 return -1;
92 }
93
94 return 1;
95}
96
97int PowerInterface::callbackGetPowerState(sd_bus_message* msg, void* context,
98 sd_bus_error* error)
99{
100 if (msg != nullptr && context != nullptr)
101 {
102 try
103 {
104 auto pwrObj = static_cast<PowerInterface*>(context);
105 // Return the current power state of the GPIO, rather than the last
106 // requested power state change
107 int pgood = pwrObj->getPgood();
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000108 lg2::debug("callbackGetPowerState: {PGOOD}", "PGOOD", pgood);
Jim Wright19920832021-08-25 11:13:56 -0500109
Patrick Williams7354ce62022-07-22 19:26:56 -0500110 auto reply = sdbusplus::message_t(msg).new_method_return();
Jim Wright19920832021-08-25 11:13:56 -0500111 reply.append(pgood);
112 reply.method_return();
113 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500114 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500115 {
116 return sd_bus_error_set(error, e.name(), e.description());
117 }
118 }
119 else
120 {
121 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000122 lg2::error("Unable to service getPowerState method callback");
Jim Wright19920832021-08-25 11:13:56 -0500123 return -1;
124 }
125
126 return 1;
127}
128
Patrick Williamsf5402192024-08-16 15:20:53 -0400129int PowerInterface::callbackSetPgoodTimeout(
130 sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
131 const char* /*property*/, sd_bus_message* msg, void* context,
132 sd_bus_error* error)
Jim Wright19920832021-08-25 11:13:56 -0500133{
134 if (msg != nullptr && context != nullptr)
135 {
136 try
137 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500138 auto m = sdbusplus::message_t(msg);
Jim Wright19920832021-08-25 11:13:56 -0500139
140 int timeout{};
141 m.read(timeout);
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000142 lg2::info("callbackSetPgoodTimeout: {TIMEOUT}", "TIMEOUT", timeout);
Jim Wrightccea2d22021-12-10 14:10:46 -0600143
144 auto pwrObj = static_cast<PowerInterface*>(context);
Jim Wright19920832021-08-25 11:13:56 -0500145 pwrObj->setPgoodTimeout(timeout);
146 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500147 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500148 {
149 return sd_bus_error_set(error, e.name(), e.description());
150 }
151 }
152 else
153 {
154 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000155 lg2::error("Unable to service set pgood timeout property callback");
Jim Wright19920832021-08-25 11:13:56 -0500156 return -1;
157 }
158
159 return 1;
160}
161
Patrick Williamsf5402192024-08-16 15:20:53 -0400162int PowerInterface::callbackGetState(
163 sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
164 const char* /*property*/, sd_bus_message* msg, void* context,
165 sd_bus_error* error)
Jim Wright19920832021-08-25 11:13:56 -0500166{
167 if (msg != nullptr && context != nullptr)
168 {
169 try
170 {
171 auto pwrObj = static_cast<PowerInterface*>(context);
172 int state = pwrObj->getState();
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000173 lg2::debug("callbackGetState: {STATE}", "STATE", state);
Jim Wright19920832021-08-25 11:13:56 -0500174
Patrick Williams7354ce62022-07-22 19:26:56 -0500175 sdbusplus::message_t(msg).append(state);
Jim Wright19920832021-08-25 11:13:56 -0500176 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500177 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500178 {
179 return sd_bus_error_set(error, e.name(), e.description());
180 }
181 }
182 else
183 {
184 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000185 lg2::error("Unable to service get state property callback");
Jim Wright19920832021-08-25 11:13:56 -0500186 return -1;
187 }
188
189 return 1;
190}
191
192int PowerInterface::callbackSetPowerState(sd_bus_message* msg, void* context,
193 sd_bus_error* error)
194{
195 if (msg != nullptr && context != nullptr)
196 {
197 try
198 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500199 auto m = sdbusplus::message_t(msg);
Jim Wright19920832021-08-25 11:13:56 -0500200
201 int state{};
202 m.read(state);
203
204 if (state != 1 && state != 0)
205 {
206 return sd_bus_error_set(error,
207 "org.openbmc.ControlPower.Error.Failed",
208 "Invalid power state");
209 }
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000210 lg2::info("callbackSetPowerState: {STATE}", "STATE", state);
Jim Wrightccea2d22021-12-10 14:10:46 -0600211
212 auto pwrObj = static_cast<PowerInterface*>(context);
Jim Wright19920832021-08-25 11:13:56 -0500213 pwrObj->setState(state);
214
215 m.new_method_return().method_return();
216 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500217 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500218 {
219 return sd_bus_error_set(error, e.name(), e.description());
220 }
221 }
222 else
223 {
224 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000225 lg2::error("Unable to service setPowerState method callback");
Jim Wright19920832021-08-25 11:13:56 -0500226 return -1;
227 }
228
229 return 1;
230}
231
Patrick Williamsf5402192024-08-16 15:20:53 -0400232int PowerInterface::callbackSetPowerSupplyError(
233 sd_bus_message* msg, void* context, sd_bus_error* error)
Jim Wrightccea2d22021-12-10 14:10:46 -0600234{
235 if (msg != nullptr && context != nullptr)
236 {
237 try
238 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500239 auto m = sdbusplus::message_t(msg);
Jim Wrightccea2d22021-12-10 14:10:46 -0600240
241 std::string psError{};
242 m.read(psError);
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000243 lg2::info("callbackSetPowerSupplyError: {PSERROR}", "PSERROR",
244 psError);
Jim Wrightccea2d22021-12-10 14:10:46 -0600245
246 auto pwrObj = static_cast<PowerInterface*>(context);
247 pwrObj->setPowerSupplyError(psError);
248
249 m.new_method_return().method_return();
250 }
251 catch (const sdbusplus::exception_t& e)
252 {
253 return sd_bus_error_set(error, e.name(), e.description());
254 }
255 }
256 else
257 {
258 // The message or context were null
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000259 lg2::error("Unable to service setPowerSupplyError method callback");
Jim Wrightccea2d22021-12-10 14:10:46 -0600260 return -1;
261 }
262
263 return 1;
264}
265
Jim Wright19920832021-08-25 11:13:56 -0500266void PowerInterface::emitPowerGoodSignal()
267{
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000268 lg2::info("emitPowerGoodSignal");
Jim Wright213ffe92022-06-03 08:54:30 -0500269 serverInterface.new_signal("PowerGood").signal_send();
Jim Wright19920832021-08-25 11:13:56 -0500270}
271
272void PowerInterface::emitPowerLostSignal()
273{
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000274 lg2::info("emitPowerLostSignal");
Jim Wright213ffe92022-06-03 08:54:30 -0500275 serverInterface.new_signal("PowerLost").signal_send();
Jim Wright19920832021-08-25 11:13:56 -0500276}
277
278void PowerInterface::emitPropertyChangedSignal(const char* property)
279{
Anwaar Hadi31ae6ee2025-05-15 17:12:26 +0000280 lg2::info("emitPropertyChangedSignal: {PROPERTY}", "PROPERTY", property);
Jim Wright213ffe92022-06-03 08:54:30 -0500281 serverInterface.property_changed(property);
Jim Wright19920832021-08-25 11:13:56 -0500282}
283
Jim Wright213ffe92022-06-03 08:54:30 -0500284const sdbusplus::vtable::vtable_t PowerInterface::vtable[] = {
Jim Wright19920832021-08-25 11:13:56 -0500285 sdbusplus::vtable::start(),
286 // Method setPowerState takes an int parameter and returns void
287 sdbusplus::vtable::method("setPowerState", "i", "", callbackSetPowerState),
288 // Method getPowerState takes no parameters and returns int
289 sdbusplus::vtable::method("getPowerState", "", "i", callbackGetPowerState),
290 // Signal PowerGood
291 sdbusplus::vtable::signal("PowerGood", ""),
292 // Signal PowerLost
293 sdbusplus::vtable::signal("PowerLost", ""),
294 // Property pgood is type int, read only, and uses the emits_change flag
295 sdbusplus::vtable::property("pgood", "i", callbackGetPgood,
296 sdbusplus::vtable::property_::emits_change),
297 // Property state is type int, read only, and uses the emits_change flag
298 sdbusplus::vtable::property("state", "i", callbackGetState,
299 sdbusplus::vtable::property_::emits_change),
300 // Property pgood_timeout is type int, read write, and uses the emits_change
301 // flag
302 sdbusplus::vtable::property("pgood_timeout", "i", callbackGetPgoodTimeout,
303 callbackSetPgoodTimeout,
304 sdbusplus::vtable::property_::emits_change),
Jim Wrightccea2d22021-12-10 14:10:46 -0600305 // Method setPowerSupplyError takes a string parameter and returns void
306 sdbusplus::vtable::method("setPowerSupplyError", "s", "",
307 callbackSetPowerSupplyError),
Jim Wright19920832021-08-25 11:13:56 -0500308 sdbusplus::vtable::end()};
309
310} // namespace phosphor::power::sequencer