prototype

Dependents:   Mbed_Prototype_copy_4_INNO_day_15_6_2017

Committer:
irayya
Date:
Tue Jun 13 03:32:52 2017 +0000
Revision:
2:c0d73ae91788
Parent:
1:fc4b4d69aa7a
Child:
3:1250751bf7b0
pro

Who changed what in which revision?

UserRevisionLine numberNew 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 0:b3b7346e07fe 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 0:b3b7346e07fe 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 2:c0d73ae91788 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 0:b3b7346e07fe 34 output.printf("\n Frame: ");
irayya 2:c0d73ae91788 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 2:c0d73ae91788 38 output.printf("\n Generator : "); // generator for CRC checking
irayya 0:b3b7346e07fe 39 for(i1=0; i1<16; i1++) {
irayya 0:b3b7346e07fe 40 output.printf("%d",generator[i1]);
irayya 0:b3b7346e07fe 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 0:b3b7346e07fe 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 0:b3b7346e07fe 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 2:c0d73ae91788 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 0:b3b7346e07fe 84 }
irayya 0:b3b7346e07fe 85
irayya 2:c0d73ae91788 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 2:c0d73ae91788 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 2:c0d73ae91788 97 }
irayya 1:fc4b4d69aa7a 98
irayya 2:c0d73ae91788 99 tf2[v+rs1]=0;
irayya 1:fc4b4d69aa7a 100
irayya 2:c0d73ae91788 101 output.printf("\n The final data to be sent is : "); // printing the Final data
irayya 2:c0d73ae91788 102 for(i1=0; i1<=v+rs1; i1++) {
irayya 2:c0d73ae91788 103 output.printf("%d",tf2[i1]);
irayya 2:c0d73ae91788 104 }
irayya 2:c0d73ae91788 105
irayya 2:c0d73ae91788 106 for(i1=0; i1<=v+rs1; i1++) {
irayya 2:c0d73ae91788 107 charBinary(tf2[i1]);
irayya 0:b3b7346e07fe 108 }
irayya 0:b3b7346e07fe 109 output.printf("\n The final data to be sent is : ");
irayya 2:c0d73ae91788 110 for(i1=0; i1<=v+rs1; i1++) {
irayya 0:b3b7346e07fe 111 output.printf("%c",binary2[i1]);
irayya 0:b3b7346e07fe 112 }
irayya 0:b3b7346e07fe 113 printChar(binary2);
irayya 2:c0d73ae91788 114
irayya 0:b3b7346e07fe 115 char* str=result1;
irayya 2:c0d73ae91788 116 //result1[18]='!';
irayya 2:c0d73ae91788 117 output.printf("\n check %s ", str);
irayya 0:b3b7346e07fe 118 v=0,v1=0;
irayya 2:c0d73ae91788 119 return result1;
irayya 0:b3b7346e07fe 120 }
irayya 2:c0d73ae91788 121
irayya 2:c0d73ae91788 122
irayya 2:c0d73ae91788 123
irayya 2:c0d73ae91788 124 int crcCheck_receive(char a1[]) //receiving the CRC with AES encryption data from slave and checking
irayya 0:b3b7346e07fe 125 {
irayya 0:b3b7346e07fe 126 int i,j,k,l;
irayya 0:b3b7346e07fe 127 int rs=15;
irayya 0:b3b7346e07fe 128 v=0;
irayya 0:b3b7346e07fe 129 output.printf("\n Receiver side : ");
irayya 0:b3b7346e07fe 130 output.printf("\n Received Frame: ");
irayya 0:b3b7346e07fe 131
irayya 2:c0d73ae91788 132 for(int m=0; m<18; m++) {
irayya 1:fc4b4d69aa7a 133 printBinary(a1[m]);
irayya 1:fc4b4d69aa7a 134 output.printf("-");
irayya 1:fc4b4d69aa7a 135 }
irayya 0:b3b7346e07fe 136 for(i=0; i<v; i++) {
irayya 0:b3b7346e07fe 137 output.printf("%d",binary1[i]);
irayya 0:b3b7346e07fe 138 }
irayya 0:b3b7346e07fe 139 v=v-16;
irayya 0:b3b7346e07fe 140 int tf[v+16];
irayya 0:b3b7346e07fe 141 for(i=0; i<v+rs; i++) {
irayya 0:b3b7346e07fe 142 tf[i]=binary1[i];
irayya 0:b3b7346e07fe 143 }
irayya 0:b3b7346e07fe 144 output.printf("\n");
irayya 0:b3b7346e07fe 145 output.printf("The data to be Analyzed on the receiving side : ");
irayya 0:b3b7346e07fe 146 for(i=0; i<v+rs; i++) {
irayya 0:b3b7346e07fe 147 output.printf("%d",tf[i]);
irayya 0:b3b7346e07fe 148 }
irayya 0:b3b7346e07fe 149 int temp[v+16];
irayya 2:c0d73ae91788 150 int temp3[v+16];
irayya 0:b3b7346e07fe 151 for(i=0; i<v+rs; i++) {
irayya 0:b3b7346e07fe 152 temp[i]=tf[i];
irayya 2:c0d73ae91788 153 temp3[i]=tf[i];
irayya 0:b3b7346e07fe 154 }
irayya 0:b3b7346e07fe 155
irayya 2:c0d73ae91788 156 //changing data
irayya 2:c0d73ae91788 157
irayya 2:c0d73ae91788 158 /*for(i=0; i<20; i++) {
irayya 2:c0d73ae91788 159 if(i%2==0) {
irayya 2:c0d73ae91788 160 tamp3[i]==0;
irayya 2:c0d73ae91788 161 }
irayya 2:c0d73ae91788 162 else
irayya 2:c0d73ae91788 163 tamp3[i]==1;
irayya 2:c0d73ae91788 164
irayya 2:c0d73ae91788 165 }
irayya 2:c0d73ae91788 166
irayya 2:c0d73ae91788 167
irayya 2:c0d73ae91788 168
irayya 2:c0d73ae91788 169 for(i=20;i<143;i++)
irayya 2:c0d73ae91788 170 {
irayya 2:c0d73ae91788 171 temp3[i]=temp[i];
irayya 2:c0d73ae91788 172 }
irayya 2:c0d73ae91788 173 for(i=0=i<sizeof(temp3);i++)
irayya 2:c0d73ae91788 174 {
irayya 2:c0d73ae91788 175 output.printf("after 20 wrong bits added in the binary data %d",binary1[i]);
irayya 2:c0d73ae91788 176 }
irayya 2:c0d73ae91788 177 */
irayya 2:c0d73ae91788 178
irayya 2:c0d73ae91788 179
irayya 2:c0d73ae91788 180 /*//Division
irayya 2:c0d73ae91788 181 for(i=0; i<v+rs; i++) {
irayya 2:c0d73ae91788 182 j=0;
irayya 2:c0d73ae91788 183 k=i;
irayya 2:c0d73ae91788 184 if (temp[k]>=generator[j]) {
irayya 2:c0d73ae91788 185 for(j=0,k=i; j<16; j++,k++) {
irayya 2:c0d73ae91788 186 if((temp[k]==1 && generator[j]==1) || (temp[k]==0 && generator[j]==0)) {
irayya 2:c0d73ae91788 187 temp[k]=0;
irayya 2:c0d73ae91788 188 } else {
irayya 2:c0d73ae91788 189 temp[k]=1;
irayya 2:c0d73ae91788 190 }
irayya 2:c0d73ae91788 191 }
irayya 2:c0d73ae91788 192 }
irayya 2:c0d73ae91788 193 }*/
irayya 2:c0d73ae91788 194
irayya 0:b3b7346e07fe 195 //Division
irayya 0:b3b7346e07fe 196 for(i=0; i<v+rs; i++) {
irayya 0:b3b7346e07fe 197 j=0;
irayya 0:b3b7346e07fe 198 k=i;
irayya 0:b3b7346e07fe 199 if (temp[k]>=generator[j]) {
irayya 0:b3b7346e07fe 200 for(j=0,k=i; j<16; j++,k++) {
irayya 0:b3b7346e07fe 201 if((temp[k]==1 && generator[j]==1) || (temp[k]==0 && generator[j]==0)) {
irayya 0:b3b7346e07fe 202 temp[k]=0;
irayya 0:b3b7346e07fe 203 } else {
irayya 0:b3b7346e07fe 204 temp[k]=1;
irayya 0:b3b7346e07fe 205 }
irayya 0:b3b7346e07fe 206 }
irayya 0:b3b7346e07fe 207 }
irayya 0:b3b7346e07fe 208 }
irayya 0:b3b7346e07fe 209
irayya 0:b3b7346e07fe 210 output.printf("\n Reaminder: ");
irayya 0:b3b7346e07fe 211 int rrem[16];
irayya 0:b3b7346e07fe 212 for (i=v,j=0; i<v+rs; i++,j++) {
irayya 0:b3b7346e07fe 213 rrem[j]= temp[i];
irayya 0:b3b7346e07fe 214 }
irayya 0:b3b7346e07fe 215 for(i=0; i<rs; i++) {
irayya 0:b3b7346e07fe 216 output.printf("%d",rrem[i]);
irayya 0:b3b7346e07fe 217 }
irayya 0:b3b7346e07fe 218
irayya 0:b3b7346e07fe 219 int flag=0;
irayya 0:b3b7346e07fe 220 for(i=0; i<rs; i++) {
irayya 0:b3b7346e07fe 221 if(rrem[i]!=0) {
irayya 0:b3b7346e07fe 222 flag=1;
irayya 0:b3b7346e07fe 223 }
irayya 0:b3b7346e07fe 224 }
irayya 0:b3b7346e07fe 225
irayya 0:b3b7346e07fe 226 if(flag==0) {
irayya 0:b3b7346e07fe 227 //output.printf("\n Since Remainder Is 0 Hence Message Transmitted From Sender To Receriver Is Correct");
irayya 0:b3b7346e07fe 228 return 0;
irayya 1:fc4b4d69aa7a 229
irayya 0:b3b7346e07fe 230
irayya 0:b3b7346e07fe 231 } else {
irayya 1:fc4b4d69aa7a 232 // output.printf(" \n Since Remainder Is Not 0 Hence Message Transmitted From Sender To Receriver Contains Error");
irayya 1:fc4b4d69aa7a 233 return 1;
irayya 0:b3b7346e07fe 234 }
irayya 0:b3b7346e07fe 235 v=0;
irayya 0:b3b7346e07fe 236 }
irayya 0:b3b7346e07fe 237
irayya 0:b3b7346e07fe 238 void printBinary(char c)
irayya 0:b3b7346e07fe 239 {
irayya 0:b3b7346e07fe 240
irayya 2:c0d73ae91788 241 for (int i = 7; i >= 0; --i) { //binary data to be printing 8 bits for each cahracter
irayya 0:b3b7346e07fe 242 int x = ((c & (1 << i))? 1 : 0);
irayya 0:b3b7346e07fe 243 output.printf("%d",x);
irayya 0:b3b7346e07fe 244 binary1[v]=x;
irayya 0:b3b7346e07fe 245 v++;
irayya 0:b3b7346e07fe 246 }
irayya 2:c0d73ae91788 247 //printf("the value of v is given as %d",v);
irayya 0:b3b7346e07fe 248 }
irayya 0:b3b7346e07fe 249
irayya 0:b3b7346e07fe 250 void charBinary(int c1)
irayya 0:b3b7346e07fe 251 {
irayya 0:b3b7346e07fe 252 char x1 = ((c1 & 1)? '1' : '0');
irayya 0:b3b7346e07fe 253 // output.printf("%c",x1);
irayya 0:b3b7346e07fe 254 binary2[v1]=x1;
irayya 0:b3b7346e07fe 255 v1++;
irayya 0:b3b7346e07fe 256 }
irayya 0:b3b7346e07fe 257
irayya 0:b3b7346e07fe 258
irayya 0:b3b7346e07fe 259 /* ####### printChar function ########## */
irayya 0:b3b7346e07fe 260
irayya 0:b3b7346e07fe 261 void printChar(char x2[])
irayya 0:b3b7346e07fe 262 {
irayya 0:b3b7346e07fe 263
irayya 0:b3b7346e07fe 264 //memcpy(a, b, sizeof(a));
irayya 0:b3b7346e07fe 265 char *ON = x2 ;
irayya 0:b3b7346e07fe 266 output.printf("\n");
irayya 0:b3b7346e07fe 267 output.printf("%c",x2);
irayya 0:b3b7346e07fe 268 char subbuff[9];
irayya 2:c0d73ae91788 269 /*for(int j=0; j<sizeof(result1); j++) {
irayya 0:b3b7346e07fe 270 result1[j]='\0';
irayya 2:c0d73ae91788 271 }*/
irayya 0:b3b7346e07fe 272 unsigned char c3;
irayya 0:b3b7346e07fe 273 int k1;
irayya 0:b3b7346e07fe 274 int index = 0;
irayya 0:b3b7346e07fe 275 int fichero_len = (int)strlen(ON);
irayya 0:b3b7346e07fe 276
irayya 0:b3b7346e07fe 277 for(k1 = 0; k1 < fichero_len; k1 += 8) {
irayya 0:b3b7346e07fe 278 memcpy(subbuff, &ON[k1], 8); // <--- copy 8 butes only
irayya 0:b3b7346e07fe 279 subbuff[8] = '\0';
irayya 0:b3b7346e07fe 280 c3 = (unsigned char)strtol(subbuff, 0, 2);
irayya 0:b3b7346e07fe 281 //printf("\%s = %c = %d = 0x%.2X\n", subbuff, c3, c3, c3);
irayya 2:c0d73ae91788 282 result1[index] = c3;
irayya 2:c0d73ae91788 283 //result1[index] = '\0';
irayya 2:c0d73ae91788 284 index++;
irayya 0:b3b7346e07fe 285 }
irayya 0:b3b7346e07fe 286 printf("\nResult = %s\n", result1);
irayya 0:b3b7346e07fe 287
irayya 0:b3b7346e07fe 288 }