/* * Copyright (C) 2020 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 #include #include #include #include #include BackThread::BackThread(QObject *parent) : QObject(parent) { cmdConnWifi = new QProcess(this); connect(cmdConnWifi , SIGNAL(readyReadStandardOutput()) , this , SLOT(on_readoutput())); connect(cmdConnWifi , SIGNAL(readyReadStandardError()) , this , SLOT(on_readerror())); cmdConnWifi->start("bash"); cmdConnWifi->waitForStarted(); } BackThread::~BackThread() { cmdConnWifi->close(); } //get the connection state of wired and wireles network IFace* BackThread::execGetIface() { IFace *iface = new IFace(); QString tmpPath = "/tmp/kylin-nm-iface-" + QDir::home().dirName(); QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli -f TYPE,DEVICE,STATE device > " + tmpPath; Utils::m_system(cmd.toUtf8().data()); QFile file(tmpPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { // print information if can not open file ~/.config/kylin-nm-iface syslog(LOG_ERR, "Can't open the file ~/.config/kylin-nm-iface!"); qDebug()<<"Can't open the file ~/.config/kylin-nm-iface!"; } QString txt = file.readAll(); QStringList txtList = txt.split("\n"); file.close(); iface->lstate = 2; iface->wstate = 2; for (int i = 1; i < txtList.size(); i ++) { QString line = txtList.at(i); if (line != "") { int index1 = line.indexOf(" "); QString type = line.left(index1); QString lastStr = line.mid(index1).trimmed(); int index2 = lastStr.indexOf(" "); QString iname = lastStr.left(index2); QString istateStr = lastStr.mid(index2).trimmed(); //只要存在一个有线设备已连接,就不再扫描其他有线设备状态,避免有线被误断开 if (type == "ethernet" && iface->lstate != 0) { // if type is wired network iface->lname = iname; if (istateStr == "unmanaged") { iface->lstate = 2; //switch of wired device is off } if (istateStr == "disconnected" || istateStr == "unavailable") { iface->lstate = 1; //wired network is disconnected } if (istateStr == "connected") { iface->lstate = 0; //wired network is connected } } if (type == "wifi") { // if type is wireless network iface->wname = iname; if (istateStr == "unmanaged" || istateStr == "unavailable") { iface->wstate = 2; //switch of wireless device is off } else if (istateStr == "disconnected") { iface->wstate = 1; //wireless network is disconnected } else if (istateStr == "connected") { iface->wstate = 0; //wireless network is connected } else { //连接中,正在配置 iface->wstate = 3; } } } } return iface; } //turn on the switch of network void BackThread::execEnNet() { char *chr = "nmcli networking on"; Utils::m_system(chr); while (1) { if (execGetIface()->lstate != 2) { sleep(3); emit enNetDone(); emit btFinish(); break; } sleep(1); } } //turn off the switch of wireless network first, then turn off the switch of network void BackThread::execDisNet() { if (execGetIface()->wstate != 2) { char *chr = "nmcli radio wifi off"; Utils::m_system(chr); while (1) { if (execGetIface()->wstate == 2) { emit disWifiDone(); emit btFinish(); break; } sleep(1); } } char *chr1 = "nmcli networking off"; Utils::m_system(chr1); while (1) { if (execGetIface()->lstate == 2) { emit disNetDone(); emit btFinish(); break; } sleep(1); } } //turn on the switch of wireless network void BackThread::execEnWifi() { char *chr1 = "nmcli radio wifi on"; Utils::m_system(chr1); while (1) { if (execGetIface()->wstate != 2) { KylinDBus objKyDbus; while (1) { if (objKyDbus.getAccessPointsNumber() > 0) { // objKyDbus.getAccessPointsNumber()>0 standard can get wireless accesspoints now emit enWifiDone(); emit btFinish(); break; } sleep(2); } break; } sleep(1); } } //turn off the switch of wireless network void BackThread::execDisWifi() { char *chr = "nmcli radio wifi off"; Utils::m_system(chr); while (1) { if (execGetIface()->wstate == 2) { emit disWifiDone(); emit btFinish(); break; } sleep(1); } } //to connect wired network void BackThread::execConnLan(QString connName, QString ifname) { KylinDBus objKyDbus; //先断开当前网卡对应的已连接有线网 QString uuid = objKyDbus.getConnLanNameByIfname(ifname); if (uuid != "--") { //qDebug() << "断开网络:" < " + tmpPath; Utils::m_system(cmd.toUtf8().data()); QFile file(tmpPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-lanprop!"); qDebug()<<"Can't open the file /tmp/kylin-nm-lanprop!"<lstate); syslog(LOG_DEBUG, "In function execConnLan, wired net state is: %d", execGetIface()->lstate); emit connDone(0); } else { QString cmd = "nmcli connection down '" + connName + "'"; Utils::m_system(cmd.toUtf8().data()); if (txt.indexOf("IP configuration could not be reserved") != -1) { emit connDone(5); } else if(txt.indexOf("MACs") != -1 || txt.indexOf("Mac") != -1 || txt.indexOf("MAC") != -1) { emit connDone(2); } else { emit connDone(4); } } } else { qDebug()<<"connect wired network failed for without wired cable plug in."; syslog(LOG_DEBUG, "connect wired network failed for without wired cable plug in."); emit connDone(1); } emit btFinish(); } //to connected wireless network need a password void BackThread::execConnWifiPWD(QString connName, QString password, QString connType) { //disConnLanOrWifi("wifi"); if (!connType.isEmpty()) { QString strConntype = "nmcli connection modify " + connName + " wifi-sec.psk-flags 0"; Utils::m_system(strConntype.toUtf8().data()); } QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName(); QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli device wifi connect '" + connName + "' password '" + password + "' > " + tmpPath; Utils::m_system(cmdStr.toUtf8().data()); QFile file(tmpPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { syslog(LOG_DEBUG, "Can't open the file /tmp/kylin-nm-btoutput !"); qDebug()<<"Can't open the file /tmp/kylin-nm-btoutput !"<wstate); syslog(LOG_DEBUG, "In function execConnWifiPWD, wireless net state is: %d", execGetIface()->wstate); } else if(line.indexOf("Secrets were required") != -1){ emit connDone(4); } else { emit connDone(1); } emit btFinish(); } //to connected wireless network driectly do not need a password void BackThread::execConnWifi(QString connName) { //disConnLanOrWifi("wifi"); QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "'\n"; cmdConnWifi->write(cmdStr.toUtf8().data()); } void BackThread::on_readoutput() { QString str = cmdConnWifi->readAllStandardOutput(); cmdConnWifi->close(); qDebug()<<"on_readoutput: "<< str; if (str.indexOf("successfully") != -1) { emit connDone(0); //send this signal if connect net successfully qDebug()<<"debug: in function on_readoutput, wireless net state is: "<wstate); syslog(LOG_DEBUG, "In function on_readoutput, wireless net state is: %d", execGetIface()->wstate); } else if(str.indexOf("unknown") != -1) { emit connDone(2); } else { //emit connDone(1); } emit btFinish(); } void BackThread::on_readerror() { QString str = cmdConnWifi->readAllStandardError(); cmdConnWifi->close(); qDebug()<<"on_readerror: "<< str; syslog(LOG_DEBUG, "on_readerror : %s", str.toUtf8().data()); if (str.indexOf("successfully") != -1) { //emit connDone(0); } else if(str.indexOf("unknown") != -1 || str.indexOf("not exist") != -1) { emit connDone(2); //send this signal if the network we want to connect has not a configuration file } else if(str.indexOf("not given") != -1 || str.indexOf("Secrets were required") != -1){ //password for '802-11-wireless-security.psk' not given in 'passwd-file' emit connDone(4); } else { emit connDone(1); //send this signal if connect net failed } emit btFinish(); } //get property of connected network QString BackThread::getConnProp(QString connName) { QString tmpPath = "/tmp/kylin-nm-connprop-" + QDir::home().dirName(); QString cmd = "nmcli connection show '" + connName + "' > " + tmpPath; Utils::m_system(cmd.toUtf8().data()); QFile file(tmpPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-connprop!"); qDebug()<<"Can't open the file /tmp/kylin-nm-connprop!"< " + tmpPath; Utils::m_system(cmd.toUtf8().data()); QFile file(tmpPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-bandwidth!"); qDebug()<<"Can't open the file /tmp/kylin-nm-bandwidth!"<