code source dbus

"Charles Hervé Tchoutat Tchabo" t_charles at gmx.de
Wed Oct 10 10:22:36 PDT 2007


thank you to answer me, I was already of the same opinion as Aashish Raina . This problem I pose it since more than one month on the forum, without continuation I hope that I would obtain finally an answer. My code is below, I am sorry it's a little long, I await the correction impatiently.
the compilation is ok, but the execution not.
Best regards
Charles

/* here is the instropection file */
<?xml version="1.0" encoding="UTF-8" ?>
<node name="/esd/esdtest/esdmessdata">
   <interface name="esd.esdtest.esdmessdata">
      <!--Remote Methods of the Pruefprogramms from the GUI Application-->
      <method name="StartEsdTest"> 
	  <arg type="a(uss(bad(ddd)(ddd))(bad(ddd)(ddd))(bddd)uua(u(bad(ddd)(ddd))(bad(ddd)(ddd))))" name="geometry" direction="in"/>
	  <arg type="(ubui)" name="Level" direction="in"/>
	  <arg type="a(ub)" name="Points" direction="in"/>
	  <!--Output EsdTestStart-->
	  <arg type="b" name="isEsdStart" direction="out"/>
      </method>     
   </interface>


/* Here is the server */
#include <dbus/dbus-protocol.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

#include "messdata.h"


typedef struct GuiMessData GuiMessData;
typedef struct GuiMessDataClass GuiMessDataClass;

GType gui_mess_data_get_type (void);

struct GuiMessData
{
  GObject parent;
};

struct GuiMessDataClass
{
  GObjectClass parent;
};

#define GUI_MESS_TYPE_DATA     	      (gui_mess_data_get_type ())
#define GUI_MESS_DATA(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GUI_MESS_TYPE_DATA, GuiMessData))
#define GUI_MESS_DATA_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GUI_MESS_TYPE_DATA, GuiMessDataClass))
#define GUI_MESS_IS_DATA(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GUI_MESS_TYPE_DATA))
#define GUI_MESS_IS_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GUI_MESS_TYPE_DATA))
#define GUI_MESS_DATA_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GUI_MESS_TYPE_DATA, GuiMessDataClass))

gboolean gui_data_start_esd_test (GuiMessData *obj, GPtrArray* IN_geometry, GValueArray* IN_Level, GPtrArray* IN_Points, gboolean* OUT_isEsdStart, GError **error);

void _printPoints (gpointer data, gpointer userdata);
void _printLevel (GValueArray *Level);
void _printPlugData (gpointer gpdata, gpointer userdata);
static gboolean receiveMessData(void);

#include "MessDataGlue.h"

/* global variable */
const char *std = NULL;

static void
lose (const char *str, ...)
{
  va_list args;

  va_start (args, str);

  vfprintf (stderr, str, args);
  fputc ('\n', stderr);

  va_end (args);

  exit (1);
}

static void
lose_gerror (const char *prefix, GError *error) 
{
  lose ("%s: %s", prefix, error->message);
}


/* Generate the GObject boilerplate */
G_DEFINE_TYPE(GuiMessData, gui_mess_data, G_TYPE_OBJECT);

/* Class init */
static void
gui_mess_data_class_init (GuiMessDataClass *klass)
{
	//dbus_g_object_type_install_info (GUI_MESS_TYPE_DATA, &dbus_glib_gui_data_object_info); 
	dbus_g_object_type_install_info (G_TYPE_FROM_CLASS(klass), &dbus_glib_gui_data_object_info);
}

/* Instance init */
static void
gui_mess_data_init (GuiMessData *obj)
{

}

void
_printLevel (GValueArray *testLevel)
{
	guint n = 0, i;
	GValue *val;	
    for (i = 0; i < testLevel->n_values; i++){
	val = g_value_array_get_nth(testLevel, i);
      switch (i){

      	case 0: //if (G_VALUE_TYPE(g_value_array_get_nth(testLevel, i)) == G_TYPE_UINT){
		if (G_VALUE_HOLDS(val, G_TYPE_UINT)){
		  printf ("%s", n++ == 0 ? "Testlevel:        " : "                  ");
		  printf ("[%2d] ", g_value_get_uint(val));
      		}
		break;
	case 1: //if (G_VALUE_TYPE(g_value_array_get_nth (testLevel, i)) == G_TYPE_BOOLEAN){
		if (G_VALUE_HOLDS(val, G_TYPE_BOOLEAN)){
		  printf ("%s", g_value_get_boolean(val) == FALSE ? "Contact" : "Air    ");
		}
		break;
	case 2: //if (G_VALUE_TYPE(g_value_array_get_nth (testLevel, i)) == G_TYPE_UINT){
		if (G_VALUE_HOLDS(val, G_TYPE_UINT)){
		  printf (" ; %2dx", g_value_get_uint(val));
      		}
		break;

	case 3: //if (G_VALUE_TYPE(g_value_array_get_nth (testLevel, i)) == G_TYPE_INT){
		if (G_VALUE_HOLDS(val, G_TYPE_INT)){
		    if (abs(g_value_get_int(g_value_array_get_nth (testLevel, i))) < 1000)
			printf (" ; %6dV ", abs(g_value_get_int(val)));
		    else
		        printf (" ; %6gkV", (float) (abs(g_value_get_int(val)) / 1000));
      		}
		break;
      }

	g_print ("\n %s ---\n", g_type_name (G_VALUE_TYPE (val)));
    }
	//g_value_array_free (testLevel);
	printf ("\n");
}


void
_printPoints (gpointer data, gpointer userdata)
{
	GValueArray *point = data;
	guint i;
	GValue *val;
	   for (i = 0; i < point->n_values; i++){
		val = g_value_array_get_nth(point, i);
      		switch (i){

      			case 0: 
			if (G_VALUE_HOLDS(val, G_TYPE_UINT)){
		 	 printf ("%d \n", g_value_get_uint(val));
      			}
			break;
			case 1: 
			if (G_VALUE_HOLDS(val, G_TYPE_BOOLEAN)){
		  	printf ("%s", g_value_get_boolean(val) == FALSE ? "ERROR" : "OK");
			}
			break;
      		}  
		    
	  }	
}

void
_printPlugData (gpointer gpdata, gpointer userdata)
{
	struct pin *p;
	struct plug *data = gpdata;
   	//angles *angl = NULL;
        guint j;
	    	if (data){
		     printf ("Stecker    %d\n", data->nr);
		     printf ("Plugs name %s\n", data->name->str);
		     printf ("Plugs info %s\n", data->info->str);
		     printf ("Origine:    X =%6.1f ;  Y =%6.1f ;  Z =%6.1f\n", data->origin.x, data->origin.y, data->origin.z);
		     printf ("Safe point: X =%6.1f ;  Y =%6.1f ;  Z =%6.1f\n", data->safe.x, data->safe.y, data->safe.z);
		    // angl = math_convertAngles(&data->turn, AF_DEG);
		     printf ("Turn     :  A =%6.1f ;  B =%6.1f ;  C =%6.1f\n", data->turn.A, data->turn.B, data->turn.C);
		     printf ("Slope  :  %d\n", data->slope);
		     printf ("Count  :  %d\n", data->count);
		     for (j = 0; j < data->pins->len; j++){
		   	  p = (struct pin *)g_ptr_array_index(data->pins, j);
			  if (p){
			     printf ("  %2d: p.X =%6.1f ; p.Y =%6.1f ; p.Z =%6.1f / s.X =%6.1f ; s.Y =%6.1f ; s.Z =%6.1f \n",
				p->nr, p->pp.x, p->pp.y, p->pp.z, p->sp.x, p->sp.y, p->sp.z);
			  }
		     }
		     
		}
	
	
}

gboolean 
gui_data_start_esd_test (GuiMessData *obj, GPtrArray* _geometry, GValueArray* _Level, GPtrArray* _Points, gboolean* _isEsdStart, GError **error)
{
     if (_geometry != NULL)
     	g_ptr_array_foreach(_geometry, (GFunc)_printPlugData, NULL);
     else{
	 *_isEsdStart = FALSE;
	 return TRUE;
     }   
     
    if (_Level != NULL)
     	_printLevel (_Level);
     else{
	 *_isEsdStart = FALSE;
	 return TRUE;
     }
     if (_Points != NULL)
     	g_ptr_array_foreach(_Points, (GFunc)_printPoints, NULL);
     else{
	 *_isEsdStart = FALSE;
	 return TRUE;
     }
    *_isEsdStart = TRUE;
     return TRUE;
}

int
main (int argc, char **argv)
{
  DBusGConnection *bus;
  DBusGProxy *bus_proxy;
  GError *error = NULL;
  GuiMessData *obj;
  GMainLoop *mainloop;
  guint request_name_result;

  g_type_init ();

  {
    GLogLevelFlags fatal_mask;
    
    fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
    fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
    g_log_set_always_fatal (fatal_mask);
  }
 
  mainloop = g_main_loop_new (NULL, FALSE);
  g_print ("Launching receiveGuiMessData ...\n"); 

  /* Obtain a connection to the session bus */
  bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
  if (!bus)
    lose_gerror ("Couldn't connect to session bus", error);

  obj = g_object_new (GUI_MESS_TYPE_DATA, NULL);
  dbus_g_connection_register_g_object (bus, "/esd/esdtest/esdmessdata", G_OBJECT (obj));

  bus_proxy = dbus_g_proxy_new_for_name (bus, 
					DBUS_SERVICE_DBUS,
					DBUS_PATH_DBUS,
					DBUS_INTERFACE_DBUS);

  if (!org_freedesktop_DBus_request_name (bus_proxy, "esd.esdtest.esdmessdata", 0, &request_name_result, &error))
      lose_gerror ("Failed to acquire esd.esdtest.esdmessdata", error);

  if (request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER){
	g_error ("Got result code %u from requesting name", request_name_result);
	exit (1); 
  }

  printf ("DBus service active Waiting for ESD Messdaten ... \n");

  g_main_loop_run (mainloop);
  exit (0);
}


/* here is the client */

#include <dbus/dbus-protocol.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;

static void
lose (const char *str, ...)
{
  va_list args;

  va_start (args, str);

  vfprintf (stderr, str, args);
  fputc ('\n', stderr);

  va_end (args);

  exit (1);
}

static void
lose_gerror (const char *prefix, GError *error) 
{
  lose ("%s: %s", prefix, error->message);
}
#include "MessDataBindings.h"
#include "messdata.h"

enum {KARTESIAN, POLAR};
enum {AIR, CONTACT};
enum {SERIE, PARALLEL};

/*
static void
print_hash_value (gpointer key, gpointer val, gpointer data)
{
  printf ("%s -> %s\n", (char *) key, (char *) val);
}
*/

void init_pin(GPtrArray* ptr_pins);
struct plug * dbase_newPlug (void);
struct pin * dbase_newPin (void);
struct testlevel * dbase_newESDTestLevel (void);
static void _gvalue_array_append_int(GValueArray *testLevel, gint nb);
static void _gvalue_array_append_bool(GValueArray *testLevel, gboolean b);
static void _gvalue_array_append_uint(GValueArray *testLevel, guint unb);


vector* math_initVector (vector *A, double x, double y, double z);

/* -- Vector functions -- */

vector*
math_initVector (vector *A, double x, double y, double z)
{
	A->type = VT_XYZ;
	A->x = x;
	A->y = y;
	A->z = z;
	return A;
}

/* -- rotation functions -- */

angles *
math_initAngles (angles *W, double A, double B, double C)
{
	W->fmt = AF_DEG;
	W->A = A;
	W->B = B;
	W->C = C;
	return W;
}

struct plug *
dbase_newPlug (void)
{
	struct plug *data;
	
	if ((data = (struct plug *) malloc (sizeof(struct plug)))) {
		data->nr = 0;
		data->name = g_string_new (NULL);
		data->info = g_string_new (NULL);
		math_initVector (&data->origin, 0, 0, 0);
		math_initVector (&data->safe, 0, 0, 0);
		math_initAngles (&data->turn, 0, 0, 0);
		data->slope = 0;
		data->count = 0;
		data->pins = NULL; /* (GSList) GPtrArray of struct pin */
	}
	return data;
}

struct pin *
dbase_newPin (void)
{
	struct pin *data;

	if ((data = (struct pin *) malloc (sizeof(struct pin)))) {
		data->nr  = 0.0;
		math_initVector (&data->pp, 0.0, 0.0, 0.0);
		math_initVector (&data->sp, 0.0, 0.0, 0.0);
	}
	return data;
}

struct testlevel *
dbase_newESDTestLevel (void)
{
	struct testlevel *data;
	
	if ((data = (struct testlevel *) malloc (sizeof(struct testlevel)))) {
		data->nr            = 0;
		data->dischargTyp   = 0;
		data->isMeasure     = 0;
		data->isGround      = 0;
		data->count         = 0;
		data->voltage       = 0.0;
	}
	return data;
}	

static void
_gvalue_array_append_int(GValueArray *testLevel, gint nb)
{
  GValue val ={0};
  g_value_init(&val, G_TYPE_INT);
  g_value_set_int(&val, nb);
  g_value_array_append(testLevel, &val);
  g_value_unset(&val);
}

static void
_gvalue_array_append_bool(GValueArray *testLevel, gboolean b)
{
  GValue val ={0};
  g_value_init(&val, G_TYPE_BOOLEAN);
  g_value_set_int(&val, b);
  g_value_array_append(testLevel, &val);
  g_value_unset(&val);
}

static void
_gvalue_array_append_uint(GValueArray *testLevel, guint unb)
{
  GValue val ={0};
  g_value_init(&val, G_TYPE_UINT);
  g_value_set_uint(&val, unb);
  g_value_array_append(testLevel, &val);
  g_value_unset(&val);
}


int
main (int argc, char **argv)
{
  DBusGConnection *bus;
  DBusGProxy *remote_object;

  GError *error = NULL;
  gboolean is_esd_start = FALSE;

  struct pin *ptr_pin = dbase_newPin ();
  struct plug *ptr_plug = dbase_newPlug (); 
  /* Electrinic definition*/

  /* Pins */
  GPtrArray* gpPins;
  gpPins = g_ptr_array_new();
  ptr_pin->nr = 1;
  math_initVector (&ptr_pin->pp, 0.0, 0.0, 0.0);
  math_initVector (&ptr_pin->sp, 0.0, 0.0, -50.0);
  g_ptr_array_add(gpPins, (gpointer)ptr_pin);
  ptr_pin->nr = 2;
  math_initVector (&ptr_pin->pp, 0.0, 5.5, 0.0);
  math_initVector (&ptr_pin->sp, 0.0, 5.5, -50.0);
  g_ptr_array_add(gpPins, (gpointer)ptr_pin);
  ptr_pin->nr = 3;
  math_initVector (&ptr_pin->pp, 0.0, 11.0, 0.0);
  math_initVector (&ptr_pin->sp, 0.0, 11.0, -50.0);
  g_ptr_array_add(gpPins, (gpointer)ptr_pin);

  /*  Plugs  */
  GPtrArray* gpPlugs;  // a(uss(bad(ddd)(ddd))(bad(ddd)(ddd))(bddd)uua(u(bad(ddd)(ddd))(bad(ddd)(ddd))))
  gpPlugs = g_ptr_array_new();
  ptr_plug->nr = 1;
  ptr_plug->name = g_string_assign(ptr_plug->name, "14_AMP-SKN");
  ptr_plug->info = g_string_assign(ptr_plug->info, "info plug 1");
  math_initVector (&ptr_plug->origin, 0.0, 0.0, 0.0);
  math_initVector (&ptr_plug->safe, 0.0, -100.0, -150.0);
  math_initAngles (&ptr_plug->turn, 0.0, 0.0, 0.0);
  ptr_plug->slope = 1;
  ptr_plug->count = 3;
  ptr_plug->pins  = gpPins;
  g_ptr_array_add(gpPlugs, (gpointer)ptr_plug);

  ptr_plug->nr = 2;
  ptr_plug->name = g_string_assign(ptr_plug->name, "18_AMP-SKN");
  ptr_plug->info = g_string_assign(ptr_plug->info, "info plug 2");
  math_initVector (&ptr_plug->origin, 40.0, 0.0, 0.0);
  math_initVector (&ptr_plug->safe, 40.0, -100.0, -150.0);
  math_initAngles (&ptr_plug->turn, 25.0, 0.0, -80.0);
  ptr_plug->slope = 1;
  ptr_plug->count = 3;
  ptr_plug->pins  = gpPins;
  g_ptr_array_add(gpPlugs, (gpointer)ptr_plug);

  GValueArray *level;  // (ubui)
  level = g_value_array_new (4);
  _gvalue_array_append_uint(level, 1);
  _gvalue_array_append_bool(level, TRUE);
  _gvalue_array_append_uint(level, 3);
  _gvalue_array_append_int(level, 2);

  GValueArray *point_a;  // (ub)
  point_a = g_value_array_new (2);
  _gvalue_array_append_uint(point_a, 1);
  _gvalue_array_append_bool(point_a, TRUE);

  GValueArray *point_b;  // (ub)
  point_b = g_value_array_new (2);
  _gvalue_array_append_uint(point_b, 1);
  _gvalue_array_append_bool(point_b, TRUE);

  GPtrArray* gp_points;  // a(ub)
  gp_points = g_ptr_array_new();
  g_ptr_array_add(gp_points, (gpointer)point_a);
  g_ptr_array_add(gp_points, (gpointer)point_b);


  g_type_init ();
  bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
  if (!bus)
    lose_gerror ("Couldn't connect to session bus", error);
  
  remote_object = dbus_g_proxy_new_for_name (bus,
					     "esd.esdtest.esdmessdata",
					     "/esd/esdtest/esdmessdata",
					     "esd.esdtest.esdmessdata");

  if (!esd_esdtest_esdmessdata_start_esd_test (remote_object, gpPlugs, level, gp_points, &is_esd_start, &error))
    lose_gerror ("Failed to complete start_esd_test ", error);

  if (is_esd_start)
  	printf ("The ESD Test has started \n");

  g_object_unref (G_OBJECT (remote_object));

  exit(0);
}


/* here is the include file */

#ifndef INCLUDE_MESSDATA_H
#define INCLUDE_MESSDATA_H

enum vectortype { VT_XYZ, VT_POLAR };
typedef struct _vector {
	enum vectortype type;
	union {
		double V[3];    /* for using in loops */
		struct {
			double x;   /* kartesian coordinates */
			double y;
			double z;
		};
		struct {
			double r;   /* polar coordinates */
			double a1;
			double a2;
		};
	};
} vector;

enum angleformat { AF_RAD, AF_DEG };
typedef struct _angles {
	enum angleformat fmt;
	double A;        /* rotation    */
	double B;
	double C;
} angles;

struct pin
{
   unsigned int 	nr;	
   vector 		pp;     
   vector 		sp;    
};

struct plug
{
    unsigned int 	nr;      
    GString* 		name;    
    GString*		info;    
    vector 		origin;  
    vector 		safe;    
    angles 		turn;    
    unsigned int 	slope;   
    unsigned int 	count;   
    GPtrArray*		pins;    

};

struct testlevel {
    int nr;                	
    gboolean dischargTyp; 	
    gboolean isMeasure;     	
    gboolean isGround;      	
    unsigned int count;    	
    signed int voltage;   
};

#endif /* INCLUDE_MESSDATA_H */

 Hi,
> 
> 2007/10/9, "Charles Hervé Tchoutat Tchabo" <t_charles at gmx.de>:
> > Hi!
> > I have a big problem with my program, a want to send and receive
> structures and array of structures like a(uibdda(uii)) and (uuiid) for examples
> with dbus-proxy. I readed the DBUS-Specication and the tutorial but I just
> can't get it. Can anyone send me programs or links wich can help me to solve
> my problem.
> >
> Could you show the code you already got, even if it doesn't work? This
> would help as it would show where you are and what might be missing.
> 
> Best regards,
> 
> Andreas

-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser


More information about the dbus mailing list