D-Bus Learning (4): basic example (synchronous and asynchronous)

Source: Internet
Author: User

 

After learning the basic concepts from Tutorial, we will enter the actual practice stage. Through learning, we will provide a small connection example, a synchronization example, and an asynchronous example.

Ing between types and gtypes

In D-Bus programming, the following table lists the mappings between basic types and gtypes. In the following small program example, we will see how it corresponds.

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

In D-Bus programming, the container type and GType ing table is as follows:

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 CT 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

In D-Bus, the dbus-1 dbus-glib-1 glib-2.0 is used (take Moblin 2.1 as an example ). The example of Makefile is as follows:

CC = gcc

CFLAGS + =-Wall-g'Pkg-config dbus-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)

Synchronization example

Synchronous means that the program sends a method call message, waiting for the method_return message. The following is a small example. If we use the send-send command, we can use:

Sending-send -- session -- print-reply -- type = method_call -- dest = org. freedesktop. Events/org. freedesktop. Rule. Introspectable. Introspect

I learned it in my previous study. It returns the type of GType G_TYPE_STRING, which corresponds to char * in the C program *.

# Include <stdio. h>

# Include <stdlib. h>

# Include <alias/dbus-glib.h>

Int main (int argc, char ** argv)

{

GError * error;

DBusGConnection * connection;

DBusGProxy * proxy;

Char * string;

/* GType initialization */

G_type_init ();

Error = NULL;

/* Dbus_g_bus_get is used to establish a connection. Here it is connected to the session bus, or it can be connected to the system bus through DBUS_BUS_SYSTEM */

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 is used to represent the remote org. freedesktop. configurations system. You can use DBUS_INTERFACE_INTROSPECTABLE and other definitions to identify it */

Proxy = dbus_g_proxy_new_for_name

(Connection,

"Org. freedesktop. specifications"

/* Service */

,

"/"

/*

Path */

,

"Org. freedesktop. Rule. Introspectable"

/* Interface. You can use a macro to define DBUS_INTERFACE_INTROSPECTABLE */

);

Error = NULL;

/* Adopt synchronous mode. The second parameter is method, the third parameter is error, and the following parameter is an indefinite parameter. The input is displayed, and the result is output. It is received with G_TYPE_INVALID, each parameter consists of the GType type and the address of the stored parameter value. In this example, the input does not have a parameter. The input has a parameter, which is 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;

}

Asynchronous example

In asynchronous mode, the program will trigger a callback function when a message is returned, instead of returning the message. The following is the same operation, but it is implemented in asynchronous mode:

/* Test for invoke method asynchronously */

# Include <stdio. h>

# Include <stdlib. h>

# Include <alias/dbus-glib.h>

Static GMainLoop * main_loop;

/* The following is the callback function. The function is triggered when the method return message is received */

Static void my_callback_func (dbusgproxy * proxy, dbusgproxycall * call_id, void * user_data)

{

Gerror * error = NULL;

Gchar * string = NULL;

/* End sending and receiving a message, process the received message, and obtain the returned value or error message */

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. writable */

Proxy =Dbus_g_proxy_new_for_name

(Connection, "org. freedesktop. specifications", "/", dbus_interface_introspectable );

/* Asynchronous triggering. You can also set a time-out limit to use dbus_g_proxy_call_with_timeout.

. The parameters here only need to be input. The fourth parameter is user_data that carries the callback function. The fifth parameter identifies the function for releasing user_data, such as 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;

}

 

In the preceding example, GMainLoop is used. For thread D-Bus, dbus_g_thread_init is also provided.

.

Related Links: My Linux articles

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.