23 #include "fawkes_bb_interface.h"
24 #include <plugins/readylogagent/eclipse_thread.h>
26 #include <core/exceptions/software.h>
27 #include <interface/interface.h>
28 #include <interfaces/ObjectPositionInterface.h>
30 #include <eclipseclass.h>
42 EC_word construct_iface_struct(
Interface* iface );
43 EC_word construct_msg_struct(
const char* iface_type,
Message* msg );
46 unsigned int num_fields );
47 void parse_struct_args( EC_word iface_struct,
50 unsigned int num_fields );
65 if ( EC_succeed != EC_arg( 1 ).is_string( &
id ) )
67 printf(
"First argument of read_interface/2 is not a string\n" );
75 printf(
"Interface with id %s is not available to the agent\n",
id );
83 iface_struct = construct_iface_struct( interface );
92 return unify( EC_arg(2), iface_struct );
107 if ( EC_succeed != EC_arg( 1 ).is_string( &
id ) )
109 printf(
"Firste argument of write_interface/2 is not a string\n" );
117 printf(
"Interface with id %s is not available to the agent\n",
id );
123 if ( EC_succeed != EC_arg( 2 ).functor( &fctor ) )
125 printf(
"Second argument of writer_interface/2 is not a compound term\n" );
130 char* iface_type = 0;
131 if ( 1 != sscanf( fctor.name(),
"data_%s", iface_type ) ||
132 0 != strcmp( interface->
type(), iface_type ) )
134 printf(
"Second argument of write_interface/2 is not of type data_%s but of type %s\n",
135 interface->
type(), fctor.name() );
142 unsigned int arity = (
unsigned int) EC_arg( 2 ).arity();
145 printf(
"Second argument of write_interface/2 has wrong arity\n" );
152 parse_struct_args( EC_word( 2 ),
176 if ( EC_succeed != EC_arg( 1 ).is_string( &iface_id ) )
178 printf(
"First argument of send_message/2 is not an atom\n" );
186 printf(
"Interface with id %s is not available to the agent\n", iface_id );
192 if ( EC_succeed != EC_arg( 2 ).functor( &fctor ) )
194 printf(
"Second argument of send_message/2 has no functor\n" );
198 char* fctor_name = strdup( fctor.name() );
199 strtok( fctor_name,
"_" );
200 char* iface_type = strtok( NULL,
"_" );
201 char* msg_type = strtok( NULL,
"_" );
205 0 != strcmp( interface->
type(), iface_type ) )
207 printf(
"Malformed functor: %s\n", fctor.name() );
219 printf(
"Message type %s is not available for interfaces of type %s\n",
220 msg_type, interface->
type() );
255 if ( EC_succeed != EC_arg( 1 ).is_string( &iface_id ) )
257 printf(
"First argument of send_message/2 is not an atom\n" );
264 printf(
"Interface with id %s is not available to the agent\n", iface_id );
268 std::list< EC_word > messages;
270 while ( !interface-> msgq_empty() )
277 msg_struct = construct_msg_struct( interface->
type(), msg );
285 messages.push_back( msg_struct );
291 while( !messages.empty() )
293 l = ::list( messages.front(), l );
294 messages.pop_front();
297 return unify( EC_arg( 2 ), l );
301 construct_iface_struct(
Interface* iface )
303 EC_word* args = construct_struct_args( iface->
fields(),
308 asprintf( &fctor,
"data_%s", iface->
type() );
310 EC_word ret = term( EC_functor( fctor, iface->
num_fields() ), args );
319 construct_msg_struct(
const char* iface_type,
Message* msg )
324 asprintf( &fctor,
"data_%s_%s", iface_type, msg->
type() );
326 EC_word ret = term( EC_functor( fctor, msg->
num_fields() ), args );
337 unsigned int num_fields )
339 EC_word* args =
new EC_word[ num_fields ];
342 unsigned int field_idx;
343 for ( field_iter = begin, field_idx = 0;
345 ++field_iter, ++field_idx )
352 char* t = strdup(
"true" );
353 char* f = strdup(
"fail" );
355 args[ field_idx ] = field_iter.
get_bool() ? EC_atom( t ) : EC_atom( f );
364 args[ field_idx ] = EC_word( field_iter.
get_int32() );
368 args[ field_idx ] = EC_word( (
long) field_iter.
get_uint32() );
372 args[ field_idx ] = EC_word( field_iter.
get_int64() );
376 args[ field_idx ] = EC_word( (
long) field_iter.
get_uint64() );
380 args[ field_idx ] = EC_word( field_iter.
get_float() );
384 args[ field_idx ] = EC_word( field_iter.
get_string() );
388 args[ field_idx ] = EC_word( field_iter.
get_byte() );
400 parse_struct_args( EC_word data_struct,
403 unsigned int num_fields )
405 unsigned int field_idx;
408 for ( field_iter = begin, field_idx = 1;
410 ++field_iter, ++field_idx )
415 if ( EC_succeed != data_struct.arg( field_idx, arg ) )
416 {
throw Exception(
"Failed to parse interface data. Couldn't read %d-th parameter.\n", field_idx ); }
421 char* t = strdup(
"true" );
422 char* f = strdup(
"fail" );
423 if ( EC_succeed == arg.unify( EC_atom( t ) ) )
425 else if ( EC_succeed == arg.unify( EC_atom( f ) ) )
442 if ( EC_succeed == arg.is_long( &val ) )
452 if ( EC_succeed == arg.is_long( &val ) )
453 { field_iter.
set_uint32( (
unsigned int) val ); }
462 if ( EC_succeed == arg.is_long( &val ) )
472 if ( EC_succeed == arg.is_long( &val ) )
473 { field_iter.
set_uint64( (
long unsigned int) val ); }
482 if ( EC_succeed == arg.is_double( &val ) )
492 if ( EC_succeed == arg.is_string( &val ) )
Interface field iterator.
void set_int64(int64_t i, unsigned int index=0)
Set value of current field as integer.
const char * get_string() const
Get value of current field as string.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
float get_float(unsigned int index=0) const
Get value of current field as float.
void set_float(float f, unsigned int index=0)
Set value of current field as float.
void set_bool(bool b, unsigned int index=0)
Set value of current field as bool.
const char * type() const
Get message type.
Fawkes library namespace.
interface_fieldtype_t get_type() const
Get type of current field.
uint64_t get_uint64(unsigned int index=0) const
Get value of current field as unsigned integer.
byte field, alias for uint8
Base class for all Fawkes BlackBoard interfaces.
InterfaceFieldIterator fields_end()
Invalid iterator.
void set_int32(int32_t i, unsigned int index=0)
Set value of current field as integer.
unsigned int num_fields()
Get the number of fields in the interface.
void msgq_pop()
Erase first message from queue.
static EclipseAgentThread * instance()
Get the EclipseAgentThread instance.
unsigned int num_fields() const
Get the number of fields in the message.
Base class for exceptions in Fawkes.
Message * msgq_first()
Get the first message from the message queue.
uint8_t get_byte(unsigned int index=0) const
Get value of current field as byte.
bool get_bool(unsigned int index=0) const
Get value of current field as bool.
void set_string(const char *s)
Set value of current field as string.
64 bit unsigned integer field
fawkes::Interface * get_registered_interface(const char *id)
Get the registered interface with the given id.
virtual Message * create_message(const char *type) const =0
Create message based on type name.
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
void set_uint32(uint32_t i, unsigned int index=0)
Set value of current field as unsigned integer.
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
void print_trace()
Prints trace to stderr.
InterfaceFieldIterator fields_end()
Invalid iterator.
void set_uint64(uint64_t i, unsigned int index=0)
Set value of current field as unsigned integer.
const char * get_typename() const
Get type of current field as string.
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
interface_fieldtype_t
Interface field type.
const char * type() const
Get type of interface.
uint32_t get_uint32(unsigned int index=0) const
Get value of current field as unsigned integer.
int32_t get_int32(unsigned int index=0) const
Get value of current field as integer.
32 bit unsigned integer field
int64_t get_int64(unsigned int index=0) const
Get value of current field as integer.