「Qtの基礎 - D-Bus」の版間の差分

ナビゲーションに移動 検索に移動
編集の要約なし
336行目: 336行目:
https://github.com/PacktPublishing/Hands-On-Embedded-Programming-with-Qt/blob/master/Chapter10/DBusBruteForce/MyObject.cpp<br>
https://github.com/PacktPublishing/Hands-On-Embedded-Programming-with-Qt/blob/master/Chapter10/DBusBruteForce/MyObject.cpp<br>
<br>
<br>
==== Firewalld ====
==== Firewalldの利用 ====
以下の例では、Firewalldにおいて、以下に示すようなコマンドと同等のものをD-Busを使用して実行している。<br>
以下の例では、Firewalldにおいて、以下に示すようなコマンドと同等のものをD-Busを使用して実行している。<br>
  sudo firewall-cmd --permanent --zone=<ゾーン名> --add-port=<ポート番号>/tcp
  sudo firewall-cmd --permanent --zone=<ゾーン名> --add-port=<ポート番号>/tcp
409行目: 409行目:
   
   
     return 0;
     return 0;
}
</syntaxhighlight>
<br>
==== 構造体の送信 ====
以下の例では、D-Busを使用して構造体を送信している。<br>
<syntaxhighlight lang="c++">
// DBusSender.hファイル
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDebug>
struct Hoge {
    QString    str;
    QStringList list;
    int        integer;
    double      fpoint;
};
// D-Bus向けマーシャリング関数
QDBusArgument &operator<<(QDBusArgument &argument, const Hoge &hoge)
{
    argument.beginStructure();
    argument << hoge.str << hoge.list << hoge.integer << hoge.fpoint;
    argument.endStructure();
    return argument;
}
// D-Bus向けマーシャリング関数
const QDBusArgument &operator>>(const QDBusArgument &argument, Hoge &hoge)
{
    argument.beginStructure();
    argument >> hoge.str >> hoge.list >> hoge.integer >> hoge.fpoint;
    argument.endStructure();
    return argument;
}
class DBusSender : public QObject
{
    Q_OBJECT
public:
    DBusSender(QObject *parent = nullptr) : QObject(parent) {}
    void sendStructure(const Hoge &hoge) {
      QDBusMessage message = QDBusMessage::createMethodCall(
            "org.example.hoge",  // D-Busサービス名
            "/org/example/hoge",  // D-Busオブジェクト名
            "org.example.hoge",  // D-Busインターフェース名
            "sendstructure"      // D-Busインターフェースメソッド名
      );
      QVariant variant;
      variant.setValue(hoge);
      message << variant;
      QDBusConnection::sessionBus().send(message);
    }
};
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// main.cppファイル
#include "DBusSender.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // 送信する構造体の型をD-Busシステムに登録
    qDBusRegisterMetaType<Hoge>();
    DBusSender sender;
    // 送信データの作成
    Hoge hoge;
    hoge.str    = "Hello, D-Bus!";
    hoge.list    = QStringList{"item1", "item2", "item3"};
    hoge.integer = 100;
    hoge.fpoint  = 3.14f;
    // データを送信
    sender.sendStructure(hoge);
    qDebug() << "Structure sent via D-Bus";
    return a.exec();
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>

案内メニュー