forked from IntelRealSense/RealSenseID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialPacket.h
150 lines (133 loc) · 4.36 KB
/
SerialPacket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020-2021 Intel Corporation. All Rights Reserved.
#pragma once
//
// Serial packet spec (little endian for all uint16_t and uint32_t fields)
//
#include <cstdint>
#include <string.h>
#pragma pack(push)
#pragma pack(1)
namespace RealSenseID
{
namespace PacketManager
{
static const unsigned char ProtocolVer = 2;
static const size_t MaxUserIdSize = 30;
struct FaMessage
{
char user_id[MaxUserIdSize + 1]; // ascii only. '\0' terminated
char fa_status; // ascii status number (e.g. '0', '1', etc.)
};
struct DataMessage
{
char data[8124]; // any binary data to complete packet total size to exactly 8k
};
enum class SyncByte : char
{
Sync1 = '@',
Sync2 = 'F'
};
enum class MsgId : char
{
None = '-',
MinFa = 'A',
Authenticate = 'A',
DetectSpoof = 'B',
RemoveAllUsers = 'C',
RemoveUser = 'D',
Enroll = 'E',
EnrollImage = 'I',
EnrollImageFeatureExtraction = 'J',
Hint = 'H',
SecureFaceprintsEnroll = 'N',
SecureFaceprintsAuthenticate = 'Q',
Progress = 'P',
Result = 'R',
EnrollFaceprintsExtraction = 'T',
AuthenticateFaceprintsExtraction = 'X',
Reply = 'Y',
MaxFa = 'Z',
HostEcdsaKey = 'a',
DeviceEcdsaKey = 'b',
HostEcdhKey = 'c',
DeviceEcdhKey = 'd',
UploadImage = 'e',
Faceprints = 'f',
FaceDetected = 'g',
GetNumberOfUsers = 'n',
StartSession = 'o',
Ping = 'p',
QueryDeviceConfig = 'q',
SetDeviceConfig = 's',
StandBy = 't',
GetUserIds = 'u',
SecureFaceprintsBeginSecureSession = 'i',
SecureFaceprintsEndSecureSession = 'j',
SecureFaceprintsOnSecureSessionReady = 'k',
SecureFaceprintsOnSecureSessionCmd = 'l',
SecureFaceprintsOnSecureSessionCmdResp = 'm',
SecureFaceprintsFaceprintsReady = 'r',
SetUserFeatures = 'x',
GetUserFeatures = 'y',
};
struct SerialPacket
{
struct
{
SyncByte sync1;
SyncByte sync2;
unsigned char protocol_ver;
MsgId id; //'A'-'Z' fa message, 'a-'z' data message
unsigned char iv[16];
uint16_t payload_size;
} header;
struct
{
uint32_t sequence_number;
union {
FaMessage fa_msg;
DataMessage data_msg;
} message;
} payload;
char hmac[32]; // if security is enabled it will store hmac calculation
uint16_t crc;
SerialPacket();
};
static_assert(sizeof(SerialPacket) <= 8192, "SerialPacket size must not exceed 8192 bytes");
static_assert((sizeof(SerialPacket::payload) % 32 == 0), "payload size must be dividable by 32");
//
// fa packet
//
struct FaPacket : public SerialPacket
{
FaPacket(MsgId id, const char* user_id, char status);
FaPacket(MsgId id);
const char* GetUserId() const;
char GetStatusCode();
};
// data packet
struct DataPacket : public SerialPacket
{
// copy data to packet. pad with zeros if data_size is smaller than actual reserved data size
DataPacket(MsgId id, char* data, size_t data_size);
DataPacket(MsgId id);
const DataMessage& Data() const;
const size_t MessageSize()const
{
return this->header.payload_size - sizeof(this->payload.sequence_number);
}
};
bool IsFaPacket(const SerialPacket& packet); // if MsgId in the 'A'..'Z' range
bool IsDataPacket(const SerialPacket& packet); // if MsgId in the 'a'..'z' range
namespace Commands
{
static const char* face_api = "\r\n__FACE_API__\r\n";
static const char* face_cancel = "\r\n__FACE_CANCEL__\r\n";
static const char* version_info = "\r\nbspver\r\n";
static const char* device_info = "\r\nbspver -device\r\n";
static const char* reset = "\r\nreset\r\n";
} // namespace Commands
} // namespace PacketManager
}; // namespace RealSenseID
#pragma pack(pop)