0

我通过 dbus 访问网络管理器。目前我已经设法列出了所有可用的连接,我有一个包含 ssid 和密码的 json。我想依靠我的 json 来激活连接。例如,“blabla”网络可用,其密码包含“qt is good”,我的 json 包含作为登录名“blabla”和密码“qt is good”我希望能够连接到该网络我的代码扫描wifi,他返回一个QString:

    QDBusInterface NetworkManager("org.freedesktop.NetworkManager",     // get the interface to NetworkManager
                      "/org/freedesktop/NetworkManager",
                      "org.freedesktop.NetworkManager",
                      QDBusConnection::systemBus());
    if(!NetworkManager.isValid())
    {
        qDebug()<< "Failed to connect to the system bus" << NetworkManager.lastError() <<endl ;
    }

    QDBusMessage DbusMsg = NetworkManager.call("GetDevices"); //Get  Devices
   // qDebug() << "GetDevices reply: " << DbusMsg << endl;

    QDBusArgument DbusArg = DbusMsg.arguments().at(0).value<QDBusArgument>();
    if(DbusArg.currentType() != QDBusArgument::ArrayType)
    {
        qDebug()<< "Something went wrong with getting the device list" << endl;
    }
    QList<QDBusObjectPath> pathsLst = qdbus_cast<QList<QDBusObjectPath>>(DbusArg);
    foreach(QDBusObjectPath DbusObjectPath, pathsLst)
    {
        QDBusInterface device("org.freedesktop.NetworkManager",        // creating an interface used to gather this devices properties
                              DbusObjectPath.path(),
                              "org.freedesktop.NetworkManager.Device",
                              QDBusConnection::systemBus());

        if (device.property("DeviceType").toInt() != 2) // 2 is WiFi dev, see https://people.freedesktop.org/~lkundrak/nm-docs/nm-dbus-types.html#NMDeviceType
        {
            continue;
        }
        QDBusInterface wifi_device(   // Get dbus Interface for WiFi Device
                    "org.freedesktop.NetworkManager",
                    DbusObjectPath.path(),
                    "org.freedesktop.NetworkManager.Device.Wireless",
                    QDBusConnection::systemBus());

        QMap<QString, QVariant> ArgList;
        QDBusMessage msg = wifi_device.call("RequestScan", ArgList);
        QThread::sleep(2); //wait for the scan

        msg = wifi_device.call("GetAllAccessPoints"); //Call devices
        QDBusArgument Dbus_Arg_List = msg.arguments().at(0).value<QDBusArgument>();
        QList<QDBusObjectPath> Dbus_Object_Path = qdbus_cast<QList<QDBusObjectPath> >(Dbus_Arg_List);

        foreach(QDBusObjectPath p ,Dbus_Object_Path)
        {
            QDBusInterface ap_interface("org.freedesktop.NetworkManager",
                                        p.path(),
                                        "org.freedesktop.NetworkManager.AccessPoint",
                                        QDBusConnection::systemBus());
            return ap_interface.property("Ssid").toString() ;

        }
    }

我在哪里使用它:

if(error.error == QJsonParseError::NoError)
        {
            QJsonObject json(parsed_object.object());

            QString pass = json.value("pass").toString();
            QString ssid = json.value("ssid").toString();
            QString action = json.value("action").toString();

            QJsonObject machine = json.value("machine").toObject(); // get sub-object
            QString login = machine.value("login").toString();
            QString password = machine.value("password").toString();

            qDebug() << "parsed BARCODE JSON..." << parsed_object << endl;

            if(action.contains("network-configuration"))
            {
                qDebug()<< "Network Requirements" << endl;

                if(machine.contains("login")){
                   if(login == ("test") && password == ("test"))
                   {
                      qDebug()<< "The machine's login and password are correct" << endl;
                      qDebug()<< "wifi available" << ScanWiFi() << endl; // scan available networks
                      if (ScanWiFi() == ssid)
                      {
                          qDebug()<< "deleting... " << ssid << endl ;
                          /*delete the connection
                          ...
                          ...
                          */

                             /*AND*/

                          /*recreate and active connection
                          ...
                          ...
                          */
                          qDebug()<< "active connection :" << ssid << endl ;

                      }
                   }
}

json 示例:

BARCODE: {"action": "network-configuration", "ssid": "truc", "pass": "machin", "machine": {"login": "...", "password": "..."}}
4

0 回答 0