packet.c 17.8 KB
Newer Older
1 2 3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
4
#include <netinet/in.h>
5
#include <stdbool.h>
6 7 8
#include "../../util/common.h"
#include "../../util/dbg.h"
#include "../clock.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
9
#include "packet.h"
10

11
static void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
12

13
static void *encode_data_header(void *buf_ptr, const void *payload);
14

15
static void *encode_redundancy_header(void *buf_ptr, const void *payload);
16

17
static void *encode_feedback_header(void *buf_ptr, const void *payload);
18

19
static void *decode_data_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
20

21
static void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer);
22

23
static void *decode_feedback_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
24

25
prrtPacketLength_t PrrtPacket_size(PrrtPacket *packet_ptr) {
26
    return (prrtPacketLength_t) (packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
27 28
}

29
int PrrtPacket_print(PrrtPacket *packet_ptr) {
30 31
    printf(" 0                   1                   2                   3\n"
                   " 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"
32
                   "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
33
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
34

35
    printf("| %5u | %5u | %13u | %29u |\n", type, PrrtPacket_priority(packet_ptr), packet_ptr->index,
36
           packet_ptr->sequenceNumber);
37
    printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
38

39
    if (type == PACKET_TYPE_DATA) {
40
        PrrtPacketDataPayload *payload = packet_ptr->payload;
41 42
        printf("| %61u |\n", payload->timestamp);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
43
        printf("| %61u |\n", payload->groupRTprop_us);
44
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
45 46
        printf("| %61u |\n", payload->packetTimeout_us);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
47 48
        printf("| %61s |\n", (char *) (packet_ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE));
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
49
    } else if (type == PACKET_TYPE_REDUNDANCY) {
50
        PrrtPacketRedundancyPayload *payload = packet_ptr->payload;
51
        printf("| %29u | %13u | %13u |\n", payload->baseSequenceNumber, payload->n, payload->k);
52
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
53
    } else if (type == PACKET_TYPE_FEEDBACK) {
54 55
        prrtPacketLength_t i = 0;

56
        PrrtPacketFeedbackPayload *payload = packet_ptr->payload;
57

58
        printf("| %61u |\n", payload->groupRTT_us);
59
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
60
        printf("| %61u |\n", payload->forwardTripTimestamp_us);
61
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
62
        printf("| %29u | %29u |\n", payload->erasureCount, payload->packetCount);
63
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
64
        printf("| %29u | %29u |\n", payload->gapLength, payload->gapCount);
65
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
66
        printf("| %29u | %29u |\n", payload->burstLength, payload->burstCount);
67
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
68 69 70 71 72

        prrtPacketLength_t remainingSpace = ((prrtPacketLength_t) (packet_ptr->payloadLength -
                                                                   PRRT_PACKET_FEEDBACK_HEADER_SIZE));
        prrtPacketLength_t blockCount = (prrtPacketLength_t) (remainingSpace / sizeof(PrrtIncompleteBlock));

73
        for (i = 0; i < blockCount; ++i) {
74 75 76 77
            PrrtIncompleteBlock block = payload->incompleteBlocks[i];
            printf("| %29u | %29u |\n", block.sequenceNumberBase, block.repairCycleIndex);
            printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
        }
78
    } else {
79 80
        printf("| Unhandeled Type                                               |\n");
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
81 82 83 84 85
    }

    return 0;
}

86
PrrtPacket *PrrtPacket_copy(PrrtPacket *original) {
87 88 89 90 91
    __sync_fetch_and_add(&original->refcount, 1);
    return original;
}

PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original) {
92 93
    PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket));
    check_mem(newPacket);
94
    void *payload = calloc(1, original->payloadLength);
95 96 97
    check_mem(payload);

    newPacket->payload = payload;
98 99
    newPacket->payloadLength = original->payloadLength;
    memcpy(newPacket->payload, original->payload, original->payloadLength);
100 101

    newPacket->index = original->index;
102
    newPacket->sequenceNumber = original->sequenceNumber;
103 104
    newPacket->type_priority = original->type_priority;

105 106
    newPacket->refcount = 1;

107 108 109

    return newPacket;
    error:
110
    PERROR("Not enough memory for packet copies.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
111
    return NULL;
112 113
}

114 115
static PrrtPacket *
create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t size, uint8_t type, uint8_t index) {
Andreas Schmidt's avatar
Andreas Schmidt committed
116 117 118 119 120 121
    PrrtPacket *packet = calloc(1, sizeof(PrrtPacket));
    check_mem(packet);

    packet->type_priority = type << 4;
    packet->type_priority |= priority & 0x0F;
    packet->index = index;
122 123
    packet->sequenceNumber = seqno;
    packet->payloadLength = size;
124
    packet->refcount = 1;
Andreas Schmidt's avatar
Andreas Schmidt committed
125 126 127 128 129

    return packet;

    error:
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
130
    return NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
131 132
}

133
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
134
    void *payload = packet_ptr->payload;
135

136
    check(packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH <= buf_size, "Buffer too small.");
137

138 139
    buf_ptr = encode_general_header(buf_ptr, packet_ptr);

140
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
141
    if (type == PACKET_TYPE_DATA) {
142
        buf_ptr = encode_data_header(buf_ptr, payload);
143
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_DATA_HEADER_SIZE);
144
    } else if (type == PACKET_TYPE_REDUNDANCY) {
145
        buf_ptr = encode_redundancy_header(buf_ptr, payload);
146
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
147
    } else if (type == PACKET_TYPE_FEEDBACK) {
148
        encode_feedback_header(buf_ptr, payload);
149 150
    } else {
        perror("NOT IMPLEMENTED");
151
        return false;
152
    }
153
    return true;
154 155

    error:
156
    return false;
157 158
}

159
#define identity(x) (x)
160

161 162 163 164 165 166
#define PrrtPacketField_encode(payload, buf_ptr, field_t, field, conversion) \
    do { \
        field_t *ptr = (field_t *) (buf_ptr); \
        *ptr = conversion((payload)->field); \
        (buf_ptr) += sizeof(field_t); \
    } while (false)
167

168 169
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
    const PrrtPacketRedundancyPayload *redundancyPayload = payload;
170

171 172 173 174
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtSequenceNumber_t, baseSequenceNumber, htons);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, n, identity);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, k, identity);
175 176 177 178

    return buf_ptr;
}

179
void *encode_feedback_header(void *buf_ptr, const void *payload) {
180
    const PrrtPacketFeedbackPayload *feedbackPayload = payload;
181

182 183 184 185 186 187 188 189 190 191
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, groupRTT_us, htonl);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimestamp_t, forwardTripTimestamp_us, htonl);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, erasureCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, packetCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, ackSequenceNumber, htons);
192

193 194 195
    return buf_ptr;
}

196
void *encode_data_header(void *buf_ptr, const void *payload) {
197
    const PrrtPacketDataPayload *data_payload = payload;
198

199 200 201
    PrrtPacketField_encode(data_payload, buf_ptr, prrtPacketLength_t, dataLength, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, groupRTprop_us, htonl);
rna's avatar
rna committed
202
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, packetTimeout_us, htonl);
203 204 205 206

    return buf_ptr;
}

207
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
208

209 210 211
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, type_priority, identity);
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, index, identity);
    PrrtPacketField_encode(packet, buf_ptr, prrtSequenceNumber_t, sequenceNumber, htons);
212

213
    return buf_ptr;
214 215
}

216
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
217 218 219 220

    // targetPacket is uninitialized, so we need to set the reference count
    targetPacket->refcount = 1;

221
    prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
Andreas Schmidt's avatar
Andreas Schmidt committed
222 223 224 225 226
    targetPacket->type_priority = *(uint8_t *) srcBuffer;
    srcBuffer += 1;
    uint8_t *index_ptr = (uint8_t *) srcBuffer;
    targetPacket->index = *index_ptr;
    srcBuffer += 1;
227 228 229
    prrtSequenceNumber_t *seqno_prt = (prrtSequenceNumber_t *) srcBuffer;
    targetPacket->sequenceNumber = ntohs(*seqno_prt);
    srcBuffer += sizeof(prrtSequenceNumber_t);
230

Andreas Schmidt's avatar
Andreas Schmidt committed
231
    void *payload_buffer = calloc(1, payload_len);
232 233
    check_mem(payload_buffer);

Andreas Schmidt's avatar
Andreas Schmidt committed
234
    targetPacket->payload = payload_buffer;
235
    targetPacket->payloadLength = payload_len;
Andreas Schmidt's avatar
Andreas Schmidt committed
236

237
    prrtPacketType_t packetType = PrrtPacket_type(targetPacket);
238
    if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
239 240
        srcBuffer = decode_data_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_DATA_HEADER_SIZE);
241
    } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
242 243
        srcBuffer = decode_redundancy_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
244
    } else if (packetType == PACKET_TYPE_FEEDBACK) {
245
        decode_feedback_header(srcBuffer, payload_buffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
246
    } else {
247
        printf("NOT IMPLEMENTED\n");
Andreas Schmidt's avatar
Andreas Schmidt committed
248
    }
249
    return true;
250 251

    error:
252
    return false;
Andreas Schmidt's avatar
Andreas Schmidt committed
253
}
254

255 256 257 258 259 260 261
#define PrrtPacketField_decode(payload, buf_ptr, field_t, field, conversion) \
    do { \
        field_t *PrrtPacketField_decode_ptr = (field_t *) (buf_ptr); \
        (payload)->field = conversion(*PrrtPacketField_decode_ptr); \
        (buf_ptr) += sizeof(field_t); \
    } while (false)

262
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
263
    PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) srcBuffer;
264

265 266 267 268
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtSequenceNumber_t, baseSequenceNumber, ntohs);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, n, identity);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, k, identity);
269

Andreas Schmidt's avatar
Andreas Schmidt committed
270
    return dstBuffer;
271 272
}

273
void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
274
    PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) srcBuffer;
275

276 277 278 279 280 281 282 283 284 285
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, groupRTT_us, ntohl);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimestamp_t, forwardTripTimestamp_us, ntohl);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, erasureCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, packetCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, ackSequenceNumber, ntohs);
286

Andreas Schmidt's avatar
Andreas Schmidt committed
287
    return dstBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
288 289
}

290
void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
291
    PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) srcBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
292

293 294 295 296
    PrrtPacketField_decode(data_payload, dstBuffer, prrtPacketLength_t, dataLength, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, groupRTprop_us, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, packetTimeout_us, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
297

Andreas Schmidt's avatar
Andreas Schmidt committed
298
    return dstBuffer;
299 300
}

301
int PrrtPacket_destroy(PrrtPacket *packet) {
302 303 304 305 306 307 308 309 310 311

    prrtRefcount_t refcount = __sync_fetch_and_sub(&packet->refcount, 1);
    if (refcount > 1) {
        return 0;
    } else {
        if (packet->payload != NULL) {
            free(packet->payload);
        }
        free(packet);
        return 0;
312
    }
313 314
}

315
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
rna's avatar
rna committed
316
                                          prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber,
317
                                          prrtTimedelta_t targetDelay) {
Andreas Schmidt's avatar
Andreas Schmidt committed
318
    PrrtPacket *packet = create_header(priority, sequenceNumber,
319 320
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, 0);
321

322
    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
323 324
    check_mem(dataPayload);
    packet->payload = dataPayload;
325

rna's avatar
rna committed
326
    dataPayload->dataLength = dataLength;
327
    dataPayload->timestamp = PrrtClock_get_current_time_us();
328
    dataPayload->packetTimeout_us = dataPayload->timestamp + targetDelay;
329
    dataPayload->groupRTprop_us = 0;
Andreas Schmidt's avatar
Andreas Schmidt committed
330
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_DATA_HEADER_SIZE)
331

Andreas Schmidt's avatar
Andreas Schmidt committed
332
    return packet;
333

334
    error:
335
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
336
    return NULL;
337 338
}

339 340
PrrtPacket *PrrtPacket_reconstruct_data_packet(PrrtPacketDataPayload *payload, prrtIndex_t index,
                                               prrtSequenceNumber_t sequenceNumber) {
rna's avatar
rna committed
341 342 343
    prrtPacketLength_t dataLength = payload->dataLength;

    PrrtPacket *packet = create_header(0, sequenceNumber,
344 345
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, index);
rna's avatar
rna committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359

    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
    check_mem(dataPayload);
    packet->payload = dataPayload;

    PrrtPacket_copy_buffer_to_payload(packet, payload, 0)

    return packet;
    error:
    PERROR("Could not reconstruct packet.%s", "");
    return NULL;

}

360 361 362
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer,
                                                prrtPacketLength_t payloadLength,
                                                prrtSequenceNumber_t sequenceNumber, uint8_t index,
363
                                                prrtSequenceNumber_t baseSequenceNumber,
364
                                                PrrtCodingConfiguration *codingParams) {
Andreas Schmidt's avatar
Andreas Schmidt committed
365
    PrrtPacket *packet = create_header(priority, sequenceNumber,
366
                                       (prrtPacketLength_t) (payloadLength + PRRT_PACKET_REDUNDANCY_HEADER_SIZE),
Andreas Schmidt's avatar
Andreas Schmidt committed
367
                                       PACKET_TYPE_REDUNDANCY, index);
368

369
    PrrtPacketRedundancyPayload *redundancyPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
370 371
    check_mem(redundancyPayload);
    packet->payload = redundancyPayload;
372

373
    redundancyPayload->baseSequenceNumber = baseSequenceNumber;
374
    redundancyPayload->timestamp = PrrtClock_get_current_time_us();
375 376
    redundancyPayload->k = codingParams->k;
    redundancyPayload->n = codingParams->n;
Andreas Schmidt's avatar
Andreas Schmidt committed
377
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
378

Andreas Schmidt's avatar
Andreas Schmidt committed
379
    return packet;
380 381

    error:
382
    PERROR("Out of memory%s.", "");
383
    return NULL;
384 385
}

386 387 388 389 390 391 392
PrrtPacket *
PrrtPacket_create_feedback_packet(uint8_t priority, prrtSequenceNumber_t sequenceNumber, prrtTimedelta_t groupRTT,
                                  prrtSequenceNumber_t gapLength, prrtSequenceNumber_t gapCount,
                                  prrtSequenceNumber_t burstLength, prrtSequenceNumber_t burstCount,
                                  prrtTimestamp_t forwardTripTime, prrtSequenceNumber_t erasureCount,
                                  prrtSequenceNumber_t packetCount, prrtSequenceNumber_t ackSequenceNumber,
                                  prrtPacketType_t ackPacketType) {
Andreas Schmidt's avatar
Andreas Schmidt committed
393
    PrrtPacket *packet = create_header(priority, sequenceNumber, PRRT_PACKET_FEEDBACK_HEADER_SIZE, PACKET_TYPE_FEEDBACK,
394
                                       0);
Andreas Schmidt's avatar
Andreas Schmidt committed
395

396
    PrrtPacketFeedbackPayload *payload = calloc(1, packet->payloadLength);
397
    check_mem(payload);
Andreas Schmidt's avatar
Andreas Schmidt committed
398
    packet->payload = payload;
399

400 401
    payload->groupRTT_us = groupRTT;
    payload->forwardTripTimestamp_us = forwardTripTime;
Andreas Schmidt's avatar
Andreas Schmidt committed
402 403
    payload->erasureCount = erasureCount;
    payload->packetCount = packetCount;
404 405 406 407
    payload->gapLength = gapLength;
    payload->gapCount = gapCount;
    payload->burstLength = burstLength;
    payload->burstCount = burstCount;
408 409
    payload->ackSequenceNumber = ackSequenceNumber;
    payload->ackPacketType = ackPacketType;
410

Andreas Schmidt's avatar
Andreas Schmidt committed
411
    return packet;
412 413

    error:
414
    return NULL;
415
}