Source

arduino_rfid_controller / wiegand_card_reader_v0.pde

Full commit
  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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * ©2010 Lokkju Brennr
 * RFID readers outputing 26-XX bit Wiegand code to pins, truncated to 32 bits and hashed :-
 * READER    WIRE COLOR PIN# DESCRIPTION CABLE COLOR
 * --------- ---------- ---- ----------- -----------
 * Reader A  Orange     2    Green LED   BLUE
 * Reader A  White      3    DATA1       GREEN/WHITE
 * Reader A  Green      4    DATA0       GREEN
 * Reader A  Red             +12v        ORANGE
 * Reader A  Black           GND         BROWN
 * Interrupt service routine gathers Wiegand pulses (zero or one) until a minimum of 32 have been recieved
 * Then a sting is sent to processing
 */

#include <EEPROM.h>

#include "interrupt_support.h"

#define BUFFERSIZE 20
#define MAX_NO_CARDS 200
#define READER_A_DATA0 4
#define READER_A_DATA1 3
#define READER_A_GREEN 2
#define OPEN_DOOR_PIN1 5

#define waitingLED 6
#define successLED 13
#define failureLED 8

int toggleState = 0;    // state of the toggling LED
long toggleTime = 0;    // delay time of the toggling LED
byte tag[4];
char inBytes[BUFFERSIZE]; //Buffer for serial in messages

// has to be a long - ints are 16 bits on this platform!
volatile long reader1 = 0;
volatile int reader1Count = 0;
int serialAvail = 0;
int serialIndex = 0;

void reader1One(void) {
  if(reader1Count < 32 && digitalRead(READER_A_DATA1) == LOW){
    reader1Count++;
    reader1 = reader1 << 1;
    reader1 |= 1;
  }
}

void reader1Zero(void) {
  if(reader1Count < 32 && digitalRead(READER_A_DATA0) == LOW){
    reader1Count++;
    reader1 = reader1 << 1;
  }
}

void setup()
{
  Serial.begin(9600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  PCattachInterrupt(READER_A_DATA1, reader1One, CHANGE);
  PCattachInterrupt(READER_A_DATA0, reader1Zero, CHANGE);

  delay(10);
  resetInterruptPin(3);
  resetInterruptPin(4);
  pinMode(OPEN_DOOR_PIN1, OUTPUT);
  digitalWrite(OPEN_DOOR_PIN1, LOW);
  pinMode(READER_A_GREEN, OUTPUT);
  digitalWrite(READER_A_GREEN, HIGH);
  digitalWrite(13, HIGH);
  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  blink(13,200,3);
  printHelp();
  delay(200);
}
void printHelp() {
  Serial.println("ARDUINO SECURITY v0.1");
  Serial.println(" all commands end with ;");
  Serial.println("h - print this help");
  Serial.println("n - add next card to database");
  Serial.println("r - delete next card from database");
  Serial.println("p - print database");
  Serial.println("c - clear entire database");
  Serial.println("d [x] - delete tag at provided index from database");
  Serial.println("o - open door");
}
void loop() {
  readSerialInput();
  seekKnownTags();
  delay(200);
} 

// erase the entire EEPROM memory storage:
void eraseEEPROM(){
  for(int i = 0; i < 1024; i++){
    EEPROM.write(i, 0);
  }
}

// writes tag number in a location:
void writeTag(int startingAddress, byte byte0, byte byte1, byte byte2, byte byte3){
  EEPROM.write( startingAddress*5, byte(1));
  EEPROM.write(startingAddress*5+1, byte0);
  EEPROM.write(startingAddress*5+2, byte1);
  EEPROM.write(startingAddress*5+3, byte2);
  EEPROM.write(startingAddress*5+4, byte3);
}
// delete tag from a specified location
void deleteTag(int startingAddress){
  EEPROM.write( startingAddress*5, byte(0));
  EEPROM.write(startingAddress*5+1, byte(0));
  EEPROM.write(startingAddress*5+2, byte(0));
  EEPROM.write(startingAddress*5+3, byte(0));
  EEPROM.write(startingAddress*5+4, byte(0));
}
// find the first empty entry in the database:
int findEmptyTag(){
  for (int startingAddress = 0; startingAddress< MAX_NO_CARDS; startingAddress++){
    byte value = EEPROM.read(startingAddress*5);
    if (value == byte(0)) {
      return(startingAddress);
    }
  }
  return(200);
}

// print the entire database
void printTags(){
  for (int thisTag = 0; thisTag< MAX_NO_CARDS; thisTag++){
    printOneTag(thisTag);
  }
}

// print a single tag given the tag's address:
void printOneTag(int address) {
  Serial.print(address);
  Serial.print(":");
  for (int offset = 1; offset < 5; offset++) {
    int thisByte = int(EEPROM.read(address*5+offset));
    // if the byte is less than 16, i.e. only one hex character
    // add a leading 0:
    if (thisByte < 0x10) {
      Serial.print("0");
    }
    // print the value:
    Serial.print(thisByte,HEX);
  }
  // add a final linefeed and carriage return:
  Serial.println();
}

//lookup tag in the database:
int lookupTag(byte byte0, byte byte1, byte byte2, byte byte3){
  for (int thisCard = 0; thisCard< MAX_NO_CARDS; thisCard++){
    byte value = EEPROM.read(thisCard*5);
    if (value != byte(0)){                    //it is a valid tag
      //see if all four bytes are the same as the ones we're looking for
      if(byte0 == byte(0) && byte1 == byte(0) && byte2 == byte(0) && byte1 == byte(0)){
        return 200;
      }
      if(byte0 == byte(255) && byte1 == byte(255) && byte2 == byte(255) && byte1 == byte(255)){
        return 200;
      }
      if(byte0 == EEPROM.read(thisCard*5+1) && byte1 == EEPROM.read(thisCard*5+2) && byte2 == EEPROM.read(thisCard*5+3) && byte3 == EEPROM.read(thisCard*5+4)) {
        return(thisCard);
      }
    }
  }
  // if you don't find the tag, return 200;
  return(200);
}

int getTag(){
  if(reader1Count >= 20){
    delay(100);
    long ret = hash(reader1);
    reader1 = 0;
    reader1Count = 0;
    tag[0] = (byte)(ret >> 24);
    tag[1] = (byte)(ret >> 16);
    tag[2] = (byte)(ret >> 8);
    tag[3] = (byte)ret;
    return 1;
  } 
  else {
    return 0;
  }
}

void cycleDoor() {
    digitalWrite(OPEN_DOOR_PIN1, HIGH);
    delay(500);
    digitalWrite(OPEN_DOOR_PIN1, LOW);  
}
void seekNewTag() {
  Serial.println("Next tag will be added to database");
  Serial.println("Waiting for card");
  while(getTag() == 0){
    // wait for tag
    if (millis() - toggleTime > 1000) {
      toggle(waitingLED);
      toggleTime  = millis();
    }
    // unless you get a byte of serial data,
    if (Serial. available()) {
      // break out of the while loop
      // and out of the seekNewTag() method:
      return;
    }
  }

  // look it up in the database:
  int tagToCheck = lookupTag(tag[0], tag[1], tag[2], tag[3]);      

  if (tagToCheck != 200){
    Serial.println("That tag is already stored");
    printOneTag(tagToCheck);
  }
  else {
    int emptyTagLocation = findEmptyTag();
    if (emptyTagLocation != 200){
      writeTag(emptyTagLocation, tag[0], tag[1], tag[2], tag[3]);
      Serial.println("That tag is new, saving...");
      printOneTag(emptyTagLocation);
      blink(successLED, 100, 1);
    }
    else {
      Serial.println("Maximum number of cards stored");
      blink(failureLED, 50, 10);
    }
  }
}


void seekAndDeleteTag() {
  Serial.println("Deleting the next card");
  Serial.println("Waiting for card");
  while(getTag() == 0){
    // do nothing; wait for tag
    // unless you get a byte of serial data,
    if (Serial. available()) {
      // break out of the while loop
      // and out of the method:
      return;
    }
  }
  int tagToDelete = lookupTag(tag[0], tag[1], tag[2], tag[3]);
  if (tagToDelete == 200){
    Serial.print("That tag is not stored");
  }
  else {
    deleteTag(tagToDelete);
  }
}

void toggle(int thisLED) {
  toggleState = !toggleState;
  digitalWrite(thisLED, toggleState);
}

void blink(int thisLED, int interval, int count) {
  for (int i = 0; i < count; i++) {
    digitalWrite(thisLED, LOW);
    delay(interval/2);
    digitalWrite(thisLED, HIGH);
    delay(interval/2);
  }
}

void seekKnownTags() {
  Serial.println("Waiting for card");
  while(getTag() == 0){
    // wait for tag
    if (millis() - toggleTime > 1000) {
      toggle(waitingLED);
      toggleTime  = millis();
    }
    // unless you get a byte of serial data,
    if (Serial.available()) {
      // break out of the while loop
      // and out of the seekKnownTag() method:
      return;
    }
  }

  // look it up in the database:
  int tagToCheck = lookupTag(tag[0], tag[1], tag[2], tag[3]);      

  if (tagToCheck != 200){
    digitalWrite(READER_A_GREEN,LOW);
    cycleDoor();
    printOneTag(tagToCheck);
    Serial.println("OPEN SESAME!!!");
    blink(successLED,500,2);
    digitalWrite(READER_A_GREEN,HIGH);
  }
  else {
    blink(READER_A_GREEN, 50, 10);
    Serial.print("INVALID CARD!!!");
    printOneTag(tagToCheck);
    blink(successLED, 50, 10);
  }
}
long hash( long a)
{
  a = (a+0x7ed55d16) + (a<<12);
  a = (a^0xc761c23c) ^ (a>>19);
  a = (a+0x165667b1) + (a<<5);
  a = (a+0xd3a2646c) ^ (a<<9);
  a = (a+0xfd7046c5) + (a<<3);
  a = (a^0xb55a4f09) ^ (a>>16);
  return a;
}

// Reads serial input if available and parses command when full command has been sent. 
void readSerialInput() {
  int si = serialIndex;
  serialAvail = Serial.available();  
  for (int i = 0; i < serialAvail; i++) {
    char c = Serial.read();
    if(si + i >= BUFFERSIZE) {
      // don't want to overflow the buffer!
      serialIndex = 0;
      for (int i= 0; i < BUFFERSIZE; i++) { 
        inBytes[i] = '\0';
      }
      Serial.flush();
      return;
    }
    //Store into buffer.
    inBytes[i + si] = c;
    //Check for command end. 
    if (inBytes[i + si] == ';') {
      // print the buffer  
      inBytes[i + si] = '\0'; //end of string char
      parseCommand(inBytes); 
      Serial.flush();
      serialIndex = 0;
      for (int i= 0; i < BUFFERSIZE; i++) { 
        inBytes[i] = '\0';
      }
    } 
    else {
      //expecting more of the command to come later.
      serialIndex += 1;
    }
  }  
}

// Cleans and parses the command
void parseCommand(char* com) {
  if (com[0] == '\0') { 
    return; 
  } //bit of error checking
  int start = 0;
  //get start of command
  while (com[start] != '<'){
    start++; 
    if (com[start] == '\0') {
      //its not there. Must be old version
      start = -1;
      break;
    }
  }
  start++;
  //Shift to beginning
  int i = 0;
  while (com[i + start - 1] != '\0') {
    com[i] = com[start + i];
    i++; 
  } 
  performCommand(com);
}

void serialReply(char* sensorname, char* tmpmsg) {
  Serial.print(sensorname);
  Serial.print(":");
  Serial.println(tmpmsg); // Send the message back out the serial line
  //Wait for the serial debugger to shut up
  delay(200); //this is a magic number
  Serial.flush(); //clears all incoming data
}

void performCommand(char* com) {
  if (strcmp(com, "n") == 0) { // store the next tag number
    seekNewTag();
  } 
  else if (strcmp(com,"c") == 0) { // erase entire database 
    Serial.println("i Deleting entire database");
    eraseEEPROM();    // if user enters 'c' then 
    serialReply("i","Database deleted");
  } 
  else if (com[0] == 'd' && com[1] == ' ') { // delete the specified tag number "d 20"
    serialReply("i","Deleting indicated tag from database");
    int value;
    Serial.println(com);
    sscanf(com,"d %d",&value); // Parse the input into multiple values
    Serial.println(value);
    deleteTag(value);
  } 
  else if (strcmp(com,"r") == 0) { // delete the next tag
    seekAndDeleteTag();
  } 
  else if (strcmp(com,"p") == 0) { // print the database
    printTags();
  } 
  else if (strcmp(com,"h") == 0) { // print help
    printHelp();
  }
  else if (strcmp(com,"o") == 0) { //cycle door
    cycleDoor();
  }
  else { 
    Serial.print("e ");
    Serial.println(com); // Echo unknown command back
  }
}