-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
251 lines (200 loc) · 6.33 KB
/
Copy pathmain.cpp
File metadata and controls
251 lines (200 loc) · 6.33 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include "config.h"
#include "Settings.h"
#include "rs_codec/RSCodec.h"
#include "activity_signals/activity_led.h"
#include "activity_signals/UdpActivity.h"
#include "generic_structures.h"
#include "system_dialogators/tun/TUNInterface.h"
#include "system_dialogators/uart/UARTHandler.h"
#include "packetizers/Packetizer.h"
#include "radio/RadioInterface.h"
#include "radio/dualrf24/DualRF24.h"
#include "radio/singlerf24/SingleRF24.h"
#include "radio/picorf24/PicoRF24.h"
#include "radio/uartrf/UARTRF.h"
#include "packetizers/harq/HARQ.h"
#include "packetizers/arq/ARQ.h"
#include "packetizers/latency_evaluator/LatencyEvaluator.h"
// Thread naming
#include <sys/prctl.h>
// Catch CTRL+C Event
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
// Exit conditions
#include <mutex>
#include <condition_variable>
#include <functional>
std::mutex sleep_mutex;
std::condition_variable cv;
bool ready_to_exit = false;
bool stop_program = false;
void test(){
//UARTHandler ua("/dev/ttyACM0");
/*unsigned long msecs = 0;
while (true) {
if (current_millis() > msecs + 1000) {
msecs = current_millis();
printf("Mbps: %f\n", (ua.receivedbytes * 8) / 1000000.0f);
ua.receivedbytes = 0;
}
std::this_thread::yield();
}*/
Settings S;
PicoRF24 prf = PicoRF24();
prf.apply_settings(S);
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
prf.apply_settings(S);
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
}
void catch_setups() {
/*Do this early in your program's initialization */
signal(SIGABRT, [](int signal_number) {
/*Your code goes here. You can output debugging info.
If you return from this function, and it was called
because abort() was called, your program will exit or crash anyway
(with a dialog box on Windows).
*/
stop_program=true;
cv.notify_all();
std::cout << "SIGABORT caught. Ouch.\n";
});
// CTRL+C Handler
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = [](int s) {
std::cout << "Caught a signal, number:\t" << (int) s << std::endl;
if (s == 2) {
printf("Caught exit signal\n");
// TODO: Handle signals gracefully
stop_program=true;
cv.notify_all();
}
};
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
}
Packetizer<TunMessage, RFMessage>* select_packetizer_from_settings(const Settings &settings) {
Settings::packetizers_available psel = settings.find_packetizer_index();
switch (psel) {
default:
std::cout << "Packetizer not found in code. Did you forget to register it in the main function?\n";
throw std::invalid_argument("Invalid packetizer in switch case");
exit(1);
break;
case Settings::packetizers_available::harq:
return new HARQ();
break;
case Settings::packetizers_available::latency_evaluator:
return new LatencyEvaluator();
break;
case Settings::packetizers_available::arq:
return new ARQ();
break;
}
}
RadioInterface* select_radio_from_settings(const Settings &settings) {
Settings::radios_available rsel = settings.find_radio_index();
switch (rsel) {
default:
std::cout << "Radio not found in code. Did you forget to register it in the main function?\n";
throw std::invalid_argument("Invalid radio in switch case");
exit(1);
break;
case Settings::radios_available::dualrf24:
return new DualRF24();
break;
case Settings::radios_available::singlerf24:
return new SingleRF24();
break;
case Settings::radios_available::picorf24:
return new PicoRF24();
break;
case Settings::radios_available::uartrf:
return new UARTRF();
break;
}
}
int main(int argc, char **argv) {
catch_setups();
prctl(PR_SET_NAME, "Main Thread", 0, 0, 0);
//test();
//return 0;
Settings settings;
if (geteuid())
std::cout << "Not running as root, make sure you have the required privileges to access SPI ( ) and the network interfaces (CAP_NET_ADMIN) \n";
std::cout << "rf24tunlink 2 BETA\t" << "Version " << rf24tunlink2_VERSION_MAJOR << "." << rf24tunlink2_VERSION_MINOR << std::endl;
if (argc < 2) {
std::cout << "You need to include at least one settings file (./thisprogram settings.txt more_settings.txt more_more_settings.txt)\nPrinting tunlink_default.txt for you in the working directory\n";
settings.to_file("tunlink_default.txt");
return (0);
}
std::function<void()> read_settings_function([&] {
for (int i = 1; i < argc; i++)
settings.read_settings(argv[i]);
settings.print_all_settings(std::cout, false);
});
read_settings_function();
RSCodec RSC(settings);
ActivitySignalInterface* actl = nullptr;
if(settings.use_activity_led)
actl = new ActivityLed(settings);
if(settings.use_udp_activity){
actl = new UdpActivity(settings);
}
TUNInterface TUNI;
RadioInterface* radio = select_radio_from_settings(settings);
Packetizer<TunMessage, RFMessage>* packetizer = select_packetizer_from_settings(settings);
if(actl) packetizer->register_activity_led(actl);
packetizer->register_rsc(&RSC);
packetizer->register_tun(&TUNI);
packetizer->register_radio(radio);
radio->register_packet_target(packetizer->expose_radio_receiver());
TUNI.register_packet_target(packetizer->expose_tun_receiver());
PacketMessageFactory PMF(settings);
std::function<void()> reload_settings_function([&] {
RSC.apply_settings(settings);
// MTU can only be calcd after applying the settings
packetizer->apply_settings(settings);
settings.mtu = packetizer->get_mtu();
PMF = PacketMessageFactory(settings);
packetizer->register_message_factory(&PMF);
radio->register_message_factory(&PMF);
TUNI.apply_settings(settings); // The read thread will start here
radio->apply_settings(settings);
});
reload_settings_function();
// If needed, reload at runtime
//read_settings_function();
//reload_settings_function();
//while(!stop_program){
// printf("Type 'r' to reload settings, type 'q' to quit\n");
//std::this_thread::sleep_for(1000ms);
// char a;
// cin >> a;
// switch (a) {
// case 'q':
// stop_program = true;
// break;
// case 'r':
// read_settings_function();
// reload_settings_function();
// break;
// }
// std::this_thread::sleep_for(std::chrono::microseconds(1000));
//}
{
std::unique_lock lock(sleep_mutex);
cv.wait(lock, [&]{return stop_program;});
}
TUNI.stop();
radio->stop();
packetizer->stop();
actl->stop();
delete packetizer;
delete radio;
delete actl;
return 0;
}