blob: 0838d8f2499e6f6559ef36663d53ebe6374a1bf8 [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
Jim Wright19920832021-08-25 11:13:56 -050021#include <phosphor-logging/log.hpp>
22#include <sdbusplus/exception.hpp>
23#include <sdbusplus/sdbus.hpp>
24#include <sdbusplus/server.hpp>
25
Shawn McCarney768d2262024-07-09 15:02:59 -050026#include <format>
Jim Wright19920832021-08-25 11:13:56 -050027#include <string>
28#include <tuple>
29
30using namespace phosphor::logging;
31
32namespace phosphor::power::sequencer
33{
34
Patrick Williams7354ce62022-07-22 19:26:56 -050035PowerInterface::PowerInterface(sdbusplus::bus_t& bus, const char* path) :
Jim Wright213ffe92022-06-03 08:54:30 -050036 serverInterface(bus, path, POWER_IFACE, vtable, this)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000037{}
Jim Wright19920832021-08-25 11:13:56 -050038
39int PowerInterface::callbackGetPgood(sd_bus* /*bus*/, const char* /*path*/,
40 const char* /*interface*/,
41 const char* /*property*/,
42 sd_bus_message* msg, void* context,
43 sd_bus_error* error)
44{
45 if (msg != nullptr && context != nullptr)
46 {
47 try
48 {
49 auto pwrObj = static_cast<PowerInterface*>(context);
50 int pgood = pwrObj->getPgood();
51 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -050052 std::format("callbackGetPgood: {}", pgood).c_str());
Jim Wright19920832021-08-25 11:13:56 -050053
Patrick Williams7354ce62022-07-22 19:26:56 -050054 sdbusplus::message_t(msg).append(pgood);
Jim Wright19920832021-08-25 11:13:56 -050055 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050056 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -050057 {
58 return sd_bus_error_set(error, e.name(), e.description());
59 }
60 }
61 else
62 {
63 // The message or context were null
64 log<level::ERR>("Unable to service get pgood property callback");
65 return -1;
66 }
67
68 return 1;
69}
70
71int PowerInterface::callbackGetPgoodTimeout(sd_bus* /*bus*/,
72 const char* /*path*/,
73 const char* /*interface*/,
74 const char* /*property*/,
75 sd_bus_message* msg, void* context,
76 sd_bus_error* error)
77{
78 if (msg != nullptr && context != nullptr)
79 {
80 try
81 {
82 auto pwrObj = static_cast<PowerInterface*>(context);
83 int timeout = pwrObj->getPgoodTimeout();
84 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -050085 std::format("callbackGetPgoodTimeout: {}", timeout).c_str());
Jim Wright19920832021-08-25 11:13:56 -050086
Patrick Williams7354ce62022-07-22 19:26:56 -050087 sdbusplus::message_t(msg).append(timeout);
Jim Wright19920832021-08-25 11:13:56 -050088 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050089 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -050090 {
91 return sd_bus_error_set(error, e.name(), e.description());
92 }
93 }
94 else
95 {
96 // The message or context were null
97 log<level::ERR>(
98 "Unable to service get pgood timeout property callback");
99 return -1;
100 }
101
102 return 1;
103}
104
105int PowerInterface::callbackGetPowerState(sd_bus_message* msg, void* context,
106 sd_bus_error* error)
107{
108 if (msg != nullptr && context != nullptr)
109 {
110 try
111 {
112 auto pwrObj = static_cast<PowerInterface*>(context);
113 // Return the current power state of the GPIO, rather than the last
114 // requested power state change
115 int pgood = pwrObj->getPgood();
116 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500117 std::format("callbackGetPowerState: {}", pgood).c_str());
Jim Wright19920832021-08-25 11:13:56 -0500118
Patrick Williams7354ce62022-07-22 19:26:56 -0500119 auto reply = sdbusplus::message_t(msg).new_method_return();
Jim Wright19920832021-08-25 11:13:56 -0500120 reply.append(pgood);
121 reply.method_return();
122 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500123 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500124 {
125 return sd_bus_error_set(error, e.name(), e.description());
126 }
127 }
128 else
129 {
130 // The message or context were null
131 log<level::ERR>("Unable to service getPowerState method callback");
132 return -1;
133 }
134
135 return 1;
136}
137
138int PowerInterface::callbackSetPgoodTimeout(sd_bus* /*bus*/,
139 const char* /*path*/,
140 const char* /*interface*/,
141 const char* /*property*/,
142 sd_bus_message* msg, void* context,
143 sd_bus_error* error)
144{
145 if (msg != nullptr && context != nullptr)
146 {
147 try
148 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500149 auto m = sdbusplus::message_t(msg);
Jim Wright19920832021-08-25 11:13:56 -0500150
151 int timeout{};
152 m.read(timeout);
Jim Wright19920832021-08-25 11:13:56 -0500153 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500154 std::format("callbackSetPgoodTimeout: {}", timeout).c_str());
Jim Wrightccea2d22021-12-10 14:10:46 -0600155
156 auto pwrObj = static_cast<PowerInterface*>(context);
Jim Wright19920832021-08-25 11:13:56 -0500157 pwrObj->setPgoodTimeout(timeout);
158 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500159 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500160 {
161 return sd_bus_error_set(error, e.name(), e.description());
162 }
163 }
164 else
165 {
166 // The message or context were null
167 log<level::ERR>(
168 "Unable to service set pgood timeout property callback");
169 return -1;
170 }
171
172 return 1;
173}
174
175int PowerInterface::callbackGetState(sd_bus* /*bus*/, const char* /*path*/,
176 const char* /*interface*/,
177 const char* /*property*/,
178 sd_bus_message* msg, void* context,
179 sd_bus_error* error)
180{
181 if (msg != nullptr && context != nullptr)
182 {
183 try
184 {
185 auto pwrObj = static_cast<PowerInterface*>(context);
186 int state = pwrObj->getState();
187 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500188 std::format("callbackGetState: {}", state).c_str());
Jim Wright19920832021-08-25 11:13:56 -0500189
Patrick Williams7354ce62022-07-22 19:26:56 -0500190 sdbusplus::message_t(msg).append(state);
Jim Wright19920832021-08-25 11:13:56 -0500191 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500192 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500193 {
194 return sd_bus_error_set(error, e.name(), e.description());
195 }
196 }
197 else
198 {
199 // The message or context were null
200 log<level::ERR>("Unable to service get state property callback");
201 return -1;
202 }
203
204 return 1;
205}
206
207int PowerInterface::callbackSetPowerState(sd_bus_message* msg, void* context,
208 sd_bus_error* error)
209{
210 if (msg != nullptr && context != nullptr)
211 {
212 try
213 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500214 auto m = sdbusplus::message_t(msg);
Jim Wright19920832021-08-25 11:13:56 -0500215
216 int state{};
217 m.read(state);
218
219 if (state != 1 && state != 0)
220 {
221 return sd_bus_error_set(error,
222 "org.openbmc.ControlPower.Error.Failed",
223 "Invalid power state");
224 }
Jim Wright19920832021-08-25 11:13:56 -0500225 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500226 std::format("callbackSetPowerState: {}", state).c_str());
Jim Wrightccea2d22021-12-10 14:10:46 -0600227
228 auto pwrObj = static_cast<PowerInterface*>(context);
Jim Wright19920832021-08-25 11:13:56 -0500229 pwrObj->setState(state);
230
231 m.new_method_return().method_return();
232 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500233 catch (const sdbusplus::exception_t& e)
Jim Wright19920832021-08-25 11:13:56 -0500234 {
235 return sd_bus_error_set(error, e.name(), e.description());
236 }
237 }
238 else
239 {
240 // The message or context were null
241 log<level::ERR>("Unable to service setPowerState method callback");
242 return -1;
243 }
244
245 return 1;
246}
247
Jim Wrightccea2d22021-12-10 14:10:46 -0600248int PowerInterface::callbackSetPowerSupplyError(sd_bus_message* msg,
249 void* context,
250 sd_bus_error* error)
251{
252 if (msg != nullptr && context != nullptr)
253 {
254 try
255 {
Patrick Williams7354ce62022-07-22 19:26:56 -0500256 auto m = sdbusplus::message_t(msg);
Jim Wrightccea2d22021-12-10 14:10:46 -0600257
258 std::string psError{};
259 m.read(psError);
260 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500261 std::format("callbackSetPowerSupplyError: {}", psError)
Jim Wrightccea2d22021-12-10 14:10:46 -0600262 .c_str());
263
264 auto pwrObj = static_cast<PowerInterface*>(context);
265 pwrObj->setPowerSupplyError(psError);
266
267 m.new_method_return().method_return();
268 }
269 catch (const sdbusplus::exception_t& e)
270 {
271 return sd_bus_error_set(error, e.name(), e.description());
272 }
273 }
274 else
275 {
276 // The message or context were null
277 log<level::ERR>(
278 "Unable to service setPowerSupplyError method callback");
279 return -1;
280 }
281
282 return 1;
283}
284
Jim Wright19920832021-08-25 11:13:56 -0500285void PowerInterface::emitPowerGoodSignal()
286{
287 log<level::INFO>("emitPowerGoodSignal");
Jim Wright213ffe92022-06-03 08:54:30 -0500288 serverInterface.new_signal("PowerGood").signal_send();
Jim Wright19920832021-08-25 11:13:56 -0500289}
290
291void PowerInterface::emitPowerLostSignal()
292{
293 log<level::INFO>("emitPowerLostSignal");
Jim Wright213ffe92022-06-03 08:54:30 -0500294 serverInterface.new_signal("PowerLost").signal_send();
Jim Wright19920832021-08-25 11:13:56 -0500295}
296
297void PowerInterface::emitPropertyChangedSignal(const char* property)
298{
299 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500300 std::format("emitPropertyChangedSignal: {}", property).c_str());
Jim Wright213ffe92022-06-03 08:54:30 -0500301 serverInterface.property_changed(property);
Jim Wright19920832021-08-25 11:13:56 -0500302}
303
Jim Wright213ffe92022-06-03 08:54:30 -0500304const sdbusplus::vtable::vtable_t PowerInterface::vtable[] = {
Jim Wright19920832021-08-25 11:13:56 -0500305 sdbusplus::vtable::start(),
306 // Method setPowerState takes an int parameter and returns void
307 sdbusplus::vtable::method("setPowerState", "i", "", callbackSetPowerState),
308 // Method getPowerState takes no parameters and returns int
309 sdbusplus::vtable::method("getPowerState", "", "i", callbackGetPowerState),
310 // Signal PowerGood
311 sdbusplus::vtable::signal("PowerGood", ""),
312 // Signal PowerLost
313 sdbusplus::vtable::signal("PowerLost", ""),
314 // Property pgood is type int, read only, and uses the emits_change flag
315 sdbusplus::vtable::property("pgood", "i", callbackGetPgood,
316 sdbusplus::vtable::property_::emits_change),
317 // Property state is type int, read only, and uses the emits_change flag
318 sdbusplus::vtable::property("state", "i", callbackGetState,
319 sdbusplus::vtable::property_::emits_change),
320 // Property pgood_timeout is type int, read write, and uses the emits_change
321 // flag
322 sdbusplus::vtable::property("pgood_timeout", "i", callbackGetPgoodTimeout,
323 callbackSetPgoodTimeout,
324 sdbusplus::vtable::property_::emits_change),
Jim Wrightccea2d22021-12-10 14:10:46 -0600325 // Method setPowerSupplyError takes a string parameter and returns void
326 sdbusplus::vtable::method("setPowerSupplyError", "s", "",
327 callbackSetPowerSupplyError),
Jim Wright19920832021-08-25 11:13:56 -0500328 sdbusplus::vtable::end()};
329
330} // namespace phosphor::power::sequencer