qt/ledset/globalfunc.cpp

347 lines
14 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
2023-09-15 10:59:00 +08:00
int GetIcTypeFromName(QString strName)
{
int iResult=0;
//"ICN系列"
if(strName == "ICN2026" ) iResult = enum_normal;
if(strName == "ICN2027" ) iResult = enum_normal;
if(strName == "ICN2028" ) iResult = enum_normal;
if(strName == "ICN2037" ) iResult = enum_normal;
if(strName == "ICN2038" ) iResult = enum_normal;
if(strName == "ICN2038S" ) iResult = enum_normal;
if(strName == "ICN2053" ) iResult = enum_pwm;
if(strName == "ICN2058" ) iResult = enum_normal;
if(strName == "ICN2088" ) iResult = enum_normal;
if(strName == "ICND2045" ) iResult = enum_normal;
if(strName == "ICND2046" ) iResult = enum_normal;
if(strName == "ICND2047" ) iResult = enum_normal;
if(strName == "ICND2049" ) iResult = enum_normal;
if(strName == "ICND2055" ) iResult = enum_normal;
if(strName == "ICND2059" ) iResult = enum_normal;
if(strName == "ICND2065" ) iResult = enum_normal;
if(strName == "ICND2069" ) iResult = enum_normal;
if(strName == "ICND2076" ) iResult = enum_normal;
if(strName == "ICND2110" ) iResult = enum_normal;
if(strName == "ICND2112" ) iResult = enum_normal;
if(strName == "ICND2153" ) iResult = enum_normal;
if(strName == "ICND2153_three") iResult = enum_normal;
if(strName == "ICND2163") iResult = enum_normal;
if(strName == "ICND2200") iResult = enum_normal;
// "SM系列"
if(strName == "SM16017_NEW") iResult = enum_normal;
if(strName == "SM16017S") iResult = enum_normal;
if(strName == "SM16159S") iResult = enum_normal;
if(strName == "SM16169S") iResult = enum_normal;
if(strName == "SM16207S") iResult = enum_normal;
if(strName == "SM16218") iResult = enum_normal;
if(strName == "SM16227S") iResult = enum_normal;
if(strName == "SM16237DS") iResult = enum_normal;
if(strName == "SM16237S") iResult = enum_normal;
if(strName == "SM16259S") iResult = enum_normal;
if(strName == "SM16259S") iResult = enum_normal;
if(strName == "SM16369") iResult = enum_normal;
if(strName == "SM16380") iResult = enum_normal;
if(strName == "SM16388") iResult = enum_normal;
if(strName == "SM16389") iResult = enum_normal;
if(strName == "SM16509/16399") iResult = enum_normal;
//"LS系列"
if(strName == "LS9918S") iResult = enum_normal;
if(strName == "LS9919S") iResult = enum_normal;
if(strName == "LS9926S") iResult = enum_normal;
if(strName == "LS9929C") iResult = enum_normal;
if(strName == "LS9929S") iResult = enum_normal;
if(strName == "LS9930S") iResult = enum_normal;
if(strName == "LS9931S") iResult = enum_normal;
if(strName == "LS9935B") iResult = enum_normal;
if(strName == "LS9935S") iResult = enum_normal;
if(strName == "LS9936S") iResult = enum_normal;
if(strName == "LS9961S") iResult = enum_normal;
//"MBI系列"
if(strName == "MBI5041/5042") iResult = enum_normal;
if(strName == "MBI5051") iResult = enum_normal;
if(strName == "MBI5124") iResult = enum_normal;
if(strName == "MBI5151") iResult = enum_normal;
if(strName == "MBI5153") iResult = enum_normal;
if(strName == "MBI5155") iResult = enum_normal;
if(strName == "MBI5158") iResult = enum_normal;
if(strName == "MBI5252") iResult = enum_normal;
if(strName == "MBI5253") iResult = enum_normal;
if(strName == "MBI5253B") iResult = enum_normal;
if(strName == "MBI5254") iResult = enum_normal;
if(strName == "MBI5264") iResult = enum_normal;
if(strName == "MBI5268") iResult = enum_normal;
if(strName == "MBI5353") iResult = enum_normal;
if(strName == "MBI5353B") iResult = enum_normal;
if(strName == "MBI6322") iResult = enum_normal;
if(strName == "MBI6328") iResult = enum_normal;
if(strName == "MBI6334") iResult = enum_normal;
//"SUM系列"
if(strName == "SUM2017") iResult = enum_normal;
if(strName == "SUM2017T") iResult = enum_normal;
if(strName == "SUM2017TD") iResult = enum_normal;
if(strName == "SUM2028") iResult = enum_normal;
if(strName == "SUM2030") iResult = enum_normal;
if(strName == "SUM2030T") iResult = enum_normal;
if(strName == "SUM2032") iResult = enum_normal;
if(strName == "SUM2033") iResult = enum_normal;
if(strName == "SUM2035") iResult = enum_normal;
if(strName == "SUM2036") iResult = enum_normal;
if(strName == "SUM2037") iResult = enum_normal;
if(strName == "SUM2117") iResult = enum_normal;
if(strName == "SUM2130") iResult = enum_normal;
if(strName == "SUM2131") iResult = enum_normal;
if(strName == "SUM2135") iResult = enum_normal;
if(strName == "SUM6086") iResult = enum_normal;
//"FM系列")
if(strName == "FM6128") iResult = enum_normal;
if(strName == "FM6129") iResult = enum_normal;
if(strName == "FM6153") iResult = enum_normal;
if(strName == "FM6182") iResult = enum_normal;
if(strName == "FM6253") iResult = enum_normal;
if(strName == "FM6353") iResult = enum_normal;
if(strName == "FM6356") iResult = enum_normal;
if(strName == "FM6363") iResult = enum_normal;
if(strName == "FM6555") iResult = enum_normal;
if(strName == "FM6565/6569") iResult = enum_normal;
if(strName == "FM6565CE") iResult = enum_normal;
//"DP系列"
if(strName == "DP5135") iResult = enum_normal;
if(strName == "DP5220X") iResult = enum_normal;
if(strName == "DP5525") iResult = enum_normal;
//"MY系列");
if(strName == "MY9758") iResult = enum_normal;
if(strName == "MY9862") iResult = enum_normal;
if(strName == "MY9866") iResult = enum_normal;
//"LYD系列"
if(strName == "LYD6168B") iResult = enum_normal;
if(strName == "LYD6168C") iResult = enum_normal;
if(strName == "LYD6168D") iResult = enum_normal;
if(strName == "LYD6168E") iResult = enum_normal;
if(strName == "LYD6188") iResult = enum_normal;
if(strName == "LYD6188PC") iResult = enum_normal;
//"其他系列"
if(strName == "通用") iResult = enum_normal;
if(strName == "A5065") iResult = enum_normal;
if(strName == "AXS6018") iResult = enum_normal;
if(strName == "CFD135A") iResult = enum_normal;
if(strName == "CFD455A") iResult = enum_normal;
if(strName == "CNS7153") iResult = enum_normal;
if(strName == "CNS7253") iResult = enum_normal;
if(strName == "CNS7263") iResult = enum_normal;
if(strName == "CS2017") iResult = enum_normal;
if(strName == "CS2033") iResult = enum_normal;
if(strName == "D26188") iResult = enum_normal;
if(strName == "HX8055") iResult = enum_normal;
if(strName == "HX8864") iResult = enum_normal;
if(strName == "HX8896") iResult = enum_normal;
if(strName == "RT5965") iResult = enum_normal;
if(strName == "RT5967") iResult = enum_normal;
if(strName == "SCL8081A") iResult = enum_normal;
if(strName == "XM11202G") iResult = enum_normal;
return iResult;
}