-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.c
93 lines (79 loc) · 2.54 KB
/
solution.c
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
// Soultion for Data packet corruption detection using CRC-16
#include <stdio.h>
#include <stdint.h>
#define MAX_PACKET_DATA_LENGTH (50)
typedef struct data_packet_t {
uint8_t id;
uint8_t data_length;
uint8_t data[MAX_PACKET_DATA_LENGTH];
uint16_t crc;
} data_packet_t;
uint16_t crc16_table[256];
void generate_crc16_table() {
uint16_t polynomial = 0x1021;
for (uint16_t i = 0; i < 256; i++) {
uint16_t c = i << 8;
for (size_t j = 0; j < 8; j++) {
if (c & 0x8000) {
c = (c << 1) ^ polynomial;
} else {
c <<= 1;
}
}
crc16_table[i] = c;
}
}
uint16_t calculate_crc(data_packet_t *packet) {
uint16_t crc = 0xFFFF;
crc = crc16_table[(crc >> 8 ^ packet->id) & 0xFF] ^ (crc << 8);
crc = crc16_table[(crc >> 8 ^ packet->data_length) & 0xFF] ^ (crc << 8);
for (int i = 0; i < packet->data_length; i++) {
crc = crc16_table[(crc >> 8 ^ packet->data[i]) & 0xFF] ^ (crc << 8);
}
return ~crc;
}
int is_corrupted(data_packet_t *packet) {
return packet->crc != calculate_crc(packet);
}
void print_packet(data_packet_t *packet) {
printf("Packet ID: %d\n", packet->id);
printf("Data length: %d\n", packet->data_length);
printf("Data: ");
for (int i = 0; i < packet->data_length; i++) {
printf("%c", packet->data[i]);
}
printf("\n");
printf("CRC: %04X\n", packet->crc);
}
int main() {
generate_crc16_table();
data_packet_t packets[] = {
{1, 4, {'t', 'e', 's', 't'}, 0},
{2, 5, {'h', 'e', 'l', 'l', 'o'}, 0}
//{3, 6, {'w', 'o', 'r', 'l', 'd', '!'}, 0},
//{4,7,{'p','a','c','k','e','t','s'},0}
};
int num_packets = sizeof(packets) / sizeof(packets[0]);
for (int i = 0; i < num_packets; i++) {
packets[i].crc = calculate_crc(&packets[i]);
printf("Transmitted data packet %d:\n", i + 1);
print_packet(&packets[i]);
//printf("\n");
printf("---------------------");
printf("\n");
}
printf("\n");
// corrupt the second packet
packets[1].data[2] = 't';
packets[1].data[3] = 'f';
for (int i = 0; i < num_packets; i++) {
printf("Received data packet %d:\n", i + 1);
print_packet(&packets[i]);
printf("Calculated CRC: %04X\n", calculate_crc(&packets[i]));
printf("Packet is %s\n", is_corrupted(&packets[i]) ? "corrupted" : "not corrupted");
printf("-----------------------------");
//printf("\n");
printf("\n");
}
return 0;
}