forked from BastianGschrey/PowerTune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiscanner.cpp
171 lines (147 loc) · 5.26 KB
/
wifiscanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "wifiscanner.h"
#include "dashboard.h"
#include "connect.h"
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QProcess>
#include <QByteArrayMatcher>
#include <QByteArray>
#include <QNetworkInterface>
QString outputline;
QStringList result;
QString eth0ip;
QString wlanip;
WifiScanner::WifiScanner(QObject *parent)
: QObject(parent)
, m_dashboard(Q_NULLPTR)
{
}
WifiScanner::WifiScanner(DashBoard *dashboard, QObject *parent)
: QObject(parent)
, m_dashboard(dashboard)
{
}
void WifiScanner::initializeWifiscanner()
{
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &WifiScanner::getconnectionStatus);
timer->start(3000); // Check the status of the connection every 3 Seconds to
WifiScanner::getconnectionStatus(); //Temporay measure
result.clear();
QString raw;
QProcess proc2;
proc2.start("sh", QStringList()<<"-c"<<"iw wlan0 scan |grep SSID");
proc2.waitForFinished();
QString output2 = proc2.readAllStandardOutput();
QStringList fields = output2.split(QRegExp("[\n]"));
foreach (const QString &str, fields) {
raw = str;
raw.replace("SSID: ","");
//raw.replace("\xe2\x80\x99","'"); //for some reason ' is shown as \xe2\x80\x99
raw.remove(0,1); // Remove the white space before the SSID
result += raw;
}
m_dashboard->setwifi(result); // Pass on wifi name list to QML Combobox
}
void WifiScanner::getconnectionStatus()
{
// displays the wlan0 and eth0 IP adresses
// Check IP Adresses direcly via QT
//qDebug()<< "TIMER";
QNetworkInterface wlan0IP = QNetworkInterface::interfaceFromName("wlan0");
QList<QNetworkAddressEntry> entries = wlan0IP.addressEntries();
if (!entries.isEmpty()) {
QNetworkAddressEntry entry = entries.first();
wlanip = entry.ip().toString();
wlanip.replace("QHostAddress(","");
wlanip.remove(QChar(')'));
wlanip.remove(QChar('"'));
if (m_dashboard->WifiStat() != wlanip)
{
m_dashboard->setWifiStat(wlanip);
}
}
else{
if (m_dashboard->WifiStat() != "NOT CONNECTED")
{
m_dashboard->setWifiStat("NOT CONNECTED");
}
}
QNetworkInterface eth0IP = QNetworkInterface::interfaceFromName("eth0");
QList<QNetworkAddressEntry> test = eth0IP.addressEntries();
if (!test.isEmpty()) {
QNetworkAddressEntry entry2 = test.first();
eth0ip = entry2.ip().toString();
eth0ip.replace("QHostAddress(","");
eth0ip.remove(QChar(')'));
eth0ip.remove(QChar('"'));
if (m_dashboard->EthernetStat() != eth0ip)
{
m_dashboard->setEthernetStat(eth0ip);
}
}
else{
if (m_dashboard->EthernetStat() != "NOT CONNECTED")
{
m_dashboard->setEthernetStat("NOT CONNECTED");
}
}
}
void WifiScanner::setwifi(const QString &country,const QString &ssid1,const QString &psk1,const QString &ssid2,const QString &psk2)
{
//Check where the wpa_supplicant file is located !
QString path = "/etc/wpa_supplicant/";
QString filepath;
if (QFileInfo::exists(path))
{
filepath = "/etc/wpa_supplicant/wpa_supplicant.conf";
}
else
{
filepath = "/etc/wpa_supplicant.conf";
}
///
QFile file(filepath);
file.remove(); //remove file if it exists to avoid appending of existing file
if ( file.open(QIODevice::ReadWrite) )
{
if (QFileInfo::exists(path))
{
QTextStream out(&file);
out << "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev" << "\r\n"
<< "update_config=1" << "\r\n"
<< "country="+country << "\r\n"
<< "#Primary WIFI" << "\r\n"
<< "network={" << "\r\n"
<< "ssid=" << "\"" << ssid1 << "\"" << "\r\n"
<< "psk=" << "\"" << psk1 << "\"" << "\r\n"
<< "}" << "\r\n"
// << "#Secondary WIFI" << "\r\n"
// << "network={" << "\r\n"
// << "ssid="<< "\"" << ssid2 << "\"" << "\r\n"
// << "psk=" << "\"" << psk2 << "\"" << "\r\n"
// << "}" << "\r\n"
<< endl;
file.close();
}
else
{
QTextStream out(&file);
out << "update_config=1" << "\r\n"
<< "country="+country << "\r\n"
<< "#Primary WIFI" << "\r\n"
<< "network={" << "\r\n"
<< "ssid=" << "\"" << ssid1 << "\"" << "\r\n"
<< "psk=" << "\"" << psk1 << "\"" << "\r\n"
<< "}" << "\r\n"
// << "#Secondary WIFI" << "\r\n"
// << "network={" << "\r\n"
// << "ssid="<< "\"" << ssid2 << "\"" << "\r\n"
// << "psk=" << "\"" << psk2 << "\"" << "\r\n"
// << "}" << "\r\n"
<< endl;
file.close();
}
}
}