This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.c
145 lines (122 loc) · 3.06 KB
/
sender.c
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
#include "lib.h"
#include "crc.h"
struct field
{
size_t sync[7 * 8];
size_t delimiter[8];
size_t mac_addr[6 * 8];
size_t mac_src[6 * 8];
size_t length[2 * 8];
size_t payload[(1500 + 4) * 8];
};
struct field frame;
struct state
{
size_t output;
size_t cond;
size_t message_length;
size_t frame_length;
size_t interval;
size_t counter;
};
struct state state;
size_t length, frequency, interval;
void signal_handler(int _);
int main(int argc, char* argv[])
{
if(argc == 3)
{
frequency = strtoll(argv[1], NULL, 10);
// interval in usecs
interval = 1000000/frequency;
length = strtoll(argv[2], NULL, 10);
if(length==0) goto invalid;
}
else
{
invalid:
fprintf(stderr, "usage: ./sender [FREQUENCY] [PACKETLENGTH]\n");
exit(1);
}
state.message_length = length;
state.interval = interval;
state.cond = 1;
state.counter = 0;
state.output = 0;
state.frame_length = (7 + 1 + 6 + 6 + 2) * 8 + length + 4 * 8;
printf("Sending thread-thread, with hardwareclock-sync and ethernet frames:\n"
"- Frequency in Hz: %zu\n"
"- Packet Length: %zu\n",
frequency, length);
// randomise message
srand(time(0));
for(int i = 0; i < length; i++)
{
frame.payload[i] = rand() % 2;
}
// clear predictions for earlier packets
FILE* f_pred = fopen("pred.txt", "w");
fprintf(f_pred, "");
fclose(f_pred);
// write message to file
FILE* f_act = fopen("act.txt", "w");
for(int i = 0; i < length; i++)
{
fprintf(f_act, "%lu\n", frame.payload[i]);
}
fclose(f_act);
// set sync (and part of delimit)
memset(frame.sync, 0lu, 8 * 8 * sizeof(size_t));
for(int i = 0; i < 8 * 8; i += 2)
{
frame.sync[i] = 1;
}
// set delimiter
frame.delimiter[7] = 1;
// setting addresses not required here
// set length
uint16_t length = state.message_length;
// sending the length little-endian
for(int i = 0; i < 16; i++)
{
frame.length[i] = (length>>(15-i)) & 0x1;
}
// set checksum
crc checksum = crcFast((const unsigned char*)frame.mac_addr, sizeof(size_t) * ((6+6+2)*8 + state.message_length));
// sending the checksum little-endian
for(int i = 0; i < 32; i++)
{
frame.payload[state.message_length+i] = (checksum>>(31-i)) & 0x1;
}
// start signal handler
signal(SIGALRM, signal_handler);
// alarm every communication cycle
ualarm(interval, interval);
loop:
while(state.cond)
{
if(state.output)
{
maccess(function + 64);
}
else
{
flush(function + 64);
}
}
printf("Sent over frame\n");
state.cond = 1;
state.counter = 0;
goto loop;
}
void signal_handler(int _)
{
// update symbol
state.output = frame.sync[state.counter];
state.counter++;
// check if frame is fully sent
if(state.counter == state.frame_length)
{
state.cond = 0;
}
}