ubuntu 下dbus的环境搭建和使用
2021/7/24 7:37:26
本文主要是介绍ubuntu 下dbus的环境搭建和使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && make install
client.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- const int RES_SUCCESS = -1;
- const int RES_FAILED = 0;
- int my_dbus_initialization(char const * _bus_name, DBusConnection ** _conn) {
- DBusError err;
- int ret;
- dbus_error_init(&err);
- *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)) {
- printf("Connection Error\n");
- dbus_error_free(&err);
- return RES_FAILED;
- }
- ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
- if(dbus_error_is_set(&err)){
- printf("Requece name error \n");
- dbus_error_free(&err);
- return RES_FAILED;
- }
- if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
- return RES_FAILED;
- }
- return RES_SUCCESS;
- }
- int my_dbus_send_sigal(DBusConnection * conn) {
- dbus_uint32_t serial = 0;
- DBusMessage* msg;
- DBusMessageIter args;
- char sigvalue[20] = "liyiwen";
- msg = dbus_message_new_signal("/test/signal/Object", // object name
- "test.signal.Type", // interface name
- "Test"); // name of signal
- if (NULL == msg) {
- printf("Message Null");
- return RES_FAILED;
- }
- dbus_message_iter_init_append(msg, &args);
- printf("%s\n", sigvalue);
- dbus_uint32_t my_age = 10;
- if(!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &my_age)) {
- printf("Out of memory\n");
- return RES_FAILED;
- }
- if(!dbus_connection_send(conn, msg, &serial)) {
- printf("Out of memory");
- return RES_FAILED;
- }
- dbus_connection_flush(conn);
- dbus_message_unref(msg);
- return RES_SUCCESS;
- }
- int main(int agrc, char** argv)
- {
- DBusConnection * conn;
- printf("Start\n");
- if (RES_FAILED == my_dbus_initialization("test.method.client", &conn)) {
- exit(1);
- }
- my_dbus_send_sigal(conn);
- while(1){sleep(10);}
- return 0;
- }
- sercer.c
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dbus/dbus.h>
- #include <unistd.h>
- const int RES_SUCCESS = -1;
- const int RES_FAILED = 0;
- int my_dbus_initialization(char const * _bus_name, DBusConnection **_conn) {
- DBusError err;
- int ret;
- dbus_error_init(&err);
- *_conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
- if(dbus_error_is_set(&err)) {
- printf("Connection Error(%s) \n", err.message);
- dbus_error_free(&err);
- return RES_FAILED;
- }
- ret = dbus_bus_request_name(*_conn, _bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
- if(dbus_error_is_set(&err)){
- printf("Requece name error(%s) \n", err.message);
- dbus_error_free(&err);
- return RES_FAILED;
- }
- if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
- return RES_FAILED;
- }
- return RES_SUCCESS;
- }
- int main(int agrc, char** argv)
- {
- DBusError err;
- DBusMessage* msg;
- DBusMessageIter args;
- dbus_error_init(&err);
- DBusConnection *conn;
- if (RES_FAILED == my_dbus_initialization("test.method.server", &conn)) {
- exit(1);
- }
- dbus_bus_add_match(conn, "type='signal', interface='test.signal.Type'", &err);
- dbus_connection_flush(conn);
- if(dbus_error_is_set(&err)) {
- printf("dbus_bus_add_match err (%s)", err.message);
- return RES_FAILED;
- }
- while(1) {
- dbus_connection_read_write(conn, 0);
- msg = dbus_connection_pop_message(conn);
- if(NULL == msg) {
- sleep(1);
- continue;
- }
- if(dbus_message_is_signal(msg, "test.signal.Type", "Test")) {
- if(!dbus_message_iter_init(msg, &args)) {
- printf("dbus_message_iter_init error, msg has no arguments!\n");
- }
- else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args)){
- printf("not a uint 32 type !\n");
- }
- else {
- dbus_uint32_t my_age = 0;
- dbus_message_iter_get_basic(&args, &my_age);
- printf("Got signal with value %d\n", my_age);
- }
- }
- dbus_message_unref(msg);
- }
- return 0;
- }
- http://dbus.freedesktop.org/doc/dbus-specification.html 这当然是最权威最重要的资料,但我觉得不是一个很好的入门资料。
- http://dbus.freedesktop.org/doc/dbus-tutorial.html 这里面有一些不错的例子,对Names 的解释也很好,但用的是 glib 的 binding,不能探究更底层的动作一度还是让我云里雾里。
- http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html DBus 的 C 编程接口的在线文档,非常棒也非常有用
- http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html 如何用 C API 层面的 DBus ,相见恨晚。
- 转自:https://www.cnblogs.com/oracleloyal/p/5329947.html
这篇关于ubuntu 下dbus的环境搭建和使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22项目:远程温湿度检测系统
- 2024-12-21《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》简介
- 2024-12-21后台管理系统开发教程:新手入门全指南
- 2024-12-21后台开发教程:新手入门及实战指南
- 2024-12-21后台综合解决方案教程:新手入门指南
- 2024-12-21接口模块封装教程:新手必备指南
- 2024-12-21请求动作封装教程:新手必看指南
- 2024-12-21RBAC的权限教程:从入门到实践
- 2024-12-21登录鉴权实战:新手入门教程
- 2024-12-21动态权限实战入门指南