通信示例

简介: 通信示例

main.cpp 文件内容:

#include <dbus/dbus.h>
#include <cstddef>
#include <cstdio>
#include <iostream>
int main(int argc, char *argv[]) {
    (void)argc;
    (void)argv;
    DBusError dbus_error;
    DBusConnection *dbus_conn = nullptr;
    DBusMessage *dbus_msg = nullptr;
    DBusMessage *dbus_reply = nullptr;
    const char *dbus_result = nullptr;
    // Initialize D-Bus error
    ::dbus_error_init(&dbus_error);
    // Connect to D-Bus
    if (nullptr == (dbus_conn = ::dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error))) {
        ::perror(dbus_error.name);
        ::perror(dbus_error.message);
        // Compose remote procedure call
    } else if (nullptr == (dbus_msg = ::dbus_message_new_method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus.Introspectable", "Introspect"))) {
        ::dbus_connection_unref(dbus_conn);
        ::perror("ERROR: ::dbus_message_new_method_call - Unable to allocate memory for the message!");
        // Invoke remote procedure call, block for response
    } else if (nullptr == (dbus_reply = ::dbus_connection_send_with_reply_and_block(dbus_conn, dbus_msg, DBUS_TIMEOUT_USE_DEFAULT, &dbus_error))) {
        ::dbus_message_unref(dbus_msg);
        ::dbus_connection_unref(dbus_conn);
        ::perror(dbus_error.name);
        ::perror(dbus_error.message);
        // Parse response
    } else if (!::dbus_message_get_args(dbus_reply, &dbus_error, DBUS_TYPE_STRING, &dbus_result, DBUS_TYPE_INVALID)) {
        ::dbus_message_unref(dbus_msg);
        ::dbus_message_unref(dbus_reply);
        ::dbus_connection_unref(dbus_conn);
        ::perror(dbus_error.name);
        ::perror(dbus_error.message);
        // Work with the results of the remote procedure call
    } else {
        std::cout << "Connected to D-Bus as \"" << ::dbus_bus_get_unique_name(dbus_conn) << "\"." << std::endl;
        std::cout << "Introspection Result:" << std::endl;
        std::cout << std::endl
                  << dbus_result << std::endl
                  << std::endl;
        ::dbus_message_unref(dbus_msg);
        ::dbus_message_unref(dbus_reply);
        /*
         * Applications must not close shared connections -
         * see dbus_connection_close() docs. This is a bug in the application.
         */
        //::dbus_connection_close(dbus_conn);
        // When using the System Bus, unreference
        // the connection instead of closing it
        ::dbus_connection_unref(dbus_conn);
    }
    return 0;
}点击复制复制失败已复制


编写 Makefile

LDFLAGS += -lhv -ldbus-1
SRCS_DIR := $(wildcard ./*.cpp)
all: clean bin/test
bin/test:
    mkdir -p bin
    $(CXX) $(CFLAGS) $(SRCS_DIR) -o $@ $(LDFLAGS)
clean:
    rm -rf bin
.PHONY: clean 点击复制复制失败已复制


编译并运行:

$ make && ./bin/test点击复制复制失败已复制


得到内容:

Connected to D-Bus as ":1.218".
Introspection Result:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="org.freedesktop.DBus">
    <method name="Hello">
      <arg direction="out" type="s"/>
    </method>
    <method name="RequestName">
      <arg direction="in" type="s"/>
      <arg direction="in" type="u"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="ReleaseName">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="StartServiceByName">
      <arg direction="in" type="s"/>
      <arg direction="in" type="u"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="UpdateActivationEnvironment">
      <arg direction="in" type="a{ss}"/>
    </method>
    <method name="NameHasOwner">
      <arg direction="in" type="s"/>
      <arg direction="out" type="b"/>
    </method>
    <method name="ListNames">
      <arg direction="out" type="as"/>
    </method>
    <method name="ListActivatableNames">
      <arg direction="out" type="as"/>
    </method>
    <method name="AddMatch">
      <arg direction="in" type="s"/>
    </method>
    <method name="RemoveMatch">
      <arg direction="in" type="s"/>
    </method>
    <method name="GetNameOwner">
      <arg direction="in" type="s"/>
      <arg direction="out" type="s"/>
    </method>
    <method name="ListQueuedOwners">
      <arg direction="in" type="s"/>
      <arg direction="out" type="as"/>
    </method>
    <method name="GetConnectionUnixUser">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="GetConnectionUnixProcessID">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="GetAdtAuditSessionData">
      <arg direction="in" type="s"/>
      <arg direction="out" type="ay"/>
    </method>
    <method name="GetConnectionSELinuxSecurityContext">
      <arg direction="in" type="s"/>
      <arg direction="out" type="ay"/>
    </method>
    <method name="GetConnectionAppArmorSecurityContext">
      <arg direction="in" type="s"/>
      <arg direction="out" type="s"/>
    </method>
    <method name="ReloadConfig">
    </method>
    <method name="GetId">
      <arg direction="out" type="s"/>
    </method>
    <method name="GetConnectionCredentials">
      <arg direction="in" type="s"/>
      <arg direction="out" type="a{sv}"/>
    </method>
    <property name="Features" type="as" access="read">
      <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/>
    </property>
    <property name="Interfaces" type="as" access="read">
      <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/>
    </property>
    <signal name="NameOwnerChanged">
      <arg type="s"/>
      <arg type="s"/>
      <arg type="s"/>
    </signal>
    <signal name="NameLost">
      <arg type="s"/>
    </signal>
    <signal name="NameAcquired">
      <arg type="s"/>
    </signal>
  </interface>
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg direction="out" type="s"/>
    </method>
  </interface>
  <interface name="org.freedesktop.DBus.Peer">
    <method name="GetMachineId">
      <arg direction="out" type="s"/>
    </method>
    <method name="Ping">
    </method>
  </interface>
  <node name="org/freedesktop/DBus"/>
</node>
目录
相关文章
|
2月前
|
物联网 C# Windows
看看如何使用 C# 代码让 MQTT 进行完美通信
看看如何使用 C# 代码让 MQTT 进行完美通信
334 0
|
5月前
|
API
socket名称与地址转换api
注意gethostbyname & gethostbyaddr函数已被引入 getaddrinfo 函数弃用。敦促应用程序的开发人员使用 getaddrinfo 函数而不是 gethostbyname。
30 0
|
5月前
关于websocket的理解和它与其他通信方式的区别
关于websocket的理解和它与其他通信方式的区别
81 0
|
10月前
|
XML JSON JavaScript
JsonRPC协议详解(协议介绍、请求示例、响应示例)
RPC(远程过程调用)是一种用于实现分布式系统中不同进程或不同计算机之间通信的技术。它允许我们像调用本地函数一样调用远程计算机上的函数,使得分布式系统的开发变得更加简单和高效。 JsonRPC是一种基于JSON(JavaScript Object Notation)的轻量级远程过程调用协议。与其他RPC协议相比,JsonRPC使用简单的文本格式进行通信,易于阅读和编写,广泛应用于Web服务和分布式系统中。
693 0
[ROS通信机制] ---话题通信之自定义msg类型
[ROS通信机制] ---话题通信之自定义msg类型
128 0
|
JSON 网络协议 安全
基于python实现的CS通信和P2P通信
基于python实现的CS通信和P2P通信
290 0
基于python实现的CS通信和P2P通信
|
API Perl
IP - 射频数据转换器 -05- API使用指南 - ADC状态指示函数
IP - 射频数据转换器 -05- API使用指南 - ADC状态指示函数
261 0
IP - 射频数据转换器 -05- API使用指南 - ADC状态指示函数
|
API 图形学
U3D客户端框架之封装 DeviceUtil 获取设备唯一ID和设备型号
U3D客户端框架之封装 DeviceUtil 获取设备唯一ID和设备型号
|
移动开发 小程序 iOS开发
小程序和h5之间的通讯及注意事项
起因:在小程序中嵌入已有的h5页面,用于快速开发。但是h5和小程序中的事件通讯是需要解决的问题。参照官网后实践之后得出结论。
1060 0
|
Java Android开发
webservice之间通信
webservice之间通信
160 0
webservice之间通信