security Sys

Dependencies:   4DGL-uLCD-SE ECE4180_Touchpad_V2 IoTsecuritySys PinDetect mbed-rtos mbed

Fork of ECE4180_Touchpad_V2 by Jacob Smith

Files at this revision

API Documentation at this revision

Comitter:
landes
Date:
Tue Dec 08 21:32:24 2015 +0000
Parent:
7:758d4e6fc145
Commit message:
123

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 758d4e6fc145 -r 429fa05b4952 main.cpp
--- a/main.cpp	Tue Dec 08 19:39:21 2015 +0000
+++ b/main.cpp	Tue Dec 08 21:32:24 2015 +0000
@@ -71,7 +71,7 @@
 volatile int codeCounter;
 // inputCode array is the sequence of numbers the user will enter
 volatile int inputCode[CODE_LENGTH];
-volatile bool code_enabled;
+//volatile bool code_enabled;
 volatile float IrVoltage = 0.0;
 int FindStrLocation(string sntsc, string word, string ptr);
 volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
@@ -83,6 +83,7 @@
 
 Mutex LCD_Access; 
 Mutex PC_Access;
+Mutex Eth_Lock;
 Semaphore Consul_Access(5);
 
 void Shiftbright_thread(void const *args);
@@ -132,11 +133,15 @@
         }
         break;
         case Alarm_ON:
+            if(key_code == 12 ){
+                state = Armed;
+                Ethernet_massage_Send("UpdateStatus");
+            }
         break; 
         case Cleared:
             if(key_code == 12 ){
                 state = Armed;
-                //Ethernet_massage_Send("UpdateStatus");
+                Ethernet_massage_Send("UpdateStatus");
                 }
             else if (key_code == 11 ){
                 doorlock = 1;  
@@ -151,19 +156,19 @@
 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
     //int i = 0;
     // only care about the even numbers (see lines 6-10)
-    //////////PC_Access.lock();
+    //PC_Access.lock();
     //pc.printf("NEW CODE: ");
     //PC_Access.unlock();
     for(int i = 0; i < CODE_LENGTH; i+=2){
         srand(t1.read_us());
         codeArray[i] = (rand() % 9)+1; //nake code only 1-9
-        //////////PC_Access.lock();
+        PC_Access.lock();
         pc.printf("%d,  ", codeArray[i]);
-        //PC_Access.unlock();
+        PC_Access.unlock();
     }
 
     snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]);
-    //////////PC_Access.lock();
+    //PC_Access.lock();
     //pc.printf("\n\r");
     //pc.printf("%s\n\r",charCode,codeArray[6]);
     //PC_Access.unlock();
@@ -186,28 +191,31 @@
          
     }
     if(j == CODE_LENGTH/2){
-        for(int i = 0; i < CODE_LENGTH; i+=2){
-            inputCode[i] =0;
-        }
+        /*for(int i = 0; i < CODE_LENGTH; i+=2){
+            //inputCode[i] =0;
+        }*/
         return(true);
     }
     else if  (Ethernet_massage_Send("GetTempCode")){
-        for(int i = 0; i < CODE_LENGTH; i+=2){
-             inputCode[i] =0;
-        }
+        /*for(int i = 0; i < CODE_LENGTH; i+=2){
+             //inputCode[i] =0;
+        }*/
         //pc.printf("return true");
         return(true);
     }
     else {
-        for(int i = 0; i < CODE_LENGTH; i+=2){
-            inputCode[i] =0;
-        }
+        /*for(int i = 0; i < CODE_LENGTH; i+=2){
+            //inputCode[i] =0;
+        }*/
        return(false);
     }
     }
     else {
-        pc.printf("3 times ");
+        //PC_Access.lock();
+        //pc.printf("3 times ");
+        //PC_Access.unlock();
         state = Alarm_ON;
+        Ethernet_massage_Send("UpdateStatus");
         return(false);
     }
 }
@@ -230,7 +238,7 @@
     int new_code_timer = 0;
     int new_code_counter = 0;
     // this tells the state machine with phase of authentication we are in
-    code_enabled = false;
+    //code_enabled = false;
     
     for(int i = 0; i < CODE_LENGTH; i++){
       authenticator[i] = 0;
@@ -241,13 +249,16 @@
     // the seed will always be random, unlike the predecessor version
     t1.start();
     init_LCD();
+    
+    Timer t;
+    t.start(); 
     //start threads:
-    //////////PC_Access.lock();
+    PC_Access.lock();
     pc.printf("\n\n\nSetting up Ethernet\n\r");
-    //PC_Access.unlock();
+    PC_Access.unlock();
     Thread Ethernetthread(Ethernet_thread);
     wait(5); //Give the Ethernet connection some time to set up 
-    Thread IRthread(IR_thread);
+    //Thread IRthread(IR_thread);
     Thread Shiftbright(Shiftbright_thread);
     Thread LCDthread(uLCD_thread);
     //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); 
@@ -255,21 +266,67 @@
     
     // while loop constantly checks if the entered code sequence is right or wrong,
     // given that the correct amount of numbers were entered
-    ////////////PC_Access.lock();
+    PC_Access.lock();
     pc.printf("Ready\n\r");
-    ////PC_Access.unlock();
+    PC_Access.unlock();
     doorlock = 0; // make sure locked 
     while (1){
         switch(state){
+            case Armed:
+            
+                
+                
+                
+                
+                
+                
+                
+                
+
+    
+   // while(1) {
+        
+        if (state == Armed) {
+            IrVoltage=IrSensor.read();
+            if (IrVoltage <= 0.1) { //if value just nois reset timer
+                t.reset();
+                state = Armed;
+                //Ethernet_massage_Send("UpdateStatus");
+                }
+            if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone 
+                state = IR_sensed; 
+                Ethernet_massage_Send("UpdateStatus");
+                }
+        //    Thread::wait(2000);
+        }
+        else {
+            //nothing to do for this thread make space for others
+      //      Thread::wait(2000);
+        }
+    //}
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                break;
             case Cleared:
                 break;
             case IR_sensed:
                 if(codeCounter >= CODE_LENGTH){
                     pass = check_code_sequence(passcode);
                     if(pass == true){
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                         //pc.printf("SENDING AUTHENTICATION CODE...\n\r");
-                        ////////PC_Access.unlock();
+                        //PC_Access.unlock();
                         generate_random_code(authenticator);
                         t1.stop(); // reset the time
                         t1.reset(); // so that it is an even
@@ -278,11 +335,12 @@
                         //code_enabled = true;
                         state = Second_Step;
                         Ethernet_massage_Send("UpdateStatus");
+                        Ethernet_massage_Send("TextCode");
                     }
                     else{
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                         //pc.printf("WRONG passcode\n\r");
-                        ////PC_Access.unlock();
+                        //PC_Access.unlock();
                         codeCounter = 0;
                     }
                 }
@@ -292,25 +350,26 @@
                     NumTry = 0;
                     pass = check_code_sequence(authenticator);
                     if(pass == true){
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                         //pc.printf("ACCESS GRANTED\n\r");
-                        ////PC_Access.unlock();
-                        doorlock = 1;  
-                        flipper.attach(&Activate_Lock, 5.0);
+                        //PC_Access.unlock();
+
                         //wait(5);
                         //doorlock = 0;*/
                         //pc.printf("Resetting....\n\r");
                         //pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
                         codeCounter = 0;
-                        code_enabled = false;
+                        //code_enabled = false;
                         state = Cleared;
                         Ethernet_massage_Send("TextCode");
                         Ethernet_massage_Send("UpdateStatus");
+                        doorlock = 1;  
+                        flipper.attach(&Activate_Lock, 5.0);
                     }
                     else{
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                         //pc.printf("ACCESS DENIED\n\r");
-                        ////PC_Access.unlock();
+                        //PC_Access.unlock();
                         codeCounter = 0;
                     }
                 }
@@ -345,6 +404,7 @@
     while(1) {
     switch (state) {
         case Armed:
+            
             for (int i = 0; i <= 50; i++) {
                 RGB_LED( i, 0, 0);
                 Thread::wait(10);
@@ -360,6 +420,8 @@
             RGB_LED( 0, 0, 0);
         break;
         case Alarm_ON:
+            mySpeaker.PlaySong(note,duration);
+        //Thread::wait(2000); 
             for (int i = 0; i <= 100; i++) {
                 RGB_LED( i, i/2, 0);
                 Thread::wait(10);
@@ -392,15 +454,15 @@
                 state = Armed;
                 Ethernet_massage_Send("UpdateStatus");
                 }
-            if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone 
+            if (t.read() >= 3) { //wait 5 seconds to make sure that sense someone 
                 state = IR_sensed; 
                 Ethernet_massage_Send("UpdateStatus");
                 }
-            Thread::wait(1000);
+            Thread::wait(2000);
         }
         else {
             //nothing to do for this thread make space for others
-            Thread::wait(1000);
+            Thread::wait(2000);
         }
     }
 }
@@ -463,6 +525,8 @@
                             LCD_Access.lock();
                             uLCD.text_width(4);
                             uLCD.text_height(4);
+                            LCD_Access.unlock();
+                            LCD_Access.lock();
                             uLCD.color(RED);
                             uLCD.locate(1,2);
                             uLCD.printf("%2D",i);                    
@@ -471,6 +535,9 @@
                             }
                         }
                         if (state == IR_sensed) {
+                            LCD_Access.lock();
+                            uLCD.cls();
+                            LCD_Access.unlock();
                             state = Alarm_ON;
                             Ethernet_massage_Send("UpdateStatus");
                             Ethernet_massage_Send("TextAlarm");
@@ -552,7 +619,7 @@
     while (1) {
     if (state == Alarm_ON) {
         mySpeaker.PlaySong(note,duration);
-        Thread::wait(1000); 
+        Thread::wait(2000); 
         }
     }
 }
@@ -573,20 +640,23 @@
         TCPSocketConnection sock;
         sock.connect("dreamphysix.com", 80);
         char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n";
-    ////////////PC_Access.lock();
+    //PC_Access.lock();
     //pc.printf("%s",http_cmd);
-    ////PC_Access.unlock();
+    //PC_Access.unlock();
+    Eth_Lock.lock();
     sock.send_all(http_cmd, sizeof(http_cmd)-1);
-    
+    Eth_Lock.unlock();
                         while (true) {
+                        Eth_Lock.lock();
                         ret = sock.receive(buffer, sizeof(buffer)-1);
+                        Eth_Lock.unlock();
                         if (ret <= 0)
                             break;
                         buffer[ret] = '\0';
                         Consul_Access.wait();
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                         //pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
-                        ////PC_Access.unlock();
+                        //PC_Access.unlock();
                         Consul_Access.release();
                         }
                     sock.close();
@@ -601,7 +671,9 @@
     //dummy = atoi(dummy);
     state = (Statetype)dummy;
     //state = (Statetype)buffer[found+7];
+    PC_Access.lock();
     pc.printf("\n\state: %i\n\r",dummy);
+    PC_Access.unlock();
     //Thread::wait(3000); 
     
 
@@ -621,24 +693,33 @@
                             char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode=";
                             strcat(http_cmd, charCode);
                             strcat(http_cmd, " HTTP/1.0\n\n");
-                            //////////////////////////PC_Access.lock();
-                            //pc.printf("%s",http_cmd);
-                            ////PC_Access.unlock();
+                            PC_Access.lock();
+                            pc.printf("%s",http_cmd);
+                            PC_Access.unlock();
+                            Eth_Lock.lock();
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                            Eth_Lock.unlock();
                     }
                     else if (buff == "TextAlarm") {
                             char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
+                            Eth_Lock.lock();
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                            Eth_Lock.unlock();
+                            PC_Access.lock();
+                            pc.printf("%s",http_cmd);
+                            PC_Access.unlock();
                    }
                     else if (buff == "GetTempCode") {
                             snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]);
                             char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code=";
                             strcat(http_cmd, charCode);
                             strcat(http_cmd, " HTTP/1.0\n\n");
-                            //////////////////////////PC_Access.lock();
-                            //pc.printf("%s",http_cmd);
-                            ////PC_Access.unlock();
+                            PC_Access.lock();
+                            pc.printf("%s",http_cmd);
+                            PC_Access.unlock();
+                            Eth_Lock.lock();
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                            Eth_Lock.unlock();
                     }
                     else if (buff == "UpdateStatus") {
                             char tempStatus[2];
@@ -648,10 +729,12 @@
                             strcat(http_cmd, "&status=");
                             strcat(http_cmd, tempStatus);
                             strcat(http_cmd, " HTTP/1.0\n\n");
-                            //////////////////////////PC_Access.lock();
-                            //pc.printf("%s",http_cmd);
-                            ////PC_Access.unlock();
+                            PC_Access.lock();
+                            pc.printf("%s",http_cmd);
+                            PC_Access.unlock();
+                            Eth_Lock.lock();
                             sock.send_all(http_cmd, sizeof(http_cmd)-1);
+                            Eth_Lock.unlock();
                     }
                     else {
                         
@@ -659,14 +742,16 @@
 
 
                     while (true) {
+                        Eth_Lock.lock();
                         ret = sock.receive(buffer, sizeof(buffer)-1);
+                        Eth_Lock.unlock();
                         if (ret <= 0)
                             break;
                         buffer[ret] = '\0';
                         Consul_Access.wait();
-                        ////////////////////////PC_Access.lock();
+                        //PC_Access.lock();
                        //pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
-                        ////PC_Access.unlock();
+                        //PC_Access.unlock();
                         Consul_Access.release();
                         }
                     sock.close();
@@ -683,8 +768,9 @@
                     }
                     else {
                         //pc.printf("default ");
-                        return false;
+                        return true;
                     }
+    Thread::wait(1000); 
 }
 
 void Activate_Lock(){