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
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
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  data->current = new_current;
98  data_changed = true;
99 }
100 
101 /** Get voltage value.
102  * Battery Voltage [mV]
103  * @return voltage value
104  */
105 uint32_t
107 {
108  return data->voltage;
109 }
110 
111 /** Get maximum length of voltage value.
112  * @return length of voltage value, can be length of the array or number of
113  * maximum number of characters for a string
114  */
115 size_t
117 {
118  return 1;
119 }
120 
121 /** Set voltage value.
122  * Battery Voltage [mV]
123  * @param new_voltage new voltage value
124  */
125 void
126 BatteryInterface::set_voltage(const uint32_t new_voltage)
127 {
128  data->voltage = new_voltage;
129  data_changed = true;
130 }
131 
132 /** Get temperature value.
133  * Battery Temperature [°C]
134  * @return temperature value
135  */
136 uint32_t
138 {
139  return data->temperature;
140 }
141 
142 /** Get maximum length of temperature value.
143  * @return length of temperature value, can be length of the array or number of
144  * maximum number of characters for a string
145  */
146 size_t
148 {
149  return 1;
150 }
151 
152 /** Set temperature value.
153  * Battery Temperature [°C]
154  * @param new_temperature new temperature value
155  */
156 void
157 BatteryInterface::set_temperature(const uint32_t new_temperature)
158 {
159  data->temperature = new_temperature;
160  data_changed = true;
161 }
162 
163 /** Get absolute_soc value.
164  * Absolute state of charge [%]
165  * @return absolute_soc value
166  */
167 float
169 {
170  return data->absolute_soc;
171 }
172 
173 /** Get maximum length of absolute_soc value.
174  * @return length of absolute_soc value, can be length of the array or number of
175  * maximum number of characters for a string
176  */
177 size_t
179 {
180  return 1;
181 }
182 
183 /** Set absolute_soc value.
184  * Absolute state of charge [%]
185  * @param new_absolute_soc new absolute_soc value
186  */
187 void
188 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
189 {
190  data->absolute_soc = new_absolute_soc;
191  data_changed = true;
192 }
193 
194 /** Get relative_soc value.
195  * Relative state of charge [%]
196  * @return relative_soc value
197  */
198 float
200 {
201  return data->relative_soc;
202 }
203 
204 /** Get maximum length of relative_soc value.
205  * @return length of relative_soc value, can be length of the array or number of
206  * maximum number of characters for a string
207  */
208 size_t
210 {
211  return 1;
212 }
213 
214 /** Set relative_soc value.
215  * Relative state of charge [%]
216  * @param new_relative_soc new relative_soc value
217  */
218 void
219 BatteryInterface::set_relative_soc(const float new_relative_soc)
220 {
221  data->relative_soc = new_relative_soc;
222  data_changed = true;
223 }
224 
225 /* =========== message create =========== */
226 Message *
227 BatteryInterface::create_message(const char *type) const
228 {
229  if ( strncmp("PushButtonMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
230  return new PushButtonMessage();
231  } else if ( strncmp("SleepMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
232  return new SleepMessage();
233  } else {
234  throw UnknownTypeException("The given type '%s' does not match any known "
235  "message type for this interface type.", type);
236  }
237 }
238 
239 
240 /** Copy values from other interface.
241  * @param other other interface to copy values from
242  */
243 void
245 {
246  const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
247  if (oi == NULL) {
248  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
249  type(), other->type());
250  }
251  memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
252 }
253 
254 const char *
255 BatteryInterface::enum_tostring(const char *enumtype, int val) const
256 {
257  throw UnknownTypeException("Unknown enum type %s", enumtype);
258 }
259 
260 /* =========== messages =========== */
261 /** @class BatteryInterface::PushButtonMessage <interfaces/BatteryInterface.h>
262  * PushButtonMessage Fawkes BlackBoard Interface Message.
263  *
264 
265  */
266 
267 
268 /** Constructor */
270 {
271  data_size = sizeof(PushButtonMessage_data_t);
272  data_ptr = malloc(data_size);
273  memset(data_ptr, 0, data_size);
274  data = (PushButtonMessage_data_t *)data_ptr;
276 }
277 
278 /** Destructor */
280 {
281  free(data_ptr);
282 }
283 
284 /** Copy constructor.
285  * @param m message to copy from
286  */
288 {
289  data_size = m->data_size;
290  data_ptr = malloc(data_size);
291  memcpy(data_ptr, m->data_ptr, data_size);
292  data = (PushButtonMessage_data_t *)data_ptr;
294 }
295 
296 /* Methods */
297 /** Clone this message.
298  * Produces a message of the same type as this message and copies the
299  * data to the new message.
300  * @return clone of this message
301  */
302 Message *
304 {
305  return new BatteryInterface::PushButtonMessage(this);
306 }
307 /** @class BatteryInterface::SleepMessage <interfaces/BatteryInterface.h>
308  * SleepMessage Fawkes BlackBoard Interface Message.
309  *
310 
311  */
312 
313 
314 /** Constructor */
316 {
317  data_size = sizeof(SleepMessage_data_t);
318  data_ptr = malloc(data_size);
319  memset(data_ptr, 0, data_size);
320  data = (SleepMessage_data_t *)data_ptr;
322 }
323 
324 /** Destructor */
326 {
327  free(data_ptr);
328 }
329 
330 /** Copy constructor.
331  * @param m message to copy from
332  */
334 {
335  data_size = m->data_size;
336  data_ptr = malloc(data_size);
337  memcpy(data_ptr, m->data_ptr, data_size);
338  data = (SleepMessage_data_t *)data_ptr;
340 }
341 
342 /* Methods */
343 /** Clone this message.
344  * Produces a message of the same type as this message and copies the
345  * data to the new message.
346  * @return clone of this message
347  */
348 Message *
350 {
351  return new BatteryInterface::SleepMessage(this);
352 }
353 /** Check if message is valid and can be enqueued.
354  * @param message Message to check
355  * @return true if the message is valid, false otherwise.
356  */
357 bool
359 {
360  const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
361  if ( m0 != NULL ) {
362  return true;
363  }
364  const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
365  if ( m1 != NULL ) {
366  return true;
367  }
368  return false;
369 }
370 
371 /// @cond INTERNALS
372 EXPORT_INTERFACE(BatteryInterface)
373 /// @endcond
374 
375 
376 } // end namespace fawkes
float relative_soc() const
Get relative_soc value.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
virtual Message * create_message(const char *type) const
Create message based on type name.
uint32_t voltage() const
Get voltage value.
virtual Message * clone() const
Clone this message.
size_t maxlenof_relative_soc() const
Get maximum length of relative_soc value.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
size_t maxlenof_current() const
Get maximum length of current value.
size_t maxlenof_absolute_soc() const
Get maximum length of absolute_soc value.
void set_absolute_soc(const float new_absolute_soc)
Set absolute_soc value.
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
BatteryInterface Fawkes BlackBoard Interface.
float absolute_soc() const
Get absolute_soc value.
virtual Message * clone() const
Clone this message.
void set_voltage(const uint32_t new_voltage)
Set voltage value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
uint32_t current() const
Get current value.
void set_temperature(const uint32_t new_temperature)
Set temperature value.
void set_relative_soc(const float new_relative_soc)
Set relative_soc value.
const char * type() const
Get type of interface.
Definition: interface.cpp:651
size_t maxlenof_voltage() const
Get maximum length of voltage value.
SleepMessage Fawkes BlackBoard Interface Message.
PushButtonMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_temperature() const
Get maximum length of temperature value.
float field
Definition: types.h:45
void set_current(const uint32_t new_current)
Set current value.
uint32_t temperature() const
Get temperature value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
32 bit unsigned integer field
Definition: types.h:42