DBus通讯

article/2025/10/1 4:04:36

linux下进程间通信的方式主要有Pipe(管道),FIFO(命名管道),信号,共享内存,消息队列,信号灯等,这些方式各有 各得特点,如管道是linux下命令行中常用的,用于父子进程的通信。但是这些通信方式都比较原始,要属功能最强大的IPC应该是dbus,故查看了一下 dbus的资料,但是资料相对较少,特别是有关python的部分。 1.dbus概念

网上有一篇叫“D-Bus Tutorial”的文章,流传较广。

D-Bus是针对桌面环境优化的IPC(interprocess communication )机制,用于进程间的通信或进程与内核的通信。最基本的D-Bus协议是一对一的通信协议。但在很多情况下,通信的一方是消息总线。消息总线是一个特殊的 应用,它同时与多个应用通信,并在应用之间传递消息。下面我们会在实例中观察消息总线的作用。消息总线的角色有点类似与X系统中的窗口管理器,窗口管理器 既是X客户,又负责管理窗口。

支持dbus的系统都有两个标准的消息总线:系统总线和会话总线。系统总线用于系统与应用的通信。会话总线用于应用之间的通信。网上有一个叫d-feet的python程序,我们可以用它来观察系统中的dbus世界。

图1、由d-feet观察到的D-Bus世界

dbus还提供了两个命令行工具用于dbus测试,dbus-send和dbus-monitor,前一个命令用于测试信号的发送,后一个命令用于监控dbus的数据流。

2.dbus概念

有关dbus的基础知识不在本文的范围内,具体的参见dbus的文档。下面给出dbus常用的流程。

2.1建立服务的流程

 dbus_bus_get(),建立一个dbus连接;

 dbus_bus_request_name(),为这个dbus连接(DbusConnection)起名,这个名字将会成为我们在后续进行远程调用的时候的服务名;

然后我们进入监听循环 -- dbus_connection_read_write();

      从总线上取出消息 -- dbus_connection_pop_message();

      并通过比对消息中的方法接口名和方法名 -- dbus_message_is_method_call();

      如果一致,那么我们跳转到相应的处理中去;

      在相应的处理中,我们会从消息中取出远程调用的参数。并且建立起回传结果的通路 --      reply_to_method_call()。回传动作本身等同于一次不需要等待结果的远程调用。

2.2建立服务的流程

建立好dbus连接之后,为这dbus连接命名,申请一个远程调用通道 -- dbus_message_new_method_call(),注意,在申请远程调用通道的时候,需要填写服务器名,本次调用的接口名,和本次调用名 (方法名)。压入本次调用的参数 -- dbus_message_iter_init_append(); dbus_message_iter_append_basic(),实际上是申请了一个首地址,我们就是把我们真正要传的参数,往这个首地址里面送(送 完之后一般都会判断是否内存越界了)。然后就是启动发送调用并释放发送相关的消息结构 -- dbus_connection_send_with_reply()。这个启动函数中带有一个句柄。我们马上会阻塞等待这个句柄给我们带回总线上回传的 消息。当这个句柄回传消息之后,我们从消息结构中分离出参数。用dbus提供的函数提取参数的类型和参数 -- dbus_message_iter_init(); dbus_message_iter_next(); dbus_message_iter_get_arg_type(); dbus_message_iter_get_basic()。也就达成了我们进行本次远程调用的目的了。
 

2.3发送信号的流程

 建立一个dbus连接之后,为这个dbus连接起名,建立一个发送信号的通道,注意,在建立通道的函数中,需要我们填写该信号的接口名和信号名 -- dbus_message_new_signal()。然后我们把信号对应的相关参数压进去 -- dbus_message_iter_init_append(); dbus_message_iter_append_basic()。然后就可以启动发送了 -- dbus_connection_send(); dbus_connection_flush。

2.4信号接收流程

建立一个dbus连接之后,为这个dbus连接起名,为我们将要进行的消息循环添加匹配条件(就是通过信号名和信号接口名来进行匹配控制的) -- dbus_bus_add_match()。我们进入等待循环后,只需要对信号名,信号接口名进行判断就可以分别处理各种信号了。在各个处理分支上。我们 可以分离出消息中的参数。对参数类型进行判断和其他的处理。
 

3. 一个C语言的示例代码

网上大部分代码都是基于dbus的一个封装库libdbus做的,以及使用glib,gtk的事件循环;为了减少库的依赖,直接使用C语言调用dbus的底层函数编写一个远程调用的示例代码,代码很简单,没使用GObject等一些复杂的库。

远程调用的服务器代码,用于监控,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include
#include
#include
#include
#include
void reply_to_method_call(DBusMessage* msg, DBusConnection* conn)
{
DBusMessage* reply;
DBusMessageIter args;
bool stat = true;
dbus_uint32_t level = 21614;
dbus_uint32_t serial = 0;
char* param = "";
// read the arguments
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message has no arguments!\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!\n");
else
dbus_message_iter_get_basic(&args, ¶m);
printf("Method called with %s\n", param);
// create a reply from the message
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &stat)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &level)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
exit(1);
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
static void
reply_to_Introspect(DBusMessage* msg, DBusConnection* conn)
{
/*反馈的消息*/
char *xml = "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"\n"
"  \n"
"    \n"
"      \n"
"    \n  \n"
"  \n"
"    \n"
"      \n"
"      \n"
"    \n"
"  \n"
"\n";
DBusMessage* reply;
DBusMessageIter args;
bool stat = true;
// create a reply from the message
reply = dbus_message_new_method_return(msg);
// add the arguments to the reply
dbus_message_iter_init_append(reply, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &xml)) {
printf ("Dbus Error: append args error\n");
dbus_message_unref(reply);
return;
}
// send the reply && flush the connection
if (!dbus_connection_send(conn, reply, NULL)) {
printf ("Dbus Error: send error\n");
dbus_message_unref(reply);
return;
}
dbus_connection_flush(conn);
// free the reply
dbus_message_unref(reply);
}
/**
* Server that exposes a method call and waits for it to be called
*/
void listen()
{
DBusMessage* msg;
DBusMessage* reply;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
char* param;
printf("Listening for method calls\n");
// initialise the error
dbus_error_init(&err);
// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
fprintf(stderr, "Connection Null\n");
exit(1);
}
// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "test.method.server",
DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
fprintf(stderr, "Not Primary Owner (%d)\n", ret);
exit(1);
}
// loop, testing for new messages
while (true) {
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't got a message
if (NULL == msg) {
sleep(1);
continue;
}
// check this is a method call for the right interface & method
if (dbus_message_is_method_call(msg, "test.method.Type", "Method"))
reply_to_method_call(msg, conn);
/*实现反射接口*/
if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect"))
reply_to_Introspect(msg, conn);
// free the message
dbus_message_unref(msg);
}
}
int main(int argc, char** argv)
{
listen();
return 0;
}

代码中很关键的一个地方是一个标准接口的实现,该接口虽说无实际意义,仅仅是反射出该session的接口信息,包含各个接口信息和信号信息,但是该信息在python版的dbus中调用很重要,否则python的调用会失败。

编译命令如下

?
1
gcc -o main main.c `pkg-config --cflags --libs dbus-1`

可以用d-feet测试一下:

用dbus-send测试命令如下:

?
1
dbus-send --session --type=method_call --print-reply --dest=test.method.server / test.method.Type.Method

 

客户端代码(及远程调用的代码):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include
#include
#include
#include
#include
/** 
* Call a method on a remote object 
*/ 
void query(char* param)   
{  
DBusMessage* msg;  
DBusMessageIter args;  
DBusConnection* conn;  
DBusError err;  
DBusPendingCall* pending;  
int ret;  
bool stat;  
dbus_uint32_t level;  
printf("Calling remote method with %s\n", param);  
// initialiset the errors  
dbus_error_init(&err);  
// connect to the system bus and check for errors  
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);  
if (dbus_error_is_set(&err)) {   
fprintf(stderr, "Connection Error (%s)\n", err.message);   
dbus_error_free(&err);  
}  
if (NULL == conn) {   
exit(1);   
}  
// request our name on the bus  
ret = dbus_bus_request_name(conn, "test.method.caller", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);  
if (dbus_error_is_set(&err)) {   
fprintf(stderr, "Name Error (%s)\n", err.message);   
dbus_error_free(&err);  
}  
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {   
exit(1);  
}  
// create a new method call and check for errors  
msg = dbus_message_new_method_call("test.method.server", // target for the method call  
"/test/method/Object", // object to call on  
"test.method.Type", // interface to call on  
"Method"); // method name  
if (NULL == msg) {   
fprintf(stderr, "Message Null\n");  
exit(1);  
}  
// append arguments  
dbus_message_iter_init_append(msg, &args);  
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, ¶m)) {  
fprintf(stderr, "Out Of Memory!\n");   
exit(1);  
}  
// send message and get a handle for a reply  
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout  
fprintf(stderr, "Out Of Memory!\n");   
exit(1);  
}  
if (NULL == pending) {   
fprintf(stderr, "Pending Call Null\n");   
exit(1);   
}  
dbus_connection_flush(conn);  
printf("Request Sent\n");  
// free message  
dbus_message_unref(msg);  
// block until we recieve a reply  
dbus_pending_call_block(pending);  
// get the reply message  
msg = dbus_pending_call_steal_reply(pending);  
if (NULL == msg) {  
fprintf(stderr, "Reply Null\n");   
exit(1);   
}  
// free the pending message handle  
dbus_pending_call_unref(pending);  
// read the parameters  
if (!dbus_message_iter_init(msg, &args))  
fprintf(stderr, "Message has no arguments!\n");   
else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args))   
fprintf(stderr, "Argument is not boolean!\n");   
else 
dbus_message_iter_get_basic(&args, &stat);  
if (!dbus_message_iter_next(&args))  
fprintf(stderr, "Message has too few arguments!\n");   
else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))   
fprintf(stderr, "Argument is not int!\n");   
else 
dbus_message_iter_get_basic(&args, &level);  
printf("Got Reply: %d, %d\n", stat, level);  
// free reply   
dbus_message_unref(msg);     
}  
int main(int argc, char** argv)
{
char* param = "no param"; 
query(param);
return 0;
}

 执行结果:

Calling remote method with no param
Request Sent
Got Reply: 1, 21614
 

4.Pthon调用dbus

?

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import dbus
bus = dbus.SessionBus()
bus_obj = bus.get_object('test.method.server', '/')
interface = dbus.Interface(bus_obj, 'test.method.Type')
info = interface.Method()
print info

转载于:https://www.cnblogs.com/oracleloyal/p/5329011.html


http://chatgpt.dhexx.cn/article/DIyua6Fy.shtml

相关文章

Linux DBUS客户端程序

DBUS客户端程序,发送一个信号,信号携带int型数据。信号的object path为"/test/signal/server",interface名为 "test.signal.Type",信号名为"Test"。接收端可以根据这三个属性来判断是否是想接收的信…

四、QtDbus

文章目录 概述一、QtDBus模块Debug备忘单 二、QtDBus类型系统1、QtDBus类型系统简介2、原生类型3、复合类型4、扩展类型系统5、类型系统使用 三、QtDBus常用类1、QDBusMessage2、QDBusConnection3、QDBusInterface4、QDBusReply5、QDBusAbstractAdaptor6、QDBusAbstractInterfa…

Hello Qt——QtDBus快速入门

一、QtDBus简介 QtDBus是一个使用D-Bus协议进行进程间通信的仅在Unix运行的库&#xff0c;是对D-Bus底层API的封装实现。 QtDBus模块提供了使用Qt信号槽机制扩展的接口。要使用QtDBus模块&#xff0c;需要在代码中加入以下代码&#xff1a; #include <QtDBus> 如果使用…

DBUS

DBUS是一种高级的进程间通信机制。DBUS支持进程间一对一和多对多的对等通信&#xff0c;在多对多的通讯时&#xff0c;需要后台进程的角色去分转消息&#xff0c;当一个进程发消息给另外一个进程时&#xff0c;先发消息到后台进程&#xff0c;再通过后台进程将信息转发到目的进…

DBUS是什么 如何使用

DBus提供了一种低延时,低开销,高可用性的进程间通信方式,其以消息作为驱动,采用二进制的协议,实现一对一及多对多的对等通信,避免通信的序列化&#xff08;编码过程&#xff09;过程,提高通信效率.DBus进程通信的核心是提供了一个后台中转守护进程,需要通信的进程首先连接到DBu…

dbus总线通信的原理和使用

1.什么是D-Bus D-Bus是一种高级的进程间通信机制&#xff0c;它由freedesktop.org项目提供&#xff0c;使用GPL许可证发行。D-Bus最主要的用途是在Linux桌面环境为进程提供通信&#xff0c;同时能将Linux桌面环境和Linux内核事件作为消息传递到进程。注册后的进程可通过总线接…

制作maven-archeType

制作maven-archeType 项目背景 解决每次开发新项目&#xff0c;要创建很多目录结构或者从老项目中修改包名&#xff0c;pom等&#xff0c;所以就自己制作一个项目骨架&#xff0c;便于以后新项目目录结构生成。 制作maven-archeType 1.自己开发一个maven-archeType项目&…

[WARNING] No archetype found in remote catalog. Defaulting to internal catalog(已解决)

前言&#xff1a; 遇到问题后&#xff0c;首先&#xff0c;在网络上查找答案。结果告诉我&#xff0c;让我重启IDEA&#xff0c;可是我使用的是CMD命令行&#xff0c;根本没有使用IDEA。或者&#xff0c;告诉我如何操作&#xff0c;却没有解释问题发生的根本原因。于是&#xf…

idea自定义archetype及错误处理

介绍&#xff1a;公司内部会制定自己的规范及包结构。当创建新项目的时候就需要选择骨架&#xff0c;即可生成包结构。本文章简单介绍如何创建&#xff0c;解决遇到的各种问题。 一、创建项目。 idea点击file--->new--->project--->点击左侧的Spring initializr- 图2…

Maven 三种archetype说明合集

Maven 三种archetype说明合集【转载】_maven-archetype-quickstart_太阳神LoveU的博客-CSDN博客 新建Maven project项目时&#xff0c;需要选择archetype。 那么&#xff0c;什么是archetype&#xff1f; archetype的意思就是模板原型的意思&#xff0c;原型是一个Maven项目模…

maven自定义archetype

在开发过程中我们经常会创建一系列结构类似的新项目&#xff0c;这些项目结构和基础配置基本或完全一致&#xff0c;maven就提供了archetype类型来规定新建项目的结构及基础配置&#xff0c;利用archetype就可以快速简单的搭建新项目。 一、创建Maven项目的一般步骤 一般情况下…

自定义Maven Archetype模板工程

文章目录 Maven Archetype介绍什么是Maven Archetype为什么要有模板工程创建模板工程的三种方式 常用的archetypemaven-archetype-quickstartmaven-archetype-webapp 自定义一个Maven模板工程生成模板上传模板到仓库(此步骤可选) 使用模板工程 源码地址&#xff1a;https://git…

如何选择创建Maven的archetype

前言&#xff1a; 在使用IDEA的Maven插件创建我们的maven项目时提供了如图所示的原型&#xff0c;为我们快速创建合适的项目提供了很大的帮助。下面我们将详细介绍各个archetype。 官网对archetype(原型)的介绍&#xff1a; http://maven.apache.org/guides/introduction/in…

Maven model archetype说明

前言 新建一个model&#xff0c;其中有一个选项“Create from archetype”感觉很奇怪&#xff0c;我就来了解一下这些内容&#xff0c;做一下笔记&#xff0c; 什么是Archetype Archetype翻译过来就是&#xff1a;骨架&#xff0c;项目工程骨架。 Archetype是Maven工程的模板…

Maven自定义Archetype项目模板

前言&#xff1a;在某课网上学习到可以使用命令mvn archetype:create-from-project来创建自己的项目模板&#xff08;文中提及的项目模板即 原型 archetype&#xff09;&#xff0c;对此我十分感兴趣。但是&#xff0c;遗憾的是&#xff0c;老师没有介绍如何去构建这样一个特殊…

Maven-Archetype Catalog

当用户以不指定Archetype坐标的方式使用maven-archetype-plugin的时候&#xff0c;会得到一个Archetype列表供选择&#xff0c;这个列表的信息来源于一个名为archetype-catalog.xml的文件&#xff0c;如&#xff1a; archetype-catlog.xml能提供Archetype的信息&#xff0c;那么…

Idea archetype介绍

一、背景 Idea创建项目时&#xff0c;被一堆archetype混淆视听&#xff0c;犹豫不决决定彻底弄懂他 1.关键词 Archetype: 骨架Maven: Java代码编译时的一个工具&#xff08;软件讲究运行时、和编译时两种状态&#xff09; 2.网友介绍 Archetype是Maven工程的模板工具包。一…

Maven Archetype

目录 Maven Archetype工程结构创建Maven Archetype创建支持生成多模块的Maven Archetype在idea中添加自定义Maven Archetype 最开始接触到maven archetype&#xff0c;是在通过idea创建project时&#xff0c;可以选择Maven -> Create from archetype&#xff0c; 在输入proj…

自定义idea archetype

编写 archetype 元数据 通过plugin从现有项目中导出 1、引入plugin <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-archetype-plugin</artifactId><version>3.2.0</version></plugin>2、通过maven…

Maven 三种archetype说明

新建Maven project项目时,需要选择archetype。 那么,什么是archetype? archetype的意思就是模板原型的意思,原型是一个Maven项目模板工具包。一个原型被定义为从其中相同类型的所有其它事情是由一个原始图案或模型。名称配合,因为我们正在努力提供一种系统,该系统提供了…