Fawkes API  Fawkes Development Version
BatteryInterface.cpp
1 
2 /***************************************************************************
3  * BatteryInterface.cpp - Fawkes BlackBoard Interface - BatteryInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/BatteryInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class BatteryInterface <interfaces/BatteryInterface.h>
36  * BatteryInterface Fawkes BlackBoard Interface.
37  * This interface contains status information about the
38  battery. In addition to this it allows to send messages which
39  turn the battery on/off
40  * @ingroup FawkesInterfaces
41  */
42 
43 
44 
45 /** Constructor */
46 BatteryInterface::BatteryInterface() : Interface()
47 {
48  data_size = sizeof(BatteryInterface_data_t);
49  data_ptr = malloc(data_size);
50  data = (BatteryInterface_data_t *)data_ptr;
51  data_ts = (interface_data_ts_t *)data_ptr;
52  memset(data_ptr, 0, data_size);
53  add_fieldinfo(IFT_UINT32, "current", 1, &data->current);
54  add_fieldinfo(IFT_UINT32, "voltage", 1, &data->voltage);
55  add_fieldinfo(IFT_UINT32, "temperature", 1, &data->temperature);
56  add_fieldinfo(IFT_FLOAT, "absolute_soc", 1, &data->absolute_soc);
57  add_fieldinfo(IFT_FLOAT, "relative_soc", 1, &data->relative_soc);
58  add_messageinfo("PushButtonMessage");
59  add_messageinfo("SleepMessage");
60  unsigned char tmp_hash[] = {0x28, 0xb6, 0xbe, 0xe7, 0xf1, 0x47, 0x2, 0x12, 0x1d, 0xe3, 0x7c, 0x14, 0xe9, 0x1f, 0x24, 0x4d};
61  set_hash(tmp_hash);
62 }
63 
64 /** Destructor */
65 BatteryInterface::~BatteryInterface()
66 {
67  free(data_ptr);
68 }
69 /* Methods */
70 /** Get current value.
71  * Battery Current [mA]
72  * @return current value
73  */
74 uint32_t
75 BatteryInterface::current() const
76 {
77  return data->current;
78 }
79 
80 /** Get maximum length of current value.
81  * @return length of current value, can be length of the array or number of
82  * maximum number of characters for a string
83  */
84 size_t
85 BatteryInterface::maxlenof_current() const
86 {
87  return 1;
88 }
89 
90 /** Set current value.
91  * Battery Current [mA]
92  * @param new_current new current value
93  */
94 void
95 BatteryInterface::set_current(const uint32_t new_current)
96 {
97  set_field(data->current, new_current);
98 }
99 
100 /** Get voltage value.
101  * Battery Voltage [mV]
102  * @return voltage value
103  */
104 uint32_t
105 BatteryInterface::voltage() const
106 {
107  return data->voltage;
108 }
109 
110 /** Get maximum length of voltage value.
111  * @return length of voltage value, can be length of the array or number of
112  * maximum number of characters for a string
113  */
114 size_t
115 BatteryInterface::maxlenof_voltage() const
116 {
117  return 1;
118 }
119 
120 /** Set voltage value.
121  * Battery Voltage [mV]
122  * @param new_voltage new voltage value
123  */
124 void
125 BatteryInterface::set_voltage(const uint32_t new_voltage)
126 {
127  set_field(data->voltage, new_voltage);
128 }
129 
130 /** Get temperature value.
131  * Battery Temperature [°C]
132  * @return temperature value
133  */
134 uint32_t
135 BatteryInterface::temperature() const
136 {
137  return data->temperature;
138 }
139 
140 /** Get maximum length of temperature value.
141  * @return length of temperature value, can be length of the array or number of
142  * maximum number of characters for a string
143  */
144 size_t
145 BatteryInterface::maxlenof_temperature() const
146 {
147  return 1;
148 }
149 
150 /** Set temperature value.
151  * Battery Temperature [°C]
152  * @param new_temperature new temperature value
153  */
154 void
155 BatteryInterface::set_temperature(const uint32_t new_temperature)
156 {
157  set_field(data->temperature, new_temperature);
158 }
159 
160 /** Get absolute_soc value.
161  * Absolute state of charge [%]
162  * @return absolute_soc value
163  */
164 float
165 BatteryInterface::absolute_soc() const
166 {
167  return data->absolute_soc;
168 }
169 
170 /** Get maximum length of absolute_soc value.
171  * @return length of absolute_soc value, can be length of the array or number of
172  * maximum number of characters for a string
173  */
174 size_t
175 BatteryInterface::maxlenof_absolute_soc() const
176 {
177  return 1;
178 }
179 
180 /** Set absolute_soc value.
181  * Absolute state of charge [%]
182  * @param new_absolute_soc new absolute_soc value
183  */
184 void
185 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
186 {
187  set_field(data->absolute_soc, new_absolute_soc);
188 }
189 
190 /** Get relative_soc value.
191  * Relative state of charge [%]
192  * @return relative_soc value
193  */
194 float
195 BatteryInterface::relative_soc() const
196 {
197  return data->relative_soc;
198 }
199 
200 /** Get maximum length of relative_soc value.
201  * @return length of relative_soc value, can be length of the array or number of
202  * maximum number of characters for a string
203  */
204 size_t
205 BatteryInterface::maxlenof_relative_soc() const
206 {
207  return 1;
208 }
209 
210 /** Set relative_soc value.
211  * Relative state of charge [%]
212  * @param new_relative_soc new relative_soc value
213  */
214 void
215 BatteryInterface::set_relative_soc(const float new_relative_soc)
216 {
217  set_field(data->relative_soc, new_relative_soc);
218 }
219 
220 /* =========== message create =========== */
221 Message *
222 BatteryInterface::create_message(const char *type) const
223 {
224  if ( strncmp("PushButtonMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
225  return new PushButtonMessage();
226  } else if ( strncmp("SleepMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
227  return new SleepMessage();
228  } else {
229  throw UnknownTypeException("The given type '%s' does not match any known "
230  "message type for this interface type.", type);
231  }
232 }
233 
234 
235 /** Copy values from other interface.
236  * @param other other interface to copy values from
237  */
238 void
239 BatteryInterface::copy_values(const Interface *other)
240 {
241  const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
242  if (oi == NULL) {
243  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
244  type(), other->type());
245  }
246  memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
247 }
248 
249 const char *
250 BatteryInterface::enum_tostring(const char *enumtype, int val) const
251 {
252  throw UnknownTypeException("Unknown enum type %s", enumtype);
253 }
254 
255 /* =========== messages =========== */
256 /** @class BatteryInterface::PushButtonMessage <interfaces/BatteryInterface.h>
257  * PushButtonMessage Fawkes BlackBoard Interface Message.
258  *
259 
260  */
261 
262 
263 /** Constructor */
264 BatteryInterface::PushButtonMessage::PushButtonMessage() : Message("PushButtonMessage")
265 {
266  data_size = sizeof(PushButtonMessage_data_t);
267  data_ptr = malloc(data_size);
268  memset(data_ptr, 0, data_size);
269  data = (PushButtonMessage_data_t *)data_ptr;
271 }
272 
273 /** Destructor */
275 {
276  free(data_ptr);
277 }
278 
279 /** Copy constructor.
280  * @param m message to copy from
281  */
283 {
284  data_size = m->data_size;
285  data_ptr = malloc(data_size);
286  memcpy(data_ptr, m->data_ptr, data_size);
287  data = (PushButtonMessage_data_t *)data_ptr;
289 }
290 
291 /* Methods */
292 /** Clone this message.
293  * Produces a message of the same type as this message and copies the
294  * data to the new message.
295  * @return clone of this message
296  */
297 Message *
299 {
300  return new BatteryInterface::PushButtonMessage(this);
301 }
302 /** @class BatteryInterface::SleepMessage <interfaces/BatteryInterface.h>
303  * SleepMessage Fawkes BlackBoard Interface Message.
304  *
305 
306  */
307 
308 
309 /** Constructor */
311 {
312  data_size = sizeof(SleepMessage_data_t);
313  data_ptr = malloc(data_size);
314  memset(data_ptr, 0, data_size);
315  data = (SleepMessage_data_t *)data_ptr;
317 }
318 
319 /** Destructor */
321 {
322  free(data_ptr);
323 }
324 
325 /** Copy constructor.
326  * @param m message to copy from
327  */
329 {
330  data_size = m->data_size;
331  data_ptr = malloc(data_size);
332  memcpy(data_ptr, m->data_ptr, data_size);
333  data = (SleepMessage_data_t *)data_ptr;
335 }
336 
337 /* Methods */
338 /** Clone this message.
339  * Produces a message of the same type as this message and copies the
340  * data to the new message.
341  * @return clone of this message
342  */
343 Message *
345 {
346  return new BatteryInterface::SleepMessage(this);
347 }
348 /** Check if message is valid and can be enqueued.
349  * @param message Message to check
350  * @return true if the message is valid, false otherwise.
351  */
352 bool
354 {
355  const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
356  if ( m0 != NULL ) {
357  return true;
358  }
359  const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
360  if ( m1 != NULL ) {
361  return true;
362  }
363  return false;
364 }
365 
366 /// @cond INTERNALS
367 EXPORT_INTERFACE(BatteryInterface)
368 /// @endcond
369 
370 
371 } // end namespace fawkes
PushButtonMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
SleepMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
BatteryInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152