1 Star 0 Fork 0

zhouws-chn/wifitools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mainwindow.cpp 7.49 KB
一键复制 编辑 原始数据 按行查看 历史
zhouws-chn 提交于 2023-05-25 14:15 . 上传源码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_apInfoModel = new QStandardItemModel();
ui->apInfoTableView->setModel(m_apInfoModel);
m_apInfoModel->setHorizontalHeaderItem(0, new QStandardItem("SSID"));
m_apInfoModel->setHorizontalHeaderItem(1, new QStandardItem("BSSID"));
m_apInfoModel->setHorizontalHeaderItem(2, new QStandardItem("RSSI"));
m_apInfoModel->setHorizontalHeaderItem(3, new QStandardItem("保存"));
ui->apInfoTableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->apInfoTableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->apInfoTableView->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
ui->apInfoTableView->setColumnWidth(1,120);
ui->apInfoTableView->setColumnWidth(2,40);
ui->apInfoTableView->setColumnWidth(3,40);
m_wifiTool = WifiTools::instance();
DWORD dwResult = m_wifiTool->wlanOpenHandle();
if(dwResult)
{
qDebug("wlanOpenHandle failed. res=%d", dwResult);
ui->logTextEdit->setText(QString("wlanOpenHandle failed. res=%1").arg(dwResult));
return ;
}
m_wifiTool->wlanRegisterNotification();
dwResult = m_wifiTool->wlanEnumInterfaces();
if(dwResult)
{
qDebug("wlanEnumInterfaces failed. res=%d", dwResult);
ui->logTextEdit->setText(QString("wlanEnumInterfaces failed. res=%1").arg(dwResult));
return ;
}
connect(m_wifiTool, SIGNAL(WlanScanComplete(BOOL,GUID)), this, SLOT(WlanScanCompleteSlot(BOOL,GUID)));
// 删除所有已添加的网卡
ui->interfaceDevComboBox->clear();
QVector<WLAN_INTERFACE_DEV> devList = m_wifiTool->GetWlanInterfaceList();
if(devList.length() < 1) {
qDebug("wlan dev num is zero.");
ui->interfaceDevComboBox->addItem(tr("暂无无线网卡设备"));
ui->interfaceDevComboBox->setEnabled(false);
return ;
}
ui->interfaceDevComboBox->setEnabled(true);
for(int i=0; i < devList.length(); i++)
{
ui->interfaceDevComboBox->addItem(devList.at(i).InterfaceDescriptionStr);
}
ui->pushButton->setEnabled(false);
ui->connectBtn->setEnabled(false);
ui->disconnectBtn->setEnabled(false);
ui->readProfileBtn->setEnabled(false);
ui->delProfileBtn->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_interfaceDevCheckBtn_clicked()
{
WifiTools *wifiTool = WifiTools::instance();
DWORD dwResult = m_wifiTool->wlanScan(0);
qDebug() <<"wlanScan :" << dwResult;
}
void MainWindow::WlanScanCompleteSlot(BOOL status, GUID interfaceGuid)
{
if(status)
{
WifiTools *wifiTool = WifiTools::instance();
m_wifiTool->wlanGetAvailableNetworkList();
QVector<WLAN_AP_INFO> apList = m_wifiTool->GetApInfoList();
for(int i=0; i < apList.length(); i++)
{
m_apInfoModel->setItem(i, 0, new QStandardItem(apList.at(i).SSID));
m_apInfoModel->setItem(i, 1, new QStandardItem(apList.at(i).BSSID));
m_apInfoModel->setItem(i, 2, new QStandardItem(QString::number(apList.at(i).rssi)));
if(apList.at(i).strProfileName.length()>0)
{
m_apInfoModel->setItem(i, 3, new QStandardItem("是"));
} else {
m_apInfoModel->setItem(i, 3, new QStandardItem("否"));
}
}
}
}
void MainWindow::on_apInfoTableView_doubleClicked(const QModelIndex &index)
{
QVector<WLAN_AP_INFO> apList = m_wifiTool->GetApInfoList();
if(apList.length() < index.row())
{
qDebug("index too big.");
ui->logTextEdit->setText(QString("index too big"));
return ;
}
WLAN_AP_INFO apInfo = apList.at(index.row());
ui->apSSIDEdit->setText(apInfo.SSID);
if(apInfo.ulChCenterFrequency < 3000000)
{
ui->channelEdit->setText(QString::number((apInfo.ulChCenterFrequency - 2412000)/5000 +1));
} else {
ui->channelEdit->setText(QString::number(apInfo.ulChCenterFrequency));
}
ui->readProfileBtn->setEnabled(true);
ui->delProfileBtn->setEnabled(true);
ui->connectBtn->setEnabled(true);
ui->disconnectBtn->setEnabled(true);
ui->pushButton->setEnabled(true);
ui->authEdit->setReadOnly(true);
ui->channelEdit->setReadOnly(true);
ui->passwdEdit->setText("");
if((!apInfo.bSecurityEnabled) || (apInfo.dot11DefaultAuthAlgorithm == DOT11_AUTH_ALGO_80211_OPEN))
{
ui->authEdit->setText("OPEN");
ui->passwdEdit->setEnabled(false);
// return ;
} else {
ui->passwdEdit->setEnabled(true);
switch(apInfo.dot11DefaultAuthAlgorithm)
{
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
ui->authEdit->setText("WEP");
break;
case DOT11_AUTH_ALGO_WPA_PSK:
ui->authEdit->setText("WPA_PSK");
break;
case DOT11_AUTH_ALGO_RSNA_PSK:
ui->authEdit->setText("WPA2_PSK");
break;
case DOT11_AUTH_ALGO_RSNA:
ui->authEdit->setText("WPA2");
break;
case DOT11_AUTH_ALGO_WPA3:
ui->authEdit->setText("WPA3");
break;
case DOT11_AUTH_ALGO_WPA3_SAE:
ui->authEdit->setText("WPA3_SAE");
break;
default:
ui->authEdit->setText("暂未适配");
ui->logTextEdit->setText(QString("暂未适配加密算法"));
break;
}
}
if(apInfo.strProfileName.length()>0)
{
ui->profileCheckBox->setCheckState(Qt::Checked);
ui->connectBtn->setEnabled(true);
} else {
ui->profileCheckBox->setCheckState(Qt::Unchecked);
ui->connectBtn->setEnabled(false);
}
m_checkApInfo = apInfo;
}
void MainWindow::on_connectBtn_clicked()
{
WifiTools *wifiTool = WifiTools::instance();
wifiTool->wlanConnect(m_checkApInfo);
qDebug("wlanConnect");
}
void MainWindow::on_pushButton_clicked()
{
WifiTools *wifiTool = WifiTools::instance();
if(m_checkApInfo.strProfileName.length() <= 0)
{
// to set profile
if((m_checkApInfo.bSecurityEnabled) && (m_checkApInfo.dot11DefaultAuthAlgorithm != DOT11_AUTH_ALGO_80211_OPEN))
{
if(ui->passwdEdit->text().length()<8)
{
qDebug("密码太短了");
ui->logTextEdit->setText(QString("密码太短了"));
return ;
}
}
DWORD dwResult = wifiTool->wlanSetProfile(m_checkApInfo,ui->passwdEdit->text());
if(dwResult != ERROR_SUCCESS )
{
if(ERROR_ALREADY_EXISTS==dwResult) qDebug("wlanProfile already exists.");
return ;
}
qDebug("wlanSetProfile");
}
}
void MainWindow::on_readProfileBtn_clicked()
{
WifiTools *wifiTool = WifiTools::instance();
qDebug() << "dot11DefaultAuthAlgorithm: " << m_checkApInfo.dot11DefaultAuthAlgorithm;
qDebug() << "dot11DefaultCipherAlgorithm: " << m_checkApInfo.dot11DefaultCipherAlgorithm;
if(m_checkApInfo.strProfileName.length() <= 0)
{
qDebug("no Profile");
ui->logTextEdit->setText(QString("no Profile"));
return ;
}
wifiTool->wlanGetProfile(m_checkApInfo);
}
void MainWindow::on_delProfileBtn_clicked()
{
WifiTools *wifiTool = WifiTools::instance();
wifiTool->wlanDeleteProfile(m_checkApInfo);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zhouws-chn/wifitools.git
[email protected]:zhouws-chn/wifitools.git
zhouws-chn
wifitools
wifitools
master

搜索帮助