Newer
Older
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "packet_interface.h"
#include <arpa/inet.h>
#include <zlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/*************** Structures definition ***************/
struct __attribute__((__packed__)) pkt {
header_t header;
uint8_t payload[512];
};
typedef uint8_t header_t[32];
/************* Functions definition *************/
pkt_t* pkt_new()
{
pkt_t * toReturn = (pkt_t *) malloc(sizeof(pkt_t));
return toReturn;
}
void pkt_del(pkt_t *pkt)
{
free(pkt);
}
/**
* @brief Calculates the CRC32
*
* @param buffer : The buffer on which to calculate CRC32.
* @param len : The number of bytes of the buffer.
* @return uint32_t
*/
uint32_t calculate_crc(char * buffer, uint32_t len)
{
return (uint32_t) crc32(0, (const void *) buffer, len);
}
pkt_status_code pkt_decode_data_fec(const char *data, const size_t len, pkt_t *pkt)
{
}
pkt_status_code pkt_decode_ack_nack(const char *data, const size_t len, pkt_t *pkt)
{
if (len < 8)
return E_NOHEADER;
// REMINDER : *data will read the first byte
ptypes_t type = ((*data) & 0xC0) >> 6;
if ( type != PTYPE_ACK && type != PTYPE_NACK )
return E_TYPE;
if ( ((*data) & 0x20) != 0)
return E_TR;
uint8_t seqnum, window;
memcpy((char *) &seqnum, data+1, 1);
memcpy((char *) &window, data, 1);
window = window & 0x1F;
if (seqnum > window)
return E_SEQNUM;
// CheckSum no need to set TR at 0 as it should be at 0
char crc[4];
memcpy(crc, data+4, 4);
if (calculate_crc(data, 4) != ntohl((uint32_t) crc))
return E_CRC;
}
pkt_status_code pkt_decode(const char *data, const size_t len, pkt_t *pkt)
{
int is_ack_nack = len <= 8; // Maximum ACK or NACK size is 12 bytes
if (is_ack_nack)
return pkt_decode_ack_nack(data, len, pkt);
else
return pkt_decode_data_fec(data, len, pkt);
}
pkt_status_code pkt_encode(const pkt_t* pkt, char *buf, size_t *len)
{
/* Your code will be inserted here */
}
ptypes_t pkt_get_type (const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint8_t pkt_get_tr(const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint8_t pkt_get_window(const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint8_t pkt_get_seqnum(const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint16_t pkt_get_length(const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint32_t pkt_get_timestamp (const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint32_t pkt_get_crc1 (const pkt_t* pkt)
{
/* Your code will be inserted here */
}
uint32_t pkt_get_crc2 (const pkt_t* pkt)
{
/* Your code will be inserted here */
}
const char* pkt_get_payload(const pkt_t* pkt)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_type(pkt_t *pkt, const ptypes_t type)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_tr(pkt_t *pkt, const uint8_t tr)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_window(pkt_t *pkt, const uint8_t window)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_seqnum(pkt_t *pkt, const uint8_t seqnum)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_length(pkt_t *pkt, const uint16_t length)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_timestamp(pkt_t *pkt, const uint32_t timestamp)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_crc1(pkt_t *pkt, const uint32_t crc1)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_crc2(pkt_t *pkt, const uint32_t crc2)
{
/* Your code will be inserted here */
}
pkt_status_code pkt_set_payload(pkt_t *pkt,
const char *data,
const uint16_t length)
{
/* Your code will be inserted here */
}
ssize_t varuint_decode(const uint8_t *data, const size_t len, uint16_t *retval)
{
/* Your code will be inserted here */
}
ssize_t varuint_encode(uint16_t val, uint8_t *data, const size_t len)
{
/* Your code will be inserted here */
}
size_t varuint_len(const uint8_t *data)
{
/* Your code will be inserted here */
}
ssize_t varuint_predict_len(uint16_t val)
{
/* Your code will be inserted here */
}
ssize_t predict_header_length(const pkt_t *pkt)
{
/* Your code will be inserted here */
}