Fawkes API  Fawkes Development Version
KeyValueInterface.cpp
1 
2 /***************************************************************************
3  * KeyValueInterface.cpp - Fawkes BlackBoard Interface - KeyValueInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2015 Gesche Gierse
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/KeyValueInterface.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 KeyValueInterface <interfaces/KeyValueInterface.h>
36  * KeyValueInterface Fawkes BlackBoard Interface.
37  * Key-Value interface. Use this to publish Key-Value based information, if you do not want to create a new interface type for the data. This interface can be used for different kind of data, but should only contain one value at a time. Set the value_type field to represent which kind of value should be transported (e.g. TYPE_INT for integer) and fill the data in the correct value field (e.g. value_int).
38  * @ingroup FawkesInterfaces
39  */
40 
41 
42 
43 /** Constructor */
44 KeyValueInterface::KeyValueInterface() : Interface()
45 {
46  data_size = sizeof(KeyValueInterface_data_t);
47  data_ptr = malloc(data_size);
48  data = (KeyValueInterface_data_t *)data_ptr;
49  data_ts = (interface_data_ts_t *)data_ptr;
50  memset(data_ptr, 0, data_size);
51  enum_map_ValueType[(int)TypeStr] = "TypeStr";
52  enum_map_ValueType[(int)TypeInt] = "TypeInt";
53  enum_map_ValueType[(int)TypeUint] = "TypeUint";
54  enum_map_ValueType[(int)TypeBool] = "TypeBool";
55  enum_map_ValueType[(int)TypeByte] = "TypeByte";
56  enum_map_ValueType[(int)TypeFloat] = "TypeFloat";
57  add_fieldinfo(IFT_STRING, "key", 32, data->key);
58  add_fieldinfo(IFT_ENUM, "value_type", 1, &data->value_type, "ValueType", &enum_map_ValueType);
59  add_fieldinfo(IFT_STRING, "value_string", 32, data->value_string);
60  add_fieldinfo(IFT_UINT32, "value_uint", 1, &data->value_uint);
61  add_fieldinfo(IFT_INT32, "value_int", 1, &data->value_int);
62  add_fieldinfo(IFT_BOOL, "value_bool", 1, &data->value_bool);
63  add_fieldinfo(IFT_BYTE, "value_byte", 1, &data->value_byte);
64  add_fieldinfo(IFT_FLOAT, "value_float", 1, &data->value_float);
65  unsigned char tmp_hash[] = {0xf1, 0x89, 0x81, 0x4f, 0xb9, 0x6e, 0x5c, 0xc8, 0x78, 0x90, 0x1a, 0x10, 0xdb, 0xa9, 0xa0, 0x52};
66  set_hash(tmp_hash);
67 }
68 
69 /** Destructor */
70 KeyValueInterface::~KeyValueInterface()
71 {
72  free(data_ptr);
73 }
74 /** Convert ValueType constant to string.
75  * @param value value to convert to string
76  * @return constant value as string.
77  */
78 const char *
80 {
81  switch (value) {
82  case TypeStr: return "TypeStr";
83  case TypeInt: return "TypeInt";
84  case TypeUint: return "TypeUint";
85  case TypeBool: return "TypeBool";
86  case TypeByte: return "TypeByte";
87  case TypeFloat: return "TypeFloat";
88  default: return "UNKNOWN";
89  }
90 }
91 /* Methods */
92 /** Get key value.
93  * The key entry
94  * @return key value
95  */
96 char *
98 {
99  return data->key;
100 }
101 
102 /** Get maximum length of key value.
103  * @return length of key value, can be length of the array or number of
104  * maximum number of characters for a string
105  */
106 size_t
108 {
109  return 32;
110 }
111 
112 /** Set key value.
113  * The key entry
114  * @param new_key new key value
115  */
116 void
117 KeyValueInterface::set_key(const char * new_key)
118 {
119  data_changed |= change_field(data->key, new_key);
120 }
121 
122 /** Get value_type value.
123  * The type of the value entry.
124  * @return value_type value
125  */
128 {
129  return (KeyValueInterface::ValueType)data->value_type;
130 }
131 
132 /** Get maximum length of value_type value.
133  * @return length of value_type value, can be length of the array or number of
134  * maximum number of characters for a string
135  */
136 size_t
138 {
139  return 1;
140 }
141 
142 /** Set value_type value.
143  * The type of the value entry.
144  * @param new_value_type new value_type value
145  */
146 void
148 {
149  data_changed |= change_field(data->value_type, new_value_type);
150 }
151 
152 /** Get value_string value.
153  * Value with type string
154  * @return value_string value
155  */
156 char *
158 {
159  return data->value_string;
160 }
161 
162 /** Get maximum length of value_string value.
163  * @return length of value_string value, can be length of the array or number of
164  * maximum number of characters for a string
165  */
166 size_t
168 {
169  return 32;
170 }
171 
172 /** Set value_string value.
173  * Value with type string
174  * @param new_value_string new value_string value
175  */
176 void
177 KeyValueInterface::set_value_string(const char * new_value_string)
178 {
179  data_changed |= change_field(data->value_string, new_value_string);
180 }
181 
182 /** Get value_uint value.
183  * Value with type uint32
184  * @return value_uint value
185  */
186 uint32_t
188 {
189  return data->value_uint;
190 }
191 
192 /** Get maximum length of value_uint value.
193  * @return length of value_uint value, can be length of the array or number of
194  * maximum number of characters for a string
195  */
196 size_t
198 {
199  return 1;
200 }
201 
202 /** Set value_uint value.
203  * Value with type uint32
204  * @param new_value_uint new value_uint value
205  */
206 void
207 KeyValueInterface::set_value_uint(const uint32_t new_value_uint)
208 {
209  data_changed |= change_field(data->value_uint, new_value_uint);
210 }
211 
212 /** Get value_int value.
213  * Value with type integer
214  * @return value_int value
215  */
216 int32_t
218 {
219  return data->value_int;
220 }
221 
222 /** Get maximum length of value_int value.
223  * @return length of value_int value, can be length of the array or number of
224  * maximum number of characters for a string
225  */
226 size_t
228 {
229  return 1;
230 }
231 
232 /** Set value_int value.
233  * Value with type integer
234  * @param new_value_int new value_int value
235  */
236 void
237 KeyValueInterface::set_value_int(const int32_t new_value_int)
238 {
239  data_changed |= change_field(data->value_int, new_value_int);
240 }
241 
242 /** Get value_bool value.
243  * Value with type Bool
244  * @return value_bool value
245  */
246 bool
248 {
249  return data->value_bool;
250 }
251 
252 /** Get maximum length of value_bool value.
253  * @return length of value_bool value, can be length of the array or number of
254  * maximum number of characters for a string
255  */
256 size_t
258 {
259  return 1;
260 }
261 
262 /** Set value_bool value.
263  * Value with type Bool
264  * @param new_value_bool new value_bool value
265  */
266 void
267 KeyValueInterface::set_value_bool(const bool new_value_bool)
268 {
269  data_changed |= change_field(data->value_bool, new_value_bool);
270 }
271 
272 /** Get value_byte value.
273  * Value with type byte
274  * @return value_byte value
275  */
276 uint8_t
278 {
279  return data->value_byte;
280 }
281 
282 /** Get maximum length of value_byte value.
283  * @return length of value_byte value, can be length of the array or number of
284  * maximum number of characters for a string
285  */
286 size_t
288 {
289  return 1;
290 }
291 
292 /** Set value_byte value.
293  * Value with type byte
294  * @param new_value_byte new value_byte value
295  */
296 void
297 KeyValueInterface::set_value_byte(const uint8_t new_value_byte)
298 {
299  data_changed |= change_field(data->value_byte, new_value_byte);
300 }
301 
302 /** Get value_float value.
303  * Value with type float
304  * @return value_float value
305  */
306 float
308 {
309  return data->value_float;
310 }
311 
312 /** Get maximum length of value_float value.
313  * @return length of value_float value, can be length of the array or number of
314  * maximum number of characters for a string
315  */
316 size_t
318 {
319  return 1;
320 }
321 
322 /** Set value_float value.
323  * Value with type float
324  * @param new_value_float new value_float value
325  */
326 void
327 KeyValueInterface::set_value_float(const float new_value_float)
328 {
329  data_changed |= change_field(data->value_float, new_value_float);
330 }
331 
332 /* =========== message create =========== */
333 Message *
335 {
336  throw UnknownTypeException("The given type '%s' does not match any known "
337  "message type for this interface type.", type);
338 }
339 
340 
341 /** Copy values from other interface.
342  * @param other other interface to copy values from
343  */
344 void
346 {
347  const KeyValueInterface *oi = dynamic_cast<const KeyValueInterface *>(other);
348  if (oi == NULL) {
349  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
350  type(), other->type());
351  }
352  memcpy(data, oi->data, sizeof(KeyValueInterface_data_t));
353 }
354 
355 const char *
356 KeyValueInterface::enum_tostring(const char *enumtype, int val) const
357 {
358  if (strcmp(enumtype, "ValueType") == 0) {
359  return tostring_ValueType((ValueType)val);
360  }
361  throw UnknownTypeException("Unknown enum type %s", enumtype);
362 }
363 
364 /* =========== messages =========== */
365 /** Check if message is valid and can be enqueued.
366  * @param message Message to check
367  * @return true if the message is valid, false otherwise.
368  */
369 bool
371 {
372  return false;
373 }
374 
375 /// @cond INTERNALS
376 EXPORT_INTERFACE(KeyValueInterface)
377 /// @endcond
378 
379 
380 } // end namespace fawkes
fawkes::KeyValueInterface::set_value_byte
void set_value_byte(const uint8_t new_value_byte)
Set value_byte value.
Definition: KeyValueInterface.cpp:297
fawkes::Interface::data_ptr
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
fawkes::KeyValueInterface::maxlenof_key
size_t maxlenof_key() const
Get maximum length of key value.
Definition: KeyValueInterface.cpp:107
fawkes::KeyValueInterface::message_valid
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Definition: KeyValueInterface.cpp:370
fawkes::KeyValueInterface::set_value_uint
void set_value_uint(const uint32_t new_value_uint)
Set value_uint value.
Definition: KeyValueInterface.cpp:207
fawkes::Message
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
fawkes::KeyValueInterface::maxlenof_value_type
size_t maxlenof_value_type() const
Get maximum length of value_type value.
Definition: KeyValueInterface.cpp:137
fawkes::Message::data_ptr
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
fawkes::IFT_BOOL
@ IFT_BOOL
boolean field
Definition: types.h:37
fawkes::KeyValueInterface::set_value_type
void set_value_type(const ValueType new_value_type)
Set value_type value.
Definition: KeyValueInterface.cpp:147
fawkes::IFT_FLOAT
@ IFT_FLOAT
float field
Definition: types.h:46
fawkes::IFT_ENUM
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:50
fawkes::Message::data_ts
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
fawkes::KeyValueInterface::maxlenof_value_float
size_t maxlenof_value_float() const
Get maximum length of value_float value.
Definition: KeyValueInterface.cpp:317
fawkes::KeyValueInterface::value_int
int32_t value_int() const
Get value_int value.
Definition: KeyValueInterface.cpp:217
fawkes::IFT_UINT32
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:43
fawkes::KeyValueInterface::set_value_float
void set_value_float(const float new_value_float)
Set value_float value.
Definition: KeyValueInterface.cpp:327
fawkes::KeyValueInterface::value_uint
uint32_t value_uint() const
Get value_uint value.
Definition: KeyValueInterface.cpp:187
fawkes::Interface::type
const char * type() const
Get type of interface.
Definition: interface.cpp:643
fawkes::KeyValueInterface::set_value_bool
void set_value_bool(const bool new_value_bool)
Set value_bool value.
Definition: KeyValueInterface.cpp:267
fawkes::KeyValueInterface::maxlenof_value_bool
size_t maxlenof_value_bool() const
Get maximum length of value_bool value.
Definition: KeyValueInterface.cpp:257
fawkes::KeyValueInterface::copy_values
virtual void copy_values(const Interface *other)
Copy values from other interface.
Definition: KeyValueInterface.cpp:345
fawkes::TypeMismatchException
Type mismatch.
Definition: software.h:44
fawkes::KeyValueInterface
KeyValueInterface Fawkes BlackBoard Interface.
Definition: KeyValueInterface.h:34
fawkes::Interface::data_changed
bool data_changed
Indicator if data has changed.
Definition: interface.h:226
fawkes::KeyValueInterface::value_type
ValueType value_type() const
Get value_type value.
Definition: KeyValueInterface.cpp:127
fawkes::IFT_INT32
@ IFT_INT32
32 bit integer field
Definition: types.h:42
fawkes::UnknownTypeException
Unknown type.
Definition: software.h:50
fawkes::KeyValueInterface::value_string
char * value_string() const
Get value_string value.
Definition: KeyValueInterface.cpp:157
fawkes
Fawkes library namespace.
fawkes::Interface::set_hash
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:316
fawkes::KeyValueInterface::value_byte
uint8_t value_byte() const
Get value_byte value.
Definition: KeyValueInterface.cpp:277
fawkes::Message::data_size
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
fawkes::KeyValueInterface::is_value_bool
bool is_value_bool() const
Get value_bool value.
Definition: KeyValueInterface.cpp:247
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::KeyValueInterface::set_key
void set_key(const char *new_key)
Set key value.
Definition: KeyValueInterface.cpp:117
fawkes::KeyValueInterface::ValueType
ValueType
Indicator of current o.
Definition: KeyValueInterface.h:42
fawkes::KeyValueInterface::maxlenof_value_string
size_t maxlenof_value_string() const
Get maximum length of value_string value.
Definition: KeyValueInterface.cpp:167
fawkes::Message::add_fieldinfo
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:400
fawkes::KeyValueInterface::maxlenof_value_byte
size_t maxlenof_value_byte() const
Get maximum length of value_byte value.
Definition: KeyValueInterface.cpp:287
fawkes::KeyValueInterface::tostring_ValueType
const char * tostring_ValueType(ValueType value) const
Convert ValueType constant to string.
Definition: KeyValueInterface.cpp:79
fawkes::KeyValueInterface::enum_tostring
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
Definition: KeyValueInterface.cpp:356
fawkes::KeyValueInterface::set_value_string
void set_value_string(const char *new_value_string)
Set value_string value.
Definition: KeyValueInterface.cpp:177
fawkes::KeyValueInterface::value_float
float value_float() const
Get value_float value.
Definition: KeyValueInterface.cpp:307
fawkes::IFT_STRING
@ IFT_STRING
string field
Definition: types.h:48
fawkes::KeyValueInterface::key
char * key() const
Get key value.
Definition: KeyValueInterface.cpp:97
fawkes::IFT_BYTE
@ IFT_BYTE
byte field, alias for uint8
Definition: types.h:49
fawkes::KeyValueInterface::set_value_int
void set_value_int(const int32_t new_value_int)
Set value_int value.
Definition: KeyValueInterface.cpp:237
fawkes::KeyValueInterface::maxlenof_value_uint
size_t maxlenof_value_uint() const
Get maximum length of value_uint value.
Definition: KeyValueInterface.cpp:197
fawkes::KeyValueInterface::maxlenof_value_int
size_t maxlenof_value_int() const
Get maximum length of value_int value.
Definition: KeyValueInterface.cpp:227
fawkes::change_field
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167
fawkes::KeyValueInterface::create_message
virtual Message * create_message(const char *type) const
Create message based on type name.
Definition: KeyValueInterface.cpp:334