forked from opelpanfan/PowerTune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiscanner.cpp
164 lines (140 loc) · 5.23 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
#include "wifiscanner.h"
#include "dashboard.h"
#include "connect.h"
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QProcess>
#include <QByteArrayMatcher>
#include <QByteArray>
QString outputline;
QStringList result;
WifiScanner::WifiScanner(QObject *parent)
: QObject(parent)
, m_dashboard(Q_NULLPTR)
{
}
WifiScanner::WifiScanner(DashBoard *dashboard, QObject *parent)
: QObject(parent)
, m_dashboard(dashboard)
{
}
void WifiScanner::initializeWifiscanner()
{
process = new QProcess(this); // create on the heap, so it doesn't go out of scope
connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(readData())); // connect process signals with your code
connect(process, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(finalize(int , QProcess::ExitStatus)));
result.clear();
outputline.clear();
//process->start("sudo /sbin/iw wlan0 scan | egrep 'SSID'");
//ifconfig wlan0 | egrep 'inet 1'
//process->start("ifconfig wlan0 | egrep 'inet 1'"); // start the process
process->start("sudo /sbin/iwlist wlan0 scan"); // start the process
process->waitForFinished();
}
void WifiScanner::checkWifiIP()
{
//qDebug()<< " CHECK IP ";
QString prog = "sudo /sbin/iwconfig";
QStringList arguments;
arguments << "wlan0"<<"egrep 'SSID'";
QProcess proc;
// qDebug() << "calling " << prog << arguments;
proc.start(prog , arguments);
proc.waitForFinished();
QString output = proc.readAllStandardOutput();
//m_dashBoard->setSerialStat("Rebooting");
}
void WifiScanner::findActiveWirelesses()
{
//m_dashboard->setSerialStat("Currently connected WIFI:");
/*
QNetworkConfigurationManager ncm;
netcfgList = ncm.allConfigurations();
wifilist.clear();
//WiFisList.clear();
for (auto &x : netcfgList)
{
if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
{
if(x.name() == "")
wifilist << "Unknown(Other Network)";
else
wifilist << x.name();
// qDebug() << "Type" <<x.type();
// qDebug() << "Network found :"<<x.name();
// qDebug() << "State :"<<x.state();
}
}*/
QProcess proc;
proc.start("sh", QStringList()<<"-c"<<"iwconfig 2>&1 | grep ESSID");
proc.waitForFinished();
QString output = proc.readAllStandardOutput();
m_dashboard->setSerialStat(output);
//m_dashBoard->setSerialStat("Rebooting");
// qDebug() << output ;
m_dashboard->setSerialStat("WLAN IP Adress:");
QProcess proc2;
proc2.start("sh", QStringList()<<"-c"<<"ifconfig wlan0 2>&1 | egrep 'inet 1'");
proc2.waitForFinished();
QString output2 = proc2.readAllStandardOutput();
m_dashboard->setSerialStat(output2);
m_dashboard->setSerialStat("Ethernet IP Adress:");
QProcess proc3;
proc3.start("sh", QStringList()<<"-c"<<"ifconfig eth0 2>&1 | egrep 'inet 1'");
proc3.waitForFinished();
QString output3 = proc3.readAllStandardOutput();
m_dashboard->setSerialStat(output3);
}
void WifiScanner::readData()
{
QString line = process->readAllStandardOutput(); // read data from console
outputline.append(line);
// qDebug() << "Line :"<< line;
}
void WifiScanner::finalize(int exitCode, QProcess::ExitStatus exitStatus)
{
//Clean up The line which contains the SSID to only show the SSID name
//ESSID:"Marku\xE2\x80\x99s iPhone"==> "\xE2\x80\x99"suould be apostrophy
int i =0;
QStringList fields = outputline.split(QRegExp("[\n]"));
//Parse List and delete all items without SSID in them
//qDebug() << "elements :" << fields.count();
foreach (const QString &str, fields) {
if (str.contains("SSID"))
{
QString raw;
raw = str;
raw.remove(QChar('"'), Qt::CaseInsensitive);
raw.replace("\xE2\x80\x99","'");
raw.remove(0,26);
result += raw;
}
}
m_dashboard->setwifi(result);
}
void WifiScanner::setwifi(const QString &country,const QString &ssid1,const QString &psk1,const QString &ssid2,const QString &psk2)
{
QFile file("/etc/wpa_supplicant/wpa_supplicant.conf");
//QFile file("testme.txt" );
file.remove(); //remove file if it exists to avoid appending of existing file
if ( file.open(QIODevice::ReadWrite) )
{
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();
}
}