-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfid.cpp
357 lines (298 loc) · 11 KB
/
rfid.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "rfid.h"
#include "MFRC522_I2C.h"
extern "C" {
#include "buzzer.h"
#include "dracula.h"
}
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/mutex.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mfrc522, CONFIG_I2C_LOG_LEVEL);
void rfid_main(void *, void *, void *);
#define DT_DRV_COMPAT nxp_mfrc522
#define MFRC522_INIT_PRIO 64
// BUILD_ASSERT(MFRC522_INIT_PRIO > CONFIG_I2C_TCA954X_CHANNEL_INIT_PRIO,
// "RFID readers must be initialised after their bus");
struct mfrc522_data {
};
struct mfrc522_cfg {
struct i2c_dt_spec i2c;
const char *room_name;
enum RoomName room;
};
int mfrc522_init(const struct device *dev)
{
const struct mfrc522_cfg *config = (const struct mfrc522_cfg *)dev->config;
MFRC522 mfrc522(&config->i2c);
if (!device_is_ready(config->i2c.bus)) {
printk("I2C bus %s not ready\n", config->i2c.bus->name);
return -ENODEV;
}
mfrc522.PCD_Init();
// mfrc522.PCD_AntennaOn();
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
printk("MFRC522 @ %s Software Version: 0x%x\n", config->room_name, v);
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
printk("Room %s not ready\n", config->room_name);
return -ENODEV;
}
mfrc522.PCD_SetAntennaGain(0x30);
return 0;
}
#define MFRC522_INIT(inst) \
static mfrc522_data mfrc522_data_##inst = { \
}; \
static const mfrc522_cfg mfrc522_cfg_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
.room_name = DT_PROP(DT_DRV_INST(inst), bearsink_room), \
.room = DT_STRING_TOKEN(DT_DRV_INST(inst), bearsink_room) \
}; \
I2C_DEVICE_DT_INST_DEFINE(inst, \
mfrc522_init, NULL, \
&mfrc522_data_##inst, &mfrc522_cfg_##inst, \
POST_KERNEL, MFRC522_INIT_PRIO, NULL);
#define MFRC522_ROOM_CASE(inst) \
case DT_STRING_TOKEN(DT_DRV_INST(inst), bearsink_room): \
return DEVICE_DT_INST_GET(inst);
#define MFRC522_DETECT_NEW(inst) \
detect_new_card(mfrc522, (const struct mfrc522_cfg *)DEVICE_DT_INST_GET(inst)->config);
DT_INST_FOREACH_STATUS_OKAY(MFRC522_INIT)
const struct device *get_room(enum RoomName room)
{
switch (room) {
DT_INST_FOREACH_STATUS_OKAY(MFRC522_ROOM_CASE)
default:
return NULL;
}
}
struct DraculaToken {
enum RoomName room = NUM_ROOMS;
unsigned char uid[7];
enum TokenKind kind;
};
K_MUTEX_DEFINE(tokensMutex);
/**
* @brief A list of the tokens currently on readers.
*
* This list is used primarily for internal tracking. When new tokens are
* detected, they are appended to this list and a callback is fired to the game
* logic. The game logic can also copy this list out with an rfid_ function.
*
* Empty entries are marked with a room name of NUM_ROOMS.
*
* This list is protected by tokensMutex
*/
DraculaToken currentTokens[MAX_TOKENS];
/**
* @brief Scan the chosen room for new tokens
*
* This function will configure mfrc522 to use the given room internally.
*/
void detect_new_card(MFRC522 mfrc522, const struct mfrc522_cfg* room)
{
mfrc522.i2c = &room->i2c;
mfrc522.PCD_AntennaOn();
// Check tokens we know about -- do this first so they don't show up as 'new' later.
for (int i = 0; i < 4; i++) {
int token_index = room->room * 4 + i;
if (currentTokens[token_index].room == NUM_ROOMS) {
// This is not a token; ignore
continue;
}
mfrc522.uid.size = 7;
mfrc522.uid.sak = 0;
memcpy(mfrc522.uid.uidByte, currentTokens[token_index].uid, 7);
// Wake up all the tokens on this reader
byte bufferATQA[2];
byte bufferSize = sizeof(bufferATQA);
byte result = mfrc522.PICC_WakeupA(bufferATQA, &bufferSize);
// Select the token we're working with currently
result = mfrc522.PICC_Select(&mfrc522.uid, 7 * 8);
if (result == MFRC522::STATUS_TIMEOUT) {
// This token probably disappeared; forget about it
printk("[Token (%d) removed from %s]\n", currentTokens[token_index].kind, room->room_name);
currentTokens[token_index].room = NUM_ROOMS;
}
// Put it into the HALT state
mfrc522.PICC_HaltA();
}
// Now check for new cards -- still in the IDLE state
if (!mfrc522.PICC_IsNewCardPresent()) {
mfrc522.PCD_AntennaOff();
return;
}
// Read the new card's UID into mfrc522.uid
if (!mfrc522.PICC_ReadCardSerial()) {
buzzer_send(READ_ERROR);
printk("[Invalid token in %s]\n", room->room_name);
mfrc522.PCD_AntennaOff();
return;
}
/*
* Bears Ink tokens contain absolute URLs to https://thebears.ink/game/kind
* as an identifier. They can be scanned e.g. a player's phone to take them
* to our website, and created by players at will with a standard NFC
* reader/writer app.
*
* Ideally, we should be parsing the NDEF metadata to extract the URL, but
* I couldn't figure out how it fits in the tag, so I'm parsing a fixed
* offset based on empirical evidence.
*/
// Read the metadata and the first half of the URL
byte data[18];
byte data_len = 18;
mfrc522.MIFARE_Read(6, data, &data_len);
byte url_len = data[1]; // Payload length field
// Make sure it's a Bears Ink token
if (strncmp(reinterpret_cast<char*>(data + 4), "thebears.ink", 12) != 0) {
buzzer_send(READ_ERROR);
printk("[Invalid token in %s]\n", room->room_name);
mfrc522.PCD_AntennaOff();
return;
}
// Read the second half of the magic URL
data_len = 18;
mfrc522.MIFARE_Read(10, data, &data_len);
// Null-terminate the payload so it can be interpreted as a string.
// 13 here represents "thebears.ink" and the leading protocol byte
data[url_len - 13] = '\0';
char *game = reinterpret_cast<char*>(data);
// Make sure it's a Dracula game token
if (strncmp(game, "/dracula/", 9) != 0) {
buzzer_send(READ_ERROR);
printk("[Invalid token in %s]\n", room->room_name);
mfrc522.PCD_AntennaOff();
return;
}
// Check what kind of token it is
char *kind = game + 9;
struct Token this_token;
this_token.room = room->room;
if (strcmp(kind, "player1") == 0) {
this_token.kind = Player1;
printk("[Player 1 placed in %s] ", room->room_name);
} else if (strcmp(kind, "player2") == 0) {
this_token.kind = Player2;
printk("[Player 2 placed in %s] ", room->room_name);
} else if (strcmp(kind, "player3") == 0) {
this_token.kind = Player3;
printk("[Player 3 placed in %s] ", room->room_name);
} else if (strcmp(kind, "player4") == 0) {
this_token.kind = Player4;
printk("[Player 4 placed in %s] ", room->room_name);
} else if (strcmp(kind, "garlic") == 0) {
this_token.kind = Garlic;
printk("[Garlic placed in %s] ", room->room_name);
} else if (strcmp(kind, "sun") == 0) {
this_token.kind = Sunlight;
printk("[Sunlight placed in %s] ", room->room_name);
} else if (strcmp(kind, "water") == 0) {
this_token.kind = HolyWater;
printk("[Holy Water placed in %s] ", room->room_name);
} else {
buzzer_send(READ_ERROR);
printk("[Invalid token in %s]\n", room->room_name);
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
mfrc522.PCD_AntennaOff();
return;
}
// Send the token to the game logic
if (token_valid(this_token)) {
buzzer_send(READ_OK);
printk("(Accepted)\n");
} else {
buzzer_send(READ_ERROR);
printk("(Rejected)\n");
}
// We've found a valid token; add it to currentTokens
k_mutex_lock(&tokensMutex, K_FOREVER);
for (int i = 0; i < 4; i++) {
int token_index = room->room * 4 + i;
if (currentTokens[token_index].room != NUM_ROOMS) {
// This entry is occupied.
continue;
}
currentTokens[token_index].room = this_token.room;
currentTokens[token_index].kind = this_token.kind;
memcpy(currentTokens[token_index].uid, mfrc522.uid.uidByte, 7);
break;
}
k_mutex_unlock(&tokensMutex);
// Halt the token. We can wake it up later when we want to check its presence.
mfrc522.PICC_HaltA();
mfrc522.PCD_AntennaOff();
}
/**
* @brief Check all the tokens in currentTokens to see if they're still there
*
* Takes tokensMutex internally and removes non-present tokens.
*/
void detect_current_cards(MFRC522 mfrc522)
{
k_mutex_lock(&tokensMutex, K_FOREVER);
for (int i = 0; i < MAX_TOKENS; i++) {
if (currentTokens[i].room == NUM_ROOMS) {
// Empty list entry; ignore.
continue;
}
const struct mfrc522_cfg *room =
(const struct mfrc522_cfg *)get_room(currentTokens[i].room)->config;
mfrc522.i2c = &room->i2c;
mfrc522.PCD_AntennaOn();
mfrc522.uid.size = 7;
mfrc522.uid.sak = 0;
memcpy(mfrc522.uid.uidByte, currentTokens[i].uid, 7);
// Wake up all the tokens on this reader
byte bufferATQA[2];
byte bufferSize = sizeof(bufferATQA);
byte result = mfrc522.PICC_WakeupA(bufferATQA, &bufferSize);
// Select the token we're working with currently
result = mfrc522.PICC_Select(&mfrc522.uid, 7 * 8);
if (result == MFRC522::STATUS_TIMEOUT) {
// This token probably disappeared; forget about it
printk("Token %d removed from room %s.\n", currentTokens[i].kind, room->room_name);
currentTokens[i].room = NUM_ROOMS;
}
mfrc522.PICC_HaltA();
mfrc522.PCD_AntennaOff();
}
k_mutex_unlock(&tokensMutex);
}
int rfid_get_tokens(struct Token *tokens)
{
// Acquire the mutex so the rfid thread doesn't change the current tokens while we're reading
// them out.
k_mutex_lock(&tokensMutex, K_FOREVER);
int tokenCount = 0;
for (int i = 0; i < MAX_TOKENS; i++) {
if (currentTokens[i].room == NUM_ROOMS) {
// Empty list entry; ignore.
continue;
}
tokens[tokenCount].kind = currentTokens[i].kind;
tokens[tokenCount].room = currentTokens[i].room;
tokenCount++;
}
k_mutex_unlock(&tokensMutex);
return tokenCount;
}
void rfid_onestep()
{
MFRC522 mfrc522;
DT_INST_FOREACH_STATUS_OKAY(MFRC522_DETECT_NEW)
// detect_current_cards(mfrc522);
}
void rfid_main(void *, void *, void *)
{
MFRC522 mfrc522;
for (;;) {
DT_INST_FOREACH_STATUS_OKAY(MFRC522_DETECT_NEW)
// detect_current_cards(mfrc522);
}
}