簡單的dbus使用

2021-08-20 22:46:43 字數 3065 閱讀 9395

首先安裝dbus的各種庫

具體步驟請在csdn上參考 別說鬱悶 寫的

再調整好所有的標頭檔案位置後即可編譯。

下面是簡單的首發例項,這個例項server從client讀取兩個整數並將其和返回給client

server:

#include #include #include #include #include #include #include #include #include void reply_to_method_call(dbusmessage* msg, dbusconnection* conn)

if ( dbus_message_is_method_call( msg, "test.method.type", "method" ) )

// create a reply from the message

reply = dbus_message_new_method_return(msg);

// add the arguments to the reply

fprintf(stderr, "out of memory!\n");

exit(1);

}// send the reply && flush the connection

if (!dbus_connection_send(conn, reply, &sum))

dbus_connection_flush(conn);

// free the reply

dbus_message_unref(reply);

}int main()

if (null == conn)

// 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))

// loop, testing for new messages

while (true)

if ( dbus_message_has_inte***ce(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)

if (null == conn)

// 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))

// 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", // inte***ce to call on

"method"); // method name

if (null == msg)

fprintf(stderr, "out of memory!\n");

exit(1);

}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))

if (null == pending)

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)

// 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)

else

}

dbus的資料型別

dbus用xml描述介面,例如 其實前兩講已經看過很多例子了。node就是介面中的物件,node可以包含node,構成物件樹。dbus的介面描述檔案統一採用utf 8編碼。我相信讀者很容易理解這個介面描述檔案。我只想解釋一下描述引數資料型別的type域。dbus的資料型別是由 s 或 a 這樣的型別...

基於Go的D Bus框架(二)

d bus 核心定義了一套自省機制,這個自省跟物件導向裡的自省有點類似,即可以在執行時查詢物件的相關資訊,包括介面和屬性等。大部分支援 d bus 的程式都實現了 org.freedesktop.dbus.introspectable 這個標準介面,為它們匯出的物件提供自省支援。這個介面只有乙個方法...

dbus在windows上的編譯

author kagula date 2015 09 16 1 expat win32bin 2.1.0.exe 2 dbus 1.6.8.tar.gz 準備 1 先安裝expat win32bin 2.1.0.exe,我這裡的安裝路徑是 d expat 2.1.0 方式一 1 參考資料一,在 ad...