-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
278 lines (234 loc) · 6.88 KB
/
Database.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "Database.h"
/*
* @note
* - `timeFlag` is used to ensure that timestamps are set once only for each
* inventoried card. Initialise to `true`.
* - Make sure every time calling inventoryCards(), a timeFlag is wrapped
* around.
* @example:
* ```
* timeFlag = true;
* Status status = inventoryCards(&tmp, rawData);
* timeFlag = false;
* ```
* @todo Find a more elegant way to do this!
*/
bool timeFlag = true;
/*
* @note
* `_prefix` is set corresponding to the project of Tictag
* (default: empty string)
*
* @todo
* it's really dangerous to set `_prefix` as an extern variable.
* Figure out how to limit the scope of `_prefix`.
*/
String _prefix = "";
Database::Database(): CQueue() {} //< Constructor
/**
* @public
* @brief Initialise Database
*/
void Database::begin()
{
// [WARNING] IMPORTANT
// seed for random number, used in hashing TID.
randomSeed(analogRead(ANALOG_PIN));
}
/**
* @public
* @brief Store inventoried cards to database
*/
Status Database::inventoryCards(CQueue* inventoryDB, byte* rawData)
{
// Get number of cards in the effective field
byte numCards = rawData[RE_INV_NUM_CARDS_INDEX];
if ((rawData[RE_STATUS_INDEX] == STATUS_SUCCESS)
&& (numCards > MAX_CARDS)) {
return ERR_NUM_CARDS;
}
if (rawData[RE_STATUS_INDEX] != STATUS_SUCCESS)
return rawData[RE_STATUS_INDEX];
Card tmp[MAX_CARDS] = {0};
byte sizeIndex = RE_INV_TID_SIZE_INDEX;
for (byte i = 0; i < numCards; i++) {
tmp[i].tid.size = rawData[sizeIndex]; //< get TID size
tmp[i].status = false;
// Get TID from inventory command respond frame
for (size_t j = 0; j < tmp[i].tid.size; j++) {
tmp[i].tid.tidByte[j] = rawData[j + sizeIndex + 1];
}
// A timeFlag is added to make sure the timestamp is set once for
// each card.
if (timeFlag) {
tmp[i].time = millis();
}
if (inventoryDB->isFull())
inventoryDB->dequeue();
inventoryDB->enqueue(tmp[i]);
/**
* Update new index of TID size
* New index = previous TID size + 1
*/
sizeIndex += rawData[sizeIndex] + 1;
}
return STATUS_SUCCESS;
}
/**
* @public
* @brief Store cards to the permanent database
*/
Status Database::updateDB(byte* rawData)
{
CQueue tmp; //< temporary database to store inventoried cards in the
// current session
timeFlag = true;
Status status = inventoryCards(&tmp, rawData);
timeFlag = false;
if (status != STATUS_SUCCESS)
return status;
// Compare the temporary database (current inventory session) with the
// permanent database.
for (byte i = 0; i < tmp.getSize(); i++) { //< Temporary databse
String newTid = toString(tmp.getQueueData()[i].tid);
byte j = 0;
for (j = 0; j < _database.getCapacity(); j++) { //< Permanent one
String tmpTid = toString(_database.getQueueData()[j].tid);
if (tmpTid == newTid) { //< if match
// Disconnect expired cards - by setting `.status = false`
if ((millis() - _database.getQueueData()[j].time) >= EXPIRE_TIME) {
_database.getQueueData()[j].status = false;
}
_database.getQueueData()[j].time = millis(); //< Update time stamp
break;
}
}
// if there is no card that match
if (j == _database.getCapacity()) {
byte q = 0;
// Iterate to overwrite new cards to expired cards
for (q = 0; q < _database.getSize(); q++) {
if ((millis() - _database.getQueueData()[q].time) >= EXPIRE_TIME) {
_database.getQueueData()[q] = tmp.getQueueData()[i];
break;
}
}
// If there is no expired cards, then enqueue card to the end of
// the queue
if (q == _database.getSize()) {
_database.enqueue(tmp.getQueueData()[i]);
}
}
}
return STATUS_SUCCESS;
}
/**
* @public
* @brief Get cards' database
*/
CQueue& Database::getDB()
{
return _database;
}
/**
* @public
* @brief Print hashed TIDs to keyboard (for web dev team).
*/
void Database::printToKeyboard()
{
// Pass prtCardKeyboard() to CQueue::_debugPrint()`
_database._debugPrint(prtCardKeyBoard);
}
/**
* @public or @protected
* @brief Debug function - print the whole database
*/
void Database::_debugPrintDB(CQueue& database)
{
Serial.println("TID\t\t\tStatus\t\tTime");
// Pass prtCardInfo() to CQueue::_debugPrint()`
database._debugPrint(prtCardInfo);
}
/**
* @public or @protected
* @brief Debug function - print welcome message
*/
void Database::_debugPrintDBMsg()
{
// Pass prtCardMsg() to CQueue::_debugPrint()`
_database._debugPrint(prtCardMsg);
}
/* @brief Set prefix for Tictag JSC projects */
void _setPrefix(const String prefix)
{
_prefix = prefix;
}
/* @brief Convert TID to string */
String toString(TID& tid)
{
String strTid;
for (size_t i = 0; i < tid.size; i++) {
byte tmp = tid.tidByte[i];
// Add leading 0s to make sure every byte has 3 digits
if (tmp < 100) strTid += "0";
if (tmp < 10) strTid += "0";
strTid += String(tmp, DEC);
}
return strTid;
}
/* @brief Hash generation */
const String generateHash(TID tid, const String prefix)
{
String strTid;
byte n = 2;
byte key[n];
for (byte i = 0; i < n; i++) {
key[i] = random(1, 255);
}
for (byte i = 0; i < tid.size; i++) {
tid.tidByte[i] ^= key[i % n];
}
strTid = toString(tid);
for (byte i = 0; i < n; i++) {
if (key[i] < 100) strTid += "0";
if (key[i] < 10) strTid += "0";
strTid += String(key[i], DEC);
}
return prefix + strTid;
}
/*
* These functions are used for passing to CQueue::_debugPrint for queue
* debugging purpose
*/
/* Print card information: TID, status, time. */
void prtCardInfo(Card* card)
{
String tid = toString(card->tid);
Serial.print(tid);
Serial.print("\t");
Serial.print(card->status);
Serial.print("\t\t");
Serial.println(card->time);
}
/* Print welcome message - for testing card.status */
void prtCardMsg(Card* card)
{
if (card->status == false) {
Serial.print("Hello ");
Serial.print(toString(card->tid));
Serial.print(". You are in at ");
Serial.print(card->time);
Serial.println(". ");
card->status = true;
}
}
/* Print hased TID to keyboard */
void prtCardKeyBoard(Card* card)
{
if (card->status == false) {
Keyboard.println(generateHash(card->tid, _prefix));
card->status = true;
delay(800); //< Modify this delay() to change delay time between cards
// if there are multiple cards are tapped at once
}
}