CMDbus Class Reference

Comunication through dbus. More...

Public Member Functions

 CMDbus (const char *name, const char *signal, unsigned int buffer)
 ~CMDbus ()
int ruleSignal ()
int recvSignal (char *signal)
int sendSignal (char *signal)

Private Member Functions

int init ()

Private Attributes

DBusConnection * conn
DBusError err
DBusMessageIter args
DBusMessage * msg
char * name
char * signal
unsigned int buffer_size

Detailed Description

Header for easy comunication aplications through Dbus.

Author:
Miguel Ángel Vílchez López 2008 <sacra@openmatica.es>

Codigo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Codigo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Examples:

TestDbus.cpp.


Constructor & Destructor Documentation

CMDbus::CMDbus ( const char *  name,
const char *  signal,
unsigned int  buffer = 1024 
)

Constructor.

Parameters:
[in] name The name of the interface.
[in] signal The name of the signal.
[in] buffer The buffer size.

00031 {
00032   this->name=(char*)malloc((strlen(name)+1)*sizeof(char));
00033   this->signal=(char*)malloc((strlen(signal)+1)*sizeof(char));
00034   strcpy(this->name,name);
00035   strcpy(this->signal,signal);
00036   this->buffer_size = buffer;
00037   if ( this->init() < 0)
00038   {
00039 //    this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__));
00040 //    this->debug.debug("Trying to initialize the class dbus...");
00041   }
00042 }

CMDbus::~CMDbus (  ) 

Destructor.

00045 {
00046   free(this->name);
00047   free(this->signal);
00048 }


Member Function Documentation

int CMDbus::init (  )  [private]

Initialize the bus connection.

Returns:
0 if correct, -1 if error.

00051 {
00052   int ret=0;
00053   
00054   // initialise the errors
00055   dbus_error_init(&this->err);
00056 
00057   // connect to the bus and check for errors
00058   this->conn = dbus_bus_get(DBUS_BUS_SESSION, &this->err);
00059   if (dbus_error_is_set(&this->err))
00060   {
00061 //    this->debug.error(this->err.message,__FILE__,__FUNCTION__,(__LINE__));
00062 //    this->debug.debug("Trying to connect to dbus...");
00063     dbus_error_free(&this->err); 
00064   }
00065   if (this->conn == NULL) {
00066     return -1;
00067   }
00068    
00069   // request our name on the bus and check for errors
00070   char* connection = (char*)malloc(100*sizeof(char));
00071   sprintf(connection,"%s.mokardu.sink",this->name);
00072   ret = dbus_bus_request_name(this->conn, connection, DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
00073   if (dbus_error_is_set(&this->err))
00074   {
00075 //    this->debug.error(this->err.message,__FILE__,__FUNCTION__,(__LINE__));
00076 //    this->debug.debug("Trying request our name on the bus...");
00077     dbus_error_free(&this->err); 
00078   }
00079 
00080   free(connection);
00081   
00082   return 0;
00083 }

int CMDbus::recvSignal ( char *  signal  ) 

Receive a signal.

Parameters:
[out] signal The variable to fill with data received.
Returns:
The number of bytes received, -1 if error.
Examples:
TestDbus.cpp.

00106 {
00107   // non blocking read of the next available message
00108   dbus_connection_read_write(this->conn, 0);
00109   this->msg = dbus_connection_pop_message(this->conn);
00110   
00111   // if we haven't read a message and we have the msg accopled
00112   if (this->msg == NULL)
00113     return 0;
00114 
00115   char* message = (char*)malloc(100*sizeof(char));
00116   sprintf(message,"%s.mokardu.Type",this->name);
00117   char* buffer = (char*)malloc(this->buffer_size*sizeof(char));
00118   
00119   // check if the message is a signal from the correct interface and with the correct name
00120   if (dbus_message_is_signal(this->msg, message, this->signal))
00121   {
00122     // read the parameters
00123     if (!dbus_message_iter_init(this->msg, &this->args))
00124     {
00125 //      this->debug.error("Message Has No Parameters",__FILE__,__FUNCTION__,(__LINE__));
00126 //      this->debug.debug("Trying to read the parameters...");
00127     }
00128     else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&this->args)) 
00129     {
00130 //      this->debug.error("Argument is not string!",__FILE__,__FUNCTION__,(__LINE__));
00131 //      this->debug.debug("Trying to read the parameters...");
00132     }
00133     else
00134       dbus_message_iter_get_basic(&this->args, &buffer);
00135 
00136     strcpy(signal, buffer);
00137     
00138     free(message);
00139     dbus_message_unref(this->msg);
00140   
00141     return strlen(signal);
00142     
00143   }
00144   else
00145   {
00146     free(message);
00147     free(buffer);
00148     dbus_message_unref(this->msg);
00149     
00150     return 0;
00151   }
00152 }

int CMDbus::ruleSignal (  ) 

Make a rule to catch a signal.

Returns:
0 if correct, -1 if error.
Examples:
TestDbus.cpp.

00086 {
00087   // add a rule for which messages we want to see
00088   char* rule = (char*)malloc(100*sizeof(char));
00089   sprintf(rule,"type='signal',interface='%s.mokardu.Type'",this->name);
00090   dbus_bus_add_match(this->conn, rule, &this->err); // see signals from the given interface
00091   dbus_connection_flush(this->conn);
00092   if (dbus_error_is_set(&this->err))
00093   {
00094 //    this->debug.error(this->err.message,__FILE__,__FUNCTION__,(__LINE__));
00095 //    this->debug.debug("Trying to set a rule on the bus...");
00096     free(rule);
00097     return -1;
00098   }
00099   
00100   free(rule);
00101   
00102   return 0;
00103 }

int CMDbus::sendSignal ( char *  signal  ) 

Send a signal.

Parameters:
[in] signal The signal to send.
Returns:
0 if correct, -1 if error.
Examples:
TestDbus.cpp.

00155 {
00156   char* object = (char*)malloc(100*sizeof(char));
00157   char* interface = (char*)malloc(100*sizeof(char));
00158   sprintf(object,"/%s/mokardu/Object",name);
00159   sprintf(interface,"%s.mokardu.Type",name);
00160   dbus_uint32_t serial = 0;
00161   
00162   // create a signal & check for errors 
00163   this->msg = dbus_message_new_signal(object, // object name of the signal
00164                                  interface, // interface name of the signal
00165                                  this->signal); // name of the signal
00166 
00167 //  this->debug.debug("Trying to set a new message bus...");
00168   if (this->msg == NULL) 
00169   {
00170 //    this->debug.error("Message Null",__FILE__,__FUNCTION__,(__LINE__));
00171     free(object);
00172     free(interface);
00173     return -1;
00174   }
00175   free(object);
00176   free(interface);
00177 
00178 //  this->debug.debug("Trying to append arguments onto signal...");
00179   dbus_message_iter_init_append(this->msg, &this->args);
00180   if (!dbus_message_iter_append_basic(&this->args, DBUS_TYPE_STRING, &signal))
00181   {
00182 //    this->debug.error("Out of Memory!",__FILE__,__FUNCTION__,(__LINE__));
00183     return -1;
00184   }
00185 
00186 //  this->debug.debug("Trying to send the message and flush the connection...");
00187   if (!dbus_connection_send(this->conn, this->msg, &serial))
00188   {
00189 //    this->debug.error("Out of Memory!",__FILE__,__FUNCTION__,(__LINE__));
00190     return -1;
00191   }
00192   dbus_connection_flush(this->conn);
00193 
00194 //  this->debug.debug("Signal sended...");
00195   
00196   // free the message 
00197   dbus_message_unref(this->msg);
00198 
00199   return 0;
00200 }


Field Documentation

DBusMessageIter CMDbus::args [private]
unsigned int CMDbus::buffer_size [private]

The buffer size.

DBusConnection* CMDbus::conn [private]

The connection.

DBusError CMDbus::err [private]

The error buffer.

DBusMessage* CMDbus::msg [private]
char* CMDbus::name [private]

The name of the interface.

char* CMDbus::signal [private]

The name of the signal.

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3