prototype
Dependents: Mbed_Prototype_copy_4_INNO_day_15_6_2017
crc.cpp@0:b3b7346e07fe, 2017-05-29 (annotated)
- Committer:
- irayya
- Date:
- Mon May 29 03:24:36 2017 +0000
- Revision:
- 0:b3b7346e07fe
- Child:
- 1:fc4b4d69aa7a
CRC
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 | 0:b3b7346e07fe | 12 | char* crcCheck_transmit(char* targetChar) |
irayya | 0:b3b7346e07fe | 13 | { |
irayya | 0:b3b7346e07fe | 14 | int i1,j1,k1,l1,h1; |
irayya | 0:b3b7346e07fe | 15 | //char* str = malloc(3 * sizeof(char)); |
irayya | 0:b3b7346e07fe | 16 | //Get Frame |
irayya | 0:b3b7346e07fe | 17 | |
irayya | 0:b3b7346e07fe | 18 | |
irayya | 0:b3b7346e07fe | 19 | v=0,v1=0; |
irayya | 0:b3b7346e07fe | 20 | for(h1=0; h1<strlen(targetChar); h1++) { |
irayya | 0:b3b7346e07fe | 21 | printBinary(targetChar[h1]); |
irayya | 0:b3b7346e07fe | 22 | output.printf(" - "); |
irayya | 0:b3b7346e07fe | 23 | } |
irayya | 0:b3b7346e07fe | 24 | //output.printf("%d",sizeof(f)); |
irayya | 0:b3b7346e07fe | 25 | output.printf("\n The data to be analyzed is : "); |
irayya | 0:b3b7346e07fe | 26 | int frame[v+16]; |
irayya | 0:b3b7346e07fe | 27 | for(h1=0; h1<v; h1++) { |
irayya | 0:b3b7346e07fe | 28 | output.printf("%d",binary1[h1]); |
irayya | 0:b3b7346e07fe | 29 | frame[h1]=binary1[h1]; |
irayya | 0:b3b7346e07fe | 30 | } |
irayya | 0:b3b7346e07fe | 31 | |
irayya | 0:b3b7346e07fe | 32 | output.printf("\n The value of v is : %d ",v); |
irayya | 0:b3b7346e07fe | 33 | |
irayya | 0:b3b7346e07fe | 34 | output.printf("\n Sender Side:"); |
irayya | 0:b3b7346e07fe | 35 | output.printf("\n Frame: "); |
irayya | 0:b3b7346e07fe | 36 | for(i1=0; i1<v; i1++) { |
irayya | 0:b3b7346e07fe | 37 | output.printf("%d",frame[i1]); |
irayya | 0:b3b7346e07fe | 38 | } |
irayya | 0:b3b7346e07fe | 39 | output.printf("\n Generator : "); |
irayya | 0:b3b7346e07fe | 40 | for(i1=0; i1<16; i1++) { |
irayya | 0:b3b7346e07fe | 41 | output.printf("%d",generator[i1]); |
irayya | 0:b3b7346e07fe | 42 | } |
irayya | 0:b3b7346e07fe | 43 | |
irayya | 0:b3b7346e07fe | 44 | //Append 0's |
irayya | 0:b3b7346e07fe | 45 | int rs1=16-1; |
irayya | 0:b3b7346e07fe | 46 | for (i1=v; i1<v+rs1; i1++) { |
irayya | 0:b3b7346e07fe | 47 | frame[i1]=0; |
irayya | 0:b3b7346e07fe | 48 | } |
irayya | 0:b3b7346e07fe | 49 | |
irayya | 0:b3b7346e07fe | 50 | int temp2[v+16]; |
irayya | 0:b3b7346e07fe | 51 | for(i1=0; i1<v+16; i1++) { |
irayya | 0:b3b7346e07fe | 52 | temp2[i1]=frame[i1]; |
irayya | 0:b3b7346e07fe | 53 | } |
irayya | 0:b3b7346e07fe | 54 | |
irayya | 0:b3b7346e07fe | 55 | output.printf("\n Message after appending 0's :"); |
irayya | 0:b3b7346e07fe | 56 | for(i1=0; i1<v+rs1; i1++) { |
irayya | 0:b3b7346e07fe | 57 | output.printf("%d",temp2[i1]); |
irayya | 0:b3b7346e07fe | 58 | } |
irayya | 0:b3b7346e07fe | 59 | |
irayya | 0:b3b7346e07fe | 60 | //Division |
irayya | 0:b3b7346e07fe | 61 | for(i1=0; i1<v; i1++) { |
irayya | 0:b3b7346e07fe | 62 | j1=0; |
irayya | 0:b3b7346e07fe | 63 | k1=i1; |
irayya | 0:b3b7346e07fe | 64 | //check whether it is divisible or not |
irayya | 0:b3b7346e07fe | 65 | if (temp2[k1]>=generator[j1]) { |
irayya | 0:b3b7346e07fe | 66 | for(j1=0,k1=i1; j1<16; j1++,k1++) { |
irayya | 0:b3b7346e07fe | 67 | if((temp2[k1]==1 && generator[j1]==1) || (temp2[k1]==0 && generator[j1]==0)) { |
irayya | 0:b3b7346e07fe | 68 | temp2[k1]=0; |
irayya | 0:b3b7346e07fe | 69 | } else { |
irayya | 0:b3b7346e07fe | 70 | temp2[k1]=1; |
irayya | 0:b3b7346e07fe | 71 | } |
irayya | 0:b3b7346e07fe | 72 | } |
irayya | 0:b3b7346e07fe | 73 | } |
irayya | 0:b3b7346e07fe | 74 | } |
irayya | 0:b3b7346e07fe | 75 | |
irayya | 0:b3b7346e07fe | 76 | //CRC |
irayya | 0:b3b7346e07fe | 77 | int crc[16]; |
irayya | 0:b3b7346e07fe | 78 | for(i1=0,j1=v; i1<rs1; i1++,j1++) { |
irayya | 0:b3b7346e07fe | 79 | crc[i1]=temp2[j1]; |
irayya | 0:b3b7346e07fe | 80 | } |
irayya | 0:b3b7346e07fe | 81 | |
irayya | 0:b3b7346e07fe | 82 | output.printf("\n CRC bits: "); |
irayya | 0:b3b7346e07fe | 83 | for(i1=0; i1<rs1; i1++) { |
irayya | 0:b3b7346e07fe | 84 | output.printf("%d",crc[i1]); |
irayya | 0:b3b7346e07fe | 85 | } |
irayya | 0:b3b7346e07fe | 86 | |
irayya | 0:b3b7346e07fe | 87 | /* output.printf("\n Transmitted Frame: "); |
irayya | 0:b3b7346e07fe | 88 | int tf2[v+16]; |
irayya | 0:b3b7346e07fe | 89 | char tf3[v+16]; |
irayya | 0:b3b7346e07fe | 90 | for(i1=0; i1<v; i1++) { |
irayya | 0:b3b7346e07fe | 91 | tf2[i1]=frame[i1]; |
irayya | 0:b3b7346e07fe | 92 | } |
irayya | 0:b3b7346e07fe | 93 | for(i1=v,j1=0; i1<v+rs1; i1++,j1++) { |
irayya | 0:b3b7346e07fe | 94 | tf2[i1]=crc[j1]; |
irayya | 0:b3b7346e07fe | 95 | } |
irayya | 0:b3b7346e07fe | 96 | for(i1=0; i1<v+rs1; i1++) { |
irayya | 0:b3b7346e07fe | 97 | output.printf("%d",tf2[i1]); |
irayya | 0:b3b7346e07fe | 98 | } |
irayya | 0:b3b7346e07fe | 99 | |
irayya | 0:b3b7346e07fe | 100 | tf2[v+rs1]=0; |
irayya | 0:b3b7346e07fe | 101 | |
irayya | 0:b3b7346e07fe | 102 | output.printf("\n The final data to be sent is : "); |
irayya | 0:b3b7346e07fe | 103 | for(i1=0; i1<=v+rs1; i1++) { |
irayya | 0:b3b7346e07fe | 104 | output.printf("%d",tf2[i1]); |
irayya | 0:b3b7346e07fe | 105 | }*/ |
irayya | 0:b3b7346e07fe | 106 | crc[15]=0; |
irayya | 0:b3b7346e07fe | 107 | for(i1=0; i1<16; i1++) { |
irayya | 0:b3b7346e07fe | 108 | charBinary(crc[i1]); |
irayya | 0:b3b7346e07fe | 109 | } |
irayya | 0:b3b7346e07fe | 110 | output.printf("\n The final data to be sent is : "); |
irayya | 0:b3b7346e07fe | 111 | for(i1=0; i1<16; i1++) { |
irayya | 0:b3b7346e07fe | 112 | output.printf("%c",binary2[i1]); |
irayya | 0:b3b7346e07fe | 113 | } |
irayya | 0:b3b7346e07fe | 114 | printChar(binary2); |
irayya | 0:b3b7346e07fe | 115 | char* str=result1; |
irayya | 0:b3b7346e07fe | 116 | |
irayya | 0:b3b7346e07fe | 117 | v=0,v1=0; |
irayya | 0:b3b7346e07fe | 118 | return str; |
irayya | 0:b3b7346e07fe | 119 | } |
irayya | 0:b3b7346e07fe | 120 | int crcCheck_receive(char a1[]) |
irayya | 0:b3b7346e07fe | 121 | { |
irayya | 0:b3b7346e07fe | 122 | int i,j,k,l; |
irayya | 0:b3b7346e07fe | 123 | int rs=15; |
irayya | 0:b3b7346e07fe | 124 | v=0; |
irayya | 0:b3b7346e07fe | 125 | output.printf("\n Receiver side : "); |
irayya | 0:b3b7346e07fe | 126 | output.printf("\n Received Frame: "); |
irayya | 0:b3b7346e07fe | 127 | |
irayya | 0:b3b7346e07fe | 128 | for(int m=0; m<strlen(a1); m++) { |
irayya | 0:b3b7346e07fe | 129 | printBinary(a1[m]); |
irayya | 0:b3b7346e07fe | 130 | output.printf("-"); |
irayya | 0:b3b7346e07fe | 131 | } |
irayya | 0:b3b7346e07fe | 132 | for(i=0; i<v; i++) { |
irayya | 0:b3b7346e07fe | 133 | output.printf("%d",binary1[i]); |
irayya | 0:b3b7346e07fe | 134 | } |
irayya | 0:b3b7346e07fe | 135 | v=v-16; |
irayya | 0:b3b7346e07fe | 136 | int tf[v+16]; |
irayya | 0:b3b7346e07fe | 137 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 138 | tf[i]=binary1[i]; |
irayya | 0:b3b7346e07fe | 139 | } |
irayya | 0:b3b7346e07fe | 140 | output.printf("\n"); |
irayya | 0:b3b7346e07fe | 141 | output.printf("The data to be Analyzed on the receiving side : "); |
irayya | 0:b3b7346e07fe | 142 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 143 | output.printf("%d",tf[i]); |
irayya | 0:b3b7346e07fe | 144 | } |
irayya | 0:b3b7346e07fe | 145 | int temp[v+16]; |
irayya | 0:b3b7346e07fe | 146 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 147 | temp[i]=tf[i]; |
irayya | 0:b3b7346e07fe | 148 | } |
irayya | 0:b3b7346e07fe | 149 | |
irayya | 0:b3b7346e07fe | 150 | //Division |
irayya | 0:b3b7346e07fe | 151 | for(i=0; i<v+rs; i++) { |
irayya | 0:b3b7346e07fe | 152 | j=0; |
irayya | 0:b3b7346e07fe | 153 | k=i; |
irayya | 0:b3b7346e07fe | 154 | if (temp[k]>=generator[j]) { |
irayya | 0:b3b7346e07fe | 155 | for(j=0,k=i; j<16; j++,k++) { |
irayya | 0:b3b7346e07fe | 156 | if((temp[k]==1 && generator[j]==1) || (temp[k]==0 && generator[j]==0)) { |
irayya | 0:b3b7346e07fe | 157 | temp[k]=0; |
irayya | 0:b3b7346e07fe | 158 | } else { |
irayya | 0:b3b7346e07fe | 159 | temp[k]=1; |
irayya | 0:b3b7346e07fe | 160 | } |
irayya | 0:b3b7346e07fe | 161 | } |
irayya | 0:b3b7346e07fe | 162 | } |
irayya | 0:b3b7346e07fe | 163 | } |
irayya | 0:b3b7346e07fe | 164 | |
irayya | 0:b3b7346e07fe | 165 | output.printf("\n Reaminder: "); |
irayya | 0:b3b7346e07fe | 166 | int rrem[16]; |
irayya | 0:b3b7346e07fe | 167 | for (i=v,j=0; i<v+rs; i++,j++) { |
irayya | 0:b3b7346e07fe | 168 | rrem[j]= temp[i]; |
irayya | 0:b3b7346e07fe | 169 | } |
irayya | 0:b3b7346e07fe | 170 | for(i=0; i<rs; i++) { |
irayya | 0:b3b7346e07fe | 171 | output.printf("%d",rrem[i]); |
irayya | 0:b3b7346e07fe | 172 | } |
irayya | 0:b3b7346e07fe | 173 | |
irayya | 0:b3b7346e07fe | 174 | int flag=0; |
irayya | 0:b3b7346e07fe | 175 | for(i=0; i<rs; i++) { |
irayya | 0:b3b7346e07fe | 176 | if(rrem[i]!=0) { |
irayya | 0:b3b7346e07fe | 177 | flag=1; |
irayya | 0:b3b7346e07fe | 178 | } |
irayya | 0:b3b7346e07fe | 179 | } |
irayya | 0:b3b7346e07fe | 180 | |
irayya | 0:b3b7346e07fe | 181 | if(flag==0) { |
irayya | 0:b3b7346e07fe | 182 | //output.printf("\n Since Remainder Is 0 Hence Message Transmitted From Sender To Receriver Is Correct"); |
irayya | 0:b3b7346e07fe | 183 | return 0; |
irayya | 0:b3b7346e07fe | 184 | |
irayya | 0:b3b7346e07fe | 185 | |
irayya | 0:b3b7346e07fe | 186 | } else { |
irayya | 0:b3b7346e07fe | 187 | // output.printf(" \n Since Remainder Is Not 0 Hence Message Transmitted From Sender To Receriver Contains Error"); |
irayya | 0:b3b7346e07fe | 188 | return 1; |
irayya | 0:b3b7346e07fe | 189 | } |
irayya | 0:b3b7346e07fe | 190 | v=0; |
irayya | 0:b3b7346e07fe | 191 | } |
irayya | 0:b3b7346e07fe | 192 | |
irayya | 0:b3b7346e07fe | 193 | void printBinary(char c) |
irayya | 0:b3b7346e07fe | 194 | { |
irayya | 0:b3b7346e07fe | 195 | |
irayya | 0:b3b7346e07fe | 196 | for (int i = 7; i >= 0; --i) { |
irayya | 0:b3b7346e07fe | 197 | int x = ((c & (1 << i))? 1 : 0); |
irayya | 0:b3b7346e07fe | 198 | output.printf("%d",x); |
irayya | 0:b3b7346e07fe | 199 | binary1[v]=x; |
irayya | 0:b3b7346e07fe | 200 | v++; |
irayya | 0:b3b7346e07fe | 201 | } |
irayya | 0:b3b7346e07fe | 202 | } |
irayya | 0:b3b7346e07fe | 203 | |
irayya | 0:b3b7346e07fe | 204 | void charBinary(int c1) |
irayya | 0:b3b7346e07fe | 205 | { |
irayya | 0:b3b7346e07fe | 206 | char x1 = ((c1 & 1)? '1' : '0'); |
irayya | 0:b3b7346e07fe | 207 | // output.printf("%c",x1); |
irayya | 0:b3b7346e07fe | 208 | binary2[v1]=x1; |
irayya | 0:b3b7346e07fe | 209 | v1++; |
irayya | 0:b3b7346e07fe | 210 | } |
irayya | 0:b3b7346e07fe | 211 | |
irayya | 0:b3b7346e07fe | 212 | |
irayya | 0:b3b7346e07fe | 213 | /* ####### printChar function ########## */ |
irayya | 0:b3b7346e07fe | 214 | |
irayya | 0:b3b7346e07fe | 215 | void printChar(char x2[]) |
irayya | 0:b3b7346e07fe | 216 | { |
irayya | 0:b3b7346e07fe | 217 | |
irayya | 0:b3b7346e07fe | 218 | //memcpy(a, b, sizeof(a)); |
irayya | 0:b3b7346e07fe | 219 | char *ON = x2 ; |
irayya | 0:b3b7346e07fe | 220 | output.printf("\n"); |
irayya | 0:b3b7346e07fe | 221 | output.printf("%c",x2); |
irayya | 0:b3b7346e07fe | 222 | char subbuff[9]; |
irayya | 0:b3b7346e07fe | 223 | for(int j=0; j<sizeof(result1); j++) { |
irayya | 0:b3b7346e07fe | 224 | result1[j]='\0'; |
irayya | 0:b3b7346e07fe | 225 | } |
irayya | 0:b3b7346e07fe | 226 | unsigned char c3; |
irayya | 0:b3b7346e07fe | 227 | int k1; |
irayya | 0:b3b7346e07fe | 228 | int index = 0; |
irayya | 0:b3b7346e07fe | 229 | int fichero_len = (int)strlen(ON); |
irayya | 0:b3b7346e07fe | 230 | |
irayya | 0:b3b7346e07fe | 231 | for(k1 = 0; k1 < fichero_len; k1 += 8) { |
irayya | 0:b3b7346e07fe | 232 | memcpy(subbuff, &ON[k1], 8); // <--- copy 8 butes only |
irayya | 0:b3b7346e07fe | 233 | subbuff[8] = '\0'; |
irayya | 0:b3b7346e07fe | 234 | c3 = (unsigned char)strtol(subbuff, 0, 2); |
irayya | 0:b3b7346e07fe | 235 | //printf("\%s = %c = %d = 0x%.2X\n", subbuff, c3, c3, c3); |
irayya | 0:b3b7346e07fe | 236 | result1[index++] = c3; |
irayya | 0:b3b7346e07fe | 237 | result1[index] = '\0'; |
irayya | 0:b3b7346e07fe | 238 | } |
irayya | 0:b3b7346e07fe | 239 | printf("\nResult = %s\n", result1); |
irayya | 0:b3b7346e07fe | 240 | |
irayya | 0:b3b7346e07fe | 241 | } |