简单的dbus使用

首先安装dbus的各种库

具体步骤请在csdn上参考 别说郁闷 写的

dbus-glib 安装环境搭建

按照他写的步骤,全部做完后再编译可能会出现找不到mechine-id的错误具体解决方法如https://www.sothink.cn/archives/2015/12/d-bus-library-appears-to-be-incorrectly-set-up.html

再调整好所有的头文件位置后即可编译。

下面是简单的首发实例,这个实例server从client读取两个整数并将其和返回给client

server:

#include 
#include               
#include 
#include 
#include 
#include 
#include 
#include 
#include 
void reply_to_method_call(DBusMessage* msg, DBusConnection* conn)
{
   DBusMessage* reply;
   DBusMessageIter rootIter;
   dbus_uint32_t serial = 0;
   dbus_uint32_t a;
   dbus_uint32_t b;
   dbus_uint32_t sum;

   // read the arguments
   if (!dbus_message_iter_init(msg,&rootIter))
      fprintf(stderr, "Message has no arguments!\n");
    dbus_message_iter_get_basic(&rootIter, &a);
    printf("Method called with %d\n", a);
    if(dbus_message_iter_has_next(&rootIter))
    {
        dbus_message_iter_next(&rootIter);
        dbus_message_iter_get_basic(&rootIter, &b);
        printf("Method called with %d\n", b);
    }

   if ( dbus_message_is_method_call( msg, "test.method.Type", "Method" ) ) 
{
    sum=a+b; 
} 
  // create a reply from the message
   reply = dbus_message_new_method_return(msg);

   // add the arguments to the reply
   dbus_message_iter_init_append(reply, &rootIter);

   if (!dbus_message_iter_append_basic(&rootIter, DBUS_TYPE_INT32, &sum)) { 
      fprintf(stderr, "Out Of Memory!\n"); 
      exit(1);

   }

   // send the reply && flush the connection
   if (!dbus_connection_send(conn, reply, &sum)) {
      fprintf(stderr, "Out Of Memory!\n"); 
      exit(1);
   }
   dbus_connection_flush(conn);

   // free the reply
   dbus_message_unref(reply);
}

int main()
{
 DBusMessage* msg;
   DBusMessage* reply;
   DBusMessageIter args;
   DBusConnection* conn;
   DBusError err;
   int ret;
   char* param;

   printf("Listening for method calls\n");

   // initialise the error
   dbus_error_init(&err);

   // connect to the bus and check for errors
   conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
   if (dbus_error_is_set(&err)) { 
      fprintf(stderr, "Connection Error (%s)\n", err.message); 
      dbus_error_free(&err); 
   }
   if (NULL == conn) {
      fprintf(stderr, "Connection Null\n"); 
      exit(1); 
   }

   // request our name on the bus and check for errors
   ret = dbus_bus_request_name(conn,"test.server.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
   if (dbus_error_is_set(&err)) { 
      fprintf(stderr, "Name Error (%s)\n", err.message); 
      dbus_error_free(&err);
   }


   // loop, testing for new messages
   while (true) {
      // non blocking read of the next available message
      dbus_connection_read_write(conn, 0);
      msg = dbus_connection_pop_message(conn);

      // loop again if we haven't got a message
      if (NULL == msg) { 
         sleep(1); 
         continue; 
      }

      if ( dbus_message_has_interface(msg, "test.method.Type") )
        reply_to_method_call( msg, conn );
      // free the message
      dbus_message_unref(msg);
   }

   // close the connection
   dbus_connection_close(conn);
}

client:

#include 
#include 
#include 
#include 
#include 
void caller(int param,int param1)
{
   DBusMessage* msg;
   DBusMessageIter args;
   DBusConnection* conn;
   DBusError err;
   DBusPendingCall* pending;
   int ret;
   int level;

   printf("Calling remote method with %d %d\n",param,param1);

   // initialiset the errors
   dbus_error_init(&err);

   // connect to the system bus and check for errors
   conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
   if (dbus_error_is_set(&err)) { 
      fprintf(stderr, "Connection Error (%s)\n", err.message); 
      dbus_error_free(&err);
   }
   if (NULL == conn) { 
      exit(1); 
   }

   // request our name on the bus
   ret = dbus_bus_request_name(conn, "test.client.caller", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
   if (dbus_error_is_set(&err)) { 
      fprintf(stderr, "Name Error (%s)\n", err.message); 
      dbus_error_free(&err);
   }

   // create a new method call and check for errors
   msg = dbus_message_new_method_call("test.server.source", // target for the method call
                                      "/test/method/Object", // object to call on
                                      "test.method.Type", // interface to call on
                                      "Method"); // method name
   if (NULL == msg) { 
      fprintf(stderr, "Message Null\n");
      exit(1);
   }

   // append arguments
   dbus_message_iter_init_append(msg, &args);
   if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, ¶m)) {
      fprintf(stderr, "Out Of Memory!\n"); 
      exit(1);
   }

   if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, ¶m1)) {
      fprintf(stderr, "Out Of Memory!\n"); 
      exit(1);
   }

   // send message and get a handle for a reply
   if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
      fprintf(stderr, "Out Of Memory!\n"); 
      exit(1);
   }
   if (NULL == pending) { 
      fprintf(stderr, "Pending Call Null\n"); 
      exit(1); 
   }
   dbus_connection_flush(conn);

   printf("Request Sent\n");

   // free message
   dbus_message_unref(msg);

   // block until we recieve a reply
   dbus_pending_call_block(pending);

   // get the reply message
   msg = dbus_pending_call_steal_reply(pending);
   if (NULL == msg) {
      fprintf(stderr, "Reply Null\n"); 
      exit(1); 
   }
   // free the pending message handle
   dbus_pending_call_unref(pending);

   // read the parameters
   if (!dbus_message_iter_init(msg, &args))
      fprintf(stderr, "Message has no arguments!\n"); 
   else
      dbus_message_iter_get_basic(&args, &level);

   printf("Got Reply:%d\n",level);

   // free reply and close connection
   dbus_message_unref(msg);   
  dbus_connection_close(conn);
}



int main(int argc, char **argv)
{
if (argc == 1)
        return -1;
else if(argc==3)
{
 int param = atoi(argv[1]);
 int param1 = atoi(argv[2]);
 caller(param,param1);
}
else
{ 
printf("Number of arguments should be 2 integers\n");
}
}

你可能感兴趣的:(ipc,dbus,嵌入式)