forward_packet_table.c 2.01 KB
Newer Older
1
2
#include <stdint.h>
#include <string.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
3
4
#include <src/defines.h>
#include <src/prrt/packet.h>
5
#include "forward_packet_table.h"
6

7

8
int is_position_relevant(const PrrtForwardPacketTable *fpt_ptr, uint16_t seqno) {
9
10
11
12
13
14
15
16
17
18
19
20
    uint16_t stop = (uint16_t) (SEQNO_SPACE / 2 + fpt_ptr->start);
    if (fpt_ptr->start < stop && !(fpt_ptr->start <= seqno && seqno <= stop)) {
        return FALSE;
    } else if (!(seqno <= stop || fpt_ptr->start <= seqno)) {
        return FALSE;
    } else {
        uint16_t which_byte = (uint16_t) (seqno / 32);
        uint16_t which_bit = (uint16_t) (seqno % 32);
        return ((fpt_ptr->data[which_byte] >> which_bit) & 0x01) == 0;
    }
}

21
22
23
24
void move_start(PrrtForwardPacketTable *fpt_ptr) {
    uint16_t seqno = fpt_ptr->start;
    uint16_t which_byte;
    uint16_t which_bit;
25

26
27
28
    while(1) {
        which_byte = (uint16_t) (seqno / 32);
        which_bit = (uint16_t) (seqno % 32);
29
        if (((fpt_ptr->data[which_byte] >> which_bit ) & 0x01) == 0) {
30
31
32
33
34
            break;
        }
        fpt_ptr->data[which_byte] &= ~(1 << which_bit);
        seqno++;
    }
35

36
37
38
39
40
41
42
43
44
    fpt_ptr->start = seqno;
}

int PrrtForwardPacketTable_create(PrrtForwardPacketTable *fpt_prt) {
    fpt_prt->start = 1;
    memset(fpt_prt->data, 0, sizeof(fpt_prt->data));
}

int PrrtForwardPacketTable_test_set_is_number_relevant(PrrtForwardPacketTable *fpt_ptr, uint16_t seqno) {
45
    int res = is_position_relevant(fpt_ptr, seqno);
46
47

    if(res) {
48
49
50
51
52
53
54
        uint16_t which_byte = (uint16_t) (seqno / 32);
        uint16_t which_bit = (uint16_t) (seqno % 32);
        fpt_ptr->data[which_byte] |= 1 << which_bit;

        if(seqno == fpt_ptr->start) {
            move_start(fpt_ptr);
        }
55
56
57
58
59
60
61
62
63
    }

    return res;
}

int PrrtForwardPacketTable_test_is_block_relevant(PrrtForwardPacketTable * forwardPacketTable, uint16_t start, uint16_t length) {
    // TODO: implement efficiently
    int res = FALSE;
    int i;
64

65
    for (i = 0; i < length; i++) {
66
        res = res || is_position_relevant(forwardPacketTable, (uint16_t) (start + i));
67
    }
68
69

    return res;
70
}