Fawkes API  Fawkes Development Version
SkillerInterface.cpp
1 
2 /***************************************************************************
3  * SkillerInterface.cpp - Fawkes BlackBoard Interface - SkillerInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 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/SkillerInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <cstring>
29 #include <cstdlib>
30 
31 namespace fawkes {
32 
33 /** @class SkillerInterface <interfaces/SkillerInterface.h>
34  * SkillerInterface Fawkes BlackBoard Interface.
35  *
36  The interface provides access to the skill execution runtime plugin.
37  It provides basic status information about skiller and allows for
38  calling skills via messages. It can also be used to manually restart
39  the Lua interpreter if something is wedged.
40 
41  * @ingroup FawkesInterfaces
42  */
43 
44 
45 
46 /** Constructor */
47 SkillerInterface::SkillerInterface() : Interface()
48 {
49  data_size = sizeof(SkillerInterface_data_t);
50  data_ptr = malloc(data_size);
51  data = (SkillerInterface_data_t *)data_ptr;
52  data_ts = (interface_data_ts_t *)data_ptr;
53  memset(data_ptr, 0, data_size);
54  add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string, "");
55  add_fieldinfo(IFT_STRING, "error", 128, data->error, "");
56  add_fieldinfo(IFT_UINT32, "exclusive_controller", 1, &data->exclusive_controller, "");
57  add_fieldinfo(IFT_ENUM, "status", 1, &data->status);
58  add_fieldinfo(IFT_BOOL, "continuous", 1, &data->continuous, "");
59  add_messageinfo("ExecSkillMessage");
60  add_messageinfo("ExecSkillContinuousMessage");
61  add_messageinfo("RestartInterpreterMessage");
62  add_messageinfo("StopExecMessage");
63  add_messageinfo("AcquireControlMessage");
64  add_messageinfo("ReleaseControlMessage");
65  unsigned char tmp_hash[] = {0x7c, 0x85, 0xf3, 0x24, 0xea, 0x55, 0x50, 0xa1, 0x6c, 0xdb, 0xdc, 0x4b, 0x40, 0xba, 0xa1, 0xda};
66  set_hash(tmp_hash);
67 }
68 
69 /** Destructor */
70 SkillerInterface::~SkillerInterface()
71 {
72  free(data_ptr);
73 }
74 /** Convert SkillStatusEnum 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 S_INACTIVE: return "S_INACTIVE";
83  case S_FINAL: return "S_FINAL";
84  case S_RUNNING: return "S_RUNNING";
85  case S_FAILED: return "S_FAILED";
86  default: return "UNKNOWN";
87  }
88 }
89 /* Methods */
90 /** Get skill_string value.
91  *
92  Currently executed skill string, at least the first 1023 bytes of it.
93  Must be properly null-terminated.
94 
95  * @return skill_string value
96  */
97 char *
99 {
100  return data->skill_string;
101 }
102 
103 /** Get maximum length of skill_string value.
104  * @return length of skill_string value, can be length of the array or number of
105  * maximum number of characters for a string
106  */
107 size_t
109 {
110  return 1024;
111 }
112 
113 /** Set skill_string value.
114  *
115  Currently executed skill string, at least the first 1023 bytes of it.
116  Must be properly null-terminated.
117 
118  * @param new_skill_string new skill_string value
119  */
120 void
121 SkillerInterface::set_skill_string(const char * new_skill_string)
122 {
123  strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
124  data_changed = true;
125 }
126 
127 /** Get error value.
128  *
129  String describing the error. Can be set by a skill when it fails.
130 
131  * @return error value
132  */
133 char *
135 {
136  return data->error;
137 }
138 
139 /** Get maximum length of error value.
140  * @return length of error value, can be length of the array or number of
141  * maximum number of characters for a string
142  */
143 size_t
145 {
146  return 128;
147 }
148 
149 /** Set error value.
150  *
151  String describing the error. Can be set by a skill when it fails.
152 
153  * @param new_error new error value
154  */
155 void
156 SkillerInterface::set_error(const char * new_error)
157 {
158  strncpy(data->error, new_error, sizeof(data->error));
159  data_changed = true;
160 }
161 
162 /** Get exclusive_controller value.
163  *
164  Instance serial of the exclusive controller of the skiller. If this does not
165  carry your instance serial your exec messages will be ignored. Aquire control with
166  the AquireControlMessage. Make sure you release control before exiting.
167 
168  * @return exclusive_controller value
169  */
170 uint32_t
172 {
173  return data->exclusive_controller;
174 }
175 
176 /** Get maximum length of exclusive_controller value.
177  * @return length of exclusive_controller value, can be length of the array or number of
178  * maximum number of characters for a string
179  */
180 size_t
182 {
183  return 1;
184 }
185 
186 /** Set exclusive_controller value.
187  *
188  Instance serial of the exclusive controller of the skiller. If this does not
189  carry your instance serial your exec messages will be ignored. Aquire control with
190  the AquireControlMessage. Make sure you release control before exiting.
191 
192  * @param new_exclusive_controller new exclusive_controller value
193  */
194 void
195 SkillerInterface::set_exclusive_controller(const uint32_t new_exclusive_controller)
196 {
197  data->exclusive_controller = new_exclusive_controller;
198  data_changed = true;
199 }
200 
201 /** Get status value.
202  *
203  The status of the current skill execution.
204 
205  * @return status value
206  */
209 {
210  return (SkillerInterface::SkillStatusEnum)data->status;
211 }
212 
213 /** Get maximum length of status value.
214  * @return length of status value, can be length of the array or number of
215  * maximum number of characters for a string
216  */
217 size_t
219 {
220  return 1;
221 }
222 
223 /** Set status value.
224  *
225  The status of the current skill execution.
226 
227  * @param new_status new status value
228  */
229 void
231 {
232  data->status = new_status;
233  data_changed = true;
234 }
235 
236 /** Get continuous value.
237  *
238  True if continuous execution is in progress, false if no skill string is executed
239  at all or it is executed one-shot with ExecSkillMessage.
240 
241  * @return continuous value
242  */
243 bool
245 {
246  return data->continuous;
247 }
248 
249 /** Get maximum length of continuous value.
250  * @return length of continuous value, can be length of the array or number of
251  * maximum number of characters for a string
252  */
253 size_t
255 {
256  return 1;
257 }
258 
259 /** Set continuous value.
260  *
261  True if continuous execution is in progress, false if no skill string is executed
262  at all or it is executed one-shot with ExecSkillMessage.
263 
264  * @param new_continuous new continuous value
265  */
266 void
267 SkillerInterface::set_continuous(const bool new_continuous)
268 {
269  data->continuous = new_continuous;
270  data_changed = true;
271 }
272 
273 /* =========== message create =========== */
274 Message *
276 {
277  if ( strncmp("ExecSkillMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
278  return new ExecSkillMessage();
279  } else if ( strncmp("ExecSkillContinuousMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
280  return new ExecSkillContinuousMessage();
281  } else if ( strncmp("RestartInterpreterMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
282  return new RestartInterpreterMessage();
283  } else if ( strncmp("StopExecMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
284  return new StopExecMessage();
285  } else if ( strncmp("AcquireControlMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
286  return new AcquireControlMessage();
287  } else if ( strncmp("ReleaseControlMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
288  return new ReleaseControlMessage();
289  } else {
290  throw UnknownTypeException("The given type '%s' does not match any known "
291  "message type for this interface type.", type);
292  }
293 }
294 
295 
296 /** Copy values from other interface.
297  * @param other other interface to copy values from
298  */
299 void
301 {
302  const SkillerInterface *oi = dynamic_cast<const SkillerInterface *>(other);
303  if (oi == NULL) {
304  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
305  type(), other->type());
306  }
307  memcpy(data, oi->data, sizeof(SkillerInterface_data_t));
308 }
309 
310 const char *
311 SkillerInterface::enum_tostring(const char *enumtype, int val) const
312 {
313  if (strcmp(enumtype, "SkillStatusEnum") == 0) {
314  return tostring_SkillStatusEnum((SkillStatusEnum)val);
315  }
316  throw UnknownTypeException("Unknown enum type %s", enumtype);
317 }
318 
319 /* =========== messages =========== */
320 /** @class SkillerInterface::ExecSkillMessage <interfaces/SkillerInterface.h>
321  * ExecSkillMessage Fawkes BlackBoard Interface Message.
322  *
323 
324  */
325 
326 
327 /** Constructor with initial values.
328  * @param ini_skill_string initial value for skill_string
329  */
330 SkillerInterface::ExecSkillMessage::ExecSkillMessage(const char * ini_skill_string) : Message("ExecSkillMessage")
331 {
332  data_size = sizeof(ExecSkillMessage_data_t);
333  data_ptr = malloc(data_size);
334  memset(data_ptr, 0, data_size);
335  data = (ExecSkillMessage_data_t *)data_ptr;
337  strncpy(data->skill_string, ini_skill_string, 1024);
338  add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string, "");
339 }
340 /** Constructor */
342 {
343  data_size = sizeof(ExecSkillMessage_data_t);
344  data_ptr = malloc(data_size);
345  memset(data_ptr, 0, data_size);
346  data = (ExecSkillMessage_data_t *)data_ptr;
348  add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string, "");
349 }
350 
351 /** Destructor */
353 {
354  free(data_ptr);
355 }
356 
357 /** Copy constructor.
358  * @param m message to copy from
359  */
361 {
362  data_size = m->data_size;
363  data_ptr = malloc(data_size);
364  memcpy(data_ptr, m->data_ptr, data_size);
365  data = (ExecSkillMessage_data_t *)data_ptr;
367 }
368 
369 /* Methods */
370 /** Get skill_string value.
371  *
372  Currently executed skill string, at least the first 1023 bytes of it.
373  Must be properly null-terminated.
374 
375  * @return skill_string value
376  */
377 char *
379 {
380  return data->skill_string;
381 }
382 
383 /** Get maximum length of skill_string value.
384  * @return length of skill_string value, can be length of the array or number of
385  * maximum number of characters for a string
386  */
387 size_t
389 {
390  return 1024;
391 }
392 
393 /** Set skill_string value.
394  *
395  Currently executed skill string, at least the first 1023 bytes of it.
396  Must be properly null-terminated.
397 
398  * @param new_skill_string new skill_string value
399  */
400 void
402 {
403  strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
404 }
405 
406 /** Clone this message.
407  * Produces a message of the same type as this message and copies the
408  * data to the new message.
409  * @return clone of this message
410  */
411 Message *
413 {
414  return new SkillerInterface::ExecSkillMessage(this);
415 }
416 /** @class SkillerInterface::ExecSkillContinuousMessage <interfaces/SkillerInterface.h>
417  * ExecSkillContinuousMessage Fawkes BlackBoard Interface Message.
418  *
419 
420  */
421 
422 
423 /** Constructor with initial values.
424  * @param ini_skill_string initial value for skill_string
425  */
426 SkillerInterface::ExecSkillContinuousMessage::ExecSkillContinuousMessage(const char * ini_skill_string) : Message("ExecSkillContinuousMessage")
427 {
428  data_size = sizeof(ExecSkillContinuousMessage_data_t);
429  data_ptr = malloc(data_size);
430  memset(data_ptr, 0, data_size);
431  data = (ExecSkillContinuousMessage_data_t *)data_ptr;
433  strncpy(data->skill_string, ini_skill_string, 1024);
434  add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string, "");
435 }
436 /** Constructor */
438 {
439  data_size = sizeof(ExecSkillContinuousMessage_data_t);
440  data_ptr = malloc(data_size);
441  memset(data_ptr, 0, data_size);
442  data = (ExecSkillContinuousMessage_data_t *)data_ptr;
444  add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string, "");
445 }
446 
447 /** Destructor */
449 {
450  free(data_ptr);
451 }
452 
453 /** Copy constructor.
454  * @param m message to copy from
455  */
457 {
458  data_size = m->data_size;
459  data_ptr = malloc(data_size);
460  memcpy(data_ptr, m->data_ptr, data_size);
461  data = (ExecSkillContinuousMessage_data_t *)data_ptr;
463 }
464 
465 /* Methods */
466 /** Get skill_string value.
467  *
468  Currently executed skill string, at least the first 1023 bytes of it.
469  Must be properly null-terminated.
470 
471  * @return skill_string value
472  */
473 char *
475 {
476  return data->skill_string;
477 }
478 
479 /** Get maximum length of skill_string value.
480  * @return length of skill_string value, can be length of the array or number of
481  * maximum number of characters for a string
482  */
483 size_t
485 {
486  return 1024;
487 }
488 
489 /** Set skill_string value.
490  *
491  Currently executed skill string, at least the first 1023 bytes of it.
492  Must be properly null-terminated.
493 
494  * @param new_skill_string new skill_string value
495  */
496 void
498 {
499  strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
500 }
501 
502 /** Clone this message.
503  * Produces a message of the same type as this message and copies the
504  * data to the new message.
505  * @return clone of this message
506  */
507 Message *
509 {
511 }
512 /** @class SkillerInterface::RestartInterpreterMessage <interfaces/SkillerInterface.h>
513  * RestartInterpreterMessage Fawkes BlackBoard Interface Message.
514  *
515 
516  */
517 
518 
519 /** Constructor */
521 {
522  data_size = sizeof(RestartInterpreterMessage_data_t);
523  data_ptr = malloc(data_size);
524  memset(data_ptr, 0, data_size);
525  data = (RestartInterpreterMessage_data_t *)data_ptr;
527 }
528 
529 /** Destructor */
531 {
532  free(data_ptr);
533 }
534 
535 /** Copy constructor.
536  * @param m message to copy from
537  */
539 {
540  data_size = m->data_size;
541  data_ptr = malloc(data_size);
542  memcpy(data_ptr, m->data_ptr, data_size);
543  data = (RestartInterpreterMessage_data_t *)data_ptr;
545 }
546 
547 /* Methods */
548 /** Clone this message.
549  * Produces a message of the same type as this message and copies the
550  * data to the new message.
551  * @return clone of this message
552  */
553 Message *
555 {
557 }
558 /** @class SkillerInterface::StopExecMessage <interfaces/SkillerInterface.h>
559  * StopExecMessage Fawkes BlackBoard Interface Message.
560  *
561 
562  */
563 
564 
565 /** Constructor */
567 {
568  data_size = sizeof(StopExecMessage_data_t);
569  data_ptr = malloc(data_size);
570  memset(data_ptr, 0, data_size);
571  data = (StopExecMessage_data_t *)data_ptr;
573 }
574 
575 /** Destructor */
577 {
578  free(data_ptr);
579 }
580 
581 /** Copy constructor.
582  * @param m message to copy from
583  */
585 {
586  data_size = m->data_size;
587  data_ptr = malloc(data_size);
588  memcpy(data_ptr, m->data_ptr, data_size);
589  data = (StopExecMessage_data_t *)data_ptr;
591 }
592 
593 /* Methods */
594 /** Clone this message.
595  * Produces a message of the same type as this message and copies the
596  * data to the new message.
597  * @return clone of this message
598  */
599 Message *
601 {
602  return new SkillerInterface::StopExecMessage(this);
603 }
604 /** @class SkillerInterface::AcquireControlMessage <interfaces/SkillerInterface.h>
605  * AcquireControlMessage Fawkes BlackBoard Interface Message.
606  *
607 
608  */
609 
610 
611 /** Constructor */
613 {
614  data_size = sizeof(AcquireControlMessage_data_t);
615  data_ptr = malloc(data_size);
616  memset(data_ptr, 0, data_size);
617  data = (AcquireControlMessage_data_t *)data_ptr;
619 }
620 
621 /** Destructor */
623 {
624  free(data_ptr);
625 }
626 
627 /** Copy constructor.
628  * @param m message to copy from
629  */
631 {
632  data_size = m->data_size;
633  data_ptr = malloc(data_size);
634  memcpy(data_ptr, m->data_ptr, data_size);
635  data = (AcquireControlMessage_data_t *)data_ptr;
637 }
638 
639 /* Methods */
640 /** Clone this message.
641  * Produces a message of the same type as this message and copies the
642  * data to the new message.
643  * @return clone of this message
644  */
645 Message *
647 {
649 }
650 /** @class SkillerInterface::ReleaseControlMessage <interfaces/SkillerInterface.h>
651  * ReleaseControlMessage Fawkes BlackBoard Interface Message.
652  *
653 
654  */
655 
656 
657 /** Constructor */
659 {
660  data_size = sizeof(ReleaseControlMessage_data_t);
661  data_ptr = malloc(data_size);
662  memset(data_ptr, 0, data_size);
663  data = (ReleaseControlMessage_data_t *)data_ptr;
665 }
666 
667 /** Destructor */
669 {
670  free(data_ptr);
671 }
672 
673 /** Copy constructor.
674  * @param m message to copy from
675  */
677 {
678  data_size = m->data_size;
679  data_ptr = malloc(data_size);
680  memcpy(data_ptr, m->data_ptr, data_size);
681  data = (ReleaseControlMessage_data_t *)data_ptr;
683 }
684 
685 /* Methods */
686 /** Clone this message.
687  * Produces a message of the same type as this message and copies the
688  * data to the new message.
689  * @return clone of this message
690  */
691 Message *
693 {
695 }
696 /** Check if message is valid and can be enqueued.
697  * @param message Message to check
698  * @return true if the message is valid, false otherwise.
699  */
700 bool
702 {
703  const ExecSkillMessage *m0 = dynamic_cast<const ExecSkillMessage *>(message);
704  if ( m0 != NULL ) {
705  return true;
706  }
707  const ExecSkillContinuousMessage *m1 = dynamic_cast<const ExecSkillContinuousMessage *>(message);
708  if ( m1 != NULL ) {
709  return true;
710  }
711  const RestartInterpreterMessage *m2 = dynamic_cast<const RestartInterpreterMessage *>(message);
712  if ( m2 != NULL ) {
713  return true;
714  }
715  const StopExecMessage *m3 = dynamic_cast<const StopExecMessage *>(message);
716  if ( m3 != NULL ) {
717  return true;
718  }
719  const AcquireControlMessage *m4 = dynamic_cast<const AcquireControlMessage *>(message);
720  if ( m4 != NULL ) {
721  return true;
722  }
723  const ReleaseControlMessage *m5 = dynamic_cast<const ReleaseControlMessage *>(message);
724  if ( m5 != NULL ) {
725  return true;
726  }
727  return false;
728 }
729 
730 /// @cond INTERNALS
731 EXPORT_INTERFACE(SkillerInterface)
732 /// @endcond
733 
734 
735 } // end namespace fawkes
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:114
virtual Message * clone() const
Clone this message.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:43
uint32_t exclusive_controller() const
Get exclusive_controller value.
SkillStatusEnum status() const
Get status value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:312
const char * type() const
Get message type.
Definition: message.cpp:378
void set_exclusive_controller(const uint32_t new_exclusive_controller)
Set exclusive_controller value.
Fawkes library namespace.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:119
void set_skill_string(const char *new_skill_string)
Set skill_string value.
StopExecMessage Fawkes BlackBoard Interface Message.
virtual void copy_values(const Interface *other)
Copy values from other interface.
string field
Definition: types.h:45
ReleaseControlMessage Fawkes BlackBoard Interface Message.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
SkillStatusEnum
This determines the current status of skill execution.
size_t maxlenof_status() const
Get maximum length of status value.
size_t maxlenof_skill_string() const
Get maximum length of skill_string value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
size_t maxlenof_exclusive_controller() const
Get maximum length of exclusive_controller value.
const char * tostring_SkillStatusEnum(SkillStatusEnum value) const
Convert SkillStatusEnum constant to string.
size_t maxlenof_skill_string() const
Get maximum length of skill_string value.
AcquireControlMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_error() const
Get maximum length of error value.
size_t maxlenof_skill_string() const
Get maximum length of skill_string value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:123
virtual Message * clone() const
Clone this message.
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:115
bool is_continuous() const
Get continuous value.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:368
bool data_changed
Indicator if data has changed.
Definition: interface.h:208
void set_error(const char *new_error)
Set error value.
virtual Message * clone() const
Clone this message.
virtual Message * create_message(const char *type) const
Create message based on type name.
size_t maxlenof_continuous() const
Get maximum length of continuous value.
virtual Message * clone() const
Clone this message.
ExecSkillMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
RestartInterpreterMessage Fawkes BlackBoard Interface Message.
char * skill_string() const
Get skill_string value.
char * error() const
Get error value.
void set_continuous(const bool new_continuous)
Set continuous value.
virtual Message * clone() const
Clone this message.
void set_skill_string(const char *new_skill_string)
Set skill_string value.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0)
Add an entry to the info list.
Definition: message.cpp:435
char * skill_string() const
Get skill_string value.
SkillerInterface Fawkes BlackBoard Interface.
void set_status(const SkillStatusEnum new_status)
Set status value.
boolean field
Definition: types.h:34
ExecSkillContinuousMessage Fawkes BlackBoard Interface Message.
const char * type() const
Get type of interface.
Definition: interface.cpp:635
char * skill_string() const
Get skill_string value.
32 bit unsigned integer field
Definition: types.h:40
field with interface specific enum type
Definition: types.h:47
void set_skill_string(const char *new_skill_string)
Set skill_string value.