modified
Dependencies: AES CRC_1 UDAES_1 mbed
Fork of AESslave_modified_test_27-9-2017 by
main.cpp@0:f4b9f14b26cc, 2017-09-27 (annotated)
- Committer:
- ajeet3004
- Date:
- Wed Sep 27 10:18:18 2017 +0000
- Revision:
- 0:f4b9f14b26cc
modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajeet3004 | 0:f4b9f14b26cc | 1 | /*************************************************************************************************** PROGRAM FOR CONFIGURATION OF SLAVE DEVICE ******************************************************************************/ |
ajeet3004 | 0:f4b9f14b26cc | 2 | #include "mbed.h" |
ajeet3004 | 0:f4b9f14b26cc | 3 | #include "string.h" |
ajeet3004 | 0:f4b9f14b26cc | 4 | #include "udaes.h" |
ajeet3004 | 0:f4b9f14b26cc | 5 | #include "crc.h" |
ajeet3004 | 0:f4b9f14b26cc | 6 | |
ajeet3004 | 0:f4b9f14b26cc | 7 | #include<sstream> |
ajeet3004 | 0:f4b9f14b26cc | 8 | #include "AES.h" |
ajeet3004 | 0:f4b9f14b26cc | 9 | |
ajeet3004 | 0:f4b9f14b26cc | 10 | Serial pc(USBTX,USBRX); //Serial Configuration for USB serial |
ajeet3004 | 0:f4b9f14b26cc | 11 | Serial device(PA_13,PA_14); //Serial Configuration for Bluetooth |
ajeet3004 | 0:f4b9f14b26cc | 12 | DigitalOut myled(D13); //Indicator to show status of the command coming from gateway |
ajeet3004 | 0:f4b9f14b26cc | 13 | AnalogIn ldr(A0); //Reading from LDR |
ajeet3004 | 0:f4b9f14b26cc | 14 | AnalogIn temp(A1); |
ajeet3004 | 0:f4b9f14b26cc | 15 | int compare_strings(char [], char []); //Function for comparing two strings to compare with the command coming from the gateway |
ajeet3004 | 0:f4b9f14b26cc | 16 | void dataPattern(char [],int); //Function for Dataformat of the data to be sent |
ajeet3004 | 0:f4b9f14b26cc | 17 | void bluetooth(); |
ajeet3004 | 0:f4b9f14b26cc | 18 | int flag,flag1,count=0,w1=0; |
ajeet3004 | 0:f4b9f14b26cc | 19 | char ackmsgon[7]="LED ON"; //Response for the status of the LED |
ajeet3004 | 0:f4b9f14b26cc | 20 | char ackmsgoff[8]="LED OFF"; //Response for the status of the LED |
ajeet3004 | 0:f4b9f14b26cc | 21 | char sendData[21]; //Data to be sent to the gateway with the encrypted format of the response |
ajeet3004 | 0:f4b9f14b26cc | 22 | char message2[18]; //Received formatted messsage from the gateway |
ajeet3004 | 0:f4b9f14b26cc | 23 | char message3[18]; |
ajeet3004 | 0:f4b9f14b26cc | 24 | char encryptedData[16]; //Encrypted message from the gateway |
ajeet3004 | 0:f4b9f14b26cc | 25 | char ldrsensor[9]; //Reading from the LDR |
ajeet3004 | 0:f4b9f14b26cc | 26 | char tempsensor[9]; |
ajeet3004 | 0:f4b9f14b26cc | 27 | char* encrypt; |
ajeet3004 | 0:f4b9f14b26cc | 28 | char* decrypt; |
ajeet3004 | 0:f4b9f14b26cc | 29 | char crc1[3]; |
ajeet3004 | 0:f4b9f14b26cc | 30 | int final[26]; |
ajeet3004 | 0:f4b9f14b26cc | 31 | char dataReceive[30]; |
ajeet3004 | 0:f4b9f14b26cc | 32 | //Main function |
ajeet3004 | 0:f4b9f14b26cc | 33 | int main() |
ajeet3004 | 0:f4b9f14b26cc | 34 | { |
ajeet3004 | 0:f4b9f14b26cc | 35 | |
ajeet3004 | 0:f4b9f14b26cc | 36 | int x,p2=0; |
ajeet3004 | 0:f4b9f14b26cc | 37 | char command_buf[] = {'O','N'}; //Data for the comparision with the command received from the gateway |
ajeet3004 | 0:f4b9f14b26cc | 38 | char command_buf1[] = {'O','F','F'}; //Data for the comaprision with the command received from the gateway |
ajeet3004 | 0:f4b9f14b26cc | 39 | myled = 1; |
ajeet3004 | 0:f4b9f14b26cc | 40 | while(1) { |
ajeet3004 | 0:f4b9f14b26cc | 41 | // count=0; |
ajeet3004 | 0:f4b9f14b26cc | 42 | float ldrout = ldr.read(); //Read the LDR |
ajeet3004 | 0:f4b9f14b26cc | 43 | float tempout = temp.read(); |
ajeet3004 | 0:f4b9f14b26cc | 44 | float tempC = ((tempout*3.3)-0.600)*100.0; |
ajeet3004 | 0:f4b9f14b26cc | 45 | // int err = sensor.readData(); |
ajeet3004 | 0:f4b9f14b26cc | 46 | // pc.printf("\nTemperature is : %f",tempC); |
ajeet3004 | 0:f4b9f14b26cc | 47 | // wait(1); |
ajeet3004 | 0:f4b9f14b26cc | 48 | if(device.readable()) { // Check whether the data is received BLE or not |
ajeet3004 | 0:f4b9f14b26cc | 49 | bluetooth(); //Receive the message and store it in message2 |
ajeet3004 | 0:f4b9f14b26cc | 50 | device.gets(dataReceive,19); |
ajeet3004 | 0:f4b9f14b26cc | 51 | pc.printf("The data received is : %s",dataReceive); //Print the message2 |
ajeet3004 | 0:f4b9f14b26cc | 52 | |
ajeet3004 | 0:f4b9f14b26cc | 53 | |
ajeet3004 | 0:f4b9f14b26cc | 54 | for(x=0; x<19; x++) { |
ajeet3004 | 0:f4b9f14b26cc | 55 | message3[x]=dataReceive[x]; //Extract the exact encrypted data with CRC bytes from message2 and store it in message3 |
ajeet3004 | 0:f4b9f14b26cc | 56 | } |
ajeet3004 | 0:f4b9f14b26cc | 57 | for(x=0; x<30; x++) { |
ajeet3004 | 0:f4b9f14b26cc | 58 | dataReceive[x]='\0'; //Extract the exact encrypted data with CRC bytes from message2 and store it in message3 |
ajeet3004 | 0:f4b9f14b26cc | 59 | } |
ajeet3004 | 0:f4b9f14b26cc | 60 | |
ajeet3004 | 0:f4b9f14b26cc | 61 | int flg = crcCheck_receive(message3); //Do the CRC check for the message3 |
ajeet3004 | 0:f4b9f14b26cc | 62 | pc.printf("\n The flag value is : %d",flg); //Print the status of the flg which the output of the crcCheck_receive |
ajeet3004 | 0:f4b9f14b26cc | 63 | if(flg==0) { //if flg is 0 |
ajeet3004 | 0:f4b9f14b26cc | 64 | pc.printf("\nCRC operation is successful"); //print CRC operation is successful |
ajeet3004 | 0:f4b9f14b26cc | 65 | for(x=0; x<16; x++) { |
ajeet3004 | 0:f4b9f14b26cc | 66 | encryptedData[x]=message3[x]; //Extract the encrypted data from the message3 and store it in encryptedData |
ajeet3004 | 0:f4b9f14b26cc | 67 | } |
ajeet3004 | 0:f4b9f14b26cc | 68 | decrypt = decryptData(encryptedData); //Decrypt the encryptedData and store it in decrypt |
ajeet3004 | 0:f4b9f14b26cc | 69 | flag = compare_strings(decrypt, command_buf); //Compare the decrypted the message for "ON" condition and store the status in flag |
ajeet3004 | 0:f4b9f14b26cc | 70 | flag1 = compare_strings(decrypt, command_buf1); //Compare the decrypted the message for "OFF" condition and store the status in flag1 |
ajeet3004 | 0:f4b9f14b26cc | 71 | |
ajeet3004 | 0:f4b9f14b26cc | 72 | //if message1 is ON |
ajeet3004 | 0:f4b9f14b26cc | 73 | if (flag == 1) { |
ajeet3004 | 0:f4b9f14b26cc | 74 | myled = 0; //Turn ON the LED |
ajeet3004 | 0:f4b9f14b26cc | 75 | // wait_ms(1); //Wait for 1 second |
ajeet3004 | 0:f4b9f14b26cc | 76 | encrypt=encryptData(ackmsgon); |
ajeet3004 | 0:f4b9f14b26cc | 77 | //Encrypt the response |
ajeet3004 | 0:f4b9f14b26cc | 78 | dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway |
ajeet3004 | 0:f4b9f14b26cc | 79 | |
ajeet3004 | 0:f4b9f14b26cc | 80 | } |
ajeet3004 | 0:f4b9f14b26cc | 81 | //if message1 is OFF |
ajeet3004 | 0:f4b9f14b26cc | 82 | else if(flag1==1) { |
ajeet3004 | 0:f4b9f14b26cc | 83 | myled =1; //Turn OFF the LED |
ajeet3004 | 0:f4b9f14b26cc | 84 | // wait_ms(10); //Wait for 1 second |
ajeet3004 | 0:f4b9f14b26cc | 85 | encrypt=encryptData(ackmsgoff); //Encrypt the response |
ajeet3004 | 0:f4b9f14b26cc | 86 | dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway |
ajeet3004 | 0:f4b9f14b26cc | 87 | |
ajeet3004 | 0:f4b9f14b26cc | 88 | } |
ajeet3004 | 0:f4b9f14b26cc | 89 | else if(flag1==1) { |
ajeet3004 | 0:f4b9f14b26cc | 90 | myled =1; //Turn OFF the LED |
ajeet3004 | 0:f4b9f14b26cc | 91 | // wait_ms(10); //Wait for 1 second |
ajeet3004 | 0:f4b9f14b26cc | 92 | encrypt=encryptData(ackmsgoff); //Encrypt the response |
ajeet3004 | 0:f4b9f14b26cc | 93 | dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway |
ajeet3004 | 0:f4b9f14b26cc | 94 | |
ajeet3004 | 0:f4b9f14b26cc | 95 | } |
ajeet3004 | 0:f4b9f14b26cc | 96 | } else { |
ajeet3004 | 0:f4b9f14b26cc | 97 | pc.printf("\nCRC Operation Failed"); |
ajeet3004 | 0:f4b9f14b26cc | 98 | } |
ajeet3004 | 0:f4b9f14b26cc | 99 | } |
ajeet3004 | 0:f4b9f14b26cc | 100 | |
ajeet3004 | 0:f4b9f14b26cc | 101 | //if LDR reading is greater than or equal to 0.200000 |
ajeet3004 | 0:f4b9f14b26cc | 102 | /* else if(ldrout>0.200000) { |
ajeet3004 | 0:f4b9f14b26cc | 103 | //Wait for 1 second |
ajeet3004 | 0:f4b9f14b26cc | 104 | sprintf(ldrsensor,"%f",ldrout); //Convert the float format to string format |
ajeet3004 | 0:f4b9f14b26cc | 105 | ldrsensor[8]='0'; |
ajeet3004 | 0:f4b9f14b26cc | 106 | encrypt=encryptData(ldrsensor); //Encrypt the response |
ajeet3004 | 0:f4b9f14b26cc | 107 | dataPattern(encrypt,100); |
ajeet3004 | 0:f4b9f14b26cc | 108 | memset(ldrsensor,'\0',sizeof(ldrsensor)); //Create the datapattern and combine it with the encrypted LDR reading and send it to the gateway |
ajeet3004 | 0:f4b9f14b26cc | 109 | wait(5); |
ajeet3004 | 0:f4b9f14b26cc | 110 | }*/ |
ajeet3004 | 0:f4b9f14b26cc | 111 | |
ajeet3004 | 0:f4b9f14b26cc | 112 | /*else if(tempC>25.000000) { |
ajeet3004 | 0:f4b9f14b26cc | 113 | //Wait for 1 second |
ajeet3004 | 0:f4b9f14b26cc | 114 | sprintf(tempsensor,"%f",tempout); //Convert the float format to string format |
ajeet3004 | 0:f4b9f14b26cc | 115 | //tempsensor[8]='0'; |
ajeet3004 | 0:f4b9f14b26cc | 116 | encrypt=encryptData(tempsensor); //Encrypt the response |
ajeet3004 | 0:f4b9f14b26cc | 117 | dataPattern(encrypt,100); //Create the datapattern and combine it with the encrypted LDR reading and send it to the gateway |
ajeet3004 | 0:f4b9f14b26cc | 118 | memset(tempsensor,'\0',sizeof(tempsensor)); |
ajeet3004 | 0:f4b9f14b26cc | 119 | wait(5); |
ajeet3004 | 0:f4b9f14b26cc | 120 | }*/ |
ajeet3004 | 0:f4b9f14b26cc | 121 | } |
ajeet3004 | 0:f4b9f14b26cc | 122 | |
ajeet3004 | 0:f4b9f14b26cc | 123 | |
ajeet3004 | 0:f4b9f14b26cc | 124 | |
ajeet3004 | 0:f4b9f14b26cc | 125 | |
ajeet3004 | 0:f4b9f14b26cc | 126 | } |
ajeet3004 | 0:f4b9f14b26cc | 127 | //Function for the data pattern format |
ajeet3004 | 0:f4b9f14b26cc | 128 | void dataPattern(char* encack,int time) |
ajeet3004 | 0:f4b9f14b26cc | 129 | { |
ajeet3004 | 0:f4b9f14b26cc | 130 | // v3=0; |
ajeet3004 | 0:f4b9f14b26cc | 131 | int v2=0; |
ajeet3004 | 0:f4b9f14b26cc | 132 | int w2; |
ajeet3004 | 0:f4b9f14b26cc | 133 | //Adding the start bit[0] |
ajeet3004 | 0:f4b9f14b26cc | 134 | |
ajeet3004 | 0:f4b9f14b26cc | 135 | //Adding the whole encrypted data |
ajeet3004 | 0:f4b9f14b26cc | 136 | /* for(int w=2;w<18;w++) |
ajeet3004 | 0:f4b9f14b26cc | 137 | { |
ajeet3004 | 0:f4b9f14b26cc | 138 | sendData[w]=encack[v3]; |
ajeet3004 | 0:f4b9f14b26cc | 139 | v3++; |
ajeet3004 | 0:f4b9f14b26cc | 140 | }*/ |
ajeet3004 | 0:f4b9f14b26cc | 141 | |
ajeet3004 | 0:f4b9f14b26cc | 142 | |
ajeet3004 | 0:f4b9f14b26cc | 143 | |
ajeet3004 | 0:f4b9f14b26cc | 144 | char* crctr = crcCheck_transmit(encack); //Genrating the CRC bits from the encrypted data and store it in crctr |
ajeet3004 | 0:f4b9f14b26cc | 145 | |
ajeet3004 | 0:f4b9f14b26cc | 146 | |
ajeet3004 | 0:f4b9f14b26cc | 147 | for(w2=0; w2<18; w2++) { |
ajeet3004 | 0:f4b9f14b26cc | 148 | sendData[w2]=crctr[w2]; //Storing the CRC along with the encrypted data in the sendData |
ajeet3004 | 0:f4b9f14b26cc | 149 | |
ajeet3004 | 0:f4b9f14b26cc | 150 | } |
ajeet3004 | 0:f4b9f14b26cc | 151 | pc.printf("\n The Data should be sent is : %s\n",sendData); //Print the whole format of the data combined with encrypted data |
ajeet3004 | 0:f4b9f14b26cc | 152 | |
ajeet3004 | 0:f4b9f14b26cc | 153 | |
ajeet3004 | 0:f4b9f14b26cc | 154 | //For loop for sending the whole data via BLE |
ajeet3004 | 0:f4b9f14b26cc | 155 | for(int k=0; k<19; k++) { |
ajeet3004 | 0:f4b9f14b26cc | 156 | wait_ms(time); //Wait for 20 milisecond |
ajeet3004 | 0:f4b9f14b26cc | 157 | pc.putc(sendData[k]); //Print the character |
ajeet3004 | 0:f4b9f14b26cc | 158 | device.putc(sendData[k]); //Send the character via BLE |
ajeet3004 | 0:f4b9f14b26cc | 159 | } |
ajeet3004 | 0:f4b9f14b26cc | 160 | |
ajeet3004 | 0:f4b9f14b26cc | 161 | } |
ajeet3004 | 0:f4b9f14b26cc | 162 | void bluetooth() //bluetooth function for reading data from slave device |
ajeet3004 | 0:f4b9f14b26cc | 163 | { |
ajeet3004 | 0:f4b9f14b26cc | 164 | int z=0; |
ajeet3004 | 0:f4b9f14b26cc | 165 | while(1) { |
ajeet3004 | 0:f4b9f14b26cc | 166 | if(device.readable()) { |
ajeet3004 | 0:f4b9f14b26cc | 167 | char c=device.getc(); |
ajeet3004 | 0:f4b9f14b26cc | 168 | if(c=='\0') { // if char is null then return to main function where it was called |
ajeet3004 | 0:f4b9f14b26cc | 169 | z=0; |
ajeet3004 | 0:f4b9f14b26cc | 170 | return; |
ajeet3004 | 0:f4b9f14b26cc | 171 | } else { |
ajeet3004 | 0:f4b9f14b26cc | 172 | dataReceive[z]=c; // |
ajeet3004 | 0:f4b9f14b26cc | 173 | printf("%c",c); |
ajeet3004 | 0:f4b9f14b26cc | 174 | z++; |
ajeet3004 | 0:f4b9f14b26cc | 175 | } |
ajeet3004 | 0:f4b9f14b26cc | 176 | } |
ajeet3004 | 0:f4b9f14b26cc | 177 | } |
ajeet3004 | 0:f4b9f14b26cc | 178 | } |
ajeet3004 | 0:f4b9f14b26cc | 179 | //Function to compare two strings |
ajeet3004 | 0:f4b9f14b26cc | 180 | int compare_strings(char a[], char b[]) |
ajeet3004 | 0:f4b9f14b26cc | 181 | { |
ajeet3004 | 0:f4b9f14b26cc | 182 | int c = 0; //Initial count=0 |
ajeet3004 | 0:f4b9f14b26cc | 183 | int n=strlen(decrypt); //Size of the decrypted message |
ajeet3004 | 0:f4b9f14b26cc | 184 | |
ajeet3004 | 0:f4b9f14b26cc | 185 | //Run the loop till the size is less than n |
ajeet3004 | 0:f4b9f14b26cc | 186 | for(int s=0; s<n; s++) { |
ajeet3004 | 0:f4b9f14b26cc | 187 | if(a[s] == b[s]) //if both the data is matched in the respective location |
ajeet3004 | 0:f4b9f14b26cc | 188 | c++; //Increment c |
ajeet3004 | 0:f4b9f14b26cc | 189 | else //if both the data is not matched in the respective location |
ajeet3004 | 0:f4b9f14b26cc | 190 | break; //Break the loop |
ajeet3004 | 0:f4b9f14b26cc | 191 | } |
ajeet3004 | 0:f4b9f14b26cc | 192 | |
ajeet3004 | 0:f4b9f14b26cc | 193 | |
ajeet3004 | 0:f4b9f14b26cc | 194 | if(c==n) { //If c is equal to n |
ajeet3004 | 0:f4b9f14b26cc | 195 | return 1; //return 1 |
ajeet3004 | 0:f4b9f14b26cc | 196 | } else //If c is not equal to n |
ajeet3004 | 0:f4b9f14b26cc | 197 | return 0; //Return 0 |
ajeet3004 | 0:f4b9f14b26cc | 198 | } |