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 <cstring>
29 #include <cstdlib>
30 
31 namespace fawkes {
32 
33 /** @class BatteryInterface <interfaces/BatteryInterface.h>
34  * BatteryInterface Fawkes BlackBoard Interface.
35  * This interface contains status information about the
36  battery. In addition to this it allows to send messages which
37  turn the battery on/off
38  * @ingroup FawkesInterfaces
39  */
40 
41 
42 
43 /** Constructor */
44 BatteryInterface::BatteryInterface() : Interface()
45 {
46  data_size = sizeof(BatteryInterface_data_t);
47  data_ptr = malloc(data_size);
48  data = (BatteryInterface_data_t *)data_ptr;
49  data_ts = (interface_data_ts_t *)data_ptr;
50  memset(data_ptr, 0, data_size);
51  add_fieldinfo(IFT_UINT32, "current", 1, &data->current, "");
52  add_fieldinfo(IFT_UINT32, "voltage", 1, &data->voltage, "");
53  add_fieldinfo(IFT_UINT32, "temperature", 1, &data->temperature, "");
54  add_fieldinfo(IFT_FLOAT, "absolute_soc", 1, &data->absolute_soc, "");
55  add_fieldinfo(IFT_FLOAT, "relative_soc", 1, &data->relative_soc, "");
56  add_messageinfo("PushButtonMessage");
57  add_messageinfo("SleepMessage");
58  unsigned char tmp_hash[] = {0x28, 0xb6, 0xbe, 0xe7, 0xf1, 0x47, 0x2, 0x12, 0x1d, 0xe3, 0x7c, 0x14, 0xe9, 0x1f, 0x24, 0x4d};
59  set_hash(tmp_hash);
60 }
61 
62 /** Destructor */
63 BatteryInterface::~BatteryInterface()
64 {
65  free(data_ptr);
66 }
67 /* Methods */
68 /** Get current value.
69  * Battery Current [mA]
70  * @return current value
71  */
72 uint32_t
74 {
75  return data->current;
76 }
77 
78 /** Get maximum length of current value.
79  * @return length of current value, can be length of the array or number of
80  * maximum number of characters for a string
81  */
82 size_t
84 {
85  return 1;
86 }
87 
88 /** Set current value.
89  * Battery Current [mA]
90  * @param new_current new current value
91  */
92 void
93 BatteryInterface::set_current(const uint32_t new_current)
94 {
95  data->current = new_current;
96  data_changed = true;
97 }
98 
99 /** Get voltage value.
100  * Battery Voltage [mV]
101  * @return voltage value
102  */
103 uint32_t
105 {
106  return data->voltage;
107 }
108 
109 /** Get maximum length of voltage value.
110  * @return length of voltage value, can be length of the array or number of
111  * maximum number of characters for a string
112  */
113 size_t
115 {
116  return 1;
117 }
118 
119 /** Set voltage value.
120  * Battery Voltage [mV]
121  * @param new_voltage new voltage value
122  */
123 void
124 BatteryInterface::set_voltage(const uint32_t new_voltage)
125 {
126  data->voltage = new_voltage;
127  data_changed = true;
128 }
129 
130 /** Get temperature value.
131  * Battery Temperature [°C]
132  * @return temperature value
133  */
134 uint32_t
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
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  data->temperature = new_temperature;
158  data_changed = true;
159 }
160 
161 /** Get absolute_soc value.
162  * Absolute state of charge [%]
163  * @return absolute_soc value
164  */
165 float
167 {
168  return data->absolute_soc;
169 }
170 
171 /** Get maximum length of absolute_soc value.
172  * @return length of absolute_soc value, can be length of the array or number of
173  * maximum number of characters for a string
174  */
175 size_t
177 {
178  return 1;
179 }
180 
181 /** Set absolute_soc value.
182  * Absolute state of charge [%]
183  * @param new_absolute_soc new absolute_soc value
184  */
185 void
186 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
187 {
188  data->absolute_soc = new_absolute_soc;
189  data_changed = true;
190 }
191 
192 /** Get relative_soc value.
193  * Relative state of charge [%]
194  * @return relative_soc value
195  */
196 float
198 {
199  return data->relative_soc;
200 }
201 
202 /** Get maximum length of relative_soc value.
203  * @return length of relative_soc value, can be length of the array or number of
204  * maximum number of characters for a string
205  */
206 size_t
208 {
209  return 1;
210 }
211 
212 /** Set relative_soc value.
213  * Relative state of charge [%]
214  * @param new_relative_soc new relative_soc value
215  */
216 void
217 BatteryInterface::set_relative_soc(const float new_relative_soc)
218 {
219  data->relative_soc = new_relative_soc;
220  data_changed = true;
221 }
222 
223 /* =========== message create =========== */
224 Message *
225 BatteryInterface::create_message(const char *type) const
226 {
227  if ( strncmp("PushButtonMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
228  return new PushButtonMessage();
229  } else if ( strncmp("SleepMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
230  return new SleepMessage();
231  } else {
232  throw UnknownTypeException("The given type '%s' does not match any known "
233  "message type for this interface type.", type);
234  }
235 }
236 
237 
238 /** Copy values from other interface.
239  * @param other other interface to copy values from
240  */
241 void
243 {
244  const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
245  if (oi == NULL) {
246  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
247  type(), other->type());
248  }
249  memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
250 }
251 
252 const char *
253 BatteryInterface::enum_tostring(const char *enumtype, int val) const
254 {
255  throw UnknownTypeException("Unknown enum type %s", enumtype);
256 }
257 
258 /* =========== messages =========== */
259 /** @class BatteryInterface::PushButtonMessage <interfaces/BatteryInterface.h>
260  * PushButtonMessage Fawkes BlackBoard Interface Message.
261  *
262 
263  */
264 
265 
266 /** Constructor */
268 {
269  data_size = sizeof(PushButtonMessage_data_t);
270  data_ptr = malloc(data_size);
271  memset(data_ptr, 0, data_size);
272  data = (PushButtonMessage_data_t *)data_ptr;
274 }
275 
276 /** Destructor */
278 {
279  free(data_ptr);
280 }
281 
282 /** Copy constructor.
283  * @param m message to copy from
284  */
286 {
287  data_size = m->data_size;
288  data_ptr = malloc(data_size);
289  memcpy(data_ptr, m->data_ptr, data_size);
290  data = (PushButtonMessage_data_t *)data_ptr;
292 }
293 
294 /* Methods */
295 /** Clone this message.
296  * Produces a message of the same type as this message and copies the
297  * data to the new message.
298  * @return clone of this message
299  */
300 Message *
302 {
303  return new BatteryInterface::PushButtonMessage(this);
304 }
305 /** @class BatteryInterface::SleepMessage <interfaces/BatteryInterface.h>
306  * SleepMessage Fawkes BlackBoard Interface Message.
307  *
308 
309  */
310 
311 
312 /** Constructor */
314 {
315  data_size = sizeof(SleepMessage_data_t);
316  data_ptr = malloc(data_size);
317  memset(data_ptr, 0, data_size);
318  data = (SleepMessage_data_t *)data_ptr;
320 }
321 
322 /** Destructor */
324 {
325  free(data_ptr);
326 }
327 
328 /** Copy constructor.
329  * @param m message to copy from
330  */
332 {
333  data_size = m->data_size;
334  data_ptr = malloc(data_size);
335  memcpy(data_ptr, m->data_ptr, data_size);
336  data = (SleepMessage_data_t *)data_ptr;
338 }
339 
340 /* Methods */
341 /** Clone this message.
342  * Produces a message of the same type as this message and copies the
343  * data to the new message.
344  * @return clone of this message
345  */
346 Message *
348 {
349  return new BatteryInterface::SleepMessage(this);
350 }
351 /** Check if message is valid and can be enqueued.
352  * @param message Message to check
353  * @return true if the message is valid, false otherwise.
354  */
355 bool
357 {
358  const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
359  if ( m0 != NULL ) {
360  return true;
361  }
362  const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
363  if ( m1 != NULL ) {
364  return true;
365  }
366  return false;
367 }
368 
369 /// @cond INTERNALS
370 EXPORT_INTERFACE(BatteryInterface)
371 /// @endcond
372 
373 
374 } // end namespace fawkes
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:114
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:43
void set_absolute_soc(const float new_absolute_soc)
Set absolute_soc value.
size_t maxlenof_temperature() const
Get maximum length of temperature value.
virtual Message * clone() const
Clone this message.
Fawkes library namespace.
size_t maxlenof_voltage() const
Get maximum length of voltage value.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:119
size_t maxlenof_absolute_soc() const
Get maximum length of absolute_soc value.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
BatteryInterface Fawkes BlackBoard Interface.
void set_voltage(const uint32_t new_voltage)
Set voltage value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:123
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:115
void set_temperature(const uint32_t new_temperature)
Set temperature value.
void set_relative_soc(const float new_relative_soc)
Set relative_soc value.
size_t maxlenof_current() const
Get maximum length of current value.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
SleepMessage Fawkes BlackBoard Interface Message.
uint32_t current() const
Get current value.
PushButtonMessage Fawkes BlackBoard Interface Message.
virtual Message * create_message(const char *type) const
Create message based on type name.
float relative_soc() const
Get relative_soc value.
float field
Definition: types.h:43
virtual Message * clone() const
Clone this message.
void set_current(const uint32_t new_current)
Set current value.
uint32_t voltage() const
Get voltage value.
float absolute_soc() const
Get absolute_soc value.
size_t maxlenof_relative_soc() const
Get maximum length of relative_soc value.
const char * type() const
Get type of interface.
Definition: interface.cpp:635
virtual void copy_values(const Interface *other)
Copy values from other interface.
32 bit unsigned integer field
Definition: types.h:40
uint32_t temperature() const
Get temperature value.