block.c 2.96 KB
Newer Older
1
#include <string.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
2
3
4
5
#include <src/defines.h>
#include <src/prrt/vdmcode/block_code.h>
#include <stdlib.h>
#include <src/util/list.h>
6
#include "block.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
7

8
9
10
11
12
13
void clear_list(gf *const *src, uint8_t k) {
    int j = 0;
    for(j = 0; j < k; j++) {
        free(src[j]);
    }
}
Andreas Schmidt's avatar
Andreas Schmidt committed
14
15
16
17
18

void PrrtBlock_free(PrrtBlock **mblock) {

}

19
int PrrtBlock_alloc(PrrtBlock **mblock, const PrrtCodingParams *cpar) {
Andreas Schmidt's avatar
Andreas Schmidt committed
20
21
    *mblock = malloc(sizeof(PrrtBlock));

22
23
24
25
    (*mblock)->coding_params = *cpar;
    (*mblock)->data_blocks =List_create();
    (*mblock)->redundancy_blocks =List_create();

Andreas Schmidt's avatar
Andreas Schmidt committed
26
27
    return 0;
}
28

29
int PrrtBlock_insert_data_packet(PrrtBlock *block_ptr, const PrrtPacket *packet_ptr) {
30
31
32
33
34
    List_push(block_ptr->data_blocks, packet_ptr);
    block_ptr->data_count++;
    block_ptr->largest_data_length = MAX(block_ptr->largest_data_length, packet_ptr->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
    return 0;
}
35
int PrrtBlock_insert_redundancy_packet(PrrtBlock *block_ptr, const PrrtPacket *ptr) {
36
37
38
39
    List_push(block_ptr->redundancy_blocks, ptr);
    block_ptr->redundancy_count++;
    return 0;
}
40

41
int PrrtBlock_ready(const PrrtBlock *block_ptr) {
42
43
44
45
46
47
48
49
    return (block_ptr->data_count == block_ptr->coding_params.k) ? TRUE : FALSE;
}

PrrtPacket *PrrtBlock_get_first_data(PrrtBlock *block_ptr) {
    block_ptr->data_count--;
    return List_shift(block_ptr->data_blocks);
}

50
void PrrtBlock_code(PrrtBlock *block_ptr, uint16_t *seqno) {
51
    int j = 0, m = 0;
52
53
54
    uint8_t k = block_ptr->coding_params.k;
    uint8_t n = block_ptr->coding_params.n;
    uint8_t r = block_ptr->coding_params.r;
55
56
    uint8_t index = 0;
    uint32_t length = block_ptr->largest_data_length;
57
58
59
60
61
62
63
64
65
66
67

    PrrtCoder* coder = NULL;

    PrrtCoder_get_coder(&coder, n, k);

    gf** src = malloc(sizeof(gf*) * k);

    LIST_FOREACH(block_ptr->data_blocks, first, next, cur) {
        src[j] = malloc(sizeof(gf) * length);
        memset(src[j], 0, sizeof(gf) * length);
        PrrtPacket* pkt = cur->value;
68
        memcpy(src[j], pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
69
70
71
        j++;
    }

72
73
    PrrtPacket *first_packet = (PrrtPacket*) block_ptr->data_blocks->first->value;
    uint16_t base_seqno = first_packet->seqno;
74

75
76
    gf** fec = malloc(sizeof(gf*) * r);
    for(j = 0; j < r; j++) {
77
78
        fec[j] = malloc(sizeof(gf) * length);
        memset(fec[j], 0, sizeof(gf) * length);
79
        PrrtCoder_encode(coder, src, fec[j], j+k, length); // gf **src, gf *fec, int index, int sz
80
81
        PrrtPacket* red_packet_ptr = PrrtPacket_create_redundancy_packet(0, (void*) fec[j], length, *seqno, index,  base_seqno, block_ptr->coding_params);
        *seqno = *seqno+1 % SEQNO_SPACE;
82
        PrrtPacket_print(red_packet_ptr);
83
84
        PrrtBlock_insert_redundancy_packet(block_ptr, red_packet_ptr);
    }
85

86
87
    block_ptr->is_coded = TRUE;

88
89
    clear_list(src, k);
    free(src);
90
91
}

92
93
94
PrrtPacket *PrrtBlock_get_first_red_data(PrrtBlock *block_ptr) {
    block_ptr->redundancy_count--;
    return List_shift(block_ptr->redundancy_blocks);
95
}