prototype
Dependents: Mbed_Prototype_copy_4_INNO_day_15_6_2017
crc.cpp@3:1250751bf7b0, 2017-06-20 (annotated)
- Committer:
- irayya
- Date:
- Tue Jun 20 05:34:42 2017 +0000
- Revision:
- 3:1250751bf7b0
- Parent:
- 2:c0d73ae91788
Modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irayya | 0:b3b7346e07fe | 1 | #include "mbed.h" |
irayya | 0:b3b7346e07fe | 2 | #include "crc.h" |
irayya | 0:b3b7346e07fe | 3 | Serial output(USBTX,USBRX); |
irayya | 0:b3b7346e07fe | 4 | char result1[18]; |
irayya | 0:b3b7346e07fe | 5 | int binary1[256]; |
irayya | 0:b3b7346e07fe | 6 | char binary2[256]; |
irayya | 0:b3b7346e07fe | 7 | int generator[]= {1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0}; |
irayya | 0:b3b7346e07fe | 8 | int v,v1; |
irayya | 0:b3b7346e07fe | 9 | |
irayya | 0:b3b7346e07fe | 10 | |
irayya | 0:b3b7346e07fe | 11 | |
irayya | 2:c0d73ae91788 | 12 | char* crcCheck_transmit(char* targetChar) //for transmission the data from gateway to slave device with crc |
irayya | 0:b3b7346e07fe | 13 | { |
irayya | 0:b3b7346e07fe | 14 | int i1,j1,k1,l1,h1; |
irayya | 2:c0d73ae91788 | 15 | |
irayya | 0:b3b7346e07fe | 16 | |
irayya | 0:b3b7346e07fe | 17 | |
irayya | 0:b3b7346e07fe | 18 | v=0,v1=0; |
irayya | 2:c0d73ae91788 | 19 | for(h1=0; h1<16; h1++) { //loop for binary printing |
irayya | 0:b3b7346e07fe | 20 | printBinary(targetChar[h1]); |
irayya | 2:c0d73ae91788 | 21 | output.printf(" - "); |
irayya | 0:b3b7346e07fe | 22 | } |
irayya | 0:b3b7346e07fe | 23 | //output.printf("%d",sizeof(f)); |
irayya | 3:1250751bf7b0 | 24 | // output.printf("\n The data to be analyzed is : "); |
irayya | 0:b3b7346e07fe | 25 | int frame[v+16]; |
irayya | 2:c0d73ae91788 | 26 | for(h1=0; h1<v; h1++) { //storing the binary data in frame array |
irayya | 3:1250751bf7b0 | 27 | // output.printf("%d",binary1[h1]); |
irayya | 0:b3b7346e07fe | 28 | frame[h1]=binary1[h1]; |
irayya | 0:b3b7346e07fe | 29 | } |
irayya | 2:c0d73ae91788 | 30 | //frame[2]=0; |
irayya | 3:1250751bf7b0 | 31 | /* output.printf("\n The value of v is : %d ",v); //Printing the value of V i.e total binary no. |
irayya | 0:b3b7346e07fe | 32 | |
irayya | 0:b3b7346e07fe | 33 | output.printf("\n Sender Side:"); |
irayya | 3:1250751bf7b0 | 34 | output.printf("\n Frame: ");*/ |
irayya | 3:1250751bf7b0 | 35 | /* for(i1=0; i1<v; i1++) { // total binary stream printing |
irayya | 0:b3b7346e07fe | 36 | output.printf("%d",frame[i1]); |
irayya | 0:b3b7346e07fe | 37 | } |
irayya | 3:1250751bf7b0 | 38 | output.printf("\n Generator : "); */ // generator for CRC checking |
irayya | 3:1250751bf7b0 | 39 | /* for(i1=0; i1<16; i1++) { |
irayya | 0:b3b7346e07fe | 40 | output.printf("%d",generator[i1]); |
irayya | 3:1250751bf7b0 | 41 | }*/ |
irayya | 0:b3b7346e07fe | 42 | |
irayya | 0:b3b7346e07fe | 43 | //Append 0's |
irayya | 0:b3b7346e07fe | 44 | int rs1=16-1; |
irayya | 2:c0d73ae91788 | 45 | for (i1=v; i1<v+rs1; i1++) { //appending the zeros to the data for calculating the crc |
irayya | 0:b3b7346e07fe | 46 | frame[i1]=0; |
irayya | 0:b3b7346e07fe | 47 | } |
irayya | 0:b3b7346e07fe | 48 | |
irayya | 0:b3b7346e07fe | 49 | int temp2[v+16]; |
irayya | 2:c0d73ae91788 | 50 | for(i1=0; i1<v+16; i1++) { // copying the data from one array to another array |
irayya | 0:b3b7346e07fe | 51 | temp2[i1]=frame[i1]; |
irayya | 0:b3b7346e07fe | 52 | } |
irayya | 0:b3b7346e07fe | 53 | |
irayya | 3:1250751bf7b0 | 54 | /* output.printf("\n Message after appending 0's :"); |
irayya | 2:c0d73ae91788 | 55 | for(i1=0; i1<v+rs1; i1++) { //printing the data after appending zeros |
irayya | 0:b3b7346e07fe | 56 | output.printf("%d",temp2[i1]); |
irayya | 3:1250751bf7b0 | 57 | }*/ |
irayya | 0:b3b7346e07fe | 58 | |
irayya | 0:b3b7346e07fe | 59 | //Division |
irayya | 2:c0d73ae91788 | 60 | for(i1=0; i1<v; i1++) { //CRC generating through division operation |
irayya | 0:b3b7346e07fe | 61 | j1=0; |
irayya | 0:b3b7346e07fe | 62 | k1=i1; |
irayya | 0:b3b7346e07fe | 63 | //check whether it is divisible or not |
irayya | 0:b3b7346e07fe | 64 | if (temp2[k1]>=generator[j1]) { |
irayya | 0:b3b7346e07fe | 65 | for(j1=0,k1=i1; j1<16; j1++,k1++) { |
irayya | 0:b3b7346e07fe | 66 | if((temp2[k1]==1 && generator[j1]==1) || (temp2[k1]==0 && generator[j1]==0)) { |
irayya | 0:b3b7346e07fe | 67 | temp2[k1]=0; |
irayya | 0:b3b7346e07fe | 68 | } else { |
irayya | 0:b3b7346e07fe | 69 | temp2[k1]=1; |
irayya | 0:b3b7346e07fe | 70 | } |
irayya | 0:b3b7346e07fe | 71 | } |
irayya | 0:b3b7346e07fe | 72 | } |
irayya | 0:b3b7346e07fe | 73 | } |
irayya | 0:b3b7346e07fe | 74 | |
irayya | 0:b3b7346e07fe | 75 | //CRC |
irayya | 0:b3b7346e07fe | 76 | int crc[16]; |
irayya | 2:c0d73ae91788 | 77 | for(i1=0,j1=v; i1<rs1; i1++,j1++) { // storing CRC bits in CRC array |
irayya | 0:b3b7346e07fe | 78 | crc[i1]=temp2[j1]; |
irayya | 0:b3b7346e07fe | 79 | } |
irayya | 0:b3b7346e07fe | 80 | |
irayya | 3:1250751bf7b0 | 81 | /*output.printf("\n CRC bits: "); |
irayya | 2:c0d73ae91788 | 82 | for(i1=0; i1<rs1; i1++) { //printing CRC bits |
irayya | 0:b3b7346e07fe | 83 | output.printf("%d",crc[i1]); |
irayya | 3:1250751bf7b0 | 84 | }*/ |
irayya | 0:b3b7346e07fe | 85 | |
irayya | 3:1250751bf7b0 | 86 | //output.printf("\n Transmitted Frame: "); |
irayya | 2:c0d73ae91788 | 87 | int tf2[v+16]; |
irayya | 2:c0d73ae91788 | 88 | char tf3[v+16]; |
irayya | 2:c0d73ae91788 | 89 | for(i1=0; i1<v; i1++) { // copying the data from one to another |
irayya | 2:c0d73ae91788 | 90 | tf2[i1]=frame[i1]; |
irayya | 2:c0d73ae91788 | 91 | } |
irayya | 2:c0d73ae91788 | 92 | for(i1=v,j1=0; i1<v+rs1; i1++,j1++) { // Adding CRC bits to the data |
irayya | 2:c0d73ae91788 | 93 | tf2[i1]=crc[j1]; |
irayya | 2:c0d73ae91788 | 94 | } |
irayya | 3:1250751bf7b0 | 95 | /* for(i1=0; i1<v+rs1; i1++) { // printing the data after adding the CRC bits to the data |
irayya | 2:c0d73ae91788 | 96 | output.printf("%d",tf2[i1]); |
irayya | 3:1250751bf7b0 | 97 | }*/ |
irayya | 1:fc4b4d69aa7a | 98 | |
irayya | 2:c0d73ae91788 | 99 | tf2[v+rs1]=0; |
irayya | 1:fc4b4d69aa7a | 100 | |
irayya | 3:1250751bf7b0 | 101 | /* output.printf("\n The final data to be sent is : "); // printing the Final data |
irayya | 3:1250751bf7b0 | 102 | for(i1=0; i1<=v+rs1; i1++) { |
irayya | 3:1250751bf7b0 | 103 | output.printf("%d",tf2[i1]); |
irayya | 3:1250751bf7b0 | 104 | }*/ |
irayya | 3:1250751bf7b0 | 105 | |
irayya | 3:1250751bf7b0 | 106 | /* for(i1=0;i1<7;i1++) |
irayya | 3:1250751bf7b0 | 107 | { |
irayya | 3:1250751bf7b0 | 108 | tf2[i1]=1; |
irayya | 3:1250751bf7b0 | 109 | } |
irayya | 3:1250751bf7b0 | 110 | for(i1=136;i1<144;i1++) |
irayya | 3:1250751bf7b0 | 111 | { |
irayya | 3:1250751bf7b0 | 112 | tf2[i1]=1; |
irayya | 3:1250751bf7b0 | 113 | } |
irayya | 2:c0d73ae91788 | 114 | output.printf("\n The final data to be sent is : "); // printing the Final data |
irayya | 2:c0d73ae91788 | 115 | for(i1=0; i1<=v+rs1; i1++) { |
irayya | 2:c0d73ae91788 | 116 | output.printf("%d",tf2[i1]); |
irayya | 3:1250751bf7b0 | 117 | }*/ |
irayya | 2:c0d73ae91788 | 118 | for(i1=0; i1<=v+rs1; i1++) { |
irayya | 2:c0d73ae91788 | 119 | charBinary(tf2[i1]); |
irayya | 0:b3b7346e07fe | 120 | } |
irayya | 3:1250751bf7b0 | 121 | /* output.printf("\n The final data to be sent is : "); |
irayya | 2:c0d73ae91788 | 122 | for(i1=0; i1<=v+rs1; i1++) { |
irayya | 0:b3b7346e07fe | 123 | output.printf("%c",binary2[i1]); |
irayya | 3:1250751bf7b0 | 124 | }*/ |
irayya | 0:b3b7346e07fe | 125 | printChar(binary2); |
irayya | 2:c0d73ae91788 | 126 | |
irayya | 0:b3b7346e07fe | 127 | char* str=result1; |
irayya | 2:c0d73ae91788 | 128 | //result1[18]='!'; |
irayya | 2:c0d73ae91788 | 129 | output.printf("\n check %s ", str); |
irayya | 0:b3b7346e07fe | 130 | v=0,v1=0; |
irayya | 2:c0d73ae91788 | 131 | return result1; |
irayya | 0:b3b7346e07fe | 132 | } |
irayya | 2:c0d73ae91788 | 133 | |
irayya | 2:c0d73ae91788 | 134 | |
irayya | 2:c0d73ae91788 | 135 | |
irayya | 2:c0d73ae91788 | 136 | int crcCheck_receive(char a1[]) //receiving the CRC with AES encryption data from slave and checking |
irayya | 0:b3b7346e07fe | 137 | { |
irayya | 0:b3b7346e07fe | 138 | int i,j,k,l; |
irayya | 0:b3b7346e07fe | 139 | int rs=15; |
irayya | 0:b3b7346e07fe | 140 | v=0; |
irayya | 3:1250751bf7b0 | 141 | /* output.printf("\n Receiver side : "); |
irayya | 3:1250751bf7b0 | 142 | output.printf("\n Received Frame: ");*/ |
irayya | 0:b3b7346e07fe | 143 | |
irayya | 2:c0d73ae91788 | 144 | for(int m=0; m<18; m++) { |
irayya | 1:fc4b4d69aa7a | 145 | printBinary(a1[m]); |
irayya | 1:fc4b4d69aa7a | 146 | output.printf("-"); |
irayya | 1:fc4b4d69aa7a | 147 | } |
irayya | 3:1250751bf7b0 | 148 | /*for(i=0; i<v; i++) { |
irayya | 0:b3b7346e07fe | 149 | output.printf("%d",binary1[i]); |
irayya | 3:1250751bf7b0 | 150 | }*/ |
irayya | 0:b3b7346e07fe | 151 | v=v-16; |
irayya | 0:b3b7346e07fe | 152 | int tf[v+16]; |
irayya | 0:b3b7346e07fe | 153 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 154 | tf[i]=binary1[i]; |
irayya | 0:b3b7346e07fe | 155 | } |
irayya | 3:1250751bf7b0 | 156 | /*output.printf("\n"); |
irayya | 3:1250751bf7b0 | 157 | output.printf("The data to be Analyzed on the receiving side : ");*/ |
irayya | 3:1250751bf7b0 | 158 | /* for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 159 | output.printf("%d",tf[i]); |
irayya | 3:1250751bf7b0 | 160 | }*/ |
irayya | 0:b3b7346e07fe | 161 | int temp[v+16]; |
irayya | 2:c0d73ae91788 | 162 | int temp3[v+16]; |
irayya | 0:b3b7346e07fe | 163 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 164 | temp[i]=tf[i]; |
irayya | 3:1250751bf7b0 | 165 | //temp3[i]=tf[i]; |
irayya | 0:b3b7346e07fe | 166 | } |
irayya | 0:b3b7346e07fe | 167 | |
irayya | 3:1250751bf7b0 | 168 | |
irayya | 3:1250751bf7b0 | 169 | //Division |
irayya | 3:1250751bf7b0 | 170 | for(i=0; i<v; i++) { |
irayya | 2:c0d73ae91788 | 171 | j=0; |
irayya | 2:c0d73ae91788 | 172 | k=i; |
irayya | 2:c0d73ae91788 | 173 | if (temp[k]>=generator[j]) { |
irayya | 2:c0d73ae91788 | 174 | for(j=0,k=i; j<16; j++,k++) { |
irayya | 3:1250751bf7b0 | 175 | if(temp[k] == generator[j]) { |
irayya | 0:b3b7346e07fe | 176 | temp[k]=0; |
irayya | 0:b3b7346e07fe | 177 | } else { |
irayya | 0:b3b7346e07fe | 178 | temp[k]=1; |
irayya | 0:b3b7346e07fe | 179 | } |
irayya | 0:b3b7346e07fe | 180 | } |
irayya | 0:b3b7346e07fe | 181 | } |
irayya | 0:b3b7346e07fe | 182 | } |
irayya | 0:b3b7346e07fe | 183 | |
irayya | 3:1250751bf7b0 | 184 | output.printf("\n Reaminder: "); |
irayya | 0:b3b7346e07fe | 185 | int rrem[16]; |
irayya | 0:b3b7346e07fe | 186 | for (i=v,j=0; i<v+rs; i++,j++) { |
irayya | 0:b3b7346e07fe | 187 | rrem[j]= temp[i]; |
irayya | 0:b3b7346e07fe | 188 | } |
irayya | 0:b3b7346e07fe | 189 | for(i=0; i<rs; i++) { |
irayya | 0:b3b7346e07fe | 190 | output.printf("%d",rrem[i]); |
irayya | 0:b3b7346e07fe | 191 | } |
irayya | 0:b3b7346e07fe | 192 | |
irayya | 0:b3b7346e07fe | 193 | int flag=0; |
irayya | 0:b3b7346e07fe | 194 | for(i=0; i<rs; i++) { |
irayya | 0:b3b7346e07fe | 195 | if(rrem[i]!=0) { |
irayya | 0:b3b7346e07fe | 196 | flag=1; |
irayya | 3:1250751bf7b0 | 197 | break; |
irayya | 0:b3b7346e07fe | 198 | } |
irayya | 0:b3b7346e07fe | 199 | } |
irayya | 0:b3b7346e07fe | 200 | |
irayya | 0:b3b7346e07fe | 201 | if(flag==0) { |
irayya | 0:b3b7346e07fe | 202 | //output.printf("\n Since Remainder Is 0 Hence Message Transmitted From Sender To Receriver Is Correct"); |
irayya | 0:b3b7346e07fe | 203 | return 0; |
irayya | 1:fc4b4d69aa7a | 204 | |
irayya | 0:b3b7346e07fe | 205 | |
irayya | 0:b3b7346e07fe | 206 | } else { |
irayya | 1:fc4b4d69aa7a | 207 | // output.printf(" \n Since Remainder Is Not 0 Hence Message Transmitted From Sender To Receriver Contains Error"); |
irayya | 1:fc4b4d69aa7a | 208 | return 1; |
irayya | 0:b3b7346e07fe | 209 | } |
irayya | 0:b3b7346e07fe | 210 | v=0; |
irayya | 0:b3b7346e07fe | 211 | } |
irayya | 0:b3b7346e07fe | 212 | |
irayya | 0:b3b7346e07fe | 213 | void printBinary(char c) |
irayya | 0:b3b7346e07fe | 214 | { |
irayya | 0:b3b7346e07fe | 215 | |
irayya | 2:c0d73ae91788 | 216 | for (int i = 7; i >= 0; --i) { //binary data to be printing 8 bits for each cahracter |
irayya | 0:b3b7346e07fe | 217 | int x = ((c & (1 << i))? 1 : 0); |
irayya | 0:b3b7346e07fe | 218 | output.printf("%d",x); |
irayya | 0:b3b7346e07fe | 219 | binary1[v]=x; |
irayya | 0:b3b7346e07fe | 220 | v++; |
irayya | 0:b3b7346e07fe | 221 | } |
irayya | 2:c0d73ae91788 | 222 | //printf("the value of v is given as %d",v); |
irayya | 0:b3b7346e07fe | 223 | } |
irayya | 0:b3b7346e07fe | 224 | |
irayya | 0:b3b7346e07fe | 225 | void charBinary(int c1) |
irayya | 0:b3b7346e07fe | 226 | { |
irayya | 0:b3b7346e07fe | 227 | char x1 = ((c1 & 1)? '1' : '0'); |
irayya | 0:b3b7346e07fe | 228 | // output.printf("%c",x1); |
irayya | 0:b3b7346e07fe | 229 | binary2[v1]=x1; |
irayya | 0:b3b7346e07fe | 230 | v1++; |
irayya | 0:b3b7346e07fe | 231 | } |
irayya | 0:b3b7346e07fe | 232 | |
irayya | 0:b3b7346e07fe | 233 | |
irayya | 0:b3b7346e07fe | 234 | /* ####### printChar function ########## */ |
irayya | 0:b3b7346e07fe | 235 | |
irayya | 0:b3b7346e07fe | 236 | void printChar(char x2[]) |
irayya | 0:b3b7346e07fe | 237 | { |
irayya | 0:b3b7346e07fe | 238 | |
irayya | 0:b3b7346e07fe | 239 | //memcpy(a, b, sizeof(a)); |
irayya | 0:b3b7346e07fe | 240 | char *ON = x2 ; |
irayya | 0:b3b7346e07fe | 241 | output.printf("\n"); |
irayya | 0:b3b7346e07fe | 242 | output.printf("%c",x2); |
irayya | 0:b3b7346e07fe | 243 | char subbuff[9]; |
irayya | 2:c0d73ae91788 | 244 | /*for(int j=0; j<sizeof(result1); j++) { |
irayya | 0:b3b7346e07fe | 245 | result1[j]='\0'; |
irayya | 2:c0d73ae91788 | 246 | }*/ |
irayya | 0:b3b7346e07fe | 247 | unsigned char c3; |
irayya | 0:b3b7346e07fe | 248 | int k1; |
irayya | 0:b3b7346e07fe | 249 | int index = 0; |
irayya | 0:b3b7346e07fe | 250 | int fichero_len = (int)strlen(ON); |
irayya | 0:b3b7346e07fe | 251 | |
irayya | 0:b3b7346e07fe | 252 | for(k1 = 0; k1 < fichero_len; k1 += 8) { |
irayya | 0:b3b7346e07fe | 253 | memcpy(subbuff, &ON[k1], 8); // <--- copy 8 butes only |
irayya | 0:b3b7346e07fe | 254 | subbuff[8] = '\0'; |
irayya | 0:b3b7346e07fe | 255 | c3 = (unsigned char)strtol(subbuff, 0, 2); |
irayya | 0:b3b7346e07fe | 256 | //printf("\%s = %c = %d = 0x%.2X\n", subbuff, c3, c3, c3); |
irayya | 2:c0d73ae91788 | 257 | result1[index] = c3; |
irayya | 2:c0d73ae91788 | 258 | //result1[index] = '\0'; |
irayya | 2:c0d73ae91788 | 259 | index++; |
irayya | 0:b3b7346e07fe | 260 | } |
irayya | 0:b3b7346e07fe | 261 | printf("\nResult = %s\n", result1); |
irayya | 0:b3b7346e07fe | 262 | |
irayya | 0:b3b7346e07fe | 263 | } |