blob: 6b96e81fb5d90fa4c08446d309f38535c8d2a845 [file] [log] [blame]
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +05301#include <algorithm>
2#include <sdbusplus/server.hpp>
3#include <sdbusplus/exception.hpp>
4#include "xyz/openbmc_project/Led/Physical/server.hpp"
5
6namespace sdbusplus
7{
8namespace xyz
9{
10namespace openbmc_project
11{
12namespace Led
13{
14namespace server
15{
16
17Physical::Physical(bus::bus& bus, const char* path)
18 : _xyz_openbmc_project_Led_Physical_interface(
19 bus, path, _interface, _vtable, this)
20{
21}
22
23
24
25auto Physical::state() const ->
26 Action
27{
28 return _state;
29}
30
31int Physical::_callback_get_State(
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<Physical*>(context);
43 m.append(convertForMessage(o->state()));
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 Physical::state(Action value) ->
55 Action
56{
57 if (_state != value)
58 {
59 _state = value;
60 _xyz_openbmc_project_Led_Physical_interface.property_changed("State");
61 }
62
63 return _state;
64}
65
66int Physical::_callback_set_State(
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<Physical*>(context);
76
77 std::string v{};
78 m.read(v);
79 o->state(convertActionFromString(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 Physical
93{
94static const auto _property_State =
95 utility::tuple_to_array(message::types::type_id<
96 std::string>());
97}
98}
99auto Physical::dutyOn() const ->
100 uint8_t
101{
102 return _dutyOn;
103}
104
105int Physical::_callback_get_DutyOn(
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<Physical*>(context);
117 m.append(convertForMessage(o->dutyOn()));
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 Physical::dutyOn(uint8_t value) ->
129 uint8_t
130{
131 if (_dutyOn != value)
132 {
133 _dutyOn = value;
134 _xyz_openbmc_project_Led_Physical_interface.property_changed("DutyOn");
135 }
136
137 return _dutyOn;
138}
139
140int Physical::_callback_set_DutyOn(
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<Physical*>(context);
150
151 uint8_t v{};
152 m.read(v);
153 o->dutyOn(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 Physical
167{
168static const auto _property_DutyOn =
169 utility::tuple_to_array(message::types::type_id<
170 uint8_t>());
171}
172}
173auto Physical::color() const ->
174 Palette
175{
176 return _color;
177}
178
179int Physical::_callback_get_Color(
180 sd_bus* bus, const char* path, const char* interface,
181 const char* property, sd_bus_message* reply, void* context,
182 sd_bus_error* error)
183{
184 using sdbusplus::server::binding::details::convertForMessage;
185
186 try
187 {
188 auto m = message::message(sd_bus_message_ref(reply));
189
190 auto o = static_cast<Physical*>(context);
191 m.append(convertForMessage(o->color()));
192 }
193 catch(sdbusplus::internal_exception_t& e)
194 {
195 sd_bus_error_set_const(error, e.name(), e.description());
196 return -EINVAL;
197 }
198
199 return true;
200}
201
202auto Physical::color(Palette value) ->
203 Palette
204{
205 if (_color != value)
206 {
207 _color = value;
208 _xyz_openbmc_project_Led_Physical_interface.property_changed("Color");
209 }
210
211 return _color;
212}
213
214int Physical::_callback_set_Color(
215 sd_bus* bus, const char* path, const char* interface,
216 const char* property, sd_bus_message* value, void* context,
217 sd_bus_error* error)
218{
219 try
220 {
221 auto m = message::message(sd_bus_message_ref(value));
222
223 auto o = static_cast<Physical*>(context);
224
225 std::string v{};
226 m.read(v);
227 o->color(convertPaletteFromString(v));
228 }
229 catch(sdbusplus::internal_exception_t& e)
230 {
231 sd_bus_error_set_const(error, e.name(), e.description());
232 return -EINVAL;
233 }
234
235 return true;
236}
237
238namespace details
239{
240namespace Physical
241{
242static const auto _property_Color =
243 utility::tuple_to_array(message::types::type_id<
244 std::string>());
245}
246}
247
248
249namespace
250{
251/** String to enum mapping for Physical::Action */
252static const std::tuple<const char*, Physical::Action> mappingPhysicalAction[] =
253 {
254 std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.Off", Physical::Action::Off ),
255 std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.On", Physical::Action::On ),
256 std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.Blink", Physical::Action::Blink ),
257 };
258
259} // anonymous namespace
260
261auto Physical::convertActionFromString(std::string& s) ->
262 Action
263{
264 auto i = std::find_if(
265 std::begin(mappingPhysicalAction),
266 std::end(mappingPhysicalAction),
267 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
268 if (std::end(mappingPhysicalAction) == i)
269 {
270 throw sdbusplus::exception::InvalidEnumString();
271 }
272 else
273 {
274 return std::get<1>(*i);
275 }
276}
277
278std::string convertForMessage(Physical::Action v)
279{
280 auto i = std::find_if(
281 std::begin(mappingPhysicalAction),
282 std::end(mappingPhysicalAction),
283 [v](auto& e){ return v == std::get<1>(e); });
284 return std::get<0>(*i);
285}
286
287namespace
288{
289/** String to enum mapping for Physical::Palette */
290static const std::tuple<const char*, Physical::Palette> mappingPhysicalPalette[] =
291 {
292 std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Unknown", Physical::Palette::Unknown ),
293 std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Red", Physical::Palette::Red ),
294 std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Green", Physical::Palette::Green ),
295 std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Blue", Physical::Palette::Blue ),
296 std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Yellow", Physical::Palette::Yellow ),
297 };
298
299} // anonymous namespace
300
301auto Physical::convertPaletteFromString(std::string& s) ->
302 Palette
303{
304 auto i = std::find_if(
305 std::begin(mappingPhysicalPalette),
306 std::end(mappingPhysicalPalette),
307 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
308 if (std::end(mappingPhysicalPalette) == i)
309 {
310 throw sdbusplus::exception::InvalidEnumString();
311 }
312 else
313 {
314 return std::get<1>(*i);
315 }
316}
317
318std::string convertForMessage(Physical::Palette v)
319{
320 auto i = std::find_if(
321 std::begin(mappingPhysicalPalette),
322 std::end(mappingPhysicalPalette),
323 [v](auto& e){ return v == std::get<1>(e); });
324 return std::get<0>(*i);
325}
326
327const vtable::vtable_t Physical::_vtable[] = {
328 vtable::start(),
329 vtable::property("State",
330 details::Physical::_property_State
331 .data(),
332 _callback_get_State,
333 _callback_set_State,
334 vtable::property_::emits_change),
335 vtable::property("DutyOn",
336 details::Physical::_property_DutyOn
337 .data(),
338 _callback_get_DutyOn,
339 _callback_set_DutyOn,
340 vtable::property_::emits_change),
341 vtable::property("Color",
342 details::Physical::_property_Color
343 .data(),
344 _callback_get_Color,
345 _callback_set_Color,
346 vtable::property_::emits_change),
347 vtable::end()
348};
349
350} // namespace server
351} // namespace Led
352} // namespace openbmc_project
353} // namespace xyz
354} // namespace sdbusplus
355