blob: 30d07667c0f67752dda0b14f8c0ff29605cd4d96 [file] [log] [blame]
Brad Bishop6ae03302016-12-19 13:37:43 -05001#include <algorithm>
2#include <sdbusplus/server.hpp>
3#include <sdbusplus/exception.hpp>
4#include <xyz/openbmc_project/Sensor/Value/server.hpp>
5
6namespace sdbusplus
7{
8namespace xyz
9{
10namespace openbmc_project
11{
12namespace Sensor
13{
14namespace server
15{
16
17Value::Value(bus::bus& bus, const char* path)
18 : _xyz_openbmc_project_Sensor_Value_interface(
19 bus, path, _interface, _vtable, this)
20{
21}
22
23
24
25auto Value::value() const ->
26 int64_t
27{
28 return _value;
29}
30
31int Value::_callback_get_Value(
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<Value*>(context);
43 m.append(convertForMessage(o->value()));
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 Value::value(int64_t value) ->
55 int64_t
56{
57 if (_value != value)
58 {
59 _value = value;
60 _xyz_openbmc_project_Sensor_Value_interface.property_changed("Value");
61 }
62
63 return _value;
64}
65
66int Value::_callback_set_Value(
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<Value*>(context);
76
77 int64_t v{};
78 m.read(v);
79 o->value(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 Value
93{
94static const auto _property_Value =
95 utility::tuple_to_array(message::types::type_id<
96 int64_t>());
97}
98}
99auto Value::unit() const ->
100 Unit
101{
102 return _unit;
103}
104
105int Value::_callback_get_Unit(
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<Value*>(context);
117 m.append(convertForMessage(o->unit()));
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 Value::unit(Unit value) ->
129 Unit
130{
131 if (_unit != value)
132 {
133 _unit = value;
134 _xyz_openbmc_project_Sensor_Value_interface.property_changed("Unit");
135 }
136
137 return _unit;
138}
139
140int Value::_callback_set_Unit(
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<Value*>(context);
150
151 std::string v{};
152 m.read(v);
153 o->unit(convertUnitFromString(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 Value
167{
168static const auto _property_Unit =
169 utility::tuple_to_array(message::types::type_id<
170 std::string>());
171}
172}
173auto Value::scale() const ->
174 int64_t
175{
176 return _scale;
177}
178
179int Value::_callback_get_Scale(
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<Value*>(context);
191 m.append(convertForMessage(o->scale()));
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 Value::scale(int64_t value) ->
203 int64_t
204{
205 if (_scale != value)
206 {
207 _scale = value;
208 _xyz_openbmc_project_Sensor_Value_interface.property_changed("Scale");
209 }
210
211 return _scale;
212}
213
214int Value::_callback_set_Scale(
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<Value*>(context);
224
225 int64_t v{};
226 m.read(v);
227 o->scale(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 Value
241{
242static const auto _property_Scale =
243 utility::tuple_to_array(message::types::type_id<
244 int64_t>());
245}
246}
247
248
249namespace
250{
251/** String to enum mapping for Value::Unit */
252static const std::tuple<const char*, Value::Unit> mappingValueUnit[] =
253 {
254 std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.DegreesC", Value::Unit::DegreesC ),
255 std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.RPMS", Value::Unit::RPMS ),
256 std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.Volts", Value::Unit::Volts ),
257 std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.Meters", Value::Unit::Meters ),
258 };
259
260} // anonymous namespace
261
262auto Value::convertUnitFromString(std::string& s) ->
263 Unit
264{
265 auto i = std::find_if(
266 std::begin(mappingValueUnit),
267 std::end(mappingValueUnit),
268 [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
269 if (std::end(mappingValueUnit) == i)
270 {
271 throw sdbusplus::exception::InvalidEnumString();
272 }
273 else
274 {
275 return std::get<1>(*i);
276 }
277}
278
279std::string convertForMessage(Value::Unit v)
280{
281 auto i = std::find_if(
282 std::begin(mappingValueUnit),
283 std::end(mappingValueUnit),
284 [v](auto& e){ return v == std::get<1>(e); });
285 return std::get<0>(*i);
286}
287
288const vtable::vtable_t Value::_vtable[] = {
289 vtable::start(),
290 vtable::property("Value",
291 details::Value::_property_Value
292 .data(),
293 _callback_get_Value,
294 _callback_set_Value,
295 vtable::property_::emits_change),
296 vtable::property("Unit",
297 details::Value::_property_Unit
298 .data(),
299 _callback_get_Unit,
300 _callback_set_Unit,
301 vtable::property_::emits_change),
302 vtable::property("Scale",
303 details::Value::_property_Scale
304 .data(),
305 _callback_get_Scale,
306 _callback_set_Scale,
307 vtable::property_::emits_change),
308 vtable::end()
309};
310
311} // namespace server
312} // namespace Sensor
313} // namespace openbmc_project
314} // namespace xyz
315} // namespace sdbusplus
316