Fawkes API  Fawkes Development Version
TestInterface.cpp
1 
2 /***************************************************************************
3  * TestInterface.cpp - Fawkes BlackBoard Interface - TestInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2006-2007 Tim Niemueller
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/TestInterface.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 TestInterface <interfaces/TestInterface.h>
36  * TestInterface Fawkes BlackBoard Interface.
37  * Test interface. Use this to play around. Do NOT remove any fields, as this
38  interface is used by BlackBoard QA.
39  * @ingroup FawkesInterfaces
40  */
41 
42 
43 /** TEST_CONSTANT constant */
44 const int32_t TestInterface::TEST_CONSTANT = 5;
45 /** TEST_FLOAT_CONSTANT constant */
46 const float TestInterface::TEST_FLOAT_CONSTANT = 1.2;
47 
48 /** Constructor */
49 TestInterface::TestInterface() : Interface()
50 {
51  data_size = sizeof(TestInterface_data_t);
52  data_ptr = malloc(data_size);
53  data = (TestInterface_data_t *)data_ptr;
54  data_ts = (interface_data_ts_t *)data_ptr;
55  memset(data_ptr, 0, data_size);
56  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
57  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
58  add_fieldinfo(IFT_BOOL, "test_bool", 1, &data->test_bool);
59  add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
60  add_fieldinfo(IFT_BYTE, "flags", 1, &data->flags);
61  add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
62  add_fieldinfo(IFT_INT32, "result", 1, &data->result);
63  add_fieldinfo(IFT_UINT32, "test_uint", 1, &data->test_uint);
64  add_messageinfo("SetTestIntMessage");
65  add_messageinfo("SetTestStringMessage");
66  add_messageinfo("CalculateMessage");
67  unsigned char tmp_hash[] = {0xe4, 0xe2, 0x1, 0xa9, 0xc8, 0x87, 0x8d, 0x3d, 0xa3, 0xab, 0xc9, 0xcd, 0xf3, 0xf, 0x5a, 0x33};
68  set_hash(tmp_hash);
69 }
70 
71 /** Destructor */
72 TestInterface::~TestInterface()
73 {
74  free(data_ptr);
75 }
76 /** Convert TestEnum constant to string.
77  * @param value value to convert to string
78  * @return constant value as string.
79  */
80 const char *
82 {
83  switch (value) {
84  case TEST_ENUM_1: return "TEST_ENUM_1";
85  case TEST_ENUM_2: return "TEST_ENUM_2";
86  default: return "UNKNOWN";
87  }
88 }
89 /* Methods */
90 /** Get test_bool value.
91  * Test Bool
92  * @return test_bool value
93  */
94 bool
96 {
97  return data->test_bool;
98 }
99 
100 /** Get maximum length of test_bool value.
101  * @return length of test_bool value, can be length of the array or number of
102  * maximum number of characters for a string
103  */
104 size_t
106 {
107  return 1;
108 }
109 
110 /** Set test_bool value.
111  * Test Bool
112  * @param new_test_bool new test_bool value
113  */
114 void
115 TestInterface::set_test_bool(const bool new_test_bool)
116 {
117  data->test_bool = new_test_bool;
118  data_changed = true;
119 }
120 
121 /** Get test_int value.
122  * Test integer
123  * @return test_int value
124  */
125 int32_t
127 {
128  return data->test_int;
129 }
130 
131 /** Get maximum length of test_int value.
132  * @return length of test_int value, can be length of the array or number of
133  * maximum number of characters for a string
134  */
135 size_t
137 {
138  return 1;
139 }
140 
141 /** Set test_int value.
142  * Test integer
143  * @param new_test_int new test_int value
144  */
145 void
146 TestInterface::set_test_int(const int32_t new_test_int)
147 {
148  data->test_int = new_test_int;
149  data_changed = true;
150 }
151 
152 /** Get flags value.
153  * Flags spit down by the writer
154  * @return flags value
155  */
156 uint8_t
158 {
159  return data->flags;
160 }
161 
162 /** Get maximum length of flags value.
163  * @return length of flags 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 1;
170 }
171 
172 /** Set flags value.
173  * Flags spit down by the writer
174  * @param new_flags new flags value
175  */
176 void
177 TestInterface::set_flags(const uint8_t new_flags)
178 {
179  data->flags = new_flags;
180  data_changed = true;
181 }
182 
183 /** Get test_string value.
184  * A test sring
185  * @return test_string value
186  */
187 char *
189 {
190  return data->test_string;
191 }
192 
193 /** Get maximum length of test_string value.
194  * @return length of test_string value, can be length of the array or number of
195  * maximum number of characters for a string
196  */
197 size_t
199 {
200  return 30;
201 }
202 
203 /** Set test_string value.
204  * A test sring
205  * @param new_test_string new test_string value
206  */
207 void
208 TestInterface::set_test_string(const char * new_test_string)
209 {
210  strncpy(data->test_string, new_test_string, sizeof(data->test_string));
211  data_changed = true;
212 }
213 
214 /** Get result value.
215  * Result of operation add operation from Calculate message.
216  * @return result value
217  */
218 int32_t
220 {
221  return data->result;
222 }
223 
224 /** Get maximum length of result value.
225  * @return length of result value, can be length of the array or number of
226  * maximum number of characters for a string
227  */
228 size_t
230 {
231  return 1;
232 }
233 
234 /** Set result value.
235  * Result of operation add operation from Calculate message.
236  * @param new_result new result value
237  */
238 void
239 TestInterface::set_result(const int32_t new_result)
240 {
241  data->result = new_result;
242  data_changed = true;
243 }
244 
245 /** Get test_uint value.
246  * Test uint32
247  * @return test_uint value
248  */
249 uint32_t
251 {
252  return data->test_uint;
253 }
254 
255 /** Get maximum length of test_uint value.
256  * @return length of test_uint value, can be length of the array or number of
257  * maximum number of characters for a string
258  */
259 size_t
261 {
262  return 1;
263 }
264 
265 /** Set test_uint value.
266  * Test uint32
267  * @param new_test_uint new test_uint value
268  */
269 void
270 TestInterface::set_test_uint(const uint32_t new_test_uint)
271 {
272  data->test_uint = new_test_uint;
273  data_changed = true;
274 }
275 
276 /* =========== message create =========== */
277 Message *
279 {
280  if ( strncmp("SetTestIntMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
281  return new SetTestIntMessage();
282  } else if ( strncmp("SetTestStringMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
283  return new SetTestStringMessage();
284  } else if ( strncmp("CalculateMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
285  return new CalculateMessage();
286  } else {
287  throw UnknownTypeException("The given type '%s' does not match any known "
288  "message type for this interface type.", type);
289  }
290 }
291 
292 
293 /** Copy values from other interface.
294  * @param other other interface to copy values from
295  */
296 void
298 {
299  const TestInterface *oi = dynamic_cast<const TestInterface *>(other);
300  if (oi == NULL) {
301  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
302  type(), other->type());
303  }
304  memcpy(data, oi->data, sizeof(TestInterface_data_t));
305 }
306 
307 const char *
308 TestInterface::enum_tostring(const char *enumtype, int val) const
309 {
310  if (strcmp(enumtype, "TestEnum") == 0) {
311  return tostring_TestEnum((TestEnum)val);
312  }
313  throw UnknownTypeException("Unknown enum type %s", enumtype);
314 }
315 
316 /* =========== messages =========== */
317 /** @class TestInterface::SetTestIntMessage <interfaces/TestInterface.h>
318  * SetTestIntMessage Fawkes BlackBoard Interface Message.
319  *
320 
321  */
322 
323 
324 /** Constructor with initial values.
325  * @param ini_test_int initial value for test_int
326  */
327 TestInterface::SetTestIntMessage::SetTestIntMessage(const int32_t ini_test_int) : Message("SetTestIntMessage")
328 {
329  data_size = sizeof(SetTestIntMessage_data_t);
330  data_ptr = malloc(data_size);
331  memset(data_ptr, 0, data_size);
332  data = (SetTestIntMessage_data_t *)data_ptr;
334  data->test_int = ini_test_int;
335  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
336  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
337  add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
338 }
339 /** Constructor */
341 {
342  data_size = sizeof(SetTestIntMessage_data_t);
343  data_ptr = malloc(data_size);
344  memset(data_ptr, 0, data_size);
345  data = (SetTestIntMessage_data_t *)data_ptr;
347  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
348  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
349  add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
350 }
351 
352 /** Destructor */
354 {
355  free(data_ptr);
356 }
357 
358 /** Copy constructor.
359  * @param m message to copy from
360  */
362 {
363  data_size = m->data_size;
364  data_ptr = malloc(data_size);
365  memcpy(data_ptr, m->data_ptr, data_size);
366  data = (SetTestIntMessage_data_t *)data_ptr;
368 }
369 
370 /* Methods */
371 /** Get test_int value.
372  * Test integer
373  * @return test_int value
374  */
375 int32_t
377 {
378  return data->test_int;
379 }
380 
381 /** Get maximum length of test_int value.
382  * @return length of test_int value, can be length of the array or number of
383  * maximum number of characters for a string
384  */
385 size_t
387 {
388  return 1;
389 }
390 
391 /** Set test_int value.
392  * Test integer
393  * @param new_test_int new test_int value
394  */
395 void
397 {
398  data->test_int = new_test_int;
399 }
400 
401 /** Clone this message.
402  * Produces a message of the same type as this message and copies the
403  * data to the new message.
404  * @return clone of this message
405  */
406 Message *
408 {
409  return new TestInterface::SetTestIntMessage(this);
410 }
411 /** @class TestInterface::SetTestStringMessage <interfaces/TestInterface.h>
412  * SetTestStringMessage Fawkes BlackBoard Interface Message.
413  *
414 
415  */
416 
417 
418 /** Constructor with initial values.
419  * @param ini_test_string initial value for test_string
420  */
421 TestInterface::SetTestStringMessage::SetTestStringMessage(const char * ini_test_string) : Message("SetTestStringMessage")
422 {
423  data_size = sizeof(SetTestStringMessage_data_t);
424  data_ptr = malloc(data_size);
425  memset(data_ptr, 0, data_size);
426  data = (SetTestStringMessage_data_t *)data_ptr;
428  strncpy(data->test_string, ini_test_string, 30);
429  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
430  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
431  add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
432 }
433 /** Constructor */
435 {
436  data_size = sizeof(SetTestStringMessage_data_t);
437  data_ptr = malloc(data_size);
438  memset(data_ptr, 0, data_size);
439  data = (SetTestStringMessage_data_t *)data_ptr;
441  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
442  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
443  add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
444 }
445 
446 /** Destructor */
448 {
449  free(data_ptr);
450 }
451 
452 /** Copy constructor.
453  * @param m message to copy from
454  */
456 {
457  data_size = m->data_size;
458  data_ptr = malloc(data_size);
459  memcpy(data_ptr, m->data_ptr, data_size);
460  data = (SetTestStringMessage_data_t *)data_ptr;
462 }
463 
464 /* Methods */
465 /** Get test_string value.
466  * A test sring
467  * @return test_string value
468  */
469 char *
471 {
472  return data->test_string;
473 }
474 
475 /** Get maximum length of test_string value.
476  * @return length of test_string value, can be length of the array or number of
477  * maximum number of characters for a string
478  */
479 size_t
481 {
482  return 30;
483 }
484 
485 /** Set test_string value.
486  * A test sring
487  * @param new_test_string new test_string value
488  */
489 void
491 {
492  strncpy(data->test_string, new_test_string, sizeof(data->test_string));
493 }
494 
495 /** Clone this message.
496  * Produces a message of the same type as this message and copies the
497  * data to the new message.
498  * @return clone of this message
499  */
500 Message *
502 {
503  return new TestInterface::SetTestStringMessage(this);
504 }
505 /** @class TestInterface::CalculateMessage <interfaces/TestInterface.h>
506  * CalculateMessage Fawkes BlackBoard Interface Message.
507  *
508 
509  */
510 
511 
512 /** Constructor with initial values.
513  * @param ini_summand initial value for summand
514  * @param ini_addend initial value for addend
515  */
516 TestInterface::CalculateMessage::CalculateMessage(const int32_t ini_summand, const int32_t ini_addend) : Message("CalculateMessage")
517 {
518  data_size = sizeof(CalculateMessage_data_t);
519  data_ptr = malloc(data_size);
520  memset(data_ptr, 0, data_size);
521  data = (CalculateMessage_data_t *)data_ptr;
523  data->summand = ini_summand;
524  data->addend = ini_addend;
525  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
526  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
527  add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
528  add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
529 }
530 /** Constructor */
532 {
533  data_size = sizeof(CalculateMessage_data_t);
534  data_ptr = malloc(data_size);
535  memset(data_ptr, 0, data_size);
536  data = (CalculateMessage_data_t *)data_ptr;
538  enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
539  enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
540  add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
541  add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
542 }
543 
544 /** Destructor */
546 {
547  free(data_ptr);
548 }
549 
550 /** Copy constructor.
551  * @param m message to copy from
552  */
554 {
555  data_size = m->data_size;
556  data_ptr = malloc(data_size);
557  memcpy(data_ptr, m->data_ptr, data_size);
558  data = (CalculateMessage_data_t *)data_ptr;
560 }
561 
562 /* Methods */
563 /** Get summand value.
564  * Summand
565  * @return summand value
566  */
567 int32_t
569 {
570  return data->summand;
571 }
572 
573 /** Get maximum length of summand value.
574  * @return length of summand value, can be length of the array or number of
575  * maximum number of characters for a string
576  */
577 size_t
579 {
580  return 1;
581 }
582 
583 /** Set summand value.
584  * Summand
585  * @param new_summand new summand value
586  */
587 void
589 {
590  data->summand = new_summand;
591 }
592 
593 /** Get addend value.
594  * Addend
595  * @return addend value
596  */
597 int32_t
599 {
600  return data->addend;
601 }
602 
603 /** Get maximum length of addend value.
604  * @return length of addend value, can be length of the array or number of
605  * maximum number of characters for a string
606  */
607 size_t
609 {
610  return 1;
611 }
612 
613 /** Set addend value.
614  * Addend
615  * @param new_addend new addend value
616  */
617 void
619 {
620  data->addend = new_addend;
621 }
622 
623 /** Clone this message.
624  * Produces a message of the same type as this message and copies the
625  * data to the new message.
626  * @return clone of this message
627  */
628 Message *
630 {
631  return new TestInterface::CalculateMessage(this);
632 }
633 /** Check if message is valid and can be enqueued.
634  * @param message Message to check
635  * @return true if the message is valid, false otherwise.
636  */
637 bool
639 {
640  const SetTestIntMessage *m0 = dynamic_cast<const SetTestIntMessage *>(message);
641  if ( m0 != NULL ) {
642  return true;
643  }
644  const SetTestStringMessage *m1 = dynamic_cast<const SetTestStringMessage *>(message);
645  if ( m1 != NULL ) {
646  return true;
647  }
648  const CalculateMessage *m2 = dynamic_cast<const CalculateMessage *>(message);
649  if ( m2 != NULL ) {
650  return true;
651  }
652  return false;
653 }
654 
655 /// @cond INTERNALS
656 EXPORT_INTERFACE(TestInterface)
657 /// @endcond
658 
659 
660 } // end namespace fawkes
void set_test_string(const char *new_test_string)
Set test_string value.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
size_t maxlenof_test_bool() const
Get maximum length of test_bool value.
TestEnum
Demonstrating enums.
Definition: TestInterface.h:44
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Fawkes library namespace.
size_t maxlenof_flags() const
Get maximum length of flags value.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
virtual Message * create_message(const char *type) const
Create message based on type name.
int32_t test_int() const
Get test_int value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
string field
Definition: types.h:47
byte field, alias for uint8
Definition: types.h:48
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
int32_t addend() const
Get addend value.
void set_summand(const int32_t new_summand)
Set summand value.
char * test_string() const
Get test_string value.
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
void set_test_int(const int32_t new_test_int)
Set test_int value.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:373
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
size_t maxlenof_summand() const
Get maximum length of summand value.
static const int32_t TEST_CONSTANT
TEST_CONSTANT constant.
Definition: TestInterface.h:40
const char * type() const
Get type of interface.
Definition: interface.cpp:651
virtual Message * clone() const
Clone this message.
virtual Message * clone() const
Clone this message.
virtual Message * clone() const
Clone this message.
char * test_string() const
Get test_string value.
void set_addend(const int32_t new_addend)
Set addend value.
size_t maxlenof_test_uint() const
Get maximum length of test_uint value.
size_t maxlenof_test_int() const
Get maximum length of test_int value.
uint32_t test_uint() const
Get test_uint value.
void set_test_bool(const bool new_test_bool)
Set test_bool value.
size_t maxlenof_addend() const
Get maximum length of addend value.
CalculateMessage Fawkes BlackBoard Interface Message.
int32_t result() const
Get result value.
void set_flags(const uint8_t new_flags)
Set flags value.
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:68
uint8_t flags() const
Get flags value.
void set_test_uint(const uint32_t new_test_uint)
Set test_uint value.
size_t maxlenof_result() const
Get maximum length of result value.
void set_test_string(const char *new_test_string)
Set test_string value.
size_t maxlenof_test_int() const
Get maximum length of test_int value.
32 bit integer field
Definition: types.h:41
int32_t summand() const
Get summand value.
bool is_test_bool() const
Get test_bool value.
int32_t test_int() const
Get test_int value.
const char * tostring_TestEnum(TestEnum value) const
Convert TestEnum constant to string.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
static const float TEST_FLOAT_CONSTANT
TEST_FLOAT_CONSTANT constant.
Definition: TestInterface.h:41
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:436
boolean field
Definition: types.h:36
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:94
void set_result(const int32_t new_result)
Set result value.
void set_test_int(const int32_t new_test_int)
Set test_int value.
const char * type() const
Get message type.
Definition: message.cpp:378
size_t maxlenof_test_string() const
Get maximum length of test_string value.
32 bit unsigned integer field
Definition: types.h:42
size_t maxlenof_test_string() const
Get maximum length of test_string value.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33