Question

I created a program which can send a signal ( string ) and an ohter application which can read that. I use dbus api. Now I need to send a struct (object) as a signal. Here is the most important part of the code (sending):

struct x
{
   int a;
   char *b;
   int c;
} obj;


DBusMessageIter myMsgItrA, myMsgItrB;
dbus_message_iter_init_append(msg, &myMsgItrA);

dbus_message_iter_open_container(&myMsgItrA, DBUS_TYPE_STRUCT, NULL, &myMsgItrB);

dbus_message_append_basic(&myMsgItrB, DBUS_TYPE_INT32, &obj.a);
dbus_message_append_basic(&myMsgItrB, DBUS_TYPE_STRING, &obj.b);
dbus_message_append_basic(&myMsgItrB, DBUS_TYPE_INT32, &obj.c);

dbus_message_close_container(&myMsgItrA, &myMsgItrB);

How to receive that signal ? ( I have used dbus_message_iter_get_basic for basic types)

Was it helpful?

Solution

Intialize iterator to your message & use it to parse through individual elements of dbus signature. Use dbus_message_iter_next to move to next individual element of dbus message and dbus_message_iter_recurse to get into a complex individual element.

Eg: Consider a signature s(iua{is}). Individual elements are s and (iua{is}). Initialze a top level iterator using dbus_message_iter_init.

Use dbus_message_iter_next to move from s to (iua{is}).

Once you point your iterator to (iua{is}), initialize a child iterator to this element using dbus_message_iter_recurse and parse further using child iterator.

For a signature (isi), parsing would be as shown below

DBusMessageIter rootIter;
dbus_message_iter_init(msg, &rootIter);

if (DBUS_TYPE_STRUCT == dbus_message_iter_get_arg_type(&rootIter))//Get type of argument
{
   //Initialize iterator for struct
   DBusMessageIter structIter;
   dbus_message_iter_recurse(&rootIter, &structIter);

    //Argument 1 is int32
    if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter))
    {
      int a;
      dbus_message_iter_get_basic(&structIter, &a);//Read integer
      dbus_message_iter_next(&structIter);//Go to next argument of structiter
      //Arg 2 should be a string

  if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&structIter))
  {
         char* str = NULL; 
         dbus_message_iter_get_basic(&structIter, &str);//this function is used to read basic dbus types like int, string etc. 
         dbus_message_iter_next(&structIter);//Go to next argument of root iter

     //Argument 3 is int32
         if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter))
         {
        int c;
        dbus_message_iter_get_basic(&structIter, &c);//Read integer
        //PARSING SHOULD END HERE ON SUCCESS
         }
        }
    }
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top