Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3a7e74d

Browse files
author
Kai-Chuan Hsieh
committedMay 29, 2018
Initial commit of wps application
0 parents  commit 3a7e74d

12 files changed

+1280
-0
lines changed
 

‎libwpa_client.so

141 KB
Binary file not shown.

‎main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
9+
if (argc > 1) {
10+
argv++;
11+
qDebug("Set Interface %s", *argv);
12+
w.setInterface(*argv);
13+
} else
14+
w.setInterface("wlan0");
15+
16+
w.setWindowTitle("WPS Test Application");
17+
w.show();
18+
19+
return a.exec();
20+
}

‎mainwindow.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
#include "wpas_utils.h"
4+
5+
MainWindow::MainWindow(QWidget *parent) :
6+
QMainWindow(parent),
7+
ui(new Ui::MainWindow)
8+
{
9+
ui->setupUi(this);
10+
ui->pinButton->setText("WPS PIN");
11+
ui->pushButton->setText("WPS Push");
12+
ui->pinText->setText("PIN:");
13+
ui->statusMessage->setReadOnly(true);
14+
connect(ui->pinButton, SIGNAL(released()), this, SLOT(handlePinButton()));
15+
connect(ui->pushButton, SIGNAL(released()), this, SLOT(handlePushButton()));
16+
17+
monThread = new MonitorThread(this);
18+
connect(monThread, SIGNAL(updateMessage(QString)), this, SLOT(handleMessage(QString)));
19+
}
20+
21+
void MainWindow::handlePinButton()
22+
{
23+
QString pin = ui->pinTextEdit->text();
24+
QString cmd = "WPS_PIN any";
25+
int ret;
26+
char resp[1024];
27+
QByteArray ba;
28+
29+
if (pin != "")
30+
cmd = cmd + " " + pin;
31+
32+
ba = cmd.toLatin1();
33+
34+
ui->statusMessage->insertPlainText(cmd);
35+
ui->statusMessage->insertPlainText("\n");
36+
37+
ret = wpa_command_resp(mInterface, ba.data(), resp, sizeof(resp));
38+
if (ret < 0) {
39+
qDebug() << "wpa_command_resp failed" << ": " << cmd << endl;
40+
return;
41+
}
42+
43+
ui->statusMessage->insertPlainText(resp);
44+
ui->statusMessage->insertPlainText("\n");
45+
}
46+
47+
void MainWindow::handlePushButton()
48+
{
49+
QString cmd = "WPS_PBC";
50+
int ret;
51+
char resp[1024];
52+
QByteArray ba;
53+
54+
ba = cmd.toLatin1();
55+
56+
ui->statusMessage->insertPlainText(cmd);
57+
ui->statusMessage->insertPlainText("\n");
58+
59+
ret = wpa_command_resp(mInterface, ba.data(), resp, sizeof(resp));
60+
if (ret < 0) {
61+
qDebug() << "wpa_command_resp failed" << ": " << cmd << endl;
62+
return;
63+
}
64+
65+
ui->statusMessage->insertPlainText(resp);
66+
ui->statusMessage->insertPlainText("\n");
67+
}
68+
69+
void MainWindow::handleMessage(QString text)
70+
{
71+
ui->statusMessage->insertPlainText(text);
72+
ui->statusMessage->insertPlainText("\n");
73+
}
74+
75+
void MainWindow::setInterface(const char *ifname)
76+
{
77+
mInterface = ifname;
78+
monThread->setInterface(mInterface);
79+
monThread->start();
80+
}
81+
82+
MainWindow::~MainWindow()
83+
{
84+
monThread->terminate();
85+
monThread->wait();
86+
delete monThread;
87+
delete ui;
88+
}

‎mainwindow.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
#include <QDebug>
6+
#include "monitorthread.h"
7+
8+
namespace Ui {
9+
class MainWindow;
10+
}
11+
12+
class MainWindow : public QMainWindow
13+
{
14+
Q_OBJECT
15+
16+
public:
17+
explicit MainWindow(QWidget *parent = 0);
18+
~MainWindow();
19+
void setInterface(const char *ifname);
20+
21+
private slots:
22+
void handlePinButton();
23+
void handlePushButton();
24+
void handleMessage(QString);
25+
private:
26+
Ui::MainWindow *ui;
27+
MonitorThread *monThread;
28+
const char *mInterface;
29+
};
30+
31+
#endif // MAINWINDOW_H

‎mainwindow.ui

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<layout class="QHBoxLayout" name="horizontalLayout">
18+
<item>
19+
<layout class="QVBoxLayout" name="verticalLayout">
20+
<item>
21+
<layout class="QHBoxLayout" name="horizontalLayout_2">
22+
<item>
23+
<widget class="QLabel" name="pinText">
24+
<property name="text">
25+
<string>TextLabel</string>
26+
</property>
27+
</widget>
28+
</item>
29+
<item>
30+
<widget class="QLineEdit" name="pinTextEdit"/>
31+
</item>
32+
</layout>
33+
</item>
34+
<item>
35+
<widget class="QPlainTextEdit" name="statusMessage"/>
36+
</item>
37+
<item>
38+
<widget class="QPushButton" name="pinButton">
39+
<property name="text">
40+
<string>PushButton</string>
41+
</property>
42+
</widget>
43+
</item>
44+
<item>
45+
<widget class="QPushButton" name="pushButton">
46+
<property name="text">
47+
<string>PushButton</string>
48+
</property>
49+
</widget>
50+
</item>
51+
</layout>
52+
</item>
53+
</layout>
54+
</widget>
55+
<widget class="QMenuBar" name="menuBar">
56+
<property name="geometry">
57+
<rect>
58+
<x>0</x>
59+
<y>0</y>
60+
<width>400</width>
61+
<height>22</height>
62+
</rect>
63+
</property>
64+
</widget>
65+
<widget class="QToolBar" name="mainToolBar">
66+
<attribute name="toolBarArea">
67+
<enum>TopToolBarArea</enum>
68+
</attribute>
69+
<attribute name="toolBarBreak">
70+
<bool>false</bool>
71+
</attribute>
72+
</widget>
73+
<widget class="QStatusBar" name="statusBar"/>
74+
</widget>
75+
<layoutdefault spacing="6" margin="11"/>
76+
<resources/>
77+
<connections/>
78+
</ui>

‎monitorthread.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include "monitorthread.h"
2+
#include "wpa_ctrl.h"
3+
4+
#define CTRL_IFACE_DIR "/var/run/wpa_supplicant"
5+
6+
MonitorThread::MonitorThread(QObject *parent)
7+
: QThread(parent)
8+
{
9+
qDebug() << "Monitor Thread created" << endl;
10+
}
11+
12+
void MonitorThread::setInterface(const char *ifname)
13+
{
14+
mInterface = ifname;
15+
}
16+
17+
void MonitorThread::run()
18+
{
19+
char buf[4096];
20+
fd_set rfd;
21+
size_t buf_size = sizeof(buf);
22+
struct wpa_ctrl *mon;
23+
int fd, ret;
24+
char *pos;
25+
26+
qDebug("Monitor Thread starts on interface %s.", mInterface);
27+
28+
sprintf(buf, "%s/%s", CTRL_IFACE_DIR, mInterface);
29+
30+
mon = wpa_ctrl_open(buf);
31+
if (mon == NULL) {
32+
qDebug("wpa_ctrl_open(%s) failed", buf);
33+
goto out;
34+
}
35+
36+
if (wpa_ctrl_attach(mon) < 0) {
37+
qDebug() << "wpa_ctrl_attach failed" << endl;
38+
goto close1;
39+
}
40+
41+
fd = wpa_ctrl_get_fd(mon);
42+
if (fd < 0) {
43+
qDebug() << "wpa_ctrl_get_fd failed" << endl;
44+
goto close2;
45+
}
46+
47+
qDebug() << "Start revceiving event" << endl;
48+
49+
while (1) {
50+
size_t len;
51+
52+
FD_ZERO(&rfd);
53+
FD_SET(fd, &rfd);
54+
55+
ret = select(fd + 1, &rfd, NULL, NULL, NULL);
56+
if (ret < 0) {
57+
qDebug()<< "select: %s" << strerror(errno) << endl;
58+
goto close2;
59+
}
60+
61+
len = buf_size;
62+
if (wpa_ctrl_recv(mon, buf, &len) < 0) {
63+
qDebug() << "wpa_ctrl_recv failed" << endl;
64+
goto close2;
65+
}
66+
67+
if (len == buf_size)
68+
len--;
69+
buf[len] = '\0';
70+
71+
if (strstr(buf, "<")) {
72+
pos = strstr(buf, ">");
73+
pos += 1;
74+
}
75+
76+
qDebug() << "Event: " << pos << endl;
77+
78+
if (!strstr(pos, WPA_EVENT_BSS_ADDED) &&
79+
!strstr(pos, WPA_EVENT_BSS_REMOVED)) {
80+
qDebug() << "Event: " << pos << endl;
81+
emit updateMessage(pos);
82+
}
83+
84+
if (strstr(pos, WPA_EVENT_TERMINATING)) {
85+
qDebug() << "Terminating thread" << endl;
86+
break;
87+
}
88+
}
89+
90+
close2:
91+
wpa_ctrl_detach(mon);
92+
close1:
93+
wpa_ctrl_close(mon);
94+
out:
95+
qDebug() << "Monitor loop stop" << endl;
96+
}
97+
98+
MonitorThread::~MonitorThread()
99+
{
100+
qDebug() << "Monitor Thread deleted" << endl;
101+
}

‎monitorthread.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef MONITORTHREAD_H
2+
#define MONITORTHREAD_H
3+
#include <QThread>
4+
#include <QDebug>
5+
6+
class MonitorThread : public QThread
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
explicit MonitorThread(QObject *parent = 0);
12+
~MonitorThread();
13+
void setInterface(const char *ifname);
14+
signals:
15+
void updateMessage(QString);
16+
protected:
17+
void run();
18+
private:
19+
const char *mInterface;
20+
};
21+
22+
#endif // MONITORTHREAD_H

‎wpa_ctrl.h

+525
Large diffs are not rendered by default.

‎wpas_utils.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "stddef.h"
2+
#include "wpa_ctrl.h"
3+
#include "string.h"
4+
#include "wpas_utils.h"
5+
6+
#define CTRL_IFACE_DIR "/var/run/wpa_supplicant"
7+
8+
int wpa_command_resp(const char *ifname, const char *cmd, char *resp, size_t resp_size)
9+
{
10+
struct wpa_ctrl *ctrl;
11+
char buf[128];
12+
size_t len;
13+
14+
sprintf(buf, "%s/%s", CTRL_IFACE_DIR, ifname);
15+
16+
ctrl = wpa_ctrl_open(buf);
17+
if (ctrl == NULL) {
18+
qDebug() << "wpa_command: wpa_ctrl_open(" << buf << ") failed" << endl;
19+
return -1;
20+
}
21+
len = resp_size;
22+
if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), resp, &len, NULL) < 0) {
23+
qDebug("wpa_command: wpa_ctrl_request failed");
24+
wpa_ctrl_close(ctrl);
25+
return -1;
26+
}
27+
wpa_ctrl_close(ctrl);
28+
resp[len] = '\0';
29+
return 0;
30+
}

‎wpas_utils.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef WPAS_UTILS_H
2+
#define WPAS_UTILS_H
3+
4+
#include <QDebug>
5+
6+
int wpa_command_resp(const char *ifname, const char *cmd, char *resp, size_t resp_size);
7+
8+
#endif // WPAS_UTILS_H

‎wps.pro

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2018-05-28T11:06:31
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = wps
12+
TEMPLATE = app
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any feature of Qt which has been marked as deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
# You can also make your code fail to compile if you use deprecated APIs.
21+
# In order to do so, uncomment the following line.
22+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
23+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24+
25+
INCLUDEPATH += ./
26+
LIBS += -lwpa_client
27+
28+
SOURCES += \
29+
main.cpp \
30+
mainwindow.cpp \
31+
monitorthread.cpp \
32+
wpas_utils.cpp
33+
34+
HEADERS += \
35+
mainwindow.h \
36+
wpa_ctrl.h \
37+
monitorthread.h \
38+
wpas_utils.h
39+
40+
FORMS += \
41+
mainwindow.ui

‎wps.pro.user

+336
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.