modified
Dependencies: AES CRC_1 UDAES_1 mbed
main.cpp
00001 /*************************************************************************************************** PROGRAM FOR CONFIGURATION OF SLAVE DEVICE ******************************************************************************/ 00002 #include "mbed.h" 00003 #include "string.h" 00004 #include "udaes.h" 00005 #include "crc.h" 00006 00007 #include<sstream> 00008 #include "AES.h" 00009 00010 Serial pc(USBTX,USBRX); //Serial Configuration for USB serial 00011 Serial device(PA_13,PA_14); //Serial Configuration for Bluetooth 00012 DigitalOut myled(D13); //Indicator to show status of the command coming from gateway 00013 AnalogIn ldr(A0); //Reading from LDR 00014 AnalogIn temp(A1); 00015 int compare_strings(char [], char []); //Function for comparing two strings to compare with the command coming from the gateway 00016 void dataPattern(char [],int); //Function for Dataformat of the data to be sent 00017 void bluetooth(); 00018 int flag,flag1,count=0,w1=0; 00019 char ackmsgon[7]="LED ON"; //Response for the status of the LED 00020 char ackmsgoff[8]="LED OFF"; //Response for the status of the LED 00021 char sendData[21]; //Data to be sent to the gateway with the encrypted format of the response 00022 char message2[18]; //Received formatted messsage from the gateway 00023 char message3[18]; 00024 char encryptedData[16]; //Encrypted message from the gateway 00025 char ldrsensor[9]; //Reading from the LDR 00026 char tempsensor[9]; 00027 char* encrypt; 00028 char* decrypt; 00029 char crc1[3]; 00030 int final[26]; 00031 char dataReceive[30]; 00032 //Main function 00033 int main() 00034 { 00035 00036 int x,p2=0; 00037 char command_buf[] = {'O','N'}; //Data for the comparision with the command received from the gateway 00038 char command_buf1[] = {'O','F','F'}; //Data for the comaprision with the command received from the gateway 00039 myled = 1; 00040 while(1) { 00041 // count=0; 00042 float ldrout = ldr.read(); //Read the LDR 00043 float tempout = temp.read(); 00044 float tempC = ((tempout*3.3)-0.600)*100.0; 00045 // int err = sensor.readData(); 00046 // pc.printf("\nTemperature is : %f",tempC); 00047 // wait(1); 00048 if(device.readable()) { // Check whether the data is received BLE or not 00049 bluetooth(); //Receive the message and store it in message2 00050 device.gets(dataReceive,19); 00051 pc.printf("The data received is : %s",dataReceive); //Print the message2 00052 00053 00054 for(x=0; x<19; x++) { 00055 message3[x]=dataReceive[x]; //Extract the exact encrypted data with CRC bytes from message2 and store it in message3 00056 } 00057 for(x=0; x<30; x++) { 00058 dataReceive[x]='\0'; //Extract the exact encrypted data with CRC bytes from message2 and store it in message3 00059 } 00060 00061 int flg = crcCheck_receive(message3); //Do the CRC check for the message3 00062 pc.printf("\n The flag value is : %d",flg); //Print the status of the flg which the output of the crcCheck_receive 00063 if(flg==0) { //if flg is 0 00064 pc.printf("\nCRC operation is successful"); //print CRC operation is successful 00065 for(x=0; x<16; x++) { 00066 encryptedData[x]=message3[x]; //Extract the encrypted data from the message3 and store it in encryptedData 00067 } 00068 decrypt = decryptData(encryptedData); //Decrypt the encryptedData and store it in decrypt 00069 flag = compare_strings(decrypt, command_buf); //Compare the decrypted the message for "ON" condition and store the status in flag 00070 flag1 = compare_strings(decrypt, command_buf1); //Compare the decrypted the message for "OFF" condition and store the status in flag1 00071 00072 //if message1 is ON 00073 if (flag == 1) { 00074 myled = 0; //Turn ON the LED 00075 // wait_ms(1); //Wait for 1 second 00076 encrypt=encryptData(ackmsgon); 00077 //Encrypt the response 00078 dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway 00079 00080 } 00081 //if message1 is OFF 00082 else if(flag1==1) { 00083 myled =1; //Turn OFF the LED 00084 // wait_ms(10); //Wait for 1 second 00085 encrypt=encryptData(ackmsgoff); //Encrypt the response 00086 dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway 00087 00088 } 00089 else if(flag1==1) { 00090 myled =1; //Turn OFF the LED 00091 // wait_ms(10); //Wait for 1 second 00092 encrypt=encryptData(ackmsgoff); //Encrypt the response 00093 dataPattern(encrypt,50); //Create the datapattern and combine it with the encrypted repsonse and send it to the gateway 00094 00095 } 00096 } else { 00097 pc.printf("\nCRC Operation Failed"); 00098 } 00099 } 00100 00101 //if LDR reading is greater than or equal to 0.200000 00102 /* else if(ldrout>0.200000) { 00103 //Wait for 1 second 00104 sprintf(ldrsensor,"%f",ldrout); //Convert the float format to string format 00105 ldrsensor[8]='0'; 00106 encrypt=encryptData(ldrsensor); //Encrypt the response 00107 dataPattern(encrypt,100); 00108 memset(ldrsensor,'\0',sizeof(ldrsensor)); //Create the datapattern and combine it with the encrypted LDR reading and send it to the gateway 00109 wait(5); 00110 }*/ 00111 00112 /*else if(tempC>25.000000) { 00113 //Wait for 1 second 00114 sprintf(tempsensor,"%f",tempout); //Convert the float format to string format 00115 //tempsensor[8]='0'; 00116 encrypt=encryptData(tempsensor); //Encrypt the response 00117 dataPattern(encrypt,100); //Create the datapattern and combine it with the encrypted LDR reading and send it to the gateway 00118 memset(tempsensor,'\0',sizeof(tempsensor)); 00119 wait(5); 00120 }*/ 00121 } 00122 00123 00124 00125 00126 } 00127 //Function for the data pattern format 00128 void dataPattern(char* encack,int time) 00129 { 00130 // v3=0; 00131 int v2=0; 00132 int w2; 00133 //Adding the start bit[0] 00134 00135 //Adding the whole encrypted data 00136 /* for(int w=2;w<18;w++) 00137 { 00138 sendData[w]=encack[v3]; 00139 v3++; 00140 }*/ 00141 00142 00143 00144 char* crctr = crcCheck_transmit(encack); //Genrating the CRC bits from the encrypted data and store it in crctr 00145 00146 00147 for(w2=0; w2<18; w2++) { 00148 sendData[w2]=crctr[w2]; //Storing the CRC along with the encrypted data in the sendData 00149 00150 } 00151 pc.printf("\n The Data should be sent is : %s\n",sendData); //Print the whole format of the data combined with encrypted data 00152 00153 00154 //For loop for sending the whole data via BLE 00155 for(int k=0; k<19; k++) { 00156 wait_ms(time); //Wait for 20 milisecond 00157 pc.putc(sendData[k]); //Print the character 00158 device.putc(sendData[k]); //Send the character via BLE 00159 } 00160 00161 } 00162 void bluetooth() //bluetooth function for reading data from slave device 00163 { 00164 int z=0; 00165 while(1) { 00166 if(device.readable()) { 00167 char c=device.getc(); 00168 if(c=='\0') { // if char is null then return to main function where it was called 00169 z=0; 00170 return; 00171 } else { 00172 dataReceive[z]=c; // 00173 printf("%c",c); 00174 z++; 00175 } 00176 } 00177 } 00178 } 00179 //Function to compare two strings 00180 int compare_strings(char a[], char b[]) 00181 { 00182 int c = 0; //Initial count=0 00183 int n=strlen(decrypt); //Size of the decrypted message 00184 00185 //Run the loop till the size is less than n 00186 for(int s=0; s<n; s++) { 00187 if(a[s] == b[s]) //if both the data is matched in the respective location 00188 c++; //Increment c 00189 else //if both the data is not matched in the respective location 00190 break; //Break the loop 00191 } 00192 00193 00194 if(c==n) { //If c is equal to n 00195 return 1; //return 1 00196 } else //If c is not equal to n 00197 return 0; //Return 0 00198 }
Generated on Thu Jul 21 2022 01:54:31 by 1.7.2