2024-05-21 21:09:55 +08:00
|
|
|
#include "ModuleCommunication.h"
|
|
|
|
#include "BoostLog.h"
|
2024-06-13 15:41:40 +08:00
|
|
|
#include "StringUtility.h"
|
2024-05-23 19:58:36 +08:00
|
|
|
#include <mbedtls/md5.h>
|
2024-05-21 21:09:55 +08:00
|
|
|
#include <sstream>
|
2024-09-03 23:30:45 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <WinSock2.h>
|
2024-09-05 22:33:07 +08:00
|
|
|
#else
|
|
|
|
#include <arpa/inet.h>
|
2024-09-03 23:30:45 +08:00
|
|
|
#endif
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
static inline uint8_t xor_checksum_byte(const uint8_t *data, uint32_t len) {
|
|
|
|
uint8_t sum = 0;
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
sum ^= data[i];
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleCommunication::ModuleCommunication(QObject *parent) : QObject{parent} {
|
|
|
|
}
|
|
|
|
|
2024-06-13 15:41:40 +08:00
|
|
|
bool ModuleCommunication::open(const QString &portName, int baudRate) {
|
2024-09-05 22:33:07 +08:00
|
|
|
if (portName.isEmpty()) return false;
|
2024-05-21 21:09:55 +08:00
|
|
|
bool ret = true;
|
|
|
|
m_serialPort = std::make_shared<QSerialPort>(portName);
|
2024-06-13 15:41:40 +08:00
|
|
|
m_serialPort->setBaudRate(baudRate);
|
2024-05-21 21:09:55 +08:00
|
|
|
connect(m_serialPort.get(), &QSerialPort::readyRead, this, &ModuleCommunication::onReadyRead);
|
2024-06-13 15:41:40 +08:00
|
|
|
connect(m_serialPort.get(), &QSerialPort::errorOccurred, this, &ModuleCommunication::onErrorOccurred);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
ret = m_serialPort->open(QSerialPort::ReadWrite);
|
2024-06-13 15:41:40 +08:00
|
|
|
LOG_CAT(info, GUI) << "打开串口(" << portName.toStdString() << ")" << (ret ? "成功" : "失败")
|
|
|
|
<< ", 波特率: " << baudRate;
|
|
|
|
if (!ret) {
|
|
|
|
LOG(error) << m_serialPort->errorString().toStdString();
|
|
|
|
}
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleCommunication::verify(uint8_t timeout) {
|
2024-07-31 16:08:38 +08:00
|
|
|
VerifyRequest data = {0};
|
2024-05-21 21:09:55 +08:00
|
|
|
data.timeout = timeout;
|
|
|
|
|
|
|
|
auto [frameData, frameSize] = generateFrame(Verify, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-06-21 11:02:28 +08:00
|
|
|
setCurrentMessageIdStatus(Verify);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
LOG_CAT(info, GUI) << "发送识别指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-07-31 16:08:38 +08:00
|
|
|
void ModuleCommunication::verifyExtended(bool captureImage, uint8_t timeout) {
|
|
|
|
VerifyRequest data = {0};
|
|
|
|
data.timeout = timeout;
|
|
|
|
data.save_image = captureImage ? 0x01 : 0x00;
|
|
|
|
auto [frameData, frameSize] = generateFrame(VerifyExtended, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
setCurrentMessageIdStatus(VerifyExtended);
|
|
|
|
|
|
|
|
LOG_CAT(info, GUI) << "发送扩展识别指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-05-23 19:58:36 +08:00
|
|
|
void ModuleCommunication::reset() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(Reset);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-06-21 11:02:28 +08:00
|
|
|
setCurrentMessageIdStatus(Reset);
|
2024-05-23 19:58:36 +08:00
|
|
|
LOG_CAT(info, GUI) << "发送复位指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-09-11 15:45:19 +08:00
|
|
|
void ModuleCommunication::enroll(const std::string &username, bool strictMode, uint8_t excludeMode, bool persistence,
|
|
|
|
uint8_t timeout) {
|
|
|
|
EnrollRequest data = {0};
|
2024-09-05 16:36:54 +08:00
|
|
|
data.strictMode = strictMode ? 1 : 0;
|
2024-09-11 15:45:19 +08:00
|
|
|
data.excludeMode = excludeMode;
|
2024-05-21 21:09:55 +08:00
|
|
|
data.timeout = timeout;
|
2024-07-15 17:47:19 +08:00
|
|
|
data.skipSave = persistence ? 0 : 1;
|
2024-05-21 21:09:55 +08:00
|
|
|
strncpy(reinterpret_cast<char *>(data.username), username.c_str(), sizeof(data.username));
|
|
|
|
auto [frameData, frameSize] = generateFrame(EnrollSingle, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-06-21 11:02:28 +08:00
|
|
|
setCurrentMessageIdStatus(EnrollSingle);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
LOG_CAT(info, GUI) << "发送注册指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast<int>(timeout) << "s";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-09-11 15:45:19 +08:00
|
|
|
void ModuleCommunication::enrollExtended(const std::string &username, bool strictMode, uint8_t excludeMode,
|
|
|
|
bool persistence, uint8_t timeout) {
|
|
|
|
EnrollRequest data = {};
|
2024-09-05 16:36:54 +08:00
|
|
|
data.strictMode = strictMode ? 1 : 0;
|
2024-09-11 15:45:19 +08:00
|
|
|
data.excludeMode = excludeMode;
|
2024-06-05 12:13:10 +08:00
|
|
|
data.timeout = timeout;
|
2024-07-15 17:47:19 +08:00
|
|
|
data.skipSave = persistence ? 0 : 1;
|
2024-06-05 12:13:10 +08:00
|
|
|
strncpy(reinterpret_cast<char *>(data.username), username.c_str(), sizeof(data.username));
|
2024-07-15 17:47:19 +08:00
|
|
|
auto [frameData, frameSize] = generateFrame(EnrollExtended, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
|
2024-06-05 12:13:10 +08:00
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-08-31 11:37:20 +08:00
|
|
|
setCurrentMessageIdStatus(EnrollExtended);
|
2024-06-19 16:02:58 +08:00
|
|
|
LOG_CAT(info, GUI) << "发送获取注册照片指令: " << protocolDataFormatString(frameData, frameSize);
|
2024-06-05 12:13:10 +08:00
|
|
|
LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast<int>(timeout) << "s";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
void ModuleCommunication::deleteUser(uint16_t userid) {
|
|
|
|
uint16_t n = htons(userid);
|
|
|
|
auto [frameData, frameSize] = generateFrame(DeleteUser, reinterpret_cast<const uint8_t *>(&n), sizeof(n));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-06-21 11:02:28 +08:00
|
|
|
setCurrentMessageIdStatus(DeleteUser);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "发送删除用户指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << "删除用户ID: " << userid;
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleCommunication::deleteAll() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(DeleteAll);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
2024-06-21 11:02:28 +08:00
|
|
|
setCurrentMessageIdStatus(DeleteAll);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "发送删除所有指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-06-05 12:13:10 +08:00
|
|
|
void ModuleCommunication::requestEnrolledImage(uint32_t offset, uint32_t size) {
|
|
|
|
ImageSliceRequest request;
|
|
|
|
request.offset = htonl(offset);
|
|
|
|
request.size = htonl(size);
|
2024-07-31 16:08:38 +08:00
|
|
|
auto [data, frameSize] = generateFrame(GetImage, reinterpret_cast<const uint8_t *>(&request), sizeof(request));
|
2024-06-05 12:13:10 +08:00
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(data), frameSize);
|
2024-06-20 21:28:30 +08:00
|
|
|
// 打印太耗时
|
|
|
|
// LOG_CAT(info, GUI) << "发送获取图片指令: " << protocolDataFormatString(data, frameSize);
|
|
|
|
// LOG_CAT(info, GUI) << Separator;
|
2024-06-05 12:13:10 +08:00
|
|
|
}
|
|
|
|
|
2024-06-20 21:28:30 +08:00
|
|
|
void ModuleCommunication::uploadImageInfo(const UploadImageInformation &info) {
|
|
|
|
UploadImageInformation request;
|
|
|
|
request.operation = info.operation;
|
|
|
|
request.width = htons(info.width);
|
|
|
|
request.height = htons(info.height);
|
|
|
|
request.size = htonl(info.size);
|
|
|
|
memcpy(request.username, info.username, sizeof(request.username));
|
|
|
|
memcpy(request.md5, info.md5, sizeof(request.md5));
|
|
|
|
auto [data, frameSize] =
|
|
|
|
generateFrame(UploadImageInfo, reinterpret_cast<const uint8_t *>(&request), sizeof(request));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(data), frameSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleCommunication::uploadImageData(uint32_t offset, const uint8_t *data, uint32_t size) {
|
|
|
|
uint32_t dataSize = sizeof(UploadImageDataSlice) + size;
|
|
|
|
auto buffer = new uint8_t[dataSize];
|
|
|
|
auto request = reinterpret_cast<UploadImageDataSlice *>(buffer);
|
|
|
|
request->size = htonl(size);
|
|
|
|
request->offset = htonl(offset);
|
|
|
|
memcpy(request->data, data, size);
|
|
|
|
auto [frameData, frameSize] = generateFrame(UploadImageData, buffer, dataSize);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2024-06-21 15:11:18 +08:00
|
|
|
void ModuleCommunication::requestCurrentStatus() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(GetCurrentStatus);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
LOG_CAT(info, GUI) << "发送状态指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-07-31 16:08:38 +08:00
|
|
|
void ModuleCommunication::setDebugEnabled(bool enabled) {
|
|
|
|
uint8_t data = enabled ? 0x01 : 0x00;
|
|
|
|
auto [frameData, frameSize] = generateFrame(EnableDebug, &data, sizeof(data));
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
}
|
|
|
|
|
2024-08-05 17:42:27 +08:00
|
|
|
void ModuleCommunication::startOta() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(StartOta);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
}
|
|
|
|
|
2024-07-15 17:47:19 +08:00
|
|
|
void ModuleCommunication::requestUniqueId() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(GetUniqueID);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
LOG_CAT(info, GUI) << "发送获取ID指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-08-16 11:34:21 +08:00
|
|
|
void ModuleCommunication::requestVersion() {
|
|
|
|
auto [frameData, frameSize] = generateFrame(GetVersion);
|
|
|
|
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
|
|
|
|
LOG_CAT(info, GUI) << "发送获取版本指令: " << protocolDataFormatString(frameData, frameSize);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
|
2024-06-13 15:41:40 +08:00
|
|
|
ModuleCommunication::MessageId ModuleCommunication::currentMessageId() const {
|
|
|
|
return m_currentMessageId;
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:03:11 +08:00
|
|
|
void ModuleCommunication::processPackage(const uint8_t *data, uint16_t size) {
|
2024-05-21 21:09:55 +08:00
|
|
|
uint8_t messageId = data[2];
|
|
|
|
switch (messageId) {
|
|
|
|
case Reply: {
|
|
|
|
uint8_t replyId = data[5];
|
|
|
|
auto result = data[6];
|
|
|
|
switch (replyId) {
|
2024-05-23 19:58:36 +08:00
|
|
|
case Reset: {
|
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
|
|
|
if (result == Success) {
|
|
|
|
LOG_CAT(info, GUI) << "复位完成。";
|
|
|
|
}
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
break;
|
|
|
|
}
|
2024-07-31 16:08:38 +08:00
|
|
|
case Verify:
|
|
|
|
case VerifyExtended: {
|
2024-05-23 19:58:36 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
if (result == Success) {
|
2024-07-31 16:08:38 +08:00
|
|
|
auto info = reinterpret_cast<const VerifyReply *>(data + 7);
|
2024-06-13 15:41:40 +08:00
|
|
|
uint16_t userid = ntohs(info->userid);
|
2024-07-01 15:07:13 +08:00
|
|
|
uint16_t elapsed = ntohs(info->elapsed);
|
2024-06-13 15:41:40 +08:00
|
|
|
LOG_CAT(info, GUI) << "用户ID: " << userid
|
2024-07-01 15:07:13 +08:00
|
|
|
<< ", 用户名: " << std::string_view(reinterpret_cast<const char *>(info->username))
|
|
|
|
<< ", 耗时: " << elapsed << "ms";
|
|
|
|
emit newVerifyResult(userid, reinterpret_cast<const char *>(info->username), elapsed);
|
2024-05-21 21:09:55 +08:00
|
|
|
} else if (result == Failed4Timeout) {
|
|
|
|
LOG_CAT(info, GUI) << "识别超时。";
|
2024-06-13 15:41:40 +08:00
|
|
|
} else if (result == Rejected) {
|
|
|
|
LOG_CAT(info, GUI) << "模组拒绝该命令。";
|
|
|
|
} else if (result == Failed4UnknownUser) {
|
2024-07-31 16:08:38 +08:00
|
|
|
auto info = reinterpret_cast<const VerifyReply *>(data + 7);
|
2024-07-01 15:07:13 +08:00
|
|
|
uint16_t elapsed = ntohs(info->elapsed);
|
|
|
|
emit newVerifyResult(InvalidUserId, "", elapsed);
|
|
|
|
LOG_CAT(info, GUI) << "未录入用户, 耗时: " << elapsed << "ms";
|
2024-06-13 15:41:40 +08:00
|
|
|
} else if (result == Failed4UnknownReason) {
|
2024-07-31 16:08:38 +08:00
|
|
|
auto info = reinterpret_cast<const VerifyReply *>(data + 7);
|
2024-07-01 15:07:13 +08:00
|
|
|
uint16_t elapsed = ntohs(info->elapsed);
|
|
|
|
emit newVerifyResult(InvalidUserId, "", elapsed);
|
|
|
|
LOG_CAT(info, GUI) << "未知错误, 耗时: " << elapsed << "ms";
|
2024-05-21 21:09:55 +08:00
|
|
|
} else {
|
2024-06-13 15:41:40 +08:00
|
|
|
LOG_CAT(info, GUI) << "未知错误(" << static_cast<int>(result) << ")。";
|
2024-05-21 21:09:55 +08:00
|
|
|
}
|
2024-07-31 16:08:38 +08:00
|
|
|
if (replyId == VerifyExtended) {
|
|
|
|
auto info = reinterpret_cast<const VerifyExtendReply *>(data + 7);
|
|
|
|
uint16_t width = ntohs(info->image_width);
|
|
|
|
uint16_t height = ntohs(info->image_height);
|
|
|
|
if ((width > 0) && (height > 0)) {
|
|
|
|
emit newImageInfo(static_cast<MessageId>(replyId), width * height, info->md5);
|
|
|
|
}
|
|
|
|
}
|
2024-05-23 19:58:36 +08:00
|
|
|
LOG_CAT(info, GUI) << Separator;
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-08-07 11:45:13 +08:00
|
|
|
case EnrollSingle:
|
|
|
|
case EnrollExtended: {
|
2024-05-23 19:58:36 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
if (result == Success) {
|
2024-08-07 11:45:13 +08:00
|
|
|
uint16_t userId = InvalidUserId;
|
|
|
|
if (replyId == EnrollExtended) {
|
|
|
|
auto info = reinterpret_cast<const EnrollExtendedReply *>(data + 7);
|
2024-09-11 15:45:19 +08:00
|
|
|
uint16_t width = ntohs(info->imageWidth);
|
|
|
|
uint16_t height = ntohs(info->imageHeight);
|
2024-08-07 11:45:13 +08:00
|
|
|
userId = ntohs(info->userid);
|
2024-09-11 15:45:19 +08:00
|
|
|
LOG_CAT(info, GUI) << "注册成功,用户ID: " << userId << ", 图片大小: " << width << "x" << height
|
|
|
|
<< ", 已有ID: " << ntohs(info->enrolledId)
|
|
|
|
<< ", 姓名: " << (const char *)info->enrolledUsername;
|
|
|
|
|
2024-08-07 11:45:13 +08:00
|
|
|
emit newImageInfo(static_cast<MessageId>(replyId), width * height, info->md5);
|
|
|
|
} else {
|
2024-08-16 19:05:50 +08:00
|
|
|
auto info = reinterpret_cast<const EnrollReply *>(data + 7);
|
2024-08-07 11:45:13 +08:00
|
|
|
userId = ntohs(info->userid);
|
|
|
|
LOG_CAT(info, GUI) << "注册成功,用户ID: " << userId;
|
|
|
|
}
|
|
|
|
emit newEnrollResult(userId);
|
2024-05-23 19:58:36 +08:00
|
|
|
} else if (result == Failed4Timeout) {
|
2024-09-11 15:45:19 +08:00
|
|
|
LOG_CAT(info, GUI) << "录入超时。";
|
|
|
|
emit errorOccurred(NoteId::InteractWarning, "录入", "录入超时");
|
|
|
|
} else if (result == Failed4PalmEnrolled) {
|
|
|
|
emit errorOccurred(NoteId::InteractWarning, "手掌已被录入");
|
|
|
|
LOG_CAT(info, GUI) << "手掌已被录入。";
|
2024-07-31 16:08:38 +08:00
|
|
|
} else {
|
|
|
|
LOG_CAT(info, GUI) << "未知错误(" << static_cast<int>(result) << ")。";
|
2024-05-21 21:09:55 +08:00
|
|
|
}
|
2024-05-23 19:58:36 +08:00
|
|
|
LOG_CAT(info, GUI) << Separator;
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-07-31 16:08:38 +08:00
|
|
|
case GetImage: {
|
2024-06-05 12:13:10 +08:00
|
|
|
if (result == Success) {
|
|
|
|
auto info = reinterpret_cast<const ImageSliceReply *>(data + 7);
|
2024-06-20 21:28:30 +08:00
|
|
|
uint32_t sliceSize = ntohl(info->size);
|
|
|
|
emit newImageSliceData(std::vector<uint8_t>(info->data, info->data + sliceSize));
|
2024-07-31 16:08:38 +08:00
|
|
|
} else {
|
|
|
|
LOG(info) << "GetImage failed, status: " << static_cast<int>(result);
|
2024-06-05 12:13:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-05-21 21:09:55 +08:00
|
|
|
case DeleteUser: {
|
|
|
|
if (result == Success) {
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "删除用户成功。";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
} else {
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "删除用户失败。";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeleteAll: {
|
|
|
|
if (result == Success) {
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "删除所有用户成功。";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-06-20 21:28:30 +08:00
|
|
|
case UploadImageInfo: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UploadImageData: {
|
2024-08-16 19:05:50 +08:00
|
|
|
auto info = reinterpret_cast<const UploadImageReply *>(data + 7);
|
2024-07-01 15:07:13 +08:00
|
|
|
if (result == Success) {
|
2024-08-16 19:05:50 +08:00
|
|
|
if (info->operation == 0) {
|
|
|
|
LOG_CAT(info, GUI) << "图片下发注册成功,用户ID: " << ntohs(info->userid);
|
|
|
|
} else if (info->operation == 1) {
|
|
|
|
auto result = reinterpret_cast<const UploadImageVerifyReply *>(info);
|
|
|
|
LOG_CAT(info, GUI) << "图片下发识别成功,用户ID: " << ntohs(result->userid) << ", 用户名: "
|
|
|
|
<< std::string_view(reinterpret_cast<const char *>(result->result.username))
|
|
|
|
<< ", 耗时: " << ntohs(result->result.elapsed) << "ms";
|
|
|
|
} else {
|
|
|
|
LOG(warning) << "unknown upload image operation: " << info->operation;
|
|
|
|
}
|
2024-07-01 15:07:13 +08:00
|
|
|
LOG_CAT(info, GUI) << Separator;
|
2024-07-15 17:47:19 +08:00
|
|
|
} else if (result == Needmore) {
|
|
|
|
} else {
|
2024-08-16 19:05:50 +08:00
|
|
|
LOG(info) << "upload image failed, operation: " << static_cast<uint16_t>(info->operation)
|
|
|
|
<< ", status: " << static_cast<uint16_t>(result);
|
2024-07-01 15:07:13 +08:00
|
|
|
}
|
2024-06-20 21:28:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-06-21 15:11:18 +08:00
|
|
|
case GetCurrentStatus: {
|
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
|
|
|
LOG_CAT(info, GUI) << "模组当前状态: " << static_cast<int>(data[7]);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
break;
|
|
|
|
}
|
2024-07-15 17:47:19 +08:00
|
|
|
case GetUniqueID: {
|
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
|
|
|
auto id = reinterpret_cast<const ModuleId *>(data + 7);
|
|
|
|
LOG_CAT(info, GUI) << "模组ID: " << std::string_view(id->id, sizeof(id->id));
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
break;
|
|
|
|
}
|
2024-08-16 11:34:21 +08:00
|
|
|
case GetVersion: {
|
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
|
|
|
auto version = reinterpret_cast<const ModuleVersion *>(data + 7);
|
|
|
|
int length = std::strlen(version->version);
|
|
|
|
if (length > sizeof(version->version)) {
|
|
|
|
length = sizeof(version->version);
|
|
|
|
}
|
|
|
|
int ota = ntohl(version->otaVersion);
|
|
|
|
LOG_CAT(info, GUI) << "模组烧录版本: " << std::string_view(version->version, length)
|
|
|
|
<< ", OTA版本: " << ntohl(version->otaVersion);
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
m_verison = QString::fromLocal8Bit(version->version, length);
|
|
|
|
m_otaVerison = ota;
|
|
|
|
emit verisonChanged();
|
|
|
|
break;
|
|
|
|
}
|
2024-07-31 16:08:38 +08:00
|
|
|
case EnableDebug: {
|
|
|
|
LOG(info) << "set moudle debug mode: " << (result == Success);
|
|
|
|
break;
|
|
|
|
}
|
2024-05-21 21:09:55 +08:00
|
|
|
default:
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG(warning) << "unknown reply command: 0x" << (static_cast<int>(replyId) & 0xff)
|
|
|
|
<< ", data: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-06-13 15:41:40 +08:00
|
|
|
m_currentMessageId = Idle;
|
2024-06-21 11:02:28 +08:00
|
|
|
emit currentMessageIdChanged();
|
2024-06-13 15:41:40 +08:00
|
|
|
emit commandFinished(static_cast<MessageId>(replyId), static_cast<MessageStatus>(result));
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Note: {
|
2024-09-04 19:23:34 +08:00
|
|
|
auto noteId = static_cast<NoteId>(data[5]);
|
2024-05-21 21:09:55 +08:00
|
|
|
switch (noteId) {
|
2024-09-04 19:23:34 +08:00
|
|
|
case NoteId::Ready: {
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组上电初始化成功。";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
break;
|
|
|
|
}
|
2024-09-04 19:23:34 +08:00
|
|
|
case NoteId::PalmState: { // 模组返回的数据为当前帧的手掌状态
|
|
|
|
auto state = reinterpret_cast<const PalmStateNote *>(data + 6);
|
2024-09-05 16:36:54 +08:00
|
|
|
uint16_t palmState = ntohs(state->state);
|
|
|
|
if (palmState == NeedMoveToCenter) {
|
|
|
|
emit errorOccurred(NoteId::InteractWarning, "录入提示", "请将手掌置于画面中心");
|
2024-09-23 19:19:10 +08:00
|
|
|
} else if (palmState == TooFar) {
|
|
|
|
emit errorOccurred(NoteId::InteractWarning, "录入提示", "请将手掌靠近一点");
|
2024-09-05 16:36:54 +08:00
|
|
|
}
|
|
|
|
LOG(info) << "palm state: " << palmState;
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-09-04 19:23:34 +08:00
|
|
|
case NoteId::UnknownError: {
|
2024-06-05 12:13:10 +08:00
|
|
|
LOG_CAT(info, GUI) << "模组: " << protocolDataFormatString(data, size);
|
|
|
|
LOG_CAT(info, GUI) << "未知错误。";
|
|
|
|
LOG_CAT(info, GUI) << Separator;
|
|
|
|
break;
|
|
|
|
}
|
2024-09-04 19:23:34 +08:00
|
|
|
case NoteId::DebugInfo: {
|
2024-07-31 16:08:38 +08:00
|
|
|
auto message = reinterpret_cast<const char *>(data + 6);
|
|
|
|
LOG_CAT(info, GUI) << "模组日志: " << message;
|
|
|
|
break;
|
|
|
|
}
|
2024-09-04 19:23:34 +08:00
|
|
|
case NoteId::NoAliveImage: {
|
2024-08-05 17:42:27 +08:00
|
|
|
LOG(info) << "no alive image";
|
|
|
|
emit newImageInfo(Note, 600 * 800, nullptr);
|
|
|
|
break;
|
|
|
|
}
|
2024-05-21 21:09:55 +08:00
|
|
|
default:
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG(warning) << "unknown note command: 0x" << (static_cast<int>(noteId) & 0xff)
|
|
|
|
<< ", data: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2024-05-22 18:03:11 +08:00
|
|
|
LOG(warning) << "unknown command: 0x" << (static_cast<int>(data[2]) & 0xff)
|
|
|
|
<< ", data: " << protocolDataFormatString(data, size);
|
2024-05-21 21:09:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:03:11 +08:00
|
|
|
void ModuleCommunication::onReadyRead() {
|
2024-06-13 15:41:40 +08:00
|
|
|
// LOG(info) << "ModuleCommunication::onReadyRead";
|
2024-05-22 18:03:11 +08:00
|
|
|
m_receivedBuffer.append(m_serialPort->readAll());
|
|
|
|
while (m_receivedBuffer.size() >= 2) {
|
|
|
|
int beginIndex = -1;
|
|
|
|
for (int i = 0; i < m_receivedBuffer.size() - 1; i++) {
|
|
|
|
if (static_cast<uint8_t>(m_receivedBuffer[i]) == 0xEF &&
|
|
|
|
static_cast<uint8_t>(m_receivedBuffer[i + 1]) == 0xAA) {
|
|
|
|
beginIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (beginIndex < 0) {
|
|
|
|
m_receivedBuffer.clear();
|
|
|
|
break;
|
|
|
|
} else if (beginIndex != 0) {
|
|
|
|
m_receivedBuffer.remove(0, beginIndex);
|
|
|
|
beginIndex = 0;
|
|
|
|
}
|
2024-06-13 15:41:40 +08:00
|
|
|
|
2024-05-22 18:03:11 +08:00
|
|
|
if (m_receivedBuffer.size() < 5) break;
|
|
|
|
uint16_t packageSize = *reinterpret_cast<uint16_t *>(m_receivedBuffer.data() + 3);
|
|
|
|
packageSize = ntohs(packageSize);
|
|
|
|
|
|
|
|
uint16_t totalSize = 0;
|
|
|
|
if (packageSize == 0) {
|
|
|
|
totalSize = 6;
|
|
|
|
} else {
|
|
|
|
totalSize = packageSize + 6;
|
|
|
|
}
|
|
|
|
if (m_receivedBuffer.size() >= totalSize) {
|
|
|
|
processPackage(reinterpret_cast<const uint8_t *>(m_receivedBuffer.data()), totalSize);
|
|
|
|
m_receivedBuffer.remove(0, totalSize);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 15:41:40 +08:00
|
|
|
void ModuleCommunication::onErrorOccurred(QSerialPort::SerialPortError error) {
|
|
|
|
if (error == QSerialPort::NoError) return;
|
|
|
|
LOG_CAT(info, GUI) << m_serialPort->portName().toStdString() << ": " << m_serialPort->errorString().toStdString();
|
2024-09-05 16:36:54 +08:00
|
|
|
emit errorOccurred(NoteId::DeviceError, m_serialPort->errorString());
|
2024-06-13 15:41:40 +08:00
|
|
|
}
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
std::pair<uint8_t *, uint32_t> ModuleCommunication::generateFrame(MessageId command, const uint8_t *data,
|
|
|
|
uint16_t size) {
|
2024-06-20 21:28:30 +08:00
|
|
|
static uint8_t sendBuffer[4096] = {0};
|
2024-05-21 21:09:55 +08:00
|
|
|
memset(sendBuffer, 0, sizeof(sendBuffer));
|
|
|
|
sendBuffer[0] = 0xef;
|
|
|
|
sendBuffer[1] = 0xaa;
|
|
|
|
sendBuffer[2] = command;
|
|
|
|
sendBuffer[3] = size >> 8;
|
|
|
|
sendBuffer[4] = size & 0xff;
|
|
|
|
if (size > 0) {
|
|
|
|
memcpy(sendBuffer + 5, data, size);
|
|
|
|
}
|
|
|
|
sendBuffer[5 + size] = xor_checksum_byte(sendBuffer + 2, 3 + size);
|
|
|
|
return std::make_pair(sendBuffer, size + 6);
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:02:28 +08:00
|
|
|
void ModuleCommunication::setCurrentMessageIdStatus(MessageId messageId) {
|
|
|
|
if (m_currentMessageId != messageId) {
|
|
|
|
m_currentMessageId = messageId;
|
|
|
|
emit commandStarted(messageId);
|
|
|
|
emit currentMessageIdChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
std::string ModuleCommunication::protocolDataFormatString(const uint8_t *data, int size) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
for (int i = 0; i < size; i++) {
|
2024-05-22 18:03:11 +08:00
|
|
|
oss << "0x" << std::setfill('0') << std::setw(2) << std::hex << (static_cast<int>(data[i]) & 0xff) << " ";
|
2024-05-21 21:09:55 +08:00
|
|
|
}
|
|
|
|
return oss.str();
|
|
|
|
}
|