2024-05-21 21:09:55 +08:00
|
|
|
|
#ifndef MODULECOMMUNICATION_H
|
|
|
|
|
#define MODULECOMMUNICATION_H
|
|
|
|
|
|
2024-05-24 10:23:05 +08:00
|
|
|
|
#include "DataStructure.h"
|
2024-05-21 21:09:55 +08:00
|
|
|
|
#include <QObject>
|
2024-06-21 11:02:28 +08:00
|
|
|
|
#include <QQmlEngine>
|
2024-06-13 15:41:40 +08:00
|
|
|
|
#include <QSerialPort>
|
2024-05-21 21:09:55 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
class ModuleCommunication : public QObject {
|
|
|
|
|
Q_OBJECT
|
2024-06-21 11:02:28 +08:00
|
|
|
|
QML_ELEMENT
|
2024-05-21 21:09:55 +08:00
|
|
|
|
static constexpr uint32_t UsernameSize = 32;
|
|
|
|
|
static constexpr const char *Separator = "----------";
|
2024-06-21 11:02:28 +08:00
|
|
|
|
Q_PROPERTY(MessageId currentMessageId READ currentMessageId NOTIFY currentMessageIdChanged)
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2024-06-13 15:41:40 +08:00
|
|
|
|
constexpr static uint16_t InvalidUserId = std::numeric_limits<uint16_t>::max();
|
2024-05-21 21:09:55 +08:00
|
|
|
|
enum MessageId : uint8_t {
|
|
|
|
|
Reply = 0,
|
|
|
|
|
Note = 0x01,
|
2024-05-23 19:58:36 +08:00
|
|
|
|
Reset = 0x10,
|
2024-06-21 15:11:18 +08:00
|
|
|
|
GetCurrentStatus = 0x11,
|
2024-05-21 21:09:55 +08:00
|
|
|
|
Verify = 0x12,
|
|
|
|
|
EnrollSingle = 0x1D,
|
2024-06-05 12:13:10 +08:00
|
|
|
|
EnrollGetImage = 0x1E,
|
|
|
|
|
GetEnrolledImage = 0x1F,
|
2024-05-21 21:09:55 +08:00
|
|
|
|
DeleteUser = 0x20,
|
|
|
|
|
DeleteAll = 0x21,
|
2024-06-20 21:28:30 +08:00
|
|
|
|
UploadImageInfo = 0xF6,
|
|
|
|
|
UploadImageData = 0xF7,
|
2024-05-22 18:03:11 +08:00
|
|
|
|
RegisterPalmFeature = 0xF9,
|
|
|
|
|
RequestPalmFeature = 0xFA,
|
2024-06-13 15:41:40 +08:00
|
|
|
|
Idle = 0xFF,
|
2024-05-21 21:09:55 +08:00
|
|
|
|
};
|
2024-06-21 11:02:28 +08:00
|
|
|
|
Q_ENUM(MessageId)
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
|
|
enum NoteId : uint8_t {
|
|
|
|
|
Ready = 0x00,
|
2024-06-05 12:13:10 +08:00
|
|
|
|
PalmState = 0x01,
|
|
|
|
|
UnknownError = 0x02,
|
2024-05-21 21:09:55 +08:00
|
|
|
|
};
|
|
|
|
|
enum MessageStatus : uint8_t {
|
|
|
|
|
Success = 0,
|
|
|
|
|
Rejected = 1,
|
|
|
|
|
Aborted = 2,
|
|
|
|
|
Failed4Camera = 4,
|
|
|
|
|
Failed4UnknownReason = 5,
|
|
|
|
|
Failed4InvalidParam = 6,
|
|
|
|
|
Failed4NoMemory = 7,
|
|
|
|
|
Failed4UnknownUser = 8,
|
|
|
|
|
Failed4MaxUser = 9,
|
|
|
|
|
Failed4FaceEnrolled = 10,
|
|
|
|
|
Failed4LivenessCheck = 12,
|
|
|
|
|
Failed4Timeout = 13,
|
|
|
|
|
Failed4Authorization = 14,
|
|
|
|
|
Failed4ReadFile = 19,
|
|
|
|
|
Failed4WriteFile = 20,
|
|
|
|
|
Failed4NoEncrypt = 21,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#pragma pack(1)
|
|
|
|
|
struct VerifyInfo {
|
|
|
|
|
uint8_t powerDownRightAway; // power down right away after verifying
|
|
|
|
|
uint8_t timeout; // timeout, unit second, default 10s
|
|
|
|
|
};
|
|
|
|
|
struct VerifyNoteInfo {
|
|
|
|
|
int16_t state; // corresponding to PALM_STATE_*
|
|
|
|
|
// position
|
|
|
|
|
int16_t left; // in pixel
|
|
|
|
|
int16_t top;
|
|
|
|
|
int16_t right;
|
|
|
|
|
int16_t bottom;
|
|
|
|
|
// pose
|
|
|
|
|
int16_t yaw; // up and down in vertical orientation
|
|
|
|
|
int16_t pitch; // right or left turned in horizontal orientation
|
|
|
|
|
int16_t roll; // slope
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EnrollData {
|
|
|
|
|
uint8_t admin = 0;
|
|
|
|
|
uint8_t username[32];
|
|
|
|
|
uint8_t palmDirection;
|
|
|
|
|
uint8_t timeout;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EnrollDataReply {
|
|
|
|
|
uint16_t userid;
|
|
|
|
|
uint8_t face_direction; // depleted, user ignore this field
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-05 12:13:10 +08:00
|
|
|
|
struct EnrolledImageReply {
|
2024-06-19 16:02:58 +08:00
|
|
|
|
uint8_t image_format; // 0: 只有Y分量,灰度图
|
2024-06-05 12:13:10 +08:00
|
|
|
|
uint16_t width;
|
|
|
|
|
uint16_t height;
|
|
|
|
|
uint8_t md5[16];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ImageSliceRequest {
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ImageSliceReply {
|
|
|
|
|
uint32_t size;
|
|
|
|
|
uint8_t data[0];
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
|
struct VerifyDataReply {
|
|
|
|
|
uint16_t userid;
|
|
|
|
|
uint8_t username[UsernameSize]; // 32Bytes uint8_t admin;
|
|
|
|
|
uint8_t unlockStatus;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-22 18:03:11 +08:00
|
|
|
|
struct PalmFeatureHeader {
|
|
|
|
|
uint16_t userid; // 用户ID
|
|
|
|
|
uint8_t username[32]; // 用户姓名
|
|
|
|
|
uint8_t admin; // 是否管理员,YES:1 NO:0
|
|
|
|
|
uint8_t featureDataMd5[16]; // 整体特征数据的MD5值
|
|
|
|
|
uint16_t featureTotalSize; // 特征数据总长度
|
2024-06-05 12:13:10 +08:00
|
|
|
|
uint8_t feature[0];
|
2024-05-22 18:03:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-06-20 21:28:30 +08:00
|
|
|
|
struct UploadImageInformation {
|
|
|
|
|
uint8_t operation; // 0:图片录入掌静脉
|
|
|
|
|
uint8_t format; // 0: 灰度图(纯Y分量)
|
|
|
|
|
uint16_t width;
|
|
|
|
|
uint16_t height;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
uint8_t md5[16]; // 图片内容 md5 值
|
|
|
|
|
char username[32];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct UploadImageDataSlice {
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
uint32_t size;
|
|
|
|
|
uint8_t reserved[4]; // 预留
|
|
|
|
|
uint8_t data[0];
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
|
#pragma pack()
|
|
|
|
|
explicit ModuleCommunication(QObject *parent = nullptr);
|
2024-06-13 15:41:40 +08:00
|
|
|
|
bool open(const QString &portName, int baudRate);
|
|
|
|
|
Q_INVOKABLE void verify(uint8_t timeout);
|
|
|
|
|
Q_INVOKABLE void reset();
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
|
|
|
|
void enroll(const std::string &username, uint8_t timeout);
|
2024-06-05 12:13:10 +08:00
|
|
|
|
void enrollEx(const std::string &username, uint8_t timeout);
|
2024-06-13 15:41:40 +08:00
|
|
|
|
Q_INVOKABLE void deleteUser(uint16_t userid);
|
|
|
|
|
Q_INVOKABLE void deleteAll();
|
2024-06-05 12:13:10 +08:00
|
|
|
|
void requestEnrolledImage(uint32_t offset, uint32_t size);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
|
2024-05-22 18:03:11 +08:00
|
|
|
|
void requestPalmFeature(uint16_t userid);
|
2024-05-23 19:58:36 +08:00
|
|
|
|
void enrollPalmFeature(uint16_t userid, const PalmFeature &feature);
|
2024-06-13 15:41:40 +08:00
|
|
|
|
|
2024-06-20 21:28:30 +08:00
|
|
|
|
void uploadImageInfo(const UploadImageInformation &info);
|
|
|
|
|
void uploadImageData(uint32_t offset, const uint8_t *data, uint32_t size);
|
2024-06-21 15:11:18 +08:00
|
|
|
|
Q_INVOKABLE void requestCurrentStatus();
|
2024-06-13 15:41:40 +08:00
|
|
|
|
|
2024-06-20 21:28:30 +08:00
|
|
|
|
MessageId currentMessageId() const;
|
|
|
|
|
static std::string protocolDataFormatString(const uint8_t *data, int size);
|
2024-05-22 20:17:07 +08:00
|
|
|
|
signals:
|
2024-06-13 15:41:40 +08:00
|
|
|
|
void newVerifyResult(uint16_t userid, const QString &username);
|
2024-05-22 20:17:07 +08:00
|
|
|
|
void newPalmFeature(const PalmFeature &feature);
|
2024-06-05 12:13:10 +08:00
|
|
|
|
void newEnrolledImageInfo(uint32_t size, const uint8_t *md5);
|
|
|
|
|
void newImageSliceData(const std::vector<uint8_t> &data);
|
2024-06-13 15:41:40 +08:00
|
|
|
|
void errorOccurred(const QString &error);
|
|
|
|
|
void commandStarted(ModuleCommunication::MessageId messageId);
|
|
|
|
|
void commandFinished(MessageId messageId, MessageStatus status);
|
2024-06-21 11:02:28 +08:00
|
|
|
|
void currentMessageIdChanged();
|
2024-05-22 18:03:11 +08:00
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
|
protected:
|
2024-05-22 18:03:11 +08:00
|
|
|
|
void processPackage(const uint8_t *data, uint16_t size);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
void onReadyRead();
|
2024-06-13 15:41:40 +08:00
|
|
|
|
void onErrorOccurred(QSerialPort::SerialPortError error);
|
2024-05-21 21:09:55 +08:00
|
|
|
|
std::pair<uint8_t *, uint32_t> generateFrame(MessageId command, const uint8_t *data = nullptr, uint16_t size = 0);
|
2024-06-21 11:02:28 +08:00
|
|
|
|
void setCurrentMessageIdStatus(ModuleCommunication::MessageId messageId);
|
2024-06-20 21:28:30 +08:00
|
|
|
|
|
2024-05-21 21:09:55 +08:00
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<QSerialPort> m_serialPort;
|
2024-05-22 18:03:11 +08:00
|
|
|
|
QByteArray m_receivedBuffer;
|
2024-06-13 15:41:40 +08:00
|
|
|
|
|
|
|
|
|
MessageId m_currentMessageId = ModuleCommunication::Idle;
|
2024-05-21 21:09:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // MODULECOMMUNICATION_H
|