-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocolFT.h
More file actions
96 lines (79 loc) · 1.54 KB
/
protocolFT.h
File metadata and controls
96 lines (79 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
//informations inside header
#define DATA_ARRAY_SIZE 1024 // 256 bytes of data in one packet
struct Header
{
unsigned long int fileSize;
unsigned long int dataSize;
unsigned long int sequenceNo;
unsigned int sendNo;
};
struct Packet
{
struct Header head;
char dataArray[DATA_ARRAY_SIZE];
unsigned char crc[2];
};
//crc function
//this is only converting 1 byte, so put all array in a for loop for get crc
void crcgenerator(unsigned char *byte, unsigned long int size,unsigned char *crc)
{
unsigned int D[16];
int i,j,k;
for(int i=0;i<16;i++)
{
D[i] = 0;
}
for ( i = 0; i < size; i++)
{ //converting byte to bits
int bit[8];
for ( j = 7; j >= 0; j--) //i =7 becoz ,8 bit are creating from 1 byte
{
bit[j] = (byte[i] >> j) & 1;
}
int feedback=0;
for(k =0; k < 8; k++)
{
//printf("%d",bit[i]);
feedback = bit[k] ^ D[15];
D[15] = D[14] ^ feedback;
D[14] = D[13];
D[13] = D[12];
D[12] = D[11];
D[11] = D[10] ^ feedback;
D[10] = D[9];
D[9] = D[8];
D[8] = D[7];
D[7] = D[6];
D[6] = D[5];
D[5] = D[4];
D[4] = D[3] ^ feedback;
D[3] = D[2];
D[2] = D[1];
D[1] = D[0];
D[0] = feedback;
}
}
/* //for testing
for(i=15;i>=0;i--)
{
printf("%d",D[i]);
}
printf("\n");
*/
unsigned int checksum=0;
for(i = 7; i >= 0; i--)
{
checksum += D[i]*pow(2,i);
}
crc[0] = checksum;
checksum = 0;
for(i = 15; i >= 8; i--)
{
checksum += D[i]*pow(2,i-8);
}
crc[1] = checksum;
}