-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathil2p.c
More file actions
4964 lines (3828 loc) · 138 KB
/
Copy pathil2p.c
File metadata and controls
4964 lines (3828 loc) · 138 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
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
/*
Copyright (C) 2019-2020 Andrei Kopanchuk UZ7HO
This file is part of QtSoundModem
QtSoundModem 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 3 of the License, or
(at your option) any later version.
QtSoundModem 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 QtSoundModem. If not, see http://www.gnu.org/licenses
*/
// UZ7HO Soundmodem Port by John Wiseman G8BPQ
// IL2P code. Based on Direwolf code, under the following copyright
//
// Copyright (C) 2021 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/>.
//
// IP2P receive code (il2p_rec_bit) is called from the bit receiving code in ax25_demod.c, so includes parallel decoders
#include "UZ7HOStuff.h"
#include <stdint.h> // for uint64_t
void debugHexDump(unsigned char * Data, int Len, char Dirn);
extern void debugTimeStamp(char * Text, char Dirn);
extern int useTimedPTT;
// Oct 2023 Nino has added an optional crc
// Hamming(7,4) Encoding Table
// Enter this table with the 4-bit value to be encoded.
// Returns 7-bit encoded value, with high bit zero'd.
uint8_t Hamming74EncodeTable[16] = { 0x0, 0x71, 0x62, 0x13, 0x54, 0x25, 0x36, 0x47, 0x38, 0x49, 0x5a, 0x2b, 0x6c, 0x1d, 0xe, 0x7f };
// Hamming(7,4) Decoding Table
// Enter this table with 7-bit encoded value, high bit masked.
// Returns 4-bit decoded value.
uint16_t Hamming74DecodeTable[128] = { \
0x0, 0x0, 0x0, 0x3, 0x0, 0x5, 0xe, 0x7, \
0x0, 0x9, 0xe, 0xb, 0xe, 0xd, 0xe, 0xe, \
0x0, 0x3, 0x3, 0x3, 0x4, 0xd, 0x6, 0x3, \
0x8, 0xd, 0xa, 0x3, 0xd, 0xd, 0xe, 0xd, \
0x0, 0x5, 0x2, 0xb, 0x5, 0x5, 0x6, 0x5, \
0x8, 0xb, 0xb, 0xb, 0xc, 0x5, 0xe, 0xb, \
0x8, 0x1, 0x6, 0x3, 0x6, 0x5, 0x6, 0x6, \
0x8, 0x8, 0x8, 0xb, 0x8, 0xd, 0x6, 0xf, \
0x0, 0x9, 0x2, 0x7, 0x4, 0x7, 0x7, 0x7, \
0x9, 0x9, 0xa, 0x9, 0xc, 0x9, 0xe, 0x7, \
0x4, 0x1, 0xa, 0x3, 0x4, 0x4, 0x4, 0x7, \
0xa, 0x9, 0xa, 0xa, 0x4, 0xd, 0xa, 0xf, \
0x2, 0x1, 0x2, 0x2, 0xc, 0x5, 0x2, 0x7, \
0xc, 0x9, 0x2, 0xb, 0xc, 0xc, 0xc, 0xf, \
0x1, 0x1, 0x2, 0x1, 0x4, 0x1, 0x6, 0xf, \
0x8, 0x1, 0xa, 0xf, 0xc, 0xf, 0xf, 0xf };
void Debugprintf(const char * format, ...);
int SMUpdatePhaseConstellation(int chan, float * Phases, float * Mags, int intPSKPhase, int Count);
extern int openTraceLog();
extern uint64_t writeTraceLog(char * Data);
extern void closeTraceLog();
#define MAX_ADEVS 3
#define MAX_RADIO_CHANS ((MAX_ADEVS) * 2)
#define MAX_CHANS MAX_RADIO_CHANS // TODO: Replace all former with latter to avoid confusion with following.
#define MAX_TOTAL_CHANS 16 // v1.7 allows additional virtual channels which are connected
// to something other than radio modems.
// Total maximum channels is based on the 4 bit KISS field.
// Someone with very unusual requirements could increase this and
// use only the AGW network protocol.
#define MAX_SUBCHANS 9
#define MAX_SLICERS 9
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
extern int nPhases[4][16][nr_emph + 1];
extern float Phases[4][16][nr_emph + 1][4096];
extern float Mags[4][16][nr_emph + 1][4096];
/* For option to try fixing frames with bad CRC. */
typedef enum retry_e {
RETRY_NONE = 0,
RETRY_INVERT_SINGLE = 1,
RETRY_INVERT_DOUBLE = 2,
RETRY_INVERT_TRIPLE = 3,
RETRY_INVERT_TWO_SEP = 4,
RETRY_MAX = 5
} retry_t;
typedef struct alevel_s {
int rec;
int mark;
int space;
//float ms_ratio; // TODO: take out after temporary investigation.
} alevel_t;
alevel_t demod_get_audio_level(int chan, int subchan);
void tone_gen_put_bit(int chan, int dat, int scramble);
int ax25memdebug = 1;
// Code to try to determine centre freq
float MagOut[4096];
float MaxMagOut = 0;
int MaxMagIndex = 0;
// FFT Bin Size is 12000 / FFTSize
#ifndef FX25_H
#define FX25_H
extern unsigned int pskStates[4];
/* Reed-Solomon codec control block */
struct rs {
unsigned int mm; /* Bits per symbol */
unsigned int nn; /* Symbols per block (= (1<<mm)-1) */
unsigned char *alpha_to; /* log lookup table */
unsigned char *index_of; /* Antilog lookup table */
unsigned char *genpoly; /* Generator polynomial */
unsigned int nroots; /* Number of generator roots = number of parity symbols */
unsigned char fcr; /* First consecutive root, index form */
unsigned char prim; /* Primitive element, index form */
unsigned char iprim; /* prim-th root of 1, index form */
};
#define MM (rs->mm)
#define NN (rs->nn)
#define ALPHA_TO (rs->alpha_to)
#define INDEX_OF (rs->index_of)
#define GENPOLY (rs->genpoly)
#define NROOTS (rs->nroots)
#define FCR (rs->fcr)
#define PRIM (rs->prim)
#define IPRIM (rs->iprim)
#define A0 (NN)
int __builtin_popcountll(unsigned long long int i)
{
return 0;
}
int __builtin_popcount(unsigned int n)
{
unsigned int count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
static inline int modnn(struct rs *rs, int x) {
while (x >= rs->nn) {
x -= rs->nn;
x = (x >> rs->mm) + (x & rs->nn);
}
return x;
}
#define MODNN(x) modnn(rs,x)
#define ENCODE_RS encode_rs_char
#define DECODE_RS decode_rs_char
#define INIT_RS init_rs_char
#define FREE_RS free_rs_char
#define DTYPE unsigned char
void ENCODE_RS(struct rs *rs, DTYPE *data, DTYPE *bb);
int DECODE_RS(struct rs *rs, DTYPE *data, int *eras_pos, int no_eras);
struct rs *INIT_RS(unsigned int symsize, unsigned int gfpoly,
unsigned int fcr, unsigned int prim, unsigned int nroots);
void FREE_RS(struct rs *rs);
// These 3 are the external interface.
// Maybe these should be in a different file, separated from the internal stuff.
void fx25_init(int debug_level);
void fx25_rec_bit(int chan, int subchan, int slice, int dbit);
int fx25_rec_busy(int chan);
// Other functions in fx25_init.c.
struct rs *fx25_get_rs(int ctag_num);
uint64_t fx25_get_ctag_value(int ctag_num);
int fx25_get_k_data_radio(int ctag_num);
int fx25_get_k_data_rs(int ctag_num);
int fx25_get_nroots(int ctag_num);
int fx25_get_debug(void);
int fx25_tag_find_match(uint64_t t);
int fx25_pick_mode(int fx_mode, int dlen);
void fx_hex_dump(unsigned char *x, int len);
/*-------------------------------------------------------------------
*
* Name: ax25_pad.h
*
* Purpose: Header file for using ax25_pad.c
*
*------------------------------------------------------------------*/
#ifndef AX25_PAD_H
#define AX25_PAD_H 1
#define AX25_MAX_REPEATERS 8
#define AX25_MIN_ADDRS 2 /* Destination & Source. */
#define AX25_MAX_ADDRS 10 /* Destination, Source, 8 digipeaters. */
#define AX25_DESTINATION 0 /* Address positions in frame. */
#define AX25_SOURCE 1
#define AX25_REPEATER_1 2
#define AX25_REPEATER_2 3
#define AX25_REPEATER_3 4
#define AX25_REPEATER_4 5
#define AX25_REPEATER_5 6
#define AX25_REPEATER_6 7
#define AX25_REPEATER_7 8
#define AX25_REPEATER_8 9
#define AX25_MAX_ADDR_LEN 12 /* In theory, you would expect the maximum length */
/* to be 6 letters, dash, 2 digits, and nul for a */
/* total of 10. However, object labels can be 10 */
/* characters so throw in a couple extra bytes */
/* to be safe. */
#define AX25_MIN_INFO_LEN 0 /* Previously 1 when considering only APRS. */
#define AX25_MAX_INFO_LEN 2048 /* Maximum size for APRS. */
/* AX.25 starts out with 256 as the default max */
/* length but the end stations can negotiate */
/* something different. */
/* version 0.8: Change from 256 to 2028 to */
/* handle the larger paclen for Linux AX25. */
/* These don't include the 2 bytes for the */
/* HDLC frame FCS. */
/*
* Previously, for APRS only.
* #define AX25_MIN_PACKET_LEN ( 2 * 7 + 2 + AX25_MIN_INFO_LEN)
* #define AX25_MAX_PACKET_LEN ( AX25_MAX_ADDRS * 7 + 2 + AX25_MAX_INFO_LEN)
*/
/* The more general case. */
/* An AX.25 frame can have a control byte and no protocol. */
#define AX25_MIN_PACKET_LEN ( 2 * 7 + 1 )
#define AX25_MAX_PACKET_LEN ( AX25_MAX_ADDRS * 7 + 2 + 3 + AX25_MAX_INFO_LEN)
/*
* packet_t is a pointer to a packet object.
*
* The actual implementation is not visible outside ax25_pad.c.
*/
#define AX25_UI_FRAME 3 /* Control field value. */
#define AX25_PID_NO_LAYER_3 0xf0 /* protocol ID used for APRS */
#define AX25_PID_SEGMENTATION_FRAGMENT 0x08
#define AX25_PID_ESCAPE_CHARACTER 0xff
struct packet_s {
int magic1; /* for error checking. */
int seq; /* unique sequence number for debugging. */
double release_time; /* Time stamp in format returned by dtime_now(). */
/* When to release from the SATgate mode delay queue. */
#define MAGIC 0x41583235
struct packet_s *nextp; /* Pointer to next in queue. */
int num_addr; /* Number of addresses in frame. */
/* Range of AX25_MIN_ADDRS .. AX25_MAX_ADDRS for AX.25. */
/* It will be 0 if it doesn't look like AX.25. */
/* -1 is used temporarily at allocation to mean */
/* not determined yet. */
/*
* The 7th octet of each address contains:
*
* Bits: H R R SSID 0
*
* H for digipeaters set to 0 initially.
* Changed to 1 when position has been used.
*
* for source & destination it is called
* command/response. Normally both 1 for APRS.
* They should be opposites for connected mode.
*
* R R Reserved. Normally set to 1 1.
*
* SSID Substation ID. Range of 0 - 15.
*
* 0 Usually 0 but 1 for last address.
*/
#define SSID_H_MASK 0x80
#define SSID_H_SHIFT 7
#define SSID_RR_MASK 0x60
#define SSID_RR_SHIFT 5
#define SSID_SSID_MASK 0x1e
#define SSID_SSID_SHIFT 1
#define SSID_LAST_MASK 0x01
int frame_len; /* Frame length without CRC. */
int modulo; /* I & S frames have sequence numbers of either 3 bits (modulo 8) */
/* or 7 bits (modulo 128). This is conveyed by either 1 or 2 */
/* control bytes. Unfortunately, we can't determine this by looking */
/* at an isolated frame. We need to know about the context. If we */
/* are part of the conversation, we would know. But if we are */
/* just listening to others, this would be more difficult to determine. */
/* For U frames: set to 0 - not applicable */
/* For I & S frames: 8 or 128 if known. 0 if unknown. */
unsigned char frame_data[AX25_MAX_PACKET_LEN + 1];
/* Raw frame contents, without the CRC. */
unsigned char crc[4]; // received crc
int magic2; /* Will get stomped on if above overflows. */
};
typedef struct packet_s *packet_t;
typedef enum cmdres_e { cr_00 = 2, cr_cmd = 1, cr_res = 0, cr_11 = 3 } cmdres_t;
extern packet_t ax25_new(void);
int set_addrs(packet_t pp, char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, cmdres_t cr);
/*
* APRS always has one control octet of 0x03 but the more
* general AX.25 case is one or two control bytes depending on
* whether "modulo 128 operation" is in effect.
*/
//#define DEBUGX 1
static inline int ax25_get_control_offset(packet_t this_p)
{
return (this_p->num_addr * 7);
}
static inline int ax25_get_num_control(packet_t this_p)
{
int c;
c = this_p->frame_data[ax25_get_control_offset(this_p)];
if ((c & 0x01) == 0) { /* I xxxx xxx0 */
#if DEBUGX
Debugprintf("ax25_get_num_control, %02x is I frame, returns %d\n", c, (this_p->modulo == 128) ? 2 : 1);
#endif
return ((this_p->modulo == 128) ? 2 : 1);
}
if ((c & 0x03) == 1) { /* S xxxx xx01 */
#if DEBUGX
Debugprintf("ax25_get_num_control, %02x is S frame, returns %d\n", c, (this_p->modulo == 128) ? 2 : 1);
#endif
return ((this_p->modulo == 128) ? 2 : 1);
}
#if DEBUGX
Debugprintf("ax25_get_num_control, %02x is U frame, always returns 1.\n", c);
#endif
return (1); /* U xxxx xx11 */
}
/*
* APRS always has one protocol octet of 0xF0 meaning no level 3
* protocol but the more general case is 0, 1 or 2 protocol ID octets.
*/
static inline int ax25_get_pid_offset(packet_t this_p)
{
return (ax25_get_control_offset(this_p) + ax25_get_num_control(this_p));
}
static int ax25_get_num_pid(packet_t this_p)
{
int c;
int pid;
c = this_p->frame_data[ax25_get_control_offset(this_p)];
if ((c & 0x01) == 0 || /* I xxxx xxx0 */
c == 0x03 || c == 0x13) { /* UI 000x 0011 */
pid = this_p->frame_data[ax25_get_pid_offset(this_p)];
#if DEBUGX
Debugprintf("ax25_get_num_pid, %02x is I or UI frame, pid = %02x, returns %d\n", c, pid, (pid == AX25_PID_ESCAPE_CHARACTER) ? 2 : 1);
#endif
if (pid == AX25_PID_ESCAPE_CHARACTER) {
return (2); /* pid 1111 1111 means another follows. */
}
return (1);
}
#if DEBUGX
Debugprintf("ax25_get_num_pid, %02x is neither I nor UI frame, returns 0\n", c);
#endif
return (0);
}
/*
* AX.25 has info field for 5 frame types depending on the control field.
*
* xxxx xxx0 I
* 000x 0011 UI (which includes APRS)
* 101x 1111 XID
* 111x 0011 TEST
* 100x 0111 FRMR
*
* APRS always has an Information field with at least one octet for the Data Type Indicator.
*/
static inline int ax25_get_info_offset(packet_t this_p)
{
int offset = ax25_get_control_offset(this_p) + ax25_get_num_control(this_p) + ax25_get_num_pid(this_p);
#if DEBUGX
Debugprintf("ax25_get_info_offset, returns %d\n", offset);
#endif
return (offset);
}
static inline int ax25_get_num_info(packet_t this_p)
{
int len;
/* assuming AX.25 frame. */
len = this_p->frame_len - this_p->num_addr * 7 - ax25_get_num_control(this_p) - ax25_get_num_pid(this_p);
if (len < 0) {
len = 0; /* print error? */
}
return (len);
}
typedef enum ax25_modulo_e { modulo_unknown = 0, modulo_8 = 8, modulo_128 = 128 } ax25_modulo_t;
typedef enum ax25_frame_type_e {
frame_type_I = 0, // Information
frame_type_S_RR, // Receive Ready - System Ready To Receive
frame_type_S_RNR, // Receive Not Ready - TNC Buffer Full
frame_type_S_REJ, // Reject Frame - Out of Sequence or Duplicate
frame_type_S_SREJ, // Selective Reject - Request single frame repeat
frame_type_U_SABME, // Set Async Balanced Mode, Extended
frame_type_U_SABM, // Set Async Balanced Mode
frame_type_U_DISC, // Disconnect
frame_type_U_DM, // Disconnect Mode
frame_type_U_UA, // Unnumbered Acknowledge
frame_type_U_FRMR, // Frame Reject
frame_type_U_UI, // Unnumbered Information
frame_type_U_XID, // Exchange Identification
frame_type_U_TEST, // Test
frame_type_U, // other Unnumbered, not used by AX.25.
frame_not_AX25 // Could not get control byte from frame.
// This must be last because value plus 1 is
// for the size of an array.
} ax25_frame_type_t;
/*
* Originally this was a single number.
* Let's try something new in version 1.2.
* Also collect AGC values from the mark and space filters.
*/
#ifndef AXTEST
// TODO: remove this?
#define AX25MEMDEBUG 1
#endif
extern packet_t ax25_from_text(char *monitor, int strict);
extern packet_t ax25_from_frame(unsigned char *data, int len, alevel_t alevel);
extern packet_t ax25_dup(packet_t copy_from);
extern void ax25_delete(packet_t pp);
extern int ax25_parse_addr(int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard);
extern int ax25_check_addresses(packet_t pp);
extern packet_t ax25_unwrap_third_party(packet_t from_pp);
extern void ax25_set_addr(packet_t pp, int, char *);
extern void ax25_insert_addr(packet_t this_p, int n, char *ad);
extern void ax25_remove_addr(packet_t this_p, int n);
extern int ax25_get_num_addr(packet_t pp);
extern int ax25_get_num_repeaters(packet_t this_p);
extern void ax25_get_addr_with_ssid(packet_t pp, int n, char *station);
extern void ax25_get_addr_no_ssid(packet_t pp, int n, char *station);
extern int ax25_get_ssid(packet_t pp, int n);
extern void ax25_set_ssid(packet_t this_p, int n, int ssid);
extern int ax25_get_h(packet_t pp, int n);
extern void ax25_set_h(packet_t pp, int n);
extern int ax25_get_heard(packet_t this_p);
extern int ax25_get_first_not_repeated(packet_t pp);
extern int ax25_get_rr(packet_t this_p, int n);
extern int ax25_get_info(packet_t pp, unsigned char **paddr);
extern void ax25_set_info(packet_t pp, unsigned char *info_ptr, int info_len);
extern int ax25_cut_at_crlf(packet_t this_p);
extern void ax25_set_nextp(packet_t this_p, packet_t next_p);
extern int ax25_get_dti(packet_t this_p);
extern packet_t ax25_get_nextp(packet_t this_p);
extern void ax25_set_release_time(packet_t this_p, double release_time);
extern double ax25_get_release_time(packet_t this_p);
extern void ax25_set_modulo(packet_t this_p, int modulo);
extern int ax25_get_modulo(packet_t this_p);
extern void ax25_format_addrs(packet_t pp, char *);
extern void ax25_format_via_path(packet_t this_p, char *result, size_t result_size);
extern int ax25_pack(packet_t pp, unsigned char result[AX25_MAX_PACKET_LEN]);
extern ax25_frame_type_t ax25_frame_type(packet_t this_p, cmdres_t *cr, char *desc, int *pf, int *nr, int *ns);
extern void ax25_hex_dump(packet_t this_p);
extern int ax25_is_aprs(packet_t pp);
extern int ax25_is_null_frame(packet_t this_p);
extern int ax25_get_control(packet_t this_p);
extern int ax25_get_c2(packet_t this_p);
extern int ax25_get_pid(packet_t this_p);
extern int ax25_get_frame_len(packet_t this_p);
extern unsigned char *ax25_get_frame_data_ptr(packet_t this_p);
extern unsigned short ax25_dedupe_crc(packet_t pp);
extern unsigned short ax25_m_m_crc(packet_t pp);
extern void ax25_safe_print(char *, int, int ascii_only);
#define AX25_ALEVEL_TO_TEXT_SIZE 40 // overkill but safe.
extern int ax25_alevel_to_text(alevel_t alevel, char text[AX25_ALEVEL_TO_TEXT_SIZE]);
#endif /* AX25_PAD_H */
/* end ax25_pad.h */
#define CTAG_MIN 0x01
#define CTAG_MAX 0x0B
// Maximum sizes of "data" and "check" parts.
#define FX25_MAX_DATA 239 // i.e. RS(255,239)
#define FX25_MAX_CHECK 64 // e.g. RS(255, 191)
#define FX25_BLOCK_SIZE 255 // Block size always 255 for 8 bit symbols.
#endif // FX25_H
#ifndef IL2P_H
#define IL2P_H 1
#define IL2P_PREAMBLE 0x55
#define IL2P_SYNC_WORD 0xF15E48
#define IL2P_SYNC_WORD_SIZE 3
#define IL2P_HEADER_SIZE 13 // Does not include 2 parity.
#define IL2P_HEADER_PARITY 2
#define IL2P_MAX_PAYLOAD_SIZE 1023
#define IL2P_MAX_PAYLOAD_BLOCKS 5
#define IL2P_MAX_PARITY_SYMBOLS 16 // For payload only.
#define IL2P_MAX_ENCODED_PAYLOAD_SIZE (IL2P_MAX_PAYLOAD_SIZE + IL2P_MAX_PAYLOAD_BLOCKS * IL2P_MAX_PARITY_SYMBOLS)
#define IL2P_MAX_PACKET_SIZE (IL2P_SYNC_WORD_SIZE + IL2P_HEADER_SIZE + IL2P_HEADER_PARITY + IL2P_MAX_ENCODED_PAYLOAD_SIZE)
float GuessCentreFreq(int i)
{
float Freq = 0;
float Start;
float End;
int n;
float Max = 0;
int Index = 0;
float BinSize = 12000.0 / FFTSize;
Start = (rx_freq[i] - RCVR[i] * rcvr_offset[i]) / BinSize;
End = (rx_freq[i] + RCVR[i] * rcvr_offset[i]) / BinSize;
Start = (active_rx_freq[i] - RCVR[i] * rcvr_offset[i]) / BinSize;
End = (active_rx_freq[i] + RCVR[i] * rcvr_offset[i]) / BinSize;
for (n = Start; n <= End; n++)
{
if (MagOut[n] > Max)
{
Max = MagOut[n];
Index = n;
}
}
Freq = Index * BinSize;
return Freq;
}
/*------------------------------------------------------------------------------
*
* Name: ax25_new
*
* Purpose: Allocate memory for a new packet object.
*
* Returns: Identifier for a new packet object.
* In the current implementation this happens to be a pointer.
*
*------------------------------------------------------------------------------*/
int last_seq_num = 0;
int new_count = 0;
int delete_count = 0;
packet_t ax25_new(void)
{
struct packet_s *this_p;
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
Debugprintf("ax25_new(): before alloc, new=%d, delete=%d\n", new_count, delete_count);
#endif
last_seq_num++;
new_count++;
/*
* check for memory leak.
*/
// version 1.4 push up the threshold. We could have considerably more with connected mode.
//if (new_count > delete_count + 100) {
if (new_count > delete_count + 256) {
Debugprintf("Memory leak for packet objects. new=%d, delete=%d\n", new_count, delete_count);
#if AX25MEMDEBUG
#endif
}
this_p = calloc(sizeof(struct packet_s), (size_t)1);
if (this_p == NULL) {
Debugprintf("ERROR - can't allocate memory in ax25_new.\n");
}
// assert(this_p != NULL);
this_p->magic1 = MAGIC;
this_p->seq = last_seq_num;
this_p->magic2 = MAGIC;
this_p->num_addr = (-1);
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_delete
*
* Purpose: Destroy a packet object, freeing up memory it was using.
*
*------------------------------------------------------------------------------*/
void ax25_delete(packet_t this_p)
{
if (this_p == NULL) {
Debugprintf("ERROR - NULL pointer passed to ax25_delete.\n");
return;
}
delete_count++;
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
this_p->magic1 = 0;
this_p->magic1 = 0;
free(this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_s_frame
*
* Purpose: Construct an S frame.
*
* Input: addrs - Array of addresses.
*
* num_addr - Number of addresses, range 2 .. 10.
*
* cr - cr_cmd command frame, cr_res for a response frame.
*
* ftype - One of:
* frame_type_S_RR, // Receive Ready - System Ready To Receive
* frame_type_S_RNR, // Receive Not Ready - TNC Buffer Full
* frame_type_S_REJ, // Reject Frame - Out of Sequence or Duplicate
* frame_type_S_SREJ, // Selective Reject - Request single frame repeat
*
* modulo - 8 or 128. Determines if we have 1 or 2 control bytes.
*
* nr - N(R) field --- describe.
*
* pf - Poll/Final flag.
*
* pinfo - Pointer to data for Info field. Allowed only for SREJ.
*
* info_len - Length for Info field.
*
*
* Returns: Pointer to new packet object.
*
*------------------------------------------------------------------------------*/
packet_t ax25_s_frame(char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, cmdres_t cr, ax25_frame_type_t ftype, int modulo, int nr, int pf, unsigned char *pinfo, int info_len)
{
packet_t this_p;
unsigned char *p;
int ctrl = 0;
this_p = ax25_new();
if (this_p == NULL) return (NULL);
if (!set_addrs(this_p, addrs, num_addr, cr)) {
Debugprintf("Internal error in %s: Could not set addresses for S frame.\n", __func__);
ax25_delete(this_p);
return (NULL);
}
if (modulo != 8 && modulo != 128) {
Debugprintf("Internal error in %s: Invalid modulo %d for S frame.\n", __func__, modulo);
modulo = 8;
}
this_p->modulo = modulo;
if (nr < 0 || nr >= modulo) {
Debugprintf("Internal error in %s: Invalid N(R) %d for S frame.\n", __func__, nr);
nr &= (modulo - 1);
}
// Erratum: The AX.25 spec is not clear about whether SREJ should be command, response, or both.
// The underlying X.25 spec clearly says it is response only. Let's go with that.
if (ftype == frame_type_S_SREJ && cr != cr_res) {
Debugprintf("Internal error in %s: SREJ must be response.\n", __func__);
}
switch (ftype) {
case frame_type_S_RR: ctrl = 0x01; break;
case frame_type_S_RNR: ctrl = 0x05; break;
case frame_type_S_REJ: ctrl = 0x09; break;
case frame_type_S_SREJ: ctrl = 0x0d; break;
default:
Debugprintf("Internal error in %s: Invalid ftype %d for S frame.\n", __func__, ftype);
ax25_delete(this_p);
return (NULL);
break;
}
p = this_p->frame_data + this_p->frame_len;
if (modulo == 8) {
if (pf) ctrl |= 0x10;
ctrl |= nr << 5;
*p++ = ctrl;
this_p->frame_len++;
}
else {
*p++ = ctrl;
this_p->frame_len++;
ctrl = pf & 1;
ctrl |= nr << 1;
*p++ = ctrl;
this_p->frame_len++;
}
if (ftype == frame_type_S_SREJ) {
if (pinfo != NULL && info_len > 0) {
if (info_len > AX25_MAX_INFO_LEN) {
Debugprintf("Internal error in %s: SREJ frame, Invalid information field length %d.\n", __func__, info_len);
info_len = AX25_MAX_INFO_LEN;
}
memcpy(p, pinfo, info_len);
p += info_len;
this_p->frame_len += info_len;
}
}
else {
if (pinfo != NULL || info_len != 0) {
Debugprintf("Internal error in %s: Info part not allowed for RR, RNR, REJ frame.\n", __func__);
}
}
*p = '\0';
return (this_p);
} /* end ax25_s_frame */
/*------------------------------------------------------------------------------
*
* Name: ax25_i_frame
*
* Purpose: Construct an I frame.
*
* Input: addrs - Array of addresses.
*
* num_addr - Number of addresses, range 2 .. 10.
*
* cr - cr_cmd command frame, cr_res for a response frame.
*
* modulo - 8 or 128.
*
* nr - N(R) field --- describe.
*
* ns - N(S) field --- describe.
*
* pf - Poll/Final flag.
*
* pid - Protocol ID.
* Normally 0xf0 meaning no level 3.
* Could be other values for NET/ROM, etc.
*
* pinfo - Pointer to data for Info field.
*
* info_len - Length for Info field.
*
*
* Returns: Pointer to new packet object.
*
*------------------------------------------------------------------------------*/
packet_t ax25_i_frame(char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, cmdres_t cr, int modulo, int nr, int ns, int pf, int pid, unsigned char *pinfo, int info_len)
{
packet_t this_p;
unsigned char *p;
int ctrl = 0;
this_p = ax25_new();
if (this_p == NULL) return (NULL);
if (!set_addrs(this_p, addrs, num_addr, cr)) {
Debugprintf("Internal error in %s: Could not set addresses for I frame.\n", __func__);
ax25_delete(this_p);
return (NULL);
}
if (modulo != 8 && modulo != 128) {
Debugprintf("Internal error in %s: Invalid modulo %d for I frame.\n", __func__, modulo);
modulo = 8;
}
this_p->modulo = modulo;
if (nr < 0 || nr >= modulo) {
Debugprintf("Internal error in %s: Invalid N(R) %d for I frame.\n", __func__, nr);
nr &= (modulo - 1);
}
if (ns < 0 || ns >= modulo) {
Debugprintf("Internal error in %s: Invalid N(S) %d for I frame.\n", __func__, ns);
ns &= (modulo - 1);
}
p = this_p->frame_data + this_p->frame_len;
if (modulo == 8) {
ctrl = (nr << 5) | (ns << 1);
if (pf) ctrl |= 0x10;
*p++ = ctrl;
this_p->frame_len++;
}