-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
65 lines (56 loc) · 1.6 KB
/
utilities.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
#include "utilities.h"
#include "ccitt16.h"
#include <stdint.h>
#include <string.h>
void build_packet(packet_t *packet, unsigned char type, char data[], unsigned char num)
{
int i, j;
unsigned char packet_raw[PKT_SIZE];
//Set type and sequence number
packet_raw[0] = type;
packet_raw[1] = num;
//set data field
for(i = PKT_DATA_OFFSET, j = 0; i < PKT_DATA_SIZE + PKT_DATA_OFFSET; ++i, ++j)
{
packet_raw[i] = data[j]; //packet[2] = data[0]; packet[3] = data[1]
}
//union to split the CRC output into two bytes.
//Suggestion taken from GedasL at http://www.avrfreaks.net/forum/c-programming-how-split-int16-bits-2-char8bit
union short_split
{
short int CRC;
unsigned char bytes[2];
}splitter;
splitter.CRC = calculate_CCITT16(packet_raw, PKT_SIZE - 2, GENERATE_CRC);
//set the two CRC bytes
packet_raw[PKT_SIZE - 1] = splitter.bytes[0];
packet_raw[PKT_SIZE - 2] = splitter.bytes[1];
memcpy(packet, packet_raw, PKT_SIZE);
}
void print_packet(packet_t *packet) {
// print packet type
switch (packet->type) {
case PKT_TYPE_DATA:
printf("[DAT");
break;
case PKT_TYPE_ACK:
printf("[ACK");
break;
case PKT_TYPE_NAK:
printf("[NAK");
break;
default:
printf("[???");
}
// print sequence number
printf("|%d|", packet->sequence_number);
// print packet data
for (int i = 0; i < PKT_DATA_SIZE; i++) {
if (packet->data[i] == '\0')
printf(" ");
else
printf("%c", packet->data[i]);
}
// print CRC
printf("|%x%x]\n", packet->crc_sum[0], packet->crc_sum[1]);
}