從Tutorial中學(xué)習(xí)基本概念后,進(jìn)入實(shí)戰(zhàn)階段,通過學(xué)習(xí),我們給出連個(gè)小例子,同步例子和異步例子。
類型和GType的映射
在D-Bus編程中,基礎(chǔ)類型和GType的映射表格如下。在后面的程序小例子中我們會(huì)看到具體如何對(duì)應(yīng)。
D-Bus basic type GType Free function Notes
BYTE G_TYPE_BOOLEAN INT16 G_TYPE_INT Will be changed to a G_TYPE_INT16 once GLib has it UINT16 G_TYPE_UINT Will be changed to a G_TYPE_UINT16 once GLib has it INT32 G_TYPE_INT Will be changed to a G_TYPE_INT32 once GLib has it UINT32 G_TYPE_UINT Will be changed to a G_TYPE_UINT32 once GLib has it INT64 G_TYPE_GINT64 UINT64 G_TYPE_GUINT64 DOUBLE G_TYPE_DOUBLE STRING G_TYPE_STRING g_free OBJECT_PATH DBUS_TYPE_G_PROXY g_object_unref The returned proxy does not have an interface set; use dbus_g_proxy_set_interface to invoke methods 在D-Bus編程中,container類型和GType的映射表格如下:
D-Bus type signature Description GType C typedef Free function Notes
as Array of strings G_TYPE_STRV char ** g_strfreev v Generic value container G_TYPE_VALUE GValue * g_value_unset The calling conventions for values expect that method callers have allocated return values; see below. a{ss} Dictionary mapping strings to strings DBUS_TYPE_G_STRING_ STRING_HASHTABLE GHashTable * g_hash_table_destroy Makefile
在D-Bus中,使用了dbus-1 dbus-glib-1 glib-2.0(以Moblin 2.1為例)。Makefile的例子如下:
CC = gcc
CFLAGS += -Wall -g `pkg-config dbus-1 dbus-glib-1 glib-2.0 --cflags` LIBS += -Wall -g `pkg-config dbus-1 dbus-glib-1 glib-2.0 --libs` TARGET = sample
OBJ = $(TARGET).o all: $(TARGET)
%o: %c
$(CC) $(CFLAGS) -c $< -o $@ $(TARGET): $(OBJ)
$(CC) $(LIBS) -o $(TARGET) $(OBJ) 同步的例子
同步即程序發(fā)出method call消息,等待method_return消息。下面是一個(gè)小例子,如果我們用dbus-send命令,可以使用:
dbus-send --session --print-reply --type=method_call --dest=org.freedesktop.Notifications / org.freedesktop.DBus.Introspectable.Introspect
在上一次學(xué)習(xí)中學(xué)習(xí)過。它的返回GType類型為G_TYPE_STRING,在C程序中對(duì)應(yīng)char *。
#include <stdio.h>
#include <stdlib.h> #include <dbus/dbus-glib.h> int main( int argc , char ** argv)
{ GError * error; DBusGConnection * connection; DBusGProxy * proxy; char * string; /* GType初始化 */
g_type_init(); error = NULL; /* dbus_g_bus_get用來建立連接,這里和session bus連接,也可以通過DBUS_BUS_SYSTEM與系統(tǒng)總線連接*/
connection = dbus_g_bus_get (DBUS_BUS_SESSION, & error); if(connection == NULL){ g_printerr ("Failed to open connection to bus : %s\n",error->message); g_error_free( error); exit( 1 ); } /* Create a proxy object用來代表遠(yuǎn)端org.freedesktop.Notifications是系統(tǒng)帶有的,可以使用DBUS_INTERFACE_INTROSPECTABLE等定義來標(biāo)識(shí)它*/
proxy = dbus_g_proxy_new_for_name (connection, "org.freedesktop.Notifications" /* service */ , "/" /* path */ , "org.freedesktop.DBus.Introspectable" /* interface,可以使用宏定義DBUS_INTERFACE_INTROSPECTABLE */ ); error = NULL;
/* 采用同步方式,第二個(gè)參數(shù)是method,第三個(gè)參數(shù)是error,后面是不定長(zhǎng)的參數(shù),顯示輸入,后為輸出,以G_TYPE_INVALID標(biāo)識(shí)接收,每個(gè)參數(shù)由GType類型和存貯參數(shù)值的地址構(gòu)成。在這個(gè)例子中,輸入沒有參數(shù),輸入有一個(gè)參數(shù),是string*/
if( !dbus_g_proxy_call (proxy,"Introspect",&error, G_TYPE_INVALID,G_TYPE_STRING, &string, G_TYPE_INVALID) ){ if(error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) g_printerr("Caught remote method exception %s:%s",dbus_g_error_get_name(error),error->message); else g_printerr("Error : %s\n", error->message); g_error_free(error); exit (1); } g_print("Message Method return from bus:\n%s\n",string);
g_free(string);
g_object_unref(proxy); return 0; } 異步的例子
異步中,程序?qū)⒉坏确祷叵?,繼續(xù)執(zhí)行,等有返回消息的時(shí)候,觸發(fā)一個(gè)回調(diào)函數(shù) 。下面是同樣的操作,但是用異步的方式來實(shí)現(xiàn):
/* Test for invoke method asynchronously */
#include <stdio.h> #include <stdlib.h> #include <dbus/dbus-glib.h> static GMainLoop * main_loop;
/* 下面是回調(diào)函數(shù),收到method return消息觸發(fā)函數(shù) */
static void my_callback_func (DBusGProxy *proxy, DBusGProxyCall *call_id, void *user_data)
{ GError * error = NULL; gchar * string = NULL; /* 結(jié)束一個(gè)消息的收發(fā),處理收到的消息,獲取返回值或者error信息 */ dbus_g_proxy_end_call (proxy,call_id,&error,G_TYPE_STRING,&string,G_TYPE_INVALID); if(error != NULL){ g_print("Error in method call : %s\n", error->message); g_error_free(error); }else{ g_print("SUCCESS,it is now %s\n",string); } g_main_loop_quit(main_loop); } int main( int argc , char ** argv)
{ GError * error = NULL; DBusGConnection * connection; DBusGProxy * proxy; g_type_init(); main_loop = g_main_loop_new(NULL,TRUE); connection = dbus_g_bus_get (DBUS_BUS_SESSION, & error);
if(connection == NULL){ g_printerr ("Failed to open connection to bus : %s\n", error->message); g_error_free( error); exit( 1 ); } /* Create a proxy object for the 'bus driver' named org.freedesktop.DBus */
proxy = dbus_g_proxy_new_for_name (connection, "org.freedesktop.Notifications", "/", DBUS_INTERFACE_INTROSPECTABLE); /* 異步觸發(fā),也可以帶上一個(gè)超時(shí)的時(shí)間限制,使用dbus_g_proxy_call_with_timeout 。這里的參數(shù)只需帶上輸入的情況。第四個(gè)參數(shù)為攜帶到回調(diào)函數(shù)的user_data,第五個(gè)參數(shù)標(biāo)識(shí)釋放user_data的函數(shù),例如g_free等*/ dbus_g_proxy_begin_call (proxy,"Introspect",my_callback_func, NULL,NULL, G_TYPE_INVALID); g_main_loop_run(main_loop); return 0; } 在上面的例子中,使用了GMainLoop的方式,對(duì)于線程D-Bus還提供了dbus_g_thread_init 來處理。
本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/flowingflying/archive/2010/03/26/5418180.aspx |
|