我正在構建一個簡單的基于 Qt 的應用程式,用于監視和連接到 WiFi 網路。我正在通過其 D-Bus API 與 Connman 進行互動,并且能夠掃描可用網路、打開/關閉技術并按預期注冊代理。我目前無法在呼叫 Connman RequestInput 方法時(嘗試連接到受保護/安全網路時)提供請求的密碼,因為我不確定如何將 RequestInput 方法與 Qt 中的函式系結。
以下是一些概述該方法的指示性代碼:
//Create a QDBusConnection systemBus() object
QDBusConnection connection = QDBusConnection::systemBus();
//Ensure the systemBus() connection is established
if (!connection.isConnected()) {
qDebug() << "Connection error.";
}
//Create a Connman Manager D-Bus API interface object
QDBusInterface manager("net.connman", "/", "net.connman.Manager", connection);
//Register an agent with the Connman Manager API
manager.call("RegisterAgent", QVariant::fromValue(QDBusObjectPath("/test/agent")));
//Attempt to bind the mySlot() function with the net.connman.Agent RequestInput method
//This does not currently work
connection.connect("",
"/test/agent",
"net.connman.Agent",
"RequestInput",
this,
SLOT(mySlot(QDBusObjectPath, QVariantMap)));
//Create a Connman Service D-Bus API interface object (for a specific WiFi Service)
QDBusInterface service("net.connman",
"/net/connman/service/[WIFI SERVICE]",
"net.connman.Service",
connection);
//Attempt to connect to the secure WiFi network
//Note: this network has not previously been connected, so the RequestInput method is guaranteed to be called
service.call("Connect");
QVariantMap myClass::mySlot(const QDBusObjectPath &path, const QVariantMap &map)
{
//Connman Agent RequestInput() method received
}
如上所述,嘗試將 /test/agent 路徑、net.connman.Agent 介面和 RequestInput 方法系結到 mySlot() 函式不起作用;沒有報告錯誤,但從未呼叫 mySlot() 函式。如果我使用 QDBUS_DEBUG 環境變數啟用除錯,我會收到以下資訊:
QDBusConnectionPrivate(0xffff74003a00) got message (signal): QDBusMessage(type=MethodCall, service=":1.3", path="/test/agent", interface="net.connman.Agent", member="RequestInput", signature="oa{sv}", contents=([ObjectPath: /net/connman/service/[WIFI SERVICE]], [Argument: a{sv} {"Passphrase" = [Variant: [Argument: a{sv} {"Type" = [Variant(QString): "psk"], "Requirement" = [Variant(QString): "mandatory"]}]]}]) )
以上正是我所期望的;正在為帶有 oa{sv} 簽名的 net.connman.Agent 介面上的 /test/agent 路徑呼叫 RequestInput 方法。
我的問題:
- 如何“連接”到 RequestInput 方法呼叫,以便我的 mySlot() 函式可以決議 RequestInput 方法資料?
- 如何從 mySlot() 中回傳所需的 QVariantMap?
uj5u.com熱心網友回復:
從除錯輸出看來,ConnMan 正在執行MethodCall,但QDBusConnection::connect()用于處理 DBus 信號,這就是您的插槽未被呼叫的原因。
您需要將實作net.connman.Agent介面的物件注冊到相應的路徑上,以便 ConnMan 可以呼叫您的方法:
class ConnManAgent : public QObject {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "net.connman.Agent")
public:
ConnManAgent(QObject *parent = nullptr);
Q_INVOKABLE QVariantMap RequestInput(const QDBusObjectPath &, const QVariantMap &);
// ... Rest of the net.connman.Agent interface
};
然后在相應的路徑上注冊它:
connection.registerObject(
QStringLiteral("/test/agent"),
new ConnManAgent(this),
QDBusConnection::ExportAllInvokables);
這會將所有標記Q_INVOKABLE為 DBus 的方法匯出。您也可以將它們標記為Q_SLOTS并使用ExportAllSlots,這主要取決于您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/448168.html
