-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
105 lines (76 loc) · 2.2 KB
/
util.cpp
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
#include "util.h"
#include <iostream>
using namespace std;
MSG_TYPE crc (MSG_TYPE package) {
int size = 0;
MSG_TYPE gen;
MSG_TYPE checker;
MSG_TYPE package_temp = package;
for (int i=0; i<FULL_PACK_SIZE; i++) {
if (package_temp % 2)
size = i;
package_temp >>= 1;
}
//cout << "size: " << size << endl;
gen = CRC_GEN_POL << (size - CRC_SIZE);
//cout << "gen: " << gen << endl;
checker = 1 << size;
//cout << "checker: " << checker << endl;
for (int i=0; i<(size - CRC_SIZE + 1); i++) {
if (package >= checker)
package ^= gen;
gen >>= 1;
checker >>= 1;
}
return package;
}
MSG_TYPE apply_error (MSG_TYPE package, float prob_error) {
int i;
float error;
MSG_TYPE temp;
srand(RAND_SEED);
//cout << "[ApplyError] Error chance: " << prob_error << endl;
for (i=FULL_PACK_SIZE-1;i>=0;i--) {
error = (((float)(rand()%10000))/10000);
if (error < prob_error) {
// get the current bit mask
temp = 0x1 << i;
// inverted the current bit
package = package ^ temp;
}
//cout << "Erro: " << error << endl;
}
return package;
}
void message_pretty_print (MSG_TYPE msg, ID_TYPE id_size) {
cout << "Message: " << (EXTRACT_MSG_FROM_MSG(msg, id_size)) << " | " << (EXTRACT_ID_FROM_MSG(msg, id_size)) << endl;
}
void alarm_dummy (int dummy) {};
float stopnwait_eff () {
float no, na, nf, pf, t_total, r, efficiencySW;
no = (FULL_PACK_SIZE - PACK_ID_SIZE - CRC_SIZE);
na = no;
nf = FULL_PACK_SIZE;
t_total = (2 * MAX_DELAY);
r = (FULL_PACK_SIZE/MAX_DELAY);
pf = PROB_ERROR;
efficiencySW = ( (1 - ( no / nf ) ) / (1 + ( na / nf ) + ( (t_total * r) / nf ) ) ) * (1 - pf);
return efficiencySW;
}
float gobackn_eff () {
float no, nf, pf, ws, efficiencyGB;
no = (FULL_PACK_SIZE - PACK_ID_SIZE - CRC_SIZE);
nf = FULL_PACK_SIZE;
pf = PROB_ERROR;
ws = WINDOW_SIZE;
efficiencyGB = ( (1 - ( no / nf) ) / (1 + (ws - 1) * pf) ) * (1 - pf);
return efficiencyGB;
}
float selectiverepeat_eff () {
float no, nf, pf, efficiencySR;
no = (FULL_PACK_SIZE - PACK_ID_SIZE - CRC_SIZE);
nf = FULL_PACK_SIZE;
pf = PROB_ERROR;
efficiencySR = ( (1 - ( no / nf ) ) * (1 - pf) );
return efficiencySR;
}