blob: 6495d52acddfe1a23981fa5db06ddb6f7d6bd742 [file] [log] [blame]
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06001#include <algorithm>
2#include <sdbusplus/server.hpp>
3#include <sdbusplus/exception.hpp>
4#include <xyz/openbmc_project/State/BMC/server.hpp>
5
6namespace sdbusplus
7{
8namespace xyz
9{
10namespace openbmc_project
11{
12namespace State
13{
14namespace server
15{
16
17BMC::BMC(bus::bus& bus, const char* path)
18 : _xyz_openbmc_project_State_BMC_interface(
19 bus, path, _interface, _vtable, this)
20{
21}
22
23
24
25auto BMC::requestedBMCTransition() const ->
26 Transition
27{
28 return _requestedBMCTransition;
29}
30
31int BMC::_callback_get_RequestedBMCTransition(
32 sd_bus* bus, const char* path, const char* interface,
33 const char* property, sd_bus_message* reply, void* context,
34 sd_bus_error* error)
35{
36 using sdbusplus::server::binding::details::convertForMessage;
37
38 try
39 {
40 auto m = message::message(sd_bus_message_ref(reply));
41
42 auto o = static_cast<BMC*>(context);
43 m.append(convertForMessage(o->requestedBMCTransition()));
44 }
45 catch(sdbusplus::internal_exception_t& e)
46 {
47 sd_bus_error_set_const(error, e.name(), e.description());
48 return -EINVAL;
49 }
50
51 return true;
52}
53
54auto BMC::requestedBMCTransition(Transition value) ->
55 Transition
56{
57 if (_requestedBMCTransition != value)
58 {
59 _requestedBMCTransition = value;
60 _xyz_openbmc_project_State_BMC_interface.property_changed("RequestedBMCTransition");
61 }
62
63 return _requestedBMCTransition;
64}
65
66int BMC::_callback_set_RequestedBMCTransition(
67 sd_bus* bus, const char* path, const char* interface,
68 const char* property, sd_bus_message* value, void* context,
69 sd_bus_error* error)
70{
71 try
72 {
73 auto m = message::message(sd_bus_message_ref(value));
74
75 auto o = static_cast<BMC*>(context);
76
77 std::string v{};
78 m.read(v);
79 o->requestedBMCTransition(convertTransitionFromString(v));
80 }
81 catch(sdbusplus::internal_exception_t& e)
82 {
83 sd_bus_error_set_const(error, e.name(), e.description());
84 return -EINVAL;
85 }
86
87 return true;
88}
89
90namespace details
91{
92namespace BMC
93{
94static const auto _property_RequestedBMCTransition =
95 utility::tuple_to_array(message::types::type_id<
96 std::string>());
97}
98}
99auto BMC::currentBMCState() const ->
100 BMCState
101{
102 return _currentBMCState;
103}
104
105int BMC::_callback_get_CurrentBMCState(
106 sd_bus* bus, const char* path, const char* interface,
107 const char* property, sd_bus_message* reply, void* context,
108 sd_bus_error* error)
109{
110 using sdbusplus::server::binding::details::convertForMessage;
111
112 try
113 {
114 auto m = message::message(sd_bus_message_ref(reply));
115
116 auto o = static_cast<BMC*>(context);
117 m.append(convertForMessage(o->currentBMCState()));
118 }
119 catch(sdbusplus::internal_exception_t& e)
120 {
121 sd_bus_error_set_const(error, e.name(), e.description());
122 return -EINVAL;
123 }
124
125 return true;
126}
127
128auto BMC::currentBMCState(BMCState value) ->
129 BMCState
130{
131 if (_currentBMCState != value)
132 {
133 _currentBMCState = value;
134 _xyz_openbmc_project_State_BMC_interface.property_changed("CurrentBMCState");
135 }
136
137 return _currentBMCState;
138}
139
140int BMC::_callback_set_CurrentBMCState(
141 sd_bus* bus, const char* path, const char* interface,
142 const char* property, sd_bus_message* value, void* context,
143 sd_bus_error* error)
144{
145 try
146 {
147 auto m = message::message(sd_bus_message_ref(value));
148
149 auto o = static_cast<BMC*>(context);
150
151 std::string v{};
152 m.read(v);
153 o->currentBMCState(convertBMCStateFromString(v));
154 }
155 catch(sdbusplus::internal_exception_t& e)
156 {
157 sd_bus_error_set_const(error, e.name(), e.description());
158 return -EINVAL;
159 }
160
161 return true;
162}
163
164namespace details
165{
166namespace BMC
167{
168static const auto _property_CurrentBMCState =
169 utility::tuple_to_array(message::types::type_id<
170 std::string>());
171}
172}
173
174
175namespace
176{
177/** String to enum mapping for BMC::Transition */
178static const std::tuple<const char*, BMC::Transition> mappingBMCTransition[] =
179 {
180 std::make_tuple( "xyz.openbmc_project.State.BMC.Transition.Reboot", BMC::Transition::Reboot ),
181 std::make_tuple( "xyz.openbmc_project.State.BMC.Transition.None", BMC::Transition::None ),
182 };
183
184} // anonymous namespace
185
186auto BMC::convertTransitionFromString(std::string& s) ->
187 Transition
188{
189 auto i = std::find_if(
190 std::begin(mappingBMCTransition),
191 std::end(mappingBMCTransition),
192 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
193 if (std::end(mappingBMCTransition) == i)
194 {
195 throw sdbusplus::exception::InvalidEnumString();
196 }
197 else
198 {
199 return std::get<1>(*i);
200 }
201}
202
203std::string convertForMessage(BMC::Transition v)
204{
205 auto i = std::find_if(
206 std::begin(mappingBMCTransition),
207 std::end(mappingBMCTransition),
208 [v](auto& e){ return v == std::get<1>(e); });
209 return std::get<0>(*i);
210}
211
212namespace
213{
214/** String to enum mapping for BMC::BMCState */
215static const std::tuple<const char*, BMC::BMCState> mappingBMCBMCState[] =
216 {
217 std::make_tuple( "xyz.openbmc_project.State.BMC.BMCState.Ready", BMC::BMCState::Ready ),
218 std::make_tuple( "xyz.openbmc_project.State.BMC.BMCState.NotReady", BMC::BMCState::NotReady ),
219 };
220
221} // anonymous namespace
222
223auto BMC::convertBMCStateFromString(std::string& s) ->
224 BMCState
225{
226 auto i = std::find_if(
227 std::begin(mappingBMCBMCState),
228 std::end(mappingBMCBMCState),
229 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
230 if (std::end(mappingBMCBMCState) == i)
231 {
232 throw sdbusplus::exception::InvalidEnumString();
233 }
234 else
235 {
236 return std::get<1>(*i);
237 }
238}
239
240std::string convertForMessage(BMC::BMCState v)
241{
242 auto i = std::find_if(
243 std::begin(mappingBMCBMCState),
244 std::end(mappingBMCBMCState),
245 [v](auto& e){ return v == std::get<1>(e); });
246 return std::get<0>(*i);
247}
248
249const vtable::vtable_t BMC::_vtable[] = {
250 vtable::start(),
251 vtable::property("RequestedBMCTransition",
252 details::BMC::_property_RequestedBMCTransition
253 .data(),
254 _callback_get_RequestedBMCTransition,
255 _callback_set_RequestedBMCTransition,
256 vtable::property_::emits_change),
257 vtable::property("CurrentBMCState",
258 details::BMC::_property_CurrentBMCState
259 .data(),
260 _callback_get_CurrentBMCState,
261 _callback_set_CurrentBMCState,
262 vtable::property_::emits_change),
263 vtable::end()
264};
265
266} // namespace server
267} // namespace State
268} // namespace openbmc_project
269} // namespace xyz
270} // namespace sdbusplus
271