/* * Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see #include #include KylinDBus::KylinDBus(MainWindow *mainWindow, QObject *parent) :QObject(parent) { this->mw = mainWindow; getObjectPath(); //获取dbus中 lan 与 WiFi 的device路径 getPhysicalCarrierState(0); //初始化获取网线插入状态 getLanHwAddressState(); //获取有线网Mac地址 getWiredCardName(); QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), QString("/org/freedesktop/NetworkManager"), QString("org.freedesktop.NetworkManager"), QString("DeviceAdded"), mw, SLOT(onWirelessDeviceAdded(QDBusObjectPath) ) ); QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), QString("/org/freedesktop/NetworkManager"), QString("org.freedesktop.NetworkManager"), QString("DeviceRemoved"), mw, SLOT(onWirelessDeviceRemoved(QDBusObjectPath) ) ); QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), QString(wiredPath.path()), QString("org.freedesktop.NetworkManager.Device.Wired"), QString("PropertiesChanged"), this, SLOT(onLanPropertyChanged(QVariantMap) ) ); if (wirelessPath.path() != ""){ QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), QString(wirelessPath.path()), QString("org.freedesktop.NetworkManager.Device.Wireless"), QString("PropertiesChanged"), this, SLOT(onWifiPropertyChanged(QVariantMap) ) ); QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), QString(wirelessPath.path()), QString("org.freedesktop.NetworkManager.Device.Wireless"), QString("AccessPointAdded"), this, SLOT(onAccessPointAdded(QDBusObjectPath) ) ); getWirelessCardName();//获取无线网卡名称 } time = new QTimer(this); time->setTimerType(Qt::PreciseTimer); QObject::connect(time, SIGNAL(timeout()), this, SLOT(slot_timeout())); } void KylinDBus::getObjectPath() { QDBusInterface m_interface( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", QDBusConnection::systemBus() ); QDBusReply> obj_reply = m_interface.call("GetAllDevices"); QList obj_paths = obj_reply.value(); foreach (QDBusObjectPath obj_path, obj_paths){ QDBusInterface interface( "org.freedesktop.NetworkManager", obj_path.path(), "org.freedesktop.DBus.Introspectable", QDBusConnection::systemBus() ); QDBusReply reply = interface.call("Introspect"); if(reply.value().indexOf("org.freedesktop.NetworkManager.Device.Wired") != -1){ wiredPath = obj_path; } else if (reply.value().indexOf("org.freedesktop.NetworkManager.Device.Wireless") != -1){ wirelessPath = obj_path; isWirelessCardOn = true; } } } void KylinDBus::getPhysicalCarrierState(int n) { QDBusInterface interface( "org.freedesktop.NetworkManager", wiredPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply reply = interface.call("Get", "org.freedesktop.NetworkManager.Device.Wired", "Carrier"); if (reply.value().toString() == "true"){ isWiredCableOn = true; } else if (reply.value().toString() == "false"){ isWiredCableOn = false; } else { syslog(LOG_ERR, "Error occurred when get the property 'Carrier' of Wired"); qDebug()<<"Error occurred when get the property 'Carrier' of Wired"; } if (n == 1){ this->mw->onPhysicalCarrierChanged(isWiredCableOn);} } void KylinDBus::getLanHwAddressState() { QDBusInterface lanInterface( "org.freedesktop.NetworkManager", wiredPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply lanReply = lanInterface.call("Get", "org.freedesktop.NetworkManager.Device.Wired", "HwAddress"); dbusLanMac = lanReply.value().toString(); } void KylinDBus::getWiredCardName() { QDBusInterface lanInterface( "org.freedesktop.NetworkManager", wiredPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply lanReply = lanInterface.call("Get", "org.freedesktop.NetworkManager.Device", "Interface"); dbusLanCardName = lanReply.value().toString(); } void KylinDBus::getWirelessCardName() { QDBusInterface lanInterface( "org.freedesktop.NetworkManager", wirelessPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply lanReply = lanInterface.call("Get", "org.freedesktop.NetworkManager.Device", "Interface"); dbusWiFiCardName = lanReply.value().toString(); } void KylinDBus::getLanIp(QString netName) { QDBusInterface m_interface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings", "org.freedesktop.NetworkManager.Settings", QDBusConnection::systemBus() ); QDBusReply> m_reply = m_interface.call("ListConnections"); QList m_objNets = m_reply.value(); foreach (QDBusObjectPath objNet, m_objNets){ QDBusInterface m_interface("org.freedesktop.NetworkManager", objNet.path(), "org.freedesktop.NetworkManager.Settings.Connection", QDBusConnection::systemBus()); QDBusMessage result = m_interface.call("GetSettings"); const QDBusArgument &dbusArg1st = result.arguments().at( 0 ).value(); //DBus type : a{sa{sv}}, a map with a key of QString, which maps to another map of QString,QVariant QMap> map; dbusArg1st >> map; for (int i =0;i<2;i++){ if(map.values().at(1).values().at(i).toString() == netName){ for(QString key : map.keys() ){ QMap innerMap = map.value(key); //qDebug() << "Key: " << key; if (key == "ipv4") { for (QString inner_key : innerMap.keys()){ if (inner_key == "address-data"){ const QDBusArgument &dbusArg2nd = innerMap.value(inner_key).value(); QMap m_map; dbusArg2nd.beginArray(); while (!dbusArg2nd.atEnd()){ dbusArg2nd >> m_map;// append map to a vector here if you want to keep it } dbusArg2nd.endArray(); //qDebug()<<" " << m_map.value("address").toString(); dbusLanIpv4 = m_map.value("address").toString(); } } } } dbusLanIpv6 = map.values().at(3).values().at(5).toString(); break; } } } } void KylinDBus::getWifiMac(QString netName, int num) { dbusWifiMac = ""; // //将wifi名转为utf-8 十进制形式 // std::vector vec; // int len_encoded; // int len_netName = netName.size(); // for (int i=0;ifromUnicode(netName.at(i)).toHex(); // len_encoded = encoded.size(); // qDebug()<<"debug: 11"<> reply = interface.call("GetAllAccessPoints"); QList objPaths = reply.value(); foreach (QDBusObjectPath objPath, objPaths){ QDBusInterface ssid_interface( "org.freedesktop.NetworkManager", objPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply ssid_replys = ssid_interface.call("Get", "org.freedesktop.NetworkManager.AccessPoint", "Ssid"); QString str_name = ssid_replys.value().toString(); if (str_name == netName){ QDBusInterface path_interface( "org.freedesktop.NetworkManager", objPath.path(), "org.freedesktop.DBus.Properties", QDBusConnection::systemBus() ); QDBusReply path_reply = path_interface.call("Get", "org.freedesktop.NetworkManager.AccessPoint", "HwAddress"); dbusWifiMac = path_reply.value().toString(); } } } void KylinDBus::getActWifiMac(QString netName) { dbusWifiMac = ""; QDBusInterface m_interface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings", "org.freedesktop.NetworkManager.Settings", QDBusConnection::systemBus() ); QDBusReply> m_reply = m_interface.call("ListConnections"); QList m_objNets = m_reply.value(); foreach (QDBusObjectPath objNet, m_objNets){ QDBusInterface m_interface("org.freedesktop.NetworkManager", objNet.path(), "org.freedesktop.NetworkManager.Settings.Connection", QDBusConnection::systemBus()); QDBusMessage reply = m_interface.call("GetSettings"); const QDBusArgument &dbusArg = reply.arguments().at( 0 ).value(); QMap> map; dbusArg >> map; for(QString key : map.keys() ){ QMap innerMap = map.value(key); if (key == "connection"){ for (QString inner_key : innerMap.keys()){ if (inner_key == "id"){ if (netName == innerMap.value(inner_key).toString()){ for (QString subkey : map.keys()){ QMap subMap = map.value(subkey); if (subkey == "802-11-wireless"){ dbusWifiMac = subMap.value("seen-bssids").toString(); } } //end for (QString subkey : map.keys()) } } } } } } // end foreach (QDBusObjectPath objNet, m_objNets) } int KylinDBus::getAccessPointsNumber() { QDBusInterface interface( "org.freedesktop.NetworkManager", wirelessPath.path(), "org.freedesktop.NetworkManager.Device.Wireless", QDBusConnection::systemBus() ); QDBusReply> reply = interface.call("GetAllAccessPoints"); QList objPaths = reply.value(); // foreach (QDBusObjectPath objPath, objPaths){ // qDebug()<<"debug: *****path is: "< reply = interface.call("Get", "org.freedesktop.NetworkManager.Device", "ActiveConnection"); return 0; } void KylinDBus::onLanPropertyChanged(QVariantMap qvm) { if (!isRunningFunction) { isRunningFunction = true; time->start(3000); QString str = qvm.value("Carrier").toString(); if (str == "false" || str == "true"){ getPhysicalCarrierState(1); } } else { a = 0; } } void KylinDBus::slot_timeout() { isRunningFunction = false; time->stop(); } void KylinDBus::onWifiPropertyChanged(QVariantMap qvm) { //qDebug()<<"debug: *************"<