forked from Betschi/FANET_V2019_02_07
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanet_terminal.c
480 lines (409 loc) · 18.3 KB
/
fanet_terminal.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* fanet_terminal.c
*
* Copyright 2018 <pi@RPi3B_FANET_2>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#ifndef FANET_TERMINAL_C
#define FANET_TERMINAL_C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "fanet_struct.c"
#define KNRM "\x1B[0m" // Color: Normal
#define KRED "\x1B[31m" // Color: Red
#define KGRN "\x1B[32m" // Color: Green
#define KYEL "\x1B[33m" // Color: Yellow
#define KBLU "\x1B[34m" // Color: Blue
#define KMAG "\x1B[35m" // Color: Mangenta
#define KCYN "\x1B[36m" // Color: Cyan
#define KWHT "\x1B[37m" // Color: White
#define BOLD "\e[1m" // Bold/Bright
#define DIM "\e[2m" // Dim
#define UNDERLINE "\e[4m" // Underline
#define BLINK "\e[5m" // Blink
#define INVERT "\e[7m" // Reverse (invert the foreground and background colors)
#define RESET_ALL "\e[0m" // Reset all attributs
#define R_BOLD "\e[21m" // Reset Bold/Bright
#define R_DIM "\e[22m" // Reset Dim
#define R_UNDERLINE "\e[24m" // Reset Underline
#define R_BLINK "\e[25m" // Reset Blink
#define R_INVERT "\e[27m" // Reset Reverse (invert the foreground and background colors)
#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
typedef unsigned char byte;
/***********************************************************************
* Shows the FANET start screen
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |01.10.2018| bet | Init Version
**********************************************************************/
void terminal_start_screen (byte _sf, int _bandwith, int _freq)
{
clear();
printf (" FANET Monitor V0.1\n");
printf ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf ("| Listening at SF%i on %.6lf Mhz with a BW of %d kHz.\n", _sf,(double)_freq/1000000, _bandwith);
printf ("|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf ("| Time | RSSI | P.RSSI | SNR | CR | +-frq | Lenght| Raw Data \n");
printf ("| | E F T| Source | A C S| Destina| Signatur| Latitude | Longitude |On|A-Typ| Alt | Speed | Climb | Headin| T-Rate | Distance|\n");
printf ("| | E F T| Source | A C S| Destina| Signatur| Latitude | Longitude | Temp |Heading| Speed | Gusts | Humidity| Barometric|\n");
printf ("| | E F T| Source | A C S| Destina| Signatur| Name\n");
printf ("|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
/***********************************************************************
* Shows the RF information
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |01.10.2018| bet | Init Version
**********************************************************************/
void terminal_rf_info (boolean _rxtx, boolean _integrity, sRadioData *_radio_data, sFanetMAC *_mac_data)
{
struct timeval tv;
struct tm* ptm;
char _time_string[40];
long _milliseconds;
gettimeofday (&tv, NULL);
ptm = localtime (&tv.tv_sec);
_milliseconds = tv.tv_usec / 1000;
strftime (_time_string, sizeof (_time_string), "%Y-%m-%d %H:%M:%S", ptm);
printf (KNRM); printf("|");
// Timestamp
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf ("%s.%03ld",_time_string,_milliseconds);
printf (KNRM); printf("|");
// RSSI
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf (" %+4ddBm",_radio_data->rssi);
printf (KNRM); printf("|");
// P.RSSI
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf (" %+4ddBm",_radio_data->prssi);
printf (KNRM); printf("|");
// P.RSSI
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
if (_rxtx) printf(" -.-dB"); else printf (" %+5.1fdB",_radio_data->psnr);
printf (KNRM); printf("|");
// Coding rate (CR)
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf (" %s",_radio_data->coding_rate);
printf (KNRM); printf("|");
// Frequency error
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radio_data->crc_err || !_mac_data->valid_bit) printf(KYEL);
if (_rxtx) printf(" -Hz"); else printf (" %+6dHz", _radio_data->freq_err);
printf (KNRM); printf("|");
}
/***********************************************************************
* Shows the MAC information
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |25.12.2018| bet | Init Version
**********************************************************************/
void terminal_mac_info (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data)
{
// MAC Header
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf(" %d %d %2d", _mac_data->e_header, _mac_data->forward, _mac_data->type);
printf (KNRM); printf("|");
// MAC Source Address
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
printf(" %02x:%04x",_mac_data->s_manufactur_id, _mac_data->s_unique_id);
printf (KNRM); printf("|");
// MAC Extended Header
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
if (_mac_data->e_header) printf(" %d",_mac_data->ack); else printf (" -");
if (_mac_data->e_header) printf(" %d",_mac_data->cast); else printf (" -");
if (_mac_data->e_header) printf(" %d",_mac_data->signature_bit); else printf (" -");
printf (KNRM); printf("|");
// MAC Destination Address
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
if (_mac_data->cast) printf(" %02x:%04x",_mac_data->d_manufactur_id, _mac_data->d_unique_id); else printf (" --:----");
printf (KNRM); printf("|");
// MAC Signature
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
if (_mac_data->signature_bit) printf(" %08x",_mac_data->signature); else printf (" --------");
printf (KNRM); printf("|");
}
/***********************************************************************
* Shows the FANET message as HEX on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |13.01.2019| bet | Init Version
**********************************************************************/
void terminal_message_raw (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sRawMessage *_raw_message)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
//terminal_mac_info (_rxtx, _integrity, _mac_data);
// Raw Data
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
//printf(BOLD);
printf (" %3d",_raw_message->m_length);
printf (KNRM); printf("|");
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err || !_mac_data->valid_bit) printf(KYEL);
//printf(BOLD);
_raw_message->m_pointer=0;
while (_raw_message->m_pointer < (_raw_message->m_length))
{
printf (" %02x",_raw_message->message[_raw_message->m_pointer]);
_raw_message->m_pointer++;
}
//printf(R_BOLD);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the CRC error on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |26.01.2019| bet | Init Version
**********************************************************************/
void terminal_message_crc_err (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
// Name
printf(KYEL);
printf(INVERT);
printf(" CRC error");
printf(R_INVERT);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the MAC error on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |27.01.2019| bet | Init Version
**********************************************************************/
void terminal_message_mac_err (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
printf(KYEL);
printf(INVERT);
printf(" MAC error");
printf(R_INVERT);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the FANET message 0 (ACK) on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |13.01.2019| bet | Init Version
**********************************************************************/
void terminal_message_0 (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sACK *_ack)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
terminal_mac_info (_rxtx, _integrity, _radiodata, _mac_data);
// Name
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(INVERT);
printf(" ACK");
printf(R_INVERT);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the FANET message 1 (Tracking) on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |26.01.2019| bet | Init Version
**********************************************************************/
void terminal_message_1 (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sTRACKING *_tracking)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
terminal_mac_info (_rxtx, _integrity, _radiodata, _mac_data);
// Latitude
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %+9.5f°",_tracking->latitude);
printf (KNRM); printf("|");
// Longitude
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %+9.5f°",_tracking->longitude);
printf (KNRM); printf("|");
// Online Tracking
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %d",_tracking->tracking);
printf (KNRM); printf("|");
// Aircraft type
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %d",_tracking->aircraft_type);
printf (KNRM); printf("|");
// Altitude
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %4dm",_tracking->altitude);
printf (KNRM); printf("|");
// Speed
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %5.1fkm/h",_tracking->speed);
printf (KNRM); printf("|");
// Climb
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %+5.1fm/s",_tracking->climb);
printf (KNRM); printf("|");
// Heading
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %5.1f°",_tracking->heading);
printf (KNRM); printf("|");
// Turm rate
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_tracking->turn_rate_on) printf(" %+6.2f°/s",_tracking->turn_rate); else printf(" -.--°/s");
printf (KNRM); printf("|");
// Distance
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %6.2fkm",_tracking->distance);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the FANET message 2 (Name) on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |25.12.2018| bet | Init Version
**********************************************************************/
void terminal_message_2 (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sName *_name)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
terminal_mac_info (_rxtx, _integrity, _radiodata, _mac_data);
// Name
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
_name->name[_name->n_length]=0; // Add EOF at last
printf(" %s",_name->name);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the FANET message 3 (Message data) on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |29.12.2018| bet | Init Version
**********************************************************************/
void terminal_message_3 (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sMessage *_message)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
terminal_mac_info (_rxtx, _integrity, _radiodata, _mac_data);
// Message
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
_message->message[_message->m_length]=0; // Add EOF at last
printf(" %s",_message->message);
printf (KNRM); printf("|");
printf("\n");
}
/***********************************************************************
* Shows the FANET message 4 (Weather data) on the terminal
*
* *********************************************************************
* Ver | Date | Sign | Describtion
* ---------------------------------------------------------------------
* 0.1 |01.10.2018| bet | Init Version
**********************************************************************/
void terminal_message_4 (boolean _rxtx, boolean _integrity, sRadioData *_radiodata, sFanetMAC *_mac_data, sWeather *_weather_data)
{
terminal_rf_info (_rxtx, _integrity, _radiodata, _mac_data);
terminal_mac_info (_rxtx, _integrity, _radiodata, _mac_data);
// Latitude
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %+9.5f°",_weather_data->latitude);
printf (KNRM); printf("|");
// Longitude
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
printf(" %+9.5f°",_weather_data->longitude);
printf (KNRM); printf("|");
// Temperature
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->temp) printf(" %+5.1f°C",_weather_data->temperature); else printf(" -.-°C");
printf (KNRM); printf("|");
// Winde heading
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->wind) printf(" %5.1f°",_weather_data->wind_heading); else printf(" -.-°");
printf (KNRM); printf("|");
// Winde speed
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->wind) printf(" %5.1fkm/h",_weather_data->wind_speed); else printf(" -.-km/h");
printf (KNRM); printf("|");
// Winde gusts
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->wind) printf(" %5.1fkm/h",_weather_data->wind_gusts); else printf(" -.-km/h");
printf (KNRM); printf("|");
// Humidity
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->humid) printf(" %5.1f%%rh",_weather_data->humidity); else printf(" -.-%%rh");
printf (KNRM); printf("|");
// Barometric
if (_rxtx) printf(KCYN); else printf(KGRN);
if (_radiodata->crc_err) printf(KYEL);
if (_weather_data->barom) printf(" %7.2fhPa",_weather_data->barometric); else printf(" -.--hPa");
printf (KNRM); printf("|");
printf("\n");
}
#endif