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 *
79 KeyValueInterface::tostring_ValueType(ValueType value) const
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  strncpy(data->key, new_key, sizeof(data->key)-1);
120  data->key[sizeof(data->key)-1] = 0;
121  data_changed = true;
122 }
123 
124 /** Get value_type value.
125  * The type of the value entry.
126  * @return value_type value
127  */
130 {
131  return (KeyValueInterface::ValueType)data->value_type;
132 }
133 
134 /** Get maximum length of value_type value.
135  * @return length of value_type value, can be length of the array or number of
136  * maximum number of characters for a string
137  */
138 size_t
140 {
141  return 1;
142 }
143 
144 /** Set value_type value.
145  * The type of the value entry.
146  * @param new_value_type new value_type value
147  */
148 void
149 KeyValueInterface::set_value_type(const ValueType new_value_type)
150 {
151  data->value_type = new_value_type;
152  data_changed = true;
153 }
154 
155 /** Get value_string value.
156  * Value with type string
157  * @return value_string value
158  */
159 char *
161 {
162  return data->value_string;
163 }
164 
165 /** Get maximum length of value_string value.
166  * @return length of value_string value, can be length of the array or number of
167  * maximum number of characters for a string
168  */
169 size_t
171 {
172  return 32;
173 }
174 
175 /** Set value_string value.
176  * Value with type string
177  * @param new_value_string new value_string value
178  */
179 void
180 KeyValueInterface::set_value_string(const char * new_value_string)
181 {
182  strncpy(data->value_string, new_value_string, sizeof(data->value_string)-1);
183  data->value_string[sizeof(data->value_string)-1] = 0;
184  data_changed = true;
185 }
186 
187 /** Get value_uint value.
188  * Value with type uint32
189  * @return value_uint value
190  */
191 uint32_t
193 {
194  return data->value_uint;
195 }
196 
197 /** Get maximum length of value_uint value.
198  * @return length of value_uint value, can be length of the array or number of
199  * maximum number of characters for a string
200  */
201 size_t
203 {
204  return 1;
205 }
206 
207 /** Set value_uint value.
208  * Value with type uint32
209  * @param new_value_uint new value_uint value
210  */
211 void
212 KeyValueInterface::set_value_uint(const uint32_t new_value_uint)
213 {
214  data->value_uint = new_value_uint;
215  data_changed = true;
216 }
217 
218 /** Get value_int value.
219  * Value with type integer
220  * @return value_int value
221  */
222 int32_t
224 {
225  return data->value_int;
226 }
227 
228 /** Get maximum length of value_int value.
229  * @return length of value_int value, can be length of the array or number of
230  * maximum number of characters for a string
231  */
232 size_t
234 {
235  return 1;
236 }
237 
238 /** Set value_int value.
239  * Value with type integer
240  * @param new_value_int new value_int value
241  */
242 void
243 KeyValueInterface::set_value_int(const int32_t new_value_int)
244 {
245  data->value_int = new_value_int;
246  data_changed = true;
247 }
248 
249 /** Get value_bool value.
250  * Value with type Bool
251  * @return value_bool value
252  */
253 bool
255 {
256  return data->value_bool;
257 }
258 
259 /** Get maximum length of value_bool value.
260  * @return length of value_bool value, can be length of the array or number of
261  * maximum number of characters for a string
262  */
263 size_t
265 {
266  return 1;
267 }
268 
269 /** Set value_bool value.
270  * Value with type Bool
271  * @param new_value_bool new value_bool value
272  */
273 void
274 KeyValueInterface::set_value_bool(const bool new_value_bool)
275 {
276  data->value_bool = new_value_bool;
277  data_changed = true;
278 }
279 
280 /** Get value_byte value.
281  * Value with type byte
282  * @return value_byte value
283  */
284 uint8_t
286 {
287  return data->value_byte;
288 }
289 
290 /** Get maximum length of value_byte value.
291  * @return length of value_byte value, can be length of the array or number of
292  * maximum number of characters for a string
293  */
294 size_t
296 {
297  return 1;
298 }
299 
300 /** Set value_byte value.
301  * Value with type byte
302  * @param new_value_byte new value_byte value
303  */
304 void
305 KeyValueInterface::set_value_byte(const uint8_t new_value_byte)
306 {
307  data->value_byte = new_value_byte;
308  data_changed = true;
309 }
310 
311 /** Get value_float value.
312  * Value with type float
313  * @return value_float value
314  */
315 float
317 {
318  return data->value_float;
319 }
320 
321 /** Get maximum length of value_float value.
322  * @return length of value_float value, can be length of the array or number of
323  * maximum number of characters for a string
324  */
325 size_t
327 {
328  return 1;
329 }
330 
331 /** Set value_float value.
332  * Value with type float
333  * @param new_value_float new value_float value
334  */
335 void
336 KeyValueInterface::set_value_float(const float new_value_float)
337 {
338  data->value_float = new_value_float;
339  data_changed = true;
340 }
341 
342 /* =========== message create =========== */
343 Message *
344 KeyValueInterface::create_message(const char *type) const
345 {
346  throw UnknownTypeException("The given type '%s' does not match any known "
347  "message type for this interface type.", type);
348 }
349 
350 
351 /** Copy values from other interface.
352  * @param other other interface to copy values from
353  */
354 void
356 {
357  const KeyValueInterface *oi = dynamic_cast<const KeyValueInterface *>(other);
358  if (oi == NULL) {
359  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
360  type(), other->type());
361  }
362  memcpy(data, oi->data, sizeof(KeyValueInterface_data_t));
363 }
364 
365 const char *
366 KeyValueInterface::enum_tostring(const char *enumtype, int val) const
367 {
368  if (strcmp(enumtype, "ValueType") == 0) {
369  return tostring_ValueType((ValueType)val);
370  }
371  throw UnknownTypeException("Unknown enum type %s", enumtype);
372 }
373 
374 /* =========== messages =========== */
375 /** Check if message is valid and can be enqueued.
376  * @param message Message to check
377  * @return true if the message is valid, false otherwise.
378  */
379 bool
380 KeyValueInterface::message_valid(const Message *message) const
381 {
382  return false;
383 }
384 
385 /// @cond INTERNALS
386 EXPORT_INTERFACE(KeyValueInterface)
387 /// @endcond
388 
389 
390 } // 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:311
fawkes::Interface::data_ptr
void * data_ptr
Definition: interface.h:224
fawkes::KeyValueInterface::maxlenof_key
size_t maxlenof_key() const
Get maximum length of key value.
Definition: KeyValueInterface.cpp:113
fawkes::KeyValueInterface::message_valid
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Definition: KeyValueInterface.cpp:386
fawkes::KeyValueInterface::set_value_uint
void set_value_uint(const uint32_t new_value_uint)
Set value_uint value.
Definition: KeyValueInterface.cpp:218
fawkes::Message
Definition: message.h:41
fawkes::KeyValueInterface::maxlenof_value_type
size_t maxlenof_value_type() const
Get maximum length of value_type value.
Definition: KeyValueInterface.cpp:145
fawkes::Message::data_ptr
void * data_ptr
Definition: message.h:125
fawkes::IFT_BOOL
@ IFT_BOOL
boolean field
Definition: types.h:51
fawkes::KeyValueInterface::set_value_type
void set_value_type(const ValueType new_value_type)
Set value_type value.
Definition: KeyValueInterface.cpp:155
fawkes::IFT_FLOAT
@ IFT_FLOAT
float field
Definition: types.h:60
fawkes::IFT_ENUM
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:64
fawkes::Message::data_ts
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:135
fawkes::KeyValueInterface::maxlenof_value_float
size_t maxlenof_value_float() const
Get maximum length of value_float value.
Definition: KeyValueInterface.cpp:332
fawkes::KeyValueInterface::value_int
int32_t value_int() const
Get value_int value.
Definition: KeyValueInterface.cpp:229
fawkes::IFT_UINT32
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:57
fawkes::KeyValueInterface::set_value_float
void set_value_float(const float new_value_float)
Set value_float value.
Definition: KeyValueInterface.cpp:342
fawkes::KeyValueInterface::value_uint
uint32_t value_uint() const
Get value_uint value.
Definition: KeyValueInterface.cpp:198
fawkes::Interface::type
const char * type() const
Get type of interface.
Definition: interface.cpp:645
fawkes::KeyValueInterface::set_value_bool
void set_value_bool(const bool new_value_bool)
Set value_bool value.
Definition: KeyValueInterface.cpp:280
fawkes::KeyValueInterface::maxlenof_value_bool
size_t maxlenof_value_bool() const
Get maximum length of value_bool value.
Definition: KeyValueInterface.cpp:270
fawkes::KeyValueInterface::copy_values
virtual void copy_values(const Interface *other)
Copy values from other interface.
Definition: KeyValueInterface.cpp:361
fawkes::TypeMismatchException
Definition: software.h:49
fawkes::KeyValueInterface
Definition: KeyValueInterface.h:39
fawkes::Interface::data_changed
bool data_changed
Definition: interface.h:226
fawkes::KeyValueInterface::value_type
ValueType value_type() const
Get value_type value.
Definition: KeyValueInterface.cpp:135
fawkes::IFT_INT32
@ IFT_INT32
32 bit integer field
Definition: types.h:56
fawkes::UnknownTypeException
Definition: software.h:55
fawkes::KeyValueInterface::value_string
char * value_string() const
Get value_string value.
Definition: KeyValueInterface.cpp:166
fawkes
fawkes::Interface::set_hash
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:321
fawkes::KeyValueInterface::value_byte
uint8_t value_byte() const
Get value_byte value.
Definition: KeyValueInterface.cpp:291
fawkes::Message::data_size
unsigned int data_size
Definition: message.h:126
fawkes::KeyValueInterface::is_value_bool
bool is_value_bool() const
Get value_bool value.
Definition: KeyValueInterface.cpp:260
fawkes::Interface
Definition: interface.h:78
fawkes::KeyValueInterface::set_key
void set_key(const char *new_key)
Set key value.
Definition: KeyValueInterface.cpp:123
fawkes::KeyValueInterface::ValueType
ValueType
Indicator of current o.
Definition: KeyValueInterface.h:54
fawkes::KeyValueInterface::maxlenof_value_string
size_t maxlenof_value_string() const
Get maximum length of value_string value.
Definition: KeyValueInterface.cpp:176
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:406
fawkes::KeyValueInterface::maxlenof_value_byte
size_t maxlenof_value_byte() const
Get maximum length of value_byte value.
Definition: KeyValueInterface.cpp:301
fawkes::KeyValueInterface::tostring_ValueType
const char * tostring_ValueType(ValueType value) const
Convert ValueType constant to string.
Definition: KeyValueInterface.cpp:85
fawkes::KeyValueInterface::enum_tostring
virtual const char * enum_tostring(const char *enumtype, int val) const
Definition: KeyValueInterface.cpp:372
fawkes::KeyValueInterface::set_value_string
void set_value_string(const char *new_value_string)
Set value_string value.
Definition: KeyValueInterface.cpp:186
fawkes::KeyValueInterface::value_float
float value_float() const
Get value_float value.
Definition: KeyValueInterface.cpp:322
fawkes::IFT_STRING
@ IFT_STRING
string field
Definition: types.h:62
fawkes::KeyValueInterface::key
char * key() const
Get key value.
Definition: KeyValueInterface.cpp:103
fawkes::IFT_BYTE
@ IFT_BYTE
byte field, alias for uint8
Definition: types.h:63
fawkes::KeyValueInterface::set_value_int
void set_value_int(const int32_t new_value_int)
Set value_int value.
Definition: KeyValueInterface.cpp:249
fawkes::KeyValueInterface::maxlenof_value_uint
size_t maxlenof_value_uint() const
Get maximum length of value_uint value.
Definition: KeyValueInterface.cpp:208
fawkes::KeyValueInterface::maxlenof_value_int
size_t maxlenof_value_int() const
Get maximum length of value_int value.
Definition: KeyValueInterface.cpp:239
fawkes::KeyValueInterface::create_message
virtual Message * create_message(const char *type) const
Definition: KeyValueInterface.cpp:350