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