305 lines
11 KiB
C++
305 lines
11 KiB
C++
#include "widget.h"
|
|
#include "./ui_widget.h"
|
|
#include "CCFile.h"
|
|
#include "CCFIleInStream.h"
|
|
#include "CCFileOutStream.h"
|
|
#include "CCJSONObject.h"
|
|
#include "CCThread.h"
|
|
|
|
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){
|
|
AES_USB::init();
|
|
ui->setupUi(this);
|
|
checkBox_R_PIN = new QCheckBox(ui->R_PIN);
|
|
checkBox_R_PIN->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
|
|
"QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/file/media/test_no_display.png);}"
|
|
"QCheckBox::indicator:checked {image: url(:/file/media/test_display.png);}");
|
|
checkBox_R_PIN->setGeometry(ui->R_PIN->pos().x() + 460,ui->R_PIN->pos().y() + 8,20,20);
|
|
connect(checkBox_R_PIN, &QPushButton::clicked,[this]() {
|
|
if (checkBox_R_PIN->isChecked()) {
|
|
ui->R_PIN->setEchoMode(QLineEdit::Normal);
|
|
}
|
|
else {
|
|
ui->R_PIN->setEchoMode(QLineEdit::Password);
|
|
}
|
|
});
|
|
checkBox_S_PIN = new QCheckBox(ui->S_PIN);
|
|
checkBox_S_PIN->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
|
|
"QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/file/media/test_no_display.png);}"
|
|
"QCheckBox::indicator:checked {image: url(:/file/media/test_display.png);}");
|
|
checkBox_S_PIN->setGeometry(ui->S_PIN->pos().x() + 280,ui->S_PIN->pos().y() + 8,20,20);
|
|
connect(checkBox_S_PIN, &QPushButton::clicked,[this]() {
|
|
if (checkBox_S_PIN->isChecked()) {
|
|
ui->S_PIN->setEchoMode(QLineEdit::Normal);
|
|
}
|
|
else {
|
|
ui->S_PIN->setEchoMode(QLineEdit::Password);
|
|
}
|
|
});
|
|
this->setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
|
|
CTL::Thread::addTask([this]() {
|
|
CTL::Thread::SleepMS(1000);
|
|
readClick();
|
|
});
|
|
connect(ui->ReadBtn, &QPushButton::clicked, this, &Widget::readClick);
|
|
connect(ui->B_Auto_S, &QPushButton::clicked, [this]() {
|
|
if (ui->B_Auto_S->isChecked()) {
|
|
loop_running = true;
|
|
CTL::Thread::addTask(&Widget::LoopRunning,this);
|
|
}
|
|
else {
|
|
loop_running = false;
|
|
}
|
|
});
|
|
connect(ui->SettingBtn, &QPushButton::clicked, this,&Widget::writeClick);
|
|
const CCFile configFile("./config.txt");
|
|
if (configFile.isExists()) {
|
|
const CTL::String JsonStr = CTL::FileInputStream::ReadFileDataAll("./config.txt");
|
|
try {
|
|
const CTL::JSONObject json = CTL::JSONObject::parse(JsonStr);
|
|
config = json.get("T").get<ConfigStruct>();
|
|
ui->S_PID->setText(config.PID.c_str());
|
|
ui->S_VID->setText(config.VID.c_str());
|
|
ui->S_PIN->setText(config.PIN.c_str());
|
|
ui->S_Data->setText(config.Data.c_str());
|
|
ui->B_Data->setChecked(config.DataBool);
|
|
ui->B_PIN->setChecked(config.PINBool);
|
|
ui->B_VID_PID->setChecked(config.PIDBool);
|
|
}
|
|
catch (...){}
|
|
}
|
|
} // AA-20260203-20260505 XunBangBS
|
|
|
|
Widget::~Widget(){
|
|
loop_running = false;
|
|
AES_USB::release();
|
|
delete checkBox_R_PIN;
|
|
delete checkBox_S_PIN;
|
|
delete ui;
|
|
}
|
|
|
|
void Widget::PrintInfo(const CTL::String &info) {
|
|
QMetaObject::invokeMethod(this, [this,info]() {
|
|
ui->infoTxt->append(info.c_str());
|
|
});
|
|
}
|
|
|
|
bool Widget::readInit() {
|
|
CCUniqueLock lock(loop_mutex);
|
|
AES_USB::PID_Key key;
|
|
const int ret = AES_USB::readID_Pin(&key);
|
|
if (ret != 1) {
|
|
QMetaObject::invokeMethod(this, [this]() {
|
|
ui->R_PID->clear();
|
|
ui->R_VID->clear();
|
|
ui->R_PIN->clear();
|
|
});
|
|
return false;
|
|
}
|
|
QMetaObject::invokeMethod(this, [this,key]() {
|
|
const CTL::String PID = CTL::String::format("%04X", key.product_id);
|
|
const CTL::String VID = CTL::String::format("%04X", key.vendor_id);
|
|
ui->R_PID->setText(PID.c_str());
|
|
ui->R_VID->setText(VID.c_str());
|
|
ui->S_PID->setText(PID.c_str());
|
|
ui->S_VID->setText(VID.c_str());
|
|
ui->R_PIN->clear();
|
|
char PIN[21] = {0};
|
|
std::memcpy(PIN, key.PIN, sizeof key.PIN);
|
|
PIN[20] = '\0';
|
|
const QString str = QString::fromLocal8Bit(PIN);
|
|
ui->R_PIN->setText(str);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
void Widget::readClick() {
|
|
CCUniqueLock lock(loop_mutex);
|
|
uint8_t data[1024] = {0};
|
|
readData(data, 60);
|
|
PrintInfo("正在读取加密狗配置信息...");
|
|
AES_USB::PID_Key key;
|
|
const int ret = AES_USB::readID_Pin(&key);
|
|
if (ret != 1) {
|
|
QMetaObject::invokeMethod(this, [this]() {
|
|
ui->R_PID->clear();
|
|
ui->R_VID->clear();
|
|
ui->R_PIN->clear();
|
|
});
|
|
PrintInfo("加密狗配置信息读取失败");
|
|
return;
|
|
}
|
|
QMetaObject::invokeMethod(this, [this,key]() {
|
|
const CTL::String PID = CTL::String::format("%04X", key.product_id);
|
|
const CTL::String VID = CTL::String::format("%04X", key.vendor_id);
|
|
ui->R_PID->setText(PID.c_str());
|
|
ui->R_VID->setText(VID.c_str());
|
|
ui->S_PID->setText(PID.c_str());
|
|
ui->S_VID->setText(VID.c_str());
|
|
ui->R_PIN->clear();
|
|
char PIN[21] = {0};
|
|
std::memcpy(PIN, key.PIN, sizeof key.PIN);
|
|
PIN[20] = '\0';
|
|
const QString str = QString::fromLocal8Bit(PIN);
|
|
ui->R_PIN->setText(str);
|
|
});
|
|
PrintInfo("加密狗配置信息读取完成");
|
|
}
|
|
|
|
void Widget::LoopRunning() {
|
|
PrintInfo("开始循环初始化加密狗");
|
|
while (loop_running) {
|
|
PrintInfo("请插入一个加密狗...");
|
|
bool ret = false;
|
|
AES_USB::PID_Key key;
|
|
while (loop_running) {
|
|
CCUniqueLock lock(loop_mutex);
|
|
const int ret_t = AES_USB::readID_Pin(&key);
|
|
ret = ret_t == 1;
|
|
if (ret) {
|
|
break;
|
|
}
|
|
CTL::Thread::SleepMS(500);
|
|
}
|
|
if (ret) {
|
|
PrintInfo("读取到加密狗,开始初始化,请勿拔出加密狗");
|
|
QMetaObject::invokeMethod(this, [this,key]() {
|
|
const CTL::String PID = CTL::String::format("%04X", key.product_id);
|
|
const CTL::String VID = CTL::String::format("%04X", key.vendor_id);
|
|
ui->R_PID->setText(PID.c_str());
|
|
ui->R_VID->setText(VID.c_str());
|
|
ui->R_PIN->clear();
|
|
char PIN[21] = {0};
|
|
std::memcpy(PIN, key.PIN, sizeof key.PIN);
|
|
PIN[20] = '\0';
|
|
const QString str = QString::fromLocal8Bit(PIN);
|
|
ui->R_PIN->setText(str);
|
|
});
|
|
CTL::Thread::SleepMS(1000);
|
|
const int r = writeInit();
|
|
if (r == 1) {
|
|
PrintInfo("设置加密狗初始化完成");
|
|
}
|
|
else if (r == 0){
|
|
PrintInfo("设置加密狗初始化失败");
|
|
}
|
|
PrintInfo("请拔出加密狗...");
|
|
while (loop_running) {
|
|
CCUniqueLock lock(loop_mutex);
|
|
const int ret_t = AES_USB::readID_Pin(&key);
|
|
if (ret_t != 1) {
|
|
QMetaObject::invokeMethod(this, [this]() {
|
|
ui->R_PID->clear();
|
|
ui->R_VID->clear();
|
|
ui->R_PIN->clear();
|
|
});
|
|
break;
|
|
}
|
|
CTL::Thread::SleepMS(100);
|
|
}
|
|
}
|
|
else {
|
|
QMetaObject::invokeMethod(this, [this]() {
|
|
ui->R_PID->clear();
|
|
ui->R_VID->clear();
|
|
ui->R_PIN->clear();
|
|
});
|
|
}
|
|
if (loop_running) {
|
|
CTL::Thread::SleepMS(1000);
|
|
}
|
|
}
|
|
PrintInfo("循环初始化加密狗结束");
|
|
}
|
|
|
|
int Widget::writeInit() {
|
|
CCUniqueLock lock(loop_mutex);
|
|
if (!ui->B_VID_PID->isChecked() && !ui->B_Data->isChecked() && !ui->B_PIN->isChecked()) {
|
|
PrintInfo("错误: 请至少选择一个设置项");
|
|
return -1;
|
|
}
|
|
uint16_t PID = hexStringToUint16(ui->R_PID->text().toStdString());
|
|
uint16_t VID = hexStringToUint16(ui->R_VID->text().toStdString());
|
|
int ret = aes_usb.open(VID, PID);
|
|
aes_usb.setUserPIN(ui->R_PIN->text().toStdString());
|
|
if (ret != 1) {
|
|
return 0;
|
|
}
|
|
if (ui->B_VID_PID->isChecked() && !ui->S_PID->text().isEmpty() && !ui->S_VID->text().isEmpty()) {
|
|
PID = hexStringToUint16(ui->S_PID->text().toStdString());
|
|
VID = hexStringToUint16(ui->S_VID->text().toStdString());
|
|
ret = aes_usb.set_vid_pid(VID, PID);
|
|
if (ret < 0) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (ui->B_Data->isChecked() && !ui->S_Data->text().isEmpty()) {
|
|
const CTL::String data = ui->S_Data->text().toStdString();
|
|
ret = aes_usb.write((uint8_t*)data.c_str(),data.length());
|
|
if (ret < 0) {
|
|
return 0;
|
|
}
|
|
}
|
|
aes_usb.close();
|
|
CTL::Thread::SleepMS(300);
|
|
if (ui->B_PIN->isChecked() && !ui->S_PIN->text().isEmpty()
|
|
&& !ui->S_PID->text().isEmpty() && !ui->S_VID->text().isEmpty())
|
|
{
|
|
AES_USB::PID_Key key = { 0 };
|
|
key.product_id = hexStringToUint16(ui->S_PID->text().toStdString());
|
|
key.vendor_id = hexStringToUint16(ui->S_VID->text().toStdString());
|
|
const auto pin = ui->S_PIN->text().toStdString();
|
|
std::memcpy(key.PIN, pin.c_str(), pin.length() > 20 ? 20 : pin.length());
|
|
ret = AES_USB::writeID_Pin(&key);
|
|
if (ret != 1) {
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void Widget::writeClick() {
|
|
const int r = writeInit();
|
|
if (r == 1) {
|
|
PrintInfo("设置加密狗完成");
|
|
}
|
|
else if (r == 0){
|
|
PrintInfo("设置加密狗失败");
|
|
}
|
|
config.Data = ui->S_Data->text().toStdString();
|
|
config.PID = ui->S_PID->text().toStdString();
|
|
config.PIN = ui->S_PIN->text().toStdString();
|
|
config.VID = ui->S_VID->text().toStdString();
|
|
config.DataBool = ui->B_Data->isChecked();
|
|
config.PIDBool = ui->B_VID_PID->isChecked();
|
|
config.PINBool = ui->B_PIN->isChecked();
|
|
CTL::JSONObject json;
|
|
json.put("T",config);
|
|
const CTL::String JsonStr = json.to_String();
|
|
CTL::FileOutStream::WriteFile("./config.txt", JsonStr);
|
|
}
|
|
|
|
uint16_t Widget::hexStringToUint16(const std::string &hexStr) {
|
|
return static_cast<uint16_t>(std::stoi(hexStr, nullptr, 16));
|
|
}
|
|
|
|
bool Widget::readData(uint8_t *data, int length) {
|
|
if (ui->R_PID->text().isEmpty() || ui->R_VID->text().isEmpty()) {
|
|
return false;
|
|
}
|
|
uint16_t PID = hexStringToUint16(ui->R_PID->text().toStdString());
|
|
uint16_t VID = hexStringToUint16(ui->R_VID->text().toStdString());
|
|
int ret = aes_usb.open(VID, PID);
|
|
aes_usb.setUserPIN(ui->R_PIN->text().toStdString());
|
|
if (ret != 1) {
|
|
return false;
|
|
}
|
|
ret = aes_usb.read(data, length);
|
|
aes_usb.close();
|
|
if (ret > 0) {
|
|
CTL::System::Println("Data: {}",(char*)data);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|