forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direwolf.c
1113 lines (870 loc) · 30.5 KB
/
direwolf.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: direwolf.c
*
* Purpose: Main program for "Dire Wolf" which includes:
*
* AFSK modem using the "sound card."
* AX.25 encoder/decoder.
* APRS data encoder / decoder.
* APRS digipeater.
* KISS TNC emulator.
* APRStt (touch tone input) gateway
* Internet Gateway (IGate)
*
*
*---------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <getopt.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#if __ARM__
//#include <asm/hwcap.h>
//#include <sys/auxv.h> // Doesn't seem to be there.
// We have libc 2.13. Looks like we might need 2.17 & gcc 4.8
#endif
#if __WIN32__
#else
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __OpenBSD__
#include <soundcard.h>
#elif __APPLE__
#else
#include <sys/soundcard.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#if USE_HAMLIB
#include <hamlib/rig.h>
#endif
#define DIREWOLF_C 1
#include "direwolf.h"
#include "version.h"
#include "audio.h"
#include "config.h"
#include "multi_modem.h"
#include "demod.h"
#include "hdlc_rec.h"
#include "hdlc_rec2.h"
#include "ax25_pad.h"
#include "decode_aprs.h"
#include "textcolor.h"
#include "server.h"
#include "kiss.h"
#include "kissnet.h"
#include "kiss_frame.h"
#include "nmea.h"
#include "gen_tone.h"
#include "digipeater.h"
#include "tq.h"
#include "xmit.h"
#include "ptt.h"
#include "beacon.h"
#include "redecode.h"
#include "dtmf.h"
#include "aprs_tt.h"
#include "tt_user.h"
#include "igate.h"
#include "symbols.h"
#include "dwgps.h"
#include "log.h"
#include "recv.h"
#include "morse.h"
//static int idx_decoded = 0;
#if __WIN32__
static BOOL cleanup_win (int);
#else
static void cleanup_linux (int);
#endif
static void usage (char **argv);
#if defined(__SSE__) && !defined(__APPLE__)
static void __cpuid(int cpuinfo[4], int infotype){
__asm__ __volatile__ (
"cpuid":
"=a" (cpuinfo[0]),
"=b" (cpuinfo[1]),
"=c" (cpuinfo[2]),
"=d" (cpuinfo[3]):
"a" (infotype)
);
}
#endif
/*-------------------------------------------------------------------
*
* Name: main
*
* Purpose: Main program for packet radio virtual TNC.
*
* Inputs: Command line arguments.
* See usage message for details.
*
* Outputs: Decoded information is written to stdout.
*
* A socket and pseudo terminal are created for
* for communication with other applications.
*
*--------------------------------------------------------------------*/
static struct audio_s audio_config;
static struct tt_config_s tt_config;
struct digi_config_s digi_config;
static int d_u_opt = 0; /* "-d u" command line option to print UTF-8 also in hexadecimal. */
static int d_p_opt = 0; /* "-d p" option for dumping packets over radio. */
static int q_h_opt = 0; /* "-q h" Quiet, suppress the "heard" line with audio level. */
static int q_d_opt = 0; /* "-q d" Quiet, suppress the decoding of APRS packets. */
static struct misc_config_s misc_config;
int main (int argc, char *argv[])
{
int err;
//int eof;
int j;
char config_file[100];
int xmit_calibrate_option = 0;
int enable_pseudo_terminal = 0;
struct digi_config_s digi_config;
struct igate_config_s igate_config;
int r_opt = 0, n_opt = 0, b_opt = 0, B_opt = 0, D_opt = 0; /* Command line options. */
char P_opt[16];
char l_opt[80];
char input_file[80];
int t_opt = 1; /* Text color option. */
int a_opt = 0; /* "-a n" interval, in seconds, for audio statistics report. 0 for none. */
int d_k_opt = 0; /* "-d k" option for serial port KISS. Can be repeated for more detail. */
int d_n_opt = 0; /* "-d n" option for Network KISS. Can be repeated for more detail. */
int d_t_opt = 0; /* "-d t" option for Tracker. Can be repeated for more detail. */
int d_g_opt = 0; /* "-d g" option for GPS. Can be repeated for more detail. */
int d_o_opt = 0; /* "-d o" option for output control such as PTT and DCD. */
int d_i_opt = 0; /* "-d i" option for IGate. Repeat for more detail */
#if USE_HAMLIB
int d_h_opt = 0; /* "-d h" option for hamlib debugging. Repeat for more detail */
#endif
strlcpy(l_opt, "", sizeof(l_opt));
strlcpy(P_opt, "", sizeof(P_opt));
#if __WIN32__
// Select UTF-8 code page for console output.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms686036(v=vs.85).aspx
// This is the default I see for windows terminal:
// >chcp
// Active code page: 437
//Restore on exit? oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
#else
/*
* Default on Raspian & Ubuntu Linux is fine. Don't know about others.
*
* Should we look at LANG environment variable and issue a warning
* if it doesn't look something like en_US.UTF-8 ?
*/
#endif
/*
* Pre-scan the command line options for the text color option.
* We need to set this before any text output.
*/
t_opt = 1; /* 1 = normal, 0 = no text colors. */
for (j=1; j<argc-1; j++) {
if (strcmp(argv[j], "-t") == 0) {
t_opt = atoi (argv[j+1]);
//dw_printf ("DEBUG: text color option = %d.\n", t_opt);
}
}
// TODO: control development/beta/release by version.h instead of changing here.
// Print platform. This will provide more information when people send a copy the information displayed.
text_color_init(t_opt);
text_color_set(DW_COLOR_INFO);
dw_printf ("Dire Wolf version %d.%d (%s) Beta Test\n", MAJOR_VERSION, MINOR_VERSION, __DATE__);
//dw_printf ("Dire Wolf DEVELOPMENT version %d.%d %s (%s)\n", MAJOR_VERSION, MINOR_VERSION, "K", __DATE__);
//dw_printf ("Dire Wolf version %d.%d\n", MAJOR_VERSION, MINOR_VERSION);
#if defined(ENABLE_GPSD) || defined(USE_HAMLIB)
dw_printf ("Includes optional support for: ");
#if defined(ENABLE_GPSD)
dw_printf (" gpsd");
#endif
#if defined(USE_HAMLIB)
dw_printf (" hamlib");
#endif
dw_printf ("\n");
#endif
#if __WIN32__
SetConsoleCtrlHandler ((PHANDLER_ROUTINE)cleanup_win, TRUE);
#else
setlinebuf (stdout);
signal (SIGINT, cleanup_linux);
#endif
/*
* Starting with version 0.9, the prebuilt Windows version
* requires a minimum of a Pentium 3 or equivalent so we can
* use the SSE instructions.
* Try to warn anyone using a CPU from the previous
* century rather than just dying for no apparent reason.
*
* Apple computers with Intel processors started with P6. Since the
* cpu test code was giving Clang compiler grief it has been excluded.
*
* Now, where can I find a Pentium 2 or earlier to test this?
*/
#if defined(__SSE__) && !defined(__APPLE__)
int cpuinfo[4];
__cpuid (cpuinfo, 0);
if (cpuinfo[0] >= 1) {
__cpuid (cpuinfo, 1);
//dw_printf ("debug: cpuinfo = %x, %x, %x, %x\n", cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
if ( ! ( cpuinfo[3] & (1 << 25))) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("------------------------------------------------------------------\n");
dw_printf ("This version requires a minimum of a Pentium 3 or equivalent.\n");
dw_printf ("If you are seeing this message, you are probably using a computer\n");
dw_printf ("from the previous century. See comments in Makefile.win for\n");
dw_printf ("information on how you can recompile it for use with your antique.\n");
dw_printf ("------------------------------------------------------------------\n");
}
}
text_color_set(DW_COLOR_INFO);
#endif
/*
* Default location of configuration file is current directory.
* Can be overridden by -c command line option.
* TODO: Automatically search other places.
*/
strlcpy (config_file, "direwolf.conf", sizeof(config_file));
/*
* Look at command line options.
* So far, the only one is the configuration file location.
*/
strlcpy (input_file, "", sizeof(input_file));
while (1) {
//int this_option_optind = optind ? optind : 1;
int option_index = 0;
int c;
char *p;
static struct option long_options[] = {
{"future1", 1, 0, 0},
{"future2", 0, 0, 0},
{"future3", 1, 0, 'c'},
{0, 0, 0, 0}
};
/* ':' following option character means arg is required. */
c = getopt_long(argc, argv, "P:B:D:c:pxr:b:n:d:q:t:Ul:Sa:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0: /* possible future use */
text_color_set(DW_COLOR_DEBUG);
dw_printf("option %s", long_options[option_index].name);
if (optarg) {
dw_printf(" with arg %s", optarg);
}
dw_printf("\n");
break;
case 'a': /* -a for audio statistics interval */
a_opt = atoi(optarg);
if (a_opt < 0) a_opt = 0;
if (a_opt < 10) {
text_color_set(DW_COLOR_ERROR);
dw_printf("Setting such a small audio statistics interval will produce inaccurate sample rate display.\n");
}
break;
case 'c': /* -c for configuration file name */
strlcpy (config_file, optarg, sizeof(config_file));
break;
#if __WIN32__
#else
case 'p': /* -p enable pseudo terminal */
/* We want this to be off by default because it hangs */
/* eventually when nothing is reading from other side. */
enable_pseudo_terminal = 1;
break;
#endif
case 'B': /* -B baud rate and modem properties. */
B_opt = atoi(optarg);
if (B_opt < 100 || B_opt > 10000) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Use a more reasonable data baud rate in range of 100 - 10000.\n");
exit (EXIT_FAILURE);
}
break;
case 'P': /* -P for modem profile. */
//debug: dw_printf ("Demodulator profile set to \"%s\"\n", optarg);
strlcpy (P_opt, optarg, sizeof(P_opt));
break;
case 'D': /* -D decrease AFSK demodulator sample rate */
D_opt = atoi(optarg);
if (D_opt < 1 || D_opt > 8) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Crazy value for -D. \n");
exit (EXIT_FAILURE);
}
break;
case 'x': /* -x for transmit calibration tones. */
xmit_calibrate_option = 1;
break;
case 'r': /* -r audio samples/sec. e.g. 44100 */
r_opt = atoi(optarg);
if (r_opt < MIN_SAMPLES_PER_SEC || r_opt > MAX_SAMPLES_PER_SEC)
{
text_color_set(DW_COLOR_ERROR);
dw_printf("-r option, audio samples/sec, is out of range.\n");
r_opt = 0;
}
break;
case 'n': /* -n number of audio channels for first audio device. 1 or 2. */
n_opt = atoi(optarg);
if (n_opt < 1 || n_opt > 2)
{
text_color_set(DW_COLOR_ERROR);
dw_printf("-n option, number of audio channels, is out of range.\n");
n_opt = 0;
}
break;
case 'b': /* -b bits per sample. 8 or 16. */
b_opt = atoi(optarg);
if (b_opt != 8 && b_opt != 16)
{
text_color_set(DW_COLOR_ERROR);
dw_printf("-b option, bits per sample, must be 8 or 16.\n");
b_opt = 0;
}
break;
case '?':
/* Unknown option message was already printed. */
usage (argv);
break;
case 'd': /* Set debug option. */
/* New in 1.1. Can combine multiple such as "-d pkk" */
for (p=optarg; *p!='\0'; p++) {
switch (*p) {
case 'a': server_set_debug(1); break;
case 'k': d_k_opt++; kiss_serial_set_debug (d_k_opt); break;
case 'n': d_n_opt++; kiss_net_set_debug (d_n_opt); break;
case 'u': d_u_opt = 1; break;
// separate out gps & waypoints.
case 'g': d_g_opt++; break;
case 't': d_t_opt++; beacon_tracker_set_debug (d_t_opt); break;
case 'w': nmea_set_debug (1); break; // not documented yet.
case 'p': d_p_opt = 1; break; // TODO: packet dump for xmit side.
case 'o': d_o_opt++; ptt_set_debug(d_o_opt); break;
case 'i': d_i_opt++; break;
#if AX25MEMDEBUG
case 'm': ax25memdebug_set(); break; // Track down memory leak. Not documented.
#endif
#if USE_HAMLIB
case 'h': d_h_opt++; break; // Hamlib verbose level.
#endif
default: break;
}
}
break;
case 'q': /* Set quiet option. */
/* New in 1.2. Quiet option to suppress some types of printing. */
/* Can combine multiple such as "-q hd" */
for (p=optarg; *p!='\0'; p++) {
switch (*p) {
case 'h': q_h_opt = 1; break;
case 'd': q_d_opt = 1; break;
default: break;
}
}
break;
case 't': /* Was handled earlier. */
break;
case 'U': /* Print UTF-8 test and exit. */
dw_printf ("\n UTF-8 test string: ma%c%cana %c%c F%c%c%c%ce\n\n",
0xc3, 0xb1,
0xc2, 0xb0,
0xc3, 0xbc, 0xc3, 0x9f);
exit (0);
break;
case 'l': /* -l for log file directory name */
strlcpy (l_opt, optarg, sizeof(l_opt));
break;
case 'S': /* Print symbol tables and exit. */
symbols_init ();
symbols_list ();
exit (0);
break;
default:
/* Should not be here. */
text_color_set(DW_COLOR_DEBUG);
dw_printf("?? getopt returned character code 0%o ??\n", c);
usage (argv);
}
} /* end while(1) for options */
if (optind < argc)
{
if (optind < argc - 1)
{
text_color_set(DW_COLOR_ERROR);
dw_printf ("Warning: File(s) beyond the first are ignored.\n");
}
strlcpy (input_file, argv[optind], sizeof(input_file));
}
/*
* Get all types of configuration settings from configuration file.
*
* Possibly override some by command line options.
*/
#if USE_HAMLIB
rig_set_debug(d_h_opt);
#endif
symbols_init ();
config_init (config_file, &audio_config, &digi_config, &tt_config, &igate_config, &misc_config);
if (r_opt != 0) {
audio_config.adev[0].samples_per_sec = r_opt;
}
if (n_opt != 0) {
audio_config.adev[0].num_channels = n_opt;
if (n_opt == 2) {
audio_config.achan[1].valid = 1;
}
}
if (b_opt != 0) {
audio_config.adev[0].bits_per_sample = b_opt;
}
if (B_opt != 0) {
audio_config.achan[0].baud = B_opt;
if (audio_config.achan[0].baud < 600) {
audio_config.achan[0].modem_type = MODEM_AFSK;
audio_config.achan[0].mark_freq = 1600;
audio_config.achan[0].space_freq = 1800;
audio_config.achan[0].decimate = 3;
}
else if (audio_config.achan[0].baud > 2400) {
audio_config.achan[0].modem_type = MODEM_SCRAMBLE;
audio_config.achan[0].mark_freq = 0;
audio_config.achan[0].space_freq = 0;
}
else {
audio_config.achan[0].modem_type = MODEM_AFSK;
audio_config.achan[0].mark_freq = 1200;
audio_config.achan[0].space_freq = 2200;
}
}
audio_config.statistics_interval = a_opt;
if (strlen(P_opt) > 0) {
/* -P for modem profile. */
/* TODO: Not yet documented. Should probably since it is consistent with atest. */
strlcpy (audio_config.achan[0].profiles, P_opt, sizeof(audio_config.achan[0].profiles));
}
if (D_opt != 0) {
// Reduce audio sampling rate to reduce CPU requirements.
audio_config.achan[0].decimate = D_opt;
}
if (strlen(l_opt) > 0) {
strlcpy (misc_config.logdir, l_opt, sizeof(misc_config.logdir));
}
misc_config.enable_kiss_pt = enable_pseudo_terminal;
if (strlen(input_file) > 0) {
strlcpy (audio_config.adev[0].adevice_in, input_file, sizeof(audio_config.adev[0].adevice_in));
}
/*
* Open the audio source
* - soundcard
* - stdin
* - UDP
* Files not supported at this time.
* Can always "cat" the file and pipe it into stdin.
*/
err = audio_open (&audio_config);
if (err < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Pointless to continue without audio device.\n");
SLEEP_SEC(5);
exit (1);
}
/*
* Initialize the AFSK demodulator and HDLC decoder.
*/
multi_modem_init (&audio_config);
/*
* Initialize the touch tone decoder & APRStt gateway.
*/
dtmf_init (&audio_config);
aprs_tt_init (&tt_config);
tt_user_init (&audio_config, &tt_config);
/*
* Should there be an option for audio output level?
* Note: This is not the same as a volume control you would see on the screen.
* It is the range of the digital sound representation.
*/
gen_tone_init (&audio_config, 100);
morse_init (&audio_config, 100);
assert (audio_config.adev[0].bits_per_sample == 8 || audio_config.adev[0].bits_per_sample == 16);
assert (audio_config.adev[0].num_channels == 1 || audio_config.adev[0].num_channels == 2);
assert (audio_config.adev[0].samples_per_sec >= MIN_SAMPLES_PER_SEC && audio_config.adev[0].samples_per_sec <= MAX_SAMPLES_PER_SEC);
/*
* Initialize the transmit queue.
*/
xmit_init (&audio_config, d_p_opt);
/*
* If -x option specified, transmit alternating tones for transmitter
* audio level adjustment, up to 1 minute then quit.
* TODO: enhance for more than one channel.
*/
if (xmit_calibrate_option) {
int max_duration = 60; /* seconds */
int n = audio_config.achan[0].baud * max_duration;
int chan = 0;
text_color_set(DW_COLOR_INFO);
dw_printf ("\nSending transmit calibration tones. Press control-C to terminate.\n");
ptt_set (OCTYPE_PTT, chan, 1);
while (n-- > 0) {
tone_gen_put_bit (chan, n & 1);
}
ptt_set (OCTYPE_PTT, chan, 0);
exit (0);
}
/*
* Initialize the digipeater and IGate functions.
*/
digipeater_init (&audio_config, &digi_config);
igate_init (&audio_config, &igate_config, &digi_config, d_i_opt);
/*
* Provide the AGW & KISS socket interfaces for use by a client application.
*/
server_init (&audio_config, &misc_config);
kissnet_init (&misc_config);
/*
* Create a pseudo terminal and KISS TNC emulator.
*/
kiss_init (&misc_config);
kiss_frame_init (&audio_config);
/*
* Open port for communication with GPS.
*/
dwgps_init (&misc_config, d_g_opt);
nmea_init (&misc_config); // TODO: revisit.
/*
* Create thread for trying to salvage frames with bad FCS.
*/
redecode_init (&audio_config);
/*
* Enable beaconing.
* Open log file first because "-dttt" (along with -l...) will
* log the tracker beacon transmissions with fake channel 999.
*/
log_init(misc_config.logdir);
beacon_init (&audio_config, &misc_config);
/*
* Get sound samples and decode them.
* Use hot attribute for all functions called for every audio sample.
*/
recv_init (&audio_config);
recv_process ();
exit (EXIT_SUCCESS);
}
/*-------------------------------------------------------------------
*
* Name: app_process_rec_frame
*
* Purpose: This is called when we receive a frame with a valid
* FCS and acceptable size.
*
* Inputs: chan - Audio channel number, 0 or 1.
* subchan - Which modem caught it.
* Special case -1 for DTMF decoder.
* slice - Slicer which caught it.
* pp - Packet handle.
* alevel - Audio level, range of 0 - 100.
* (Special case, use negative to skip
* display of audio level line.
* Use -2 to indicate DTMF message.)
* retries - Level of bit correction used.
* spectrum - Display of how well multiple decoders did.
*
*
* Description: Print decoded packet.
* Optionally send to another application.
*
*--------------------------------------------------------------------*/
// TODO: Use only one printf per line so output doesn't get jumbled up with stuff from other threads.
void app_process_rec_packet (int chan, int subchan, int slice, packet_t pp, alevel_t alevel, retry_t retries, char *spectrum)
{
char stemp[500];
unsigned char *pinfo;
int info_len;
char heard[AX25_MAX_ADDR_LEN];
//int j;
int h;
char display_retries[32];
assert (chan >= 0 && chan < MAX_CHANS);
assert (subchan >= -1 && subchan < MAX_SUBCHANS);
assert (slice >= 0 && slice < MAX_SLICERS);
assert (pp != NULL); // 1.1J+
strlcpy (display_retries, "", sizeof(display_retries));
if (audio_config.achan[chan].fix_bits != RETRY_NONE || audio_config.achan[chan].passall) {
snprintf (display_retries, sizeof(display_retries), " [%s] ", retry_text[(int)retries]);
}
ax25_format_addrs (pp, stemp);
info_len = ax25_get_info (pp, &pinfo);
/* Print so we can see what is going on. */
/* Display audio input level. */
/* Who are we hearing? Original station or digipeater. */
if (ax25_get_num_addr(pp) == 0) {
/* Not AX.25. No station to display below. */
h = -1;
strlcpy (heard, "", sizeof(heard));
}
else {
h = ax25_get_heard(pp);
ax25_get_addr_with_ssid(pp, h, heard);
}
text_color_set(DW_COLOR_DEBUG);
dw_printf ("\n");
if (( ! q_h_opt ) && alevel.rec >= 0) { /* suppress if "-q h" option */
if (h != -1 && h != AX25_SOURCE) {
dw_printf ("Digipeater ");
}
char alevel_text[AX25_ALEVEL_TO_TEXT_SIZE];
ax25_alevel_to_text (alevel, alevel_text);
/* As suggested by KJ4ERJ, if we are receiving from */
/* WIDEn-0, it is quite likely (but not guaranteed), that */
/* we are actually hearing the preceding station in the path. */
if (h >= AX25_REPEATER_2 &&
strncmp(heard, "WIDE", 4) == 0 &&
isdigit(heard[4]) &&
heard[5] == '\0') {
char probably_really[AX25_MAX_ADDR_LEN];
ax25_get_addr_with_ssid(pp, h-1, probably_really);
dw_printf ("%s (probably %s) audio level = %s %s %s\n", heard, probably_really, alevel_text, display_retries, spectrum);
}
else if (strcmp(heard, "DTMF") == 0) {
dw_printf ("%s audio level = %s tt\n", heard, alevel_text);
}
else {
dw_printf ("%s audio level = %s %s %s\n", heard, alevel_text, display_retries, spectrum);
}
}
/* Version 1.2: Cranking the input level way up produces 199. */
/* Keeping it under 100 gives us plenty of headroom to avoid saturation. */
// TODO: suppress this message if not using soundcard input.
// i.e. we have no control over the situation when using SDR.
if (alevel.rec > 110) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Audio input level is too high. Reduce so most stations are around 50.\n");
}
// Display non-APRS packets in a different color.
// Display subchannel only when multiple modems configured for channel.
// -1 for APRStt DTMF decoder.
if (subchan == -1) {
text_color_set(DW_COLOR_REC);
dw_printf ("[%d.dtmf] ", chan);
}
else {
if (ax25_is_aprs(pp)) {
text_color_set(DW_COLOR_REC);
}
else {
text_color_set(DW_COLOR_DEBUG);
}
if (audio_config.achan[chan].num_subchan > 1 && audio_config.achan[chan].num_slicers == 1) {
dw_printf ("[%d.%d] ", chan, subchan);
}
else if (audio_config.achan[chan].num_subchan == 1 && audio_config.achan[chan].num_slicers > 1) {
dw_printf ("[%d.%d] ", chan, slice);
}
else if (audio_config.achan[chan].num_subchan > 1 && audio_config.achan[chan].num_slicers > 1) {
dw_printf ("[%d.%d.%d] ", chan, subchan, slice);
}
else {
dw_printf ("[%d] ", chan);
}
}
dw_printf ("%s", stemp); /* stations followed by : */
// for APRS we generally want to display non-ASCII to see UTF-8.
// for other, probably want to restrict to ASCII only because we are
// more likely to have compressed data than UTF-8 text.
// TODO: Might want to use d_u_opt for transmitted frames too.
ax25_safe_print ((char *)pinfo, info_len, ( ! ax25_is_aprs(pp)) && ( ! d_u_opt) );
dw_printf ("\n");
// Also display in pure ASCII if non-ASCII characters and "-d u" option specified.
if (d_u_opt) {
unsigned char *p;
int n = 0;
for (p = pinfo; *p != '\0'; p++) {
if (*p >= 0x80) n++;
}
if (n > 0) {
text_color_set(DW_COLOR_DEBUG);
ax25_safe_print ((char *)pinfo, info_len, 1);
dw_printf ("\n");
}
}
/* Optional hex dump of packet. */
if (d_p_opt) {
text_color_set(DW_COLOR_DEBUG);
dw_printf ("------\n");
ax25_hex_dump (pp);
dw_printf ("------\n");
}
/* Decode the contents of APRS frames and display in human-readable form. */
/* Suppress decoding if "-q d" option used. */
if ( ( ! q_d_opt ) && ax25_is_aprs(pp)) {
decode_aprs_t A;
decode_aprs (&A, pp, 0);
//Print it all out in human readable format.
decode_aprs_print (&A);
/*
* Perform validity check on each address.
* This should print an error message if any issues.
*/
(void)ax25_check_addresses(pp);
// Send to log file.
log_write (chan, &A, pp, alevel, retries);
// Convert to NMEA waypoint sentence if we have a location.
if (A.g_lat != G_UNKNOWN && A.g_lon != G_UNKNOWN) {
nmea_send_waypoint (strlen(A.g_name) > 0 ? A.g_name : A.g_src,
A.g_lat, A.g_lon, A.g_symbol_table, A.g_symbol_code,
DW_FEET_TO_METERS(A.g_altitude_ft), A.g_course, DW_MPH_TO_KNOTS(A.g_speed_mph),
A.g_comment);
}
}
/* Send to another application if connected. */
// TODO1.3: Put a wrapper around this so we only call one function to send by all methods.
int flen;
unsigned char fbuf[AX25_MAX_PACKET_LEN];
flen = ax25_pack(pp, fbuf);
server_send_rec_packet (chan, pp, fbuf, flen);
kissnet_send_rec_packet (chan, fbuf, flen);
kiss_send_rec_packet (chan, fbuf, flen);
/*
* If it came from DTMF decoder, send it to APRStt gateway.
* Otherwise, it is a candidate for IGate and digipeater.
*
* TODO: It might be useful to have some way to simulate touch tone
* sequences with BEACON sendto=R... for testing.
*/
if (subchan == -1) {
if (tt_config.gateway_enabled && info_len >= 2) {
aprs_tt_sequence (chan, (char*)(pinfo+1));
}
}
else {
/* Send to Internet server if option is enabled. */
/* Consider only those with correct CRC. */
if (ax25_is_aprs(pp) && retries == RETRY_NONE) {
igate_send_rec_packet (chan, pp);
}
/* Send out a regenerated copy. Applies to all types, not just APRS. */
/* This was an experimental feature never documented in the User Guide. */
/* Initial feedback was positive but it fell by the wayside. */
/* Should follow up with testers and either document this or clean out the clutter. */
digi_regen (chan, pp);
/*