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>
40 using namespace fawkes;
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 ) )