This repository was archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfcaccess.cpp
More file actions
394 lines (336 loc) · 11.5 KB
/
Copy pathnfcaccess.cpp
File metadata and controls
394 lines (336 loc) · 11.5 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
NFC/Rfid Permission checker with MFRC522, SDCard and Cloud Functions
Copyright (C) 2015 Leonid Verhovskij<info@leonv.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "application.h"
//this include command compile only on particle-cli, to make it work on atom, remove the folder
#include "SparkJson/SparkJson.h" //json lib
#include "SdFat/SdFat.h" //sd-card lib
#include "MFRC522/MFRC522.h" //rfid lib
//include random number generator from stm32 lib
#include "stm32f2xx_rng.h"
#include "stm32f2xx_rcc.h"
#define SS_PIN A1
#define RST_PIN D2
#define BUFFER_SIZE 18
#define SESSIONKEY_SIZE 4
// SOFTWARE SPI pin configuration - modify as required
// The default pins are the same as HARDWARE SPI
//VCC should be 5V, 3.3V was not suficient!
#define SD_CS A2 // Also used for HARDWARE SPI setup
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
//d4 = miso, d5 = mosi, d3 = sck
SdFatSoftSpi<D4, D5, D3> SD;
File myFile;
MFRC522::MIFARE_Key key;
//make functions available
bool nfcScanCard();
bool nfcAuthKeys();
bool nfcCheckPiccType();
int cloudUpdateKey(String);
bool fileWriteLog(String);
int fileAuthUid(String);
void openDoor();
void checkTaster();
// In this sample we use the second sector (ie block 4-7). the first sector is = 0
byte sector = 2;
// block sector 0-3(sector0) 4-7(sector1) 8-11(sector2)
byte valueBlockA = 4;
byte trailerBlock = 7;
byte status;
bool sdStatus = false;
String fileLog = "log.txt";
String weekDays[7] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
int userAuthStatus = 0;
//timout var counter for door access
unsigned long previousMillis = 0;
const int doorPin = D0; // number of relais pin
const int tasterPin = D1; //number of taster pin - used to open door from inside
const long interval = 2000; // interval how long door will be opened
bool openDoorCommand = false;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
mfrc522.setSPIConfig();
mfrc522.PCD_Init(); // Init MFRC522 card
// Initialize SOFTWARE SPI, save status of sd init for later
if (!SD.begin(SD_CS, SPI_FULL_SPEED)) {
Serial.println("initialization failed!");
sdStatus = false;
}else{
sdStatus = true;
}
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
//enable random number generator
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
//start random number generator
RNG_Cmd(ENABLE);
Particle.function("updateKey", cloudUpdateKey);
Time.zone(+2);
pinMode(doorPin, OUTPUT);
pinMode(tasterPin, INPUT);
}
void loop() {
openDoor();
checkTaster();
if(!nfcScanCard()){
mfrc522.PICC_HaltA();
//Serial.println("Scanning...");
delay(200);
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
Serial.print("Card UID:");
String uid = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
//uid += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "_";
uid += "_";
uid += mfrc522.uid.uidByte[i], HEX;
}
Serial.println(uid);
if(!nfcCheckPiccType()) return;
//check if sd card is present, otherwise cancel
if(!sdStatus){
Serial.println("sd card initialization failed!");
mfrc522.PICC_HaltA();
return;
}
fileWriteLog(uid);
if(!nfcAuthKeys()) {
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
Serial.println("sector decryption failed!");
//expose uid to cloud
uid.concat("_0");
Particle.publish("chip-uid", uid, 60, PRIVATE);
return;
}
userAuthStatus = fileAuthUid(uid);
if(userAuthStatus == 0){
Serial.println("User not found.");
uid.concat("_1");
Particle.publish("chip-uid", uid, 60, PRIVATE);
}
else if(userAuthStatus == 1){
Serial.println("User not authorized.");
uid.concat("_2");
Particle.publish("chip-uid", uid, 60, PRIVATE);
}else if(userAuthStatus == 2){
Serial.println("Welcome!");
uid.concat("_3");
Particle.publish("chip-uid", uid, 60, PRIVATE);
previousMillis = millis();
openDoorCommand = true;
openDoor();
}
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
void openDoor(){
if(openDoorCommand == false){
digitalWrite(doorPin, LOW);
}else{
digitalWrite(doorPin, HIGH);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
openDoorCommand = false;
}
}
}
void checkTaster(){
if(digitalRead(tasterPin) == LOW){
previousMillis = millis();
openDoorCommand = true;
openDoor();
}
}
bool nfcScanCard(){
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return false;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return false;
}
return true;
}
bool nfcCheckPiccType(){
// Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.print("PICC type: ");
Serial.println(mfrc522.PICC_GetTypeName(piccType));
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
//Serial.println("This sample only works with MIFARE Classic cards.");
return false;
}
return true;
}
bool nfcAuthKeys(){
// Authenticate using key A.
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return false;
}
// Authenticate using key B.
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return false;
}
return true;
}
/*
* @return: 0 = User not exist, 1 = auth error!; 2=auth successful
*/
int fileAuthUid(String uid){
int id;
DynamicJsonBuffer jsonBuffer;
//read existing data from file
myFile = SD.open(uid);
String command = "";
char character;
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
character = myFile.read();
command += character;
}
// close the file:
myFile.close();
} else {
return 0; //no file found, end of user search..
}
//debug only!
Serial.println(command);
//parse config
char json[command.length() + 1];
command.toCharArray(json, command.length() + 1);
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
//Serial.println("parseObject() failed");
return 0;//goto next user
}
//compare uid
if(!uid.equals(root["u"])) return 0; //goto next user
//compare time
int currentWeekDay = Time.weekday();
int currentHour = Time.hour();
//look for daily access permission
if(root["t"][currentWeekDay-1] == 0){
return 1; //access denied on current day
}
//compare and override otp
unsigned char sessionKey[BUFFER_SIZE];
byte size = BUFFER_SIZE;
status = mfrc522.MIFARE_Read(valueBlockA, sessionKey, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return 1; //READ Failed
}
//convert char array to uint32_t
uint32_t intSessionKey;
memcpy(&intSessionKey, sessionKey, 4);
if(root["otp"] != 0 && root["otp"] != intSessionKey){
return 1;
}
//wait for random number generator and save values
while(!RNG_GetFlagStatus(RNG_FLAG_DRDY));
uint32_t otp = RNG_GetRandomNumber(); //read and clear random number
Serial.print("OTP: ");
Serial.println(otp);
unsigned char otpChar[16];
for(int i=0;i<16;i++) otpChar[i] = 0xFF;
memcpy(otpChar, &otp, SESSIONKEY_SIZE);
status = mfrc522.MIFARE_Write(valueBlockA, otpChar, 16); //must be 16 here, otherwise write fail
if (status != MFRC522::STATUS_OK) {
Serial.print(F("updateSessionId() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return 1;
}
root["otp"] = otp;
String fileName = String(id);
char charFile[fileName.length() + 1];
fileName.toCharArray(charFile, fileName.length() + 1);
SD.remove(charFile); //rewrite file
myFile = SD.open(String(id), FILE_WRITE);
if (myFile) {
root.printTo(myFile);
// close the file:
myFile.close();
return 2;
} else {
// if the file didn't open, print an error:
Serial.println("error opening uid file");
return 1;
}
}
bool fileWriteLog(String command){
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open(fileLog, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print(Time.now());
myFile.print(" : ");
myFile.println(command);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening fileLog");
}
}
int cloudUpdateKey(String command){
//Convert cloud param to json
DynamicJsonBuffer jsonBuffer;
char json[command.length() + 1];
command.toCharArray(json, command.length() + 1);
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("parseObject() failed");
return -1;
}
int id;
bool userFound = false;
bool firstUser = true;
const char* charUid = root["u"];
String fileName = String(charUid);
SD.remove(fileName); //rewrite file if user exists
myFile = SD.open(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile && sdStatus) {
Serial.print("Writing uid file");
root.printTo(myFile);
// close the file:
myFile.close();
Serial.println(" done.");
return 1;
} else {
// if the file didn't open, print an error:
Serial.println("error opening uid file");
return -1;
}
}