forked from wb2osz/direwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aprs_tt.c
2013 lines (1604 loc) · 53.2 KB
/
aprs_tt.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) 2013, 2014, 2015 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: aprs_tt.c
*
* Purpose: First half of APRStt gateway.
*
* Description: This file contains functions to parse the tone sequences
* and extract meaning from them.
*
* tt_user.c maintains information about users and
* generates the APRS Object Reports.
*
*
* References: This is based upon APRStt (TM) documents with some
* artistic freedom.
*
* http://www.aprs.org/aprstt.html
*
*---------------------------------------------------------------*/
#define APRS_TT_C 1
// TODO: clean up terminolgy.
// "Message" has a specific meaning in APRS and this is not it.
// Touch Tone sequence should be appropriate.
// What do we call the parts separated by * key? Field.
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include "direwolf.h"
#include "version.h"
#include "ax25_pad.h"
#include "hdlc_rec2.h" /* for process_rec_frame */
#include "textcolor.h"
#include "aprs_tt.h"
#include "tt_text.h"
#include "tt_user.h"
#include "symbols.h"
#include "latlong.h"
#include "dlq.h"
#include "demod.h" /* for alevel_t & demod_get_audio_level() */
#include "tq.h"
// geotranz
#include "utm.h"
#include "mgrs.h"
#include "usng.h"
#include "error_string.h"
/* Convert between degrees and radians. */
#define D2R(d) ((d) * M_PI / 180.)
#define R2D(r) ((r) * 180. / M_PI)
/*
* Touch Tone sequences are accumulated here until # terminator found.
* Kept separate for each audio channel so the gateway CAN be listening
* on multiple channels at the same time.
*/
#define MAX_MSG_LEN 100
static char msg_str[MAX_CHANS][MAX_MSG_LEN+1];
static int msg_len[MAX_CHANS];
static int parse_fields (char *msg);
static int parse_callsign (char *e);
static int parse_object_name (char *e);
static int parse_symbol (char *e);
static int parse_aprstt3_call (char *e);
static int parse_location (char *e);
static int parse_comment (char *e);
static int expand_macro (char *e);
static void raw_tt_data_to_app (int chan, char *msg);
static int find_ttloc_match (char *e, char *xstr, char *ystr, char *zstr, char *bstr, char *dstr, size_t valstrsize);
#if TT_MAIN
static void check_result (void);
#endif
/*------------------------------------------------------------------
*
* Name: aprs_tt_init
*
* Purpose: Initialize the APRStt gateway at system startup time.
*
* Inputs: Configuration options gathered by config.c.
*
* Global out: Make our own local copy of the structure here.
*
* Returns: None
*
* Description: The main program needs to call this at application
* start up time after reading the configuration file.
*
* TT_MAIN is defined for unit testing.
*
*----------------------------------------------------------------*/
static struct tt_config_s tt_config;
#if TT_MAIN
#define NUM_TEST_CONFIG (sizeof(test_config) / sizeof (struct ttloc_s))
static struct ttloc_s test_config[] = {
{ TTLOC_POINT, "B01", .point.lat = 12.25, .point.lon = 56.25 },
{ TTLOC_POINT, "B988", .point.lat = 12.50, .point.lon = 56.50 },
{ TTLOC_VECTOR, "B5bbbdddd", .vector.lat = 53., .vector.lon = -1., .vector.scale = 1000. }, /* km units */
/* Hilltop Tower http://www.aprs.org/aprs-jamboree-2013.html */
{ TTLOC_VECTOR, "B5bbbddd", .vector.lat = 37+55.37/60., .vector.lon = -(81+7.86/60.), .vector.scale = 16.09344 }, /* .01 mile units */
{ TTLOC_GRID, "B2xxyy", .grid.lat0 = 12.00, .grid.lon0 = 56.00,
.grid.lat9 = 12.99, .grid.lon9 = 56.99 },
{ TTLOC_GRID, "Byyyxxx", .grid.lat0 = 37 + 50./60.0, .grid.lon0 = 81,
.grid.lat9 = 37 + 59.99/60.0, .grid.lon9 = 81 + 9.99/60.0 },
{ TTLOC_MHEAD, "BAxxxxxx", .mhead.prefix = "326129" },
{ TTLOC_SATSQ, "BAxxxx" },
{ TTLOC_MACRO, "xxyyy", .macro.definition = "B9xx*AB166*AA2B4C5B3B0Ayyy" },
{ TTLOC_MACRO, "xxxxzzzzzzzzzz", .macro.definition = "BAxxxx*ACzzzzzzzzzz" },
};
#endif
void aprs_tt_init (struct tt_config_s *p)
{
int c;
#if TT_MAIN
/* For unit testing. */
memset (&tt_config, 0, sizeof(struct tt_config_s));
tt_config.ttloc_size = NUM_TEST_CONFIG;
tt_config.ttloc_ptr = test_config;
tt_config.ttloc_len = NUM_TEST_CONFIG;
/* Don't care about xmit timing or corral here. */
#else
// TODO: Keep ptr instead of making a copy.
memcpy (&tt_config, p, sizeof(struct tt_config_s));
#endif
for (c=0; c<MAX_CHANS; c++) {
msg_len[c] = 0;
msg_str[c][0] = '\0';
}
}
/*------------------------------------------------------------------
*
* Name: aprs_tt_button
*
* Purpose: Process one received button press.
*
* Inputs: chan - Audio channel it came from.
*
* button 0123456789ABCD*# - Received button press.
* $ - No activity timeout.
* space - Quiet time filler.
*
* Returns: None
*
* Description: Individual key presses are accumulated here until
* the # message terminator is found.
* The complete message is then processed.
* The touch tone decoder sends $ if no activity
* for some amount of time, perhaps 5 seconds.
* A partially accumulated messge is discarded if
* there is a long gap.
*
* '.' means no activity during processing period.
* space, between blocks, shouldn't get here.
*
*----------------------------------------------------------------*/
#ifndef TT_MAIN
void aprs_tt_button (int chan, char button)
{
static int poll_period = 0;
assert (chan >= 0 && chan < MAX_CHANS);
//if (button != '.') {
// dw_printf ("aprs_tt_button (%d, '%c')\n", chan, button);
//}
// TODO: Might make more sense to put timeout here rather in the dtmf decoder.
if (button == '$') {
/* Timeout reset. */
msg_len[chan] = 0;
msg_str[chan][0] = '\0';
}
else if (button != '.' && button != ' ') {
if (msg_len[chan] < MAX_MSG_LEN) {
msg_str[chan][msg_len[chan]++] = button;
msg_str[chan][msg_len[chan]] = '\0';
}
if (button == '#') {
/*
* Put into the receive queue like any other packet.
* This way they are all processed by the common receive thread
* rather than the thread associated with the particular audio device.
*/
raw_tt_data_to_app (chan, msg_str[chan]);
msg_len[chan] = 0;
msg_str[chan][0] = '\0';
}
}
else {
/*
* Idle time. Poll occasionally for processing.
* Timing would be off we we are listening to more than
* one channel so do this only for the one specified
* in the TTOBJ command.
*/
if (chan == tt_config.obj_recv_chan) {
poll_period++;
if (poll_period >= 39) {
poll_period = 0;
tt_user_background ();
}
}
}
} /* end aprs_tt_button */
#endif
/*------------------------------------------------------------------
*
* Name: aprs_tt_sequence
*
* Purpose: Process complete received touch tone sequence
* terminated by #.
*
* Inputs: chan - Audio channel it came from.
*
* msg - String of DTMF buttons.
* # should be the final character.
*
* Returns: None
*
* Description: Process a complete tone sequence.
* It should have one or more fields separated by *
* and terminated by a final # like these:
*
* callsign #
* entry1 * callsign #
* entry1 * entry * callsign #
*
* Limitation: Has one set of static data for communication among
* group of functions. This shouldn't be a problem
* when receiving on multiple channels at once
* because they get serialized thru the receive packet queue.
*
*----------------------------------------------------------------*/
static char m_callsign[20]; /* really object name */
/*
* Standard APRStt has symbol code 'A' (box) with overlay of 0-9, A-Z.
*
* Dire Wolf extension allows:
* Symbol table '/' (primary), any symbol code.
* Symbol table '\' (alternate), any symbol code.
* Alternate table symbol code, overlay of 0-9, A-Z.
*/
static char m_symtab_or_overlay;
static char m_symbol_code; // Default 'A'
static char m_loc_text[24];
static double m_longitude; // Set to G_UNKNOWN if not defined.
static double m_latitude; // Set to G_UNKNOWN if not defined.
static int m_ambiguity;
static char m_comment[200];
static char m_freq[12];
static char m_ctcss[8];
static char m_mic_e;
static char m_dao[6];
static int m_ssid; // Default 12 for APRStt user.
void aprs_tt_sequence (int chan, char *msg)
{
int err;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("\n\"%s\"\n", msg);
#endif
/*
* Discard empty message.
* In case # is there as optional start.
*/
if (msg[0] == '#') return;
/*
* The parse functions will fill these in.
*/
strlcpy (m_callsign, "", sizeof(m_callsign));
m_symtab_or_overlay = APRSTT_DEFAULT_SYMTAB;
m_symbol_code = APRSTT_DEFAULT_SYMBOL;
strlcpy (m_loc_text, "", sizeof(m_loc_text));
m_longitude = G_UNKNOWN;
m_latitude = G_UNKNOWN;
m_ambiguity = 0;
strlcpy (m_comment, "", sizeof(m_comment));
strlcpy (m_freq, "", sizeof(m_freq));
strlcpy (m_ctcss, "", sizeof(m_ctcss));
m_mic_e = ' ';
strlcpy (m_dao, "!T !", sizeof(m_dao)); /* start out unknown */
m_ssid = 12;
/*
* Parse the touch tone sequence.
*/
err = parse_fields (msg);
#if defined(DEBUG)
text_color_set(DW_COLOR_DEBUG);
dw_printf ("callsign=\"%s\", ssid=%d, symbol=\"%c%c\", freq=\"%s\", ctcss=\"%s\", comment=\"%s\", lat=%.4f, lon=%.4f, dao=\"%s\"\n",
m_callsign, m_ssid, m_symtab_or_overlay, m_symbol_code, m_freq, m_ctcss, m_comment, m_latitude, m_longitude, m_dao);
#endif
#if TT_MAIN
check_result (); // for unit testing.
#else
/*
* If digested successfully. Add to our list of users and schedule transmissions.
*/
if (err == 0) {
err = tt_user_heard (m_callsign, m_ssid, m_symtab_or_overlay, m_symbol_code,
m_loc_text, m_latitude, m_longitude, m_ambiguity,
m_freq, m_ctcss, m_comment, m_mic_e, m_dao);
}
/*
* If a command / script was supplied, run it now.
* This can do additional processing and provide a custom audible response.
* This is done only for the success case.
* It might be useful to run it for error cases as well but we currently
* don't pass in the success / failure code to know the difference.
*/
char script_response[1000];
strlcpy (script_response, "", sizeof(script_response));
if (err == 0 && strlen(tt_config.ttcmd) > 0) {
dw_run_cmd (tt_config.ttcmd, 1, script_response, sizeof(script_response));
}
/*
* Send response to user by constructing packet with SPEECH or MORSE as destination.
* Source shouldn't matter because it doesn't get transmitted as AX.25 frame.
* Use high priority queue for consistent timing.
*
* Anything from script, above, will override other predefined responses.
*/
char audible_response[1000];
snprintf (audible_response, sizeof(audible_response),
"APRSTT>%s:%s",
tt_config.response[err].method,
(strlen(script_response) > 0) ? script_response : tt_config.response[err].mtext);
packet_t pp;
pp = ax25_from_text (audible_response, 0);
if (pp == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Internal error. Couldn't make frame from \"%s\"\n", audible_response);
return;
}
tq_append (chan, TQ_PRIO_0_HI, pp);
#endif /* ifndef TT_MAIN */
} /* end aprs_tt_sequence */
/*------------------------------------------------------------------
*
* Name: parse_fields
*
* Purpose: Separate the complete string of touch tone characters
* into fields, delimited by *, and process each.
*
* Inputs: msg - String of DTMF buttons.
*
* Returns: None
*
* Description: It should have one or more fields separated by *.
*
* callsign #
* entry1 * callsign #
* entry1 * entry * callsign #
*
* Note that this will be used recursively when macros
* are expanded.
*
* "To iterate is human, to recurse divine."
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*
*----------------------------------------------------------------*/
static int parse_fields (char *msg)
{
char stemp[MAX_MSG_LEN+1];
char *e;
char *save;
int err;
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("parse_fields (%s).\n", msg);
strlcpy (stemp, msg, sizeof(stemp));
e = strtok_r (stemp, "*#", &save);
while (e != NULL) {
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("parse_fields () field = %s\n", e);
switch (*e) {
case 'A':
switch (e[1]) {
case 'A': /* AA object-name */
err = parse_object_name (e);
if (err != 0) return (err);
break;
case 'B': /* AB symbol */
err = parse_symbol (e);
if (err != 0) return (err);
break;
case 'C': /* AC new-style-callsign */
err = parse_aprstt3_call (e);
if (err != 0) return (err);
break;
default: /* Traditional style call or suffix */
err = parse_callsign (e);
if (err != 0) return (err);
break;
}
break;
case 'B':
err = parse_location (e);
if (err != 0) return (err);
break;
case 'C':
err = parse_comment (e);
if (err != 0) return (err);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
err = expand_macro (e);
if (err != 0) return (err);
break;
case '\0':
/* Empty field. Just ignore it. */
/* This would happen if someone uses a leading *. */
break;
default:
text_color_set(DW_COLOR_ERROR);
dw_printf ("Field does not start with A, B, C, or digit: \"%s\"\n", msg);
return (TT_ERROR_D_MSG);
}
e = strtok_r (NULL, "*#", &save);
}
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("parse_fields () normal return\n");
return (0);
} /* end parse_fields */
/*------------------------------------------------------------------
*
* Name: expand_macro
*
* Purpose: Expand compact form "macro" to full format then process.
*
* Inputs: e - An "entry" extracted from a complete
* APRStt messsage.
* In this case, it should contain only digits.
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*
* Description: Separate out the fields, perform substitution,
* call parse_fields for processing.
*
*
* Future: Generalize this to allow any lower case letter for substitution?
*
*----------------------------------------------------------------*/
#define VALSTRSIZE 20
static int expand_macro (char *e)
{
//int len;
int ipat;
char xstr[VALSTRSIZE], ystr[VALSTRSIZE], zstr[VALSTRSIZE], bstr[VALSTRSIZE], dstr[VALSTRSIZE];
char stemp[MAX_MSG_LEN+1];
char *d;
text_color_set(DW_COLOR_DEBUG);
dw_printf ("Macro tone sequence: '%s'\n", e);
//len = strlen(e);
ipat = find_ttloc_match (e, xstr, ystr, zstr, bstr, dstr, VALSTRSIZE);
if (ipat >= 0) {
// Why did we print b & d here?
// Documentation says only x, y, z can be used with macros.
// Only those 3 are processed below.
//dw_printf ("Matched pattern %3d: '%s', x=%s, y=%s, z=%s, b=%s, d=%s\n", ipat, tt_config.ttloc_ptr[ipat].pattern, xstr, ystr, zstr, bstr, dstr);
dw_printf ("Matched pattern %3d: '%s', x=%s, y=%s, z=%s\n", ipat, tt_config.ttloc_ptr[ipat].pattern, xstr, ystr, zstr);
dw_printf ("Replace with: '%s'\n", tt_config.ttloc_ptr[ipat].macro.definition);
if (tt_config.ttloc_ptr[ipat].type != TTLOC_MACRO) {
/* Found match to a different type. Really shouldn't be here. */
/* Print internal error message... */
dw_printf ("expand_macro: type != TTLOC_MACRO\n");
return (TT_ERROR_INTERNAL);
}
/*
* We found a match for the length and any fixed digits.
* Substitute values in to the definition.
*/
strlcpy (stemp, "", sizeof(stemp));
for (d = tt_config.ttloc_ptr[ipat].macro.definition; *d != '\0'; d++) {
while (( *d == 'x' || *d == 'y' || *d == 'z') && *d == d[1]) {
/* Collapse adjacent matching substitution characters. */
d++;
}
switch (*d) {
case 'x':
strlcat (stemp, xstr, sizeof(stemp));
break;
case 'y':
strlcat (stemp, ystr, sizeof(stemp));
break;
case 'z':
strlcat (stemp, zstr, sizeof(stemp));
break;
default:
{
char c1[2];
c1[0] = *d;
c1[1] = '\0';
strlcat (stemp, c1, sizeof(stemp));
}
break;
}
}
/*
* Process as if we heard this over the air.
*/
dw_printf ("After substitution: '%s'\n", stemp);
return (parse_fields (stemp));
}
else {
/* Send reject sound. */
/* Does not match any macro definitions. */
text_color_set(DW_COLOR_ERROR);
dw_printf ("Tone sequence did not match any pattern\n");
return (TT_ERROR_MACRO_NOMATCH);
}
/* should be unreachable */
return (0);
}
/*------------------------------------------------------------------
*
* Name: parse_callsign
*
* Purpose: Extract traditional format callsign or object name from touch tone sequence.
*
* Inputs: e - An "entry" extracted from a complete
* APRStt messsage.
* In this case, it should start with "A".
*
* Outputs: m_callsign
*
* m_symtab_or_overlay - Set to 0-9 or A-Z if specified.
*
* m_symbol_code - Always set to 'A'.
* NO! This should be applied only if we
* have the default value at this point.
* The symbol might have been explicitly
* set already and we don't want to overwrite that.
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*
* Description: We recognize 3 different formats:
*
* Annn - 3 digits are a tactical callsign. No overlay.
*
* Annnvk - Abbreviation with 3 digits, numeric overlay, checksum.
* Annnvvk - Abbreviation with 3 digits, letter overlay, checksum.
*
* Att...ttvk - Full callsign in two key method, numeric overlay, checksum.
* Att...ttvvk - Full callsign in two key method, letter overlay, checksum.
*
*
*----------------------------------------------------------------*/
static int checksum_not_ok (char *str, int len, char found)
{
int i;
int sum;
char expected;
sum = 0;
for (i=0; i<len; i++) {
if (isdigit(str[i])) {
sum += str[i] - '0';
}
else if (str[i] >= 'A' && str[i] <= 'D') {
sum += str[i] - 'A' + 10;
}
else {
text_color_set(DW_COLOR_ERROR);
dw_printf ("aprs_tt: checksum: bad character \"%c\" in checksum calculation!\n", str[i]);
}
}
expected = '0' + (sum % 10);
if (expected != found) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Bad checksum for \"%.*s\". Expected %c but received %c.\n", len, str, expected, found);
return (TT_ERROR_BAD_CHECKSUM);
}
return (0);
}
static int parse_callsign (char *e)
{
int len;
char tttemp[40], stemp[30];
assert (*e == 'A');
len = strlen(e);
/*
* special case: 3 digit tactical call.
*/
if (len == 4 && isdigit(e[1]) && isdigit(e[2]) && isdigit(e[3])) {
strlcpy (m_callsign, e+1, sizeof(m_callsign));
return (0);
}
/*
* 3 digit abbreviation: We only do the parsing here.
* Another part of application will try to find corresponding full call.
*/
if ((len == 6 && isdigit(e[1]) && isdigit(e[2]) && isdigit(e[3]) && isdigit(e[4]) && isdigit(e[5])) ||
(len == 7 && isdigit(e[1]) && isdigit(e[2]) && isdigit(e[3]) && isdigit(e[4]) && isupper(e[5]) && isdigit(e[6]))) {
int cs_err = checksum_not_ok (e+1, len-2, e[len-1]);
if (cs_err != 0) {
return (cs_err);
}
strncpy (m_callsign, e+1, 3);
m_callsign[3] = '\0';
if (len == 7) {
tttemp[0] = e[len-3];
tttemp[1] = e[len-2];
tttemp[2] = '\0';
tt_two_key_to_text (tttemp, 0, stemp);
m_symbol_code = APRSTT_DEFAULT_SYMBOL;
m_symtab_or_overlay = stemp[0];
}
else {
m_symbol_code = APRSTT_DEFAULT_SYMBOL;
m_symtab_or_overlay = e[len-2];
}
return (0);
}
/*
* Callsign in two key format.
*/
if (len >= 7 && len <= 24) {
int cs_err = checksum_not_ok (e+1, len-2, e[len-1]);
if (cs_err != 0) {
return (cs_err);
}
if (isupper(e[len-2])) {
strncpy (tttemp, e+1, len-4);
tttemp[len-4] = '\0';
tt_two_key_to_text (tttemp, 0, m_callsign);
tttemp[0] = e[len-3];
tttemp[1] = e[len-2];
tttemp[2] = '\0';
tt_two_key_to_text (tttemp, 0, stemp);
m_symbol_code = APRSTT_DEFAULT_SYMBOL;
m_symtab_or_overlay = stemp[0];
}
else {
strncpy (tttemp, e+1, len-3);
tttemp[len-3] = '\0';
tt_two_key_to_text (tttemp, 0, m_callsign);
m_symbol_code = APRSTT_DEFAULT_SYMBOL;
m_symtab_or_overlay = e[len-2];
}
return (0);
}
text_color_set(DW_COLOR_ERROR);
dw_printf ("Touch tone callsign not valid: \"%s\"\n", e);
return (TT_ERROR_INVALID_CALL);
}
/*------------------------------------------------------------------
*
* Name: parse_object_name
*
* Purpose: Extract object name from touch tone sequence.
*
* Inputs: e - An "entry" extracted from a complete
* APRStt messsage.
* In this case, it should start with "AA".
*
* Outputs: m_callsign
*
* m_ssid - Cleared to remove the default of 12.
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*
* Description: Data format
*
* AAtt...tt - Symbol name, two key method, up to 9 characters.
*
*----------------------------------------------------------------*/
static int parse_object_name (char *e)
{
int len;
//int c_length;
//char tttemp[40];
//char stemp[30];
assert (e[0] == 'A');
assert (e[1] == 'A');
len = strlen(e);
/*
* Object name in two key format.
*/
if (len >= 2 + 1 && len <= 30) {
if (tt_two_key_to_text (e+2, 0, m_callsign) == 0) {
m_callsign[9] = '\0'; /* truncate to 9 */
m_ssid = 0; /* No ssid for object name */
return (0);
}
}
text_color_set(DW_COLOR_ERROR);
dw_printf ("Touch tone object name not valid: \"%s\"\n", e);
return (TT_ERROR_INVALID_OBJNAME);
} /* end parse_oject_name */
/*------------------------------------------------------------------
*
* Name: parse_symbol
*
* Purpose: Extract symbol from touch tone sequence.
*
* Inputs: e - An "entry" extracted from a complete
* APRStt messsage.
* In this case, it should start with "AB".
*
* Outputs: m_symtab_or_overlay
*
* m_symbol_code
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*
* Description: Data format
*
* AB1nn - Symbol from primary symbol table.
* Two digits nn are the same as in the GPSCnn
* generic address used as a destination.
*
* AB2nn - Symbol from alternate symbol table.
* Two digits nn are the same as in the GPSEnn
* generic address used as a destination.
*
* AB0nnvv - Symbol from alternate symbol table.
* Two digits nn are the same as in the GPSEnn
* generic address used as a destination.
* vv is an overlay digit or letter in two key method.
*
*----------------------------------------------------------------*/
static int parse_symbol (char *e)
{
int len;
char nstr[3];
int nn;
char stemp[10];
assert (e[0] == 'A');
assert (e[1] == 'B');
len = strlen(e);
if (len >= 4 && len <= 10) {
nstr[0] = e[3];
nstr[1] = e[4];
nstr[2] = '\0';
nn = atoi (nstr);
if (nn < 1) {
nn = 1;
}
else if (nn > 94) {
nn = 94;
}
switch (e[2]) {
case '1':
m_symtab_or_overlay = '/';
m_symbol_code = 32 + nn;
return (0);
break;
case '2':
m_symtab_or_overlay = '\\';
m_symbol_code = 32 + nn;
return (0);
break;
case '0':
if (len >= 6) {
if (tt_two_key_to_text (e+5, 0, stemp) == 0) {
m_symbol_code = 32 + nn;
m_symtab_or_overlay = stemp[0];
return (0);
}
}
break;
}
}
text_color_set(DW_COLOR_ERROR);
dw_printf ("Touch tone symbol not valid: \"%s\"\n", e);
return (TT_ERROR_INVALID_SYMBOL);
} /* end parse_oject_name */
/*------------------------------------------------------------------
*
* Name: parse_aprstt3_call
*
* Purpose: Extract QIKcom-2 / APRStt 3 ten digit call or five digit suffix.
*
* Inputs: e - An "entry" extracted from a complete
* APRStt messsage.
* In this case, it should start with "AC".
*
* Outputs: m_callsign
*
* Returns: 0 for success or one of the TT_ERROR_... codes.
*