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
33namespace 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 */
44const int32_t TestInterface::TEST_CONSTANT = 5;
45/** TEST_FLOAT_CONSTANT constant */
47
48/** Constructor */
49TestInterface::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 */
72TestInterface::~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 */
80const char *
81TestInterface::tostring_TestEnum(TestEnum value) const
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 */
94bool
95TestInterface::is_test_bool() const
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 */
104size_t
105TestInterface::maxlenof_test_bool() const
106{
107 return 1;
108}
109
110/** Set test_bool value.
111 * Test Bool
112 * @param new_test_bool new test_bool value
113 */
114void
115TestInterface::set_test_bool(const bool new_test_bool)
116{
117 set_field(data->test_bool, new_test_bool);
118}
119
120/** Get test_int value.
121 * Test integer
122 * @return test_int value
123 */
124int32_t
125TestInterface::test_int() const
126{
127 return data->test_int;
128}
129
130/** Get maximum length of test_int value.
131 * @return length of test_int value, can be length of the array or number of
132 * maximum number of characters for a string
133 */
134size_t
135TestInterface::maxlenof_test_int() const
136{
137 return 1;
138}
139
140/** Set test_int value.
141 * Test integer
142 * @param new_test_int new test_int value
143 */
144void
145TestInterface::set_test_int(const int32_t new_test_int)
146{
147 set_field(data->test_int, new_test_int);
148}
149
150/** Get flags value.
151 * Flags spit down by the writer
152 * @return flags value
153 */
154uint8_t
155TestInterface::flags() const
156{
157 return data->flags;
158}
159
160/** Get maximum length of flags value.
161 * @return length of flags value, can be length of the array or number of
162 * maximum number of characters for a string
163 */
164size_t
165TestInterface::maxlenof_flags() const
166{
167 return 1;
168}
169
170/** Set flags value.
171 * Flags spit down by the writer
172 * @param new_flags new flags value
173 */
174void
175TestInterface::set_flags(const uint8_t new_flags)
176{
177 set_field(data->flags, new_flags);
178}
179
180/** Get test_string value.
181 * A test sring
182 * @return test_string value
183 */
184char *
185TestInterface::test_string() const
186{
187 return data->test_string;
188}
189
190/** Get maximum length of test_string value.
191 * @return length of test_string value, can be length of the array or number of
192 * maximum number of characters for a string
193 */
194size_t
195TestInterface::maxlenof_test_string() const
196{
197 return 30;
198}
199
200/** Set test_string value.
201 * A test sring
202 * @param new_test_string new test_string value
203 */
204void
205TestInterface::set_test_string(const char * new_test_string)
206{
207 set_field(data->test_string, new_test_string);
208}
209
210/** Get result value.
211 * Result of operation add operation from Calculate message.
212 * @return result value
213 */
214int32_t
215TestInterface::result() const
216{
217 return data->result;
218}
219
220/** Get maximum length of result value.
221 * @return length of result value, can be length of the array or number of
222 * maximum number of characters for a string
223 */
224size_t
225TestInterface::maxlenof_result() const
226{
227 return 1;
228}
229
230/** Set result value.
231 * Result of operation add operation from Calculate message.
232 * @param new_result new result value
233 */
234void
235TestInterface::set_result(const int32_t new_result)
236{
237 set_field(data->result, new_result);
238}
239
240/** Get test_uint value.
241 * Test uint32
242 * @return test_uint value
243 */
244uint32_t
245TestInterface::test_uint() const
246{
247 return data->test_uint;
248}
249
250/** Get maximum length of test_uint value.
251 * @return length of test_uint value, can be length of the array or number of
252 * maximum number of characters for a string
253 */
254size_t
255TestInterface::maxlenof_test_uint() const
256{
257 return 1;
258}
259
260/** Set test_uint value.
261 * Test uint32
262 * @param new_test_uint new test_uint value
263 */
264void
265TestInterface::set_test_uint(const uint32_t new_test_uint)
266{
267 set_field(data->test_uint, new_test_uint);
268}
269
270/* =========== message create =========== */
271Message *
272TestInterface::create_message(const char *type) const
273{
274 if ( strncmp("SetTestIntMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
275 return new SetTestIntMessage();
276 } else if ( strncmp("SetTestStringMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
277 return new SetTestStringMessage();
278 } else if ( strncmp("CalculateMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
279 return new CalculateMessage();
280 } else {
281 throw UnknownTypeException("The given type '%s' does not match any known "
282 "message type for this interface type.", type);
283 }
284}
285
286
287/** Copy values from other interface.
288 * @param other other interface to copy values from
289 */
290void
291TestInterface::copy_values(const Interface *other)
292{
293 const TestInterface *oi = dynamic_cast<const TestInterface *>(other);
294 if (oi == NULL) {
295 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
296 type(), other->type());
297 }
298 memcpy(data, oi->data, sizeof(TestInterface_data_t));
299}
300
301const char *
302TestInterface::enum_tostring(const char *enumtype, int val) const
303{
304 if (strcmp(enumtype, "TestEnum") == 0) {
305 return tostring_TestEnum((TestEnum)val);
306 }
307 throw UnknownTypeException("Unknown enum type %s", enumtype);
308}
309
310/* =========== messages =========== */
311/** @class TestInterface::SetTestIntMessage <interfaces/TestInterface.h>
312 * SetTestIntMessage Fawkes BlackBoard Interface Message.
313 *
314
315 */
316
317
318/** Constructor with initial values.
319 * @param ini_test_int initial value for test_int
320 */
321TestInterface::SetTestIntMessage::SetTestIntMessage(const int32_t ini_test_int) : Message("SetTestIntMessage")
322{
323 data_size = sizeof(SetTestIntMessage_data_t);
324 data_ptr = malloc(data_size);
325 memset(data_ptr, 0, data_size);
326 data = (SetTestIntMessage_data_t *)data_ptr;
328 data->test_int = ini_test_int;
329 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
330 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
331 add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
332}
333/** Constructor */
335{
336 data_size = sizeof(SetTestIntMessage_data_t);
337 data_ptr = malloc(data_size);
338 memset(data_ptr, 0, data_size);
339 data = (SetTestIntMessage_data_t *)data_ptr;
341 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
342 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
343 add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
344}
345
346/** Destructor */
348{
349 free(data_ptr);
350}
351
352/** Copy constructor.
353 * @param m message to copy from
354 */
356{
357 data_size = m->data_size;
358 data_ptr = malloc(data_size);
359 memcpy(data_ptr, m->data_ptr, data_size);
360 data = (SetTestIntMessage_data_t *)data_ptr;
362}
363
364/* Methods */
365/** Get test_int value.
366 * Test integer
367 * @return test_int value
368 */
369int32_t
371{
372 return data->test_int;
373}
374
375/** Get maximum length of test_int value.
376 * @return length of test_int value, can be length of the array or number of
377 * maximum number of characters for a string
378 */
379size_t
381{
382 return 1;
383}
384
385/** Set test_int value.
386 * Test integer
387 * @param new_test_int new test_int value
388 */
389void
391{
392 set_field(data->test_int, new_test_int);
393}
394
395/** Clone this message.
396 * Produces a message of the same type as this message and copies the
397 * data to the new message.
398 * @return clone of this message
399 */
400Message *
402{
403 return new TestInterface::SetTestIntMessage(this);
404}
405/** @class TestInterface::SetTestStringMessage <interfaces/TestInterface.h>
406 * SetTestStringMessage Fawkes BlackBoard Interface Message.
407 *
408
409 */
410
411
412/** Constructor with initial values.
413 * @param ini_test_string initial value for test_string
414 */
415TestInterface::SetTestStringMessage::SetTestStringMessage(const char * ini_test_string) : Message("SetTestStringMessage")
416{
417 data_size = sizeof(SetTestStringMessage_data_t);
418 data_ptr = malloc(data_size);
419 memset(data_ptr, 0, data_size);
420 data = (SetTestStringMessage_data_t *)data_ptr;
422 strncpy(data->test_string, ini_test_string, 30-1);
423 data->test_string[30-1] = 0;
424 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
425 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
426 add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
427}
428/** Constructor */
430{
431 data_size = sizeof(SetTestStringMessage_data_t);
432 data_ptr = malloc(data_size);
433 memset(data_ptr, 0, data_size);
434 data = (SetTestStringMessage_data_t *)data_ptr;
436 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
437 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
438 add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
439}
440
441/** Destructor */
443{
444 free(data_ptr);
445}
446
447/** Copy constructor.
448 * @param m message to copy from
449 */
451{
452 data_size = m->data_size;
453 data_ptr = malloc(data_size);
454 memcpy(data_ptr, m->data_ptr, data_size);
455 data = (SetTestStringMessage_data_t *)data_ptr;
457}
458
459/* Methods */
460/** Get test_string value.
461 * A test sring
462 * @return test_string value
463 */
464char *
466{
467 return data->test_string;
468}
469
470/** Get maximum length of test_string value.
471 * @return length of test_string value, can be length of the array or number of
472 * maximum number of characters for a string
473 */
474size_t
476{
477 return 30;
478}
479
480/** Set test_string value.
481 * A test sring
482 * @param new_test_string new test_string value
483 */
484void
486{
487 set_field(data->test_string, new_test_string);
488}
489
490/** Clone this message.
491 * Produces a message of the same type as this message and copies the
492 * data to the new message.
493 * @return clone of this message
494 */
495Message *
497{
499}
500/** @class TestInterface::CalculateMessage <interfaces/TestInterface.h>
501 * CalculateMessage Fawkes BlackBoard Interface Message.
502 *
503
504 */
505
506
507/** Constructor with initial values.
508 * @param ini_summand initial value for summand
509 * @param ini_addend initial value for addend
510 */
511TestInterface::CalculateMessage::CalculateMessage(const int32_t ini_summand, const int32_t ini_addend) : Message("CalculateMessage")
512{
513 data_size = sizeof(CalculateMessage_data_t);
514 data_ptr = malloc(data_size);
515 memset(data_ptr, 0, data_size);
516 data = (CalculateMessage_data_t *)data_ptr;
518 data->summand = ini_summand;
519 data->addend = ini_addend;
520 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
521 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
522 add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
523 add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
524}
525/** Constructor */
527{
528 data_size = sizeof(CalculateMessage_data_t);
529 data_ptr = malloc(data_size);
530 memset(data_ptr, 0, data_size);
531 data = (CalculateMessage_data_t *)data_ptr;
533 enum_map_TestEnum[(int)TEST_ENUM_1] = "TEST_ENUM_1";
534 enum_map_TestEnum[(int)TEST_ENUM_2] = "TEST_ENUM_2";
535 add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
536 add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
537}
538
539/** Destructor */
541{
542 free(data_ptr);
543}
544
545/** Copy constructor.
546 * @param m message to copy from
547 */
549{
550 data_size = m->data_size;
551 data_ptr = malloc(data_size);
552 memcpy(data_ptr, m->data_ptr, data_size);
553 data = (CalculateMessage_data_t *)data_ptr;
555}
556
557/* Methods */
558/** Get summand value.
559 * Summand
560 * @return summand value
561 */
562int32_t
564{
565 return data->summand;
566}
567
568/** Get maximum length of summand value.
569 * @return length of summand value, can be length of the array or number of
570 * maximum number of characters for a string
571 */
572size_t
574{
575 return 1;
576}
577
578/** Set summand value.
579 * Summand
580 * @param new_summand new summand value
581 */
582void
584{
585 set_field(data->summand, new_summand);
586}
587
588/** Get addend value.
589 * Addend
590 * @return addend value
591 */
592int32_t
594{
595 return data->addend;
596}
597
598/** Get maximum length of addend value.
599 * @return length of addend value, can be length of the array or number of
600 * maximum number of characters for a string
601 */
602size_t
604{
605 return 1;
606}
607
608/** Set addend value.
609 * Addend
610 * @param new_addend new addend value
611 */
612void
614{
615 set_field(data->addend, new_addend);
616}
617
618/** Clone this message.
619 * Produces a message of the same type as this message and copies the
620 * data to the new message.
621 * @return clone of this message
622 */
623Message *
625{
626 return new TestInterface::CalculateMessage(this);
627}
628/** Check if message is valid and can be enqueued.
629 * @param message Message to check
630 * @return true if the message is valid, false otherwise.
631 */
632bool
634{
635 const SetTestIntMessage *m0 = dynamic_cast<const SetTestIntMessage *>(message);
636 if ( m0 != NULL ) {
637 return true;
638 }
639 const SetTestStringMessage *m1 = dynamic_cast<const SetTestStringMessage *>(message);
640 if ( m1 != NULL ) {
641 return true;
642 }
643 const CalculateMessage *m2 = dynamic_cast<const CalculateMessage *>(message);
644 if ( m2 != NULL ) {
645 return true;
646 }
647 return false;
648}
649
650/// @cond INTERNALS
651EXPORT_INTERFACE(TestInterface)
652/// @endcond
653
654
655} // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
void set_field(FieldT &field, DataT &data)
Set a field, set data_changed to true and update data_changed accordingly.
Definition: interface.h:304
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
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:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
CalculateMessage Fawkes BlackBoard Interface Message.
int32_t summand() const
Get summand value.
void set_addend(const int32_t new_addend)
Set addend value.
size_t maxlenof_addend() const
Get maximum length of addend value.
void set_summand(const int32_t new_summand)
Set summand value.
size_t maxlenof_summand() const
Get maximum length of summand value.
int32_t addend() const
Get addend value.
virtual Message * clone() const
Clone this message.
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:69
size_t maxlenof_test_int() const
Get maximum length of test_int value.
virtual Message * clone() const
Clone this message.
int32_t test_int() const
Get test_int value.
void set_test_int(const int32_t new_test_int)
Set test_int value.
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:95
void set_test_string(const char *new_test_string)
Set test_string value.
virtual Message * clone() const
Clone this message.
char * test_string() const
Get test_string value.
size_t maxlenof_test_string() const
Get maximum length of test_string value.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
static const float TEST_FLOAT_CONSTANT
TEST_FLOAT_CONSTANT constant.
Definition: TestInterface.h:41
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
static const int32_t TEST_CONSTANT
TEST_CONSTANT constant.
Definition: TestInterface.h:40
TestEnum
Demonstrating enums.
Definition: TestInterface.h:44
Fawkes library namespace.
@ IFT_INT32
32 bit integer field
Definition: types.h:42
@ IFT_STRING
string field
Definition: types.h:48
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152