qt/ledset/globalfunc.cpp

195 lines
6.9 KiB
C++
Raw Normal View History

2022-12-16 15:08:53 +08:00
#include "globalfunc.h"
2023-03-31 18:44:09 +08:00
#include "gutil/qgui.h"
2022-12-16 15:08:53 +08:00
#include "pcap.h"
#include <QMessageBox>
#include <QPushButton>
2023-05-27 17:43:57 +08:00
#include <QDialogButtonBox>
2023-06-07 19:11:03 +08:00
#include <QEventLoop>
2022-12-16 15:08:53 +08:00
2023-07-28 14:48:41 +08:00
QString gFileHome;
2023-05-27 17:43:57 +08:00
HeadMap headMap;
2023-08-24 16:55:38 +08:00
HeadMap_ZRF headMap_zrf; //added by alahover 20230822
2023-09-01 17:25:53 +08:00
St_Zrf_rb_Param st_zrf_rb_param; //added by alahover 20230825
2023-09-01 16:17:33 +08:00
St_Zrf_Base_Param st_zrf_base_param; //added by alahover 20230823
St_Zrf_Cmd_Param st_zrf_cmd_param; //added by alahover 20230823
St_MiaoDian_Param st_miaodian_param; //added by alahover 20230823
2023-06-13 18:00:19 +08:00
byte *getSepas() {
auto sepas = new byte[headMap.end+4]{0};
sepas[headMap.ver] = ' ';
sepas[headMap.srv] = ' ';
sepas[headMap.len] = ' ';
sepas[headMap.tgtAddr] = ' ';
sepas[headMap.srcAddr] = ' ';
sepas[headMap.ptr] = ' ';
sepas[headMap.ans] = ' ';
sepas[headMap.chk] = ' ';
sepas[headMap.body] = '\n';
sepas[headMap.bodylen] = ' ';
sepas[headMap.end] = ' ';
return sepas;
}
byte *sepas = getSepas();
2023-03-20 12:11:58 +08:00
pcap_t *pcapSend{0};
PcapReThread *reThd{0};
2023-09-01 17:25:53 +08:00
SyncResp sendMsgSync(QByteArray &msg, int id, qint64 timeout, WaitingDlg *waitingDlg) {
2023-06-07 19:11:03 +08:00
QEventLoop loop;
2023-09-01 17:25:53 +08:00
QByteArray resmsg;
auto res = reThd->sendMsgNet((byte*)msg.data(), msg.size(), id, timeout, [=, &loop, &resmsg](int code, const QByteArray data) {
resmsg = data;
2023-06-07 19:11:03 +08:00
loop.exit(code);
2023-09-01 17:25:53 +08:00
}, true, waitingDlg);
2023-06-07 19:11:03 +08:00
if(res==0) res = loop.exec(QEventLoop::ExcludeUserInputEvents);
if(res && waitingDlg) waitingDlg->close();
2023-09-01 17:25:53 +08:00
return {res, resmsg};
2023-06-07 19:11:03 +08:00
}
2023-03-24 18:41:48 +08:00
QByteArray getNetDev(QWidget *parent, QByteArray def, bool justReturnIfHave) {
2022-12-16 15:08:53 +08:00
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *devs;
if(pcap_findalldevs(&devs, errbuf) == -1) {
QMessageBox::critical(parent, "Error", QString("寻找网卡失败")+errbuf);
return QByteArray();
}
auto table = new Table{
2023-05-27 17:43:57 +08:00
{"name", "网卡名称"},
{"desc", "网卡描述"},
{"loopback", "Loopback"},
{"ip", "IP"},
{"netmask", "Netmask"},
{"broadaddr", "Broad Addr"},
{"dstaddr", "Dst Addr"}
2022-12-16 15:08:53 +08:00
};
pcap_if_t *device = 0;
for(pcap_if_t *dev = devs; dev; dev = dev->next) {
pcap_addr_t *a;
for(a=dev->addresses; a; a=a->next) {
auto sa_family = a->addr->sa_family;
if(sa_family==AF_INET) {
2023-03-24 18:41:48 +08:00
if(justReturnIfHave && ! def.isEmpty() && dev->name == def) {
2022-12-16 15:08:53 +08:00
pcap_freealldevs(devs);
table->deleteLater();
return def;
}
auto rr = table->rowCount();
table->setRowCount(rr+1);
2023-03-31 18:44:09 +08:00
table->setText(rr, "name", dev->name);
table->setText(rr, "desc", dev->description);
table->setText(rr, "loopback", (dev->flags & PCAP_IF_LOOPBACK)?"True":"False");
2022-12-16 15:08:53 +08:00
auto ip = (u_char *) &((sockaddr_in *) a->addr)->sin_addr.s_addr;
2023-03-31 18:44:09 +08:00
table->setText(rr, "ip", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
2022-12-16 15:08:53 +08:00
if(a->netmask) {
ip = (u_char *) &((sockaddr_in *) a->netmask)->sin_addr.s_addr;
2023-03-31 18:44:09 +08:00
table->setText(rr, "netmask", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
2022-12-16 15:08:53 +08:00
}
if(a->broadaddr) {
ip = (u_char *) &((sockaddr_in *) a->broadaddr)->sin_addr.s_addr;
2023-03-31 18:44:09 +08:00
table->setText(rr, "broadaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
2022-12-16 15:08:53 +08:00
}
if(a->dstaddr) {
ip = (u_char *) &((sockaddr_in *) a->dstaddr)->sin_addr.s_addr;
2023-03-31 18:44:09 +08:00
table->setText(rr, "dstaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
2022-12-16 15:08:53 +08:00
}
if(device==0) device = dev;
}
}
}
pcap_freealldevs(devs);
auto rowCnt = table->rowCount();
if(rowCnt==0) {
table->deleteLater();
QMessageBox::critical(parent, "Error", "没找到 internet 设备");
return QByteArray();
} else if(rowCnt==1) {
table->deleteLater();
return table->item(0, "name")->text().toLocal8Bit();
} else {
2023-05-27 17:43:57 +08:00
QDialog dlg(parent);
dlg.setWindowFlag(Qt::WindowMaximizeButtonHint);
dlg.setWindowTitle("选择网卡");
dlg.resize(900, 300);
2022-12-16 15:08:53 +08:00
2023-05-27 17:43:57 +08:00
auto vBox = new QVBoxLayout(&dlg);
2022-12-16 15:08:53 +08:00
vBox->setContentsMargins(0,0,0,0);
vBox->setSpacing(3);
vBox->addSpacing(30);
table->setDefs();
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
2023-03-24 18:41:48 +08:00
if(! def.isEmpty()) for(int r=0; r<table->rowCount(); r++) if(table->item(r, "name")->text()==def) table->selectRow(r);
2022-12-16 15:08:53 +08:00
QByteArray name;
2023-05-27 17:43:57 +08:00
QObject::connect(table, &Table::cellDoubleClicked, &dlg, [&, table](int row) {
2022-12-16 15:08:53 +08:00
name = table->item(row, "name")->text().toLocal8Bit();
2023-05-27 17:43:57 +08:00
dlg.accept();
2022-12-16 15:08:53 +08:00
});
vBox->addWidget(table);
2023-05-27 17:43:57 +08:00
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
QObject::connect(btnBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
QObject::connect(btnBox, &QDialogButtonBox::accepted, &dlg, [&, table] {
2022-12-16 15:08:53 +08:00
auto sels = table->selectedRanges();
if(sels.isEmpty()) {
2023-05-27 17:43:57 +08:00
QMessageBox::warning(&dlg, "Warning", "请选择网卡");
2022-12-16 15:08:53 +08:00
return;
}
name = table->item(sels[0].topRow(), "name")->text().toLocal8Bit();
2023-05-27 17:43:57 +08:00
dlg.accept();
2022-12-16 15:08:53 +08:00
});
2023-05-27 17:43:57 +08:00
vBox->addWidget(btnBox);
2022-12-16 15:08:53 +08:00
2023-05-27 17:43:57 +08:00
dlg.exec();
2022-12-16 15:08:53 +08:00
return name;
}
}
2023-09-01 17:25:53 +08:00
//added by alahover -s 20230829
#define GAMMA_GRAY_DEPTH_NUM (256)
void GammaGen(unsigned short pGammaDataBuffer[],float fGamma,unsigned int uiGray)
{
unsigned int i=0;
float fGray=0;
float k=0;
fGray=(float)uiGray;
k=(fGray/GAMMA_GRAY_DEPTH_NUM);
for (i=0;i<GAMMA_GRAY_DEPTH_NUM;i++)
{ float f=((i*k)+0.5F)/fGray;
f=(float)pow((double)f,fGamma);
pGammaDataBuffer[i]=(unsigned int)(f*fGray-0.5F);
}
if (1)
if (pGammaDataBuffer[GAMMA_GRAY_DEPTH_NUM-1]<uiGray)
{ if (uiGray)
for (i=0;i<GAMMA_GRAY_DEPTH_NUM;i++)
{ if (pGammaDataBuffer[i])
pGammaDataBuffer[i]=(unsigned int)
( ( pGammaDataBuffer[i]
* uiGray
) / pGammaDataBuffer[GAMMA_GRAY_DEPTH_NUM-1]
);
}
}
for (i=0;i<GAMMA_GRAY_DEPTH_NUM;i++)
{ if (uiGray)
{
if (pGammaDataBuffer[i]>=uiGray)
{
pGammaDataBuffer[i]=(unsigned int)uiGray-1;
}
}
if (0)
printf("G[%d]=%d\r\n",i,pGammaDataBuffer[i]);
}
}
//added by alahover -o 20230829