Documentation/Maemo 5 Developer Guide/DBus/Implementing and Using D-Bus Signals

(Declaring Signals in Interface XML)
(add example links)
 
(6 intermediate revisions not shown)
Line 1: Line 1:
-
=Implementing and using D-Bus signals =
 
-
 
== D-Bus Signal properties ==
== D-Bus Signal properties ==
 +
Performing remote method invocations over the D-Bus is only one half of D-Bus capabilities. As was noted before, D-Bus also supports a ''broadcast'' method of communication, which is ''asynchronous''. This mechanism is called a ''signal'' (in D-Bus terminology), and it is useful when several receivers must be notified about a state change that could affect them. Some examples where signals could be useful are notifying a lot of receivers, when the system is being shut down, network connectivity has been lost, and similar system-wide conditions. This way, the receivers do not need to poll for the status continuously.
Performing remote method invocations over the D-Bus is only one half of D-Bus capabilities. As was noted before, D-Bus also supports a ''broadcast'' method of communication, which is ''asynchronous''. This mechanism is called a ''signal'' (in D-Bus terminology), and it is useful when several receivers must be notified about a state change that could affect them. Some examples where signals could be useful are notifying a lot of receivers, when the system is being shut down, network connectivity has been lost, and similar system-wide conditions. This way, the receivers do not need to poll for the status continuously.
-
However, signals are not the solution to all problems. If a recipient is not processing its D-Bus messages quickly enough (or there just are too many), a signal might get lost on its way to the recipient. Other complications can also occur, as with any RPC mechanism. For these reasons, if the application requires extra reliability, you need to think how to arrange it. One possibility is to occasionally check the state that the application is interested in, assuming it can be checked over the D-Bus. This, however, should not be done too often; the recommended interval is once a minute or less than that, and only when the application is already active for other reasons. This model leads to a reduction in battery life, so its use should be carefully weighed.
+
However, signals are not the solution to all problems. If a recipient is not processing its D-Bus messages quickly enough (or there just are too many), a signal might get lost on its way to the recipient. Other complications can also occur, as with any RPC mechanism. For these reasons, if the application requires extra reliability, you need to think how to arrange it. One possibility is to occasionally check the state that the application is interested in, assuming it can be checked over the D-Bus. This, however, should not be done too often; the recommended interval is once a minute or less often than that, and only when the application is already active for other reasons. This model leads to a reduction in battery life, so its use should be carefully weighed.
Signals in D-Bus are able to carry information. Each signal has its own name (specified in the interface XML), as well as "arguments". In signal's case, the argument list is actually a list of information that is passed along the signal and should not be confused with method call parameters (although both are delivered in the same manner).
Signals in D-Bus are able to carry information. Each signal has its own name (specified in the interface XML), as well as "arguments". In signal's case, the argument list is actually a list of information that is passed along the signal and should not be confused with method call parameters (although both are delivered in the same manner).
Line 10: Line 9:
Signals do not "return", meaning that when a D-Bus signal is sent, no reply is received, nor is one expected. If the signal emitter wants to be sure that the signal was delivered, additional mechanisms need to be constructed for this (D-Bus does not include them directly). A D-Bus signal is very similar to most datagram-based network protocols, for example UDP. Sending a signal succeeds, even if there are no receivers interested in that specific signal.
Signals do not "return", meaning that when a D-Bus signal is sent, no reply is received, nor is one expected. If the signal emitter wants to be sure that the signal was delivered, additional mechanisms need to be constructed for this (D-Bus does not include them directly). A D-Bus signal is very similar to most datagram-based network protocols, for example UDP. Sending a signal succeeds, even if there are no receivers interested in that specific signal.
-
Most D-Bus language bindings attempts to map D-Bus signals into something more natural in the target language. Since GLib already supports the notion of signals (as GLib signals), this mapping is quite natural. So in practice, the client registers for GLib signals, and then handles the signals in callback functions (a special wrapper function must be used to register for the wrapped signals: dbus_g_proxy_connect_signal).
+
Most D-Bus language bindings attempts to map D-Bus signals into something more natural in the target language. Since GLib already supports the notion of signals (as GLib signals), this mapping is quite natural. So in practice, the client registers for GLib signals, and then handles the signals in callback functions (a special wrapper function must be used to register for the wrapped signals: <code>dbus_g_proxy_connect_signal</code>).
== Declaring Signals in Interface XML ==
== Declaring Signals in Interface XML ==
-
Next, the Value object is extended so that it contains two threshold values (minimum and maximum), and the object emits signals, whenever a set operation falls outside the thresholds.
+
Next, the <code>Value</code> object is extended so that it contains two threshold values (minimum and maximum), and the object emits signals, whenever a set operation falls outside the thresholds.
A signal is also emitted whenever a value is changed (making the binary content of the new value different from the old one).
A signal is also emitted whenever a value is changed (making the binary content of the new value different from the old one).
-
In order to make the signals available to introspection data, the interface XML file is modified accordingly: glib-dbus-signals/value-dbus-interface.xml
+
In order to make the signals available to introspection data, the interface XML file is modified accordingly: [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/value-dbus-interface.xml glib-dbus-signals/value-dbus-interface.xml]
-
<tt><span>'''<span><font color="#0000FF">&lt;node&gt;</font></span>'''</span>
+
<source lang="xml">
-
<span>'''<span><font color="#0000FF">&lt;interface</font></span>'''</span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"org.maemo.Value"</font></span><span>'''<span><font color="#0000FF">&gt;</font></span>'''</span>
+
<node>
-
<span>''<span><font color="#9A1900">&lt;!- ... Listing cut for brevity ... -&gt;</font></span>''</span>
+
<interface name="org.maemo.Value">
-
<span>''<span><font color="#9A1900">&lt;!- Signal (D-Bus) definitions -&gt;</font></span>''</span>
+
<!- ... Listing cut for brevity ... ->
-
<span>''<span><font color="#9A1900">&lt;!- NOTE: The current version of dbus-bindings-tool doesn't</font></span>''</span>
+
<!- Signal (D-Bus) definitions ->
-
<span>''<span><font color="#9A1900">     actually enforce the signal arguments _at_all_. Signals need</font></span>''</span>
+
<!- NOTE: The current version of dbus-bindings-tool doesn't
-
<span>''<span><font color="#9A1900">     to be declared in order to be passed through the bus itself,</font></span>''</span>
+
    actually enforce the signal arguments _at_all_. Signals need
-
<span>''<span><font color="#9A1900">     but otherwise no checks are done! For example, you could</font></span>''</span>
+
    to be declared in order to be passed through the bus itself,
-
<span>''<span><font color="#9A1900">     leave the signal arguments unspecified completely, and the</font></span>''</span>
+
    but otherwise no checks are done! For example, you could
-
<span>''<span><font color="#9A1900">     code would still work. -&gt;</font></span>''</span>
+
    leave the signal arguments unspecified completely, and the
-
<span>''<span><font color="#9A1900">&lt;!- Signals to tell interested clients about state change.</font></span>''</span>
+
    code would still work. ->
-
<span>''<span><font color="#9A1900">     We send a string parameter with them. They never can have</font></span>''</span>
+
<!- Signals to tell interested clients about state change.
-
<span>''<span><font color="#9A1900">     arguments with direction=in. -&gt;</font></span>''</span>
+
    We send a string parameter with them. They never can have
-
<span>'''<span><font color="#0000FF">&lt;signal</font></span>'''</span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"changed_value1"</font></span><span>'''<span><font color="#0000FF">&gt;</font></span>'''</span>
+
    arguments with direction=in. ->
-
<span>'''<span><font color="#0000FF">&lt;arg</font></span>'''</span> <span><font color="#009900">type</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"s"</font></span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"change_source_name"</font></span> <span><font color="#009900">direction</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"out"</font></span><span>'''<span><font color="#0000FF">/&gt;</font></span>'''</span>
+
<signal name="changed_value1">
-
<span>'''<span><font color="#0000FF">&lt;/signal&gt;</font></span>'''</span>
+
<arg type="s" name="change_source_name" direction="out"/>
-
<span>'''<span><font color="#0000FF">&lt;signal</font></span>'''</span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"changed_value2"</font></span><span>'''<span><font color="#0000FF">&gt;</font></span>'''</span>
+
</signal>
-
<span>'''<span><font color="#0000FF">&lt;arg</font></span>'''</span> <span><font color="#009900">type</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"s"</font></span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"change_source_name"</font></span> <span><font color="#009900">direction</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"out"</font></span><span>'''<span><font color="#0000FF">/&gt;</font></span>'''</span>
+
<signal name="changed_value2">
-
<span>'''<span><font color="#0000FF">&lt;/signal&gt;</font></span>'''</span>
+
<arg type="s" name="change_source_name" direction="out"/>
-
<span>''<span><font color="#9A1900">&lt;!- Signals to tell interested clients that values are outside</font></span>''</span>
+
</signal>
-
<span>''<span><font color="#9A1900">     the internally configured range (thresholds). -&gt;</font></span>''</span>
+
<!- Signals to tell interested clients that values are outside
-
<span>'''<span><font color="#0000FF">&lt;signal</font></span>'''</span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"outofrange_value1"</font></span><span>'''<span><font color="#0000FF">&gt;</font></span>'''</span>
+
    the internally configured range (thresholds). ->
-
<span>'''<span><font color="#0000FF">&lt;arg</font></span>'''</span> <span><font color="#009900">type</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"s"</font></span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"outofrange_source_name"</font></span> <span><font color="#009900">direction</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"out"</font></span><span>'''<span><font color="#0000FF">/&gt;</font></span>'''</span>
+
<signal name="outofrange_value1">
-
<span>'''<span><font color="#0000FF">&lt;/signal&gt;</font></span>'''</span>
+
<arg type="s" name="outofrange_source_name" direction="out"/>
-
<span>'''<span><font color="#0000FF">&lt;signal</font></span>'''</span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"outofrange_value2"</font></span><span>'''<span><font color="#0000FF">&gt;</font></span>'''</span>
+
</signal>
-
<span>'''<span><font color="#0000FF">&lt;arg</font></span>'''</span> <span><font color="#009900">type</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"s"</font></span> <span><font color="#009900">name</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"outofrange_source_name"</font></span> <span><font color="#009900">direction</font></span><span><font color="#990000"><nowiki>=</nowiki></font></span><span><font color="#FF0000">"out"</font></span><span>'''<span><font color="#0000FF">/&gt;</font></span>'''</span>
+
<signal name="outofrange_value2">
-
<span>'''<span><font color="#0000FF">&lt;/signal&gt;</font></span>'''</span>
+
<arg type="s" name="outofrange_source_name" direction="out"/>
-
<span>'''<span><font color="#0000FF">&lt;/interface&gt;</font></span>'''</span>
+
</signal>
-
<span>'''<span><font color="#0000FF">&lt;/node&gt;</font></span>'''</span></tt>
+
</interface>
 +
</node>
 +
</source>
-
The signal definitions are required the dbus-bindings-tool is used; however, the argument specification for each signal is not required by the tool. In fact, the tool ignores all argument specifications, and as can be seen below, a lot of "manual coding" has to be made in order to implement and use the signals (on both the client and server side).
+
The signal definitions are required if the <code>dbus-bindings-tool</code> is used; however, the argument specification for each signal is not required by the tool. In fact, the tool ignores all argument specifications, and as can be seen below, a lot of "manual coding" has to be made in order to implement and use the signals (on both the client and server side).
== Emitting Signals from GObject ==
== Emitting Signals from GObject ==
-
So that the signal names can easily be changed later, they will be defined in a header file, and the header file will be used in both the server and client. The following is the section with the signal names: glib-dbus-signals/common-defs.h
+
The signal names are defined in a header file so that they can easily be changed later, and the header file is used in both the server and the client. The following is the section with the signal names: [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/common-defs.h glib-dbus-signals/common-defs.h]
-
<tt><span>''<span><font color="#9A1900">/* Symbolic constants for the signal names to use with GLib.</font></span>''</span>
+
<source lang="c">
-
<span>''<span><font color="#9A1900">  These need to map into the D-Bus signal names. */</font></span>''</span>
+
/* Symbolic constants for the signal names to use with GLib.
-
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SIGNAL_CHANGED_VALUE1    <span><font color="#FF0000">"changed_value1"</font></span>
+
  These need to map into the D-Bus signal names. */
-
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SIGNAL_CHANGED_VALUE2    <span><font color="#FF0000">"changed_value2"</font></span>
+
#define SIGNAL_CHANGED_VALUE1    "changed_value1"
-
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SIGNAL_OUTOFRANGE_VALUE1 <span><font color="#FF0000">"outofrange_value1"</font></span>
+
#define SIGNAL_CHANGED_VALUE2    "changed_value2"
-
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SIGNAL_OUTOFRANGE_VALUE2 <span><font color="#FF0000">"outofrange_value2"</font></span></tt>
+
#define SIGNAL_OUTOFRANGE_VALUE1 "outofrange_value1"
 +
#define SIGNAL_OUTOFRANGE_VALUE2 "outofrange_value2"
 +
</source>
-
Before a GObject can emit a GLib signal, the signal itself needs to be defined and created. This is best done in the class constructor code (since the signal types need to be created only once): glib-dbus-signals/server.c
+
Before a <code>GObject</code> can emit a GLib signal, the signal itself needs to be defined and created. The best way to do this is to use the class constructor code (because the signal types need to be created only once): [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/server.c glib-dbus-signals/server.c]
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
<source lang="c">
-
  <span>''<span><font color="#9A1900"> * Define enumerations for the different signals that we can generate</font></span>''</span>
+
/**
-
  <span>''<span><font color="#9A1900"> * (so that we can refer to them within the signals-array [below]</font></span>''</span>
+
  * Define enumerations for the different signals that we can generate
-
  <span>''<span><font color="#9A1900"> * using symbolic names). These are not the same as the signal name</font></span>''</span>
+
  * (so that we can refer to them within the signals-array [below]
-
  <span>''<span><font color="#9A1900"> * strings.</font></span>''</span>
+
  * using symbolic names). These are not the same as the signal name
-
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
+
  * strings.
-
  <span>''<span><font color="#9A1900"> * NOTE: E_SIGNAL_COUNT is NOT a signal enum. We use it as a</font></span>''</span>
+
  *
-
  <span>''<span><font color="#9A1900"> *      convenient constant giving the number of signals defined so</font></span>''</span>
+
  * NOTE: E_SIGNAL_COUNT is NOT a signal enum. We use it as a
-
  <span>''<span><font color="#9A1900"> *      far. It needs to be listed last.</font></span>''</span>
+
  *      convenient constant giving the number of signals defined so
-
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
+
  *      far. It needs to be listed last.
-
<span>'''<span><font color="#0000FF">typedef</font></span>'''</span> <span>'''<span><font color="#0000FF">enum</font></span>'''</span> <span><font color="#FF0000">{</font></span>
+
  */
-
  E_SIGNAL_CHANGED_VALUE1<span><font color="#990000">,</font></span>
+
typedef enum {
-
  E_SIGNAL_CHANGED_VALUE2<span><font color="#990000">,</font></span>
+
  E_SIGNAL_CHANGED_VALUE1,
-
  E_SIGNAL_OUTOFRANGE_VALUE1<span><font color="#990000">,</font></span>
+
  E_SIGNAL_CHANGED_VALUE2,
-
  E_SIGNAL_OUTOFRANGE_VALUE2<span><font color="#990000">,</font></span>
+
  E_SIGNAL_OUTOFRANGE_VALUE1,
-
  E_SIGNAL_COUNT
+
  E_SIGNAL_OUTOFRANGE_VALUE2,
-
<span><font color="#FF0000">}</font></span> ValueSignalNumber<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  E_SIGNAL_COUNT
-
  <span>''<span><font color="#9A1900">/*... Listing cut for brevity ...*/</font></span>''</span>
+
} ValueSignalNumber;
-
<span>'''<span><font color="#0000FF">typedef</font></span>'''</span> <span>'''<span><font color="#0000FF">struct</font></span>'''</span> <span><font color="#FF0000">{</font></span>
+
  /*... Listing cut for brevity ...*/
-
  <span>''<span><font color="#9A1900">/* The parent class state. */</font></span>''</span>
+
typedef struct {
-
  GObjectClass parent<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  /* The parent class state. */
-
  <span>''<span><font color="#9A1900">/* The minimum number under which values will cause signals to be</font></span>''</span>
+
  GObjectClass parent;
-
<span>''<span><font color="#9A1900">    emitted. */</font></span>''</span>
+
  /* The minimum number under which values cause signals to be
-
  gint thresholdMin<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    emitted. */
-
  <span>''<span><font color="#9A1900">/* The maximum number over which values will cause signals to be</font></span>''</span>
+
  gint thresholdMin;
-
<span>''<span><font color="#9A1900">    emitted. */</font></span>''</span>
+
  /* The maximum number over which values cause signals to be
-
  gint thresholdMax<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    emitted. */
-
  <span>''<span><font color="#9A1900">/* Signals created for this class. */</font></span>''</span>
+
  gint thresholdMax;
-
  guint signals<span><font color="#990000">[</font></span>E_SIGNAL_COUNT<span><font color="#990000">];</font></span>
+
  /* Signals created for this class. */
-
<span><font color="#FF0000">}</font></span> ValueObjectClass<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  guint signals[E_SIGNAL_COUNT];
-
  <span>''<span><font color="#9A1900">/*... Listing cut for brevity ...*/</font></span>''</span>
+
} ValueObjectClass;
-
<span>''<span><font color="#9A1900">/**</font></span>''</span>
+
  /*... Listing cut for brevity ...*/
-
  <span>''<span><font color="#9A1900"> * Per class initializer</font></span>''</span>
+
/**
-
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
+
  * Per class initializer
-
  <span>''<span><font color="#9A1900"> * Sets up the thresholds (-100 .. 100), creates the signals that we</font></span>''</span>
+
  *
-
  <span>''<span><font color="#9A1900"> * can emit from any object of this class and finally registers the</font></span>''</span>
+
  * Sets up the thresholds (-100 .. 100), creates the signals that we
-
  <span>''<span><font color="#9A1900"> * type into the GLib/D-Bus wrapper so that it may add its own magic.</font></span>''</span>
+
  * can emit from any object of this class and finally registers the
-
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
+
  * type into the GLib/D-Bus wrapper so that it may add its own magic.
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#009900">void</font></span> <span>'''<span><font color="#000000">value_object_class_init</font></span>'''</span><span><font color="#990000">(</font></span>ValueObjectClass<span><font color="#990000"><nowiki>*</nowiki></font></span> klass<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
  */
-
  <span>''<span><font color="#9A1900">/* Since all signals have the same prototype (each will get one</font></span>''</span>
+
static void value_object_class_init(ValueObjectClass* klass) {
-
<span>''<span><font color="#9A1900">    string as a parameter), we create them in a loop below. The only</font></span>''</span>
+
  /* Because all signals have the same prototype (each gets one
-
<span>''<span><font color="#9A1900">    difference between them is the index into the klass-&gt;signals</font></span>''</span>
+
    string as a parameter), we create them in a loop below. The only
-
<span>''<span><font color="#9A1900">    array, and the signal name.</font></span>''</span>
+
    difference between them is the index into the klass->signals
-
<span>''<span><font color="#9A1900">    Since the index goes from 0 to E_SIGNAL_COUNT-1, we just specify</font></span>''</span>
+
    array, and the signal name.
-
<span>''<span><font color="#9A1900">    the signal names into an array and iterate over it.</font></span>''</span>
+
    Because the index goes from 0 to E_SIGNAL_COUNT-1, we just specify
-
<span>''<span><font color="#9A1900">    Note that the order here must correspond to the order of the</font></span>''</span>
+
    the signal names into an array and iterate over it.
-
<span>''<span><font color="#9A1900">    enumerations before. */</font></span>''</span>
+
    Note that the order here must correspond to the order of the
-
  <span>'''<span><font color="#0000FF">const</font></span>'''</span> gchar<span><font color="#990000"><nowiki>*</nowiki></font></span> signalNames<span><font color="#990000">[</font></span>E_SIGNAL_COUNT<span><font color="#990000">]</font></span> <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">{</font></span>
+
    enumerations before. */
-
    SIGNAL_CHANGED_VALUE1<span><font color="#990000">,</font></span>
+
  const gchar* signalNames[E_SIGNAL_COUNT] = {
-
    SIGNAL_CHANGED_VALUE2<span><font color="#990000">,</font></span>
+
    SIGNAL_CHANGED_VALUE1,
-
    SIGNAL_OUTOFRANGE_VALUE1<span><font color="#990000">,</font></span>
+
    SIGNAL_CHANGED_VALUE2,
-
    SIGNAL_OUTOFRANGE_VALUE2 <span><font color="#FF0000">}</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    SIGNAL_OUTOFRANGE_VALUE1,
-
  <span>''<span><font color="#9A1900">/* Loop variable */</font></span>''</span>
+
    SIGNAL_OUTOFRANGE_VALUE2 };
-
  <span><font color="#009900">int</font></span> i<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  /* Loop variable */
-
  <span>'''<span><font color="#000000">dbg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Called"</font></span><span><font color="#990000">);</font></span>
+
  int i;
-
  <span>'''<span><font color="#000000">g_assert</font></span>'''</span><span><font color="#990000">(</font></span>klass <span><font color="#990000"><nowiki>!=</nowiki></font></span> NULL<span><font color="#990000">);</font></span>
+
  dbg("Called");
-
  <span>''<span><font color="#9A1900">/* Setup sane minimums and maximums for the thresholds. There is no</font></span>''</span>
+
  g_assert(klass != NULL);
-
<span>''<span><font color="#9A1900">    way to change these afterwards (currently), so you can consider</font></span>''</span>
+
  /* Setup sane minimums and maximums for the thresholds. There is no
-
<span>''<span><font color="#9A1900">    them as constants. */</font></span>''</span>
+
    way to change these afterwards (currently), so you can consider
-
  klass<span><font color="#990000">-&gt;</font></span>thresholdMin <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#990000">-</font></span><span><font color="#993399">100</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    them as constants. */
-
  klass<span><font color="#990000">-&gt;</font></span>thresholdMax <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">100</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  klass->thresholdMin = -100;
-
  <span>'''<span><font color="#000000">dbg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Creating signals"</font></span><span><font color="#990000">);</font></span>
+
  klass->thresholdMax = 100;
-
  <span>''<span><font color="#9A1900">/* Create the signals in one loop, since they all are similar</font></span>''</span>
+
  dbg("Creating signals");
-
<span>''<span><font color="#9A1900">    (except for the names). */</font></span>''</span>
+
  /* Create the signals in one loop, because they all are similar
-
  <span>'''<span><font color="#0000FF">for</font></span>'''</span> <span><font color="#990000">(</font></span>i <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">0</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span> i <span><font color="#990000">&lt;</font></span> E_SIGNAL_COUNT<span><font color="#990000"><nowiki>;</nowiki></font></span> i<span><font color="#990000">++)</font></span> <span><font color="#FF0000">{</font></span>
+
    (except for the names). */
-
    guint signalId<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
  for (i = 0; i < E_SIGNAL_COUNT; i++) {
-
    <span>''<span><font color="#9A1900">/* Most of the time you will encounter the following code without</font></span>''</span>
+
    guint signalId;
-
<span>''<span><font color="#9A1900">      comments. This is why all the parameters are documented</font></span>''</span>
+
    /* Most of the time you encounter the following code without
-
<span>''<span><font color="#9A1900">      directly below. */</font></span>''</span>
+
      comments. This is why all the parameters are documented
-
    signalId <span><font color="#990000"><nowiki>=</nowiki></font></span>
+
      directly below. */
-
      <span>'''<span><font color="#000000">g_signal_new</font></span>'''</span><span><font color="#990000">(</font></span>signalNames<span><font color="#990000">[</font></span>i<span><font color="#990000">],</font></span> <span>''<span><font color="#9A1900">/* str name of the signal */</font></span>''</span>
+
    signalId =
-
                    <span>''<span><font color="#9A1900">/* GType to which signal is bound to */</font></span>''</span>
+
      g_signal_new(signalNames[i], /* str name of the signal */
-
                    <span>'''<span><font color="#000000">G_OBJECT_CLASS_TYPE</font></span>'''</span><span><font color="#990000">(</font></span>klass<span><font color="#990000">),</font></span>
+
                  /* GType to which signal is bound to */
-
                    <span>''<span><font color="#9A1900">/* Combination of GSignalFlags which tell the</font></span>''</span>
+
                  G_OBJECT_CLASS_TYPE(klass),
-
<span>''<span><font color="#9A1900">                      signal dispatch machinery how and when to</font></span>''</span>
+
                  /* Combination of GSignalFlags which tell the
-
<span>''<span><font color="#9A1900">                      dispatch this signal. The most common is the</font></span>''</span>
+
                      signal dispatch machinery how and when to
-
<span>''<span><font color="#9A1900">                      G_SIGNAL_RUN_LAST specification. */</font></span>''</span>
+
                      dispatch this signal. The most common is the
-
                    G_SIGNAL_RUN_LAST<span><font color="#990000">,</font></span>
+
                      G_SIGNAL_RUN_LAST specification. */
-
                    <span>''<span><font color="#9A1900">/* Offset into the class structure for the type</font></span>''</span>
+
                  G_SIGNAL_RUN_LAST,
-
<span>''<span><font color="#9A1900">                      function pointer. Since we're implementing a</font></span>''</span>
+
                  /* Offset into the class structure for the type
-
<span>''<span><font color="#9A1900">                      simple class/type, we'll leave this at zero. */</font></span>''</span>
+
                      function pointer. Because we are implementing a
-
                    <span><font color="#993399">0</font></span><span><font color="#990000">,</font></span>
+
                      simple class/type, we leave this at zero. */
-
                    <span>''<span><font color="#9A1900">/* GSignalAccumulator to use. We don't need one. */</font></span>''</span>
+
                  0,
-
                    NULL<span><font color="#990000">,</font></span>
+
                  /* GSignalAccumulator to use. We do not need one. */
-
                    <span>''<span><font color="#9A1900">/* User-data to pass to the accumulator. */</font></span>''</span>
+
                  NULL,
-
                    NULL<span><font color="#990000">,</font></span>
+
                  /* User-data to pass to the accumulator. */
-
                    <span>''<span><font color="#9A1900">/* Function to use to marshal the signal data into</font></span>''</span>
+
                  NULL,
-
<span>''<span><font color="#9A1900">                      the parameters of the signal call. Luckily for</font></span>''</span>
+
                  /* Function to use to marshal the signal data into
-
<span>''<span><font color="#9A1900">                      us, GLib (GCClosure) already defines just the</font></span>''</span>
+
                      the parameters of the signal call. Luckily for
-
<span>''<span><font color="#9A1900">                      function that we want for a signal handler that</font></span>''</span>
+
                      us, GLib (GCClosure) already defines just the
-
<span>''<span><font color="#9A1900">                      we don't expect any return values (void) and</font></span>''</span>
+
                      function that we want for a signal handler that
-
<span>''<span><font color="#9A1900">                      one that will accept one string as parameter</font></span>''</span>
+
                      we do not expect any return values (void) and
-
<span>''<span><font color="#9A1900">                      (besides the instance pointer and pointer to</font></span>''</span>
+
                      one that accepts one string as parameter
-
<span>''<span><font color="#9A1900">                      user-data).</font></span>''</span>
+
                      (besides the instance pointer and pointer to
-
<span>''<span><font color="#9A1900">                      If no such function would exist, you would need</font></span>''</span>
+
                      user-data).
-
<span>''<span><font color="#9A1900">                      to create a new one (by using glib-genmarshal</font></span>''</span>
+
                      If no such function exists, you need
-
<span>''<span><font color="#9A1900">                      tool). */</font></span>''</span>
+
                      to create a new one (by using glib-genmarshal
-
                    g_cclosure_marshal_VOID__STRING<span><font color="#990000">,</font></span>
+
                      tool). */
-
                    <span>''<span><font color="#9A1900">/* Return GType of the return value. The handler</font></span>''</span>
+
                  g_cclosure_marshal_VOID__STRING,
-
<span>''<span><font color="#9A1900">                      does not return anything, so we use G_TYPE_NONE</font></span>''</span>
+
                  /* Return GType of the return value. The handler
-
<span>''<span><font color="#9A1900">                      to mark that. */</font></span>''</span>
+
                      does not return anything, so we use G_TYPE_NONE
-
                    G_TYPE_NONE<span><font color="#990000">,</font></span>
+
                      to mark that. */
-
                    <span>''<span><font color="#9A1900">/* Number of parameter GTypes to follow. */</font></span>''</span>
+
                  G_TYPE_NONE,
-
                    <span><font color="#993399">1</font></span><span><font color="#990000">,</font></span>
+
                  /* Number of parameter GTypes to follow. */
-
                    <span>''<span><font color="#9A1900">/* GType(s) of the parameters. We only have one. */</font></span>''</span>
+
                  1,
-
                    G_TYPE_STRING<span><font color="#990000">);</font></span>
+
                  /* GType(s) of the parameters. We only have one. */
-
    <span>''<span><font color="#9A1900">/* Store the signal Id into the class state, so that we can use</font></span>''</span>
+
                  G_TYPE_STRING);
-
<span>''<span><font color="#9A1900">      it later. */</font></span>''</span>
+
    /* Store the signal Id into the class state, so that we can use
-
    klass<span><font color="#990000">-&gt;</font></span>signals<span><font color="#990000">[</font></span>i<span><font color="#990000">]</font></span> <span><font color="#990000"><nowiki>=</nowiki></font></span> signalId<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
      it later. */
-
    <span>''<span><font color="#9A1900">/* Proceed with the next signal creation. */</font></span>''</span>
+
    klass->signals[i] = signalId;
-
  <span><font color="#FF0000">}</font></span>
+
    /* Proceed with the next signal creation. */
-
  <span>''<span><font color="#9A1900">/* All signals created. */</font></span>''</span>
+
  }
-
  <span>'''<span><font color="#000000">dbg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Binding to GLib/D-Bus"</font></span><span><font color="#990000">);</font></span>
+
  /* All signals created. */
-
  <span>''<span><font color="#9A1900">/*... Listing cut for brevity ...*/</font></span>''</span>
+
  dbg("Binding to GLib/D-Bus");
-
<span><font color="#FF0000">}</font></span></tt>
+
  /*... Listing cut for brevity ...*/
 +
}
 +
</source>
-
The signal types will be kept in the class structure, so that they can be referenced easily by the signal emitting utility (covered next). The class constructor code will also set up the threshold limits, which in this implementation will be immutable (i.e. they cannot be changed). It is advisable to experiment with adding more methods to adjust the thresholds as necessary.
+
The signal types are kept in the class structure, so that they can be referenced easily by the signal emitting utility. The class constructor code also sets up the threshold limits, which in this implementation are immutable (meaning that they cannot be changed). Experiment with the implementation and add more methods to adjust the thresholds as necessary.
-
Emitting the signals is then quite easy, but in order to reduce code amount, a utility function will be created to launch a given signal based on its enumeration: glib-dbus-signals/server.c
+
Emitting the signals is quite easy, but in order to reduce code amount, a utility function is created to launch a given signal based on its enumeration: [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/server.c glib-dbus-signals/server.c]
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
<source lang="c">
-
  <span>''<span><font color="#9A1900"> * Utility helper to emit a signal given with internal enumeration and</font></span>''</span>
+
/**
-
  <span>''<span><font color="#9A1900"> * the passed string as the signal data.</font></span>''</span>
+
  * Utility helper to emit a signal given with internal enumeration and
-
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
+
  * the passed string as the signal data.
-
  <span>''<span><font color="#9A1900"> * Used in the setter functions below.</font></span>''</span>
+
  *
-
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
+
  * Used in the setter functions below.
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#009900">void</font></span> <span>'''<span><font color="#000000">value_object_emitSignal</font></span>'''</span><span><font color="#990000">(</font></span>ValueObject<span><font color="#990000"><nowiki>*</nowiki></font></span> obj<span><font color="#990000">,</font></span>
+
  */
-
                                    ValueSignalNumber num<span><font color="#990000">,</font></span>
+
static void value_object_emitSignal(ValueObject* obj,
-
                                    <span>'''<span><font color="#0000FF">const</font></span>'''</span> gchar<span><font color="#990000"><nowiki>*</nowiki></font></span> message<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
                                    ValueSignalNumber num,
-
  <span>''<span><font color="#9A1900">/* In order to access the signal identifiers, we need to get a hold</font></span>''</span>
+
                                    const gchar* message) {
-
<span>''<span><font color="#9A1900">    of the class structure first. */</font></span>''</span>
+
  /* In order to access the signal identifiers, we need to get a hold
-
  ValueObjectClass<span><font color="#990000"><nowiki>*</nowiki></font></span> klass <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">VALUE_OBJECT_GET_CLASS</font></span>'''</span><span><font color="#990000">(</font></span>obj<span><font color="#990000">);</font></span>
+
    of the class structure first. */
-
  <span>''<span><font color="#9A1900">/* Check that the given num is valid (abort if not).</font></span>''</span>
+
  ValueObjectClass* klass = VALUE_OBJECT_GET_CLASS(obj);
-
<span>''<span><font color="#9A1900">    Given that this file is the module actually using this utility,</font></span>''</span>
+
  /* Check that the given num is valid (abort if not).
-
<span>''<span><font color="#9A1900">    you can consider this check superfluous (but useful for</font></span>''</span>
+
    Given that this file is the module actually using this utility,
-
<span>''<span><font color="#9A1900">    development work). */</font></span>''</span>
+
    you can consider this check superfluous (but useful for
-
  <span>'''<span><font color="#000000">g_assert</font></span>'''</span><span><font color="#990000">((</font></span>num <span><font color="#990000">&lt;</font></span> E_SIGNAL_COUNT<span><font color="#990000">)</font></span> <span><font color="#990000">&amp;&amp;</font></span> <span><font color="#990000">(</font></span>num <span><font color="#990000">&gt;=</font></span> <span><font color="#993399">0</font></span><span><font color="#990000">));</font></span>
+
    development work). */
-
  <span>'''<span><font color="#000000">dbg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Emitting signal id %d, with message '%s'"</font></span><span><font color="#990000">,</font></span> num<span><font color="#990000">,</font></span> message<span><font color="#990000">);</font></span>
+
  g_assert((num < E_SIGNAL_COUNT) && (num >= 0));
-
  <span>''<span><font color="#9A1900">/* This is the simplest way of emitting signals. */</font></span>''</span>
+
  dbg("Emitting signal id %d, with message '%s'", num, message);
-
  <span>'''<span><font color="#000000">g_signal_emit</font></span>'''</span><span><font color="#990000">(</font></span><span>''<span><font color="#9A1900">/* Instance of the object that is generating this</font></span>''</span>
+
  /* This is the simplest way of emitting signals. */
-
<span>''<span><font color="#9A1900">                  signal. This will be passed as the first parameter</font></span>''</span>
+
  g_signal_emit(/* Instance of the object that is generating this
-
<span>''<span><font color="#9A1900">                  to the signal handler (eventually). But obviously</font></span>''</span>
+
                  signal. This is passed as the first parameter
-
<span>''<span><font color="#9A1900">                  when speaking about D-Bus, a signal caught on the</font></span>''</span>
+
                  to the signal handler (eventually). But obviously
-
<span>''<span><font color="#9A1900">                  other side of D-Bus will be first processed by</font></span>''</span>
+
                  when speaking about D-Bus, a signal caught on the
-
<span>''<span><font color="#9A1900">                  the GLib-wrappers (the object proxy) and only then</font></span>''</span>
+
                  other side of D-Bus is first processed by
-
<span>''<span><font color="#9A1900">                  processed by the signal handler. */</font></span>''</span>
+
                  the GLib-wrappers (the object proxy) and only then
-
                obj<span><font color="#990000">,</font></span>
+
                  processed by the signal handler. */
-
                <span>''<span><font color="#9A1900">/* Signal id for the signal to generate. These are</font></span>''</span>
+
                obj,
-
<span>''<span><font color="#9A1900">                  stored inside the class state structure. */</font></span>''</span>
+
                /* Signal id for the signal to generate. These are
-
                klass<span><font color="#990000">-&gt;</font></span>signals<span><font color="#990000">[</font></span>num<span><font color="#990000">],</font></span>
+
                  stored inside the class state structure. */
-
                <span>''<span><font color="#9A1900">/* Detail of signal. Since we are not using detailed</font></span>''</span>
+
                klass->signals[num],
-
<span>''<span><font color="#9A1900">                  signals, we leave this at zero (default). */</font></span>''</span>
+
                /* Detail of signal. Because we are not using detailed
-
                <span><font color="#993399">0</font></span><span><font color="#990000">,</font></span>
+
                  signals, we leave this at zero (default). */
-
                <span>''<span><font color="#9A1900">/* Data to marshal into the signal. In our case it's</font></span>''</span>
+
                0,
-
<span>''<span><font color="#9A1900">                  just one string. */</font></span>''</span>
+
                /* Data to marshal into the signal. In our case it is
-
                message<span><font color="#990000">);</font></span>
+
                  just one string. */
-
  <span>''<span><font color="#9A1900">/* g_signal_emit returns void, so we cannot check for success. */</font></span>''</span>
+
                message);
-
  <span>''<span><font color="#9A1900">/* Done emitting signal. */</font></span>''</span>
+
  /* g_signal_emit returns void, so we cannot check for success. */
-
<span><font color="#FF0000">}</font></span></tt>
+
  /* Done emitting signal. */
 +
}
 +
</source>
-
So that it would not be necessary to check the threshold values in multiple places in the source code, that will also be implemented as a separate function. Emitting the "threshold exceeded" signal is still up to the caller. glib-dbus-signals/server.c
+
To avoid having to check the threshold values in multiple places in the source code, the threshold value is also implemented as a separate function. Emitting the "threshold exceeded" signal is still up to the caller. [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/server.c glib-dbus-signals/server.c]
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
<source lang="c">
-
  <span>''<span><font color="#9A1900"> * Utility to check the given integer against the thresholds.</font></span>''</span>
+
/**
-
  <span>''<span><font color="#9A1900"> * Will return TRUE if thresholds are not exceeded, FALSE otherwise.</font></span>''</span>
+
  * Utility to check the given integer against the thresholds.
-
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
+
  * Returns TRUE if thresholds are not exceeded, FALSE otherwise.
-
  <span>''<span><font color="#9A1900"> * Used in the setter functions below.</font></span>''</span>
+
  *
-
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
+
  * Used in the setter functions below.
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> gboolean <span>'''<span><font color="#000000">value_object_thresholdsOk</font></span>'''</span><span><font color="#990000">(</font></span>ValueObject<span><font color="#990000"><nowiki>*</nowiki></font></span> obj<span><font color="#990000">,</font></span>
+
  */
-
                                          gint value<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
static gboolean value_object_thresholdsOk(ValueObject* obj,
-
  <span>''<span><font color="#9A1900">/* Thresholds are in class state data, get access to it */</font></span>''</span>
+
                                          gint value) {
-
  ValueObjectClass<span><font color="#990000"><nowiki>*</nowiki></font></span> klass <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">VALUE_OBJECT_GET_CLASS</font></span>'''</span><span><font color="#990000">(</font></span>obj<span><font color="#990000">);</font></span>
+
  /* Thresholds are in class state data, get access to it */
-
  <span>'''<span><font color="#0000FF">return</font></span>'''</span> <span><font color="#990000">((</font></span>value <span><font color="#990000">&gt;=</font></span> klass<span><font color="#990000">-&gt;</font></span>thresholdMin<span><font color="#990000">)</font></span> <span><font color="#990000">&amp;&amp;</font></span>
+
  ValueObjectClass* klass = VALUE_OBJECT_GET_CLASS(obj);
-
          <span><font color="#990000">(</font></span>value <span><font color="#990000">&lt;=</font></span> klass<span><font color="#990000">-&gt;</font></span>thresholdMax<span><font color="#990000">));</font></span>
+
  return ((value >= klass->thresholdMin) &&
-
<span><font color="#FF0000">}</font></span></tt>
+
          (value <= klass->thresholdMax));
 +
}
 +
</source>
-
Both utility functions are then used from within the respective set functions, one of which is presented below: glib-dbus-signals/server.c
+
Both utility functions are then used from within the respective set functions, one of which is presented below: [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/server.c glib-dbus-signals/server.c]
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
<source lang="c">
-
  <span>''<span><font color="#9A1900"> * Function that gets called when someone tries to execute "setvalue1"</font></span>''</span>
+
/**
-
  <span>''<span><font color="#9A1900"> * over the D-Bus. (Actually the marshalling code from the stubs gets</font></span>''</span>
+
  * Function that gets called when someone tries to execute "setvalue1"
-
  <span>''<span><font color="#9A1900"> * executed first, but they will eventually execute this function.)</font></span>''</span>
+
  * over the D-Bus. (Actually the marshalling code from the stubs gets
-
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
+
  * executed first, but eventually they execute this function.)
-
gboolean <span>'''<span><font color="#000000">value_object_setvalue1</font></span>'''</span><span><font color="#990000">(</font></span>ValueObject<span><font color="#990000"><nowiki>*</nowiki></font></span> obj<span><font color="#990000">,</font></span> gint valueIn<span><font color="#990000">,</font></span>
+
  */
-
                                                  GError<span><font color="#990000"><nowiki>**</nowiki></font></span> error<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
gboolean value_object_setvalue1(ValueObject* obj, gint valueIn,
-
  <span>'''<span><font color="#000000">dbg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Called (valueIn=%d)"</font></span><span><font color="#990000">,</font></span> valueIn<span><font color="#990000">);</font></span>
+
                                                  GError** error) {
-
  <span>'''<span><font color="#000000">g_assert</font></span>'''</span><span><font color="#990000">(</font></span>obj <span><font color="#990000"><nowiki>!=</nowiki></font></span> NULL<span><font color="#990000">);</font></span>
+
  dbg("Called (valueIn=%d)", valueIn);
-
  <span>''<span><font color="#9A1900">/* Compare the current value against old one. If they're the same,</font></span>''</span>
+
  g_assert(obj != NULL);
-
<span>''<span><font color="#9A1900">    we don't need to do anything (except return success). */</font></span>''</span>
+
  /* Compare the current value against old one. If they're the same,
-
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span>obj<span><font color="#990000">-&gt;</font></span>value1 <span><font color="#990000"><nowiki>!=</nowiki></font></span> valueIn<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
    we do not need to do anything (except return success). */
-
    <span>''<span><font color="#9A1900">/* Change the value. */</font></span>''</span>
+
  if (obj->value1 != valueIn) {
-
    obj<span><font color="#990000">-&gt;</font></span>value1 <span><font color="#990000"><nowiki>=</nowiki></font></span> valueIn<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    /* Change the value. */
-
    <span>''<span><font color="#9A1900">/* Emit the "changed_value1" signal. */</font></span>''</span>
+
    obj->value1 = valueIn;
-
    <span>'''<span><font color="#000000">value_object_emitSignal</font></span>'''</span><span><font color="#990000">(</font></span>obj<span><font color="#990000">,</font></span> E_SIGNAL_CHANGED_VALUE1<span><font color="#990000">,</font></span> <span><font color="#FF0000">"value1"</font></span><span><font color="#990000">);</font></span>
+
    /* Emit the "changed_value1" signal. */
-
    <span>''<span><font color="#9A1900">/* If new value falls outside the thresholds, emit</font></span>''</span>
+
    value_object_emitSignal(obj, E_SIGNAL_CHANGED_VALUE1, "value1");
-
<span>''<span><font color="#9A1900">      "outofrange_value1" signal as well. */</font></span>''</span>
+
    /* If new value falls outside the thresholds, emit
-
    <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">value_object_thresholdsOk</font></span>'''</span><span><font color="#990000">(</font></span>obj<span><font color="#990000">,</font></span> valueIn<span><font color="#990000">))</font></span> <span><font color="#FF0000">{</font></span>
+
      "outofrange_value1" signal as well. */
-
      <span>'''<span><font color="#000000">value_object_emitSignal</font></span>'''</span><span><font color="#990000">(</font></span>obj<span><font color="#990000">,</font></span> E_SIGNAL_OUTOFRANGE_VALUE1<span><font color="#990000">,</font></span>
+
    if (!value_object_thresholdsOk(obj, valueIn)) {
-
                                    <span><font color="#FF0000">"value1"</font></span><span><font color="#990000">);</font></span>
+
      value_object_emitSignal(obj, E_SIGNAL_OUTOFRANGE_VALUE1,
-
    <span><font color="#FF0000">}</font></span>
+
                                  "value1");
-
  <span><font color="#FF0000">}</font></span>
+
    }
-
  <span>''<span><font color="#9A1900">/* Return success to GLib/D-Bus wrappers. In this case we don't need</font></span>''</span>
+
  }
-
<span>''<span><font color="#9A1900">    to touch the supplied error pointer-pointer. */</font></span>''</span>
+
  /* Return success to GLib/D-Bus wrappers. In this case we do not need
-
  <span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
    to touch the supplied error pointer-pointer. */
-
<span><font color="#FF0000">}</font></span></tt>
+
  return TRUE;
 +
}
 +
</source>
-
The role of the "value1" string parameter that is sent along both of the signals above might raise a question. Sending the signal origin name with the signal allows one to reuse the same callback function in the client. It is quite rare that this kind of "source naming" would be useful, but it allows for writing a slightly shorter client program.
+
The role of the <code>value1</code> string parameter that is sent along both of the signals above can raise a question. Sending the signal origin name with the signal allows you to reuse the same callback function in the client. This kind of "source naming" is rarely useful but it allows for writing a slightly shorter client program.
-
The implementation of setvalue2 is almost identical, but deals with the gdouble parameter.
+
The implementation of <code>setvalue2</code> is almost identical, but deals with the <code>gdouble</code> parameter.
The getvalue functions are identical to the versions before as is the Makefile.
The getvalue functions are identical to the versions before as is the Makefile.
-
Next, the server is built and started on the background (in preparation for testing with dbus-send):
+
Next, the server is built and started on the background (in preparation for testing with <code>dbus-send</code>):
-
<div class="graybox">
+
<pre>
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; make server
+
[sbox-DIABLO_X86: ~/glib-dbus-signals] > make server
-
dbus-binding-tool --prefix=value_object --mode=glib-server \
+
dbus-binding-tool --prefix=value_object --mode=glib-server \
-
  value-dbus-interface.xml &gt; value-server-stub.h
+
  value-dbus-interface.xml > value-server-stub.h
-
cc -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include \
+
cc -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include \
-
  -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -Wall \
+
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -Wall \
-
  -DG_DISABLE_DEPRECATED -DNO_DAEMON -DPROGNAME=\"server\" \
+
-DG_DISABLE_DEPRECATED -DNO_DAEMON -DPROGNAME=\"server\" \
-
  -c server.c -o server.o
+
-c server.c -o server.o
-
cc server.o -o server -ldbus-glib-1 -ldbus-1 -lgobject-2.0 -lglib-2.0
+
cc server.o -o server -ldbus-glib-1 -ldbus-1 -lgobject-2.0 -lglib-2.0
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh ./server &amp;
+
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh ./server &amp;
-
[1] 15293
+
[1] 15293
-
server:main Connecting to the Session D-Bus.
+
server:main Connecting to the Session D-Bus.
-
server:main Registering the well-known name (org.maemo.Platdev_ex)
+
server:main Registering the well-known name (org.maemo.Platdev_ex)
-
server:main RequestName returned 1.
+
server:main RequestName returned 1.
-
server:main Creating one Value object.
+
server:main Creating one Value object.
-
server:value_object_class_init: Called
+
server:value_object_class_init: Called
-
server:value_object_class_init: Creating signals
+
server:value_object_class_init: Creating signals
-
server:value_object_class_init: Binding to GLib/D-Bus
+
server:value_object_class_init: Binding to GLib/D-Bus
-
server:value_object_class_init: Done
+
server:value_object_class_init: Done
-
server:value_object_init: Called
+
server:value_object_init: Called
-
server:main Registering it on the D-Bus.
+
server:main Registering it on the D-Bus.
-
server:main Ready to serve requests (daemonizing).
+
server:main Ready to serve requests (daemonizing).
-
server: Not daemonizing (built with NO_DAEMON-build define)
+
server: Not daemonizing (built with NO_DAEMON-build define)
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt;
+
[sbox-DIABLO_X86: ~/glib-dbus-signals] >
 +
</pre>
-
</div>
+
The next step is to test the <code>setvalue1</code> method:
-
The next step is to test the setvalue1 method:
+
<pre>
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 +
--type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 +
/GlobalValue org.maemo.Value.setvalue1 int32:10
 +
server:value_object_setvalue1: Called (valueIn=10)
 +
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
 +
method return sender=:1.38 -> dest=:1.41
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 +
--type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 +
/GlobalValue org.maemo.Value.setvalue1 int32:-200
 +
server:value_object_setvalue1: Called (valueIn=-200)
 +
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
 +
server:value_object_emitSignal: Emitting signal id 2, with message 'value1'
 +
method return sender=:1.38 -> dest=:1.42
 +
</pre>
-
<div class="graybox">
+
After this, the next step is to test the <code>setvalue2</code> (with <code>double</code>s). At this point, the threshold triggering can behave in an unexpected manner:
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh dbus-send \
+
-
  --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
+
-
  /GlobalValue org.maemo.Value.setvalue1 int32:10
+
-
server:value_object_setvalue1: Called (valueIn=10)
+
-
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
+
-
method return sender=:1.38 -&gt; dest=:1.41
+
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh dbus-send \
+
-
  --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
+
-
  /GlobalValue org.maemo.Value.setvalue1 int32:-200
+
-
server:value_object_setvalue1: Called (valueIn=-200)
+
-
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
+
-
server:value_object_emitSignal: Emitting signal id 2, with message 'value1'
+
-
method return sender=:1.38 -&gt; dest=:1.42
+
-
</div>
+
<pre>
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 +
--type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 +
/GlobalValue org.maemo.Value.setvalue2 double:100.5
 +
server:value_object_setvalue2: Called (valueIn=100.500)
 +
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
 +
method return sender=:1.38 -> dest=:1.44
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 +
--type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 +
/GlobalValue org.maemo.Value.setvalue2 double:101
 +
server:value_object_setvalue2: Called (valueIn=101.000)
 +
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
 +
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
 +
method return sender=:1.38 -> dest=:1.45
 +
</pre>
-
And then setvalue2 (with doubles). At this point, something strange might be noticed in the threshold triggering:
+
Because the threshold testing logic truncates the <code>gdouble</code> before testing against the (integer) thresholds, a value of 100.5 is detected as 100 and it still fits within the thresholds.
-
<div class="graybox">
+
Instead of printing out the emitted signal names, the enumeration values of the emitted signals are printed. This could be rectified with a small enumeration to string table, but it was omitted from the program for simplicity.
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh dbus-send \
+
-
  --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
+
-
  /GlobalValue org.maemo.Value.setvalue2 double:100.5
+
-
server:value_object_setvalue2: Called (valueIn=100.500)
+
-
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
+
-
method return sender=:1.38 -&gt; dest=:1.44
+
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh dbus-send \
+
-
  --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
+
-
  /GlobalValue org.maemo.Value.setvalue2 double:101
+
-
server:value_object_setvalue2: Called (valueIn=101.000)
+
-
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
+
-
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
+
-
method return sender=:1.38 -&gt; dest=:1.45
+
-
</div>
+
Other than seeing the server messages about emitting the signals, there is no trace of them being sent or received. This is because <code>dbus-send</code> does not listen for signals. A separate tool (<code>dbus-monitor</code>) can be used for tracing signals.
-
Since the threshold testing logic will truncate the gdouble before testing against the (integer) thresholds, a value of 100.5 will be detected as 100, and will still fit within the thresholds.
+
== Catching Signals in GLib/D-Bus Clients ==
-
Instead of printing out the emitted signal names, their enumeration values are printed. This could be rectified with a small enumeration to string table, but it was emitted from the program for simplicity.
+
In order to receive D-Bus signals in the client, you need to perform several tasks per signal. This is because <code>dbus-bindings-tool</code> does not generate any code for signals. The aim is to make the GLib wrappers emit GLib signals whenever an interesting D-Bus signal arrives. This also means that registering the interest for a particular D-Bus signal is necessary.
-
It can also be noticed that other than seeing the server messages about emitting the signals, there is not a trace of them being sent or received. This is because dbus-send does not listen for signals. There is a separate tool for tracing signals, and it will be covered at the end of this chapter (dbus-monitor).
+
When implementing the callbacks for the signals, the prototype must be implemented correctly. Because the signals are sent with one attached string value, the callbacks receive at least the string parameter. Besides the signal attached arguments, the callback receives the proxy object, through which the signal was received, and optional user-specified data (which is not used in this example, so it is always <code>NULL</code>). [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/client.c glib-dbus-signals/client.c]
-
== Catching Signals in GLib/D-Bus Clients ==
+
<source lang="c">
 +
/**
 +
* Signal handler for the "changed" signals. These are sent by the
 +
* Value-object whenever its contents change (whether within
 +
* thresholds or not).
 +
*
 +
* Like before, we use strcmp to differentiate between the two
 +
* values, and act accordingly (in this case by retrieving
 +
* synchronously the values using getvalue1 or getvalue2).
 +
*
 +
* NOTE: Because we use synchronous getvalues, it is possible to get
 +
*      this code stuck if for some reason the server is stuck
 +
*      in an eternal loop.
 +
*/
 +
static void valueChangedSignalHandler(DBusGProxy* proxy,
 +
                                      const char* valueName,
 +
                                      gpointer userData) {
 +
  /* Because method calls over D-Bus can fail, we need to check
 +
    for failures. The server might be shut down in the middle of
 +
    things, or might act badly in other ways. */
 +
  GError* error = NULL;
-
In order to receive D-Bus signals in the client, one needs to do quite a bit of work per signal. This is because dbus-bindings-tool does not generate any code for signals (at the moment). The aim is to make the GLib wrappers emit GSignals, whenever an interesting D-Bus signal arrives. This also means that it will be necessary to register the interest for a particular D-Bus signal.
+
  g_print(PROGNAME ":value-changed (%s)\n", valueName);
-
When implementing the callbacks for the signals, it is necessary to take care to implement the prototype correctly. Since the signals will be sent with one attached string value, the callbacks will receive at least the string parameter. Besides the signal attached arguments, the callback will receive the proxy object, through which the signal was received, and optional user-specified data (which will not be used in this example, so it will be always NULL). glib-dbus-signals/client.c
+
  /* Find out which value changed and act accordingly. */
 +
  if (strcmp(valueName, "value1") == 0) {
 +
    gint v = 0;
 +
    /* Execute the RPC to get value1. */
 +
    org_maemo_Value_getvalue1(proxy, &v, &error);
 +
    if (error == NULL) {
 +
      g_print(PROGNAME ":value-changed Value1 now %d\n", v);
 +
    } else {
 +
      /* You could interrogate the GError further to find out exactly
 +
        what the error was, but in our case, we just ignore the
 +
        error with the hope that some day (preferably soon), the
 +
        RPC succeeds again (server comes back on the bus). */
 +
      handleError("Failed to retrieve value1", error->message, FALSE);
 +
    }
 +
  } else {
 +
    gdouble v = 0.0;
 +
    org_maemo_Value_getvalue2(proxy, &v, &error);
 +
    if (error == NULL) {
 +
      g_print(PROGNAME ":value-changed Value2 now %.3f\n", v);
 +
    } else {
 +
      handleError("Failed to retrieve value2", error->message, FALSE);
 +
    }
 +
  }
 +
  /* Free up error object if one was allocated. */
 +
  g_clear_error(&error);
 +
}
 +
</source>
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
The callback first determines what was the source value that caused the signal to be generated. For this, it uses the string argument of the signal. It then retrieves the current value using the respective RPC methods (<code>getvalue1</code> or <code>getvalue2</code>) and prints out the value.
-
<span>''<span><font color="#9A1900"> * Signal handler for the "changed" signals. These will be sent by the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * Value-object whenever its contents change (whether within</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * thresholds or not).</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * Like before, we use strcmp to differentiate between the two</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * values, and act accordingly (in this case by retrieving</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * synchronously the values using getvalue1 or getvalue2.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * NOTE: Since we use synchronous getvalues, it is possible to get</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *      this code stuck if for some reason the server would be stuck</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *      in an eternal loop.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#009900">void</font></span> <span>'''<span><font color="#000000">valueChangedSignalHandler</font></span>'''</span><span><font color="#990000">(</font></span>DBusGProxy<span><font color="#990000"><nowiki>*</nowiki></font></span> proxy<span><font color="#990000">,</font></span>
+
-
                                      <span>'''<span><font color="#0000FF">const</font></span>'''</span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>*</nowiki></font></span> valueName<span><font color="#990000">,</font></span>
+
-
                                      gpointer userData<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
  <span>''<span><font color="#9A1900">/* Since method calls over D-Bus can fail, we'll need to check</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    for failures. The server might be shut down in the middle of</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    things, or might act badly in other ways. */</font></span>''</span>
+
-
  GError<span><font color="#990000"><nowiki>*</nowiki></font></span> error <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
+
-
  <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":value-changed (%s)</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> valueName<span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Find out which value changed, and act accordingly. */</font></span>''</span>
+
-
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span><span>'''<span><font color="#000000">strcmp</font></span>'''</span><span><font color="#990000">(</font></span>valueName<span><font color="#990000">,</font></span> <span><font color="#FF0000">"value1"</font></span><span><font color="#990000">)</font></span> <span><font color="#990000"><nowiki>==</nowiki></font></span> <span><font color="#993399">0</font></span><span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
    gint v <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">0</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
    <span>''<span><font color="#9A1900">/* Execute the RPC to get value1. */</font></span>''</span>
+
-
    <span>'''<span><font color="#000000">org_maemo_Value_getvalue1</font></span>'''</span><span><font color="#990000">(</font></span>proxy<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>v<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">);</font></span>
+
-
    <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span>error <span><font color="#990000"><nowiki>==</nowiki></font></span> NULL<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
      <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":value-changed Value1 now %d</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> v<span><font color="#990000">);</font></span>
+
-
    <span><font color="#FF0000">}</font></span> <span>'''<span><font color="#0000FF">else</font></span>'''</span> <span><font color="#FF0000">{</font></span>
+
-
      <span>''<span><font color="#9A1900">/* You could interrogate the GError further, to find out exactly</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">        what the error was, but in our case, we'll just ignore the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">        error with the hope that some day (preferably soon), the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">        RPC will succeed again (server comes back on the bus). */</font></span>''</span>
+
-
      <span>'''<span><font color="#000000">handleError</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Failed to retrieve value1"</font></span><span><font color="#990000">,</font></span> error<span><font color="#990000">-&gt;</font></span>message<span><font color="#990000">,</font></span> FALSE<span><font color="#990000">);</font></span>
+
-
    <span><font color="#FF0000">}</font></span>
+
-
  <span><font color="#FF0000">}</font></span> <span>'''<span><font color="#0000FF">else</font></span>'''</span> <span><font color="#FF0000">{</font></span>
+
-
    gdouble v <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">0.0</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
    <span>'''<span><font color="#000000">org_maemo_Value_getvalue2</font></span>'''</span><span><font color="#990000">(</font></span>proxy<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>v<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">);</font></span>
+
-
    <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span>error <span><font color="#990000"><nowiki>==</nowiki></font></span> NULL<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
      <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":value-changed Value2 now %.3f</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> v<span><font color="#990000">);</font></span>
+
-
    <span><font color="#FF0000">}</font></span> <span>'''<span><font color="#0000FF">else</font></span>'''</span> <span><font color="#FF0000">{</font></span>
+
-
      <span>'''<span><font color="#000000">handleError</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Failed to retrieve value2"</font></span><span><font color="#990000">,</font></span> error<span><font color="#990000">-&gt;</font></span>message<span><font color="#990000">,</font></span> FALSE<span><font color="#990000">);</font></span>
+
-
    <span><font color="#FF0000">}</font></span>
+
-
  <span><font color="#FF0000">}</font></span>
+
-
  <span>''<span><font color="#9A1900">/* Free up error object if one was allocated. */</font></span>''</span>
+
-
  <span>'''<span><font color="#000000">g_clear_error</font></span>'''</span><span><font color="#990000">(&amp;</font></span>error<span><font color="#990000">);</font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
The callback will first determine, what was the source value that caused the signal to be generated. For this, it uses the string argument of the signal. It will then retrieve the current value using the respective RPC methods (getvalue1 or getvalue2), and print out the value.
+
If any errors occur during the method calls, the errors are printed out, but the program continues to run. If an error does occur, the <code>GError</code> object needs to be freed (performed with <code>g_clear_error</code>). The program does not terminate on RPC errors, because the condition might be temporary (the <code>Value</code> object server might be restarted later).
-
If any errors occur during the method calls, the errors will be printed out, but the program will continue to run. If an error does occur, the GError object will need to be freed (performed with g_clear_error). The program will not be terminated on RPC errors, since the condition might be temporary (the Value object server might be restarted later).
+
The code for the <code>outOfRangeSignalHandler</code> callback has been omitted, because it does not contain anything beyond what <code>valueChangedSignalHandler</code> demonstrates.
-
The code for the outOfRangeSignalHandler callback has been omitted, since it does not contain anything beyond what valueChangedSignalHandler demonstrates.
+
Registering for the signals is a two-step process. First, it is necessary to register the interest in the D-Bus signals, and then install the callbacks for the respective GLib signals. This is done within main: [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/client.c glib-dbus-signals/client.c]
-
Registering for the signals is a two-step process. First, it is necessary to register the interest in the D-Bus signals, and then install the callbacks for the respective GLib signals. This is done within main: glib-dbus-signals/client.c
+
<source lang="c">
 +
/**
 +
* The test program itself.
 +
*
 +
* 1) Setup GType/GSignal
 +
* 2) Create GMainLoop object
 +
* 3) Connect to the Session D-Bus
 +
* 4) Create a proxy GObject for the remote Value object
 +
* 5) Register signals that we're interested from the Value object
 +
* 6) Register signal handlers for them
 +
* 7) Start a timer that launches timerCallback once per second.
 +
* 8) Run main-loop (forever)
 +
*/
 +
int main(int argc, char** argv) {
-
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
+
  /*... Listing cut for brevity ...*/
-
<span>''<span><font color="#9A1900"> * The test program itself.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 1) Setup GType/GSignal</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 2) Create GMainLoop object</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 3) Connect to the Session D-Bus</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 4) Create a proxy GObject for the remote Value object</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 5) Register signals that we're interested from the Value object</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 6) Register signal handlers for them</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 7) Start a timer that will launch timerCallback once per second.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * 8) Run main-loop (forever)</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> */</font></span>''</span>
+
-
<span><font color="#009900">int</font></span> <span>'''<span><font color="#000000">main</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#009900">int</font></span> argc<span><font color="#990000">,</font></span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>**</nowiki></font></span> argv<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/*... Listing cut for brevity ...*/</font></span>''</span>
+
-
+
-
  remoteValue <span><font color="#990000"><nowiki>=</nowiki></font></span>
+
-
    <span>'''<span><font color="#000000">dbus_g_proxy_new_for_name</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">,</font></span>
+
-
                              VALUE_SERVICE_NAME<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* name */</font></span>''</span>
+
-
                              VALUE_SERVICE_OBJECT_PATH<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* obj path */</font></span>''</span>
+
-
                              VALUE_SERVICE_INTERFACE <span>''<span><font color="#9A1900">/* interface */</font></span>''</span><span><font color="#990000">);</font></span>
+
-
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span>remoteValue <span><font color="#990000"><nowiki>==</nowiki></font></span> NULL<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
+
-
    <span>'''<span><font color="#000000">handleError</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Couldn't create the proxy object"</font></span><span><font color="#990000">,</font></span>
+
-
                <span><font color="#FF0000">"Unknown(dbus_g_proxy_new_for_name)"</font></span><span><font color="#990000">,</font></span> TRUE<span><font color="#990000">);</font></span>
+
-
  <span><font color="#FF0000">}</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Register the signatures for the signal handlers.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    In our case, we'll have one string parameter passed to use along</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    the signal itself. The parameter list is terminated with</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    G_TYPE_INVALID (i.e., the GType for string objects. */</font></span>''</span>
+
-
+
-
  <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":main Registering signal handler signatures.</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Add the argument signatures for the signals (needs to be done</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    before connecting the signals). This might go away in the future,</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    when the GLib-bindings will do automatic introspection over the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    D-Bus, but for now we need the registration phase. */</font></span>''</span>
+
-
  <span><font color="#FF0000">{</font></span> <span>''<span><font color="#9A1900">/* Create a local scope for variables. */</font></span>''</span>
+
-
+
-
    <span><font color="#009900">int</font></span> i<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
    <span>'''<span><font color="#0000FF">const</font></span>'''</span> gchar<span><font color="#990000"><nowiki>*</nowiki></font></span> signalNames<span><font color="#990000">[]</font></span> <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">{</font></span> SIGNAL_CHANGED_VALUE1<span><font color="#990000">,</font></span>
+
-
                                    SIGNAL_CHANGED_VALUE2<span><font color="#990000">,</font></span>
+
-
                                    SIGNAL_OUTOFRANGE_VALUE1<span><font color="#990000">,</font></span>
+
-
                                    SIGNAL_OUTOFRANGE_VALUE2 <span><font color="#FF0000">}</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
    <span>''<span><font color="#9A1900">/* Iterate over all the entries in the above array.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">      The upper limit for i might seem strange at first glance,</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">      but is quite common idiom to extract the number of elements</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">      in a statically allocated arrays in C.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">      NOTE: The idiom will not work with dynamically allocated</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">            arrays. (Or rather it will, but the result is probably</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">            not what you expect.) */</font></span>''</span>
+
-
    <span>'''<span><font color="#0000FF">for</font></span>'''</span> <span><font color="#990000">(</font></span>i <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">0</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span> i <span><font color="#990000">&lt;</font></span> <span>'''<span><font color="#0000FF">sizeof</font></span>'''</span><span><font color="#990000">(</font></span>signalNames<span><font color="#990000">)/</font></span><span>'''<span><font color="#0000FF">sizeof</font></span>'''</span><span><font color="#990000">(</font></span>signalNames<span><font color="#990000">[</font></span><span><font color="#993399">0</font></span><span><font color="#990000">]);</font></span> i<span><font color="#990000">++)</font></span> <span><font color="#FF0000">{</font></span>
+
-
      <span>''<span><font color="#9A1900">/* Since the function doesn't return anything, we cannot check</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">        for errors here. */</font></span>''</span>
+
-
      <span>'''<span><font color="#000000">dbus_g_proxy_add_signal</font></span>'''</span><span><font color="#990000">(</font></span><span>''<span><font color="#9A1900">/* Proxy to use */</font></span>''</span>
+
-
                              remoteValue<span><font color="#990000">,</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* Signal name */</font></span>''</span>
+
-
                              signalNames<span><font color="#990000">[</font></span>i<span><font color="#990000">],</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* Will receive one string argument */</font></span>''</span>
+
-
                              G_TYPE_STRING<span><font color="#990000">,</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* Termination of the argument list */</font></span>''</span>
+
-
                              G_TYPE_INVALID<span><font color="#990000">);</font></span>
+
-
    <span><font color="#FF0000">}</font></span>
+
-
  <span><font color="#FF0000">}</font></span> <span>''<span><font color="#9A1900">/* end of local scope */</font></span>''</span>
+
-
+
-
  <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":main Registering D-Bus signal handlers.</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* We connect each of the following signals one at a time,</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    since we'll be using two different callbacks. */</font></span>''</span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Again, no return values, cannot hence check for errors. */</font></span>''</span>
+
-
  <span>'''<span><font color="#000000">dbus_g_proxy_connect_signal</font></span>'''</span><span><font color="#990000">(</font></span><span>''<span><font color="#9A1900">/* Proxy object */</font></span>''</span>
+
-
                              remoteValue<span><font color="#990000">,</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* Signal name */</font></span>''</span>
+
-
                              SIGNAL_CHANGED_VALUE1<span><font color="#990000">,</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* Signal handler to use. Note that the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                typecast is just to make the compiler</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                happy about the function, since the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                prototype is not compatible with</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                regular signal handlers. */</font></span>''</span>
+
-
                              <span>'''<span><font color="#000000">G_CALLBACK</font></span>'''</span><span><font color="#990000">(</font></span>valueChangedSignalHandler<span><font color="#990000">),</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* User-data (we don't use any). */</font></span>''</span>
+
-
                              NULL<span><font color="#990000">,</font></span>
+
-
                              <span>''<span><font color="#9A1900">/* GClosureNotify function that is</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                responsible in freeing the passed</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">                                user-data (we have no data). */</font></span>''</span>
+
-
                              NULL<span><font color="#990000">);</font></span>
+
-
+
-
  <span>'''<span><font color="#000000">dbus_g_proxy_connect_signal</font></span>'''</span><span><font color="#990000">(</font></span>remoteValue<span><font color="#990000">,</font></span> SIGNAL_CHANGED_VALUE2<span><font color="#990000">,</font></span>
+
-
                              <span>'''<span><font color="#000000">G_CALLBACK</font></span>'''</span><span><font color="#990000">(</font></span>valueChangedSignalHandler<span><font color="#990000">),</font></span>
+
-
                              NULL<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
+
-
+
-
  <span>'''<span><font color="#000000">dbus_g_proxy_connect_signal</font></span>'''</span><span><font color="#990000">(</font></span>remoteValue<span><font color="#990000">,</font></span> SIGNAL_OUTOFRANGE_VALUE1<span><font color="#990000">,</font></span>
+
-
                              <span>'''<span><font color="#000000">G_CALLBACK</font></span>'''</span><span><font color="#990000">(</font></span>outOfRangeSignalHandler<span><font color="#990000">),</font></span>
+
-
                              NULL<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
+
-
+
-
  <span>'''<span><font color="#000000">dbus_g_proxy_connect_signal</font></span>'''</span><span><font color="#990000">(</font></span>remoteValue<span><font color="#990000">,</font></span> SIGNAL_OUTOFRANGE_VALUE2<span><font color="#990000">,</font></span>
+
-
                              <span>'''<span><font color="#000000">G_CALLBACK</font></span>'''</span><span><font color="#990000">(</font></span>outOfRangeSignalHandler<span><font color="#990000">),</font></span>
+
-
                              NULL<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* All signals are now registered and we're ready to handle them. */</font></span>''</span>
+
-
+
-
  <span>'''<span><font color="#000000">g_print</font></span>'''</span><span><font color="#990000">(</font></span>PROGNAME <span><font color="#FF0000">":main Starting main loop (first timer in 1s).</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Register a timer callback that will do RPC sets on the values.</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    The userdata pointer is used to pass the proxy object to the</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    callback so that it can launch modifications to the object. */</font></span>''</span>
+
-
  <span>'''<span><font color="#000000">g_timeout_add</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#993399">1000</font></span><span><font color="#990000">,</font></span> <span><font color="#990000">(</font></span>GSourceFunc<span><font color="#990000">)</font></span>timerCallback<span><font color="#990000">,</font></span> remoteValue<span><font color="#990000">);</font></span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* Run the program. */</font></span>''</span>
+
-
  <span>'''<span><font color="#000000">g_main_loop_run</font></span>'''</span><span><font color="#990000">(</font></span>mainloop<span><font color="#990000">);</font></span>
+
-
  <span>''<span><font color="#9A1900">/* Since the main loop is not stopped (by this code), we shouldn't</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">    ever get here. The program might abort() for other reasons. */</font></span>''</span>
+
-
+
-
  <span>''<span><font color="#9A1900">/* If it does, return failure as exit code. */</font></span>''</span>
+
-
  <span>'''<span><font color="#0000FF">return</font></span>'''</span> EXIT_FAILURE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
When adding the argument signatures for the signals <br />(with dbus_g_proxy_add_signal), one needs to be very careful with the parameter list. The signal argument types must be exactly the same as are sent from the server (irrespective of the argument specification in the interface XML). This is because the current version of dbus-bindings-tool does not generate any checks to enforce signal arguments based on the interface. In this simple case, only one string is received with each different signal, so this is not a big issue. The implementation for the callback function will need to match the argument specification given to the _add_signal-function, otherwise data layout on the stack will be incorrect, and cause problems.
+
  remoteValue =
 +
    dbus_g_proxy_new_for_name(bus,
 +
                              VALUE_SERVICE_NAME, /* name */
 +
                              VALUE_SERVICE_OBJECT_PATH, /* obj path */
 +
                              VALUE_SERVICE_INTERFACE /* interface */);
 +
  if (remoteValue == NULL) {
 +
    handleError("Couldn't create the proxy object",
 +
                "Unknown(dbus_g_proxy_new_for_name)", TRUE);
 +
  }
-
Building the client happens in the same manner as before (i.e. make client). Since the server is still (hopefully) running on the background, the client will now be started in the same session:
+
  /* Register the signatures for the signal handlers.
 +
    In our case, we have one string parameter passed to use along
 +
    the signal itself. The parameter list is terminated with
 +
    G_TYPE_INVALID (i.e., the GType for string objects. */
-
<div class="graybox">
+
  g_print(PROGNAME ":main Registering signal handler signatures.\n");
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh ./client
+
-
client:main Connecting to Session D-Bus.
+
-
client:main Creating a GLib proxy object for Value.
+
-
client:main Registering signal handler signatures.
+
-
client:main Registering D-Bus signal handlers.
+
-
client:main Starting main loop (first timer in 1s).
+
-
server:value_object_setvalue1: Called (valueIn=-80)
+
-
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
+
-
client:timerCallback Set value1 to -80
+
-
server:value_object_setvalue2: Called (valueIn=-120.000)
+
-
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
+
-
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
+
-
client:timerCallback Set value2 to -120.000
+
-
client:value-changed (value1)
+
-
server:value_object_getvalue1: Called (internal value1 is -80)
+
-
client:value-changed Value1 now -80
+
-
client:value-changed (value2)
+
-
server:value_object_getvalue2: Called (internal value2 is -120.000)
+
-
client:value-changed Value2 now -120.000
+
-
client:out-of-range (value2)!
+
-
client:out-of-range Value 2 is outside threshold
+
-
server:value_object_setvalue1: Called (valueIn=-70)
+
-
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
+
-
client:timerCallback Set value1 to -70
+
-
server:value_object_setvalue2: Called (valueIn=-110.000)
+
-
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
+
-
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
+
-
client:timerCallback Set value2 to -110.000
+
-
client:value-changed (value1)
+
-
server:value_object_getvalue1: Called (internal value1 is -70)
+
-
client:value-changed Value1 now -70
+
-
client:value-changed (value2)
+
-
server:value_object_getvalue2: Called (internal value2 is -110.000)
+
-
client:value-changed Value2 now -110.000
+
-
...
+
-
</div>
+
  /* Add the argument signatures for the signals (needs to be done
 +
    before connecting the signals). This may go away in the future,
 +
    when the GLib-bindings do automatic introspection over the
 +
    D-Bus, but for now we need the registration phase. */
 +
  { /* Create a local scope for variables. */
-
The client will start with the timer callback being executed once per second (as before). Each iteration, it will call the setvalue1 and setvalue2 RPC methods with increasing values. The number for value2 is intentionally set below the minimum threshold, so that it will cause an outofrange_value2 signal to be emitted. For each set, the changed_value signals will also be emitted. Whenever the client receives either of the value change signals, it will perform a getvalue RPC method call to retrieve the current value and print it out.
+
    int i;
 +
    const gchar* signalNames[] = { SIGNAL_CHANGED_VALUE1,
 +
                                  SIGNAL_CHANGED_VALUE2,
 +
                                  SIGNAL_OUTOFRANGE_VALUE1,
 +
                                  SIGNAL_OUTOFRANGE_VALUE2 };
 +
    /* Iterate over all the entries in the above array.
 +
      The upper limit for i might seem strange at first glance,
 +
      but is quite common idiom to extract the number of elements
 +
      in a statically allocated arrays in C.
 +
      NOTE: The idiom does not work with dynamically allocated
 +
            arrays. (Or rather it works, but the result is probably
 +
            not what you expect.) */
 +
    for (i = 0; i < sizeof(signalNames)/sizeof(signalNames[0]); i++) {
 +
      /* Because the function does not return anything, we cannot check
 +
        for errors here. */
 +
      dbus_g_proxy_add_signal(/* Proxy to use */
 +
                              remoteValue,
 +
                              /* Signal name */
 +
                              signalNames[i],
 +
                              /* Receives one string argument */
 +
                              G_TYPE_STRING,
 +
                              /* Termination of the argument list */
 +
                              G_TYPE_INVALID);
 +
    }
 +
  } /* end of local scope */
-
This will continue until the client is terminated.
+
  g_print(PROGNAME ":main Registering D-Bus signal handlers.\n");
-
==Tracing D-Bus Signals ==
+
  /* We connect each of the following signals one at a time,
 +
    because we are using two different callbacks. */
-
Sometimes it is useful to see which signals are actually carried on the buses, especially when adding signal handlers for signals that are emitted from undocumented interfaces. The dbus-monitor tool will attach to the D-Bus daemon, and ask it to watch for signals and report them back to the tool, so that it can decode the signals automatically, as they appear on the bus.
+
  /* Again, no return values, cannot hence check for errors. */
 +
  dbus_g_proxy_connect_signal(/* Proxy object */
 +
                              remoteValue,
 +
                              /* Signal name */
 +
                              SIGNAL_CHANGED_VALUE1,
 +
                              /* Signal handler to use. Note that the
 +
                                typecast is just to make the compiler
 +
                                happy about the function, because the
 +
                                prototype is not compatible with
 +
                                regular signal handlers. */
 +
                              G_CALLBACK(valueChangedSignalHandler),
 +
                              /* User-data (we don't use any). */
 +
                              NULL,
 +
                              /* GClosureNotify function that is
 +
                                responsible in freeing the passed
 +
                                user-data (we have no data). */
 +
                              NULL);
-
While the server and client are still running, the next step is to start the dbus-monitor (in a separate session this time) to see whether the signals are transmitted correctly. It should be noted that signals will appear on the bus even if there are no clients currently interested in them. In this case, signals are emitted by the server, based on client-issued RPC methods, so if the client is terminated, the signals will cease.
+
  dbus_g_proxy_connect_signal(remoteValue, SIGNAL_CHANGED_VALUE2,
 +
                              G_CALLBACK(valueChangedSignalHandler),
 +
                              NULL, NULL);
-
+
   dbus_g_proxy_connect_signal(remoteValue, SIGNAL_OUTOFRANGE_VALUE1,
-
[sbox-DIABLO_X86: ~/glib-dbus-signals] &gt; run-standalone.sh dbus-monitor type='signal'
+
                              G_CALLBACK(outOfRangeSignalHandler),
-
signal sender=:1.38 -&gt; dest=(null destination)
+
                              NULL, NULL);
-
   interface=org.maemo.Value; member=changed_value1
+
-
  string "value1"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value2
+
-
  string "value2"
+
-
signal sender=:1.38 -&gt; dest=(null destination)  
+
-
  interface=org.maemo.Value; member=outofrange_value2
+
-
  string "value2"
+
-
signal sender=:1.38 -&gt; dest=(null destination)  
+
-
  interface=org.maemo.Value; member=changed_value1
+
-
  string "value1"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value2
+
-
  string "value2"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=outofrange_value2
+
-
  string "value2"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value1
+
-
  string "value1"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value2
+
-
  string "value2"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value1
+
-
  string "value1"
+
-
signal sender=:1.38 -&gt; dest=(null destination)
+
-
  interface=org.maemo.Value; member=changed_value2
+
-
  string "value2"
+
 +
  dbus_g_proxy_connect_signal(remoteValue, SIGNAL_OUTOFRANGE_VALUE2,
 +
                              G_CALLBACK(outOfRangeSignalHandler),
 +
                              NULL, NULL);
-
The tool will automatically decode the parameters to the best of its ability (the string parameter for the signals above). It does not know the semantic meaning for the different signals, so sometimes it will be necessary to perform some additional testing to decide what they actually mean. This is especially true, when mapping out undocumented interfaces (for which there might not be source code available).
+
  /* All signals are now registered and we're ready to handle them. */
-
Some examples of displaying signals on the system bus on a device follow:
+
  g_print(PROGNAME ":main Starting main loop (first timer in 1s).\n");
-
* A device turning off the backlight after inactivity
+
  /* Register a timer callback that does RPC sets on the values.
 +
    The userdata pointer is used to pass the proxy object to the
 +
    callback so that it can launch modifications to the object. */
 +
  g_timeout_add(1000, (GSourceFunc)timerCallback, remoteValue);
-
Nokia-N810-xx-xx:~# run-standalone.sh dbus-monitor --system
+
  /* Run the program. */
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
   g_main_loop_run(mainloop);
-
   interface=com.nokia.mce.signal; member=display_status_ind
+
   /* Because the main loop is not stopped (by this code), we shouldn't
-
  string "dimmed"
+
    ever get here. The program might abort() for other reasons. */
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;  
+
-
   interface=com.nokia.mce.signal; member=system_inactivity_ind
+
-
  boolean true
+
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
-
  interface=com.nokia.mce.signal; member=save_unsaved_data_ind
+
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
-
  interface=com.nokia.mce.signal; member=display_status_ind
+
-
  string "off"
+
 +
  /* If it does, return failure as exit code. */
 +
  return EXIT_FAILURE;
 +
}
 +
</source>
-
* A device coming back to life after a screen tap
+
When adding the argument signatures for the signals (with <code>dbus_g_proxy_add_signal</code>), one needs to be very careful with the parameter list. The signal argument types must be exactly the same as are sent from the server (irrespective of the argument specification in the interface XML). This is because the current version of <code>dbus-bindings-tool</code> does not generate any checks to enforce signal arguments based on the interface. In this simple case, only one string is received with each different signal, so this is not a big issue. The implementation for the callback function needs to match the argument specification given to the <code>_add_signal</code>-function, otherwise data layout on the stack is incorrect and can cause problems.
-
+
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
-
  interface=com.nokia.mce.signal; member=system_inactivity_ind
+
-
  boolean false
+
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
-
  interface=com.nokia.mce.signal; member=display_status_ind
+
-
  string "on"
+
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
+
-
  interface=com.nokia.mce.signal; member=tklock_mode_ind
+
-
  string "unlocked"
+
 +
Building the client is done in the same manner as before (make client). Because the server is still (hopefully) running on the background, the client is now started in the same session:
 +
 +
<pre>
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh ./client
 +
client:main Connecting to Session D-Bus.
 +
client:main Creating a GLib proxy object for Value.
 +
client:main Registering signal handler signatures.
 +
client:main Registering D-Bus signal handlers.
 +
client:main Starting main loop (first timer in 1s).
 +
server:value_object_setvalue1: Called (valueIn=-80)
 +
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
 +
client:timerCallback Set value1 to -80
 +
server:value_object_setvalue2: Called (valueIn=-120.000)
 +
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
 +
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
 +
client:timerCallback Set value2 to -120.000
 +
client:value-changed (value1)
 +
server:value_object_getvalue1: Called (internal value1 is -80)
 +
client:value-changed Value1 now -80
 +
client:value-changed (value2)
 +
server:value_object_getvalue2: Called (internal value2 is -120.000)
 +
client:value-changed Value2 now -120.000
 +
client:out-of-range (value2)!
 +
client:out-of-range Value 2 is outside threshold
 +
server:value_object_setvalue1: Called (valueIn=-70)
 +
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
 +
client:timerCallback Set value1 to -70
 +
server:value_object_setvalue2: Called (valueIn=-110.000)
 +
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
 +
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
 +
client:timerCallback Set value2 to -110.000
 +
client:value-changed (value1)
 +
server:value_object_getvalue1: Called (internal value1 is -70)
 +
client:value-changed Value1 now -70
 +
client:value-changed (value2)
 +
server:value_object_getvalue2: Called (internal value2 is -110.000)
 +
client:value-changed Value2 now -110.000
 +
...
 +
</pre>
 +
 +
The client starts with the timer callback being executed once per second (as before). Each iteration, it calls the <code>setvalue1</code> and <code>setvalue2</code> RPC methods with increasing values. The number for <code>value2</code> is intentionally set below the minimum threshold, so that it causes an <code>outofrange_value2</code> signal to be emitted. For each set, the <code>changed_value</code> signals are also emitted. Whenever the client receives either of the value change signals, it performs a <code>getvalue</code> RPC method call to retrieve the current value and print it out.
 +
 +
This continues until the client is terminated.
 +
 +
==Tracing D-Bus Signals ==
 +
 +
Sometimes seeing which signals are actually carried on the buses can be useful, especially when adding signal handlers for signals that are emitted from undocumented interfaces. The <code>dbus-monitor</code> tool attaches to the D-Bus daemon, and asks it to watch for signals and report them back to the tool, so that it can decode the signals automatically, as they appear on the bus.
 +
 +
While the server and client are still running, the next step is to start the <code>dbus-monitor</code> (in a separate session this time) to see whether the signals are transmitted correctly. It should be noted that signals appear on the bus even if there are no clients currently interested in them. In this case, signals are emitted by the server, based on client-issued RPC methods, so if the client is terminated, the signals cease.
 +
 +
<pre>
 +
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-monitor type='signal'
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value1
 +
string "value1"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value2
 +
string "value2"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=outofrange_value2
 +
string "value2"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value1
 +
string "value1"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value2
 +
string "value2"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=outofrange_value2
 +
string "value2"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value1
 +
string "value1"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value2
 +
string "value2"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value1
 +
string "value1"
 +
signal sender=:1.38 -> dest=(null destination)
 +
interface=org.maemo.Value; member=changed_value2
 +
string "value2"
 +
</pre>
 +
 +
The tool decodes the parameters automatically to the best of its ability (the string parameter for the signals above). It does not know the semantic meaning for the different signals, so sometimes additional testing has to be performed to decide what they actually mean. This is especially true when mapping out undocumented interfaces (for which the source code is not necessarily available).
 +
 +
Some examples of displaying signals on the system bus on a device follow:
 +
 +
* A device turning off the backlight after inactivity
 +
<pre>
 +
Nokia-N810-xx-xx:~# run-standalone.sh dbus-monitor --system
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=display_status_ind
 +
string "dimmed"
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=system_inactivity_ind
 +
boolean true
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=save_unsaved_data_ind
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=display_status_ind
 +
string "off"
 +
</pre>
 +
 +
* A device coming back to life after a screen tap
 +
<pre>
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=system_inactivity_ind
 +
boolean false
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=display_status_ind
 +
string "on"
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=tklock_mode_ind
 +
string "unlocked"
 +
</pre>
* A device going into offline mode  
* A device going into offline mode  
-
 
-
signal sender=:1.0 -&gt; dest=(null destination)
 
-
  path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 
-
  interface=org.freedesktop.Hal.Device; member=Condition
 
-
    string "ButtonPressed"
 
-
    string "power"
 
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
 
-
  interface=com.nokia.mce.signal; member=sig_device_mode_ind
 
-
    string "offline"
 
-
signal sender=:1.26 -&gt; dest=(null destination)
 
-
  path=/com/nokia/wlancond/signal;
 
-
  interface=com.nokia.wlancond.signal; member=disconnected
 
-
    string "wlan0"
 
-
signal sender=:1.28 -&gt; dest=(null destination) path=/com/nokia/icd;
 
-
  interface=com.nokia.icd; member=status_changed
 
-
    string "SSID"
 
-
    string "WLAN_INFRA"
 
-
    string "DISCONNECTING"
 
-
    string "com.nokia.icd.error.network_error"
 
-
signal sender=:1.22 -&gt; dest=(null destination) path=/org/bluez/hci0;
 
-
  interface=org.bluez.Adapter; member=ModeChanged
 
-
    string "off"
 
-
signal sender=:1.25 -&gt; dest=(null destination)
 
-
  path=/com/nokia/btcond/signal;
 
-
  interface=com.nokia.btcond.signal; member=hci_dev_down
 
-
    string "hci0"
 
 +
<pre>
 +
signal sender=:1.0 -> dest=(null destination)
 +
path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 +
interface=org.freedesktop.Hal.Device; member=Condition
 +
  string "ButtonPressed"
 +
  string "power"
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=sig_device_mode_ind
 +
  string "offline"
 +
signal sender=:1.26 -> dest=(null destination)
 +
path=/com/nokia/wlancond/signal;
 +
interface=com.nokia.wlancond.signal; member=disconnected
 +
  string "wlan0"
 +
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 +
interface=com.nokia.icd; member=status_changed
 +
  string "SSID"
 +
  string "WLAN_INFRA"
 +
  string "DISCONNECTING"
 +
  string "com.nokia.icd.error.network_error"
 +
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 +
interface=org.bluez.Adapter; member=ModeChanged
 +
  string "off"
 +
signal sender=:1.25 -> dest=(null destination)
 +
path=/com/nokia/btcond/signal;
 +
interface=com.nokia.btcond.signal; member=hci_dev_down
 +
  string "hci0"
 +
</pre>
* A device going back into normal mode  
* A device going back into normal mode  
-
 
-
signal sender=:1.0 -&gt; dest=(null destination)
 
-
  path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 
-
  interface=org.freedesktop.Hal.Device; member=Condition
 
-
    string "ButtonPressed"
 
-
    string "power"
 
-
signal sender=:1.3 -&gt; dest=(null destination) path=/com/nokia/mce/signal;
 
-
  interface=com.nokia.mce.signal; member=sig_device_mode_ind
 
-
    string "normal"
 
-
signal sender=:1.39 -&gt; dest=(null destination)
 
-
  path=/org/kernel/class/firmware/hci_h4p;
 
-
  interface=org.kernel.kevent; member=add
 
-
signal sender=:1.39 -&gt; dest=(null destination)
 
-
  path=/org/kernel/class/firmware/hci_h4p;
 
-
  interface=org.kernel.kevent; member=remove
 
-
signal sender=:1.25 -&gt; dest=(null destination)
 
-
  path=/com/nokia/btcond/signal;
 
-
  interface=com.nokia.btcond.signal; member=hci_dev_up
 
-
    string "hci0"
 
-
signal sender=:1.22 -&gt; dest=(null destination) path=/org/bluez/hci0;
 
-
  interface=org.bluez.Adapter; member=ModeChanged
 
-
    string "connectable"
 
-
signal sender=:1.22 -&gt; dest=(null destination) path=/org/bluez/hci0;
 
-
  interface=org.bluez.Adapter; member=NameChanged
 
-
    string "Nokia N810"
 
-
signal sender=:1.28 -&gt; dest=(null destination) path=/com/nokia/icd;
 
-
  interface=com.nokia.icd; member=status_changed
 
-
    string "SSID"
 
-
    string "WLAN_INFRA"
 
-
    string "CONNECTING"
 
-
    string ""
 
-
signal sender=:1.26 -&gt; dest=(null destination)
 
-
  path=/com/nokia/wlancond/signal;
 
-
  interface=com.nokia.wlancond.signal; member=connected
 
-
    string "wlan0"
 
-
    array [
 
-
      byte 0
 
-
      byte 13
 
-
      byte 157
 
-
      byte 198
 
-
      byte 120
 
-
      byte 175
 
-
    ]
 
-
    int32 536870912
 
-
signal sender=:1.100 -&gt; dest=(null destination) path=/com/nokia/eap/signal;
 
-
  interface=com.nokia.eap.signal; member=auth_status
 
-
    uint32 4
 
-
signal sender=:1.28 -&gt; dest=(null destination) path=/com/nokia/icd;
 
-
  interface=com.nokia.icd; member=proxies
 
-
    uint32 0
 
-
    string ""
 
-
    int32 0
 
-
    string ""
 
-
    int32 0
 
-
    string ""
 
-
    int32 0
 
-
    string ""
 
-
    int32 0
 
-
    string ""
 
-
    int32 0
 
-
    array [ ]
 
-
    string ""
 
-
signal sender=:1.28 -&gt; dest=(null destination) path=/com/nokia/icd;
 
-
  interface=com.nokia.icd; member=status_changed
 
-
    string "SSID"
 
-
    string "WLAN_INFRA"
 
-
    string "CONNECTED"
 
-
    string ""
 
 +
<pre>
 +
signal sender=:1.0 -> dest=(null destination)
 +
path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 +
interface=org.freedesktop.Hal.Device; member=Condition
 +
  string "ButtonPressed"
 +
  string "power"
 +
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 +
interface=com.nokia.mce.signal; member=sig_device_mode_ind
 +
  string "normal"
 +
signal sender=:1.39 -> dest=(null destination)
 +
path=/org/kernel/class/firmware/hci_h4p;
 +
interface=org.kernel.kevent; member=add
 +
signal sender=:1.39 -> dest=(null destination)
 +
path=/org/kernel/class/firmware/hci_h4p;
 +
interface=org.kernel.kevent; member=remove
 +
signal sender=:1.25 -> dest=(null destination)
 +
path=/com/nokia/btcond/signal;
 +
interface=com.nokia.btcond.signal; member=hci_dev_up
 +
  string "hci0"
 +
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 +
interface=org.bluez.Adapter; member=ModeChanged
 +
  string "connectable"
 +
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 +
interface=org.bluez.Adapter; member=NameChanged
 +
  string "Nokia N810"
 +
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 +
interface=com.nokia.icd; member=status_changed
 +
  string "SSID"
 +
  string "WLAN_INFRA"
 +
  string "CONNECTING"
 +
  string ""
 +
signal sender=:1.26 -> dest=(null destination)
 +
path=/com/nokia/wlancond/signal;
 +
interface=com.nokia.wlancond.signal; member=connected
 +
  string "wlan0"
 +
  array [
 +
      byte 0
 +
      byte 13
 +
      byte 157
 +
      byte 198
 +
      byte 120
 +
      byte 175
 +
  ]
 +
  int32 536870912
 +
signal sender=:1.100 -> dest=(null destination) path=/com/nokia/eap/signal;
 +
interface=com.nokia.eap.signal; member=auth_status
 +
  uint32 4
 +
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 +
interface=com.nokia.icd; member=proxies
 +
  uint32 0
 +
  string ""
 +
  int32 0
 +
  string ""
 +
  int32 0
 +
  string ""
 +
  int32 0
 +
  string ""
 +
  int32 0
 +
  string ""
 +
  int32 0
 +
  array [ ]
 +
  string ""
 +
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 +
interface=com.nokia.icd; member=status_changed
 +
  string "SSID"
 +
  string "WLAN_INFRA"
 +
  string "CONNECTED"
 +
  string ""
 +
</pre>
 +
 +
It is also possible to send signals from the command line, which is useful when some feature of a device needs to be emulated inside the SDK. This (like RPC method calls) can be performed with the <code>dbus-send</code> tool. An example of this kind of simulation was given in the [[Documentation/Maemo 5 Developer Guide/Application Development/LibOSSO library|LibOSSO section]].
-
It is also possible to send signals from the command line, which is useful when wanting to emulate some feature of a device inside the SDK. This (like RPC method calls) can be performed with the dbus-send tool, and an example of this kind of simulation was given in the LibOSSO section.
+
[[Category:Development]]
 +
[[Category:Documentation]]
 +
[[Category:Fremantle]]

Latest revision as of 12:15, 6 September 2010

Contents

[edit] D-Bus Signal properties

Performing remote method invocations over the D-Bus is only one half of D-Bus capabilities. As was noted before, D-Bus also supports a broadcast method of communication, which is asynchronous. This mechanism is called a signal (in D-Bus terminology), and it is useful when several receivers must be notified about a state change that could affect them. Some examples where signals could be useful are notifying a lot of receivers, when the system is being shut down, network connectivity has been lost, and similar system-wide conditions. This way, the receivers do not need to poll for the status continuously.

However, signals are not the solution to all problems. If a recipient is not processing its D-Bus messages quickly enough (or there just are too many), a signal might get lost on its way to the recipient. Other complications can also occur, as with any RPC mechanism. For these reasons, if the application requires extra reliability, you need to think how to arrange it. One possibility is to occasionally check the state that the application is interested in, assuming it can be checked over the D-Bus. This, however, should not be done too often; the recommended interval is once a minute or less often than that, and only when the application is already active for other reasons. This model leads to a reduction in battery life, so its use should be carefully weighed.

Signals in D-Bus are able to carry information. Each signal has its own name (specified in the interface XML), as well as "arguments". In signal's case, the argument list is actually a list of information that is passed along the signal and should not be confused with method call parameters (although both are delivered in the same manner).

Signals do not "return", meaning that when a D-Bus signal is sent, no reply is received, nor is one expected. If the signal emitter wants to be sure that the signal was delivered, additional mechanisms need to be constructed for this (D-Bus does not include them directly). A D-Bus signal is very similar to most datagram-based network protocols, for example UDP. Sending a signal succeeds, even if there are no receivers interested in that specific signal.

Most D-Bus language bindings attempts to map D-Bus signals into something more natural in the target language. Since GLib already supports the notion of signals (as GLib signals), this mapping is quite natural. So in practice, the client registers for GLib signals, and then handles the signals in callback functions (a special wrapper function must be used to register for the wrapped signals: dbus_g_proxy_connect_signal).

[edit] Declaring Signals in Interface XML

Next, the Value object is extended so that it contains two threshold values (minimum and maximum), and the object emits signals, whenever a set operation falls outside the thresholds.

A signal is also emitted whenever a value is changed (making the binary content of the new value different from the old one).

In order to make the signals available to introspection data, the interface XML file is modified accordingly: glib-dbus-signals/value-dbus-interface.xml

<node>
	<interface name="org.maemo.Value">
		<!- ... Listing cut for brevity ... ->
		<!- Signal (D-Bus) definitions ->
		<!- NOTE: The current version of dbus-bindings-tool doesn't
		     actually enforce the signal arguments _at_all_. Signals need
		     to be declared in order to be passed through the bus itself,
		     but otherwise no checks are done! For example, you could
		     leave the signal arguments unspecified completely, and the
		     code would still work. ->
		<!- Signals to tell interested clients about state change.
		     We send a string parameter with them. They never can have
		     arguments with direction=in. ->
		<signal name="changed_value1">
			<arg type="s" name="change_source_name" direction="out"/>
		</signal>
		<signal name="changed_value2">
			<arg type="s" name="change_source_name" direction="out"/>
		</signal>
		<!- Signals to tell interested clients that values are outside
		     the internally configured range (thresholds). ->
		<signal name="outofrange_value1">
			<arg type="s" name="outofrange_source_name" direction="out"/>
		</signal>
		<signal name="outofrange_value2">
			<arg type="s" name="outofrange_source_name" direction="out"/>
		</signal>
	</interface>
</node>

The signal definitions are required if the dbus-bindings-tool is used; however, the argument specification for each signal is not required by the tool. In fact, the tool ignores all argument specifications, and as can be seen below, a lot of "manual coding" has to be made in order to implement and use the signals (on both the client and server side).

[edit] Emitting Signals from GObject

The signal names are defined in a header file so that they can easily be changed later, and the header file is used in both the server and the client. The following is the section with the signal names: glib-dbus-signals/common-defs.h

/* Symbolic constants for the signal names to use with GLib.
   These need to map into the D-Bus signal names. */
#define SIGNAL_CHANGED_VALUE1    "changed_value1"
#define SIGNAL_CHANGED_VALUE2    "changed_value2"
#define SIGNAL_OUTOFRANGE_VALUE1 "outofrange_value1"
#define SIGNAL_OUTOFRANGE_VALUE2 "outofrange_value2"

Before a GObject can emit a GLib signal, the signal itself needs to be defined and created. The best way to do this is to use the class constructor code (because the signal types need to be created only once): glib-dbus-signals/server.c

/**
 * Define enumerations for the different signals that we can generate
 * (so that we can refer to them within the signals-array [below]
 * using symbolic names). These are not the same as the signal name
 * strings.
 *
 * NOTE: E_SIGNAL_COUNT is NOT a signal enum. We use it as a
 *       convenient constant giving the number of signals defined so
 *       far. It needs to be listed last.
 */
typedef enum {
  E_SIGNAL_CHANGED_VALUE1,
  E_SIGNAL_CHANGED_VALUE2,
  E_SIGNAL_OUTOFRANGE_VALUE1,
  E_SIGNAL_OUTOFRANGE_VALUE2,
  E_SIGNAL_COUNT
} ValueSignalNumber;
  /*... Listing cut for brevity ...*/
typedef struct {
  /* The parent class state. */
  GObjectClass parent;
  /* The minimum number under which values cause signals to be
     emitted. */
  gint thresholdMin;
  /* The maximum number over which values cause signals to be
     emitted. */
  gint thresholdMax;
  /* Signals created for this class. */
  guint signals[E_SIGNAL_COUNT];
} ValueObjectClass;
  /*... Listing cut for brevity ...*/
/**
 * Per class initializer
 *
 * Sets up the thresholds (-100 .. 100), creates the signals that we
 * can emit from any object of this class and finally registers the
 * type into the GLib/D-Bus wrapper so that it may add its own magic.
 */
static void value_object_class_init(ValueObjectClass* klass) {
  /* Because all signals have the same prototype (each gets one
     string as a parameter), we create them in a loop below. The only
     difference between them is the index into the klass->signals
     array, and the signal name.
     Because the index goes from 0 to E_SIGNAL_COUNT-1, we just specify
     the signal names into an array and iterate over it.
     Note that the order here must correspond to the order of the
     enumerations before. */
  const gchar* signalNames[E_SIGNAL_COUNT] = {
    SIGNAL_CHANGED_VALUE1,
    SIGNAL_CHANGED_VALUE2,
    SIGNAL_OUTOFRANGE_VALUE1,
    SIGNAL_OUTOFRANGE_VALUE2 };
  /* Loop variable */
  int i;
  dbg("Called");
  g_assert(klass != NULL);
  /* Setup sane minimums and maximums for the thresholds. There is no
     way to change these afterwards (currently), so you can consider
     them as constants. */
  klass->thresholdMin = -100;
  klass->thresholdMax = 100;
  dbg("Creating signals");
  /* Create the signals in one loop, because they all are similar
     (except for the names). */
  for (i = 0; i < E_SIGNAL_COUNT; i++) {
    guint signalId;
    /* Most of the time you encounter the following code without
       comments. This is why all the parameters are documented
       directly below. */
    signalId =
      g_signal_new(signalNames[i], /* str name of the signal */
                   /* GType to which signal is bound to */
                   G_OBJECT_CLASS_TYPE(klass),
                   /* Combination of GSignalFlags which tell the
                      signal dispatch machinery how and when to
                      dispatch this signal. The most common is the
                      G_SIGNAL_RUN_LAST specification. */
                   G_SIGNAL_RUN_LAST,
                   /* Offset into the class structure for the type
                      function pointer. Because we are implementing a
                      simple class/type, we leave this at zero. */
                   0,
                   /* GSignalAccumulator to use. We do not need one. */
                   NULL,
                   /* User-data to pass to the accumulator. */
                   NULL,
                   /* Function to use to marshal the signal data into
                      the parameters of the signal call. Luckily for
                      us, GLib (GCClosure) already defines just the
                      function that we want for a signal handler that
                      we do not expect any return values (void) and
                      one that accepts one string as parameter
                      (besides the instance pointer and pointer to
                      user-data).
                      If no such function exists, you need
                      to create a new one (by using glib-genmarshal
                      tool). */
                   g_cclosure_marshal_VOID__STRING,
                   /* Return GType of the return value. The handler
                      does not return anything, so we use G_TYPE_NONE
                      to mark that. */
                   G_TYPE_NONE,
                   /* Number of parameter GTypes to follow. */
                   1,
                   /* GType(s) of the parameters. We only have one. */
                   G_TYPE_STRING);
    /* Store the signal Id into the class state, so that we can use
       it later. */
    klass->signals[i] = signalId;
    /* Proceed with the next signal creation. */
  }
  /* All signals created. */
  dbg("Binding to GLib/D-Bus");
  /*... Listing cut for brevity ...*/
}

The signal types are kept in the class structure, so that they can be referenced easily by the signal emitting utility. The class constructor code also sets up the threshold limits, which in this implementation are immutable (meaning that they cannot be changed). Experiment with the implementation and add more methods to adjust the thresholds as necessary.

Emitting the signals is quite easy, but in order to reduce code amount, a utility function is created to launch a given signal based on its enumeration: glib-dbus-signals/server.c

/**
 * Utility helper to emit a signal given with internal enumeration and
 * the passed string as the signal data.
 *
 * Used in the setter functions below.
 */
static void value_object_emitSignal(ValueObject* obj,
                                    ValueSignalNumber num,
                                    const gchar* message) {
  /* In order to access the signal identifiers, we need to get a hold
     of the class structure first. */
  ValueObjectClass* klass = VALUE_OBJECT_GET_CLASS(obj);
  /* Check that the given num is valid (abort if not).
     Given that this file is the module actually using this utility,
     you can consider this check superfluous (but useful for
     development work). */
  g_assert((num < E_SIGNAL_COUNT) && (num >= 0));
  dbg("Emitting signal id %d, with message '%s'", num, message);
  /* This is the simplest way of emitting signals. */
  g_signal_emit(/* Instance of the object that is generating this
                   signal. This is passed as the first parameter
                   to the signal handler (eventually). But obviously
                   when speaking about D-Bus, a signal caught on the
                   other side of D-Bus is first processed by
                   the GLib-wrappers (the object proxy) and only then
                   processed by the signal handler. */
                obj,
                /* Signal id for the signal to generate. These are
                   stored inside the class state structure. */
                klass->signals[num],
                /* Detail of signal. Because we are not using detailed
                   signals, we leave this at zero (default). */
                0,
                /* Data to marshal into the signal. In our case it is
                   just one string. */
                message);
  /* g_signal_emit returns void, so we cannot check for success. */
  /* Done emitting signal. */
}

To avoid having to check the threshold values in multiple places in the source code, the threshold value is also implemented as a separate function. Emitting the "threshold exceeded" signal is still up to the caller. glib-dbus-signals/server.c

/**
 * Utility to check the given integer against the thresholds.
 * Returns TRUE if thresholds are not exceeded, FALSE otherwise.
 *
 * Used in the setter functions below.
 */
static gboolean value_object_thresholdsOk(ValueObject* obj,
                                          gint value) {
  /* Thresholds are in class state data, get access to it */
  ValueObjectClass* klass = VALUE_OBJECT_GET_CLASS(obj);
  return ((value >= klass->thresholdMin) &&
          (value <= klass->thresholdMax));
}

Both utility functions are then used from within the respective set functions, one of which is presented below: glib-dbus-signals/server.c

/**
 * Function that gets called when someone tries to execute "setvalue1"
 * over the D-Bus. (Actually the marshalling code from the stubs gets
 * executed first, but eventually they execute this function.)
 */
gboolean value_object_setvalue1(ValueObject* obj, gint valueIn,
                                                  GError** error) {
  dbg("Called (valueIn=%d)", valueIn);
  g_assert(obj != NULL);
  /* Compare the current value against old one. If they're the same,
     we do not need to do anything (except return success). */
  if (obj->value1 != valueIn) {
    /* Change the value. */
    obj->value1 = valueIn;
    /* Emit the "changed_value1" signal. */
    value_object_emitSignal(obj, E_SIGNAL_CHANGED_VALUE1, "value1");
    /* If new value falls outside the thresholds, emit
       "outofrange_value1" signal as well. */
    if (!value_object_thresholdsOk(obj, valueIn)) {
      value_object_emitSignal(obj, E_SIGNAL_OUTOFRANGE_VALUE1,
                                   "value1");
    }
  }
  /* Return success to GLib/D-Bus wrappers. In this case we do not need
     to touch the supplied error pointer-pointer. */
  return TRUE;
}

The role of the value1 string parameter that is sent along both of the signals above can raise a question. Sending the signal origin name with the signal allows you to reuse the same callback function in the client. This kind of "source naming" is rarely useful but it allows for writing a slightly shorter client program.

The implementation of setvalue2 is almost identical, but deals with the gdouble parameter.

The getvalue functions are identical to the versions before as is the Makefile.

Next, the server is built and started on the background (in preparation for testing with dbus-send):

[sbox-DIABLO_X86: ~/glib-dbus-signals] > make server
dbus-binding-tool --prefix=value_object --mode=glib-server \
  value-dbus-interface.xml > value-server-stub.h
cc -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include \
 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -g -Wall \
 -DG_DISABLE_DEPRECATED -DNO_DAEMON -DPROGNAME=\"server\" \
 -c server.c -o server.o
cc server.o -o server -ldbus-glib-1 -ldbus-1 -lgobject-2.0 -lglib-2.0
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh ./server &
[1] 15293
server:main Connecting to the Session D-Bus.
server:main Registering the well-known name (org.maemo.Platdev_ex)
server:main RequestName returned 1.
server:main Creating one Value object.
server:value_object_class_init: Called
server:value_object_class_init: Creating signals
server:value_object_class_init: Binding to GLib/D-Bus
server:value_object_class_init: Done
server:value_object_init: Called
server:main Registering it on the D-Bus.
server:main Ready to serve requests (daemonizing).
server: Not daemonizing (built with NO_DAEMON-build define)
[sbox-DIABLO_X86: ~/glib-dbus-signals] >

The next step is to test the setvalue1 method:

[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 /GlobalValue org.maemo.Value.setvalue1 int32:10
server:value_object_setvalue1: Called (valueIn=10)
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
method return sender=:1.38 -> dest=:1.41
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 /GlobalValue org.maemo.Value.setvalue1 int32:-200
server:value_object_setvalue1: Called (valueIn=-200)
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
server:value_object_emitSignal: Emitting signal id 2, with message 'value1'
method return sender=:1.38 -> dest=:1.42

After this, the next step is to test the setvalue2 (with doubles). At this point, the threshold triggering can behave in an unexpected manner:

[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 /GlobalValue org.maemo.Value.setvalue2 double:100.5
server:value_object_setvalue2: Called (valueIn=100.500)
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
method return sender=:1.38 -> dest=:1.44
[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-send \
 --type=method_call --print-reply --dest=org.maemo.Platdev_ex \
 /GlobalValue org.maemo.Value.setvalue2 double:101
server:value_object_setvalue2: Called (valueIn=101.000)
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
method return sender=:1.38 -> dest=:1.45

Because the threshold testing logic truncates the gdouble before testing against the (integer) thresholds, a value of 100.5 is detected as 100 and it still fits within the thresholds.

Instead of printing out the emitted signal names, the enumeration values of the emitted signals are printed. This could be rectified with a small enumeration to string table, but it was omitted from the program for simplicity.

Other than seeing the server messages about emitting the signals, there is no trace of them being sent or received. This is because dbus-send does not listen for signals. A separate tool (dbus-monitor) can be used for tracing signals.

[edit] Catching Signals in GLib/D-Bus Clients

In order to receive D-Bus signals in the client, you need to perform several tasks per signal. This is because dbus-bindings-tool does not generate any code for signals. The aim is to make the GLib wrappers emit GLib signals whenever an interesting D-Bus signal arrives. This also means that registering the interest for a particular D-Bus signal is necessary.

When implementing the callbacks for the signals, the prototype must be implemented correctly. Because the signals are sent with one attached string value, the callbacks receive at least the string parameter. Besides the signal attached arguments, the callback receives the proxy object, through which the signal was received, and optional user-specified data (which is not used in this example, so it is always NULL). glib-dbus-signals/client.c

/**
 * Signal handler for the "changed" signals. These are sent by the
 * Value-object whenever its contents change (whether within
 * thresholds or not).
 *
 * Like before, we use strcmp to differentiate between the two
 * values, and act accordingly (in this case by retrieving
 * synchronously the values using getvalue1 or getvalue2).
 *
 * NOTE: Because we use synchronous getvalues, it is possible to get
 *       this code stuck if for some reason the server is stuck
 *       in an eternal loop.
 */
static void valueChangedSignalHandler(DBusGProxy* proxy,
                                      const char* valueName,
                                      gpointer userData) {
  /* Because method calls over D-Bus can fail, we need to check
     for failures. The server might be shut down in the middle of
     things, or might act badly in other ways. */
  GError* error = NULL;
 
  g_print(PROGNAME ":value-changed (%s)\n", valueName);
 
  /* Find out which value changed and act accordingly. */
  if (strcmp(valueName, "value1") == 0) {
    gint v = 0;
    /* Execute the RPC to get value1. */
    org_maemo_Value_getvalue1(proxy, &v, &error);
    if (error == NULL) {
      g_print(PROGNAME ":value-changed Value1 now %d\n", v);
    } else {
      /* You could interrogate the GError further to find out exactly
         what the error was, but in our case, we just ignore the
         error with the hope that some day (preferably soon), the
         RPC succeeds again (server comes back on the bus). */
      handleError("Failed to retrieve value1", error->message, FALSE);
    }
  } else {
    gdouble v = 0.0;
    org_maemo_Value_getvalue2(proxy, &v, &error);
    if (error == NULL) {
      g_print(PROGNAME ":value-changed Value2 now %.3f\n", v);
    } else {
      handleError("Failed to retrieve value2", error->message, FALSE);
    }
  }
  /* Free up error object if one was allocated. */
  g_clear_error(&error);
}

The callback first determines what was the source value that caused the signal to be generated. For this, it uses the string argument of the signal. It then retrieves the current value using the respective RPC methods (getvalue1 or getvalue2) and prints out the value.

If any errors occur during the method calls, the errors are printed out, but the program continues to run. If an error does occur, the GError object needs to be freed (performed with g_clear_error). The program does not terminate on RPC errors, because the condition might be temporary (the Value object server might be restarted later).

The code for the outOfRangeSignalHandler callback has been omitted, because it does not contain anything beyond what valueChangedSignalHandler demonstrates.

Registering for the signals is a two-step process. First, it is necessary to register the interest in the D-Bus signals, and then install the callbacks for the respective GLib signals. This is done within main: glib-dbus-signals/client.c

/**
 * The test program itself.
 *
 * 1) Setup GType/GSignal
 * 2) Create GMainLoop object
 * 3) Connect to the Session D-Bus
 * 4) Create a proxy GObject for the remote Value object
 * 5) Register signals that we're interested from the Value object
 * 6) Register signal handlers for them
 * 7) Start a timer that launches timerCallback once per second.
 * 8) Run main-loop (forever)
 */
int main(int argc, char** argv) {
 
  /*... Listing cut for brevity ...*/
 
  remoteValue =
    dbus_g_proxy_new_for_name(bus,
                              VALUE_SERVICE_NAME, /* name */
                              VALUE_SERVICE_OBJECT_PATH, /* obj path */
                              VALUE_SERVICE_INTERFACE /* interface */);
  if (remoteValue == NULL) {
    handleError("Couldn't create the proxy object",
                "Unknown(dbus_g_proxy_new_for_name)", TRUE);
  }
 
  /* Register the signatures for the signal handlers.
     In our case, we have one string parameter passed to use along
     the signal itself. The parameter list is terminated with
     G_TYPE_INVALID (i.e., the GType for string objects. */
 
  g_print(PROGNAME ":main Registering signal handler signatures.\n");
 
  /* Add the argument signatures for the signals (needs to be done
     before connecting the signals). This may go away in the future,
     when the GLib-bindings do automatic introspection over the
     D-Bus, but for now we need the registration phase. */
  { /* Create a local scope for variables. */
 
    int i;
    const gchar* signalNames[] = { SIGNAL_CHANGED_VALUE1,
                                   SIGNAL_CHANGED_VALUE2,
                                   SIGNAL_OUTOFRANGE_VALUE1,
                                   SIGNAL_OUTOFRANGE_VALUE2 };
    /* Iterate over all the entries in the above array.
       The upper limit for i might seem strange at first glance,
       but is quite common idiom to extract the number of elements
       in a statically allocated arrays in C.
       NOTE: The idiom does not work with dynamically allocated
             arrays. (Or rather it works, but the result is probably
             not what you expect.) */
    for (i = 0; i < sizeof(signalNames)/sizeof(signalNames[0]); i++) {
      /* Because the function does not return anything, we cannot check
         for errors here. */
      dbus_g_proxy_add_signal(/* Proxy to use */
                              remoteValue,
                              /* Signal name */
                              signalNames[i],
                              /* Receives one string argument */
                              G_TYPE_STRING,
                              /* Termination of the argument list */
                              G_TYPE_INVALID);
    }
  } /* end of local scope */
 
  g_print(PROGNAME ":main Registering D-Bus signal handlers.\n");
 
  /* We connect each of the following signals one at a time,
     because we are using two different callbacks. */
 
  /* Again, no return values, cannot hence check for errors. */
  dbus_g_proxy_connect_signal(/* Proxy object */
                              remoteValue,
                              /* Signal name */
                              SIGNAL_CHANGED_VALUE1,
                              /* Signal handler to use. Note that the
                                 typecast is just to make the compiler
                                 happy about the function, because the
                                 prototype is not compatible with
                                 regular signal handlers. */
                              G_CALLBACK(valueChangedSignalHandler),
                              /* User-data (we don't use any). */
                              NULL,
                              /* GClosureNotify function that is
                                 responsible in freeing the passed
                                 user-data (we have no data). */
                              NULL);
 
  dbus_g_proxy_connect_signal(remoteValue, SIGNAL_CHANGED_VALUE2,
                              G_CALLBACK(valueChangedSignalHandler),
                              NULL, NULL);
 
  dbus_g_proxy_connect_signal(remoteValue, SIGNAL_OUTOFRANGE_VALUE1,
                              G_CALLBACK(outOfRangeSignalHandler),
                              NULL, NULL);
 
  dbus_g_proxy_connect_signal(remoteValue, SIGNAL_OUTOFRANGE_VALUE2,
                              G_CALLBACK(outOfRangeSignalHandler),
                              NULL, NULL);
 
  /* All signals are now registered and we're ready to handle them. */
 
  g_print(PROGNAME ":main Starting main loop (first timer in 1s).\n");
 
  /* Register a timer callback that does RPC sets on the values.
     The userdata pointer is used to pass the proxy object to the
     callback so that it can launch modifications to the object. */
  g_timeout_add(1000, (GSourceFunc)timerCallback, remoteValue);
 
  /* Run the program. */
  g_main_loop_run(mainloop);
  /* Because the main loop is not stopped (by this code), we shouldn't
     ever get here. The program might abort() for other reasons. */
 
  /* If it does, return failure as exit code. */
  return EXIT_FAILURE;
}

When adding the argument signatures for the signals (with dbus_g_proxy_add_signal), one needs to be very careful with the parameter list. The signal argument types must be exactly the same as are sent from the server (irrespective of the argument specification in the interface XML). This is because the current version of dbus-bindings-tool does not generate any checks to enforce signal arguments based on the interface. In this simple case, only one string is received with each different signal, so this is not a big issue. The implementation for the callback function needs to match the argument specification given to the _add_signal-function, otherwise data layout on the stack is incorrect and can cause problems.

Building the client is done in the same manner as before (make client). Because the server is still (hopefully) running on the background, the client is now started in the same session:

[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh ./client
client:main Connecting to Session D-Bus.
client:main Creating a GLib proxy object for Value.
client:main Registering signal handler signatures.
client:main Registering D-Bus signal handlers.
client:main Starting main loop (first timer in 1s).
server:value_object_setvalue1: Called (valueIn=-80)
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
client:timerCallback Set value1 to -80
server:value_object_setvalue2: Called (valueIn=-120.000)
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
client:timerCallback Set value2 to -120.000
client:value-changed (value1)
server:value_object_getvalue1: Called (internal value1 is -80)
client:value-changed Value1 now -80
client:value-changed (value2)
server:value_object_getvalue2: Called (internal value2 is -120.000)
client:value-changed Value2 now -120.000
client:out-of-range (value2)!
client:out-of-range Value 2 is outside threshold
server:value_object_setvalue1: Called (valueIn=-70)
server:value_object_emitSignal: Emitting signal id 0, with message 'value1'
client:timerCallback Set value1 to -70
server:value_object_setvalue2: Called (valueIn=-110.000)
server:value_object_emitSignal: Emitting signal id 1, with message 'value2'
server:value_object_emitSignal: Emitting signal id 3, with message 'value2'
client:timerCallback Set value2 to -110.000
client:value-changed (value1)
server:value_object_getvalue1: Called (internal value1 is -70)
client:value-changed Value1 now -70
client:value-changed (value2)
server:value_object_getvalue2: Called (internal value2 is -110.000)
client:value-changed Value2 now -110.000
...

The client starts with the timer callback being executed once per second (as before). Each iteration, it calls the setvalue1 and setvalue2 RPC methods with increasing values. The number for value2 is intentionally set below the minimum threshold, so that it causes an outofrange_value2 signal to be emitted. For each set, the changed_value signals are also emitted. Whenever the client receives either of the value change signals, it performs a getvalue RPC method call to retrieve the current value and print it out.

This continues until the client is terminated.

[edit] Tracing D-Bus Signals

Sometimes seeing which signals are actually carried on the buses can be useful, especially when adding signal handlers for signals that are emitted from undocumented interfaces. The dbus-monitor tool attaches to the D-Bus daemon, and asks it to watch for signals and report them back to the tool, so that it can decode the signals automatically, as they appear on the bus.

While the server and client are still running, the next step is to start the dbus-monitor (in a separate session this time) to see whether the signals are transmitted correctly. It should be noted that signals appear on the bus even if there are no clients currently interested in them. In this case, signals are emitted by the server, based on client-issued RPC methods, so if the client is terminated, the signals cease.

[sbox-DIABLO_X86: ~/glib-dbus-signals] > run-standalone.sh dbus-monitor type='signal'
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value1
 string "value1"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value2
 string "value2"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=outofrange_value2
 string "value2"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value1
 string "value1"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value2
 string "value2"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=outofrange_value2
 string "value2"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value1
 string "value1"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value2
 string "value2"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value1
 string "value1"
signal sender=:1.38 -> dest=(null destination) 
 interface=org.maemo.Value; member=changed_value2
 string "value2"

The tool decodes the parameters automatically to the best of its ability (the string parameter for the signals above). It does not know the semantic meaning for the different signals, so sometimes additional testing has to be performed to decide what they actually mean. This is especially true when mapping out undocumented interfaces (for which the source code is not necessarily available).

Some examples of displaying signals on the system bus on a device follow:

  • A device turning off the backlight after inactivity
Nokia-N810-xx-xx:~# run-standalone.sh dbus-monitor --system
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=display_status_ind
 string "dimmed"
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=system_inactivity_ind
 boolean true
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=save_unsaved_data_ind
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=display_status_ind
 string "off"
  • A device coming back to life after a screen tap
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=system_inactivity_ind
 boolean false
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=display_status_ind
 string "on"
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal; 
 interface=com.nokia.mce.signal; member=tklock_mode_ind
 string "unlocked"
  • A device going into offline mode
signal sender=:1.0 -> dest=(null destination)
 path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 interface=org.freedesktop.Hal.Device; member=Condition
   string "ButtonPressed"
   string "power"
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 interface=com.nokia.mce.signal; member=sig_device_mode_ind
   string "offline"
signal sender=:1.26 -> dest=(null destination)
 path=/com/nokia/wlancond/signal;
 interface=com.nokia.wlancond.signal; member=disconnected
   string "wlan0"
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 interface=com.nokia.icd; member=status_changed
   string "SSID"
   string "WLAN_INFRA"
   string "DISCONNECTING"
   string "com.nokia.icd.error.network_error"
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 interface=org.bluez.Adapter; member=ModeChanged
   string "off"
signal sender=:1.25 -> dest=(null destination)
 path=/com/nokia/btcond/signal;
 interface=com.nokia.btcond.signal; member=hci_dev_down
   string "hci0"
  • A device going back into normal mode
signal sender=:1.0 -> dest=(null destination)
 path=/org/freedesktop/Hal/devices/computer_logicaldev_input_0;
 interface=org.freedesktop.Hal.Device; member=Condition
   string "ButtonPressed"
   string "power"
signal sender=:1.3 -> dest=(null destination) path=/com/nokia/mce/signal;
 interface=com.nokia.mce.signal; member=sig_device_mode_ind
   string "normal"
signal sender=:1.39 -> dest=(null destination)
 path=/org/kernel/class/firmware/hci_h4p;
 interface=org.kernel.kevent; member=add
signal sender=:1.39 -> dest=(null destination)
 path=/org/kernel/class/firmware/hci_h4p;
 interface=org.kernel.kevent; member=remove
signal sender=:1.25 -> dest=(null destination)
 path=/com/nokia/btcond/signal;
 interface=com.nokia.btcond.signal; member=hci_dev_up
   string "hci0"
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 interface=org.bluez.Adapter; member=ModeChanged
   string "connectable"
signal sender=:1.22 -> dest=(null destination) path=/org/bluez/hci0;
 interface=org.bluez.Adapter; member=NameChanged
   string "Nokia N810"
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 interface=com.nokia.icd; member=status_changed
   string "SSID"
   string "WLAN_INFRA"
   string "CONNECTING"
   string ""
signal sender=:1.26 -> dest=(null destination)
 path=/com/nokia/wlancond/signal;
 interface=com.nokia.wlancond.signal; member=connected
   string "wlan0"
   array [
      byte 0
      byte 13
      byte 157
      byte 198
      byte 120
      byte 175
   ]
   int32 536870912
signal sender=:1.100 -> dest=(null destination) path=/com/nokia/eap/signal;
 interface=com.nokia.eap.signal; member=auth_status
   uint32 4
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 interface=com.nokia.icd; member=proxies
   uint32 0
   string ""
   int32 0
   string ""
   int32 0
   string ""
   int32 0
   string ""
   int32 0
   string ""
   int32 0
   array [ ]
   string ""
signal sender=:1.28 -> dest=(null destination) path=/com/nokia/icd;
 interface=com.nokia.icd; member=status_changed
   string "SSID"
   string "WLAN_INFRA"
   string "CONNECTED"
   string ""

It is also possible to send signals from the command line, which is useful when some feature of a device needs to be emulated inside the SDK. This (like RPC method calls) can be performed with the dbus-send tool. An example of this kind of simulation was given in the LibOSSO section.