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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
|
/*
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++;
}
else {
ctrl = ns << 1;
*p++ = ctrl;
this_p->frame_len++;
ctrl = nr << 1;
if (pf) ctrl |= 0x01;
*p++ = ctrl;
this_p->frame_len++;
}
// Definitely don't want pid value of 0 (not in valid list)
// or 0xff (which means more bytes follow).
if (pid < 0 || pid == 0 || pid == 0xff) {
Debugprintf("Warning: Client application provided invalid PID value, 0x%02x, for I frame.\n", pid);
pid = AX25_PID_NO_LAYER_3;
}
*p++ = pid;
this_p->frame_len++;
if (pinfo != NULL && info_len > 0) {
if (info_len > AX25_MAX_INFO_LEN) {
Debugprintf("Internal error in %s: I 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;
}
*p = '\0';
return (this_p);
} /* end ax25_i_frame */
extern TStringList detect_list[5];
extern TStringList detect_list_c[5];
void multi_modem_process_rec_packet(int snd_ch, int subchan, int slice, packet_t pp, alevel_t alevel, retry_t retries, int is_fx25, int emph, int centreFreq)
{
// Convert to QtSM internal format
struct TDetector_t * pDET = &DET[emph][subchan];
string * data = newString();
char Mode[32] = "IL2P";
int Quality = 0;
int CRCOK = 1;
char debugmsg[256];
sprintf(Mode, "IL2P %d", centreFreq);
unsigned char * axcall = &pp->frame_data[7];
char call[10];
call[ConvFromAX25(axcall, call)] = 0;
// check crc if enabled
if (il2p_crc[snd_ch] & 1)
{
unsigned short CRCMSG;
unsigned short CRCCALC;
uint8_t crc[4];
// check crc if enabled
// The four encoded CRC bytes are arranged :
// | CRC3 | CRC2 | CRC1 | CRC0 | but we store as received, so F->crc[0] is CRC3
// CRC3 encoded from high nibble of 16 - bit CRC value (from crc2)
// CRC0 encoded from low nibble of 16 - bit CRC value (from crc1)
crc[0] = Hamming74DecodeTable[(pp->crc[0] & 0x7f)];
crc[1] = Hamming74DecodeTable[(pp->crc[1] & 0x7f)];
crc[2] = Hamming74DecodeTable[(pp->crc[2] & 0x7f)];
crc[3] = Hamming74DecodeTable[(pp->crc[3] & 0x7f)];
debugTimeStamp("CRC after Hamming decode is", 'R');
debugHexDump(crc, 4, 'R');
CRCMSG = crc[0] << 12 | crc[1] << 8 | crc[2] << 4 | crc[3];
CRCCALC = get_fcs(pp->frame_data, pp->frame_len);
if (CRCCALC != CRCMSG)
{
CRCOK = 0;
if ((il2p_crc[snd_ch] & 2) == 0) // Ignore CRC Error
{
Debugprintf("CRC Error from %s Decoder %d Calculated %x Received %x FEC corrections %d But ignore CRC Set", call, subchan, CRCCALC, CRCMSG, retries);
sprintf(debugmsg, "CRC Error from %s Decoder %d Calculated %x Received %x FEC corrections %d But ignore CRC Set", call, subchan, CRCCALC, CRCMSG, retries);
debugTimeStamp(debugmsg, 'R');
}
else
{
Debugprintf("CRC Error from %s Decoder %d Calculated %x Received %x FEC corrections %d", call, subchan, CRCCALC, CRCMSG, retries);
freeString(data);
ax25_delete(pp);
return;
}
}
}
stringAdd(data, pp->frame_data, pp->frame_len + 2); // QTSM assumes a CRC
ax25_delete(pp);
if (retries)
{
pDET->rx_decoded = decodedFEC;
pDET->emph_decoded = decodedFEC;
pDET->errors = retries;
}
else
{
pDET->rx_decoded = decodedNormal;
pDET->emph_decoded = decodedNormal;
pDET->errors = 0;
}
if (detect_list[snd_ch].Count > 0)
{
int index = my_indexof(&detect_list[snd_ch], data);
if (index >= 0)
{
// Already have a copy of this frame
// See if new one has fewer corrections
string * xx = Strings(&detect_list_c[snd_ch], index); // Should be corresponding frame info
string * olddata = Strings(&detect_list[snd_ch], index);
if (xx)
{
int oldRetries = xx->Data[255];
int oldCRCOK = xx->Data[254];
if ((oldCRCOK == 0 && CRCOK == 1) || (oldRetries > retries))
{
replaceString(&detect_list[snd_ch], index, data);
freeString(olddata);
// Just update the metadata
Debugprintf("Replacing il2p frame from %s rcvr %d emph %d FEC corrections %d CRCOK %d", call, subchan, slice, retries, CRCOK);
memset(xx->Data, 0, 16);
if (pskStates[snd_ch])
{
Quality = SMUpdatePhaseConstellation(snd_ch, &Phases[snd_ch][subchan][slice][0], &Mags[snd_ch][subchan][slice][0], pskStates[snd_ch], nPhases[snd_ch][subchan][slice]);
sprintf(Mode, "%s][Q%d", Mode, Quality);
}
xx->Length= sprintf(xx->Data, "%s", Mode);
xx->Data[254] = CRCOK;
xx->Data[255] = retries;
return;
}
}
freeString(data);
Debugprintf("Discarding copy rcvr %d emph %d FEC corrections %d", subchan, slice, retries);
return;
}
}
Debugprintf("Good il2p frame from %s rcvr %d emph %d FEC corrections %d", call, subchan, slice, retries);
sprintf(debugmsg, "Good il2p frame from %s rcvr %d emph %d FEC corrections %d", call, subchan, slice, retries);
debugTimeStamp(debugmsg, 'R');
string * xx = newString();
memset(xx->Data, 0, 16);
if (pskStates[snd_ch])
{
Quality = SMUpdatePhaseConstellation(snd_ch, &Phases[snd_ch][subchan][slice][0], &Mags[snd_ch][subchan][slice][0], pskStates[snd_ch], nPhases[snd_ch][subchan][slice]);
sprintf(Mode, "%s][Q%d", Mode, Quality);
}
Add(&detect_list_c[snd_ch], xx);
Add(&detect_list[snd_ch], data);
// if (retries)
// sprintf(Mode, "IP2P-%d", retries);
stringAdd(xx, Mode, strlen(Mode));
xx->Data[254] = CRCOK;
xx->Data[255] = retries;
closeTraceLog();
openTraceLog();
return;
}
alevel_t demod_get_audio_level(int chan, int subchan)
{
alevel_t alevel;
alevel.rec = 0;
alevel.mark = 0;
alevel.space = 0;
return (alevel);
}
void ax25_hex_dump(packet_t this_p)
{}
/*------------------------------------------------------------------------------
*
* Name: ax25_from_frame
*
* Purpose: Split apart an HDLC frame to components.
*
* Inputs: fbuf - Pointer to beginning of frame.
*
* flen - Length excluding the two FCS bytes.
*
* alevel - Audio level of received signal.
* Maximum range 0 - 100.
* -1 might be used when not applicable.
*
* Returns: Pointer to new packet object or NULL if error.
*
* Outputs: Use the "get" functions to retrieve information in different ways.
*
*------------------------------------------------------------------------------*/
packet_t ax25_from_frame(unsigned char *fbuf, int flen, alevel_t alevel)
{
packet_t this_p;
/*
* First make sure we have an acceptable length:
*
* We are not concerned with the FCS (CRC) because someone else checked it.
*
* Is is possible to have zero length for info?
*
* In the original version, assuming APRS, the answer was no.
* We always had at least 3 octets after the address part:
* control, protocol, and first byte of info part for data type.
*
* In later versions, this restriction was relaxed so other
* variations of AX.25 could be used. Now the minimum length
* is 7+7 for addresses plus 1 for control.
*
*/
if (flen < AX25_MIN_PACKET_LEN || flen > AX25_MAX_PACKET_LEN)
{
Debugprintf("Frame length %d not in allowable range of %d to %d.", flen, AX25_MIN_PACKET_LEN, AX25_MAX_PACKET_LEN);
return (NULL);
}
this_p = ax25_new();
/* Copy the whole thing intact. */
memcpy(this_p->frame_data, fbuf, flen);
this_p->frame_data[flen] = 0;
this_p->frame_len = flen;
/* Find number of addresses. */
this_p->num_addr = (-1);
(void)ax25_get_num_addr(this_p);
return (this_p);
}
/*------------------------------------------------------------------------------
*
* Name: ax25_get_num_addr
*
* Purpose: Return number of addresses in current packet.
*
* Assumption: ax25_from_text or ax25_from_frame was called first.
*
* Returns: Number of addresses in the current packet.
* Should be in the range of 2 .. AX25_MAX_ADDRS.
*
* Version 0.9: Could be zero for a non AX.25 frame in KISS mode.
*
*------------------------------------------------------------------------------*/
int ax25_get_num_addr(packet_t this_p)
{
//unsigned char *pf;
int a;
int addr_bytes;
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
/* Use cached value if already set. */
if (this_p->num_addr >= 0) {
return (this_p->num_addr);
}
/* Otherwise, determine the number ofaddresses. */
this_p->num_addr = 0; /* Number of addresses extracted. */
addr_bytes = 0;
for (a = 0; a < this_p->frame_len && addr_bytes == 0; a++) {
if (this_p->frame_data[a] & SSID_LAST_MASK) {
addr_bytes = a + 1;
}
}
if (addr_bytes % 7 == 0) {
int addrs = addr_bytes / 7;
if (addrs >= AX25_MIN_ADDRS && addrs <= AX25_MAX_ADDRS) {
this_p->num_addr = addrs;
}
}
return (this_p->num_addr);
}
void ax25_get_addr_with_ssid(packet_t pp, int n, char *station)
{}
/*------------------------------------------------------------------------------
*
* Name: ax25_get_addr_no_ssid
*
* Purpose: Return specified address WITHOUT any SSID.
*
* Inputs: n - Index of address. Use the symbols
* AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
*
* Outputs: station - String representation of the station, WITHOUT the SSID.
* e.g. "WB2OSZ"
* Usually variables will be AX25_MAX_ADDR_LEN bytes
* but 7 would be adequate.
*
* Bugs: No bounds checking is performed. Be careful.
*
* Assumption: ax25_from_text or ax25_from_frame was called first.
*
* Returns: Character string in usual human readable format,
*
*
*------------------------------------------------------------------------------*/
void ax25_get_addr_no_ssid(packet_t this_p, int n, char *station)
{
int i;
//assert(this_p->magic1 == MAGIC);
//assert(this_p->magic2 == MAGIC);
if (n < 0) {
Debugprintf("Internal error detected in ax25_get_addr_no_ssid, %s, line %d.\n", __FILE__, __LINE__);
Debugprintf("Address index, %d, is less than zero.\n", n);
strcpy(station, "??????");
return;
}
if (n >= this_p->num_addr) {
Debugprintf("Internal error detected in ax25_get_no_with_ssid, %s, line %d.\n", __FILE__, __LINE__);
Debugprintf("Address index, %d, is too large for number of addresses, %d.\n", n, this_p->num_addr);
strcpy(station, "??????");
return;
}
// At one time this would stop at the first space, on the assumption we would have only trailing spaces.
// Then there was a forum discussion where someone encountered the address " WIDE2" with a leading space.
// In that case, we would have returned a zero length string here.
// Now we return exactly what is in the address field and trim trailing spaces.
// This will provide better information for troubleshooting.
for (i = 0; i < 6; i++) {
station[i] = (this_p->frame_data[n * 7 + i] >> 1) & 0x7f;
}
station[6] = '\0';
for (i = 5; i >= 0; i--) {
if (station[i] == ' ')
station[i] = '\0';
else
break;
}
if (strlen(station) == 0) {
Debugprintf("Station address, in position %d, is empty! This is not a valid AX.25 frame.\n", n);
}
} /* end ax25_get_addr_no_ssid */
/*------------------------------------------------------------------------------
*
* Name: ax25_get_ssid
*
* Purpose: Return SSID of specified address in current packet.
*
* Inputs: n - Index of address. Use the symbols
* AX25_DESTINATION, AX25_SOURCE, AX25_REPEATER1, etc.
*
* Assumption: ax25_from_text or ax25_from_frame was called first.
*
* Returns: Substation id, as integer 0 .. 15.
*
*------------------------------------------------------------------------------*/
int ax25_get_ssid(packet_t this_p, int n)
{
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
if (n >= 0 && n < this_p->num_addr) {
return ((this_p->frame_data[n * 7 + 6] & SSID_SSID_MASK) >> SSID_SSID_SHIFT);
}
else {
Debugprintf("Internal error: ax25_get_ssid(%d), num_addr=%d\n", n, this_p->num_addr);
return (0);
}
}
/*------------------------------------------------------------------------------
*
* Name: ax25_get_info
*
* Purpose: Obtain Information part of current packet.
*
* Inputs: this_p - Packet object pointer.
*
* Outputs: paddr - Starting address of information part is returned here.
*
* Assumption: ax25_from_text or ax25_from_frame was called first.
*
* Returns: Number of octets in the Information part.
* Should be in the range of AX25_MIN_INFO_LEN .. AX25_MAX_INFO_LEN.
*
*------------------------------------------------------------------------------*/
int ax25_get_info(packet_t this_p, unsigned char **paddr)
{
unsigned char *info_ptr;
int info_len;
//assert(this_p->magic1 == MAGIC);
//assert(this_p->magic2 == MAGIC);
if (this_p->num_addr >= 2) {
/* AX.25 */
info_ptr = this_p->frame_data + ax25_get_info_offset(this_p);
info_len = ax25_get_num_info(this_p);
}
else {
/* Not AX.25. Treat Whole packet as info. */
info_ptr = this_p->frame_data;
info_len = this_p->frame_len;
}
/* Add nul character in case caller treats as printable string. */
// assert(info_len >= 0);
info_ptr[info_len] = '\0';
*paddr = info_ptr;
return (info_len);
} /* end ax25_get_info */
void ax25_set_info(packet_t this_p, unsigned char *new_info_ptr, int new_info_len)
{
unsigned char *old_info_ptr;
int old_info_len = ax25_get_info(this_p, &old_info_ptr);
this_p->frame_len -= old_info_len;
if (new_info_len < 0) new_info_len = 0;
if (new_info_len > AX25_MAX_INFO_LEN) new_info_len = AX25_MAX_INFO_LEN;
memcpy(old_info_ptr, new_info_ptr, new_info_len);
this_p->frame_len += new_info_len;
}
int ax25_get_pid(packet_t this_p)
{
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
// TODO: handle 2 control byte case.
// TODO: sanity check: is it I or UI frame?
if (this_p->frame_len == 0) return(-1);
if (this_p->num_addr >= 2) {
return (this_p->frame_data[ax25_get_pid_offset(this_p)]);
}
return (-1);
}
int ax25_get_frame_len(packet_t this_p)
{
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
// assert(this_p->frame_len >= 0 && this_p->frame_len <= AX25_MAX_PACKET_LEN);
return (this_p->frame_len);
} /* end ax25_get_frame_len */
unsigned char *ax25_get_frame_data_ptr(packet_t this_p)
{
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
return (this_p->frame_data);
} /* end ax25_get_frame_data_ptr */
int ax25_get_modulo(packet_t this_p)
{
return 7;
}
/*------------------------------------------------------------------
*
* Function: ax25_get_control
ax25_get_c2
*
* Purpose: Get Control field from packet.
*
* Inputs: this_p - pointer to packet object.
*
* Returns: APRS uses AX25_UI_FRAME.
* This could also be used in other situations.
*
*------------------------------------------------------------------*/
int ax25_get_control(packet_t this_p)
{
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
if (this_p->frame_len == 0) return(-1);
if (this_p->num_addr >= 2) {
return (this_p->frame_data[ax25_get_control_offset(this_p)]);
}
return (-1);
}
/*------------------------------------------------------------------
*
* Function: ax25_frame_type
*
* Purpose : Extract the type of frame.
* This is derived from the control byte(s) but
* is an enumerated type for easier handling.
*
* Inputs : this_p - pointer to packet object.
*
* Outputs : desc - Text description such as "I frame" or
*"U frame SABME".
* Supply 56 bytes to be safe.
*
* cr - Command or response ?
*
* pf - P / F - Poll / Final or -1 if not applicable
*
* nr - N(R) - receive sequence or -1 if not applicable.
*
* ns - N(S) - send sequence or -1 if not applicable.
*
* Returns: Frame type from enum ax25_frame_type_e.
*
*------------------------------------------------------------------*/
// TODO: need someway to ensure caller allocated enough space.
// Should pass in as parameter.
#define DESC_SIZ 56
ax25_frame_type_t ax25_frame_type(packet_t this_p, cmdres_t *cr, char *desc, int *pf, int *nr, int *ns)
{
int c; // U frames are always one control byte.
int c2 = 0; // I & S frames can have second Control byte.
// assert(this_p->magic1 == MAGIC);
// assert(this_p->magic2 == MAGIC);
strcpy(desc, "????");
*cr = cr_11;
*pf = -1;
*nr = -1;
*ns = -1;
c = ax25_get_control(this_p);
if (c < 0) {
strcpy(desc, "Not AX.25");
return (frame_not_AX25);
}
/*
* TERRIBLE HACK :-( for display purposes.
*
* I and S frames can have 1 or 2 control bytes but there is
* no good way to determine this without dipping into the data
* link state machine. Can we guess?
*
* S frames have no protocol id or information so if there is one
* more byte beyond the control field, we could assume there are
* two control bytes.
*
* For I frames, the protocol id will usually be 0xf0. If we find
* that as the first byte of the information field, it is probably
* the pid and not part of the information. Ditto for segments 0x08.
* Not fool proof but good enough for troubleshooting text out.
*
* If we have a link to the peer station, this will be set properly
* before it needs to be used for other reasons.
*
* Setting one of the RR bits (find reference!) is sounding better and better.
* It's in common usage so I should lobby to get that in the official protocol spec.
*/
// Dont support mod 128
/*
if (this_p->modulo == 0 && (c & 3) == 1 && ax25_get_c2(this_p) != -1) {
this_p->modulo = modulo_128;
}
else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0xF0) {
this_p->modulo = modulo_128;
}
else if (this_p->modulo == 0 && (c & 1) == 0 && this_p->frame_data[ax25_get_info_offset(this_p)] == 0x08) { // same for segments
this_p->modulo = modulo_128;
}
if (this_p->modulo == modulo_128) {
c2 = ax25_get_c2(this_p);
}
*/
int dst_c = this_p->frame_data[AX25_DESTINATION * 7 + 6] & SSID_H_MASK;
int src_c = this_p->frame_data[AX25_SOURCE * 7 + 6] & SSID_H_MASK;
char cr_text[8];
char pf_text[8];
if (dst_c) {
if (src_c) { *cr = cr_11; strcpy(cr_text, "cc=11"); strcpy(pf_text, "p/f"); }
else { *cr = cr_cmd; strcpy(cr_text, "cmd"); strcpy(pf_text, "p"); }
}
else {
if (src_c) { *cr = cr_res; strcpy(cr_text, "res"); strcpy(pf_text, "f"); }
else { *cr = cr_00; strcpy(cr_text, "cc=00"); strcpy(pf_text, "p/f"); }
}
if ((c & 1) == 0) {
// Information rrr p sss 0 or sssssss 0 rrrrrrr p
if (this_p->modulo == modulo_128) {
*ns = (c >> 1) & 0x7f;
*pf = c2 & 1;
*nr = (c2 >> 1) & 0x7f;
}
else {
*ns = (c >> 1) & 7;
*pf = (c >> 4) & 1;
*nr = (c >> 5) & 7;
}
//snprintf (desc, DESC_SIZ, "I %s, n(s)=%d, n(r)=%d, %s=%d", cr_text, *ns, *nr, pf_text, *pf);
sprintf(desc, "I %s, n(s)=%d, n(r)=%d, %s=%d, pid=0x%02x", cr_text, *ns, *nr, pf_text, *pf, ax25_get_pid(this_p));
return (frame_type_I);
}
else if ((c & 2) == 0) {
// Supervisory rrr p/f ss 0 1 or 0000 ss 0 1 rrrrrrr p/f
if (this_p->modulo == modulo_128) {
*pf = c2 & 1;
*nr = (c2 >> 1) & 0x7f;
}
else {
*pf = (c >> 4) & 1;
*nr = (c >> 5) & 7;
}
switch ((c >> 2) & 3) {
case 0: sprintf(desc, "RR %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_RR); break;
case 1: sprintf(desc, "RNR %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_RNR); break;
case 2: sprintf(desc, "REJ %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_REJ); break;
case 3: sprintf(desc, "SREJ %s, n(r)=%d, %s=%d", cr_text, *nr, pf_text, *pf); return (frame_type_S_SREJ); break;
}
}
else {
// Unnumbered mmm p/f mm 1 1
*pf = (c >> 4) & 1;
switch (c & 0xef) {
case 0x6f: sprintf(desc, "SABME %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABME); break;
case 0x2f: sprintf(desc, "SABM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_SABM); break;
case 0x43: sprintf(desc, "DISC %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DISC); break;
case 0x0f: sprintf(desc, "DM %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_DM); break;
case 0x63: sprintf(desc, "UA %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UA); break;
case 0x87: sprintf(desc, "FRMR %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_FRMR); break;
case 0x03: sprintf(desc, "UI %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_UI); break;
case 0xaf: sprintf(desc, "XID %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_XID); break;
case 0xe3: sprintf(desc, "TEST %s, %s=%d", cr_text, pf_text, *pf); return (frame_type_U_TEST); break;
default: sprintf(desc, "U other???"); return (frame_type_U); break;
}
}
// Should be unreachable but compiler doesn't realize that.
// Here only to suppress "warning: control reaches end of non-void function"
return (frame_not_AX25);
} /* end ax25_frame_type */
packet_t ax25_u_frame(char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, cmdres_t cr, ax25_frame_type_t ftype, int pf, int pid, unsigned char *pinfo, int info_len)
{
packet_t this_p;
unsigned char *p;
int ctrl = 0;
unsigned int t = 999; // 1 = must be cmd, 0 = must be response, 2 = can be either.
int i = 0; // Is Info part allowed?
this_p = ax25_new();
if (this_p == NULL) return (NULL);
this_p->modulo = 0;
if (!set_addrs(this_p, addrs, num_addr, cr)) {
Debugprintf("Internal error in %s: Could not set addresses for U frame.\n", __func__);
ax25_delete(this_p);
return (NULL);
}
switch (ftype) {
// 1 = cmd only, 0 = res only, 2 = either
case frame_type_U_SABME: ctrl = 0x6f; t = 1; break;
case frame_type_U_SABM: ctrl = 0x2f; t = 1; break;
case frame_type_U_DISC: ctrl = 0x43; t = 1; break;
case frame_type_U_DM: ctrl = 0x0f; t = 0; break;
case frame_type_U_UA: ctrl = 0x63; t = 0; break;
case frame_type_U_FRMR: ctrl = 0x87; t = 0; i = 1; break;
case frame_type_U_UI: ctrl = 0x03; t = 2; i = 1; break;
case frame_type_U_XID: ctrl = 0xaf; t = 2; i = 1; break;
case frame_type_U_TEST: ctrl = 0xe3; t = 2; i = 1; break;
default:
Debugprintf("Internal error in %s: Invalid ftype %d for U frame.\n", __func__, ftype);
ax25_delete(this_p);
return (NULL);
break;
}
if (pf) ctrl |= 0x10;
if (t != 2) {
if (cr != t) {
Debugprintf("Internal error in %s: U frame, cr is %d but must be %d. ftype=%d\n", __func__, cr, t, ftype);
}
}
p = this_p->frame_data + this_p->frame_len;
*p++ = ctrl;
this_p->frame_len++;
if (ftype == frame_type_U_UI) {
// Definitely don't want pid value of 0 (not in valid list)
// or 0xff (which means more bytes follow).
if (pid < 0 || pid == 0 || pid == 0xff) {
Debugprintf("Internal error in %s: U frame, Invalid pid value 0x%02x.\n", __func__, pid);
pid = AX25_PID_NO_LAYER_3;
}
*p++ = pid;
this_p->frame_len++;
}
if (i) {
if (pinfo != NULL && info_len > 0) {
if (info_len > AX25_MAX_INFO_LEN) {
Debugprintf("Internal error in %s: U 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 U frame type.\n", __func__);
}
}
*p = '\0';
//assert(p == this_p->frame_data + this_p->frame_len);
//assert(this_p->magic1 == MAGIC);
//assert(this_p->magic2 == MAGIC);
#if PAD2TEST
ax25_frame_type_t check_ftype;
cmdres_t check_cr;
char check_desc[80];
int check_pf;
int check_nr;
int check_ns;
check_ftype = ax25_frame_type(this_p, &check_cr, check_desc, &check_pf, &check_nr, &check_ns);
text_color_set(DW_COLOR_DEBUG);
Debugprintf("check: ftype=%d, desc=\"%s\", pf=%d\n", check_ftype, check_desc, check_pf);
assert(check_cr == cr);
assert(check_ftype == ftype);
assert(check_pf == pf);
assert(check_nr == -1);
assert(check_ns == -1);
#endif
return (this_p);
} /* end ax25_u_frame */
static const char *position_name[1 + AX25_MAX_ADDRS] = {
"", "Destination ", "Source ",
"Digi1 ", "Digi2 ", "Digi3 ", "Digi4 ",
"Digi5 ", "Digi6 ", "Digi7 ", "Digi8 " };
int ax25_parse_addr(int position, char *in_addr, int strict, char *out_addr, int *out_ssid, int *out_heard)
{
char *p;
char sstr[8]; /* Should be 1 or 2 digits for SSID. */
int i, j, k;
int maxlen;
*out_addr = '\0';
*out_ssid = 0;
*out_heard = 0;
// Debugprintf ("ax25_parse_addr in: position=%d, '%s', strict=%d\n", position, in_addr, strict);
if (position < -1) position = -1;
if (position > AX25_REPEATER_8) position = AX25_REPEATER_8;
position++; /* Adjust for position_name above. */
if (strlen(in_addr) == 0) {
Debugprintf("%sAddress \"%s\" is empty.\n", position_name[position], in_addr);
return 0;
}
if (strict && strlen(in_addr) >= 2 && strncmp(in_addr, "qA", 2) == 0) {
Debugprintf("%sAddress \"%s\" is a \"q-construct\" used for communicating with\n", position_name[position], in_addr);
Debugprintf("APRS Internet Servers. It should never appear when going over the radio.\n");
}
// Debugprintf ("ax25_parse_addr in: %s\n", in_addr);
maxlen = strict ? 6 : (AX25_MAX_ADDR_LEN - 1);
p = in_addr;
i = 0;
for (p = in_addr; *p != '\0' && *p != '-' && *p != '*'; p++) {
if (i >= maxlen) {
Debugprintf("%sAddress is too long. \"%s\" has more than %d characters.\n", position_name[position], in_addr, maxlen);
return 0;
}
if (!isalnum(*p)) {
Debugprintf("%sAddress, \"%s\" contains character other than letter or digit in character position %d.\n", position_name[position], in_addr, (int)(long)(p - in_addr) + 1);
return 0;
}
out_addr[i++] = *p;
out_addr[i] = '\0';
#if DECAMAIN // Hack when running in decode_aprs utility.
// Exempt the "qA..." case because it was already mentioned.
if (strict && islower(*p) && strncmp(in_addr, "qA", 2) != 0) {
text_color_set(DW_COLOR_ERROR);
Debugprintf("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
}
#else
if (strict && islower(*p)) {
Debugprintf("%sAddress has lower case letters. \"%s\" must be all upper case.\n", position_name[position], in_addr);
return 0;
}
#endif
}
j = 0;
sstr[j] = '\0';
if (*p == '-') {
for (p++; isalnum(*p); p++) {
if (j >= 2) {
Debugprintf("%sSSID is too long. SSID part of \"%s\" has more than 2 characters.\n", position_name[position], in_addr);
return 0;
}
sstr[j++] = *p;
sstr[j] = '\0';
if (strict && !isdigit(*p)) {
Debugprintf("%sSSID must be digits. \"%s\" has letters in SSID.\n", position_name[position], in_addr);
return 0;
}
}
k = atoi(sstr);
if (k < 0 || k > 15) {
Debugprintf("%sSSID out of range. SSID of \"%s\" not in range of 0 to 15.\n", position_name[position], in_addr);
return 0;
}
*out_ssid = k;
}
if (*p == '*') {
*out_heard = 1;
p++;
if (strict == 2) {
Debugprintf("\"*\" is not allowed at end of address \"%s\" here.\n", in_addr);
return 0;
}
}
if (*p != '\0') {
Debugprintf("Invalid character \"%c\" found in %saddress \"%s\".\n", *p, position_name[position], in_addr);
return 0;
}
// Debugprintf ("ax25_parse_addr out: '%s' %d %d\n", out_addr, *out_ssid, *out_heard);
return (1);
} /* end ax25_parse_addr */
int set_addrs(packet_t pp, char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN], int num_addr, cmdres_t cr)
{
int n;
//assert(pp->frame_len == 0);
//assert(cr == cr_cmd || cr == cr_res);
if (num_addr < AX25_MIN_ADDRS || num_addr > AX25_MAX_ADDRS) {
Debugprintf("INTERNAL ERROR: %s %s %d, num_addr = %d\n", __FILE__, __func__, __LINE__, num_addr);
return (0);
}
for (n = 0; n < num_addr; n++) {
unsigned char *pa = pp->frame_data + n * 7;
int ok;
int strict = 1;
char oaddr[AX25_MAX_ADDR_LEN];
int ssid;
int heard;
int j;
ok = ax25_parse_addr(n, addrs[n], strict, oaddr, &ssid, &heard);
if (!ok) return (0);
// Fill in address.
memset(pa, ' ' << 1, 6);
for (j = 0; oaddr[j]; j++) {
pa[j] = oaddr[j] << 1;
}
pa += 6;
// Fill in SSID.
*pa = 0x60 | ((ssid & 0xf) << 1);
// Command / response flag.
switch (n) {
case AX25_DESTINATION:
if (cr == cr_cmd) *pa |= 0x80;
break;
case AX25_SOURCE:
if (cr == cr_res) *pa |= 0x80;
break;
default:
break;
}
// Is this the end of address field?
if (n == num_addr - 1) {
*pa |= 1;
}
pp->frame_len += 7;
}
pp->num_addr = num_addr;
return (1);
} /* end set_addrs */
///////////////////////////////////////////////////////////////////////////////
//
// il2p_init.c
//
///////////////////////////////////////////////////////////////////////////////
// Init must be called at start of application.
extern void il2p_init(int debug);
extern struct rs *il2p_find_rs(int nparity); // Internal later?
extern void il2p_encode_rs(unsigned char *tx_data, int data_size, int num_parity, unsigned char *parity_out);
extern int il2p_decode_rs(unsigned char *rec_block, int data_size, int num_parity, unsigned char *out);
extern int il2p_get_debug(void);
extern void il2p_set_debug(int debug);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_rec.c
//
///////////////////////////////////////////////////////////////////////////////
// Receives a bit stream from demodulator.
extern void il2p_rec_bit(int chan, int subchan, int slice, int dbit);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_send.c
//
///////////////////////////////////////////////////////////////////////////////
// Send bit stream to modulator.
string * il2p_send_frame(int chan, packet_t pp, int max_fec, int polarity);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_codec.c
//
///////////////////////////////////////////////////////////////////////////////
extern int il2p_encode_frame(packet_t pp, int max_fec, unsigned char *iout);
packet_t il2p_decode_frame(unsigned char *irec);
packet_t il2p_decode_header_payload(unsigned char* uhdr, unsigned char *epayload, int *symbols_corrected);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_header.c
//
///////////////////////////////////////////////////////////////////////////////
extern int il2p_type_1_header(packet_t pp, int max_fec, unsigned char *hdr);
extern packet_t il2p_decode_header_type_1(unsigned char *hdr, int num_sym_changed);
extern int il2p_type_0_header(packet_t pp, int max_fec, unsigned char *hdr);
extern int il2p_clarify_header(unsigned char *rec_hdr, unsigned char *corrected_descrambled_hdr);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_scramble.c
//
///////////////////////////////////////////////////////////////////////////////
extern void il2p_scramble_block(unsigned char *in, unsigned char *out, int len);
extern void il2p_descramble_block(unsigned char *in, unsigned char *out, int len);
///////////////////////////////////////////////////////////////////////////////
//
// il2p_payload.c
//
///////////////////////////////////////////////////////////////////////////////
typedef struct {
int payload_byte_count; // Total size, 0 thru 1023
int payload_block_count;
int small_block_size;
int large_block_size;
int large_block_count;
int small_block_count;
int parity_symbols_per_block; // 2, 4, 6, 8, 16
} il2p_payload_properties_t;
extern int il2p_payload_compute(il2p_payload_properties_t *p, int payload_size, int max_fec);
extern int il2p_encode_payload(unsigned char *payload, int payload_size, int max_fec, unsigned char *enc);
extern int il2p_decode_payload(unsigned char *received, int payload_size, int max_fec, unsigned char *payload_out, int *symbols_corrected);
extern int il2p_get_header_attributes(unsigned char *hdr, int *hdr_type, int *max_fec);
#endif
// Interesting related stuff:
// https://www.kernel.org/doc/html/v4.15/core-api/librs.html
// https://berthub.eu/articles/posts/reed-solomon-for-programmers/
#define MAX_NROOTS 16
#define NTAB 5
static struct {
int symsize; // Symbol size, bits (1-8). Always 8 for this application.
int genpoly; // Field generator polynomial coefficients.
int fcs; // First root of RS code generator polynomial, index form.
// FX.25 uses 1 but IL2P uses 0.
int prim; // Primitive element to generate polynomial roots.
int nroots; // RS code generator polynomial degree (number of roots).
// Same as number of check bytes added.
struct rs *rs; // Pointer to RS codec control block. Filled in at init time.
} Tab[NTAB] = {
{8, 0x11d, 0, 1, 2, NULL }, // 2 parity
{8, 0x11d, 0, 1, 4, NULL }, // 4 parity
{8, 0x11d, 0, 1, 6, NULL }, // 6 parity
{8, 0x11d, 0, 1, 8, NULL }, // 8 parity
{8, 0x11d, 0, 1, 16, NULL }, // 16 parity
};
static int g_il2p_debug = 0;
/*-------------------------------------------------------------
*
* Name: il2p_init
*
* Purpose: This must be called at application start up time.
* It sets up tables for the Reed-Solomon functions.
*
* Inputs: debug - Enable debug output.
*
*--------------------------------------------------------------*/
void il2p_init(int il2p_debug)
{
g_il2p_debug = il2p_debug;
for (int i = 0; i < NTAB; i++) {
//assert(Tab[i].nroots <= MAX_NROOTS);
Tab[i].rs = INIT_RS(Tab[i].symsize, Tab[i].genpoly, Tab[i].fcs, Tab[i].prim, Tab[i].nroots);
if (Tab[i].rs == NULL) {
Debugprintf("IL2P internal error: init_rs_char failed!\n");
exit(0);
}
}
openTraceLog();
} // end il2p_init
int il2p_get_debug(void)
{
return (g_il2p_debug);
}
void il2p_set_debug(int debug)
{
g_il2p_debug = debug;
}
// Find RS codec control block for specified number of parity symbols.
struct rs *il2p_find_rs(int nparity)
{
for (int n = 0; n < NTAB; n++) {
if (Tab[n].nroots == nparity) {
return (Tab[n].rs);
}
}
Debugprintf("IL2P INTERNAL ERROR: il2p_find_rs: control block not found for nparity = %d.\n", nparity);
return (Tab[0].rs);
}
/*-------------------------------------------------------------
*
* Name: void il2p_encode_rs
*
* Purpose: Add parity symbols to a block of data.
*
* Inputs: tx_data Header or other data to transmit.
* data_size Number of data bytes in above.
* num_parity Number of parity symbols to add.
* Maximum of IL2P_MAX_PARITY_SYMBOLS.
*
* Outputs: parity_out Specified number of parity symbols
*
* Restriction: data_size + num_parity <= 255 which is the RS block size.
* The caller must ensure this.
*
*--------------------------------------------------------------*/
void il2p_encode_rs(unsigned char *tx_data, int data_size, int num_parity, unsigned char *parity_out)
{
//assert(data_size >= 1);
//assert(num_parity == 2 || num_parity == 4 || num_parity == 6 || num_parity == 8 || num_parity == 16);
//assert(data_size + num_parity <= 255);
unsigned char rs_block[FX25_BLOCK_SIZE];
memset(rs_block, 0, sizeof(rs_block));
memcpy(rs_block + sizeof(rs_block) - data_size - num_parity, tx_data, data_size);
ENCODE_RS(il2p_find_rs(num_parity), rs_block, parity_out);
}
/*-------------------------------------------------------------
*
* Name: void il2p_decode_rs
*
* Purpose: Check and attempt to fix block with FEC.
*
* Inputs: rec_block Received block composed of data and parity.
* Total size is sum of following two parameters.
* data_size Number of data bytes in above.
* num_parity Number of parity symbols (bytes) in above.
*
* Outputs: out Original with possible corrections applied.
* data_size bytes.
*
* Returns: -1 for unrecoverable.
* >= 0 for success. Number of symbols corrected.
*
*--------------------------------------------------------------*/
int il2p_decode_rs(unsigned char *rec_block, int data_size, int num_parity, unsigned char *out)
{
// Use zero padding in front if data size is too small.
int n = data_size + num_parity; // total size in.
unsigned char rs_block[FX25_BLOCK_SIZE];
// We could probably do this more efficiently by skipping the
// processing of the bytes known to be zero. Good enough for now.
memset(rs_block, 0, sizeof(rs_block) - n);
memcpy(rs_block + sizeof(rs_block) - n, rec_block, n);
if (il2p_get_debug() >= 3) {
Debugprintf("============================== il2p_decode_rs ==============================\n");
Debugprintf("%d filler zeros, %d data, %d parity\n", (int)(sizeof(rs_block) - n), data_size, num_parity);
fx_hex_dump(rs_block, sizeof(rs_block));
}
int derrlocs[FX25_MAX_CHECK]; // Half would probably be OK.
int derrors = DECODE_RS(il2p_find_rs(num_parity), rs_block, derrlocs, 0);
memcpy(out, rs_block + sizeof(rs_block) - n, data_size);
if (il2p_get_debug() >= 3) {
if (derrors == 0) {
Debugprintf("No errors reported for RS block.\n");
}
else if (derrors > 0) {
Debugprintf("%d errors fixed in positions:\n", derrors);
for (int j = 0; j < derrors; j++) {
Debugprintf(" %3d (0x%02x)\n", derrlocs[j], derrlocs[j]);
}
fx_hex_dump(rs_block, sizeof(rs_block));
}
}
// It is possible to have a situation where too many errors are
// present but the algorithm could get a good code block by "fixing"
// one of the padding bytes that should be 0.
for (int i = 0; i < derrors; i++) {
if (derrlocs[i] < sizeof(rs_block) - n) {
if (il2p_get_debug() >= 3) {
Debugprintf("RS DECODE ERROR! Padding position %d should be 0 but it was set to %02x.\n", derrlocs[i], rs_block[derrlocs[i]]);
}
derrors = -1;
break;
}
}
if (il2p_get_debug() >= 3) {
Debugprintf("============================== il2p_decode_rs returns %d ==============================\n", derrors);
}
return (derrors);
}
// end il2p_init.c
void ENCODE_RS(struct rs * rs, DTYPE * data, DTYPE * bb)
{
int i, j;
DTYPE feedback;
memset(bb, 0, NROOTS * sizeof(DTYPE)); // clear out the FEC data area
for (i = 0; i < NN - NROOTS; i++) {
feedback = INDEX_OF[data[i] ^ bb[0]];
if (feedback != A0) { /* feedback term is non-zero */
for (j = 1; j < NROOTS; j++)
bb[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS - j])];
}
/* Shift */
memmove(&bb[0], &bb[1], sizeof(DTYPE)*(NROOTS - 1));
if (feedback != A0)
bb[NROOTS - 1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
else
bb[NROOTS - 1] = 0;
}
}
int DECODE_RS(struct rs * rs, DTYPE * data, int *eras_pos, int no_eras) {
int deg_lambda, el, deg_omega;
int i, j, r, k;
DTYPE u, q, tmp, num1, num2, den, discr_r;
// DTYPE lambda[NROOTS+1], s[NROOTS]; /* Err+Eras Locator poly and syndrome poly */
// DTYPE b[NROOTS+1], t[NROOTS+1], omega[NROOTS+1];
// DTYPE root[NROOTS], reg[NROOTS+1], loc[NROOTS];
DTYPE lambda[FX25_MAX_CHECK + 1], s[FX25_MAX_CHECK]; /* Err+Eras Locator poly and syndrome poly */
DTYPE b[FX25_MAX_CHECK + 1], t[FX25_MAX_CHECK + 1], omega[FX25_MAX_CHECK + 1];
DTYPE root[FX25_MAX_CHECK], reg[FX25_MAX_CHECK + 1], loc[FX25_MAX_CHECK];
int syn_error, count;
/* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
for (i = 0; i < NROOTS; i++)
s[i] = data[0];
for (j = 1; j < NN; j++) {
for (i = 0; i < NROOTS; i++) {
if (s[i] == 0) {
s[i] = data[j];
}
else {
s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR + i)*PRIM)];
}
}
}
/* Convert syndromes to index form, checking for nonzero condition */
syn_error = 0;
for (i = 0; i < NROOTS; i++) {
syn_error |= s[i];
s[i] = INDEX_OF[s[i]];
}
// fprintf(stderr,"syn_error = %4x\n",syn_error);
if (!syn_error) {
/* if syndrome is zero, data[] is a codeword and there are no
* errors to correct. So return data[] unmodified
*/
count = 0;
goto finish;
}
memset(&lambda[1], 0, NROOTS * sizeof(lambda[0]));
lambda[0] = 1;
if (no_eras > 0) {
/* Init lambda to be the erasure locator polynomial */
lambda[1] = ALPHA_TO[MODNN(PRIM*(NN - 1 - eras_pos[0]))];
for (i = 1; i < no_eras; i++) {
u = MODNN(PRIM*(NN - 1 - eras_pos[i]));
for (j = i + 1; j > 0; j--) {
tmp = INDEX_OF[lambda[j - 1]];
if (tmp != A0)
lambda[j] ^= ALPHA_TO[MODNN(u + tmp)];
}
}
#if DEBUG >= 1
/* Test code that verifies the erasure locator polynomial just constructed
Needed only for decoder debugging. */
/* find roots of the erasure location polynomial */
for (i = 1; i <= no_eras; i++)
reg[i] = INDEX_OF[lambda[i]];
count = 0;
for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) {
q = 1;
for (j = 1; j <= no_eras; j++)
if (reg[j] != A0) {
reg[j] = MODNN(reg[j] + j);
q ^= ALPHA_TO[reg[j]];
}
if (q != 0)
continue;
/* store root and error location number indices */
root[count] = i;
loc[count] = k;
count++;
}
if (count != no_eras) {
fprintf(stderr, "count = %d no_eras = %d\n lambda(x) is WRONG\n", count, no_eras);
count = -1;
goto finish;
}
#if DEBUG >= 2
fprintf(stderr, "\n Erasure positions as determined by roots of Eras Loc Poly:\n");
for (i = 0; i < count; i++)
fprintf(stderr, "%d ", loc[i]);
fprintf(stderr, "\n");
#endif
#endif
}
for (i = 0; i < NROOTS + 1; i++)
b[i] = INDEX_OF[lambda[i]];
/*
* Begin Berlekamp-Massey algorithm to determine error+erasure
* locator polynomial
*/
r = no_eras;
el = no_eras;
while (++r <= NROOTS) { /* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
discr_r = 0;
for (i = 0; i < r; i++) {
if ((lambda[i] != 0) && (s[r - i - 1] != A0)) {
discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r - i - 1])];
}
}
discr_r = INDEX_OF[discr_r]; /* Index form */
if (discr_r == A0) {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1], b, NROOTS * sizeof(b[0]));
b[0] = A0;
}
else {
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
t[0] = lambda[0];
for (i = 0; i < NROOTS; i++) {
if (b[i] != A0)
t[i + 1] = lambda[i + 1] ^ ALPHA_TO[MODNN(discr_r + b[i])];
else
t[i + 1] = lambda[i + 1];
}
if (2 * el <= r + no_eras - 1) {
el = r + no_eras - el;
/*
* 2 lines below: B(x) <-- inv(discr_r) *
* lambda(x)
*/
for (i = 0; i <= NROOTS; i++)
b[i] = (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN);
}
else {
/* 2 lines below: B(x) <-- x*B(x) */
memmove(&b[1], b, NROOTS * sizeof(b[0]));
b[0] = A0;
}
memcpy(lambda, t, (NROOTS + 1) * sizeof(t[0]));
}
}
/* Convert lambda to index form and compute deg(lambda(x)) */
deg_lambda = 0;
for (i = 0; i < NROOTS + 1; i++) {
lambda[i] = INDEX_OF[lambda[i]];
if (lambda[i] != A0)
deg_lambda = i;
}
/* Find roots of the error+erasure locator polynomial by Chien search */
memcpy(®[1], &lambda[1], NROOTS * sizeof(reg[0]));
count = 0; /* Number of roots of lambda(x) */
for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) {
q = 1; /* lambda[0] is always 0 */
for (j = deg_lambda; j > 0; j--) {
if (reg[j] != A0) {
reg[j] = MODNN(reg[j] + j);
q ^= ALPHA_TO[reg[j]];
}
}
if (q != 0)
continue; /* Not a root */
/* store root (index-form) and error location number */
#if DEBUG>=2
fprintf(stderr, "count %d root %d loc %d\n", count, i, k);
#endif
root[count] = i;
loc[count] = k;
/* If we've already found max possible roots,
* abort the search to save time
*/
if (++count == deg_lambda)
break;
}
if (deg_lambda != count) {
/*
* deg(lambda) unequal to number of roots => uncorrectable
* error detected
*/
count = -1;
goto finish;
}
/*
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
* x**NROOTS). in index form. Also find deg(omega).
*/
deg_omega = 0;
for (i = 0; i < NROOTS; i++) {
tmp = 0;
j = (deg_lambda < i) ? deg_lambda : i;
for (; j >= 0; j--) {
if ((s[i - j] != A0) && (lambda[j] != A0))
tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])];
}
if (tmp != 0)
deg_omega = i;
omega[i] = INDEX_OF[tmp];
}
omega[NROOTS] = A0;
/*
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
* inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
*/
for (j = count - 1; j >= 0; j--) {
num1 = 0;
for (i = deg_omega; i >= 0; i--) {
if (omega[i] != A0)
num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])];
}
num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)];
den = 0;
/* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
for (i = min(deg_lambda, NROOTS - 1) & ~1; i >= 0; i -= 2) {
if (lambda[i + 1] != A0)
den ^= ALPHA_TO[MODNN(lambda[i + 1] + i * root[j])];
}
if (den == 0) {
#if DEBUG >= 1
fprintf(stderr, "\n ERROR: denominator = 0\n");
#endif
count = -1;
goto finish;
}
/* Apply error to data */
if (num1 != 0) {
data[loc[j]] ^= ALPHA_TO[MODNN(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])];
}
}
finish:
if (eras_pos != NULL) {
for (i = 0; i < count; i++)
eras_pos[i] = loc[i];
}
return count;
}
struct rs *INIT_RS(unsigned int symsize, unsigned int gfpoly, unsigned fcr, unsigned prim,
unsigned int nroots) {
struct rs *rs;
int i, j, sr, root, iprim;
if (symsize > 8 * sizeof(DTYPE))
return NULL; /* Need version with ints rather than chars */
if (fcr >= (1 << symsize))
return NULL;
if (prim == 0 || prim >= (1 << symsize))
return NULL;
if (nroots >= (1 << symsize))
return NULL; /* Can't have more roots than symbol values! */
rs = (struct rs *)calloc(1, sizeof(struct rs));
if (rs == NULL) {
Debugprintf("FATAL ERROR: Out of memory.\n");
exit(0);
}
rs->mm = symsize;
rs->nn = (1 << symsize) - 1;
rs->alpha_to = (DTYPE *)calloc((rs->nn + 1), sizeof(DTYPE));
if (rs->alpha_to == NULL) {
Debugprintf("FATAL ERROR: Out of memory.\n");
exit(0);
}
rs->index_of = (DTYPE *)calloc((rs->nn + 1), sizeof(DTYPE));
if (rs->index_of == NULL) {
Debugprintf("FATAL ERROR: Out of memory.\n");
exit(0);
}
/* Generate Galois field lookup tables */
rs->index_of[0] = A0; /* log(zero) = -inf */
rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
sr = 1;
for (i = 0; i < rs->nn; i++) {
rs->index_of[sr] = i;
rs->alpha_to[i] = sr;
sr <<= 1;
if (sr & (1 << symsize))
sr ^= gfpoly;
sr &= rs->nn;
}
if (sr != 1) {
/* field generator polynomial is not primitive! */
free(rs->alpha_to);
free(rs->index_of);
free(rs);
return NULL;
}
/* Form RS code generator polynomial from its roots */
rs->genpoly = (DTYPE *)calloc((nroots + 1), sizeof(DTYPE));
if (rs->genpoly == NULL) {
Debugprintf("FATAL ERROR: Out of memory.\n");
exit(0);
}
rs->fcr = fcr;
rs->prim = prim;
rs->nroots = nroots;
/* Find prim-th root of 1, used in decoding */
for (iprim = 1; (iprim % prim) != 0; iprim += rs->nn)
;
rs->iprim = iprim / prim;
rs->genpoly[0] = 1;
for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
rs->genpoly[i + 1] = 1;
/* Multiply rs->genpoly[] by @**(root + x) */
for (j = i; j > 0; j--) {
if (rs->genpoly[j] != 0)
rs->genpoly[j] = rs->genpoly[j - 1] ^ rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[j]] + root)];
else
rs->genpoly[j] = rs->genpoly[j - 1];
}
/* rs->genpoly[0] can never be zero */
rs->genpoly[0] = rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[0]] + root)];
}
/* convert rs->genpoly[] to index form for quicker encoding */
for (i = 0; i <= nroots; i++) {
rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
}
// diagnostic prints
#if 0
printf("Alpha To:\n\r");
for (i = 0; i < sizeof(DTYPE)*(rs->nn + 1); i++)
printf("0x%2x,", rs->alpha_to[i]);
printf("\n\r");
printf("Index Of:\n\r");
for (i = 0; i < sizeof(DTYPE)*(rs->nn + 1); i++)
printf("0x%2x,", rs->index_of[i]);
printf("\n\r");
printf("GenPoly:\n\r");
for (i = 0; i <= nroots; i++)
printf("0x%2x,", rs->genpoly[i]);
printf("\n\r");
#endif
return rs;
}
// TEMPORARY!!!
// FIXME: We already have multiple copies of this.
// Consolidate them into one somewhere.
void fx_hex_dump(unsigned char *p, int len)
{
int n, i, offset;
offset = 0;
while (len > 0) {
n = len < 16 ? len : 16;
Debugprintf(" %03x: ", offset);
for (i = 0; i < n; i++) {
Debugprintf(" %02x", p[i]);
}
for (i = n; i < 16; i++) {
Debugprintf(" ");
}
Debugprintf(" ");
for (i = 0; i < n; i++) {
Debugprintf("%c", isprint(p[i]) ? p[i] : '.');
}
Debugprintf("\n");
p += 16;
offset += 16;
len -= 16;
}
}
/*-------------------------------------------------------------
*
* File: il2p_codec.c
*
* Purpose: Convert IL2P encoded format from and to direwolf internal packet format.
*
*--------------------------------------------------------------*/
/*-------------------------------------------------------------
*
* Name: il2p_encode_frame
*
* Purpose: Convert AX.25 frame to IL2P encoding.
*
* Inputs: chan - Audio channel number, 0 = first.
*
* pp - Packet object pointer.
*
* max_fec - 1 to send maximum FEC size rather than automatic.
*
* Outputs: iout - Encoded result, excluding the 3 byte sync word.
* Caller should provide IL2P_MAX_PACKET_SIZE bytes.
*
* Returns: Number of bytes for transmission.
* -1 is returned for failure.
*
* Description: Encode into IL2P format.
*
* Errors: If something goes wrong, return -1.
*
* Most likely reason is that the frame is too large.
* IL2P has a max payload size of 1023 bytes.
* For a type 1 header, this is the maximum AX.25 Information part size.
* For a type 0 header, this is the entire AX.25 frame.
*
*--------------------------------------------------------------*/
int il2p_encode_frame(packet_t pp, int max_fec, unsigned char *iout)
{
// Can a type 1 header be used?
unsigned char hdr[IL2P_HEADER_SIZE + IL2P_HEADER_PARITY];
int e;
int out_len = 0;
debugTimeStamp("TX Raw Packet is", 'T');
debugHexDump(pp->frame_data, pp->frame_len, 'T');
e = il2p_type_1_header(pp, max_fec, hdr);
if (e >= 0) {
il2p_scramble_block(hdr, iout, IL2P_HEADER_SIZE);
il2p_encode_rs(iout, IL2P_HEADER_SIZE, IL2P_HEADER_PARITY, iout + IL2P_HEADER_SIZE);
out_len = IL2P_HEADER_SIZE + IL2P_HEADER_PARITY;
if (e == 0) {
// Success. No info part.
debugTimeStamp("TX Type 1 IL2P Packet no info is", 'T');
debugHexDump(iout, out_len, 'R');
return (out_len);
}
// Payload is AX.25 info part.
unsigned char *pinfo;
int info_len;
info_len = ax25_get_info(pp, &pinfo);
int k = il2p_encode_payload(pinfo, info_len, max_fec, iout + out_len);
if (k > 0) {
out_len += k;
// Success. Info part was <= 1023 bytes.
debugTimeStamp("TX Type 1 IL2P Packet is", 'T');
debugHexDump(iout, out_len, 'T');
return (out_len);
}
// Something went wrong with the payload encoding.
return (-1);
}
else if (e == -1) {
// Could not use type 1 header for some reason.
// e.g. More than 2 addresses, extended (mod 128) sequence numbers, etc.
e = il2p_type_0_header(pp, max_fec, hdr);
if (e > 0) {
il2p_scramble_block(hdr, iout, IL2P_HEADER_SIZE);
il2p_encode_rs(iout, IL2P_HEADER_SIZE, IL2P_HEADER_PARITY, iout + IL2P_HEADER_SIZE);
out_len = IL2P_HEADER_SIZE + IL2P_HEADER_PARITY;
// Payload is entire AX.25 frame.
unsigned char *frame_data_ptr = ax25_get_frame_data_ptr(pp);
int frame_len = ax25_get_frame_len(pp);
int k = il2p_encode_payload(frame_data_ptr, frame_len, max_fec, iout + out_len);
if (k > 0) {
out_len += k;
// Success. Entire AX.25 frame <= 1023 bytes.
debugTimeStamp("TX Type 2 IL2P Packet is", 'T');
debugHexDump(iout, out_len, 'T');
return (out_len);
}
// Something went wrong with the payload encoding.
return (-1);
}
else if (e == 0) {
// Impossible condition. Type 0 header must have payload.
return (-1);
}
else {
// AX.25 frame is too large.
return (-1);
}
}
// AX.25 Information part is too large.
return (-1);
}
/*-------------------------------------------------------------
*
* Name: il2p_decode_frame
*
* Purpose: Convert IL2P encoding to AX.25 frame.
* This is only used during testing, with a whole encoded frame.
* During reception, the header would have FEC and descrambling
* applied first so we would know how much to collect for the payload.
*
* Inputs: irec - Received IL2P frame excluding the 3 byte sync word.
*
* Future Out: Number of symbols corrected.
*
* Returns: Packet pointer or NULL for error.
*
*--------------------------------------------------------------*/
packet_t il2p_decode_frame(unsigned char *irec)
{
unsigned char uhdr[IL2P_HEADER_SIZE]; // After FEC and descrambling.
int e = il2p_clarify_header(irec, uhdr);
// TODO?: for symmetry we might want to clarify the payload before combining.
return (il2p_decode_header_payload(uhdr, irec + IL2P_HEADER_SIZE + IL2P_HEADER_PARITY, &e));
}
/*-------------------------------------------------------------
*
* Name: il2p_decode_header_payload
*
* Purpose: Convert IL2P encoding to AX.25 frame
*
* Inputs: uhdr - Received header after FEC and descrambling.
* epayload - Encoded payload.
*
* In/Out: symbols_corrected - Symbols (bytes) corrected in the header.
* Should be 0 or 1 because it has 2 parity symbols.
* Here we add number of corrections for the payload.
*
* Returns: Packet pointer or NULL for error.
*
*--------------------------------------------------------------*/
packet_t il2p_decode_header_payload(unsigned char* uhdr, unsigned char *epayload, int *symbols_corrected)
{
int hdr_type;
int max_fec;
int payload_len = il2p_get_header_attributes(uhdr, &hdr_type, &max_fec);
packet_t pp = NULL;
if (hdr_type == 1) {
// Header type 1. Any payload is the AX.25 Information part.
pp = il2p_decode_header_type_1(uhdr, *symbols_corrected);
if (pp == NULL) {
// Failed for some reason.
return (NULL);
}
if (payload_len > 0) {
// This is the AX.25 Information part.
unsigned char extracted[IL2P_MAX_PAYLOAD_SIZE];
int e = il2p_decode_payload(epayload, payload_len, max_fec, extracted, symbols_corrected);
// It would be possible to have a good header but too many errors in the payload.
if (e <= 0) {
ax25_delete(pp);
pp = NULL;
return (pp);
}
if (e != payload_len) {
Debugprintf("IL2P Internal Error: %s(): hdr_type=%d, max_fec=%d, payload_len=%d, e=%d.\n", __func__, hdr_type, max_fec, payload_len, e);
}
ax25_set_info(pp, extracted, payload_len);
}
return (pp);
}
else {
// Header type 0. The payload is the entire AX.25 frame.
unsigned char extracted[IL2P_MAX_PAYLOAD_SIZE];
int e = il2p_decode_payload(epayload, payload_len, max_fec, extracted, symbols_corrected);
if (e <= 0) { // Payload was not received correctly.
return (NULL);
}
if (e != payload_len) {
Debugprintf("IL2P Internal Error: %s(): hdr_type=%d, e=%d, payload_len=%d\n", __func__, hdr_type, e, payload_len);
return (NULL);
}
alevel_t alevel;
memset(&alevel, 0, sizeof(alevel));
//alevel = demod_get_audio_level (chan, subchan); // What TODO? We don't know channel here.
// I think alevel gets filled in somewhere later making
// this redundant.
pp = ax25_from_frame(extracted, payload_len, alevel);
return (pp);
}
} // end il2p_decode_header_payload
// end il2p_codec.c
/*--------------------------------------------------------------------------------
*
* File: il2p_header.c
*
* Purpose: Functions to deal with the IL2P header.
*
* Reference: http://tarpn.net/t/il2p/il2p-specification0-4.pdf
*
*--------------------------------------------------------------------------------*/
// Convert ASCII to/from DEC SIXBIT as defined here:
// https://en.wikipedia.org/wiki/Six-bit_character_code#DEC_six-bit_code
static inline int ascii_to_sixbit(int a)
{
if (a >= ' ' && a <= '_') return (a - ' ');
return (31); // '?' for any invalid.
}
static inline int sixbit_to_ascii(int s)
{
return (s + ' ');
}
// Functions for setting the various header fields.
// It is assumed that it was zeroed first so only the '1' bits are set.
static void set_field(unsigned char *hdr, int bit_num, int lsb_index, int width, int value)
{
while (width > 0 && value != 0) {
//assert(lsb_index >= 0 && lsb_index <= 11);
if (value & 1) {
hdr[lsb_index] |= 1 << bit_num;
}
value >>= 1;
lsb_index--;
width--;
}
//assert(value == 0);
}
#define SET_UI(hdr,val) set_field(hdr, 6, 0, 1, val)
#define SET_PID(hdr,val) set_field(hdr, 6, 4, 4, val)
#define SET_CONTROL(hdr,val) set_field(hdr, 6, 11, 7, val)
#define SET_FEC_LEVEL(hdr,val) set_field(hdr, 7, 0, 1, val)
#define SET_HDR_TYPE(hdr,val) set_field(hdr, 7, 1, 1, val)
#define SET_PAYLOAD_BYTE_COUNT(hdr,val) set_field(hdr, 7, 11, 10, val)
// Extracting the fields.
static int get_field(unsigned char *hdr, int bit_num, int lsb_index, int width)
{
int result = 0;
lsb_index -= width - 1;
while (width > 0) {
result <<= 1;
//assert(lsb_index >= 0 && lsb_index <= 11);
if (hdr[lsb_index] & (1 << bit_num)) {
result |= 1;
}
lsb_index++;
width--;
}
return (result);
}
#define GET_UI(hdr) get_field(hdr, 6, 0, 1)
#define GET_PID(hdr) get_field(hdr, 6, 4, 4)
#define GET_CONTROL(hdr) get_field(hdr, 6, 11, 7)
#define GET_FEC_LEVEL(hdr) get_field(hdr, 7, 0, 1)
#define GET_HDR_TYPE(hdr) get_field(hdr, 7, 1, 1)
#define GET_PAYLOAD_BYTE_COUNT(hdr) get_field(hdr, 7, 11, 10)
// AX.25 'I' and 'UI' frames have a protocol ID which determines how the
// information part should be interpreted.
// Here we squeeze the most common cases down to 4 bits.
// Return -1 if translation is not possible. Fall back to type 0 header in this case.
static int encode_pid(packet_t pp)
{
int pid = ax25_get_pid(pp);
if ((pid & 0x30) == 0x20) return (0x2); // AX.25 Layer 3
if ((pid & 0x30) == 0x10) return (0x2); // AX.25 Layer 3
if (pid == 0x01) return (0x3); // ISO 8208 / CCIT X.25 PLP
if (pid == 0x06) return (0x4); // Compressed TCP/IP
if (pid == 0x07) return (0x5); // Uncompressed TCP/IP
if (pid == 0x08) return (0x6); // Segmentation fragmen
if (pid == 0xcc) return (0xb); // ARPA Internet Protocol
if (pid == 0xcd) return (0xc); // ARPA Address Resolution
if (pid == 0xce) return (0xd); // FlexNet
if (pid == 0xcf) return (0xe); // TheNET
if (pid == 0xf0) return (0xf); // No L3
return (-1);
}
// Convert IL2P 4 bit PID to AX.25 8 bit PID.
static int decode_pid(int pid)
{
static const unsigned char axpid[16] = {
0xf0, // Should not happen. 0 is for 'S' frames.
0xf0, // Should not happen. 1 is for 'U' frames (but not UI).
0x20, // AX.25 Layer 3
0x01, // ISO 8208 / CCIT X.25 PLP
0x06, // Compressed TCP/IP
0x07, // Uncompressed TCP/IP
0x08, // Segmentation fragment
0xf0, // Future
0xf0, // Future
0xf0, // Future
0xf0, // Future
0xcc, // ARPA Internet Protocol
0xcd, // ARPA Address Resolution
0xce, // FlexNet
0xcf, // TheNET
0xf0 }; // No L3
//assert(pid >= 0 && pid <= 15);
return (axpid[pid]);
}
/*--------------------------------------------------------------------------------
*
* Function: il2p_type_1_header
*
* Purpose: Attempt to create type 1 header from packet object.
*
* Inputs: pp - Packet object.
*
* max_fec - 1 to use maximum FEC symbols , 0 for automatic.
*
* Outputs: hdr - IL2P header with no scrambling or parity symbols.
* Must be large enough to hold IL2P_HEADER_SIZE unsigned bytes.
*
* Returns: Number of bytes for information part or -1 for failure.
* In case of failure, fall back to type 0 transparent encapsulation.
*
* Description: Type 1 Headers do not support AX.25 repeater callsign addressing,
* Modulo-128 extended mode window sequence numbers, nor any callsign
* characters that cannot translate to DEC SIXBIT.
* If these cases are encountered during IL2P packet encoding,
* the encoder switches to Type 0 Transparent Encapsulation.
* SABME can't be handled by type 1.
*
*--------------------------------------------------------------------------------*/
int il2p_type_1_header(packet_t pp, int max_fec, unsigned char *hdr)
{
memset(hdr, 0, IL2P_HEADER_SIZE);
if (ax25_get_num_addr(pp) != 2) {
// Only two addresses are allowed for type 1 header.
return (-1);
}
// Check does not apply for 'U' frames but put in one place rather than two.
if (ax25_get_modulo(pp) == 128) return(-1);
// Destination and source addresses go into low bits 0-5 for bytes 0-11.
char dst_addr[AX25_MAX_ADDR_LEN];
char src_addr[AX25_MAX_ADDR_LEN];
ax25_get_addr_no_ssid(pp, AX25_DESTINATION, dst_addr);
int dst_ssid = ax25_get_ssid(pp, AX25_DESTINATION);
ax25_get_addr_no_ssid(pp, AX25_SOURCE, src_addr);
int src_ssid = ax25_get_ssid(pp, AX25_SOURCE);
if ((pp->frame_data[6] & 0x80) == (pp->frame_data[13] & 0x80))
{
// Both C bits are the same (ax.25 v1) so can't be sent as type 1 as will be changed
return -1;
}
unsigned char *a = (unsigned char *)dst_addr;
for (int i = 0; *a != '\0'; i++, a++) {
if (*a < ' ' || *a > '_') {
// Shouldn't happen but follow the rule.
return (-1);
}
hdr[i] = ascii_to_sixbit(*a);
}
a = (unsigned char *)src_addr;
for (int i = 6; *a != '\0'; i++, a++) {
if (*a < ' ' || *a > '_') {
// Shouldn't happen but follow the rule.
return (-1);
}
hdr[i] = ascii_to_sixbit(*a);
}
// Byte 12 has DEST SSID in upper nybble and SRC SSID in lower nybble and
hdr[12] = (dst_ssid << 4) | src_ssid;
ax25_frame_type_t frame_type;
cmdres_t cr; // command or response.
char description[64];
int pf; // Poll/Final.
int nr, ns; // Sequence numbers.
frame_type = ax25_frame_type(pp, &cr, description, &pf, &nr, &ns);
//Debugprintf ("%s(): %s-%d>%s-%d: %s\n", __func__, src_addr, src_ssid, dst_addr, dst_ssid, description);
switch (frame_type) {
case frame_type_S_RR: // Receive Ready - System Ready To Receive
case frame_type_S_RNR: // Receive Not Ready - TNC Buffer Full
case frame_type_S_REJ: // Reject Frame - Out of Sequence or Duplicate
case frame_type_S_SREJ: // Selective Reject - Request single frame repeat
// S frames (RR, RNR, REJ, SREJ), mod 8, have control N(R) P/F S S 0 1
// These are mapped into P/F N(R) C S S
// Bit 6 is not mentioned in documentation but it is used for P/F for the other frame types.
// C is copied from the C bit in the destination addr.
// C from source is not used here. Reception assumes it is the opposite.
// PID is set to 0, meaning none, for S frames.
SET_UI(hdr, 0);
SET_PID(hdr, 0);
SET_CONTROL(hdr, (pf << 6) | (nr << 3) | (((cr == cr_cmd) | (cr == cr_11)) << 2));
// This gets OR'ed into the above.
switch (frame_type) {
case frame_type_S_RR: SET_CONTROL(hdr, 0); break;
case frame_type_S_RNR: SET_CONTROL(hdr, 1); break;
case frame_type_S_REJ: SET_CONTROL(hdr, 2); break;
case frame_type_S_SREJ: SET_CONTROL(hdr, 3); break;
default: break;
}
break;
case frame_type_U_SABM: // Set Async Balanced Mode
case frame_type_U_DISC: // Disconnect
case frame_type_U_DM: // Disconnect Mode
case frame_type_U_UA: // Unnumbered Acknowledge
case frame_type_U_FRMR: // Frame Reject
case frame_type_U_UI: // Unnumbered Information
case frame_type_U_XID: // Exchange Identification
case frame_type_U_TEST: // Test
// The encoding allows only 3 bits for frame type and SABME got left out.
// Control format: P/F opcode[3] C n/a n/a
// The grayed out n/a bits are observed as 00 in the example.
// The header UI field must also be set for UI frames.
// PID is set to 1 for all U frames other than UI.
if (frame_type == frame_type_U_UI) {
SET_UI(hdr, 1); // I guess this is how we distinguish 'I' and 'UI'
// on the receiving end.
int pid = encode_pid(pp);
if (pid < 0) return (-1);
SET_PID(hdr, pid);
}
else {
SET_PID(hdr, 1); // 1 for 'U' other than 'UI'.
}
// Each of the destination and source addresses has a "C" bit.
// They should normally have the opposite setting.
// IL2P has only a single bit to represent 4 possbilities.
//
// dst src il2p meaning
// --- --- ---- -------
// 0 0 0 Not valid (earlier protocol version)
// 1 0 1 Command (v2)
// 0 1 0 Response (v2)
// 1 1 1 Not valid (earlier protocol version)
//
// APRS does not mention how to set these bits and all 4 combinations
// are seen in the wild. Apparently these are ignored on receive and no
// one cares. Here we copy from the C bit in the destination address.
// It should be noted that the case of both C bits being the same can't
// be represented so the il2p encode/decode bit not produce exactly the
// same bits. We see this in the second example in the protocol spec.
// The original UI frame has both C bits of 0 so it is received as a response.
SET_CONTROL(hdr, (pf << 6) | (((cr == cr_cmd) | (cr == cr_11)) << 2));
// This gets OR'ed into the above.
switch (frame_type) {
case frame_type_U_SABM: SET_CONTROL(hdr, 0 << 3); break;
case frame_type_U_DISC: SET_CONTROL(hdr, 1 << 3); break;
case frame_type_U_DM: SET_CONTROL(hdr, 2 << 3); break;
case frame_type_U_UA: SET_CONTROL(hdr, 3 << 3); break;
case frame_type_U_FRMR: SET_CONTROL(hdr, 4 << 3); break;
case frame_type_U_UI: SET_CONTROL(hdr, 5 << 3); break;
case frame_type_U_XID: SET_CONTROL(hdr, 6 << 3); break;
case frame_type_U_TEST: SET_CONTROL(hdr, 7 << 3); break;
default: break;
}
break;
case frame_type_I: // Information
// I frames (mod 8 only)
// encoded control: P/F N(R) N(S)
SET_UI(hdr, 0);
int pid2 = encode_pid(pp);
if (pid2 < 0) return (-1);
SET_PID(hdr, pid2);
SET_CONTROL(hdr, (pf << 6) | (nr << 3) | ns);
break;
case frame_type_U_SABME: // Set Async Balanced Mode, Extended
case frame_type_U: // other Unnumbered, not used by AX.25.
case frame_not_AX25: // Could not get control byte from frame.
default:
// Fall back to the header type 0 for these.
return (-1);
}
// Common for all header type 1.
// Bit 7 has [FEC Level:1], [HDR Type:1], [Payload byte Count:10]
SET_FEC_LEVEL(hdr, max_fec);
SET_HDR_TYPE(hdr, 1);
unsigned char *pinfo;
int info_len;
info_len = ax25_get_info(pp, &pinfo);
if (info_len < 0 || info_len > IL2P_MAX_PAYLOAD_SIZE) {
return (-2);
}
SET_PAYLOAD_BYTE_COUNT(hdr, info_len);
return (info_len);
}
// This should create a packet from the IL2P header.
// The information part will not be filled in.
static void trim(char *stuff)
{
char *p = stuff + strlen(stuff) - 1;
while (strlen(stuff) > 0 && (*p == ' ')) {
*p = '\0';
p--;
}
}
/*--------------------------------------------------------------------------------
*
* Function: il2p_decode_header_type_1
*
* Purpose: Attempt to convert type 1 header to a packet object.
*
* Inputs: hdr - IL2P header with no scrambling or parity symbols.
*
* num_sym_changed - Number of symbols changed by FEC in the header.
* Should be 0 or 1.
*
* Returns: Packet Object or NULL for failure.
*
* Description: A later step will process the payload for the information part.
*
*--------------------------------------------------------------------------------*/
packet_t il2p_decode_header_type_1(unsigned char *hdr, int num_sym_changed)
{
if (GET_HDR_TYPE(hdr) != 1) {
Debugprintf("IL2P Internal error. Should not be here: %s, when header type is 0.\n", __func__);
return (NULL);
}
// First get the addresses including SSID.
char addrs[AX25_MAX_ADDRS][AX25_MAX_ADDR_LEN];
int num_addr = 2;
memset(addrs, 0, 2 * AX25_MAX_ADDR_LEN);
// The IL2P header uses 2 parity symbols which means a single corrupted symbol (byte)
// can always be corrected.
// However, I have seen cases, where the error rate is very high, where the RS decoder
// thinks it found a valid code block by changing one symbol but it was the wrong one.
// The result is trash. This shows up as address fields like 'R&G4"A' and 'TEW\ !'.
// I added a sanity check here to catch characters other than upper case letters and digits.
// The frame should be rejected in this case. The question is whether to discard it
// silently or print a message so the user can see that something strange is happening?
// My current thinking is that it should be silently ignored if the header has been
// modified (correctee or more likely, made worse in this cases).
// If no changes were made, something weird is happening. We should mention it for
// troubleshooting rather than sweeping it under the rug.
// The same thing has been observed with the payload, under very high error conditions,
// and max_fec==0. Here I don't see a good solution. AX.25 information can contain
// "binary" data so I'm not sure what sort of sanity check could be added.
// This was not observed with max_fec==1. If we make that the default, same as Nino TNC,
// it would be extremely extremely unlikely unless someone explicitly selects weaker FEC.
// TODO: We could do something similar for header type 0.
// The address fields should be all binary zero values.
// Someone overly ambitious might check the addresses found in the first payload block.
for (int i = 0; i <= 5; i++) {
addrs[AX25_DESTINATION][i] = sixbit_to_ascii(hdr[i] & 0x3f);
}
trim(addrs[AX25_DESTINATION]);
for (int i = 0; i < strlen(addrs[AX25_DESTINATION]); i++) {
if (!isupper(addrs[AX25_DESTINATION][i]) && !isdigit(addrs[AX25_DESTINATION][i])) {
if (num_sym_changed == 0) {
// This can pop up sporadically when receiving random noise.
// Would be better to show only when debug is enabled but variable not available here.
// TODO: For now we will just suppress it.
//text_color_set(DW_COLOR_ERROR);
//Debugprintf ("IL2P: Invalid character '%c' in destination address '%s'\n", addrs[AX25_DESTINATION][i], addrs[AX25_DESTINATION]);
}
return (NULL);
}
}
sprintf(addrs[AX25_DESTINATION] + strlen(addrs[AX25_DESTINATION]), "-%d", (hdr[12] >> 4) & 0xf);
for (int i = 0; i <= 5; i++) {
addrs[AX25_SOURCE][i] = sixbit_to_ascii(hdr[i + 6] & 0x3f);
}
trim(addrs[AX25_SOURCE]);
for (int i = 0; i < strlen(addrs[AX25_SOURCE]); i++) {
if (!isupper(addrs[AX25_SOURCE][i]) && !isdigit(addrs[AX25_SOURCE][i])) {
if (num_sym_changed == 0) {
// This can pop up sporadically when receiving random noise.
// Would be better to show only when debug is enabled but variable not available here.
// TODO: For now we will just suppress it.
//text_color_set(DW_COLOR_ERROR);
//Debugprintf ("IL2P: Invalid character '%c' in source address '%s'\n", addrs[AX25_SOURCE][i], addrs[AX25_SOURCE]);
}
return (NULL);
}
}
sprintf(addrs[AX25_SOURCE] + strlen(addrs[AX25_SOURCE]), "-%d", hdr[12] & 0xf);
// The PID field gives us the general type.
// 0 = 'S' frame.
// 1 = 'U' frame other than UI.
// others are either 'UI' or 'I' depending on the UI field.
int pid = GET_PID(hdr);
int ui = GET_UI(hdr);
if (pid == 0) {
// 'S' frame.
// The control field contains: P/F N(R) C S S
int control = GET_CONTROL(hdr);
cmdres_t cr = (control & 0x04) ? cr_cmd : cr_res;
ax25_frame_type_t ftype;
switch (control & 0x03) {
case 0: ftype = frame_type_S_RR; break;
case 1: ftype = frame_type_S_RNR; break;
case 2: ftype = frame_type_S_REJ; break;
default: ftype = frame_type_S_SREJ; break;
}
int modulo = 8;
int nr = (control >> 3) & 0x07;
int pf = (control >> 6) & 0x01;
unsigned char *pinfo = NULL; // Any info for SREJ will be added later.
int info_len = 0;
return (ax25_s_frame(addrs, num_addr, cr, ftype, modulo, nr, pf, pinfo, info_len));
}
else if (pid == 1) {
// 'U' frame other than 'UI'.
// The control field contains: P/F OPCODE{3) C x x
int control = GET_CONTROL(hdr);
cmdres_t cr = (control & 0x04) ? cr_cmd : cr_res;
int axpid = 0; // unused for U other than UI.
ax25_frame_type_t ftype;
switch ((control >> 3) & 0x7) {
case 0: ftype = frame_type_U_SABM; break;
case 1: ftype = frame_type_U_DISC; break;
case 2: ftype = frame_type_U_DM; break;
case 3: ftype = frame_type_U_UA; break;
case 4: ftype = frame_type_U_FRMR; break;
case 5: ftype = frame_type_U_UI; axpid = 0xf0; break; // Should not happen with IL2P pid == 1.
case 6: ftype = frame_type_U_XID; break;
default: ftype = frame_type_U_TEST; break;
}
int pf = (control >> 6) & 0x01;
unsigned char *pinfo = NULL; // Any info for UI, XID, TEST will be added later.
int info_len = 0;
return (ax25_u_frame(addrs, num_addr, cr, ftype, pf, axpid, pinfo, info_len));
}
else if (ui) {
// 'UI' frame.
// The control field contains: P/F OPCODE{3) C x x
int control = GET_CONTROL(hdr);
cmdres_t cr = (control & 0x04) ? cr_cmd : cr_res;
ax25_frame_type_t ftype = frame_type_U_UI;
int pf = (control >> 6) & 0x01;
int axpid = decode_pid(GET_PID(hdr));
unsigned char *pinfo = NULL; // Any info for UI, XID, TEST will be added later.
int info_len = 0;
return (ax25_u_frame(addrs, num_addr, cr, ftype, pf, axpid, pinfo, info_len));
}
else {
// 'I' frame.
// The control field contains: P/F N(R) N(S)
int control = GET_CONTROL(hdr);
cmdres_t cr = cr_cmd; // Always command.
int pf = (control >> 6) & 0x01;
int nr = (control >> 3) & 0x7;
int ns = control & 0x7;
int modulo = 8;
int axpid = decode_pid(GET_PID(hdr));
unsigned char *pinfo = NULL; // Any info for UI, XID, TEST will be added later.
int info_len = 0;
return (ax25_i_frame(addrs, num_addr, cr, modulo, nr, ns, pf, axpid, pinfo, info_len));
}
return (NULL); // unreachable but avoid warning.
} // end
/*--------------------------------------------------------------------------------
*
* Function: il2p_type_0_header
*
* Purpose: Attempt to create type 0 header from packet object.
*
* Inputs: pp - Packet object.
*
* max_fec - 1 to use maximum FEC symbols, 0 for automatic.
*
* Outputs: hdr - IL2P header with no scrambling or parity symbols.
* Must be large enough to hold IL2P_HEADER_SIZE unsigned bytes.
*
* Returns: Number of bytes for information part or -1 for failure.
* In case of failure, fall back to type 0 transparent encapsulation.
*
* Description: The type 0 header is used when it is not one of the restricted cases
* covered by the type 1 header.
* The AX.25 frame is put in the payload.
* This will cover: more than one address, mod 128 sequences, etc.
*
*--------------------------------------------------------------------------------*/
int il2p_type_0_header(packet_t pp, int max_fec, unsigned char *hdr)
{
memset(hdr, 0, IL2P_HEADER_SIZE);
// Bit 7 has [FEC Level:1], [HDR Type:1], [Payload byte Count:10]
SET_FEC_LEVEL(hdr, max_fec);
SET_HDR_TYPE(hdr, 0);
int frame_len = ax25_get_frame_len(pp);
if (frame_len < 14 || frame_len > IL2P_MAX_PAYLOAD_SIZE) {
return (-2);
}
SET_PAYLOAD_BYTE_COUNT(hdr, frame_len);
return (frame_len);
}
/***********************************************************************************
*
* Name: il2p_get_header_attributes
*
* Purpose: Extract a few attributes from an IL2p header.
*
* Inputs: hdr - IL2P header structure.
*
* Outputs: hdr_type - 0 or 1.
*
* max_fec - 0 for automatic or 1 for fixed maximum size.
*
* Returns: Payload byte count. (actual payload size, not the larger encoded format)
*
***********************************************************************************/
int il2p_get_header_attributes(unsigned char *hdr, int *hdr_type, int *max_fec)
{
*hdr_type = GET_HDR_TYPE(hdr);
*max_fec = GET_FEC_LEVEL(hdr);
return(GET_PAYLOAD_BYTE_COUNT(hdr));
}
/***********************************************************************************
*
* Name: il2p_clarify_header
*
* Purpose: Convert received header to usable form.
* This involves RS FEC then descrambling.
*
* Inputs: rec_hdr - Header as received over the radio.
*
* Outputs: corrected_descrambled_hdr - After RS FEC and unscrambling.
*
* Returns: Number of symbols that were corrected:
* 0 = No errors
* 1 = Single symbol corrected.
* <0 = Unable to obtain good header.
*
***********************************************************************************/
int il2p_clarify_header(unsigned char *rec_hdr, unsigned char *corrected_descrambled_hdr)
{
unsigned char corrected[IL2P_HEADER_SIZE + IL2P_HEADER_PARITY];
int e = il2p_decode_rs(rec_hdr, IL2P_HEADER_SIZE, IL2P_HEADER_PARITY, corrected);
if (e > 1) // only have 2 rs bytes so can only detect 1 error
{
Debugprintf("Header correction seems ok but errors > 1");
return -1;
}
il2p_descramble_block(corrected, corrected_descrambled_hdr, IL2P_HEADER_SIZE);
return (e);
}
// end il2p_header.c
/*--------------------------------------------------------------------------------
*
* File: il2p_payload.c
*
* Purpose: Functions dealing with the payload.
*
*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------
*
* Function: il2p_payload_compute
*
* Purpose: Compute number and sizes of data blocks based on total size.
*
* Inputs: payload_size 0 to 1023. (IL2P_MAX_PAYLOAD_SIZE)
* max_fec true for 16 parity symbols, false for automatic.
*
* Outputs: *p Payload block sizes and counts.
* Number of parity symbols per block.
*
* Returns: Number of bytes in the encoded format.
* Could be 0 for no payload blocks.
* -1 for error (i.e. invalid unencoded size: <0 or >1023)
*
*--------------------------------------------------------------------------------*/
int il2p_payload_compute(il2p_payload_properties_t *p, int payload_size, int max_fec)
{
memset(p, 0, sizeof(il2p_payload_properties_t));
if (payload_size < 0 || payload_size > IL2P_MAX_PAYLOAD_SIZE) {
return (-1);
}
if (payload_size == 0) {
return (0);
}
if (max_fec) {
p->payload_byte_count = payload_size;
p->payload_block_count = (p->payload_byte_count + 238) / 239;
p->small_block_size = p->payload_byte_count / p->payload_block_count;
p->large_block_size = p->small_block_size + 1;
p->large_block_count = p->payload_byte_count - (p->payload_block_count * p->small_block_size);
p->small_block_count = p->payload_block_count - p->large_block_count;
p->parity_symbols_per_block = 16;
}
else {
p->payload_byte_count = payload_size;
p->payload_block_count = (p->payload_byte_count + 246) / 247;
p->small_block_size = p->payload_byte_count / p->payload_block_count;
p->large_block_size = p->small_block_size + 1;
p->large_block_count = p->payload_byte_count - (p->payload_block_count * p->small_block_size);
p->small_block_count = p->payload_block_count - p->large_block_count;
//p->parity_symbols_per_block = (p->small_block_size / 32) + 2; // Looks like error in documentation
// It would work if the number of parity symbols was based on large block size.
if (p->small_block_size <= 61) p->parity_symbols_per_block = 2;
else if (p->small_block_size <= 123) p->parity_symbols_per_block = 4;
else if (p->small_block_size <= 185) p->parity_symbols_per_block = 6;
else if (p->small_block_size <= 247) p->parity_symbols_per_block = 8;
else {
// Should not happen. But just in case...
Debugprintf("IL2P parity symbol per payload block error. small_block_size = %d\n", p->small_block_size);
return (-1);
}
}
// Return the total size for the encoded format.
return (p->small_block_count * (p->small_block_size + p->parity_symbols_per_block) +
p->large_block_count * (p->large_block_size + p->parity_symbols_per_block));
} // end il2p_payload_compute
/*--------------------------------------------------------------------------------
*
* Function: il2p_encode_payload
*
* Purpose: Split payload into multiple blocks such that each set
* of data and parity symbols fit into a 255 byte RS block.
*
* Inputs: *payload Array of bytes.
* payload_size 0 to 1023. (IL2P_MAX_PAYLOAD_SIZE)
* max_fec true for 16 parity symbols, false for automatic.
*
* Outputs: *enc Encoded payload for transmission.
* Up to IL2P_MAX_ENCODED_SIZE bytes.
*
* Returns: -1 for error (i.e. invalid size)
* 0 for no blocks. (i.e. size zero)
* Number of bytes generated. Maximum IL2P_MAX_ENCODED_SIZE.
*
* Note: I interpreted the protocol spec as saying the LFSR state is retained
* between data blocks. During interoperability testing, I found that
* was not the case. It is reset for each data block.
*
*--------------------------------------------------------------------------------*/
int il2p_encode_payload(unsigned char *payload, int payload_size, int max_fec, unsigned char *enc)
{
if (payload_size > IL2P_MAX_PAYLOAD_SIZE) return (-1);
if (payload_size == 0) return (0);
// Determine number of blocks and sizes.
il2p_payload_properties_t ipp;
int e;
e = il2p_payload_compute(&ipp, payload_size, max_fec);
if (e <= 0) {
return (e);
}
unsigned char *pin = payload;
unsigned char *pout = enc;
int encoded_length = 0;
unsigned char scram[256];
unsigned char parity[IL2P_MAX_PARITY_SYMBOLS];
// First the large blocks.
for (int b = 0; b < ipp.large_block_count; b++) {
il2p_scramble_block(pin, scram, ipp.large_block_size);
memcpy(pout, scram, ipp.large_block_size);
pin += ipp.large_block_size;
pout += ipp.large_block_size;
encoded_length += ipp.large_block_size;
il2p_encode_rs(scram, ipp.large_block_size, ipp.parity_symbols_per_block, parity);
memcpy(pout, parity, ipp.parity_symbols_per_block);
pout += ipp.parity_symbols_per_block;
encoded_length += ipp.parity_symbols_per_block;
}
// Then the small blocks.
for (int b = 0; b < ipp.small_block_count; b++) {
il2p_scramble_block(pin, scram, ipp.small_block_size);
memcpy(pout, scram, ipp.small_block_size);
pin += ipp.small_block_size;
pout += ipp.small_block_size;
encoded_length += ipp.small_block_size;
il2p_encode_rs(scram, ipp.small_block_size, ipp.parity_symbols_per_block, parity);
memcpy(pout, parity, ipp.parity_symbols_per_block);
pout += ipp.parity_symbols_per_block;
encoded_length += ipp.parity_symbols_per_block;
}
return (encoded_length);
} // end il2p_encode_payload
/*--------------------------------------------------------------------------------
*
* Function: il2p_decode_payload
*
* Purpose: Extract original data from encoded payload.
*
* Inputs: received Array of bytes. Size is unknown but in practice it
* must not exceed IL2P_MAX_ENCODED_SIZE.
* payload_size 0 to 1023. (IL2P_MAX_PAYLOAD_SIZE)
* Expected result size based on header.
* max_fec true for 16 parity symbols, false for automatic.
*
* Outputs: payload_out Recovered payload.
*
* In/Out: symbols_corrected Number of symbols corrected.
*
*
* Returns: Number of bytes extracted. Should be same as payload_size going in.
* -3 for unexpected internal inconsistency.
* -2 for unable to recover from signal corruption.
* -1 for invalid size.
* 0 for no blocks. (i.e. size zero)
*
* Description: Each block is scrambled separately but the LSFR state is carried
* from the first payload block to the next.
*
*--------------------------------------------------------------------------------*/
int il2p_decode_payload(unsigned char *received, int payload_size, int max_fec, unsigned char *payload_out, int *symbols_corrected)
{
// Determine number of blocks and sizes.
il2p_payload_properties_t ipp;
int e;
e = il2p_payload_compute(&ipp, payload_size, max_fec);
if (e <= 0) {
return (e);
}
unsigned char *pin = received;
unsigned char *pout = payload_out;
int decoded_length = 0;
int failed = 0;
// First the large blocks.
for (int b = 0; b < ipp.large_block_count; b++) {
unsigned char corrected_block[255];
int e = il2p_decode_rs(pin, ipp.large_block_size, ipp.parity_symbols_per_block, corrected_block);
// Debugprintf ("%s:%d: large block decode_rs returned status = %d\n", __FILE__, __LINE__, e);
if (e < 0) failed = 1;
*symbols_corrected += e;
il2p_descramble_block(corrected_block, pout, ipp.large_block_size);
if (il2p_get_debug() >= 2) {
Debugprintf("Descrambled large payload block, %d bytes:\n", ipp.large_block_size);
fx_hex_dump(pout, ipp.large_block_size);
}
pin += ipp.large_block_size + ipp.parity_symbols_per_block;
pout += ipp.large_block_size;
decoded_length += ipp.large_block_size;
}
// Then the small blocks.
for (int b = 0; b < ipp.small_block_count; b++) {
unsigned char corrected_block[255];
int e = il2p_decode_rs(pin, ipp.small_block_size, ipp.parity_symbols_per_block, corrected_block);
// Debugprintf ("%s:%d: small block decode_rs returned status = %d\n", __FILE__, __LINE__, e);
if (e < 0) failed = 1;
*symbols_corrected += e;
il2p_descramble_block(corrected_block, pout, ipp.small_block_size);
if (il2p_get_debug() >= 2) {
Debugprintf("Descrambled small payload block, %d bytes:\n", ipp.small_block_size);
fx_hex_dump(pout, ipp.small_block_size);
}
pin += ipp.small_block_size + ipp.parity_symbols_per_block;
pout += ipp.small_block_size;
decoded_length += ipp.small_block_size;
}
if (failed) {
//Debugprintf ("%s:%d: failed = %0x\n", __FILE__, __LINE__, failed);
return (-2);
}
if (decoded_length != payload_size) {
Debugprintf("IL2P Internal error: decoded_length = %d, payload_size = %d\n", decoded_length, payload_size);
return (-3);
}
return (decoded_length);
} // end il2p_decode_payload
// end il2p_payload.c
struct il2p_context_s *il2p_context[4][16][3];
/***********************************************************************************
*
* Name: il2p_rec_bit
*
* Purpose: Extract il2p packets from a stream of bits.
*
* Inputs: chan - Channel number.
*
* subchan - This allows multiple demodulators per channel.
*
* slice - Allows multiple slicers per demodulator (subchannel).
*
* dbit - One bit from the received data stream.
*
* Description: This is called once for each received bit.
* For each valid packet, process_rec_frame() is called for further processing.
* It can gather multiple candidates from different parallel demodulators
* ("subchannels") and slicers, then decide which one is the best.
*
***********************************************************************************/
int centreFreq[4] = { 0, 0, 0, 0 };
void il2p_rec_bit(int chan, int subchan, int slice, int dbit)
{
// Allocate context blocks only as needed.
if (dbit)
dbit = 1;
else
dbit = 0;
struct il2p_context_s *F = il2p_context[chan][subchan][slice];
if (F == NULL) {
//assert(chan >= 0 && chan < MAX_CHANS);
//assert(subchan >= 0 && subchan < MAX_SUBCHANS);
//assert(slice >= 0 && slice < MAX_SLICERS);
F = il2p_context[chan][subchan][slice] = (struct il2p_context_s *)malloc(sizeof(struct il2p_context_s));
//assert(F != NULL);
memset(F, 0, sizeof(struct il2p_context_s));
}
// Accumulate most recent 24 bits received. Most recent is LSB.
F->acc = ((F->acc << 1) | (dbit & 1)) & 0x00ffffff;
// State machine to look for sync word then gather appropriate number of header and payload bytes.
switch (F->state) {
case IL2P_SEARCHING: // Searching for the sync word.
if (__builtin_popcount(F->acc ^ IL2P_SYNC_WORD) <= 1) { // allow single bit mismatch
//text_color_set (DW_COLOR_INFO);
//Debugprintf ("IL2P header has normal polarity\n");
F->polarity = 0;
F->state = IL2P_HEADER;
F->bc = 0;
F->hc = 0;
nPhases[chan][subchan][slice] = 0;
// Determine Centre Freq
centreFreq[chan] = GuessCentreFreq(chan);
debugTimeStamp("SYNC Detected", 'R');
}
else if (__builtin_popcount((~(F->acc) & 0x00ffffff) ^ IL2P_SYNC_WORD) <= 1) {
// FIXME - this pops up occasionally with random noise. Find better way to convey information.
// This also happens for each slicer - to noisy.
//Debugprintf ("IL2P header has reverse polarity\n");
F->polarity = 1;
F->state = IL2P_HEADER;
F->bc = 0;
F->hc = 0;
centreFreq[chan] = GuessCentreFreq(chan);
nPhases[chan][subchan][slice] = 0;
}
break;
case IL2P_HEADER: // Gathering the header.
F->bc++;
if (F->bc == 8) { // full byte has been collected.
F->bc = 0;
if (!F->polarity) {
F->shdr[F->hc++] = F->acc & 0xff;
}
else {
F->shdr[F->hc++] = (~F->acc) & 0xff;
}
if (F->hc == IL2P_HEADER_SIZE + IL2P_HEADER_PARITY) { // Have all of header
//if (il2p_get_debug() >= 1)
//{
// Debugprintf("IL2P header as received [%d.%d.%d]:\n", chan, subchan, slice);
// fx_hex_dump(F->shdr, IL2P_HEADER_SIZE + IL2P_HEADER_PARITY);
//}
// Fix any errors and descramble.
F->corrected = il2p_clarify_header(F->shdr, F->uhdr);
if (F->corrected >= 0) { // Good header.
// How much payload is expected?
il2p_payload_properties_t plprop;
int hdr_type, max_fec;
int len = il2p_get_header_attributes(F->uhdr, &hdr_type, &max_fec);
F->eplen = il2p_payload_compute(&plprop, len, max_fec);
if (il2p_get_debug() >= 2)
{
Debugprintf("Header type %d, max fec = %d", hdr_type, max_fec);
Debugprintf("Need to collect %d encoded bytes for %d byte payload.", F->eplen, len);
Debugprintf("%d small blocks of %d and %d large blocks of %d. %d parity symbols per block",
plprop.small_block_count, plprop.small_block_size,
plprop.large_block_count, plprop.large_block_size, plprop.parity_symbols_per_block);
}
if (len > 340)
{
Debugprintf("Packet too big for QtSM");
F->state = IL2P_SEARCHING;
return;
}
if (F->eplen >= 1) { // Need to gather payload.
F->pc = 0;
F->state = IL2P_PAYLOAD;
}
else if (F->eplen == 0)
{
// No payload.
F->pc = 0;
if (il2p_crc[chan])
{
// enter collect crc state
F->crccount = 0;
F->state = IL2P_CRC;
}
else
F->state = IL2P_DECODE;
}
else { // Error.
if (il2p_get_debug() >= 1) {
Debugprintf("IL2P header INVALID.\n");
}
F->state = IL2P_SEARCHING;
}
} // good header after FEC.
else {
F->state = IL2P_SEARCHING; // Header failed FEC check.
}
} // entire header has been collected.
} // full byte collected.
break;
case IL2P_PAYLOAD: // Gathering the payload, if any.
F->bc++;
if (F->bc == 8) { // full byte has been collected.
F->bc = 0;
if (!F->polarity) {
F->spayload[F->pc++] = F->acc & 0xff;
}
else {
F->spayload[F->pc++] = (~F->acc) & 0xff;
}
if (F->pc == F->eplen)
{
// got frame. See if need crc
if (il2p_crc[chan])
{
// enter collect crc state
F->crccount = 0;
F->state = IL2P_CRC;
}
else
F->state = IL2P_DECODE;
}
}
break;
case IL2P_CRC:
F->bc++;
if (F->bc == 8)
{
// full byte has been collected.
F->bc = 0;
if (!F->polarity)
F->crc[F->crccount++] = F->acc & 0xff;
else
F->crc[F->crccount++] = (~F->acc) & 0xff;
if (F->crccount == 4)
{
// have all crc bytes. enter DECODE
debugTimeStamp("CRC Complete Header is", 'R');
debugHexDump(F->shdr, 15, 'R');
if (F->pc)
{
debugTimeStamp("Payload is", 'R');
debugHexDump(F->spayload, F->pc, 'R');
}
debugTimeStamp("CRC is", 'R');
debugHexDump(F->crc, 4, 'R');
F->state = IL2P_DECODE;
}
}
break;
case IL2P_DECODE:
// We get here after a good header and any payload has been collected.
// Processing is delayed by one bit but I think it makes the logic cleaner.
// During unit testing be sure to send an extra bit to flush it out at the end.
// in uhdr[IL2P_HEADER_SIZE]; // Header after FEC and descrambling.
// TODO?: for symmetry, we might decode the payload here and later build the frame.
{
packet_t pp = il2p_decode_header_payload(F->uhdr, F->spayload, &(F->corrected));
if (il2p_get_debug() >= 1)
{
if (pp == NULL)
{
// Most likely too many FEC errors.
Debugprintf("FAILED to construct frame in %s.\n", __func__);
debugTimeStamp("Packet Decode failed", 'R');
}
}
if (pp != NULL)
{
alevel_t alevel = demod_get_audio_level(chan, subchan);
retry_t retries = F->corrected;
int is_fx25 = 1; // FIXME: distinguish fx.25 and IL2P.
// Currently this just means that a FEC mode was used.
// TODO: Could we put last 3 arguments in packet object rather than passing around separately?
// if using crc pass received crc to packet object
debugTimeStamp("Decoded Packet is", 'R');
debugHexDump(pp->frame_data, pp->frame_len, 'R');
if (il2p_crc[chan])
{
//copy crc bytes to packet object
pp->crc[0] = F->crc[0];
pp->crc[1] = F->crc[1];
pp->crc[2] = F->crc[2];
pp->crc[3] = F->crc[3];
}
debugTimeStamp("CRC raw bytes", 'R');
debugHexDump(pp->crc, 4, 'R');
multi_modem_process_rec_packet(chan, subchan, slice, pp, alevel, retries, is_fx25, slice, centreFreq[chan]);
}
} // end block for local variables.
if (il2p_get_debug() >= 2)
Debugprintf("-----");
F->state = IL2P_SEARCHING;
break;
} // end of switch
} // end il2p_rec_bit
// Scramble bits for il2p transmit.
// Note that there is a delay of 5 until the first bit comes out.
// So we need to need to ignore the first 5 out and stick in
// an extra 5 filler bits to flush at the end.
#define INIT_TX_LSFR 0x00f
static inline int scramble_bit(int in, int *state)
{
int out = ((*state >> 4) ^ *state) & 1;
*state = ((((in ^ *state) & 1) << 9) | (*state ^ ((*state & 1) << 4))) >> 1;
return (out);
}
// Undo data scrambling for il2p receive.
#define INIT_RX_LSFR 0x1f0
static inline int descramble_bit(int in, int *state)
{
int out = (in ^ *state) & 1;
*state = ((*state >> 1) | ((in & 1) << 8)) ^ ((in & 1) << 3);
return (out);
}
/*--------------------------------------------------------------------------------
*
* Function: il2p_scramble_block
*
* Purpose: Scramble a block before adding RS parity.
*
* Inputs: in Array of bytes.
* len Number of bytes both in and out.
*
* Outputs: out Array of bytes.
*
*--------------------------------------------------------------------------------*/
void il2p_scramble_block(unsigned char *in, unsigned char *out, int len)
{
int tx_lfsr_state = INIT_TX_LSFR;
memset(out, 0, len);
int skipping = 1; // Discard the first 5 out.
int ob = 0; // Index to output byte.
int om = 0x80; // Output bit mask;
for (int ib = 0; ib < len; ib++) {
for (int im = 0x80; im != 0; im >>= 1) {
int s = scramble_bit((in[ib] & im) != 0, &tx_lfsr_state);
if (ib == 0 && im == 0x04) skipping = 0;
if (!skipping) {
if (s) {
out[ob] |= om;
}
om >>= 1;
if (om == 0) {
om = 0x80;
ob++;
}
}
}
}
// Flush it.
// This is a relic from when I thought the state would need to
// be passed along for the next block.
// Preserve the LSFR state from before flushing.
// This might be needed as the initial state for later payload blocks.
int x = tx_lfsr_state;
for (int n = 0; n < 5; n++) {
int s = scramble_bit(0, &x);
if (s) {
out[ob] |= om;
}
om >>= 1;
if (om == 0) {
om = 0x80;
ob++;
}
}
} // end il2p_scramble_block
/*--------------------------------------------------------------------------------
*
* Function: il2p_descramble_block
*
* Purpose: Descramble a block after removing RS parity.
*
* Inputs: in Array of bytes.
* len Number of bytes both in and out.
*
* Outputs: out Array of bytes.
*
*--------------------------------------------------------------------------------*/
void il2p_descramble_block(unsigned char *in, unsigned char *out, int len)
{
int rx_lfsr_state = INIT_RX_LSFR;
memset(out, 0, len);
for (int b = 0; b < len; b++) {
for (int m = 0x80; m != 0; m >>= 1) {
int d = descramble_bit((in[b] & m) != 0, &rx_lfsr_state);
if (d) {
out[b] |= m;
}
}
}
}
// end il2p_scramble.c
static int number_of_bits_sent[MAX_CHANS]; // Count number of bits sent by "il2p_send_frame"
static void send_bytes(int chan, unsigned char *b, int count, int polarity);
static void send_bit(int chan, int b, int polarity);
/*-------------------------------------------------------------
*
* Name: il2p_send_frame
*
* Purpose: Convert frames to a stream of bits in IL2P format.
*
* Inputs: chan - Audio channel number, 0 = first.
*
* pp - Pointer to packet object.
*
* max_fec - 1 to force 16 parity symbols for each payload block.
* 0 for automatic depending on block size.
*
* polarity - 0 for normal. 1 to invert signal.
* 2 special case for testing - introduce some errors to test FEC.
*
* Outputs: Bits are shipped out by calling tone_gen_put_bit().
*
* Returns: Number of bits sent including
* - Preamble (01010101...)
* - 3 byte Sync Word.
* - 15 bytes for Header.
* - Optional payload.
* The required time can be calculated by dividing this
* number by the transmit rate of bits/sec.
* -1 is returned for failure.
*
* Description: Generate an IL2P encoded frame.
*
* Assumptions: It is assumed that the tone_gen module has been
* properly initialized so that bits sent with
* tone_gen_put_bit() are processed correctly.
*
* Errors: Return -1 for error. Probably frame too large.
*
* Note: Inconsistency here. ax25 version has just a byte array
* and length going in. Here we need the full packet object.
*
*--------------------------------------------------------------*/
string * il2p_send_frame(int chan, packet_t pp, int max_fec, int polarity)
{
unsigned char encoded[IL2P_MAX_PACKET_SIZE] = "";
string * packet = newString();
int preamblecount;
unsigned char preamble[1024];
// The data includes the 2 byte crc but length doesn't
uint8_t crc1 = pp->frame_data[pp->frame_len]; // Low 8 bits
uint8_t crc2 = pp->frame_data[pp->frame_len + 1]; // High 8 bits
encoded[0] = (IL2P_SYNC_WORD >> 16) & 0xff;
encoded[1] = (IL2P_SYNC_WORD >> 8) & 0xff;
encoded[2] = (IL2P_SYNC_WORD) & 0xff;
int elen = il2p_encode_frame(pp, max_fec, encoded + IL2P_SYNC_WORD_SIZE);
if (elen <= 0) {
Debugprintf("IL2P: Unable to encode frame into IL2P.\n");
return (packet);
}
elen += IL2P_SYNC_WORD_SIZE;
// if we are using crc add it now. elen should point to end of data
// crc should be at pp->frame_data[pp->frame_len]
if (il2p_crc[chan] & 1)
{
// The four encoded CRC bytes are arranged :
// | CRC3 | CRC2 | CRC1 | CRC0 |
// CRC3 encoded from high nibble of 16 - bit CRC value (from crc2)
// CRC0 encoded from low nibble of 16 - bit CRC value (from crc1)
encoded[elen++] = Hamming74EncodeTable[crc2 >> 4];
encoded[elen++] = Hamming74EncodeTable[crc2 & 0xf];
encoded[elen++] = Hamming74EncodeTable[crc1 >> 4];
encoded[elen++] = Hamming74EncodeTable[crc1 &0xf];
}
number_of_bits_sent[chan] = 0;
if (il2p_get_debug() >= 2) {
Debugprintf("IL2P frame, max_fec = %d, %d encoded bytes total", max_fec, elen);
// fx_hex_dump(encoded, elen);
}
// Send bits to modulator.
// Try using preaamble for txdelay
// Nino now uses 00 as preamble for QPSK
// We don't need txdelay between frames in one transmission
if (Continuation[chan] == 0)
{
preamblecount = (txdelay[chan] * tx_bitrate[chan]) / 8000; // 8 for bits, 1000 for mS
if (preamblecount > 1024)
preamblecount = 1024;
if (pskStates[chan]) // PSK Modes
memset(preamble, 01, preamblecount);
else
memset(preamble, IL2P_PREAMBLE, preamblecount);
stringAdd(packet, preamble, preamblecount);
Continuation[chan] = 1;
}
stringAdd(packet, encoded, elen);
// Add bytes for tail and TX padding, but don't send if another packet is available (?? how ??)
number_of_bits_sent[chan] = 0;
tx_fx25_size[chan] = packet->Length * 8;
return packet;
}
// TX Code. Builds whole packet then sends a bit at a time
#define TX_SILENCE 0
#define TX_DELAY 1
#define TX_TAIL 2
#define TX_NO_DATA 3
#define TX_FRAME 4
#define TX_WAIT_BPF 5
#define TX_BIT0 0
#define TX_BIT1 1
#define FRAME_EMPTY 0
#define FRAME_FULL 1
#define FRAME_NO_FRAME 2
#define FRAME_NEW_FRAME 3
#define BYTE_EMPTY 0
#define BYTE_FULL 1
extern UCHAR tx_frame_status[5];
extern UCHAR tx_byte_status[5];
extern string * tx_data[5];
extern int tx_data_len[5];
extern UCHAR tx_bit_stream[5];
extern UCHAR tx_bit_cnt[5];
extern long tx_tail_cnt[5];
extern BOOL tx_bs_bit[5];
string * fill_il2p_data(int snd_ch, string * data)
{
string * result;
packet_t pp = ax25_new();
// Call il2p_send_frame to build the bit stream
pp->frame_len = data->Length - 2; // Included CRC
memcpy(pp->frame_data, data->Data, data->Length); // Copy the crc in case we are going to send it
result = il2p_send_frame(snd_ch, pp, 1, 0);
ax25_delete(pp);
debugTimeStamp("TX Complete packet including Preamble and CRC and Tail", 'T');
debugHexDump(result->Data, result->Length, 'T');
return result;
}
void il2p_get_new_frame(int snd_ch, TStringList * frame_stream)
{
string * myTemp;
tx_bs_bit[snd_ch] = 0;
tx_bit_cnt[snd_ch] = 0;
tx_fx25_size_cnt[snd_ch] = 0;
tx_fx25_size[snd_ch] = 1;
tx_frame_status[snd_ch] = FRAME_NEW_FRAME;
tx_byte_status[snd_ch] = BYTE_EMPTY;
if (frame_stream->Count == 0)
tx_frame_status[snd_ch] = FRAME_NO_FRAME;
else
{
// We now pass control byte and ack bytes on front and pointer to socket on end if ackmode
myTemp = Strings(frame_stream, 0); // get message
if ((myTemp->Data[0] & 0x0f) == 12) // ACKMODE
{
// Save copy then copy data up 3 bytes
Add(&KISS_acked[snd_ch], duplicateString(myTemp));
mydelete(myTemp, 0, 3);
myTemp->Length -= sizeof(void *);
}
else
{
// Just remove control
mydelete(myTemp, 0, 1);
}
AGW_AX25_frame_analiz(snd_ch, FALSE, myTemp);
put_frame(snd_ch, myTemp, "", TRUE, FALSE);
tx_data[snd_ch] = fill_il2p_data(snd_ch, myTemp);
Delete(frame_stream, 0); // This will invalidate temp
}
}
// Original code
/*
static void send_bytes(int chan, unsigned char *b, int count, int polarity)
{
for (int j = 0; j < count; j++) {
unsigned int x = b[j];
for (int k = 0; k < 8; k++) {
send_bit(chan, (x & 0x80) != 0, polarity);
x <<= 1;
}
}
}
// NRZI would be applied for AX.25 but IL2P does not use it.
// However we do have an option to invert the signal.
// The direwolf receive implementation will automatically compensate
// for either polarity but other implementations might not.
static void send_bit(int chan, int b, int polarity)
{
tone_gen_put_bit(chan, (b ^ polarity) & 1);
number_of_bits_sent[chan]++;
}
*/
int il2p_get_new_bit(int snd_ch, Byte bit)
{
string *s;
if (tx_frame_status[snd_ch] == FRAME_EMPTY)
{
il2p_get_new_frame(snd_ch, &all_frame_buf[snd_ch]);
if (tx_frame_status[snd_ch] == FRAME_NEW_FRAME)
tx_frame_status[snd_ch] = FRAME_FULL;
}
if (tx_frame_status[snd_ch] == FRAME_FULL)
{
if (tx_byte_status[snd_ch] == BYTE_EMPTY)
{
if (tx_data[snd_ch]->Length)
{
s = tx_data[snd_ch];
tx_bit_stream[snd_ch] = s->Data[0];
tx_frame_status[snd_ch] = FRAME_FULL;
tx_byte_status[snd_ch] = BYTE_FULL;
tx_bit_cnt[snd_ch] = 0;
mydelete(tx_data[snd_ch], 0, 1);
}
else
tx_frame_status[snd_ch] = FRAME_EMPTY;
}
if (tx_byte_status[snd_ch] == BYTE_FULL)
{
// il2p sends high order bit first
bit = tx_bit_stream[snd_ch] >> 7; // top bit to bottom
tx_bit_stream[snd_ch] = tx_bit_stream[snd_ch] << 1;
tx_bit_cnt[snd_ch]++;
tx_fx25_size_cnt[snd_ch]++;
if (tx_bit_cnt[snd_ch] >= 8)
tx_byte_status[snd_ch] = BYTE_EMPTY;
if (tx_fx25_size_cnt[snd_ch] == tx_fx25_size[snd_ch])
tx_frame_status[snd_ch] = FRAME_EMPTY;
}
}
if (tx_frame_status[snd_ch] == FRAME_EMPTY)
{
il2p_get_new_frame(snd_ch, &all_frame_buf[snd_ch]);
switch (tx_frame_status[snd_ch])
{
case FRAME_NEW_FRAME:
tx_frame_status[snd_ch] = FRAME_FULL;
break;
case FRAME_NO_FRAME:
// I dont really like this state machine. We have run out of frames to send so
// should go straight to tail. This way we add an extra bit. Or does this really matter ??
tx_tail_cnt[snd_ch] = 0;
tx_frame_status[snd_ch] = FRAME_EMPTY;
tx_status[snd_ch] = TX_TAIL;
break;
}
}
return bit;
}
extern int txLatency;
extern int useTImedPTT;
int il2p_get_new_bit_tail(UCHAR snd_ch, UCHAR bit)
{
// This sends reversals. It is an experiment
int tailbits = (txtail[snd_ch] * tx_baudrate[snd_ch]) / 1000;
#ifndef WIN32
if (useTimedPTT)
tailbits += (txLatency * tx_baudrate[snd_ch]) / 1000; // add padding to tx buffer to make sure we don't send silence
#endif
if (tx_tail_cnt[snd_ch]++ > tailbits)
tx_status[snd_ch] = TX_WAIT_BPF;
return (tx_tail_cnt[snd_ch] & 1); // altenating 1/0
}
void debugHexDump(unsigned char * Data, int Len, char Dirn)
{
char Line[256];
#ifndef LOGTX
if (Dirn == 'T')
return;
#endif
#ifndef LOGRX
if (Dirn == 'R')
return;
#endif
while (Len > 0)
{
sprintf(Line, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Data[0], Data[1], Data[2], Data[3], Data[4], Data[5], Data[6], Data[7],
Data[8], Data[9], Data[10], Data[11], Data[12], Data[13], Data[14], Data[15]);
if (Len < 16)
{
Line[Len * 3] = 10;
Line[Len * 3 + 1] = 0;
}
writeTraceLog(Line);
Data += 16;
Len -= 16;
}
}
// Hamming experiments
// from https://github.com/nasserkessas/hamming-codes/blob/master/hamming.c
#define block unsigned short // 16 bits
#define bit uint8_t // 8 bits (only last is used)
int multipleXor(int *indicies, int len)
{
int val = indicies[0];
for (int i = 1; i < len; i++)
{
val = val ^ indicies[i];
}
return val;
}
bit getBit(unsigned short b, int i)
{
return (b << i) & (int)pow(2, (sizeof(unsigned short) * 8 - 1));
}
unsigned short toggleBit(unsigned short b, int i)
{
return b ^ (1 << i);
}
bit getCharBit(char b, int i)
{
return (b << i) & (int)pow(2, (sizeof(char) * 8 - 1));
}
block modifyBit(block n, int p, bit b)
{
return ((n & ~(1 << (sizeof(block) * 8 - 1 - p))) | (b << (sizeof(block) * 8 - 1 - p)));
}
void encode(char *input, int len, FILE *ptr) {
// Amount of bits in a block //
int bits = sizeof(block) * 8;
// Amount of bits per block used to carry the message //
int messageBits = bits - log2(bits) - 1;
// Amount of blocks needed to encode message //
int blocks = ceil((float)len / messageBits);
// Array of encoded blocks //
block encoded[16];
// Loop through each block //
for (int i = 0; i < blocks + 1; i++) {
printf("On Block %d:\n", i);
// Final encoded block variable //
block thisBlock = 0;
// Amount of "skipped" bits (used for parity) //
int skipped = 0;
// Count of how many bits are "on" //
int onCount = 0;
// Array of "on" bits //
int onList[64];
// Loop through each message bit in this block to populate final block //
for (int j = 0; j < bits; j++) {
// Skip bit if reserved for parity bit //
if ((j & (j - 1)) == 0) { // Check if j is a power of two or 0
skipped++;
continue;
}
bit thisBit;
if (i != blocks) {
// Current overall bit number //
int currentBit = i * messageBits + (j - skipped);
// Current character //
int currentChar = currentBit / (sizeof(char) * 8); // int division
// Value of current bit //
thisBit = currentBit < len * sizeof(char) * 8 ? getCharBit(input[currentChar], currentBit - currentChar * 8) : 0;
}
else {
thisBit = getBit(len / 8, j - skipped + (sizeof(block) * 8 - messageBits));
}
// If bit is "on", add to onList and onCount //
if (thisBit) {
onList[onCount] = j;
onCount++;
}
// Populate final message block //
thisBlock = modifyBit(thisBlock, j, thisBit);
}
// Calculate values of parity bits //
block parityBits = multipleXor(onList, onCount);
// Loop through skipped bits (parity bits) //
for (int k = 1; k < skipped; k++) { // skip bit 0
// If bit is "on", add to onCount
if (getBit(parityBits, sizeof(block) * 8 - skipped + k)) {
onCount++;
}
// Add parity bit to final block //
thisBlock = modifyBit(thisBlock, (int)pow(2, skipped - k - 1), getBit(parityBits, sizeof(block) * 8 - skipped + k));
}
// Add overall parity bit (total parity of onCount) //
thisBlock = modifyBit(thisBlock, 0, onCount & 1);
// Output final block //
// printBlock(thisBlock);
// putchar('\n');
// Add block to encoded blocks //
encoded[i] = thisBlock;
}
// Write encoded message to file //
fwrite(encoded, sizeof(block), blocks + 1, ptr);
}
void decode(block input[], int len, FILE *ptr)
{
// Amount of bits in a block //
int bits = sizeof(block) * 8;
for (int b = 0; b < (len / sizeof(block)); b++) {
printf("On Block %d:\n", b);
// Print initial block //
// printBlock(input[b]);
// Count of how many bits are "on" //
int onCount = 0;
// Array of "on" bits //
int onList[64];
// Populate onCount and onList //
for (int i = 1; i < bits; i++) {
getBit(input[b], i);
if (getBit(input[b], i)) {
onList[onCount] = i;
onCount++;
}
}
// Check for single errors //
int errorLoc = multipleXor(onList, onCount);
if (errorLoc) {
// Check for multiple errors //
if (!(onCount & 1 ^ getBit(input[b], 0))) { // last bit of onCount (total parity) XOR first bit of block (parity bit)
printf("\nMore than one error detected. Aborting.\n");
exit(1);
}
// Flip error bit //
else {
printf("\nDetected error at position %d, flipping bit.\n", errorLoc);
input[b] = toggleBit(input[b], (bits - 1) - errorLoc);
// Re-print block for comparison //
// printBlock(input[b]);
}
}
putchar('\n');
}
}
|