Maemo Linux手机平台系列分析:8 Maemo平台开发之 使用Glib绑定的D-Bus

 

使用 Glib 封装过的 D-Bus
这部分的内容:
  • GObject介绍
  • 使用XML文件定义D-Bus接口
  • 自动生成proxy/stub代码
  • 创建一个简单的D-Bus对象
  • 通过D-Bus发布一个GType类型
  • 客户端如何使用Glib封装过的D-Bus
  • D-Bus的自省功能
 
GObject 介绍
为了在运行时把 GTK+ widgets 绑定到解释语言,一些牛人就用 C 语言实现了相对难以理解的面向对象的机制。根据你个人的爱好,你可以把这种面向对象称之为 GObject 或者 GType GType GObject 的底层基础。 GObject/GType Glib 的一部分,并且单独编译成一个库: libgobject ,对于 GObject 的详细介绍,大家可以到网上 google 一下,另外还要仔细阅读其代码,非常巧妙的实现!
这里的例子:实现一个非继承的类,类的接口去访问和修改两个私有的成员变量: value1 vaule2, value1 32 位的整数, value2 是一个 gdouble 类型的数据。
我们需要实现类构造函数和对象构造函数。这里,这两个构造函数都比较简短。如果你了解 C++, 你会发现这里怎么有两个构造函数呢? C++ 只有一个啊!不错,这就是 GObject 别扭的地方。慢慢你就会习惯的,当你很喜欢它后,你可能觉得 C++ 是个另类。因为你了解 C++ 的构造函数比较早,这个晚了些时候。
 
使用 XML 文件定义 D-Bus 接口:
 
由于我们主要的目的是做一个可以在 D-Bus 中使用的对象,因此我们从一个最简单的地方入手,通过 dbus-binding-tool 工具,这个工具会自动由 XML 文件生成 client server 端的代码。我们就是把需要的接口安装 XML 语法定义在一个 XML 文件中就行了。
定义 方法 ,即函数:用 method 来包含,其 name 是指定这个函数的名字,这个指定的名字会拷贝到生成的 stub 代码里面;函数 method 带一个参数: new_value, <arg /> 包含。其中对于参数类型 (type) 的的形式,需要参考 D-Bus 的参数类型定义。
    <!-- setvalue1(int newValue): sets value1 -->
    <method name="setvalue1">
      <arg type="i" name="new_value" direction="in"/>
    </method>
 
// direction 表示入口参数还是出口参数,in:入口; out: 出口参数,如果不指定in或者out, 默认为in;
// type 的指定要参考D-Bus的类型定义,见下表;
 
其中,最难搞的就是要严格指定参数 (arg) 的类型( type ),正是这个 type 来定义参数的数据类型, D-Bus 定义的参数类型如下:

Conventional Name
Code
Description
INVALID
0 (ASCII NUL)
Not a valid type code, used to terminate signatures
BYTE
121 (ASCII 'y')
8-bit unsigned integer
BOOLEAN
98 (ASCII 'b')
Boolean value, 0 is FALSE and 1 is TRUE . Everything else is invalid.
INT16
110 (ASCII 'n')
16-bit signed integer
UINT16
113 (ASCII 'q')
16-bit unsigned integer
INT32
105 (ASCII 'i')
32-bit signed integer
UINT32
117 (ASCII 'u')
32-bit unsigned integer
INT64
120 (ASCII 'x')
64-bit signed integer
UINT64
116 (ASCII 't')
64-bit unsigned integer
DOUBLE
100 (ASCII 'd')
IEEE 754 double
STRING
115 (ASCII 's')
UTF-8 string (must be valid UTF-8). Must be nul terminated and contain no other nul bytes.
OBJECT_PATH
111 (ASCII 'o')
Name of an object instance
SIGNATURE
103 (ASCII 'g')
A type signature
ARRAY
97 (ASCII 'a')
Array
STRUCT
114 (ASCII 'r'), 40 (ASCII '('), 41 (ASCII ')')
Struct
VARIANT
118 (ASCII 'v')
Variant type (the type of the value is part of the value itself)
DICT_ENTRY
101 (ASCII 'e'), 123 (ASCII '{'), 125 (ASCII '}')
Entry in a dict or map (array of key-value pairs)

 
我用蓝色标出的是比较常用的,你要根据你参数的需要,把上述第二列的类型码写到 XML 文件中去。开始你可能不习惯,慢慢就好了。
另外, D-Bus 本身并不限制返回参数的个数,但是 C 语言只支持一个返回参数,因此如果你需要把其它需要携带回来的参数当作出口参数处理。其它的一些高级语言并不像 C 语言这样有限制。
 
下面是 D-Bus 函数所能支持的参数类型: ( 括号中是 Glib 对应的类型 ):
  • b: boolean (gboolean)
  • y: 8-bit unsigned integer (guint8)
  • q/n: 16-bit unsigned/signed integer (guint16/gint16)
  • u/i: 32-bit unsigned/signed integer (guint32/gint32)
  • t/x: 64-bit unsigned/signed integer (guint64/gint64)
  • d: IEEE 754 double precision floating point number (gdouble)
  • s: UTF-8 encoded text string with NUL termination (only one NUL allowed) (gchar* with additional restrictions)
  • a: Array of the following type specification (case-dependent)
  • o/g/r/(/)/v/e/{/}: Complex types, please see the official D-Bus documentation on type signatures.
从这个列表可以看出,我们上面定义的函数: setvalue1 有一个 32 位整形参数 (new_value). 这里定义的参数名称: new_value 将会影响到所生成的 stub 代码,对于生成文档和 D-Bus 自省是非常有用的。
下面我们再定义另外一个函数: getvalue1 , 这个函数用于返回当前对象的整数成员的值,没有入口参数,只有出口参数 : cur_value 。具体定义见下:
    <!-- getvalue1(): returns the first value (int) -->
    <method name="getvalue1">
      <arg type="i" name="cur_value" direction="out"/>
    </method>
我们已经知道, D-Bus method 是隶属于 interface 的,就说一个 interface 可以有 N method, XML 中,我们把这些 method 元素包含在 interface 元素中,借以表达这种隶属关系。这个 interface 的名字属性是可选的,你可以指定,也可以不指定,我们强烈推荐你写上这个名字,一是防止各个模块的 interface 重名,另外一个重要的功用是为了 introspection.
再进一步, method 隶属于 interface, 那么 interface 又属于谁呢?隶属于 object, 一个 object 可以有 N interface. 我们把 interface 元素包含在 node 元素内。在 XML 中, node 是最顶层的元素了。我们这个例子里面,只实现了一个 interface(binding tool 会自动增加 introspection 接口的,因此不必在 XML 文件中指定 ) ,到此,我们就写完了一个最基本的 XML 文件,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<node>
 <interface name="org.maemo.Value">
    <!-- getvalue1(): returns the first value (int) -->
    <method name="getvalue1">
      <arg type="i" name="cur_value" direction="out"/>
    </method>
    <!-- setvalue1(int newValue): sets value1 -->
    <method name="setvalue1">
      <arg type="i" name="new_value" direction="in"/>
    </method>
 </interface>
</node>
 
再对上面的最基本的 XML 接口定义文件做些扩充:增加 DTD 扫描,其实这个可以不用。另外还有就是增加了 2 个函数定义: get_value2, set_value2
如果你需要写 XML 文件,强烈建议你在一个模板基础上写,就是拿个没有基本语法错误的模板,然后增加修改你的接口,这样容易成功,不要白手起家,不要在这个 XML 文件上面做过多的纠缠。
<?xml version="1.0" encoding="UTF-8" ?>
 
<!-- This maemo code example is licensed under a MIT-style license,
     that can be found in the file called "License" in the same
     directory as this file.
     Copyright (c) 2007 Nokia Corporation. All rights reserved. -->
 
<!-- If you keep the following DOCTYPE tag in your interface
     specification, xmllint can fetch the DTD over the Internet
     for validation automatically. // 自动对你定义的接口、方法、参数进行扫描,检查其合法性,如果你没有联网,就不要加这个了 -->
<!DOCTYPE node PUBLIC
 "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"
 "http://standards.freedesktop.org/dbus/1.0/introspect.dtd">
 
<!-- This file defines the D-Bus interface for a simple object, that
     will hold a simple state consisting of two values (one a 32-bit
     integer, the other a double).
 
     The interface name is "org.maemo.Value".
     One known reference implementation is provided for it by the
     "/GlobalValue" object found via a well-known name of
     "org.maemo.Platdev_ex". -->
 
<node>
 <interface name="org.maemo.Value">
 
    <!-- Method definitions -->
 
    <!-- getvalue1(): returns the first value (int) -->
    <method name="getvalue1">
      <!-- NOTE Naming arguments is not mandatory, but is recommended
                so that D-Bus introspection tools are more useful.
                Otherwise the arguments will be automatically named
                "arg0", "arg1" and so on. -->
      <arg type="i" name="cur_value" direction="out"/>
    </method>
 
    <!-- getvalue2(): returns the second value (double) -->
    <method name="getvalue2">
      <arg type="d" name="cur_value" direction="out"/>
    </method>
 
    <!-- setvalue1(int newValue): sets value1 -->
    <method name="setvalue1">
      <arg type="i" name="new_value" direction="in"/>
    </method>
 
    <!-- setvalue2(double newValue): sets value2 -->
    <method name="setvalue2">
      <arg type="d" name="new_value" direction="in"/>
    </method>
 
 </interface>
</node>
 
写完 XML 文件后,下面我们是不是可以使用 D-Bus 工具来生成代码了?别急!我们还要对 XML 文件做些自动检查。主要检查两个方面:是否符合 XML1.0 规范;验证 XML 结构(即:元素是否成对匹配)。结构验证的规则是由 DTD(Document Type Definition) 文档规定的。 D-Bus 规定的 XML 格式如下:

<!-- DTD for D-BUS Introspection data -->
<!-- (C) 2005-02-02 David A. Wheeler; released under the D-BUS licenses,
         GNU GPL version 2 (or greater) and AFL 1.1 (or greater) -->
 
<!-- see D-BUS specification for documentation -->
 
<!ELEMENT node (interface*,node*)>
<!ATTLIST node name CDATA #REQUIRED>
 
<!ELEMENT interface (annotation*,method*,signal*,property*)>
<!ATTLIST interface name CDATA #REQUIRED>
 
<!ELEMENT method (annotation*,arg*)>
<!ATTLIST method name CDATA #REQUIRED>
 
<!ELEMENT arg EMPTY>
<!ATTLIST arg name CDATA #IMPLIED>
<!ATTLIST arg type CDATA #REQUIRED>
<!-- Method arguments SHOULD include "direction",
     while signal and error arguments SHOULD not (since there's no point).
     The DTD format can't express that subtlety. -->
<!ATTLIST arg direction (in|out) "in">
 
<!ELEMENT signal (arg,annotation)>
<!ATTLIST signal name CDATA #REQUIRED>
 
<!ELEMENT property (annotation)> <!-- AKA "attribute" -->
<!ATTLIST property name CDATA #REQUIRED>
<!ATTLIST property type CDATA #REQUIRED>
<!ATTLIST property access (read|write|readwrite) #REQUIRED>
 
<!ELEMENT annotation EMPTY> <!-- Generic metadata -->
<!ATTLIST annotation name CDATA #REQUIRED>
<!ATTLIST annotation value CDATA #REQUIRED>
 

然后根据这个模板对你定义的 XML 文档进行规则检查。
仅仅检查了 DTD 有效性,还不完美:因为 DTD 类型检查只是对语法做检查,并不能对语义 / 含义做检查。
为了检查语义,我们使用另外一个工具: checkxml, 把它写到你的 makefile 中:
# One extra target (which requires xmllint, from package libxml2-utils)
# is available to verify the well-formedness and the structure of the
# interface definition xml file.
#
# Use the 'checkxml' target to run the interface XML through xmllint
# verification. You'll need to be connected to the Internet in order
# for xmllint to retrieve the DTD from fd.o (unless you setup local
# catalogs, which are not covered here).
 
# ... Listing cut for brevity ...
 
# Interface XML name (used in multiple targets)
interface_xml := value-dbus-interface.xml
 
# ... Listing cut for brevity ...
 
# Special target to run DTD validation on the interface XML. Not run
# automatically (since xmllint isn't always available and also needs
# Internet connectivity).
checkxml: $(interface_xml)
      @xmllint --valid --noout $<
      @echo $< checks out ok
检查语义:
 
[sbox-CHINOOK_X86: ~/glib-dbus-sync] > make checkxml
value-dbus-interface.xml checks out ok
 
为了验证 DTD/checkxml 能否起到作用,我们简单修改一下上面的 XML 文件,增加一个 DTD 中没有规定的节点 </invalidElement> , 并且去掉一个 method 开始标签,看看效果:
[sbox-CHINOOK_X86: ~/glib-dbus-sync] > make checkxml
value-dbus-interface.xml:36: element invalidElement: validity error :
 No declaration for element invalidElement
    </invalidElement>
              ^
value-dbus-interface.xml:53: parser error :
 Opening and ending tag mismatch: method line 39 and interface
 </interface>
              ^
value-dbus-interface.xml:54: parser error :
 Opening and ending tag mismatch: interface line 22 and node
</node>
       ^
value-dbus-interface.xml:55: parser error :
 Premature end of data in tag node line 21
 
^
make: *** [checkxml] Error 1
上面,第一个被发现的错误 ( 验证错误 ) ,是由于 XML 文件不符合 DTD 的规定导致的。第二个 ( 解析错误 ) 则是没有正确的书写 XML
完成 XML 文件的两方面的检查后,我们开始生成代码。这个生成的代码我们叫它“ glue” 代码,什么意思呢? glue 的本意是粘合,就是通过 glue 代码完成从 Glib D-Bus 的映射关系:
图:
XML 文件,运行 dbus-binding-tool 命令,辅以不同的参数,可以生成 client 侧和 sever 侧的 stub 代码,生成的 client stub server stub 代码正好构成了 Client/Server 结构。由这两个 stub 来完成 Glib D-Bus 的互通,由于应用程序使用 Glib ,进而完成应用程序与 D-Bus 的互通。我们把 makefile 改写一下:
# Define a list of generated files so that they can be cleaned as well
cleanfiles := value-client-stub.h /
              value-server-stub.h
 
# ... Listing cut for brevity ...
 
# If the interface XML changes, the respective stub interfaces will be
# automatically regenerated. Normally this would also mean that your
# builds would fail after this since you'd be missing implementation
# code.
# 这里的—prefix前缀会反映到你的stub代码中,会在生成的结构变量和函数名称中添加这个前缀,这个前缀大家不要省略,因为这里仅仅是生成头文件,实际的实现代码需要我们自己写,但是我们需要拷贝这里的声明到.c文件中,如果不指定这个前缀,并且有多个模块的话,会冲突的;
 
# mode 指定为glib-server就生成server stub代码;指定为glib-client
# 则生成client stub代码
 
# 生成sever stub代码
value-server-stub.h: $(interface_xml)
      dbus-binding-tool --prefix=value_object --mode=glib-server /
       $< > $@
 
# 生成client stub代码
value-client-stub.h: $(interface_xml)
      dbus-binding-tool --prefix=value_object --mode=glib-client /
       $< > $@
 
# ... Listing cut for brevity ...
 
clean:
      $(RM) $(targets) $(cleanfiles) *.o
这里生成的 client 侧的代码是想通过 Glib 去访问对象的实现,而 server 侧代码则是实现对象。
生成两个头文件 :
[sbox-CHINOOK_X86: ~/glib-dbus-sync] > make value-server-stub.h value-client-stub.h
dbus-binding-tool --prefix=value_object --mode=glib-server /
 value-dbus-interface.xml > value-server-stub.h
dbus-binding-tool --prefix=value_object --mode=glib-client /
 value-dbus-interface.xml > value-client-stub.h
[sbox-CHINOOK_X86: ~/glib-dbus-sync] > ls -la value*stub.h
-rw-rw-r-- 1 user user 5184 Nov 21 14:02 value-client-stub.h
-rw-rw-r-- 1 user user 10603 Nov 21 14:02 value-server-stub.h
在开始实现实际的对象之前,我们先来瞄一眼生成的代码,看看到底什么妖蛾子。先看看 server 侧的 stub 文件:
/* Generated by dbus-binding-tool; do not edit! */
 
 /*... Listing cut for brevity ...*/
 
#include <dbus/dbus-glib.h>
static const DBusGMethodInfo dbus_glib_value_object_methods[] = {
 { (GCallback) value_object_getvalue1,
    dbus_glib_marshal_value_object_BOOLEAN__POINTER_POINTER, 0 },
 { (GCallback) value_object_getvalue2,
    dbus_glib_marshal_value_object_BOOLEAN__POINTER_POINTER, 47 },
 { (GCallback) value_object_setvalue1,
    dbus_glib_marshal_value_object_BOOLEAN__INT_POINTER, 94 },
 { (GCallback) value_object_setvalue2,
    dbus_glib_marshal_value_object_BOOLEAN__DOUBLE_POINTER, 137 },
} ;
 
const DBusGObjectInfo dbus_glib_value_object_object_info = {
 0,
 dbus_glib_value_object_methods,
  4,
"org.maemo.Value /0 getvalue1 /0 S /0 cur_value /0 O /0F/0 N /0 i /0/0 "
"org.maemo.Value /0 getvalue2 /0 S /0 cur_value /0 O /0F/0 N /0 d /0/0 "
"org.maemo.Value /0 setvalue1 /0 S /0 new_value /0 I /0 i /0/0 "
"org.maemo.Value /0 setvalue2 /0 S /0 new_value /0 I /0 d /0/0/0 " ,
" /0 " ,
" /0 "
} ;
[ The method table and object introspection data (glib-dbus-sync/value-server-stub.h) ]
在这个头文件中,生成的 4 个函数: value_object_getvalue1 , value_object_getvalue2 , value_object_setvalue1 value_object_setvalue2 . 需要我们去实现;在函数表 dbus_glib_value_object_methods ,由函数地址; marshalling 函数(用于在 Glib D-Bus 之间 marshal 数据)组成。
Marshaling 的作用就是参数从一种形式转换为另外一种形式,主要是为了两种不同参数能互相兼容地传递。 Marshalling 技术是在大多数 RPC 中运用的。由于 Glib D-Bus 各自有不同的类型系统,如果我们手动去写这种转换代码,是很乏味和无聊的。 Binding tool 可以帮忙解决这个问题。
上述头文件中还有一个比较有意思的东西: xxx_object_info 结构变量,这个结构变量是要注册给 D-Bus daemon 的,并且把它发布在通道 (bus) ( 然后 client 侧才能发现并调用其函数 ) ,这个过程其实和我们的生活很贴近,比如公交车上的电视广告:厂家把商品 ( 看作 object) 的广告,送到广告发布商那里,广告商把该广告投放到公交车 ( 看作 D-Bus daemon) 的电视中 , 消费者 ( 看作 client) 看到了该广告,可能购买这个产品 ( 调用 object 的函数 ) 上面很长的字符串将会安装 interface 规范的样式进行压缩编码,对于函数名、参数等也是按照这样编码的。在这个头文件中还有很多自省的代码,这也是非常重要的部分,最后再解释。
正如头文件的第一句提示:这是 dbus-binding-tool 自动生成的代码,请不要编辑它。因为每次编译都回自动生成这个文件,你如果修改了这里的头文件,会被覆盖掉的。所以最好不要修改。那如何使用这里的代码呢,你只要拷贝粘帖这里的代码到 .c 文件中就行了。
下面我们就继续:实现在 server stub 函数表中调用的具体函数。 We'll next continue with the server implementation for the functions that are called via the method table.
 
定义一个对象,这个过程就是 GObject 的八股文:
/* This defines the per-instance state.
 
   Each GObject must start with the 'parent' definition so that common
   operations that all GObjects support can be called on it. */
typedef struct {
 /* The parent class object state. */
 GObject parent; // 要继承自GObject,这样才能用GObject的便利
 /* Our first per-object state variable. */
 gint value1;
 /* Our second per-object state variable. */
 gdouble value2;
} ValueObject;
 
/* Per class state.
 
   For the first Value implementation we only have the bare minimum,
   that is, the common implementation for any GObject class. */
typedef struct {
 /* The parent class state. */
 GObjectClass parent;
} ValueObjectClass;
下面接着定义一些宏,全是用 G_TYPE_ 开头的,你不必知道的太多,会套用就行,如果想知道的很详细,可以参考: http://maemo.org/api_refs/4.0/gobject/index.html.
/* Forward declaration of the function that will return the GType of
   the Value implementation. Not used in this program since we only
   need to push this over the D-Bus. */
GType value_object_get_type(void);
 
/* Macro for the above. It is common to define macros using the
   naming convention (seen below) for all GType implementations,
   and that's why we're going to do that here as well. */
 
/* 下面这些定义其实就是“八股文”,固定的格式,你只要套用就行,等熟悉了后,你也不会一个个写,而是拷贝模板。
*/
#define VALUE_TYPE_OBJECT (value_object_get_type())
 
#define VALUE_OBJECT(object) /
        (G_TYPE_CHECK_INSTANCE_CAST((object), /
         VALUE_TYPE_OBJECT, ValueObject))
#define VALUE_OBJECT_CLASS(klass) /
        (G_TYPE_CHECK_CLASS_CAST((klass), /
         VALUE_TYPE_OBJECT, ValueObjectClass))
#define VALUE_IS_OBJECT(object) /
        (G_TYPE_CHECK_INSTANCE_TYPE((object), /
         VALUE_TYPE_OBJECT))
#define VALUE_IS_OBJECT_CLASS(klass) /
        (G_TYPE_CHECK_CLASS_TYPE((klass), /
         VALUE_TYPE_OBJECT))
#define VALUE_OBJECT_GET_CLASS(obj) /
        (G_TYPE_INSTANCE_GET_CLASS((obj), /
         VALUE_TYPE_OBJECT, ValueObjectClass))
 
/* Utility macro to define the value_object GType structure. */
G_DEFINE_TYPE ( ValueObject, value_object, G_TYPE_OBJECT)
宏定义完成后,下面我们就着手实现实例初始化和类初始化,类的初始化中我们调用了 D-Bus 的接口函数,把我们的对象信息注册给了 D-Bus 了,就是暴露在通道( bus )中了,这样,客户端就能找到这个对象了。
/**
 * Since the stub generator will reference the functions from a call
 * table, the functions must be declared before the stub is included.
 */
gboolean value_object_getvalue1(ValueObject* obj, gint* value_out,
                                                  GError** error);
gboolean value_object_getvalue2(ValueObject* obj, gdouble* value_out,
                                                  GError** error);
gboolean value_object_setvalue1(ValueObject* obj, gint value_in,
                                                  GError** error);
gboolean value_object_setvalue2(ValueObject* obj, gdouble value_in,
                                                  GError** error);
 
/**
 * Pull in the stub for the server side.
 */
#include "value-server-stub.h"
 
 /*... Listing cut for brevity ...*/
 
/**
 * Per object initializer
 *
 * Only sets up internal state (both values set to zero)
 */
static void value_object_init(ValueObject* obj) {
 dbg("Called");
 
 g_assert(obj != NULL);
 
 obj->value1 = 0;
 obj->value2 = 0.0;
}
 
/**
 * Per class initializer
 *
 * 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) {
 
 dbg("Called");
 
 g_assert(klass != NULL);
 
 dbg("Binding to GLib/D-Bus");
 
 /* Time to bind this GType into the GLib/D-Bus wrappers.
     NOTE: This is not yet "publishing" the object on the D-Bus, but
           since it is only allowed to do this once per class
           creation, the safest place to put it is in the class
           initializer.
           Specifically, this function adds "method introspection
           data" to the class so that methods can be called over
           the D-Bus. */
 dbus_g_object_type_install_info(VALUE_TYPE_OBJECT,
                                 &dbus_glib_value_object_object_info);
 
 dbg("Done");
 /* All done. Class is ready to be used for instantiating objects */
}
 
下面我们实现具体的 get /set 函数,也就是 object 提供的 methods 。请注意:函数名和参数都要和 stub 头文件中的一模一样 :
/**
 * Function that gets called when someone tries to execute "setvalue1"
 * over the D-Bus. (Actually the marshaling code from the stubs gets
 * executed first, but they will eventually execute this function.)
 *
 * NOTE: If you change the name of this function, the generated
 *       stubs will no longer find it! On the other hand, if you
 *       decide to modify the interface XML, this is one of the places
 *       that you'll have to modify as well.
 *       This applies to the next four functions (including this one).
 */
gboolean value_object_setvalue1(ValueObject* obj, gint valueIn,
                                                  GError** error) {
 
 dbg("Called (valueIn=%d)", valueIn);
 
 g_assert(obj != NULL);
 
 /* Change the value. */
 obj->value1 = valueIn;
 
 /* Return success to GLib/D-Bus wrappers. In this case we don't need
     to touch the supplied error pointer-pointer. */
 return TRUE;
}
 
/**
 * Function that gets executed on "setvalue2".
 * Other than this function operating with different type input
 * parameter (and different internal value), all the comments from
 * set_value1 apply here as well.
 */
gboolean value_object_setvalue2(ValueObject* obj, gdouble valueIn,
                                                  GError** error) {
 
 dbg("Called (valueIn=%.3f)", valueIn);
 
 g_assert(obj != NULL);
 
 obj->value2 = valueIn;
 
 return TRUE;
}
 
/**
 * Function that gets executed on "getvalue1".
 */
gboolean value_object_getvalue1(ValueObject* obj, gint* valueOut,
                                                  GError** error) {
 
 dbg("Called (internal value1 is %d)", obj->value1);
 
 g_assert(obj != NULL);
 
 /* Check that the target pointer is not NULL.
     Even is the only caller for this will be the GLib-wrapper code,
     we cannot trust the stub generated code and should handle the
     situation. We will terminate with an error in this case.
 
     Another option would be to create a new GError, and store
     the error condition there. */
 g_assert(valueOut != NULL);
 
 /* Copy the current first value to caller specified memory. */
 *valueOut = obj->value1;
 
 /* Return success. */
 return TRUE;
}
 
/**
 * Function that gets executed on "getvalue2".
 * (Again, similar to "getvalue1").
 */
gboolean value_object_getvalue2(ValueObject* obj, gdouble* valueOut,
                                                  GError** error) {
 
 dbg("Called (internal value2 is %.3f)", obj->value2);
 
 g_assert(obj != NULL);
 g_assert(valueOut != NULL);
 
  *valueOut = obj->value2;
 return TRUE;
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值