简体   繁体   中英

How to receive a struct which was sent as a signal using dbus?

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)

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 to move from s to (iua{is}). 从s移至(iua {is})。

Once you point your iterator to (iua{is}), initialize a child iterator to this element using 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
         }
        }
    }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM