-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardReaderSystem.cpp
160 lines (142 loc) · 5.49 KB
/
cardReaderSystem.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
#include <Arduino.h>
#include <Ethernet.h>
#include <Adafruit_PN532.h>
#include <LiquidCrystal_I2C.h>
#include "doorDisplay.h"
#include "doorLock.h"
#include "makey.h"
#include "cardReaderSystem.h"
CardReaderSystem::CardReaderSystem(DoorDisplay *display, DoorLock *lock, Makey *makey, Adafruit_PN532 *nfc, uint8_t *secret_key)
{
this->display = display;
this->lock = lock;
this->makey = makey;
this->nfc = nfc;
this->secret_key = secret_key;
}
void CardReaderSystem::initializeReader() {
while(1) {
nfc->begin();
uint32_t versiondata = nfc->getFirmwareVersion();
if (versiondata) {
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
break;
}
}
// configure board to read RFID tags
nfc->SAMConfig();
}
void CardReaderSystem::attemptRead() {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t data[16];
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
display->readyMessage();
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");
Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc->PrintHex(uid, uidLength);
Serial.println("");
//try to read the badge data from page 4 using the default code
Serial.println("Trying to authenticate block 4 with default KEYA value");
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uint8_t success = nfc->mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
if(success) {
//new card! we need to generate a random key for use in the future.
Serial.println("New card");
display->verifyMessage();
if(success) {
Serial.println("Reading data...");
success = nfc->mifareclassic_ReadDataBlock(7, data);
if(success) {
//now we have the keys, overwrite keya
data[0] = secret_key[0];
data[1] = secret_key[1];
data[2] = secret_key[2];
data[3] = secret_key[3];
data[4] = secret_key[4];
data[5] = secret_key[5];
success = nfc->mifareclassic_WriteDataBlock(7, data);
Serial.println("Wrote key");
success = nfc->mifareclassic_WriteDataBlock(4, makey->key);
Serial.println("Wrote secret");
makey->rotateKey();
}
}
} else {
Serial.println("Not factory, trying our key");
success = nfc->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
}
if (success) {
// SHHH IT'S A SECRET - this is subobtimal, but we'll go with it for now
success = nfc->mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, secret_key);
if(success) {
success = nfc->mifareclassic_ReadDataBlock(4, data);
if(success) {
Serial.println("Valid card!");
//state = STATE_VERIFYING;
display->verifyMessage2();
// Make a HTTP request:
char buffer[14];
char buffer2[33];
for(int xx=0;xx<7;xx++) {
if(xx>=uidLength) {
uid[xx]=0;
}
}
sprintf(buffer, "/door/%02X%02X%02X%02X%02X%02X%02X%02X", uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uidLength);
sprintf(buffer2, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
, data[0]
, data[1]
, data[2]
, data[3]
, data[4]
, data[5]
, data[6]
, data[7]
, data[8]
, data[9]
, data[10]
, data[11]
, data[12]
, data[13]
, data[14]
, data[15]);
const char* headers[] = {"Host: makey->localdomain", "Connection: close", "Content-Type: text/plain;charset=UTF-8", "Content-Length: 32"};
MakeyResponse res = makey->request(buffer, headers, 4, buffer2, 60000);
if(res.statusCode == 200) {
Serial.println("Accepted!");
int idx2 = res.body.indexOf("\n");
display->message(res.body.substring(0, idx2).c_str(), res.body.substring(idx2+1).c_str());
lock->unlockFor(5000);
} else if (res.statusCode == 0) {
display->serverFailMessage();
delay(5000);
} else {
Serial.println("Failed!");
int idx2 = res.body.indexOf("\n");
display->message(res.body.substring(0, idx2).c_str(), res.body.substring(idx2+1).c_str());
delay(5000);
}
}
}
if(!success) {
Serial.println("Invalid card!");
display->serverFailMessage();
delay(5000);
return;
}
}
return;
}
}