D-bus:transmit a Data Array in simple and useful Form

Source: Internet
Author: User



In lots situation, one would send data via a simple data structure:a byte array contain real data, and an integer to not E This data ' s length.  This concept would appear when one uses the D-bus, of Course. For D-bus, D-bus_type_array was different from the others data types, for it was not a-to-know the data length if there is No additional information. (length of Dbus_type_string could was known by seeking, where is the null flag.)

In this example, I demonstrate how to send a data array via D-bus.

SERVER.C:


 / * SERVER.C * /#include <dbus/dbus.h>#include <stdbool.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>Dbushandlerresult Filter_func(Dbusconnection *Connection,       Dbusmessage *message, void *usr_data){    Dbusmessage *reply;    dbus_bool_t handled = false;    Char *Preaddata;    int Len;    unsigned Char i;         Dbuserror Dberr;        Dbus_error_init (&dberr);        printf ("Preaddata =%x\ n", (unsigned int) preaddata);        if(FALSE == Dbus_message_get_args (Message, &Dberr, Dbus_type_array,  Dbus_type_byte, &Preaddata, &Len,  dbus_type_invalid) && 0 != len) {  //printf ("len =%d\n");  //printf ("Receiver Data error\n");  return dbus_handler_result_not_yet_handled; }  if(0 == len)  return dbus_handler_result_handled;        printf ("Len =%d,", len);         for( I = 0; I < Len; I++)  printf ("% #2x", (unsigned Char) preaddata[i]); printf ("\ n");        handled = true;        printf ("Preaddata =%x\ n", (unsigned int) preaddata);        /*if One free preaddata, it'll crash!*/    //dbus_free_string_array ((char**) &preaddata);        return (Handled ? dbus_handler_result_handled :  dbus_handler_result_not_yet_handled);  }/*filter_func*/int Main(int argc, Char *argv[]){    Dbuserror Dberr;    dbusconnection *Dbconn;    Dbus_error_init (&dberr);    Dbconn = Dbus_bus_get (dbus_bus_session, &dberr);    if (!Dbus_connection_add_filter (Dbconn, Filter_func, NULL, NULL)) {        return -1;    }    Dbus_bus_add_match (Dbconn, "type= ' signal ', interface= ' Gaiger. Drstein.demonstration ' ", &dberr);         while(Dbus_connection_read_write_dispatch (Dbconn, -1)) {        / * loop * /    }        return 0;}/*main*/


Take a notice:the buffer which containers received data are allocated by D-bus and you could not release it manually. D-bus would manage this buffer automatically, it would not occur memory leaking.


CLIENT.C:


#include <dbus/dbus.h>#include <stdbool.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int Db_send(Dbusconnection *dbconn){    Dbusmessage *dbmsg;    Char *Psenddata;    int Len;    unsigned Char i; Psenddata = (Char *) malloc ( the);     dbmsg = dbus_message_new_signal ("/client/signal/object",   "Gaiger. Drstein.demonstration ", "Test");   Len = 6;  for(i = 0; I < Len; I++)  Psenddata[i] = (unsigned Char) I;       if (!Dbus_message_append_args (dbmsg, Dbus_type_array, Dbus_type_byte,   &Psenddata,  Len, dbus_type_invalid))  {  return -1;    }    if (!dbus_connection_send (Dbconn, Dbmsg, NULL)) {        return -1;    }        Dbus_connection_flush (dbconn);    printf ("Send Message:len =%d,", Len );      for( I = 0; I < Len; I++)  printf ("% #x", (unsigned Char) psenddata[i]);     printf ("\ n");     Dbus_message_unref (dbmsg);        Free (psenddata);       return 0;}/**/int Main(int argc, Char *argv[]){ unsigned int i;    Dbuserror Dberr;    dbusconnection *Dbconn;    Dbus_error_init (&dberr);    Dbconn = Dbus_bus_get (dbus_bus_session, &dberr);#if (1)     for(i = 0; I < 3; I++)  db_send (dbconn);#else     while(Dbus_connection_read_write_dispatch (Dbconn, -1)) {        db_send (dbconn);    }#endif      Dbus_connection_unref (dbconn);    return 0;}

The codes is consistent, client could send data to server. Hope it is useful.

Note the line:


if (FALSE= =dbus_message_get_args (message,&dberr,dbus_type_ ARRAY,dbus_type_byte,&preaddata,&len,   Dbus_type_invalid)&&0!=len)


The type of the length variable should be int. even your data length is as long as Int. It is, inside the Dbus library, the Lengh pointer have been assume as int*. If you put the "other" integer types as length, the values in vicinity of the passed length is marred, it would let Crash.

More detail-ref this post in stack overflow.

If you don't know how to rewrite Makefile for the 2 codes, you could refer to this post I wrote.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

D-bus:transmit a Data Array in simple and useful Form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.