SkillerInterface.cpp

00001
00002 /***************************************************************************
00003  *  SkillerInterface.cpp - Fawkes BlackBoard Interface - SkillerInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <interfaces/SkillerInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class SkillerInterface <interfaces/SkillerInterface.h>
00034  * SkillerInterface Fawkes BlackBoard Interface.
00035  * 
00036       The interface provides access to the skill execution runtime plugin.
00037       It provides basic status information about skiller and allows for
00038       calling skills via messages. It can also be used to manually restart
00039       the Lua interpreter if something is wedged.
00040     
00041  * @ingroup FawkesInterfaces
00042  */
00043
00044
00045 
00046 /** Constructor */
00047 SkillerInterface::SkillerInterface() : Interface()
00048 {
00049   data_size = sizeof(SkillerInterface_data_t);
00050   data_ptr  = malloc(data_size);
00051   data      = (SkillerInterface_data_t *)data_ptr;
00052   memset(data_ptr, 0, data_size);
00053   add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string);
00054   add_fieldinfo(IFT_STRING, "error", 128, data->error);
00055   add_fieldinfo(IFT_UINT, "exclusive_controller", 1, &data->exclusive_controller);
00056   add_fieldinfo(IFT_BOOL, "continuous", 1, &data->continuous);
00057   add_messageinfo("ExecSkillMessage");
00058   add_messageinfo("ExecSkillContinuousMessage");
00059   add_messageinfo("RestartInterpreterMessage");
00060   add_messageinfo("StopExecMessage");
00061   add_messageinfo("AcquireControlMessage");
00062   add_messageinfo("ReleaseControlMessage");
00063   unsigned char tmp_hash[] = {0x61, 0x7c, 0x70, 0xc4, 0x68, 0x8b, 0x8b, 0x69, 0x87, 0xc1, 0xd1, 0xe6, 0xed, 0x34, 0xb5, 0x5};
00064   set_hash(tmp_hash);
00065 }
00066 
00067 /** Destructor */
00068 SkillerInterface::~SkillerInterface()
00069 {
00070   free(data_ptr);
00071 }
00072 /* Methods */
00073 /** Get skill_string value.
00074  * 
00075       Currently executed skill string, at least the first 1023 bytes of it.
00076       Must be properly null-terminated.
00077     
00078  * @return skill_string value
00079  */
00080 char *
00081 SkillerInterface::skill_string() const
00082 {
00083   return data->skill_string;
00084 }
00085 
00086 /** Get maximum length of skill_string value.
00087  * @return length of skill_string value, can be length of the array or number of 
00088  * maximum number of characters for a string
00089  */
00090 size_t
00091 SkillerInterface::maxlenof_skill_string() const
00092 {
00093   return 1024;
00094 }
00095 
00096 /** Set skill_string value.
00097  * 
00098       Currently executed skill string, at least the first 1023 bytes of it.
00099       Must be properly null-terminated.
00100     
00101  * @param new_skill_string new skill_string value
00102  */
00103 void
00104 SkillerInterface::set_skill_string(const char * new_skill_string)
00105 {
00106   strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
00107 }
00108 
00109 /** Get error value.
00110  * 
00111       String describing the error. Can be set by a skill when it fails.
00112     
00113  * @return error value
00114  */
00115 char *
00116 SkillerInterface::error() const
00117 {
00118   return data->error;
00119 }
00120 
00121 /** Get maximum length of error value.
00122  * @return length of error value, can be length of the array or number of 
00123  * maximum number of characters for a string
00124  */
00125 size_t
00126 SkillerInterface::maxlenof_error() const
00127 {
00128   return 128;
00129 }
00130 
00131 /** Set error value.
00132  * 
00133       String describing the error. Can be set by a skill when it fails.
00134     
00135  * @param new_error new error value
00136  */
00137 void
00138 SkillerInterface::set_error(const char * new_error)
00139 {
00140   strncpy(data->error, new_error, sizeof(data->error));
00141 }
00142 
00143 /** Get exclusive_controller value.
00144  * 
00145       Instance serial of the exclusive controller of the skiller. If this does not
00146       carry your instance serial your exec messages will be ignored. Aquire control with
00147       the AquireControlMessage. Make sure you release control before exiting.
00148     
00149  * @return exclusive_controller value
00150  */
00151 unsigned int
00152 SkillerInterface::exclusive_controller() const
00153 {
00154   return data->exclusive_controller;
00155 }
00156 
00157 /** Get maximum length of exclusive_controller value.
00158  * @return length of exclusive_controller value, can be length of the array or number of 
00159  * maximum number of characters for a string
00160  */
00161 size_t
00162 SkillerInterface::maxlenof_exclusive_controller() const
00163 {
00164   return 1;
00165 }
00166 
00167 /** Set exclusive_controller value.
00168  * 
00169       Instance serial of the exclusive controller of the skiller. If this does not
00170       carry your instance serial your exec messages will be ignored. Aquire control with
00171       the AquireControlMessage. Make sure you release control before exiting.
00172     
00173  * @param new_exclusive_controller new exclusive_controller value
00174  */
00175 void
00176 SkillerInterface::set_exclusive_controller(const unsigned int new_exclusive_controller)
00177 {
00178   data->exclusive_controller = new_exclusive_controller;
00179 }
00180 
00181 /** Get status value.
00182  * 
00183       The status of the current skill execution.
00184     
00185  * @return status value
00186  */
00187 SkillerInterface::SkillStatusEnum
00188 SkillerInterface::status() const
00189 {
00190   return data->status;
00191 }
00192 
00193 /** Get maximum length of status value.
00194  * @return length of status value, can be length of the array or number of 
00195  * maximum number of characters for a string
00196  */
00197 size_t
00198 SkillerInterface::maxlenof_status() const
00199 {
00200   return 1;
00201 }
00202 
00203 /** Set status value.
00204  * 
00205       The status of the current skill execution.
00206     
00207  * @param new_status new status value
00208  */
00209 void
00210 SkillerInterface::set_status(const SkillStatusEnum new_status)
00211 {
00212   data->status = new_status;
00213 }
00214 
00215 /** Get continuous value.
00216  * 
00217       True if continuous execution is in progress, false if no skill string is executed
00218       at all or it is executed one-shot with ExecSkillMessage.
00219     
00220  * @return continuous value
00221  */
00222 bool
00223 SkillerInterface::is_continuous() const
00224 {
00225   return data->continuous;
00226 }
00227 
00228 /** Get maximum length of continuous value.
00229  * @return length of continuous value, can be length of the array or number of 
00230  * maximum number of characters for a string
00231  */
00232 size_t
00233 SkillerInterface::maxlenof_continuous() const
00234 {
00235   return 1;
00236 }
00237 
00238 /** Set continuous value.
00239  * 
00240       True if continuous execution is in progress, false if no skill string is executed
00241       at all or it is executed one-shot with ExecSkillMessage.
00242     
00243  * @param new_continuous new continuous value
00244  */
00245 void
00246 SkillerInterface::set_continuous(const bool new_continuous)
00247 {
00248   data->continuous = new_continuous;
00249 }
00250
00251 /* =========== message create =========== */
00252 Message *
00253 SkillerInterface::create_message(const char *type) const
00254 {
00255   if ( strncmp("ExecSkillMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00256     return new ExecSkillMessage();
00257   } else if ( strncmp("ExecSkillContinuousMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00258     return new ExecSkillContinuousMessage();
00259   } else if ( strncmp("RestartInterpreterMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00260     return new RestartInterpreterMessage();
00261   } else if ( strncmp("StopExecMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00262     return new StopExecMessage();
00263   } else if ( strncmp("AcquireControlMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00264     return new AcquireControlMessage();
00265   } else if ( strncmp("ReleaseControlMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00266     return new ReleaseControlMessage();
00267   } else {
00268     throw UnknownTypeException("The given type '%s' does not match any known "
00269                                "message type for this interface type.", type);
00270   }
00271 }
00272
00273 
00274 /** Copy values from other interface.
00275  * @param other other interface to copy values from
00276  */
00277 void
00278 SkillerInterface::copy_values(const Interface *other)
00279 {
00280   const SkillerInterface *oi = dynamic_cast<const SkillerInterface *>(other);
00281   if (oi == NULL) {
00282     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00283                                 type(), other->type());
00284   }
00285   memcpy(data, oi->data, sizeof(SkillerInterface_data_t));
00286 }
00287
00288 /* =========== messages =========== */
00289 /** @class SkillerInterface::ExecSkillMessage <interfaces/SkillerInterface.h>
00290  * ExecSkillMessage Fawkes BlackBoard Interface Message.
00291  * 
00292     
00293  */
00294
00295 
00296 /** Constructor with initial values.
00297  * @param ini_skill_string initial value for skill_string
00298  */
00299 SkillerInterface::ExecSkillMessage::ExecSkillMessage(const char * ini_skill_string) : Message("ExecSkillMessage")
00300 {
00301   data_size = sizeof(ExecSkillMessage_data_t);
00302   data_ptr  = malloc(data_size);
00303   memset(data_ptr, 0, data_size);
00304   data      = (ExecSkillMessage_data_t *)data_ptr;
00305   strncpy(data->skill_string, ini_skill_string, 1024);
00306   add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string);
00307 }
00308 /** Constructor */
00309 SkillerInterface::ExecSkillMessage::ExecSkillMessage() : Message("ExecSkillMessage")
00310 {
00311   data_size = sizeof(ExecSkillMessage_data_t);
00312   data_ptr  = malloc(data_size);
00313   memset(data_ptr, 0, data_size);
00314   data      = (ExecSkillMessage_data_t *)data_ptr;
00315   add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string);
00316 }
00317 
00318 /** Destructor */
00319 SkillerInterface::ExecSkillMessage::~ExecSkillMessage()
00320 {
00321   free(data_ptr);
00322 }
00323 
00324 /** Copy constructor.
00325  * @param m message to copy from
00326  */
00327 SkillerInterface::ExecSkillMessage::ExecSkillMessage(const ExecSkillMessage *m) : Message("ExecSkillMessage")
00328 {
00329   data_size = m->data_size;
00330   data_ptr  = malloc(data_size);
00331   memcpy(data_ptr, m->data_ptr, data_size);
00332   data      = (ExecSkillMessage_data_t *)data_ptr;
00333 }
00334
00335 /* Methods */
00336 /** Get skill_string value.
00337  * 
00338       Currently executed skill string, at least the first 1023 bytes of it.
00339       Must be properly null-terminated.
00340     
00341  * @return skill_string value
00342  */
00343 char *
00344 SkillerInterface::ExecSkillMessage::skill_string() const
00345 {
00346   return data->skill_string;
00347 }
00348 
00349 /** Get maximum length of skill_string value.
00350  * @return length of skill_string value, can be length of the array or number of 
00351  * maximum number of characters for a string
00352  */
00353 size_t
00354 SkillerInterface::ExecSkillMessage::maxlenof_skill_string() const
00355 {
00356   return 1024;
00357 }
00358 
00359 /** Set skill_string value.
00360  * 
00361       Currently executed skill string, at least the first 1023 bytes of it.
00362       Must be properly null-terminated.
00363     
00364  * @param new_skill_string new skill_string value
00365  */
00366 void
00367 SkillerInterface::ExecSkillMessage::set_skill_string(const char * new_skill_string)
00368 {
00369   strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
00370 }
00371 
00372 /** Clone this message.
00373  * Produces a message of the same type as this message and copies the
00374  * data to the new message.
00375  * @return clone of this message
00376  */
00377 Message *
00378 SkillerInterface::ExecSkillMessage::clone() const
00379 {
00380   return new SkillerInterface::ExecSkillMessage(this);
00381 }
00382 /** @class SkillerInterface::ExecSkillContinuousMessage <interfaces/SkillerInterface.h>
00383  * ExecSkillContinuousMessage Fawkes BlackBoard Interface Message.
00384  * 
00385     
00386  */
00387
00388 
00389 /** Constructor with initial values.
00390  * @param ini_skill_string initial value for skill_string
00391  */
00392 SkillerInterface::ExecSkillContinuousMessage::ExecSkillContinuousMessage(const char * ini_skill_string) : Message("ExecSkillContinuousMessage")
00393 {
00394   data_size = sizeof(ExecSkillContinuousMessage_data_t);
00395   data_ptr  = malloc(data_size);
00396   memset(data_ptr, 0, data_size);
00397   data      = (ExecSkillContinuousMessage_data_t *)data_ptr;
00398   strncpy(data->skill_string, ini_skill_string, 1024);
00399   add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string);
00400 }
00401 /** Constructor */
00402 SkillerInterface::ExecSkillContinuousMessage::ExecSkillContinuousMessage() : Message("ExecSkillContinuousMessage")
00403 {
00404   data_size = sizeof(ExecSkillContinuousMessage_data_t);
00405   data_ptr  = malloc(data_size);
00406   memset(data_ptr, 0, data_size);
00407   data      = (ExecSkillContinuousMessage_data_t *)data_ptr;
00408   add_fieldinfo(IFT_STRING, "skill_string", 1024, data->skill_string);
00409 }
00410 
00411 /** Destructor */
00412 SkillerInterface::ExecSkillContinuousMessage::~ExecSkillContinuousMessage()
00413 {
00414   free(data_ptr);
00415 }
00416 
00417 /** Copy constructor.
00418  * @param m message to copy from
00419  */
00420 SkillerInterface::ExecSkillContinuousMessage::ExecSkillContinuousMessage(const ExecSkillContinuousMessage *m) : Message("ExecSkillContinuousMessage")
00421 {
00422   data_size = m->data_size;
00423   data_ptr  = malloc(data_size);
00424   memcpy(data_ptr, m->data_ptr, data_size);
00425   data      = (ExecSkillContinuousMessage_data_t *)data_ptr;
00426 }
00427
00428 /* Methods */
00429 /** Get skill_string value.
00430  * 
00431       Currently executed skill string, at least the first 1023 bytes of it.
00432       Must be properly null-terminated.
00433     
00434  * @return skill_string value
00435  */
00436 char *
00437 SkillerInterface::ExecSkillContinuousMessage::skill_string() const
00438 {
00439   return data->skill_string;
00440 }
00441 
00442 /** Get maximum length of skill_string value.
00443  * @return length of skill_string value, can be length of the array or number of 
00444  * maximum number of characters for a string
00445  */
00446 size_t
00447 SkillerInterface::ExecSkillContinuousMessage::maxlenof_skill_string() const
00448 {
00449   return 1024;
00450 }
00451 
00452 /** Set skill_string value.
00453  * 
00454       Currently executed skill string, at least the first 1023 bytes of it.
00455       Must be properly null-terminated.
00456     
00457  * @param new_skill_string new skill_string value
00458  */
00459 void
00460 SkillerInterface::ExecSkillContinuousMessage::set_skill_string(const char * new_skill_string)
00461 {
00462   strncpy(data->skill_string, new_skill_string, sizeof(data->skill_string));
00463 }
00464 
00465 /** Clone this message.
00466  * Produces a message of the same type as this message and copies the
00467  * data to the new message.
00468  * @return clone of this message
00469  */
00470 Message *
00471 SkillerInterface::ExecSkillContinuousMessage::clone() const
00472 {
00473   return new SkillerInterface::ExecSkillContinuousMessage(this);
00474 }
00475 /** @class SkillerInterface::RestartInterpreterMessage <interfaces/SkillerInterface.h>
00476  * RestartInterpreterMessage Fawkes BlackBoard Interface Message.
00477  * 
00478     
00479  */
00480
00481 
00482 /** Constructor */
00483 SkillerInterface::RestartInterpreterMessage::RestartInterpreterMessage() : Message("RestartInterpreterMessage")
00484 {
00485   data_size = 0;
00486   data_ptr  = NULL;
00487 }
00488 
00489 /** Destructor */
00490 SkillerInterface::RestartInterpreterMessage::~RestartInterpreterMessage()
00491 {
00492 }
00493 
00494 /** Copy constructor.
00495  * @param m message to copy from
00496  */
00497 SkillerInterface::RestartInterpreterMessage::RestartInterpreterMessage(const RestartInterpreterMessage *m) : Message("RestartInterpreterMessage")
00498 {
00499   data_size = 0;
00500   data_ptr  = NULL;
00501 }
00502
00503 /* Methods */
00504 /** Clone this message.
00505  * Produces a message of the same type as this message and copies the
00506  * data to the new message.
00507  * @return clone of this message
00508  */
00509 Message *
00510 SkillerInterface::RestartInterpreterMessage::clone() const
00511 {
00512   return new SkillerInterface::RestartInterpreterMessage(this);
00513 }
00514 /** @class SkillerInterface::StopExecMessage <interfaces/SkillerInterface.h>
00515  * StopExecMessage Fawkes BlackBoard Interface Message.
00516  * 
00517     
00518  */
00519
00520 
00521 /** Constructor */
00522 SkillerInterface::StopExecMessage::StopExecMessage() : Message("StopExecMessage")
00523 {
00524   data_size = 0;
00525   data_ptr  = NULL;
00526 }
00527 
00528 /** Destructor */
00529 SkillerInterface::StopExecMessage::~StopExecMessage()
00530 {
00531 }
00532 
00533 /** Copy constructor.
00534  * @param m message to copy from
00535  */
00536 SkillerInterface::StopExecMessage::StopExecMessage(const StopExecMessage *m) : Message("StopExecMessage")
00537 {
00538   data_size = 0;
00539   data_ptr  = NULL;
00540 }
00541
00542 /* Methods */
00543 /** Clone this message.
00544  * Produces a message of the same type as this message and copies the
00545  * data to the new message.
00546  * @return clone of this message
00547  */
00548 Message *
00549 SkillerInterface::StopExecMessage::clone() const
00550 {
00551   return new SkillerInterface::StopExecMessage(this);
00552 }
00553 /** @class SkillerInterface::AcquireControlMessage <interfaces/SkillerInterface.h>
00554  * AcquireControlMessage Fawkes BlackBoard Interface Message.
00555  * 
00556     
00557  */
00558
00559 
00560 /** Constructor */
00561 SkillerInterface::AcquireControlMessage::AcquireControlMessage() : Message("AcquireControlMessage")
00562 {
00563   data_size = 0;
00564   data_ptr  = NULL;
00565 }
00566 
00567 /** Destructor */
00568 SkillerInterface::AcquireControlMessage::~AcquireControlMessage()
00569 {
00570 }
00571 
00572 /** Copy constructor.
00573  * @param m message to copy from
00574  */
00575 SkillerInterface::AcquireControlMessage::AcquireControlMessage(const AcquireControlMessage *m) : Message("AcquireControlMessage")
00576 {
00577   data_size = 0;
00578   data_ptr  = NULL;
00579 }
00580
00581 /* Methods */
00582 /** Clone this message.
00583  * Produces a message of the same type as this message and copies the
00584  * data to the new message.
00585  * @return clone of this message
00586  */
00587 Message *
00588 SkillerInterface::AcquireControlMessage::clone() const
00589 {
00590   return new SkillerInterface::AcquireControlMessage(this);
00591 }
00592 /** @class SkillerInterface::ReleaseControlMessage <interfaces/SkillerInterface.h>
00593  * ReleaseControlMessage Fawkes BlackBoard Interface Message.
00594  * 
00595     
00596  */
00597
00598 
00599 /** Constructor */
00600 SkillerInterface::ReleaseControlMessage::ReleaseControlMessage() : Message("ReleaseControlMessage")
00601 {
00602   data_size = 0;
00603   data_ptr  = NULL;
00604 }
00605 
00606 /** Destructor */
00607 SkillerInterface::ReleaseControlMessage::~ReleaseControlMessage()
00608 {
00609 }
00610 
00611 /** Copy constructor.
00612  * @param m message to copy from
00613  */
00614 SkillerInterface::ReleaseControlMessage::ReleaseControlMessage(const ReleaseControlMessage *m) : Message("ReleaseControlMessage")
00615 {
00616   data_size = 0;
00617   data_ptr  = NULL;
00618 }
00619
00620 /* Methods */
00621 /** Clone this message.
00622  * Produces a message of the same type as this message and copies the
00623  * data to the new message.
00624  * @return clone of this message
00625  */
00626 Message *
00627 SkillerInterface::ReleaseControlMessage::clone() const
00628 {
00629   return new SkillerInterface::ReleaseControlMessage(this);
00630 }
00631 /** Check if message is valid and can be enqueued.
00632  * @param message Message to check
00633  */
00634 bool
00635 SkillerInterface::message_valid(const Message *message) const
00636 {
00637   const ExecSkillMessage *m0 = dynamic_cast<const ExecSkillMessage *>(message);
00638   if ( m0 != NULL ) {
00639     return true;
00640   }
00641   const ExecSkillContinuousMessage *m1 = dynamic_cast<const ExecSkillContinuousMessage *>(message);
00642   if ( m1 != NULL ) {
00643     return true;
00644   }
00645   const RestartInterpreterMessage *m2 = dynamic_cast<const RestartInterpreterMessage *>(message);
00646   if ( m2 != NULL ) {
00647     return true;
00648   }
00649   const StopExecMessage *m3 = dynamic_cast<const StopExecMessage *>(message);
00650   if ( m3 != NULL ) {
00651     return true;
00652   }
00653   const AcquireControlMessage *m4 = dynamic_cast<const AcquireControlMessage *>(message);
00654   if ( m4 != NULL ) {
00655     return true;
00656   }
00657   const ReleaseControlMessage *m5 = dynamic_cast<const ReleaseControlMessage *>(message);
00658   if ( m5 != NULL ) {
00659     return true;
00660   }
00661   return false;
00662 }
00663 
00664 /// @cond INTERNALS
00665 EXPORT_INTERFACE(SkillerInterface)
00666 /// @endcond
00667 
00668
00669 } // end namespace fawkes