From 56e70715efc789bbb304381946a460ca6b7c5fdf Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sun, 27 Nov 2011 19:52:45 -0700 Subject: [PATCH] Removed everything but bare minimum functions --- Open_Access_Control/Open_Access_Control.pde | 936 ++------------------ 1 file changed, 57 insertions(+), 879 deletions(-) diff --git a/Open_Access_Control/Open_Access_Control.pde b/Open_Access_Control/Open_Access_Control.pde index dcbf367..75d8d56 100644 --- a/Open_Access_Control/Open_Access_Control.pde +++ b/Open_Access_Control/Open_Access_Control.pde @@ -200,7 +200,11 @@ const prog_uchar statusMessage5[] PROGMEM = {"Door 1 unlocked state(1=l const prog_uchar statusMessage6[] PROGMEM = {"Door 2 unlocked state(1=locked):"}; - +// strings for storing results from web server +String httpresponse = ""; +String username = ""; +bool authorized = false; +bool relay1engaged = false; void setup(){ // Runs once at Arduino boot-up @@ -242,7 +246,7 @@ void setup(){ // Runs once at Arduino boot-up Serial.begin(57600); // Set up Serial output at 8,N,1,57600bps - logReboot(); + // start the Ethernet connection: @@ -253,22 +257,21 @@ void setup(){ // Runs once at Arduino boot-up //delay(1000); - chirpAlarm(1); // Chirp the alarm to show system ready. + // hardwareTest(100); // IO Pin testing routing (use to check your inputs with hi/lo +(5-12V) sources) // Also checks relays - // if there are incoming bytes available - // from the server, read them and print them: - String httpresponse = ""; - String username = ""; } void loop() // Main branch, runs over and over again -{ +{ + ////////////////////////// + // Reader input/authentication section + ////////////////////////// if(reader1Count >= 26) { // When tag presented to reader1 (No keypad on this reader) - logTagPresent(reader1,1); // write log entry to serial port + Serial.println("connecting..."); @@ -286,6 +289,11 @@ void loop() // Main branch, runs over and ov client.print(reader1, HEX); client.println(" HTTP/1.0"); client.println(); + + // reset values coming from http + httpresponse = ""; + username = ""; + authorized = false; } else { @@ -295,265 +303,68 @@ void loop() // Main branch, runs over and ov wiegand26.initReaderOne(); // Reset for next tag scan - httpresponse = ""; - username = ""; } while (client.available()) { - httpresponse += (char)client.read(); + char thisChar = client.read(); + // only fill up httpresponse with data after a ^ sign. + if (httpresponse.charAt(0) == '^' || thisChar == '^') { + httpresponse += thisChar; + } } - if(httpresponse.length()>0) { + if(!client.available() && httpresponse.length()>0) { Serial.println("Response: "); - int c = httpresponse.indexOf('AUTH:'); - Serial.println(httpresponse.substring(c+1)); + Serial.println(httpresponse); + int c = httpresponse.indexOf('^'); + int d = httpresponse.indexOf('|'); + int e = httpresponse.indexOf('$'); + + Serial.print("IndexOf:"); + Serial.println(c); + Serial.println(d); + + Serial.println("SubStr:"); + Serial.println(httpresponse.substring(c+1,d)); - username = httpresponse.substring(c+1); - httpresponse = ""; + username = httpresponse.substring(c+1,d); Serial.print("User: "); Serial.println(username); + Serial.println("SubStr:"); + Serial.println(httpresponse.substring(d+1,e)); + + if(httpresponse.substring(d+1,e) == "OK") { + authorized = true; + } + + Serial.print("Auth: "); + Serial.println(authorized); + + Serial.println("End Response"); + httpresponse = ""; } // if the server's disconnected, stop the client: if (!client.connected()) { client.stop(); } + + ////////////////////////// + // Normal operation section + ////////////////////////// + + if(authorized) { + + } + } // End of loop() -void runCommand(long command) { // Run any commands entered at the pin pad. - - switch(command) { - - - case 0x1: - { // If command = 1, deactivate alarm - alarmState(0); // Set global alarm level variable - armAlarm(0); - chirpAlarm(1); - break; - } - - case 0x2: - { // If command =2, activate alarm with delay. - - doorUnlock(1); // Set global alarm level variable - door1Locked=false; - doorClosed=false; // 200 chirps = ~30 seconds delay - - if((pollAlarm(3) == 0) && (pollAlarm(2) == 0)) { // Do not arm the alarm if doors are open - - for(byte i=0; i<30; i++) { - if((pollAlarm(3) !=0) && doorClosed==false) { // Set door to be unlocked until alarm timeout or user exits - lockall(); - doorClosed=true; - } - digitalWrite(ALARMSTROBEPIN, HIGH); - delay(500); - digitalWrite(ALARMSTROBEPIN, LOW); - delay(500); - } - chirpAlarm(2); - armAlarm(1); - lockall(); // Lock all doors on exit - } - else { // Beep the alarm once and exit if attempt made to arm alarm with doors open - digitalWrite(ALARMSTROBEPIN, HIGH); - delay(500); - digitalWrite(ALARMSTROBEPIN, LOW); - delay(500); - lockall(); // Lock all doors anyway - } - break; - } - - case 0x3: - { - - doorLock(1); // Set door 2 to stay unlocked, and door 1 to be locked - doorUnlock(2); - door1Locked=true; - door2Locked=false; - chirpAlarm(3); - break; - } - - case 0x4: // Set doors to remain open - { - armAlarm(4); - doorUnlock(1); - doorUnlock(2); - door1Locked=false; - door2Locked=false; - chirpAlarm(4); - break; - } - case 0x5: // Relock all doors - { - lockall(); - chirpAlarm(5); - break; - } - - case 0x911: - { - chirpAlarm(9); // Emergency - armAlarm(1); - alarmState(1); - break; - } - - case 0x20: - { // If command = 20, do nothing - break; - } - default: - { - break; - } - } - - -} - - -/* Alarm System Functions - Modify these as needed for your application. - Sensor zones may be polled with digital or analog pins. Unique reader2 - resistors can be used to check more zones from the analog pins. - */ - -void alarmState(byte alarmLevel) { //Changes the alarm status based on this flow - - logalarmState(alarmLevel); - switch (alarmLevel) { - case 0: - { // If alarmLevel == 0 turn off alarm. - digitalWrite(ALARMSIRENPIN, LOW); - digitalWrite(ALARMSTROBEPIN, LOW); - alarmActivated = alarmLevel; //Set global alarm level variable - break; - } - case 1: - { - digitalWrite(ALARMSIRENPIN, HIGH); // If alarmLevel == 1 turn on strobe lights and siren - // digitalWrite(ALARMSTROBEPIN, HIGH); // Optionally activate yoru strobe/chome - alarmSirenTimer=millis(); - alarmActivated = alarmLevel; //Set global alarm level variable - logalarmTriggered(); - - break; - } - - case 2: - { - digitalWrite(ALARMSTROBEPIN, HIGH); - alarmActivated = alarmLevel; - break; - } - - case 3: - { - - alarmActivated = alarmLevel; - break; - } - /* - case 4: { - vaporize_intruders(STUN); - break; - } - - case 5: { - vaporize_intruders(MAIM); - } etc. etc. etc. - break; - */ - - default: - { // Exceptional cases kill alarm outputs - digitalWrite(ALARMSIRENPIN, LOW); // Turn off siren and strobe - // digitalWrite(ALARMSTROBEPIN, LOW); - break; - } - - - - - } - - if(alarmActivated != EEPROM.read(EEPROM_ALARM)){ // Update eeprom value - EEPROM.write(EEPROM_ALARM,alarmActivated); - } - -} //End of alarmState() - -void chirpAlarm(byte chirps){ // Chirp the siren pin or strobe to indicate events. - for(byte i=0; iSENSORTHRESHOLD){ - return 1; - - } - else return 0; -} - -void trainAlarm(){ // Train the system about the default states of the alarm pins. - armAlarm(0); // Disarm alarm first - alarmState(0); - - int temp[5]={0}; - int avg; - - for(int i=0; i NUMUSERS)) { // Do not write to invalid EEPROM addresses. - - Serial.print("Invalid user modify attempted."); - } - else - { - - - - - EEPROM_buffer[0] = byte(tagNumber & 0xFFF); // Fill the buffer with the values to write to bytes 0..4 - EEPROM_buffer[1] = byte(tagNumber >> 8); - EEPROM_buffer[2] = byte(tagNumber >> 16); - EEPROM_buffer[3] = byte(tagNumber >> 24); - EEPROM_buffer[4] = byte(userMask); - - - - for(int i=0; i<5; i++){ - EEPROM.write((offset+i), (EEPROM_buffer[i])); // Store the resulting value in 5 bytes of EEPROM. - - } - - Serial.print("User "); - Serial.print(userNum,DEC); - Serial.println(" successfully modified"); - - - } -} - -void deleteUser(int userNum) // Deletes a user from the local database. -{ // Users number 0..NUMUSERS - int offset = (EEPROM_FIRSTUSER+(userNum*5)); // Find the offset to write this user to - - logDate(); - - if((userNum <0) || (userNum > NUMUSERS)) { // Do not write to invalid EEPROM addresses. - - Serial.print("Invalid user delete attempted."); - } - else - { - - - - for(int i=0; i<5; i++){ - EEPROM.write((offset+i), 0xFF); // Store the resulting value in 5 bytes of EEPROM. - // Starting at offset. - - - - } - - Serial.print("User deleted at position "); - Serial.println(userNum); - - } - -} - - - -int checkUser(unsigned long tagNumber) // Check if a particular tag exists in the local database. Returns userMask if found. -{ // Users number 0..NUMUSERS - // Find the first offset to check - - unsigned long EEPROM_buffer=0; // Buffer for recreating tagNumber from the 4 stored bytes. - int found=-1; - - logDate(); - - - for(int i=EEPROM_FIRSTUSER; i<=(EEPROM_LASTUSER-5); i=i+5){ - - - EEPROM_buffer=0; - EEPROM_buffer=(EEPROM.read(i+3)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i+2)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i+1)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i)); - - - if((EEPROM_buffer == tagNumber) && (tagNumber !=0xFFFFFFFF) && (tagNumber !=0x0)) { // Return a not found on blank (0xFFFFFFFF) entries - Serial.print("User "); - Serial.print(((i-EEPROM_FIRSTUSER)/5),DEC); - Serial.println(" authenticated."); - found = EEPROM.read(i+4); - return found; - } - - } - Serial.println("User not found"); - delay(1000); // Delay to prevent brute-force attacks on reader - return found; -} - - - - - -void dumpUser(byte usernum) // Return information ona particular entry in the local DB -{ // Users number 0..NUMUSERS - - - unsigned long EEPROM_buffer=0; // Buffer for recreating tagNumber from the 4 stored bytes. - - - if((0<=usernum) && (usernum <=199)){ - - int i=usernum*5+EEPROM_FIRSTUSER; - - EEPROM_buffer=0; - EEPROM_buffer=(EEPROM.read(i+3)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i+2)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i+1)); - EEPROM_buffer= EEPROM_buffer<<8; - EEPROM_buffer=(EEPROM_buffer ^ EEPROM.read(i)); - - - - Serial.print(((i-EEPROM_FIRSTUSER)/5),DEC); - Serial.print("\t"); - Serial.print(EEPROM.read(i+4),DEC); - Serial.print("\t"); - - if(DEBUG==2){ - Serial.println(EEPROM_buffer,HEX); - } - else { - if(EEPROM_buffer != 0xFFFFFFFF) { - Serial.println("********"); - } - } - - } - else Serial.println("Bad user number!"); -} - - -/* Displays a serial terminal menu system for - * user management and other tasks - */ - -void readCommand() { - - -byte stringSize=(sizeof(inString)/sizeof(char)); -char cmdString[4][9]; // Size of commands (4=number of items to parse, 10 = max length of each) - - -byte j=0; // Counters -byte k=0; -char cmd=0; - - -char ch; - - if (Serial.available()) { // Check if user entered a command this round - ch = Serial.read(); - if( ch == '\r' || inCount >=stringSize-1) { // Check if this is the terminating carriage return - inString[inCount] = 0; - inCount=0; - } - else{ - (inString[inCount++] = ch); } - //Serial.print(ch); // Turns echo on or off - - -if(inCount==0) { - for(byte i=0; i=5) && (millis()-consolefailTimer<300000)) // Do not allow priv mode if more than 5 failed logins in 5 minute - { - PROGMEMprintln(privsAttemptsMessage); - break; - } - if (strtoul(cmdString[1],NULL,16) == PRIVPASSWORD) - { - consoleFail=0; - PROGMEMprintln(privsenabledMessage); - privmodeEnabled=true; - } - else { - PROGMEMprintln(privsdisabledMessage); - privmodeEnabled=false; - if(consoleFail==0) { // Set the timeout for failed logins - consolefailTimer=millis(); - } - consoleFail++; // Increment the login failure counter - } - - break; - - } - -//privmodeEnabled=true; //Debugging statement - - - case 'a': { // List whole user database - if(privmodeEnabled==true) { - logDate(); - Serial.println("User dump started."); - Serial.print("UserNum:"); - Serial.print(" "); - Serial.print("Usermask:"); - Serial.print(" "); - Serial.println("TagNum:"); - - for(int i=0; i<(NUMUSERS); i++){ - dumpUser(i); - //Serial.println(); // commented out due to dumpUser now always printing a line -WB 7-7-2011 - } - } - else{logprivFail();} - break; - } - - case 's': { // List user - if(privmodeEnabled==true) { - Serial.print("UserNum:"); - Serial.print(" "); - Serial.print("Usermask:"); - Serial.print(" "); - Serial.println("TagNum:"); - dumpUser(atoi(cmdString[1])); - //Serial.println(); // commented out due to dumpUser always printing a line -WB 7-7-2011 - } - else{logprivFail();} - break; - } - - case 'd': { // Display current time - logDate(); - Serial.println(); - break; - } - - case '1': { // Deactivate alarm - if(privmodeEnabled==true) { - armAlarm(0); - alarmState(0); - chirpAlarm(1); - } - else{logprivFail();} - break; - } - case '2': { // Activate alarm with delay. - chirpAlarm(20); // 200 chirps = ~30 seconds delay - armAlarm(1); - break; - } - - case 'u': { - if(privmodeEnabled==true) { - alarmState(0); // Set to door chime only/open doors - armAlarm(4); - doorUnlock(1); - doorUnlock(2); - door1Locked=false; - door2Locked=false; - chirpAlarm(3); - } - - else{logprivFail();} - break; - } - case 'l': { // Lock all doors - lockall(); - chirpAlarm(1); - break; - } - - case '3': { // Train alarm sensors - if(privmodeEnabled==true) { - trainAlarm(); - } - else{logprivFail();} - break; - } - case '9': { // Show site status - PROGMEMprint(statusMessage1); - Serial.println(alarmArmed,DEC); - PROGMEMprint(statusMessage2); - Serial.println(alarmActivated,DEC); - PROGMEMprint(statusMessage3); - Serial.println(pollAlarm(3),DEC); - PROGMEMprint(statusMessage4); - Serial.println(pollAlarm(2),DEC); - PROGMEMprint(statusMessage5); - Serial.println(door1Locked); - PROGMEMprint(statusMessage6); - Serial.println(door2Locked); - break; - } - - case 'o': { - if(privmodeEnabled==true) { - if(atoi(cmdString[1]) == 1){ - alarmState(0); // Set to door chime only/open doors - armAlarm(4); - doorUnlock(1); // Open the door specified - door1locktimer=millis(); - break; - } - if(atoi(cmdString[1]) == 2){ - alarmState(0); // Set to door chime only/open doors - armAlarm(4); - doorUnlock(2); - door2locktimer=millis(); - break; - } - Serial.print("Invalid door number!"); - } - - else{logprivFail();} - break; - } - - case 'r': { // Remove a user - if(privmodeEnabled==true) { - dumpUser(atoi(cmdString[1])); - deleteUser(atoi(cmdString[1])); - } - else{logprivFail();} - break; - } - - case 'm': { // Add/change a user - if(privmodeEnabled==true) { - dumpUser(atoi(cmdString[1])); - addUser(atoi(cmdString[1]), atoi(cmdString[2]), strtoul(cmdString[3],NULL,16)); - dumpUser(atoi(cmdString[1])); - } - else{logprivFail();} - - break; - } - - case '?': { // Display help menu - PROGMEMprintln(consolehelpMessage1); - PROGMEMprintln(consolehelpMessage2); - PROGMEMprintln(consolehelpMessage3); - PROGMEMprintln(consolehelpMessage4); - PROGMEMprintln(consolehelpMessage5); - PROGMEMprintln(consolehelpMessage6); - break; - } - - default: - PROGMEMprintln(consoledefaultMessage); - break; - } - - } // End of 'if' statement for Serial.available - } // End of 'if' for string finished -} // End of function - - /* Wrapper functions for interrupt attachment