ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

QDBus使用

2021-10-19 11:04:53  阅读:283  来源: 互联网

标签:com dbus Test QDBus 使用 test include my


QDBus使用

一、服务端注册

// main.cpp
#include <QCoreApplication>
#include <QtDBus/QDBusConnection>
#include <QDebug>
#include <QtDBus/QDBusError>
#include <iostream>
#include "test.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //建立到session bus的连接
    QDBusConnection connection = QDBusConnection::systemBus();
    //在session bus上注册名为com.nde.chattr的服务
    if(!connection.registerService("com.my.test"))
    {
        qDebug() << "error:" << connection.lastError().message();
        exit(-1);
    }

    Test object;
    // 注册名为/ndeChattr的对象,把类Object所有槽函数导出为object的method
    connection.registerObject("/object", "com.my.test", &object, QDBusConnection::ExportAllSlots);

    return a.exec();
}
//test.h
#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QStringList>
#include <QSettings>

class Test : public QObject
{
    Q_OBJECT
    //定义Interface名称为com.my.test
    Q_CLASSINFO("D-Bus Interface", "com.my.test")
public:
    explicit Test(QObject *parent = nullptr);

public slots:
    void value();  

private:
    int process_count;
};

#endif // TEST_H
//test.cpp
#include "ndechattr.h"
#include <iostream>

Test::Test(QObject *parent) :
    QObject(parent),
    process_count(0)
{
    
}

void Test::value()
{
	std::cout << process_count << std::endl;
}

二、配置服务端自动启动

方法一:当客户端调用到接口时自动启动服务

编写配置文件/usr/share/dbus-1/services/com.my.test.service

[D-BUS Service]
Name=com.my.test
Exec=/usr/bin/test

方法二:开机自启动服务

/usr/lib/systemd/system/test.service

[Unit]
Description=My Test Service

[Service]
ExecStart=/usr/bin/test
Restart=always

[Install]
WantedBy=multi-user.target

/etc/dbus-1/system.d/com.nde.chattr.conf

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

  <!-- Only root can own the service -->
  <policy user="root">
    <allow own="com.my.test"/>
  </policy>

  <policy context="default">
    <allow send_destination="com.my.test" send_interface="org.freedesktop.DBus.Properties"/>
    <allow send_destination="com.my.test" send_interface="org.freedesktop.DBus.Introspectable"/>
    <allow send_destination="com.my.test" send_interface="com.my.test"/>
  </policy>

</busconfig>

设定启动服务
systemctl enable test.service
systemctl start test.service

三、客户端调用:dbus-send

dbus-send --system --print-reply --dest=com.my.test /object com.my.test.value

标签:com,dbus,Test,QDBus,使用,test,include,my
来源: https://blog.csdn.net/qq_39392008/article/details/120841492

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有