1、簡介
DBus的出現,使得Linux進程間通訊更加便捷,不只能夠和用戶空間應用程序進行通訊,並且還能夠和內核的程序進行通訊,DBus使得Linux變得更加智能,更加具備交互性。
DBus分爲兩種類型:system bus(系統總線),用於系統(Linux)和用戶程序之間進行通訊和消息的傳遞;session bus(回話總線),用於桌面(GNOME, KDE等)用戶程序之間進行通訊。 html
2、詳解之Qt代碼
一、代碼一
(1)test.hlinux
- #ifndef TEST_H
- #define TEST_H
-
- #include <QtCore>
- #include <QTimer>
-
- class Test : public QObject
- {
- Q_OBJECT
- public:
- Test();
-
- public slots:
- QString testStart();
- void changeTest();
-
- signals:
- void stateChange(QString str);
-
- private:
- QTimer *timer;
- };
- #endif /*TEST_H*/
(2)test.cpp服務器
- #include "test.h"
-
- Test::Test()
- {
- qDebug() << "===========test init===========";
- timer = new QTimer;
- connect(timer, SIGNAL(timeout()), this, SLOT(changeTest()));
- }
-
- QString Test::testStart()
- {
- qDebug() << "+++++++QtDBus test++++++++++";
- QString tmp;
- tmp = QString("OFF");
- qDebug() << tmp;
- if (timer->isActive()) {
- timer->stop();
- }
- timer->start(2000);
- return tmp;
- }
-
- void Test::changeTest()
- {
- QString tmp;
- tmp = QString("ON");
- qDebug() << "+++++++++++++++++++" << tmp;
- emit stateChange(tmp);
- }
(3)test_adaptor.hsession
- #include "test_adaptor.h"
- #include <QtCore>
-
- TestInterfaceAdaptor::TestInterfaceAdaptor(QObject *parent)
- :QDBusAbstractAdaptor(parent)
- {
- qDebug() << "***************TestInterfaceAdaptor::TestInterfaceAdaptor**************";
- setAutoRelaySignals(true); //connection of the signals on the parent
- }
-
- TestInterfaceAdaptor::~TestInterfaceAdaptor()
- {
- }
-
- QString TestInterfaceAdaptor::test()
- {
- QString out;
- QMetaObject::invokeMethod(parent(), "testStart",Q_RETURN_ARG(QString, out));
- qDebug() << "==========" << out;
- return out;
- }
(4)test_adaptor.cppapp
- #ifndef TEST_ADAPTOR_H
- #define TEST_ADAPTOR_H
-
- #include <QtCore/QObject>
- #include <QtDBus/QtDBus>
- /****
- **dbus-send --session --print-reply --dest=com.asianux.btagent / com.asianux.btagent.adaptor.test
- **dbus-monitor --session \ "type='signal',interface='com.asianux.btagent.adaptor',member='stateChange'"
- *****/
- class TestInterfaceAdaptor : public QDBusAbstractAdaptor
- {
- Q_OBJECT
- Q_CLASSINFO("D-Bus Interface", "com.asianux.btagent.adaptor")
-
- Q_CLASSINFO("D-Bus Introspection", ""
- " <interface name=\"com.asianux.btagent.adaptor\">\n"
- " <method name=\"test\">\n"
- " <arg name=\"state\" type=\"s\" direction=\"out\"/>\n"
- " </method> \n"
- " <signal name=\"stateChange\"> \n"
- " <arg type=\"s\" direction=\"out\"/>\n"
- " </signal> \n"
- " </interface>\n"
- "")
-
- public:
- TestInterfaceAdaptor(QObject *parent);
- virtual ~TestInterfaceAdaptor();
-
- public:
- public slots:
- QString test();
-
- signals:
- void stateChange(QString str);
- };
-
- #endif /*TEST_ADAPTOR_H*/
(5)main.cpp異步
- #include <QApplication>
- #include <QtDBus>
- #include <QDebug>
- #include "test_adaptor.h"
- #include "test.h"
-
- int main(int argc,char *argv[])
- {
- QApplication app(argc,argv);
-
- Test *test = new Test();
-
- new TestInterfaceAdaptor(test);
- QDBusConnection conn = QDBusConnection::sessionBus();
- conn.registerObject("/",test);
- conn.registerService("com.asianux.btagent");
-
- return app.exec();
- }
(6)運行函數
能夠在linux終端發送(dbus-send)和監控dbus(dbus-monitor)的信息。 dbus-send調用遠程方法的通常形式是:$ dbus-send [--system | --session] --type=method_call --print-reply --dest=鏈接名 對象路徑 接口名.方法名 參數類型:參數值 參數類型:參數值,dbus-send支持的參數類型包括:string, int32, uint32, double, byte, boolean。
啓動程序後,先執行:dbus-send --session --print-reply --dest=com.asianux.btagent / com.asianux.btagent.adaptor.test,發送dbus信號,獲得輸出結果:
而後輸入:dbus-monitor --session \ "type='signal',interface='com.asianux.btagent.adaptor',member='stateChange'",監控,從應用程序發出的DBus信號:
也能夠經過qt自帶的工具qdbusviewer查看和操做相應的DBus信號:
工具
(7)除了上述方法,也可使用glib的程序進行DBus通訊。
main.c:oop
- #include <stdio.h>
- #include <stdlib.h>
- #include <glib.h>
- #include <dbus/dbus-glib.h>
- #include <dbus/dbus.h>
-
- static void bt_manager_changed_cb (DBusGProxy *proxy,
- const gchar *state,
- gpointer user_data)
- {
- printf("state = %s\n",state);
- }
-
- int main(int argc,char *argv[])
- {
- GMainLoop *loop = g_main_loop_new(NULL,TRUE);
- g_type_init();
- GError * error = NULL;
-
- DBusGConnection *gconn = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
- DBusGProxy *m_proxy = dbus_g_proxy_new_for_name(gconn, "com.asianux.btagent","/","com.asianux.btagent.adaptor");
-
- char *str = NULL;
- dbus_g_proxy_call(m_proxy, "test", &error,
- G_TYPE_INVALID,
- G_TYPE_STRING,&str,
- G_TYPE_INVALID);
-
- dbus_g_proxy_add_signal (m_proxy,
- "stateChange",
- G_TYPE_STRING,
- G_TYPE_INVALID);
-
- dbus_g_proxy_connect_signal (m_proxy,
- "stateChange",
- G_CALLBACK (bt_manager_changed_cb),
- NULL,
- NULL);
- printf("str = %s\n",str);
- g_main_loop_run(loop);
-
- }
makefile:學習
- all:
- gcc -g main.c -o test `pkg-config --cflags --libs dbus-1 gthread-2.0 glib-2.0 dbus-glib-1`
- clean:
- rm -rf *.o test
運行結果(先啓動最上的服務器qt程序):
二、代碼二
(1)qdbus.h
- #ifndef QDBUS
- #define QDBUS
- #include <QtCore>
- /*dbus-send --session --print-reply --dest=com.asianux.btagent2 / com.asianux.btagent2.interface.slotInterface string:"helloworld"*/
- class DeviceManager : public QObject
- {
- Q_OBJECT
- Q_CLASSINFO("D-Bus Interface", "com.asianux.btagent2.interface")
-
- public:
- // DeviceManager(){}
- // ~DeviceManager(){}
- public slots:
- void slotInterface(QString);
- };
- #endif // QDBUS
(2)main.cpp
- #include <QCoreApplication>
- #include <QObject>
- #include <QtDBus>
- #include "qdbus.h"
-
- void DeviceManager::slotInterface(QString str)
- {
- qDebug() << "D-Bus Interface(com.asianux.btagent2.interface):" << str;
- }
-
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QDBusConnection bus = QDBusConnection::sessionBus();
- if(!bus.registerService("com.asianux.btagent2")){
- qDebug() << bus.lastError().message();
- exit(1);
- }
- DeviceManager *deviceManager = new DeviceManager();
- bus.registerObject("/", deviceManager, QDBusConnection::ExportAllSlots);
- return a.exec();
- }
運行結果:
三、其餘網上代碼
D-Bus的QT4綁定
下面,咱們經過一個實例來介紹D-Bus的QT4綁定。(參見hotel.pro)
在Session bus上建立一個"com.test.hotel" service,經過這個service能夠執行check in,check out和query三個動做。
建立Service而且註冊Object:
// 用於創建到session bus的鏈接 QDBusConnection bus = QDBusConnection::sessionBus(); // 在session bus上註冊名爲"com.test.hotel"的service if (!bus.registerService("com.test.hotel")){ qDebug()<< bus.lastError().message(); exit(1); } Hotel my_hotel; // 註冊名爲"/hotel/registry"的object。 // "QDBusConnection::ExportAllSlots" // 表示把類Hotel的全部Slot都導出爲這個Object的method bus.registerObject("/hotel/registry",&my_hotel, QDBusConnection::ExportAllSlots); return a.exec(); } |
再看一下Hotel類的定義。
class Hotel:public QObject { Q_OBJECT // 定義Interface名稱爲"com.test.hotel.registry" Q_CLASSINFO("D-Bus Interface","com.test.hotel.registry") public: Hotel() { m_rooms = MAX_ROOMS;} public slots: // Check in,參數爲房間數,返回成功拿到的房間數 int checkIn(int num_room); // Check out,參數爲房間數,返回成功退回的房間數 int checkOut(int num_room); // Query,用於查詢目前還剩下的房間數 int query(); private: int m_rooms; QReadWriteLock m_lock; }; |
運行這個程序,咱們可使用qdbusviewer查看和操做這個Object。
經過QDBusMessage訪問Service
在QT4中,用QDBusMessage表示在D-Bus上發送和接收的Message。(參見checkin.pro)
// 用來構造一個在D-Bus上傳遞的Message QDBusMessage m = QDBusMessage::createMethodCall("com.test.hotel", "/hotel/registry", "com.test.hotel.registry", "checkIn"); if (argc== 2){ // 給QDBusMessage增長一個參數; // 這是一種比較友好的寫法,也能夠用setArguments來實現 m << QString(argv[1]).toInt(); } // 發送Message QDBusMessage response = QDBusConnection::sessionBus().call(m); // 判斷Method是否被正確返回 if (response.type()== QDBusMessage::ReplyMessage){ // QDBusMessage的arguments不只能夠用來存儲發送的參數,也用來存儲返回值; // 這裏取得checkIn的返回值 int num_room = response.arguments().takeFirst().toInt(); printf("Got %d %s\n", num_room,(num_room> 1)?"rooms": "room"); } else { fprintf(stderr,"Check In fail!\n"); } |
經過QDBusInterface 訪問Service
在QT4中,QDBusInterface能夠更加方便的訪問Service。(參見checkin2.pro)
// 建立QDBusInterface QDBusInterface iface( "com.test.hotel", "/hotel/registry", "com.test.hotel.registry", QDBusConnection::sessionBus()); if (!iface.isValid()){ qDebug()<< qPrintable(QDBusConnection::sessionBus(). lastError().message()); exit(1); } // 呼叫遠程的checkIn,參數爲num_room QDBusReply<int> reply= iface.call("checkIn", num_room); if (reply.isValid()){ num_room = reply.value(); printf("Got %d %s\n", num_room,(num_room> 1)?"rooms": "room"); } else { fprintf(stderr,"Check In fail!\n"); } |
看,用QDBusInterface來訪問Service是否是更加方便?
從D-Bus XML自動生成Proxy類
用QDB usInterface訪問Service已經很是方便了,但還不夠直觀。還有沒有更直觀的方法,就像訪問本地類成員變量的方式訪問遠程的method?答案是Proxy。
Proxy Object提供了一種更加直觀的方式來訪問Service,就好像調用本地對象的方法同樣。
歸納的說,達成上述目標須要分三步走:
(1)使用工具qdbuscpp2xml從hotel.h生成XML文件;
qdbuscpp2xml -M hotel.h -o com.test.hotel.xml
(2)使用工具qdbusxml2cpp從XML文件生成繼承自QDBusInterface的類;
qdbusxml2cpp com.test.hotel.xml -i hotel.h -p hotelInterface
這條命令會生成兩個文件:hotelInterface.cpp和hotelInterface.h
(3)調用(2)生成的類來訪問Service。
下面是舉例(參見checkin3.pro ):
// 初始化自動生成的Proxy類com::test::hotel::registry com::test::hotel::registry myHotel("com.test.hotel", "/hotel/registry", QDBusConnection::sessionBus()); // 調用checkIn QDBusPendingReply<int> reply= myHotel.checkIn(num_room); // qdbusxml2cpp生成的Proxy類是採用異步的方式來傳遞Message, // 因此在此須要調用waitForFinished來等到Message執行完成 reply.waitForFinished(); if (reply.isValid()){ num_room = reply.value(); printf("Got %d %s\n", num_room,(num_room> 1)?"rooms": "room"); } else { fprintf(stderr,"Check In fail!\n"); }; |
使用Adapter註冊Object
如前文所述,咱們能夠直接把class Hotel註冊爲Message Bus上的一個Object,但這種方式並非QT4所推薦的。QT4推薦使用Adapter來註冊Object。
不少狀況下,咱們可能只須要把咱們定義的類裏的方法有選擇的發佈到Message Bus上,使用Adapter能夠很方便的實現這種意圖。
之前文中的Hotel爲例,假設咱們只須要把checkIn和checkOut發佈到Message Bus上,應該怎麼辦?
(1)使用工具 qdbuscpp2xml從hotel.h生成XML文件;
qdbuscpp2xml -M hotel.h -o com.test.hotel.xml
(2)編輯com.test.hotel.xml,把其中的query部分去掉;
即去掉如下三條語句:
<method name="query">
<arg type="i" direction="out"/>
</method>
(3)使用工具qdbusxml2cpp從XML文件生成繼承自QDBusInterface的類;
qdbusxml2cpp com.test.hotel.xml -i hotel.h -a hotelAdaptor
這條命令會生成兩個文件:hotelAdaptor.cpp和hotelAdaptor.h
(4)調用(3)生成的類來註冊Object。(參見hotel2.pro)
int main(int argc,char*argv[]) { QCoreApplication a(argc, argv); QDBusConnection bus = QDBusConnection::sessionBus(); Hotel myHotel; // RegistryAdaptor是qdbusxml2cpp生成的Adaptor類 RegistryAdaptor myAdaptor(&myHotel); if (!bus.registerService("com.test.hotel")){ qDebug()<< bus.lastError().message(); exit(1); } bus.registerObject("/hotel/registry",&myHotel); return a.exec(); } |
運行這個應用程序,咱們從qdbusviewer上能夠看到,只有checkIn和checkOut兩個method被髮布。
自動啓動Service
D-Bus系統提供了一種機制能夠在訪問某個service時,自動把該程序運行起來。
咱們須要在/usr/share/dbus-1/services下面創建com.test.hotel.service文件,文件的內容以下:
[D-BUS Service]
Name=com.test.hotel
Exec=/path/to/your/hotel
這樣,咱們在訪問Hotel的method以前,就沒必要手動運行該應用程序了。
四、其餘細節
(1)若是想使用system bus(自啓動服務參考上述),須要在/etc/dbus-1/system.d/目錄下建立一個配置文件my.conf:
- <!DOCTYPE busconfig PUBLIC
- "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
- "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
- <busconfig>
- <policy context="default">
- <allow own="com.asianux.btagent2"/>
- <allow send_destination="com.asianux.btagent2"/>
- <allow receive_sender="com.asianux.btagent2"/>
- </policy>
- </busconfig>
(2)能夠參考經過QDbus實現的Qt檢測U盤的例子:http://download.csdn.net/detail/taiyang1987912/8686677
(3)除了DBus,也可以使用SIGHUP信號用於進程間通訊,好比重寫了配置文件,又不想重啓程序就讓配置生效,能夠往該程序的進程發送一個SIGHUP信號:killall -HUP <進程名稱>或kill -HUP <進程號>,可能因之前的系統沒有提供用戶自定義信號 SIGUSR1 和 SIGUSR1 ,而對於一個沒有終端的守護進程來講是不可能收到 SIGHUP 信號的,因此就把 SIGHUP 當用戶自定義信號使用。
- #include<stdio.h>
- #include <stdlib.h>
- #include<signal.h>
- char**args;
-
- void exithandle(int sig)
- {
- printf("%s:(%d)sighup received\n", args[0], sig);
- exit(0);
- }
-
- int main(int argc,char **argv)
- {
- args = argv;
- signal(SIGHUP,exithandle);
- while(1) sleep(1);
- return 0;
- }
運行程序,打開另終端發送killall -HUP ./sighupcode,則會處理SIGHUP信號:
3、詳解之C代碼
一、代碼
使用C語言調用dbus的底層函數編寫一個遠程調用的示例代碼,代碼很簡單,沒使用GObject等一些複雜的庫。
(1)method_send.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dbus/dbus-glib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- /*gcc -o method_send method_send.c -I/usr/include/glib-2.0 -I/usr/include/dbus-1.0 -I/usr/lib64/glib-2.0/include -I/usr/lib64/dbus-1.0/include -lglib-2.0 -ldbus-glib-1*/
- void reply_to_method_call(DBusMessage * msg, DBusConnection * conn)
- {
- DBusMessage * reply;
- DBusMessageIter arg;
- char * param = NULL;
- dbus_bool_t stat = TRUE;
- dbus_uint32_t level = 2010;
- dbus_uint32_t serial = 0;
-
- //從msg中讀取參數,這個在上一次學習中學過
- if(!dbus_message_iter_init(msg,&arg))
- printf("Message has noargs\n");
- else if(dbus_message_iter_get_arg_type(&arg)!= DBUS_TYPE_STRING)
- printf("Arg is notstring!\n");
- else
- dbus_message_iter_get_basic(&arg,& param);
- if(param == NULL) return;
- //建立返回消息reply
- reply = dbus_message_new_method_return(msg);
- //在返回消息中填入兩個參數,和信號加入參數的方式是同樣的。此次咱們將加入兩個參數。
- dbus_message_iter_init_append(reply,&arg);
- if(!dbus_message_iter_append_basic(&arg,DBUS_TYPE_BOOLEAN,&stat)){
- printf("Out ofMemory!\n");
- exit(1);
- }
- if(!dbus_message_iter_append_basic(&arg,DBUS_TYPE_UINT32,&level)){
- printf("Out ofMemory!\n");
- exit(1);
- }
- //發送返回消息
- if( !dbus_connection_send(conn, reply,&serial)){
- printf("Out of Memory\n");
- exit(1);
- }
- dbus_connection_flush (conn);
- dbus_message_unref (reply);
- }
-
- void listen_dbus()
- {
- DBusMessage * msg;
- DBusMessageIter arg;
- DBusConnection * connection;
- DBusError err;
- int ret;
- char * sigvalue;
- dbus_error_init(&err);
- //建立於session D-Bus的鏈接
- connection =dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)){
- fprintf(stderr,"ConnectionError %s\n",err.message);
- dbus_error_free(&err);
- }
- if(connection == NULL)
- return;
- //設置一個BUS name:test.wei.dest
- ret =dbus_bus_request_name(connection,"test.wei.dest",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
- if(dbus_error_is_set(&err)){
- fprintf(stderr,"Name Error%s\n",err.message);
- dbus_error_free(&err);
- }
- if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
- return;
- //要求監聽某個singal:來自接口test.signal.Type的信號
- dbus_bus_add_match(connection,"type='signal',interface='test.signal.Type'",&err);
- dbus_connection_flush(connection);
- if(dbus_error_is_set(&err)){
- fprintf(stderr,"Match Error%s\n",err.message);
- dbus_error_free(&err);
- }
- while(1){
- dbus_connection_read_write(connection,0);
- msg =dbus_connection_pop_message (connection);
-
- if(msg == NULL){
- sleep(1);
- continue;
- }
-
- if(dbus_message_is_signal(msg,"test.signal.Type","Test")){
- if(!dbus_message_iter_init(msg,&arg))
- fprintf(stderr,"Message Has no Param");
- else if(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_STRING)
- g_printerr("Param isnot string");
- else
- dbus_message_iter_get_basic(&arg,&sigvalue);
- }else if(dbus_message_is_method_call(msg,"test.method.Type","Method")){
- //咱們這裏面先比較了接口名字和方法名字,實際上應當現比較路徑
- if(strcmp(dbus_message_get_path(msg),"/test/method/Object") == 0)
- reply_to_method_call(msg,connection);
- }
- dbus_message_unref(msg);
- }
- }
-
- int main( int argc , char ** argv)
- {
- listen_dbus();
- return 0;
- }
(2)method_recv.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dbus/dbus-glib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- /*gcc -o method_recv method_recv.c -I/usr/include/glib-2.0 -I/usr/include/dbus-1.0 -I/usr/lib64/glib-2.0/include -I/usr/lib64/dbus-1.0/include -lglib-2.0 -ldbus-glib-1*/
- //創建與session D-Bus daemo的鏈接,並設定鏈接的名字,相關的代碼已經屢次使用過了
- DBusConnection * connect_dbus()
- {
- DBusError err;
- DBusConnection * connection;
- int ret;
-
- //Step 1: connecting session bus
- dbus_error_init(&err);
-
- connection =dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)){
- fprintf(stderr,"ConnectionErr : %s\n",err.message);
- dbus_error_free(&err);
- }
- if(connection == NULL)
- return NULL;
- //step 2: 設置BUS name,也即鏈接的名字。
- ret =dbus_bus_request_name(connection,"test.wei.source",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
- if(dbus_error_is_set(&err)){
- fprintf(stderr,"Name Err :%s\n",err.message);
- dbus_error_free(&err);
- }
- if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
- return NULL;
-
- return connection;
- }
-
- void send_a_method_call(DBusConnection * connection,char * param)
- {
- DBusError err;
- DBusMessage * msg;
- DBusMessageIter arg;
- DBusPendingCall * pending;
- dbus_bool_t * stat;
- dbus_uint32_t * level;
-
- dbus_error_init(&err);
- //針對目的地地址,請參考圖,建立一個method call消息。Constructs a new message to invoke a method on a remote object.
- msg =dbus_message_new_method_call ("test.wei.dest","/test/method/Object","test.method.Type","Method");
- if(msg == NULL){
- g_printerr("MessageNULL");
- return;
- }
-
- //爲消息添加參數。Appendarguments
- dbus_message_iter_init_append(msg, &arg);
- if(!dbus_message_iter_append_basic(&arg, DBUS_TYPE_STRING,&stat)){
- g_printerr("Out of Memory!");
- exit(1);
- }
-
- //發送消息並得到reply的handle。Queues amessage to send, as withdbus_connection_send() , but also returns aDBusPendingCall used to receive a reply to the message.
- if(!dbus_connection_send_with_reply (connection, msg,&pending, -1)){
- g_printerr("Out of Memory!");
- exit(1);
- }
-
- if(pending == NULL){
- g_printerr("Pending CallNULL: connection is disconnected ");
- dbus_message_unref(msg);
- return;
- }
-
- dbus_connection_flush(connection);
- dbus_message_unref(msg);
-
- //waiting a reply,在發送的時候,已經獲取了methodreply的handle,類型爲DBusPendingCall。
- // block until we recieve a reply, Block until the pendingcall is completed.
- dbus_pending_call_block (pending);
- //get the reply message,Gets thereply, or returns NULL if none has been received yet.
- msg =dbus_pending_call_steal_reply (pending);
- if (msg == NULL) {
- fprintf(stderr, "ReplyNull\n");
- exit(1);
- }
- // free the pendingmessage handle
- dbus_pending_call_unref(pending);
- // read the parameters
- if(!dbus_message_iter_init(msg, &arg))
- fprintf(stderr, "Message hasno arguments!\n");
- else if (dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_BOOLEAN)
- fprintf(stderr, "Argument isnot boolean!\n");
- else
- dbus_message_iter_get_basic(&arg, &stat);
-
- if (!dbus_message_iter_next(&arg))
- fprintf(stderr, "Message hastoo few arguments!\n");
- else if (dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_UINT32 )
- fprintf(stderr, "Argument isnot int!\n");
- else
- dbus_message_iter_get_basic(&arg, &level);
-
- printf("Got Reply: %d,%d\n", stat, level);
- dbus_message_unref(msg);
- }
- int main( int argc , char ** argv)
- {
- DBusConnection * connection;
- connection = connect_dbus();
- if(connection == NULL)
- return -1;
-
- send_a_method_call(connection,"Hello, D-Bus");
- return 0;
- }
(3)編譯運行(先運行method_send)
編譯method_send.c:
編譯method_recv.c(獲得運行結果):
4、總結
(1)本文僅提供代碼的測試,其餘的具體函數的意義請查閱相應的幫助文檔。
(2)源碼已經打包上傳到csdn上,可登陸下載(http://download.csdn.net/detail/taiyang1987912/8687183)。
(3)如有建議,請留言,在此先感謝!
http://blog.csdn.net/taiyang1987912/article/details/45642079