-
Notifications
You must be signed in to change notification settings - Fork 10
/
power_interface.cpp
323 lines (284 loc) · 9.66 KB
/
power_interface.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Copyright © 2021 IBM Corporation
*
* 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 "power_interface.hpp"
#include "types.hpp"
#include <phosphor-logging/log.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/sdbus.hpp>
#include <sdbusplus/server.hpp>
#include <format>
#include <string>
#include <tuple>
using namespace phosphor::logging;
namespace phosphor::power::sequencer
{
PowerInterface::PowerInterface(sdbusplus::bus_t& bus, const char* path) :
serverInterface(bus, path, POWER_IFACE, vtable, this)
{}
int PowerInterface::callbackGetPgood(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto pwrObj = static_cast<PowerInterface*>(context);
int pgood = pwrObj->getPgood();
log<level::INFO>(
std::format("callbackGetPgood: {}", pgood).c_str());
sdbusplus::message_t(msg).append(pgood);
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>("Unable to service get pgood property callback");
return -1;
}
return 1;
}
int PowerInterface::callbackGetPgoodTimeout(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto pwrObj = static_cast<PowerInterface*>(context);
int timeout = pwrObj->getPgoodTimeout();
log<level::INFO>(
std::format("callbackGetPgoodTimeout: {}", timeout).c_str());
sdbusplus::message_t(msg).append(timeout);
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>(
"Unable to service get pgood timeout property callback");
return -1;
}
return 1;
}
int PowerInterface::callbackGetPowerState(sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto pwrObj = static_cast<PowerInterface*>(context);
// Return the current power state of the GPIO, rather than the last
// requested power state change
int pgood = pwrObj->getPgood();
log<level::INFO>(
std::format("callbackGetPowerState: {}", pgood).c_str());
auto reply = sdbusplus::message_t(msg).new_method_return();
reply.append(pgood);
reply.method_return();
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>("Unable to service getPowerState method callback");
return -1;
}
return 1;
}
int PowerInterface::callbackSetPgoodTimeout(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto m = sdbusplus::message_t(msg);
int timeout{};
m.read(timeout);
log<level::INFO>(
std::format("callbackSetPgoodTimeout: {}", timeout).c_str());
auto pwrObj = static_cast<PowerInterface*>(context);
pwrObj->setPgoodTimeout(timeout);
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>(
"Unable to service set pgood timeout property callback");
return -1;
}
return 1;
}
int PowerInterface::callbackGetState(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto pwrObj = static_cast<PowerInterface*>(context);
int state = pwrObj->getState();
log<level::INFO>(
std::format("callbackGetState: {}", state).c_str());
sdbusplus::message_t(msg).append(state);
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>("Unable to service get state property callback");
return -1;
}
return 1;
}
int PowerInterface::callbackSetPowerState(sd_bus_message* msg, void* context,
sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto m = sdbusplus::message_t(msg);
int state{};
m.read(state);
if (state != 1 && state != 0)
{
return sd_bus_error_set(error,
"org.openbmc.ControlPower.Error.Failed",
"Invalid power state");
}
log<level::INFO>(
std::format("callbackSetPowerState: {}", state).c_str());
auto pwrObj = static_cast<PowerInterface*>(context);
pwrObj->setState(state);
m.new_method_return().method_return();
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>("Unable to service setPowerState method callback");
return -1;
}
return 1;
}
int PowerInterface::callbackSetPowerSupplyError(
sd_bus_message* msg, void* context, sd_bus_error* error)
{
if (msg != nullptr && context != nullptr)
{
try
{
auto m = sdbusplus::message_t(msg);
std::string psError{};
m.read(psError);
log<level::INFO>(
std::format("callbackSetPowerSupplyError: {}", psError)
.c_str());
auto pwrObj = static_cast<PowerInterface*>(context);
pwrObj->setPowerSupplyError(psError);
m.new_method_return().method_return();
}
catch (const sdbusplus::exception_t& e)
{
return sd_bus_error_set(error, e.name(), e.description());
}
}
else
{
// The message or context were null
log<level::ERR>(
"Unable to service setPowerSupplyError method callback");
return -1;
}
return 1;
}
void PowerInterface::emitPowerGoodSignal()
{
log<level::INFO>("emitPowerGoodSignal");
serverInterface.new_signal("PowerGood").signal_send();
}
void PowerInterface::emitPowerLostSignal()
{
log<level::INFO>("emitPowerLostSignal");
serverInterface.new_signal("PowerLost").signal_send();
}
void PowerInterface::emitPropertyChangedSignal(const char* property)
{
log<level::INFO>(
std::format("emitPropertyChangedSignal: {}", property).c_str());
serverInterface.property_changed(property);
}
const sdbusplus::vtable::vtable_t PowerInterface::vtable[] = {
sdbusplus::vtable::start(),
// Method setPowerState takes an int parameter and returns void
sdbusplus::vtable::method("setPowerState", "i", "", callbackSetPowerState),
// Method getPowerState takes no parameters and returns int
sdbusplus::vtable::method("getPowerState", "", "i", callbackGetPowerState),
// Signal PowerGood
sdbusplus::vtable::signal("PowerGood", ""),
// Signal PowerLost
sdbusplus::vtable::signal("PowerLost", ""),
// Property pgood is type int, read only, and uses the emits_change flag
sdbusplus::vtable::property("pgood", "i", callbackGetPgood,
sdbusplus::vtable::property_::emits_change),
// Property state is type int, read only, and uses the emits_change flag
sdbusplus::vtable::property("state", "i", callbackGetState,
sdbusplus::vtable::property_::emits_change),
// Property pgood_timeout is type int, read write, and uses the emits_change
// flag
sdbusplus::vtable::property("pgood_timeout", "i", callbackGetPgoodTimeout,
callbackSetPgoodTimeout,
sdbusplus::vtable::property_::emits_change),
// Method setPowerSupplyError takes a string parameter and returns void
sdbusplus::vtable::method("setPowerSupplyError", "s", "",
callbackSetPowerSupplyError),
sdbusplus::vtable::end()};
} // namespace phosphor::power::sequencer