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
/
send_ascii.c
147 lines (127 loc) · 3.21 KB
/
send_ascii.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
146
147
#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;
// converting string to arr of size_t
length = strlen(argv[2]);
if(length > 1500)
{
fprintf(stdout, "Text must not be more than 1500 characters\n");
exit(1);
}
if(length < 46) // pad with spaces
{
for(int i = length; i == 46; i++)
{
for(int j = 0; j < 8; j++)
{
frame.payload[i*8+j] = (32>>(7-j)) & 0x1;
}
}
}
for(int i = 0; i < length; i++)
{
for(int j = 0; j < 8; j++)
{
frame.payload[i*8+j] = (argv[2][i]>>(7-j)) & 0x1;
}
}
}
else
{
invalid:
fprintf(stderr, "usage: ./sender [FREQUENCY] [ASCII]\n");
exit(1);
}
state.message_length = length*8;
state.interval = interval;
state.cond = 1;
state.counter = 0;
state.output = 0;
state.frame_length = (7 + 1 + 6 + 6 + 2) * 8 + length*8 + 4 * 8;
printf("Sending thread-thread, with hardwareclock-sync and ethernet frames:\n"
"- Frequency in Hz: %zu\n"
"- Message: %s\n",
frequency, argv[2]);
// 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;
}
}