summaryrefslogtreecommitdiff
path: root/gsmdecode/src/data_out.c
blob: a419f5832d53eeade1c8f296c441044218befb4e (plain)
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
/*
 * TODO: memcpy for concatendated sms is unchecked.
 */
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <arpa/inet.h>
//#include <netinet/in.h>
#include "id_list.h"
#include "gsm_desc.h"

#define OUTF(a...)	do { \
	printf("  %3d: %02x ", (int)(data - start), data[0]); \
	printf(a); \
} while (0)

#define OUT(a...)	do { \
	printf(a); \
} while (0)

#define RETTRUNK()	do { \
	printf("%s:%d TRUNKATED (0x%p - 0x%p)\n", __func__, __LINE__, data, end); \
	return; \
} while (0)

extern struct _opt opt;

static void l2_rrm();
static void l2_sms();
static void l2_cc();
static void l2_RRsystemInfo1();
static void l2_MccMncLac();
static void l2_RRsystemInfo2();
//static void l2_RRsystemInfo2ter();
static void l2_RRsystemInfo3C();
static void l2_RRsystemInfo4C();
static void l2_RRsystemInfo6();
static void l2_RRsystemInfo13C();
static void l2_RRimmediateAssTBFC();
static void l2_RRassignCommand();
static void l2_RRassignComplete();
static void l2_RRpagingrequest1();
static void l2_RRpagingrequest2();
static void l2_RRpagingrequest3();
static void l2_RRimmediateAssignment();
static void l2_RRimmAssTBFDirEncHoChaC();
static void l2_MobId();
static void l2_mmm();
static void l2_HoppingChannel();
static void l2_HoppingChannelC();
static void l2_SingleChannel();
static void l2_SingleChannelC();
static void l2_HoppingChannelAssCom();
static void l2_SingleChannelAssCom();
static void l2_MobileAllocation();
static void l2_BcchAllocation();
static void l2_TmsiReallocCommand();
static void l2_RachControlParameters();
static void l2_bcc();
static void l2_Bbis();
static void l2_ChannelRelease();
static void l2_MMcmServiceRequest();
static void l2_RRciphModCmd();
static void l2_RRciphModCompl();
static void l2_RRpagingresponse();
static void l2_RRclassmarkChange();
static void l2_NonCallSS();
static void l2_FacilityRegister();
static void l2_FacilityInvoke();
static void l2_Facility();
static void l2_FacilityReturnResult();
static void l2_UssRequest();
static void l2_UssData();
static void l2_CCReleaseComplete();

static void l2_ChannelNeeded(char *str, unsigned char ch);
static void l2_MNCC(const char *str, unsigned char a, unsigned char b, unsigned char c);

static char *BitRow(unsigned char c, int pos);
static char *PageMode(unsigned char mode);
static char *BitRowFill(unsigned char c, unsigned char mask);

static void dcch_address();
static void dcch_control();
static void ControlChannelDescription();
static void CellOptionsBcch();
static void CellSelectionParameters();
static void RequestReference();
static void TimingAdvance();
static void StartingTime();
static void TypeOfIdentity();
static void l2_NeighbourCellDescription();
static void CellIdentity();
static void MSClassMarkTwo();
static void MSClassMarkOne();
static void ClassMarkThree();
static void cpData();
static void Address(const char *str);
static void TPAddress(const char *str);
static void ChannelDescriptionTwo();
static void CCalerting();
static void CCsetup();
static void ProgressIndicator();
static void Cause();
static void SmsProtocolDataValidity();
static void BearerCap();
static void BCDNumber();
static void AuthenticationRequest();
static void AuthenticationResponse();
static void sms_dcs();
static void sms_udh();
static void sms_default_alphabet();
static void SmscTimestamp();
static void simdatadownload();
static void LocationUpdateRequest();
static void MultiSupportTwo();
static void MeasurmentReport();

static const unsigned char *start;
static const unsigned char *data;
static const unsigned char *end;

struct _nfo
{
	unsigned int flags;
	unsigned char seq_counter;
	unsigned char sapi;
};
#define GSMSP_NFO_SMS			(0x01)
#define GSMSP_NFO_SEGMENTATION		(0x02)
#define GSMSP_NFO_UDHI			(0x04)
#define GSMSP_NFO_DEFAULTALPHABET	(0x08)
#define GSMSP_NFO_SIMDATADL		(0x10)	/* Sim Data Download */
#define GSMSP_NFO_SMSCON		(0x20)	/* Concatenated SMS */
#define GSMSP_NFO_LASTSMSCHUNK		(0x40)

static struct _nfo nfo;

#if 0
struct _sms
{
	unsigned char buf[248 + 3];
	unsigned char *ptr;
};
#endif

/* For concatenating SMS'es */
struct _sms_con
{
	unsigned char buf[8192];
	unsigned char *ptr;
};

struct _con
{
	unsigned char buf[248 + 3];
	unsigned char *ptr;
};
/* Is initialized to 0 (do not remove from .bss) */
static struct _sms_con sms_con;

struct _con con[8];
struct _con *conptr;



/*
 * B-format (and also A-Format)
 */
void
l2_data_out_B(int fn, const unsigned char *input_data, int len)
{
	const unsigned char *from;
	data = input_data;
	start = data;
	end = data + len;
	HEXDUMPF(data, 23 /*len*/, "Format B DATA\n");
	/* Need at least 3 octets */
	if (data + 2 >= end)
		RETTRUNK();

	memset(&nfo, 0, sizeof nfo);
	dcch_address();
	data++;
	dcch_control();
	data++;
	/* FIXME: Why is extended length always set to 1? */
	OUTF("%s EL, Extended Length: %s\n", BitRow(data[0], 0), (data[0] & 1)?"y":"n");
	OUTF("%s M, segmentation: %c\n", BitRow(data[0], 1), ((data[0] >> 1) & 1)?'Y':'N');
	if ((data[0] >> 1) & 1)
		nfo.flags |= GSMSP_NFO_SEGMENTATION;

	OUTF("%s Length: %u\n", BitRowFill(data[0], 0xfc), data[0] >> 2);
	if (data + (data[0] >> 2) < end)
		end = data + (data[0] >> 2) + 1;

	data++;
	if (data >= end)
		return;

	/* Initialization. have to do this only once but there
	 * is no better place to do it atm
	 */
	if (conptr->ptr == NULL)
		conptr->ptr = conptr->buf;

	/* Chunk of a fragmented. */
	/* All SMS type messages go into the same buffer.
	 * Other segmented messages are currently not supported.
	 */
	//if (nfo.flags & GSMSP_NFO_SMS)
	if ((conptr->ptr > conptr->buf) || (nfo.flags & GSMSP_NFO_SEGMENTATION))
	{
		from = data;

		if (conptr->ptr + (end - data) < conptr->buf + sizeof conptr->buf)
		{
			memcpy(conptr->ptr, data, end - data);
			conptr->ptr += (end - data);
		} else {
			OUTF("ERROR, buffer to small!!!\n");
			OUTF("buf: %p, ptr %p, filled: %d len %d\n", conptr->buf, conptr->ptr, conptr->ptr - conptr->buf, end - from);
		}
	}

	if (nfo.flags & GSMSP_NFO_SEGMENTATION)
	{
		OUTF("-------- [MORE DATA FOLLOWS...]\n");
		/* More fragments follow. No need to decode yet */
		return;
	}

	/* Here: segmentation == No */
	/* See if we get an SMS message and if this was the last fragment */
	if (conptr->ptr > conptr->buf)
	{
		start = conptr->buf;
		data = conptr->buf;
		end = conptr->ptr;
		if (nfo.flags & GSMSP_NFO_SMS)
			HEXDUMPF(data, end - data, "Format SMS data\n");
		else
			HEXDUMPF(data, end - data, "Format Bbis (RR, MM or CC)\n");
		l2_Bbis();
		conptr->ptr = conptr->buf;
		return;
	}

		
	l2_Bbis();

}

static void
dcch_control()
{
	if ((data[0] & 0x03) != 3)
	{
		if ((data[0] & 1) == 0)
		{
			OUTF("-------0 Information Frame\n");
			OUTF("%s N(S), Sequence counter: %u\n", BitRowFill(data[0], 0x0e), (data[0] >> 1) & 0x07);
			nfo.seq_counter = ((data[0] >> 1) & 0x07);
			OUTF("%s P\n", BitRow(data[0], 4));
		} else if ((data[0] & 0x03) == 1) {
			OUTF("------01 Supvervisory Frame\n");
			if (((data[0] >> 2) & 0x03) == 0)
				OUTF("----00-- RR Frame (Receive ready)\n");
			else if (((data[0] >> 2) & 0x03) == 1)
				OUTF("----01-- RNR Frame (Receive not ready)\n");
			else if (((data[0] >> 2) & 0x03) == 2)
				OUTF("----10-- REJ Frame (REJect)\n");
			else
				OUTF("----11-- UNKNOWN\n");
			OUTF("%s Poll/Final bit (P/F)\n", BitRow(data[0], 4));
		} 
		OUTF("%s N(R), Retransmission counter: %u\n", BitRowFill(data[0], 0xe0), (data[0] >> 5) & 0x07);
		return;
	}
	OUTF("------11 Unnumbered Frame\n");
	switch (data[0] & 0xec) /* 11101100 */
	{
	case 0x2c: /* 001-11-- According to J.Goeller this is SABM */
	case 0x6c: /* 011-11-- */
		OUTF("%s P\n", BitRow(data[0], 4));
		OUTF("011-11-- SABM frame (Set asynchonous balance mode)\n");
		break;
	case 0x0c: /* 000-11-- */
		OUTF("%s F\n", BitRow(data[0], 4));
		OUTF("000-11-- DM frame (Disconnected mode)\n");
		break;
	case 0x00:
		OUTF("%s P\n", BitRow(data[0], 4));
		OUTF("000-00-- UI frame (Unnumbered information)\n");
		break;
	case 0x40:
		OUTF("%s P\n", BitRow(data[0], 4));
		OUTF("010-00-- DISC frame (DISConnect)\n");
		break;
	case 0x60:
		OUTF("%s P\n", BitRow(data[0], 4));
		OUTF("011-00-- UA frame (Unnumbered acknowledgement)\n");
		break;
	default:
		OUTF("%s P/F\n", BitRow(data[0], 4));
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xec));
		break;
	}
}

static void
dcch_address()
{
	if (data[0] & 1)
		OUTF("-------1 Extended Address: 1 octet long\n");
	else
		OUTF("-------0 Extended Address: more octets follow\n");

	if ((data[0] >> 1) & 1)
		OUTF("------1- C/R: Command\n");
	else
		OUTF("------0- C/R: Response\n");

	if (data[0] & 1)
	{
		/* SAPI */
		nfo.sapi = (data[0] >> 2) & 0x07;
		conptr = &con[nfo.sapi];
		switch ((data[0] >> 2) & 0x07)
		{
		case 0x03:
			nfo.flags |= GSMSP_NFO_SMS;
			OUTF("---011-- SAPI: SMS and SS\n");
			break;
		case 0x00:
			OUTF("---000-- SAPI: RR, MM and CC\n");
			break;
		default:
			OUTF("%s SAPI: UNKNWON\n", BitRowFill(data[0], 0x1c));
			break;
		}
	
		switch ((data[0] >> 4 ) & 0x03)
		{
		case 0x00:
			OUTF("%s Link Protocol Disciminator: GSM (not Cell Broadcasting)\n", BitRowFill(data[0], 0x60));
			break;
		case 0x01:
			OUTF("%s Link Protocol Disciminator: Cell Broadcasting (CBS)\n", BitRowFill(data[0], 0x60));
			break;
		default:
			OUTF("%s Link Protocol Disciminator: UNKNOWN %u\n", BitRowFill(data[0], 0x60), (data[0] >> 5) & 0x03);
		}
	} else {
		switch ((data[0] >> 2))
		{
		case 0x03:
			nfo.flags |= GSMSP_NFO_SMS;
			OUTF("000011-- SAPI: SMS and SS\n");
			break;
		case 0x00:
			OUTF("000000-- SAPI: RR, MM and CC\n");
			break;
		default:
			OUTF("%s SAPI: UNKNOWN\n", BitRowFill(data[0], 0xfc));
			break;
		}
		data++;
		if (data >= end)
			RETTRUNK();
		if (data[0] & 1)
			OUTF("-------1 Extended Address: 1 octet long\n");
		else
			OUTF("-------0 Extended Address: more octets follow [ERROR]\n");
		OUTF("%s Terminal Endpoint Identifier (TEI): %u\n", BitRowFill(data[0], 0xfe), data[0] >> 1);
	}
}

void
l2_data_out_Bbis(int fn, const unsigned char *input_data, int len)
{
	int i;


	memset(&nfo, 0, sizeof nfo);
	if (len <= 0)
		return;

	data = input_data;
	start = data;

	/* 2008-01-05: Motorola output has Length field wrongly set.. */
	if (opt.flags & FL_MOTOROLA)
		i = data[0];
	else
		i = data[0] >> 2;
	if (len - 1 < i)
		OUTF("WARN: packet to short\n");

	len = MIN(len - 1, i);

	/* len = number of octets following the length field */
	end = data + len + 1;

	HEXDUMPF(data, 23 /*len*/, "Format Bbis DATA\n");
	if (len <= 0)
		return;

	OUTF("%s Pseudo Length: %d\n", BitRowFill(data[0], 0xfc), data[0] >> 2);
	data++;
	l2_Bbis();
}

static void
l2_Bbis()	/* GSM 04.07 11.2.3.2.1 */
{
	if (data >= end)
		RETTRUNK();

	switch (data[0] >> 7)
	{
	case 1:
		OUTF("1------- Direction: To originating site\n");
		break;
	default:
		OUTF("0------- Direction: From originating site\n");
	}

	OUTF("%s %d TransactionID\n", BitRowFill(data[0], 0x70), (data[0] >> 4) & 7);

	switch (data[0] & 0x0f)
	{
	case 0:
		OUTF("----0000 Group Call Control [FIXME]\n");
		break;
	case 1:
		OUTF("----0001 Broadcast call control [FIXME]\n");
		data++;
		l2_bcc();
		/* TS GSM 04.69 */
		break;
	case 2:
		OUTF("----0010 PDSS1 [FIXME]\n");
		break;
	case 3:
		OUTF("----0011 Call control. call related SS messages\n");
		data++;
		l2_cc();
		/* TS 24.008 */
		break;
	case 4:
		OUTF("----01-- PDSS2 [FIXME]\n");
		break;
	case 5:
		OUTF("----0101 Mobile Management Message (non GPRS)\n");
		data++;
		/* TS 24.008 */
		l2_mmm();
		break;
	case 6:
		OUTF("----0110 Radio Resouce Management\n");
		data++;
		l2_rrm();
		break;
	case 7:
		OUTF("----0111 RFU [FIXME]\n");
		break;
	case 8:
		OUTF("----1000 GPRS Mobile Management\n");
		/* in GMMattachAccept */
		/* in GMMidentityRequest */
		OUTF("FIXME: possible IMEI in here\n");
		break;
	case 9:
		OUTF("----1001 SMS messages\n");
		data++;
		l2_sms();
		/* TS 04.11 */
		break;
	case 0x0a:
		OUTF("----1011 GRPS session management messages [FIXME]\n");
		break;
	case 0x0b:
		OUTF("----1011 Non-call related SS message\n");
		data++;
		l2_NonCallSS();
		/* GSM 04.80 */
		break;
	case 0x0c:
		OUTF("----1100 Location services [FIXME]\n");
		break;
	case 0x0d:
		OUTF("----1101 RFU [FIXME]\n");
		break;
	case 0x0e:
		OUTF("----1110 Extension of the PD to one octet length [FIXME]\n");
		break;
	case 0x0f:
		OUTF("----1111 Tests procedures describe in TS GSM 11.10 [FIXME]\n");
		break;
	default:
		OUTF("%s 0x%02x UNKNOWN\n", BitRowFill(data[0], 0x0f), data[0] & 0x0f);
	}

}

/*
 * Broadcast Call Control (04.69)
 */
static void
l2_bcc()
{
	if (data >= end)
		RETTRUNK();

	/* Message type 04.69:9.3*/
	switch (data[0] & 0x3f)
	{
	case 0x06: /* 0-000110 */
		OUTF("--000110 ???\n");
		break;
	default:
		OUTF("--?????? UNKNOWN [FIXME]\n");
		return;
	}

	/* Call reference 04.69:9.4.1*/

	/* Orig indication 04.69:9.5.5*/
	/* Spare half octet 04.69:9.4.5*/

}

/*
 * ProtDisc3
 */
static void
l2_cc()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Send Sequence Number: %u\n", BitRowFill(data[0], 0xc0), data[0] >> 6);

	if ((data[0] & 0x3f) == 0x01)
	{
		OUTF("--000001 Call Alerting\n");
		data++;
		CCalerting();
	} else if ((data[0] & 0x3f) == 0x02) {
		OUTF("--000010 Call Proceesing\n");
		if (++data >= end)
			return;
		OUTF("FIXME %s\n", __func__);
	} else if ((data[0] & 0x3f) == 0x07) {
		OUTF("--000111 Call Connect\n");
		if (++data >= end)
			return;
		OUTF("FIXME %s\n", __func__);
	} else if ((data[0] & 0x3f) == 0x08) {
		OUTF("--001000 Call Confirmed\n");
		if (++data >= end)
			return;
		if (data[0] != 0x04)
			return;
		OUTF("--000010 Bearer Capability\n");
		data++;
		BearerCap();
	} else if ((data[0] & 0x3f) == 0x05) {
		OUTF("--000101 Call Setup\n");
		data++;
		CCsetup();
	} else if ((data[0] & 0x3f) == 0x03) {
		OUTF("--000011 Call Progress\n");
		data++;
		ProgressIndicator();
	} else if ((data[0] & 0x3f) == 0x0f) {
		OUTF("--001111 Connect Acknowledge\n");
	} else if ((data[0] & 0x3f) == 0x25) {
		OUTF("--100101 Disconnect\n");
		data++;
		Cause();
	} else if ((data[0] & 0x3f) == 0x2d) {
		OUTF("--101101 CC Release\n");
		if (++data >= end)
			RETTRUNK();
		if (data[0] == 0x08)
		{
			data++;
			Cause();
		}
	} else if ((data[0] & 0x3f) == 0x2a) {
		OUTF("--101010 CC Release Complete\n");
		if (++data >= end)
			RETTRUNK();
		if (data[0] == 0x08)
		{
			data++;
			Cause();
		} 
		if (data >= end)
			RETTRUNK();
		if (data[0] == 0x1c)
		{
			/* facility */
			OUTF("FIXME\n");
		}
	} else {
		OUTF("%s FIXME %s\n", BitRowFill(data[0], 0xff), __func__);
	}
}

/*
 * ----0101
 * ProtDisc5 - Mobile Management message (non GPRS)
 */
static void
l2_mmm()
{
	if (data >= end)
		return;
	OUTF("%s SendSequenceNumber: %d\n", BitRowFill(data[0], 0xc0), data[0] >> 6);
	switch (data[0] & 0x3f)
	{
	case 1:
		//l2_MMimsiDetIndication(data + 1, end);
		OUTF("--000001 Imsi Det Indication\n"); /* FIXME */
		OUTF("FIXME: Possible IMSI in here\n");
		OUTF("FIXME: Possible cipher mode here\n");
		break;
	case 2:
		OUTF("--000010 Location Update Accept\n"); /* FIXME */
		break;
	case 4:
		OUTF("--000100 Message Type: Location Updating Reject\n");
		break;
	case 8:
		OUTF("--001000 MM Location Update Request\n"); /* FIXME */
		data++;
		LocationUpdateRequest();
		break;
	case 0x12:
		OUTF("--010010 Authentication Request\n");
		data++;
		AuthenticationRequest();
		break;
	case 0x14:
		OUTF("--010100 Authentication Response\n");
		data++;
		AuthenticationResponse();
		break;
	case 0x18:
		OUTF("--011000 MMIdentityRequest\n");
		data++;
		TypeOfIdentity();
		break;
	case 0x19:
		OUTF("--011001 MMidentityResponse\n");
		data++;
		l2_MobId();
		break;
	case 0x1a:
		OUTF("--011010 TMSI Realloc Command\n");
		data++;
		l2_TmsiReallocCommand();
		break;
	case 0x21:
		OUTF("--100001 CM Service Accept\n");
		data++;
		break;
	case 0x24:	/* --100100 */
		OUTF("--100100 MMcmServiceRequest\n");
		data++;
		l2_MMcmServiceRequest();
		/* in multisupport2 and others! */
		break;
	default:
		OUTF("UNKNOWN\n");
	}
}

/*
 * ProtDisc6 - Radio Resource Management Messages
 */
static void
l2_rrm()
{
	if (data >= end)
		return;

	switch (data[0] & 0x3f)
	{
	case 0x00:
		OUTF("00000000 System Information Type 13\n");
		data++;
		l2_RRsystemInfo13C();
		break;
	case 0x06:
		OUTF("00000110 System Information Type 5ter\n");
		data++;
		l2_BcchAllocation();
		break;
	case 0x0d:
		OUTF("00001101 Channel Release\n");
		data++;
		l2_ChannelRelease();
		break;
	case 0x15:
		OUTF("00010101 RR Measurement Report C\n");
		data++;
		MeasurmentReport();
		break;
	case 0x16:
		OUTF("00010110 RRclassmarkChange\n");
		data++;
		l2_RRclassmarkChange();
		break;
	case 0x19:
		OUTF("00011001 RRsystemInfo1\n");
		data++;
		l2_RRsystemInfo1();
		break;
	case 0x1a:
		OUTF("00011010 RRsystemInfo2\n");
		data++;
		l2_RRsystemInfo2();
		break;
	case 0x1B:	/* 0001 1011 */
		OUTF("00011011 RRsystemInfo3C\n");
		data++;
		l2_RRsystemInfo3C();
		break;
	case 0x1c:
		OUTF("00011100 RRsystemInfo4-C\n");
		data++;
		l2_RRsystemInfo4C();
		break;
	case 0x1d:
		/* From SDCCH */
		OUTF("00011101 Neighbour Cells Description\n");
		data++;
		l2_NeighbourCellDescription();
		break;
	case 0x1e:
		/* From SDCCH */
		OUTF("00011110 System Information Type 6\n");
		data++;
		l2_RRsystemInfo6();
		break;
	case 0x21:
		OUTF("00100001 Paging Request Type 1\n");
		data++;
		l2_RRpagingrequest1();
		break;
	case 0x22:
		OUTF("00100010 Paging Request Type 2\n");
		data++;
		l2_RRpagingrequest2();
		break;
	case 0x24:
		OUTF("00100100 Paging Request Type 3\n");
		data++;
		l2_RRpagingrequest3();
		break;
	case 0x27:
		OUTF("0-100111 RRpagingResponse\n");
		OUTF("-x------ Send sequence number: %d\n", (data[0] >> 7) & 0x01);
		data++;
		l2_RRpagingresponse();
		break;
	case 0x29:
		OUTF("0-101001 RR Assign Complete\n");
		data++;
		l2_RRassignComplete();
		break;
	case 0x2e:
		OUTF("00101110 RR Assign Command\n");
		data++;
		l2_RRassignCommand();
		break;
	case 0x32:
		OUTF("00110010 RR Cipher Mode Complete\n");
		data++;
		l2_RRciphModCompl();
		break;
	case 0x35:
		OUTF("00110101 RR Cipher Mode Command\n");
		data++;
		l2_RRciphModCmd();
		break;
	case 0x3f:
		OUTF("0-111111 RRimmediateAssignment\n");
		OUTF("-x------ Send sequence number: %d\n", (data[0] >> 7) & 0x01);
		data++;
		l2_RRimmediateAssignment();
		break;
	default:
		OUTF("???????? UNKNOWN. FIXME\n");
	}
}
static void
l2_RRsystemInfo13C()
{
	if (data >= end);
		return;
	if (data[0] >> 7)
		OUTF("1------- SI 13 Restoctet present\n");
	else
		OUTF("0------- SI 13 Restoctet NOT present\n");
	OUTF("%s BCCH_CHANGE_MARK : %d\n", BitRowFill(data[0], 0x70), (data[0] >> 4) & 0x07);
	switch (data[0] & 0x0f)
	{
	case 0x00:
		OUTF("----0000 SI_CHANGE_FIELD : Update of unspecified SI messages\n");
		break;
	case 0x01:
		OUTF("----00001 SI_CHANGE_FIELD : Update of unspecified SI1 messages\n");
		break;
	case 0x02:
		OUTF("----0010 SI_CHANGE_FIELD : Update of unspecified SI2 messages\n");
		break;
	case 0x03:
		OUTF("----0011 SI_CHANGE_FIELD : Update of unspecified SI3,4,7,8 messages\n");
		break;
	case 0x04:
		OUTF("----0100 SI_CHANGE_FIELD : Update of unspecified SI9 messages\n");
		break;
	default:
		OUTF("----???? Unknown %d\n", data[0] & 0x0f);
		break;
	}
	OUTF("FIXME: implement me\n");

}

static void
l2_RRimmediateAssignment()
{
	if (data >= end)
		return;

	/* Octect 4, 0x79 */
	OUTF("%s\n", PageMode(data[0] & 0x03));

	if ((data[0] >> 6) & 0x01)
		OUTF("-1------ Two messages assign.: 1. message of..(continue)\n");
	else
		OUTF("-0------ No meaning\n");
	if ((data[0] >> 5) & 0x01)
		OUTF("--1----- Assigns a resource identified in the IA rest octets.\n");
	else
		OUTF("--0----- Downlink assig to MS: No meaning\n");
	if ((data[0] >> 4) & 0x01)
	{
		OUTF("---1---- Temporary Block Flow (TBF)\n");
		data++;
		l2_RRimmediateAssTBFC();
		return;
	}
	else
		OUTF("---0---- This messages assigns a dedicated mode resource\n");
	data++;
	if (data >= end)
		return;
	
	/* Channel Description */
	ChannelDescriptionTwo();

	if (data >= end)
		return;
	if (((data[0] >> 2) & 0x07) == 0)
		l2_SingleChannel();
	else if (((data[0] >> 4) & 0x01) == 1)
	{
		l2_HoppingChannel();
	} else {
		OUTF("xxx0??xxx UNKNOWN %d\n", (data[0] >> 3) & 0x3);
	}
}

static void
l2_SingleChannel()
{
	l2_SingleChannelC();
	RequestReference();
	TimingAdvance();
	l2_MobileAllocation();
	if (data >= end)
		return;
}

static void
l2_SingleChannelC()
{
	int freq;
	if (data + 1 >= end)
		RETTRUNK();
	OUTF("%s Training seq. code: %d\n", BitRowFill(data[0], 0xe0), data[0] >> 5);
	OUTF("---0---- Single channel\n");
	freq = (data[0] & 0x03) << 8;
	data++;
	freq |= data[0];
	OUTF("........ Absolute RF channel number: %u\n", freq);
	data++;
}

static void
l2_HoppingChannel()
{
	unsigned char maio = 0;
	OUTF("%s Training seq. code : %d\n", BitRowFill(data[0], 0xe0), data[0] >> 5);
	OUTF("---1---- HoppingChannel\n");
	maio = (data[0] & 0x0f) << 2;
	data++;
	if (data >= end)
		RETTRUNK();
	maio |= (data[0] >> 6);
	OUTF("........ MAIO %d\n", maio);
	OUTF("%s Hopping Seq. Number: %d\n", BitRowFill(data[0], 0x3f), data[0] & 0x3f);

	data++;
	RequestReference();
	TimingAdvance();
	l2_MobileAllocation();
	if (data >= end)
		return;	/* finished. not truncated! */

	OUTF("FIXME, more data left here???\n");
}

static void
l2_HoppingChannelC()
{
	OUTF("FIXME-2\n");
}

static void
l2_MobileAllocation()
{
	int c = 64, pos;
	char *str = "Mobile allocation RF chann.";
	const unsigned char *thisend;

	if (data >= end)
		RETTRUNK();
	OUTF("%s Length of Mobile Allocation: %d\n", BitRowFill(data[0], 0xff), data[0]);
	thisend = data + data[0] + 1;
	if (thisend > end)
	{
		OUTF("xxxxxxxx ERROR: Packet to short or length to long\n");
		thisend = end;
	}

	data++;
	/* If mobile allocation has length 0 */
	if (data >= thisend)
		return;

	while (data < thisend)
	{
		pos = 7;
		while (pos >= 0)
		{
			if ((data[0] >> pos) & 1)
				OUTF("%s %s%d\n", BitRow(data[0], pos), str, c - (7 - pos));
			pos--;

		}

		c -= 8;
		data++;
		if (c <= 0)
			break;
	}
}

/*
 * From RRsystemInfo2
 */
static void
l2_BcchAllocation()
{
	int c, pos;
	char *str = "BCCH alloc. RF chan.: ";

#if 0
	/* goeller script for Info2 outputs channels 128 + 127
	 * but opengpa outputs bitmap format for info2.
	 * We do what opengpa does. (correct?)
	 */
	if ((data[0] >> 7))
		OUTF("1------- %s%d\n", str, 128);
	if ((data[0] >> 6) & 1)
		OUTF("-1------ %s%d\n", str, 127);
#endif
	if ((data[0] >> 6) == 0x00)
		OUTF("00------ Bitmap format: 0\n");
	else {
		OUTF("%s Bitmap format: UNKNOWN [FIXME]\n", BitRowFill(data[0], 0xc0));
		return;
	}

	if ((data[0] & 0x8e) == 0x8e)
	{
		/* From System Information Type 5ter */
		OUTF("1---111- Variable Bitmap SI5ter [FIXME]\n");
		return;
	}

	if ((data[0] >> 5) & 1)
		OUTF("--1----- Extension Indicator: The IE carries only a part of the BA\n");
	else
		OUTF("--0----- Extension Indicator: The IE carries the complete BA\n");
	OUTF("---x---- BCCH alloc. seq. num: %d\n", (data[0] >> 4) & 1);
	if ((data[0] >> 3) & 1)
		OUTF("----1--- %s%d\n", str, 124);
	if ((data[0] >> 2) & 1)
		OUTF("-----1-- %s%d\n", str, 123);
	if ((data[0] >> 1) & 1)
		OUTF("------1- %s%d\n", str, 122);
	if (data[0] & 1)
		OUTF("-------1 %s%d\n", str, 121);

	data++;
	c = 120;
	while (data < end)
	{
		pos = 7;
		while (pos >= 0)
		{
			if ((data[0] >> pos) & 1)
				OUTF("%s %s%d\n", BitRow(data[0], pos), str, c - (7 - pos));
			pos--;

		}

		c -= 8;
		data++;
		if (c <= 0)
			break;
	}
}

static void
l2_RRimmediateAssTBFC()
{
	if (data >= end)
		return;

	/* GPRS Packet Channel Description */
	OUTF("%s Channel Type : %d\n", BitRowFill(data[0], 0xf8), data[0] >> 3);
	OUTF("%s Time Slot Number : %d\n", BitRowFill(data[0], 0x07), data[0] & 0x07);
	data++;
	if (data >= end)
		return;

	OUTF("%s Tranining Sequence Code: %d\n", BitRowFill(data[0], 0xe0), data[0] >> 5);
	if ((data[0] >> 4) & 0x01)
	{
		OUTF("---1---- Direct Encoding of Hopping Channels\n");
		l2_RRimmAssTBFDirEncHoChaC();
		return;
	} else {
		OUTF("---0---- non-hopping RF channel config or indirect encoding of hopping RFCC\n");
	}

	if ((data[0] >> 3) & 0x01)
	{
		OUTF("----1--- indirect encoding of hopping RF channel config\n");
	} else {
		OUTF("----0--- RRimmAssTBFaRFCN-C FIXME\n");
		return;
	}

	data++;
	if (data >= end)
		RETTRUNK();
	OUTF("xxxxxxxx MAIO [FIXME]\n");

	data++;
	RequestReference();
	TimingAdvance();
	l2_MobileAllocation();
	if (data >= end)
		RETTRUNK();
	OUTF("FIXME: implenet\n");
}

static void
l2_RRsystemInfo1()
{
	int ca;

	if (data + 1 >= end)
		return;
	switch (data[0] >> 6)
	{
		case 0x00:
			OUTF("00------ Bitmap 0 format\n");
			break;
		case 0x01:
			OUTF("10------ Bitmap format: (FIXME)\n");
			break;
		default:
			OUTF("%s Bitmap %d format (FIXME)\n", BitRowFill(data[0], 0xc0), data[0] >> 6);
	}
	if ((data[0] >> 3) & 1)
		OUTF("----1--- Cell Allocation   : ARFCN 124\n");
	if ((data[0] >> 2) & 1)
		OUTF("-----1-- Cell Allocation   : ARFCN 123\n");
	if ((data[0] >> 1) & 1)
		OUTF("------1- Cell Allocation   : ARFCN 122\n");
	if (data[0] & 1)
		OUTF("-------1 Cell Allocation   : ARFCN 121\n");

	ca = 120;
	while (ca > 0)
	{
		data++;
		if (data >= end)
			return;
		if ((data[0] >> 7) & 1)
			OUTF("1------- Cell Allocation   : ARFCN %d\n", ca);
		if ((data[0] >> 6) & 1)
			OUTF("-1------ Cell Allocation   : ARFCN %d\n", ca - 1);
		if ((data[0] >> 5) & 1)
			OUTF("--1----- Cell Allocation   : ARFCN %d\n", ca - 2);
		if ((data[0] >> 4) & 1)
			OUTF("---1---- Cell Allocation   : ARFCN %d\n", ca - 3);
		if ((data[0] >> 3) & 1)
			OUTF("----1--- Cell Allocation   : ARFCN %d\n", ca - 4);
		if ((data[0] >> 2) & 1)
			OUTF("-----1-- Cell Allocation   : ARFCN %d\n", ca - 5);
		if ((data[0] >> 1) & 1)
			OUTF("------1- Cell Allocation   : ARFCN %d\n", ca - 6);
		if (data[0] & 1)
			OUTF("-------1 Cell Allocation   : ARFCN %d\n", ca - 7);

		ca -= 8;
	}

	data++;

	l2_RachControlParameters();
	if (data >= end)
		return;
	OUTF("FIXME: NCH Position\n");
}

static void
l2_RachControlParameters()
{
	int ca = -1;

	if (data >= end)
		return;

	if (((data[0] >> 6) & 0x03) == 0)
		ca = 1;
	else if (((data[0] >> 6) & 0x03) == 0x01)
		ca = 2;
	else if (((data[0] >> 6) & 0x03) == 0x02)
		ca = 4;
	else if (((data[0] >> 6) & 0x03) == 0x03)
		ca = 7;
	OUTF("%s Max. of retransmiss : %u\n", BitRowFill(data[0], 0xc0), ca);
	if (((data[0] >> 2) & 0x0f) <= 9)
	{
		ca = ((data[0] >> 2) & 0x0f) + 3;
	} else {
		switch ((data[0] >> 2) & 0x0f)
		{
		case 10:	/* --1010-- */
			ca = 14;
			break;
		case 11:
			ca = 16;
			break;
		case 12:
			ca = 20;
			break;
		case 13:
			ca = 25;
			break;
		case 14:
			ca = 32;
			break;
		case 15:
			ca = 50;
			break;
		default:
			ca = -1;
			break;
		}
	}
	OUTF("%s slots to spread TX : %u\n", BitRowFill(data[0], 0x3c), ca);
	switch ((data[0] >> 1) & 0x01)
	{
	case 0:
		OUTF("------0- The cell is barred  : no\n");
		break;
	case 1:
		OUTF("------1- The cell is barred  : yes\n");
		break;
	}

	switch (data[0] & 0x01)
	{
	case 0:
		OUTF("-------0 Call reestabl.i.cell: allowed\n");
		break;
	case 1:
		OUTF("-------1 Cell reestabl.i.cell: not allowed\n");
	}

	data++;
	if (data >= end)
		return;
	switch ((data[0] >> 2) & 0x01)
	{
	case 0:
		OUTF("-----0-- Emergency call EC 10: allowed\n");
		break;
	case 1:
		OUTF("-----1-- Emergency call EC 10: not allowed\n");
		break;
	}
	OUTF("%s Acc ctrl cl 11-15: 0 = permitted, 1 = forbidden\n", BitRowFill(data[0], 0xf8));
	OUTF("%s Acc ctrl cl  8- 9: 0 = permitted, 1 = forbidden\n", BitRowFill(data[0], 0x03));
	OUTF("%s Ordinary subscribers (8)\n", BitRowFill(data[0], 0x01));
	OUTF("%s Ordinary subscribers (9)\n", BitRowFill(data[0], 0x02));
	OUTF("%s Emergency call (10): %s\n", BitRowFill(data[0], 0x04), ((data[0] >> 4) & 1)?"Class 11-15 only":"Everyone");
	OUTF("%s Operator Specific (11)\n", BitRowFill(data[0], 0x08));
	OUTF("%s Security service (12)\n", BitRowFill(data[0], 0x10));
	OUTF("%s Public service (13)\n", BitRowFill(data[0], 0x20));
	OUTF("%s Emergency service (14)\n", BitRowFill(data[0], 0x40));
	OUTF("%s Network Operator (15)\n", BitRowFill(data[0], 0x80));
	data++;
	if (data >= end)
		RETTRUNK();
	OUTF("%s Acc ctrl cl  0- 7: 0 = permitted, 1 = forbidden\n", BitRowFill(data[0], 0xff));
	OUTF("%s Ordinary subscribers (0-7)\n", BitRowFill(data[0], 0xff));
	data++;
}

static char *
BitRow(unsigned char c, int pos)
{
	unsigned char bit = 0;
	static char buf[9];

	if ((c >> pos) & 1)
		bit = 1;

	if (pos == 0)
		snprintf(buf, sizeof buf, "-------%d", bit);
	else if (pos == 1)
		snprintf(buf, sizeof buf, "------%d-", bit);
	else if (pos == 2)
		snprintf(buf, sizeof buf, "-----%d--", bit);
	else if (pos == 3)
		snprintf(buf, sizeof buf, "----%d---", bit);
	else if (pos == 4)
		snprintf(buf, sizeof buf, "---%d----", bit);
	else if (pos == 5)
		snprintf(buf, sizeof buf, "--%d-----", bit);
	else if (pos == 6)
		snprintf(buf, sizeof buf, "-%d------", bit);
	else if (pos == 7)
		snprintf(buf, sizeof buf, "%d-------", bit);

	return buf;
}

static char *
BitRowFill(unsigned char c, unsigned char mask)
{
	static char buf[9];
	
	memset(buf, '-', sizeof buf);
	buf[sizeof buf - 1] = '\0';
	int i = 0;
	while (i < 8)
	{
		if ((mask >> i) & 1)
		{
			if ((c >> i) & 1)
				buf[7 - i] = '1';
			else
				buf[7 - i] = '0';
		}
		i++;
	}

	return buf;
}

static void
l2_RRsystemInfo2()
{
	if (data >= end)
		RETTRUNK();

	l2_BcchAllocation();
	if (data >= end)
		RETTRUNK();

	int c = 7;
	while (c >= 0)
	{
		if ((data[0] >> c) & 1)
			OUTF("%s BCCH carrier with NCC = %d is permitted for monitoring\n", BitRow(data[0], c), c);
		c--;
	}
	data++;
	if (data >= end)
		RETTRUNK();

	l2_RachControlParameters();

	if (data > end)	/* Note: not >= */
		RETTRUNK();
}

#if 0
static void
l2_RRsystemInfo2ter()
{
	if (data >= end)
		return;
	if ((data[0] >> 7) == 0)
		OUTF("%s Bitmap 0 format\n", BitRowFill(data[0], 0x8e));
	else {
		/* 0x8e = 10001110 */
		if (((data[0] >> 1) & 0x07) == 0x04)
			OUTF("1---100- 1024 range\nFIXME\n");
		else if (((data[0] >> 1) & 0x07) == 0x05)
			OUTF("1---101- 512 range\nFIXME\n");
		else if (((data[0] >> 1) & 0x07) == 0x06)
			OUTF("1---110- 128 range\nFIXME\n");
		else if (((data[0] >> 1) & 0x07) == 0x07)
			OUTF("1---111- variable Bitmap\nFIXME\n");
		else
			OUTF("1---xxx- UNKNOWN 0x%08x\n", data[0]);
	}
	OUTF("FIXME\n");
}
#endif


/*
 * RRsystemInfo4-C
 */
static void
l2_RRsystemInfo4C()
{
	if (data + 2 >= end)
		RETTRUNK();
	l2_MccMncLac();
	CellSelectionParameters();
	l2_RachControlParameters();
	if (data + 1 >= end)
		RETTRUNK();

	if (data[0] != 0x64)
	{
		OUTF("UNKNWON\n");
		return;
	}
	OUTF("01100100 Channel Description\n");
	data++;
	ChannelDescriptionTwo();
	if (data >= end)
		RETTRUNK();
	//OUTF("%s Training sequence code: %d\n", BitRowFill(data[0], 0xe0), data[0] >> 5);
	if (((data[0] >> 3) & 0x1) == 0)
	{
		l2_SingleChannelC();
	} else {
		l2_HoppingChannelC();
	}

	if (data >= end)
		return;

	if (data[0] != 0x72)
	{
		OUTF("UNKNOWN\n");
		return;
	}
	OUTF("01110010 CBCH Mobile Allocation\n");
	data++;
	if (data >= end)
		RETTRUNK();
	l2_MobileAllocation();

//	OUTF("FIXME\n");
}

/*
 * Output MCC, MNC and LAC. consume 5 bytes.
 */
static void
l2_MccMncLac()
{
	if (data + 2 >= end)
		return;
	unsigned short lac;
	
	l2_MNCC("Mobile Country Code", data[0] & 0x0f, (data[0] >> 4) & 0x0f, data[1] & 0x0f);
	data++;
	l2_MNCC("Mobile Network Code", data[1] & 0x0f, (data[1] >> 4) & 0x0f, (data[0] >> 4) & 0x0f);
	data += 2;

	if (data + 1 >= end)
		return;

	lac = data[0];
	lac = (lac << 8) | data[1];
	OUTF("%-8u [0x%02x%02x] Local Area Code\n", lac, data[0], data[1]);
	data += 2;
}
/*
 * RRsystemINfo3-C
 */
static void
l2_RRsystemInfo3C()
{
	CellIdentity();
	l2_MccMncLac();

	ControlChannelDescription();
	CellOptionsBcch();
	CellSelectionParameters();
	l2_RachControlParameters();

	/* FIXME: complete here */
}

static void
l2_RRsystemInfo6()
{
	CellIdentity();
	l2_MccMncLac();
	CellOptionsBcch();
	if (data >= end)
		RETTRUNK();
	OUTF("%s Network Colour Code: %u\n", BitRowFill(data[0], 0xff), data[0]);
}

static void
CellIdentity()
{
	unsigned short id;

	if (data + 1 >= end)
		return;

	id = data[0];
	id = (id << 8) | data[1];
	OUTF("%-8u [0x%02x%02x] Cell identity\n", id, data[0], data[1]);
	data += 2;
}

static void
ControlChannelDescription()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Spare bit (should be 0)\n", BitRow(data[0], 7));
	if ((data[0] >> 6) & 1)
		OUTF("-1------ MSs in the cell shall apply IMSI attach/detach procedure\n");
	else
		OUTF("-0------ MSs in cell are not allowed attach/detach procedure\n");
	OUTF("%s Number of blocks: %u\n", BitRowFill(data[0], 0x38), (data[0] >> 3) & 0x07);

	switch (data[0] & 0x07)
	{
	case 0x00:
		OUTF("-----000 1 basic physical channel for CCCH, not combined with SDCCHs\n");
		break;
	case 0x01:
		OUTF("-----001 1 basic physical channel for CCCH,     combined with SDCCHs\n");
		break;
	case 0x02:
		OUTF("-----010 2 basic physical channel for CCCH, not combined with SDCCHs\n");
		break;
	case 0x04:
		OUTF("-----100 3 basic physical channel for CCCH, not combined with SDCCHs\n");
		break;
	case 0x06:
		OUTF("-----110 4 basic physical channel for CCCH, not combined with SDCCHs\n");
		break;
	default:
		OUTF("%s Unknown CCCH config (ERROR)\n", BitRowFill(data[0], 0x07));
		break;
	}

	data++;
	if (data >= end)
		RETTRUNK();

	OUTF("%s spare bits (should be 0)\n", BitRowFill(data[0], 0xf8));
	OUTF("%s %u multi frames period for paging request\n", BitRowFill(data[0], 0x07), (data[0] & 0x07) + 2);

	data++;
	if (data >= end)
		RETTRUNK();
	OUTF("%s T3212 TimeOut value: %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
}

static void
CellOptionsBcch()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s spare bit (should be 0)\n", BitRowFill(data[0], 0x80));
	if ((data[0] >> 6) & 1)
		OUTF("-1------ Power control indicator is set\n");
	else
		OUTF("-0------ Power control indicator is not set\n");

	if (((data[0] >> 4) & 0x03) == 0x00)
		OUTF("--00---- MSs may use uplink DTX\n");
	else if (((data[0] >> 4) & 0x03) == 0x01)
		OUTF("--01---- MSs shall use uplink DTX\n");
	else if (((data[0] >> 4) & 0x03) == 0x02)
		OUTF("--10---- MSs shall not use uplink DTX\n");
	else
		OUTF("%s DTX UNKNOWN [ERROR]\n", BitRowFill(data[0], 0x30));

	OUTF("%s Radio Link Timeout: %u\n", BitRowFill(data[0], 0x0f), ((data[0] & 0x0f) + 1 ) * 4);
	data++;
}

static void
l2_MNCC(const char *str, unsigned char a, unsigned char b, unsigned char c)
{
	char buf[128];
	char f[12];

	snprintf(f, sizeof f, "%x%x%x", a, b, c);
	/* Nokia netmonitor shows NC's like '30F' and '10F' */
	snprintf(buf, sizeof buf, "%-8s %s\n", f, str);

#if 0
	buf[0] = '\0';
	if (a != 0x0f)
	{
		snprintf(buf, sizeof buf, "%x", a);
		if (b != 0x0f)
		{
			snprintf(buf + 1, sizeof buf - 1, "%x", b);
			if (c != 0x0f)
				snprintf(buf + 2, sizeof buf - 2, "%x", c);
		}
	}
	snprintf(buf + strlen(buf), sizeof buf - strlen(buf), "  - %s\n", str);
#endif

	OUTF(buf);
}

static char *
PageMode(unsigned char mode)
{
	switch (mode)
	{
	case 0:
		return "------00 Page Mode: Normal paging";
	case 1:
		return "------01 Page Mode: Extended paging";
	case 2:
		return "------10 Page Mode: Paging reorganisation";
	case 3:
		return "------11 Page Mode: reserved / same as before";
	}

	return "------?? UNKNOWN\n";
}

static void
l2_RRpagingrequest1()
{
	if (data >= end)
		return;

	OUTF("%s\n", PageMode(data[0] & 0x3));

	/* FIXME complete here */

	data++;
	if (data >= end)
		return;

	l2_MobId();
	if (data >= end)
		return;	 /* sometimes it's end here */
	if (data[0] == 0x17)
	{
		data++;
		l2_MobId();
		return;
	}
	OUTF("ERR: wrong data\n");
}

static void
l2_ChannelNeeded(char *str, unsigned char ch)
{
	switch (ch)
	{
	case 0x00:
		OUTF("%s Channel Needed: Any channel\n", str);
		break;
	case 0x01:
		OUTF("%s Channel Needed: SDCCH\n", str);
		break;
	case 0x02:
		OUTF("%s Channel Needed: TCH/F (Full rate)\n", str);
		break;
	case 0x03:
		OUTF("%s Channel Needed: TCH/H or TCH/F (Dual rate)\n", str);
		break;
	}

}

static void
l2_RRpagingrequest2()
{
	if (data >= end)
		return;
	OUTF("%s\n", PageMode(data[0] & 0x03));

	l2_ChannelNeeded("--xx---- (first)", (data[0] >> 4) & 0x03);
	l2_ChannelNeeded("xx------ (second)", data[0] >> 6);

	data++;
	if (data + 3 >= end)
		RETTRUNK();
	OUTF("........ Mob. Ident 1 (P)TMSI: %02X%02X%02X%02X\n", data[0], data[1], data[2], data[3]);
	data += 4;
	if (data + 3 >= end)
		RETTRUNK();
	OUTF("........ Mob. Ident 2 (P)TMSI: %02X%02X%02X%02X\n", data[0], data[1], data[2], data[3]);
	data += 4;
	if (data >= end)
		RETTRUNK();

	if (data[0] == 0x17)
	{
		data++;
		l2_MobId();
		return;
	}

	OUTF("FIXME, unknown\n");
}

static void
l2_RRpagingrequest3()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s\n", PageMode(data[0] & 0x03));
	l2_ChannelNeeded("--xx---- (first)", (data[0] >> 4) & 0x03);
	l2_ChannelNeeded("xx------ (second)", data[0] >> 6);
	data++;

	int c = 0;
	while (c++ < 4)
	{
		if (data + 3 >= end)
			RETTRUNK();
		OUTF("........ Mob. Ident %u (P)TMSI: %02X%02X%02X%02X\n", c, data[0], data[1], data[2], data[3]);
		data += 4;
	}
}

static void
l2_MobId()
{
	const unsigned char *thisend = end;
	unsigned char len = data[0];
	char odd = 0;
	int bcd = 0;

	data++;
	if (data >= end)
		return;

	if ((data[0] >> 3) & 1)
		odd = 1;

	switch (data[0] & 0x07)
	{
	case 0:
		OUTF("-----000 Type of identity: No Identity\n");
		break;
	case 1:
		OUTF("-----001 Type of identity: IMSI\n");
		bcd = 1;
		break;
	case 2:
		OUTF("-----010 Type of identity: IMEI\n");
		bcd = 1;
		break;
	case 3:
		OUTF("-----011 Type of identity: IMEISV\n");
		bcd = 1;
		break;
	case 4:
		OUTF("-----100 Type of identity: TMSI/P-TMSI\n");
		break;
	default:
		OUTF("-----000 Type of identity: UNKNOWN\n");
		return;
	}
	if (len <= 0)
		return;

	/* Nokia Netmonitor never outputs the first value */
	//OUTF("%x", data[0] >> 4);
	unsigned char c;
	c = data[0] >> 4;
	len--;
	data++;
	if (len <= 0)
		return;

	OUTF("-------- ID(%d/%s): ", len, odd?"odd":"even");

	if (data + len < thisend)
		thisend = data + len;
	if (bcd)
	{
		OUT("%X", c);
		while (data < thisend)
		{
			if ((data + 1 == thisend) && (!odd))
				OUT("%X", data[0] & 0x0f);
			else
				OUT("%X%X", data[0] & 0x0f, (data[0] >> 4) & 0x0f);
			data++;
		}
	} else {
		while (data < thisend)
		{
			if ((data + 1 == thisend) && (odd))
				OUT("%X", (data[0] >> 4 ) & 0x0f);
			else
				OUT("%X%X", (data[0] >> 4) & 0x0f, data[0] & 0x0f);
			data++;
		}
	}
	OUT("\n");
}


static void CellSelectionParameters()
{
	if (data >= end)
		RETTRUNK();

	switch (data[0] >> 5)
	{
	case 0:
		OUTF("000----- Cell Reselect Hyst. :  0 db RXLEV\n");
		break;
	case 1:
		OUTF("001----- Cell Reselect Hyst. :  2 db RXLEV\n");
		break;
	case 2:
		OUTF("010----- Cell Reselect Hyst. :  4 db RXLEV\n");
		break;
	case 3:
		OUTF("011----- Cell Reselect Hyst. :  6 db RXLEV\n");
		break;
	case 4:
		OUTF("100----- Cell Reselect Hyst. :  8 db RXLEV\n");
		break;
	case 5:
		OUTF("101----- Cell Reselect Hyst. : 10 db RXLEV\n");
		break;
	case 6:
		OUTF("110----- Cell Reselect Hyst. : 12 db RXLEV\n");
		break;
	case 7:
		OUTF("111----- Cell Reselect Hyst. : 14 db RXLEV\n");
		break;
	}
	OUTF("---xxxxx Max Tx power level: %d\n", data[0] & 0x1f);
	data++;
	if (data >= end)
		RETTRUNK();

	if (data[0] >> 7)
		OUTF("1------- Additional cells in SysInfo 16,17\n");
	else
		OUTF("0------- No additional cells in SysInfo 7-8\n");
	if ((data[0] >> 6) & 1)
		OUTF("-1------ New establishm cause: supported\n");
	else
		OUTF("-0------ New establishm cause: not supported\n");
	OUTF("--xxxxxx RXLEV Access Min permitted = -110 + %ddB\n", data[0] & 0x3f);
	data++;
}

static void
l2_RRimmAssTBFDirEncHoChaC()
{
	unsigned char maio = (data[0] & 0x0f) << 4;

	data++;
	if (data >= end)
		RETTRUNK();
	maio |= (data[0] >> 6);
	OUTF("xxxxxxxx MAIO: %u\n", maio);
	OUTF("%s HSN: %u\n", BitRowFill(data[0], 0x3f), data[0] & 0x3f);
	data++;

	RequestReference();

	TimingAdvance();
	l2_MobileAllocation();

	if (data >= end)
		RETTRUNK();
	if (data[0] == 0x7c)
	{
		StartingTime();
		return;
	}
	OUTF("FIXME: implement me\n");
}

static void
RequestReference()
{
	if (data >= end)
		RETTRUNK();

	/* Request Reference */
	if ((data[0] >> 5) == 0)
		OUTF("000----- Establishing Cause : All other cases\n");
	else if ((data[0] >> 5) == 0x01)
		OUTF("001----- Establishing Cause : All other cases\n");
	else if ((data[0] >> 5) == 0x02)
		OUTF("010----- Establishing Cause : All other cases\n");
	else if ((data[0] >> 5) == 0x03)
		OUTF("011----- Establishing Cause : All other cases\n");
	else if ((data[0] >> 5) == 0x04)
		OUTF("100----- Establishing Cause: Answer to paging\n");
	else if ((data[0] >> 5) == 0x05)
		OUTF("101----- Establishing Cause: Emergency call\n");
	else if ((data[0] >> 5) == 0x07)
		OUTF("111----- Establishing Cause: Other services req. by user\n");
/* Random refernce must be 5 bit long ?! */
//	else if ((data[0] >> 4) == 0x05)
//		OUTF("0101---- Establishing Cause: Originating data call from dual rate mobile station\n");
//	else if ((data[0] >> 4) == 0x02)
//		OUTF("0010---- Establishing Cause: Answer to paging\n");
	else 
		OUTF("%s Establishing Cause: UNKNOWN [FIXME}\n", BitRowFill(data[0], 0xe0));

	OUTF("---xxxxx Random Reference : %d\n", data[0] & 0x1f);

	data++;
	if (data + 1>= end)
		RETTRUNK();

	OUTF("xxxxxxxx T1/T2/T3\n");
	data++;
	OUTF("xxxxxxxx T1/T2/T3\n");
	data++;
	/* END Request Reference */
}

static void
TimingAdvance()
{
	if (data >= end)
		RETTRUNK();
	OUTF("--xxxxxx Timing advance value: %d\n", data[0] & 0x3f);
	data++;
}

static void
StartingTime()
{
	if (data >= end)
		RETTRUNK();
	OUTF("01111100 Starting Time block\n");
	data++;
	if (data >= end)
		RETTRUNK();
	unsigned char t3;
	OUTF("%s T1 Frame Number: %u\n", BitRowFill(data[0], 0xf8), data[0] >> 3);
	t3 = (data[0] & 0x07) << 5;
	data++;
	if (data >= end)
		RETTRUNK();
	t3 |= (data[0] >> 5);
	OUTF("%s T2 Frame Number: %u\n", BitRowFill(data[0], 0x1f), data[0] & 0x1f);
	OUTF("........ T3 Frame Number: %u\n", t3);
}

/*
 * RRsystemInfo5
 */
static void
l2_NeighbourCellDescription()
{
	if (data >= end)
		RETTRUNK();
	l2_BcchAllocation();
}


static void
l2_ChannelRelease()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s\n", id_list_get(list_ChannelRelease, data[0]));
	data++;
}

static void
l2_MMcmServiceRequest()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s Ciphering key sequence: %u\n", BitRowFill(data[0], 0x70), (data[0] >> 4) & 0x07);
	OUTF("%s\n", id_list_get(list_RequestServiceType, data[0] & 0x0f));
	data++;
	MSClassMarkTwo();
	if (data >= end)
		RETTRUNK();

	if (data[0] == 0x20)
	{
		OUTF("FIXME: classmark3\n");
		return;
	}

	l2_MobId();
}


static void
MSClassMarkOne()
{
	if (data >= end)
		RETTRUNK();

	if (((data[0] >> 5) & 0x03) == 0)
		OUTF("-00----- Revision Level: Phase 1\n");
	else if (((data[0] >> 5) & 0x03) == 1)
		OUTF("-01----- Revision Level: Phase 2\n");
	else
		OUTF("-xx----- Revision Level: Unknown\n");
	if (((data[0] >> 4) & 1) == 0)
		OUTF("---0---- Controlled early classmark sending: Not implemented\n");
	else
		OUTF("---1---- Controlled early classmark sending: Implemented\n");
	if ((data[0] >> 3) & 1)
		OUTF("----1--- A5/1 not available\n");
	else
		OUTF("----0--- A5/1 available\n");
	
	OUTF("%s RF power class capability: Class %u\n", BitRowFill(data[0], 0x07), (data[0] & 0x07) + 1);

	data++;
}

static void
MSClassMarkTwo()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s MS Classmark 2 length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
	MSClassMarkOne();

	if ((data[0] >> 6) & 1)
		OUTF("-1------ Pseudo Sync Capability: present\n");
	else
		OUTF("-1------ Pseudo Sync Capability: not present\n");

	if (((data[0] >> 4) & 0x03) == 0)
		OUTF("--00---- SS Screening: Phase 1 default value\n");
	else if (((data[0] >> 4) & 0x03) == 1)
		OUTF("--01---- SS Screening: Phase 2 error handling\n");
	else
		OUTF("--xx---- SS Screening: UNKNOWN\n");

	if ((data[0] >> 3) & 1)
		OUTF("----1--- Mobile Terminated Point to Point SMS: supported\n");
	else
		OUTF("----0--- Mobile Terminated Point to Point SMS: not supported\n");

	if ((data[0] >> 2) & 1)
		OUTF("-----1-- VoiceBroadcastService: supported\n");
	else
		OUTF("-----0-- VoiceBroadcastService: not supported\n");

	if ((data[0] >> 1) & 1)
		OUTF("------1- VoiceGroupCallService: supported\n");
	else
		OUTF("------0- VoiceGroupCallService: not supported\n");

	if (data[0] & 1)
		OUTF("-------1 MS supports E-GSM or R-GSM: supported\n");
	else
		OUTF("-------0 MS supports E-GSM or R-GSM: not supported\n");

	data++;
	if (data >= end)
		RETTRUNK();


	if ((data[0] >> 7) & 1)
		OUTF("1------- CM3 option: supported\n");
	else
		OUTF("0------- CM3 option: not supported\n");

	if ((data[0] >> 5) & 1)
		OUTF("--1----- LocationServiceValueAdded Capability: supported\n");
	else
		OUTF("--0----- LocationServiceValueAdded Capability: not supported\n");

	if ((data[0] >> 3) & 1)
		OUTF("----1--- SoLSA Capability: supported\n");
	else
		OUTF("----0--- SoLSA Capability: not supported\n");

	if ((data[0] >> 1) & 1)
		OUTF("------1- A5/3 available\n");
	else
		OUTF("------0- A5/3 not available\n");
	
	if (data[0] & 1)
		OUTF("-------1 A5/2: available\n");
	else
		OUTF("-------0 A5/2: not available\n");
	data++;
}

static void
l2_RRciphModCmd()
{
	if (data >= end)
		RETTRUNK();
	if (((data[0] >> 1) & 0x07) == 0x07)
		OUTF("----111- Cipher: reserved [UNKNOWN]\n");
	else
		OUTF("%s Cipher: A5/%u\n", BitRowFill(data[0], 0x0e), ((data[0] >> 1) & 0x07) + 1);


	if (data[0] & 1)
		OUTF("-------1 Start ciphering\n");
	else
		OUTF("-------0 No ciphering\n");

	if ((data[0] >> 4) & 1)
		OUTF("---1---- Cipher Response: IMEISV shall be included\n");
	else
		OUTF("---0---- Cipher Response: IMEISV shall not be included\n");
	data++;
}

static void
l2_RRciphModCompl()
{
	if (data >= end)
		RETTRUNK();

	if (data[0] != 0x17)
		return;
	if (++data >= end)
		RETTRUNK();
	l2_MobId();
}

static void
l2_TmsiReallocCommand()
{
	if (data >= end)
		RETTRUNK();

	l2_MccMncLac();
	l2_MobId();
}

static void
l2_sms()
{
	if (data >= end)
		RETTRUNK();

	if ((data[0] == 0x04))
		OUTF("00000100 Type: CP-ACK\n");
	else if (data[0] == 0x10)
		OUTF("00010000 Type: CP-ERROR\n");
	else if (data[0] == 1) {
		OUTF("00000001 Type: CP-DATA\n");
		data++;
		cpData();
	} else
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xff));
}

static void
cpDataUp()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Parameter %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Parameter %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Parameter %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
	Address("SMSC");
	if (data >= end)
		RETTRUNK();

	/* FIXME: Be more detailed here about message flags */
	OUTF("%s Message Flags: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data + 1 >= end)
		RETTRUNK();
	int num = data[0] << 8;
	OUTF("%s Reference Number [continue]\n", BitRowFill(data[0], 0xff));
	data++;
	num |= data[0];
	OUTF("%s Reference Number: %u\n", BitRowFill(data[0], 0xff), num);
	data++;

	/* Destination address */
	Address("Destination");
	SmsProtocolDataValidity();
	if (data >= end)
		RETTRUNK();
}

/* From Network to MS */
/* Called when a full 140 byte SMS is received */
static void
cpData()
{
	int n_symbols;
	if (data >= end)
		RETTRUNK();
	OUTF("%s Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
	if (data >= end)
		RETTRUNK();
	if ((data[0] & 1) == 0)
	{
		//OUTF("xxxxxxx0 cpDataUp FIXME\n");
		//data++;
		cpDataUp();
		return;
	}
	OUTF("%s reserved\n", BitRowFill(data[0], 0xf8));
	OUTF("%s Message Type Indicator(MTI): %s\n", BitRowFill(data[0], 0x07), id_list_get(list_MTI, data[0] & 0x07));
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Message Reference: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		return; /* Can happen that msg terminated here... */

	/* RP-address */
	Address("SMSC");
	if (data >= end)
		RETTRUNK();

#if 0
	OUTF("%s TP-MTI, TP-MMS, TP-SRI, TP-UDIH, TP-RP: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Reference number: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if ((data[0]) == 0x44)
	{
		OUTF("FIXME: ems_type\n");
		return;
	}
	if (++data >= end)
		RETTRUNK();

	OUTF("%s Parameter\n", BitRowFill(data[0], 0xff));
	data++;
#endif

	TPAddress("Destination");
	OUTF("%s TP-MTI: %s\n", BitRowFill(data[0], 0x03), id_list_get(list_TP_MTI, data[0] & 0x03));

	/* SMS-DELIVER and SMS-STATUS-REPORT.
	 * This may be different for other SMS-types! (FIXME) */
	if ((data[0] >> 2) & 1)
		OUTF("-----1-- More Messages (TP-MMS): No\n");
	else
		OUTF("-----0-- More Messages (TP-MMS): Yes\n");
	OUTF("%s Status Report Indication (TP-SRI)\n", BitRowFill(data[0], 0x20));
	if ((data[0] >> 6) & 1)
	{
		OUTF("-1------ User Data Header Indicator (TP-UDHI): Yes\n");
		nfo.flags |= GSMSP_NFO_UDHI;
	} else {
		OUTF("-0------ User Data Header Indicator (TP-UDHI): No\n");
	}
	OUTF("%s Reply Path (TP-RP)\n", BitRowFill(data[0], 0x80));
	/* SMS-DELIVER and SMS-STATUS-REPORT end */
	data++;

	/* FIXME: Why is TPAddress differently encoded as SMSC address?
	 * Am i doing something wrong or are the GSM people nuts?
	 */
	TPAddress("Originating (TP-OA)");

	SmsProtocolDataValidity();

	SmscTimestamp();
	if (data >= end)
		RETTRUNK();
	n_symbols = data[0];
	OUTF("%s User Data Length (TP-UDL): %u symbols\n", BitRowFill(data[0], 0xff), data[0]);
 
	data++;
#if 0
	if ((data[0] >> 6) & 1)
	{
		OUTF("-1------ TP User Data Header Indicator (TP-UDHI): yes\n");
		data++;
		OUTF("-------- FIXME: Decode header\n");
		data += (data[0] + 1);
		/* FIXME: Skip over fill bits as well. GSM 03.40 9.2.3.24 */
	} else {
		OUTF("-0------ TP User Data Header Indicator (TP-UDHI): no\n");
		//data++; /* Contains directly the data!!! */
	}
#endif
	
	if (sms_con.ptr == NULL)
		sms_con.ptr = sms_con.buf;

	if (nfo.flags & GSMSP_NFO_UDHI)
		sms_udh();
	if ((nfo.flags & GSMSP_NFO_SMSCON) && (!(nfo.flags & GSMSP_NFO_LASTSMSCHUNK)))
	{
		HEXDUMPF(data, end - data, "TP-UD\n");
		/* If this is a concatendated SMS then wait for rest of data */
		return;
	}
	if (sms_con.ptr > sms_con.buf)
	{
		/* was a concatenated sms. output concatendated TP-UD */
		data = sms_con.buf;
		start = data;
		end = sms_con.ptr;
	}

	if (nfo.flags & GSMSP_NFO_DEFAULTALPHABET)
	{
		if (((end - data) * 8 ) / 7 < n_symbols)
			n_symbols = ((end - data) * 8) / 7;
		sms_default_alphabet(n_symbols);
	} else if (nfo.flags & GSMSP_NFO_SIMDATADL) {
		HEXDUMPF(data, end - data, "Format Sim Data Download\n");
		simdatadownload();
	} else {
		HEXDUMPF(data, end - data, "TP-UD\n");
	}
	/* Reset concatendating sms counter again */
	if ((sms_con.ptr > sms_con.buf) && (nfo.flags & GSMSP_NFO_LASTSMSCHUNK))
	{
		memset(&sms_con, 0, sizeof sms_con);
	}
}

static unsigned char sms_default_alpha[] = ""
"@_$__________\r__"
"________________"
" !\"#_%&`()*+,-./"
"0123456789:;<=>?"
"_ABCDEFGHIJKLMNO"
"PQRSTUVWXYZ_____"
"_abcdefghijklmno"
"pqrstuvwxyz_____";

/*
 * Len is the number of symbols (not number of bytes)
 */
static void
sms_default_alphabet(int len)
{
	unsigned char buf[512];
	unsigned char *ptr = buf;
	unsigned char *buf_end = buf + sizeof buf;

	while (ptr < buf_end)
	{
		*ptr++ = sms_default_alpha[data[0] & 0x7f];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[0] >> 7) & 0x01) | ((data[1] & 0x3f) << 1)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[1] >> 6) & 0x03) | ((data[2] & 0x1f) << 2)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[2] >> 5) & 0x07) | ((data[3] & 0x0f) << 3)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[3] >> 4) & 0x0f) | ((data[4] & 0x07) << 4)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[4] >> 3) & 0x1f) | ((data[5] & 0x03) << 5)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[5] >> 2) & 0x3f) | ((data[6] & 0x01) << 6)];
		if (--len <= 0) break;
		*ptr++ = sms_default_alpha[((data[6] >> 1) & 0x7f)];
		if (--len <= 0) break;
		data += 7;
	}
	data = end;
	*ptr = '\0';
	OUTF("-------- Content: %s\n", buf);
}

/* User Data Header (GSM 03.40:9.2.3.24) */
static void
sms_udh()
{
	int len_udh;
	int len;
	int seq_total;
	const unsigned char *end_udh;
	const unsigned char *next;

	if (data >= end)
		RETTRUNK();
	len_udh = data[0];
	end_udh = data + data[0] + 1;
	OUTF("%s User Data Header Length: %u octets\n", BitRowFill(data[0], 0xff), data[0]);
	data++;

	if (end_udh > end)
		end_udh = end;

	while (data < end_udh)
	{
		if ((data + 1 >= end_udh) || (data + data[1] + 2 > end_udh))
		{
			OUTF("ERR: Short data\n");
			break;
		}
		next = data + data[1] + 2;
		len = data[1];

		if (data[0] == 0) {
			OUTF("00000000 Concatenated short messages, 8-bit reference number\n");
			data++;
			OUTF("--------  Length: %u\n", data[0]);
			data++;
			OUTF("--------  Message Reference Number: %.2X\n", data[0]);
			data++;
			OUTF("--------  Number of Segments: %.2X\n", data[0]);
			seq_total = data[0];
			data++;
			OUTF("--------  Seq Number: %.2X\n", data[0]);
			//OUTF("DEBUG: %u += memcpy(%p, %p, %u)\n", sms_con.ptr - sms_con.buf, sms_con.ptr, end_udh, end-end_udh);
			memcpy(sms_con.ptr, end_udh, end - end_udh);
			sms_con.ptr += (end - end_udh);

			nfo.flags |= GSMSP_NFO_SMSCON;
			if (data[0] == seq_total)
				nfo.flags |= GSMSP_NFO_LASTSMSCHUNK;
			data++;
		} else if (data[0] == 1)
			OUTF("00000001 Special SMS Message Indication\n");
		else if (data[0] == 4) {
			OUTF("00000100 Application port address, 8 bit\n");
			OUTF("--------  Desitnation Port: %u (0x%.2X)\n", data[1], data[1]);
			OUTF("--------  Source Port: %u (0x%.2X)\n", data[2], data[2]);
			data += 3;
		} else if (data[0] == 5) {
			unsigned short dport, sport;
			dport = (data[2] << 8) + data[3];
			sport = (data[4] << 8) + data[5];
			OUTF("00000101 Application port address, 16 bit\n");
			data++;
			OUTF("--------  Length: %u\n", data[0]);
			data++;
			/* 0 - 15999 allocated by IANA
			 * 16000 - 16999 Available for allocation by application
			 * 17000 - 65535 reserved
			 */
			OUTF("--------  Destination Port: %u\n", dport);
			data += 2;
			OUTF("--------  Source Port: %u\n", sport);
			data += 2;
		} else if (data[0] == 6)
			OUTF("00000110 SMSC Control Parameters\n");
		else if (data[0] == 7)
			OUTF("00000111 UDH Source Indicator\n");
		else if (data[0] == 8) {
			OUTF("00001000 Concatenated short messages, 16-bit reference number\n");
			data++;
			OUTF("--------  Message Reference Number: %.4X\n", (data[0]<<8) + data[1]);
			data += 2;
			OUTF("--------  Number of Segments: %.2X\n", data[0]);
			seq_total = data[0];
			data++;
			OUTF("--------  Seq Number: %.2X\n", data[0]);
			memcpy(sms_con.ptr, end_udh, end - end_udh);
			sms_con.ptr += (end - end_udh);

			nfo.flags |= GSMSP_NFO_SMSCON;
			if (data[0] == seq_total)
				nfo.flags |= GSMSP_NFO_LASTSMSCHUNK;
			data++;
		} else if (data[0] == 9)
			OUTF("00001001 Wireless Control Message Protocol\n");
		else if ((data[0] >= 0x70) && (data[0] <= 0x7f))
			OUTF("%s SIM Toolkit Security Header\n", BitRowFill(data[0], 0xff));
		else if ((data[0] >= 0x80) && (data[0] <= 0x9f))
			OUTF("%s SME to SME specific use\n", BitRowFill(data[0], 0xff));
		else if ((data[0] >= 0xc0) && (data[0] <= 0xdf))
			OUTF("%s SC specific use\n", BitRowFill(data[0], 0xff));
		else
			OUTF("%s reserved\n", BitRowFill(data[0], 0xff));

		data = next;
	}

}

static void
TPAddress(const char *str)
{
	int len;

	if (data >= end)
		RETTRUNK();

	len = data[0];
	OUTF("%s %s Address Length: %u\n", BitRowFill(data[0], 0xff), str, data[0]);
	data++;
	if (len <= 0)
	{
		data++;
		return;
	}
	if (data >= end)
		RETTRUNK();
	if (data[0] >> 7)
		OUTF("1------- Extension\n");

	OUTF("%s\n", id_list_get(list_SMSCAddressType, (data[0] >> 4) & 0x07));
	OUTF("%s\n", id_list_get(list_SMSCAddressNumberingPlan, data[0] & 0x0f));
	data++;

	OUTF("-------- Number(%u): ", len);
	while (len > 0)
	{
		if ((data[0] >> 4) == 0x0f)
			OUT("%X", data[0] & 0x0f);
		else
			OUT("%X%X", data[0] & 0x0f, data[0] >> 4);
		len -= 2;
		data++;
	}
	OUT("\n");
}

static void
Address(const char *str)
{
	int len;

	if (data >= end)
		RETTRUNK();
	len = data[0];
	OUTF("%s %s Address Length: %u\n", BitRowFill(data[0], 0xff), str, data[0]);
	data++;
	if (len <= 0)
	{
		data++;
		return;
	}
	if (data >= end)
		RETTRUNK();
	if (data[0] >> 7)
		OUTF("1------- Extension\n");

	OUTF("%s\n", id_list_get(list_SMSCAddressType, (data[0] >> 4) & 0x07));
	OUTF("%s\n", id_list_get(list_SMSCAddressNumberingPlan, data[0] & 0x0f));
	len--;
	data++;
	if (len <= 0)
		return;
	const unsigned char *thisend = data + len;
	if (thisend > end)
		thisend = end;

	OUTF("-------- Number(%d): ", len);
	while (data < thisend)
	{
		if ((data[0] >> 4) == 0x0f)
			OUT("%X", data[0] & 0x0f);
		else
			OUT("%X%X", data[0] & 0x0f, data[0] >> 4);
		data++;
	}
	OUT("\n");
}


static void
l2_RRpagingresponse()
{
	if (data >= end)
		RETTRUNK();

	if ((data[0] & 0x07) == 0x07)
		OUTF("-----111 Cipher key sequence: Key not available!\n");
	else
		OUTF("%s Ciphering key sequence: %u\n", BitRowFill(data[0], 0x07), data[0] & 0x07);

	OUTF("%s Ciphering key sequence: %u\n", BitRowFill(data[0], 0x70), (data[0] >> 4) & 0x07);

	data++;
	MSClassMarkTwo();
	if (data >= end)
		RETTRUNK();
	l2_MobId();
}

static void
l2_RRassignCommand()
{
	ChannelDescriptionTwo();
	if (data >= end)
		RETTRUNK();
	
	OUTF("%s Training seq. code: %d\n", BitRowFill(data[0], 0xe0), data[0] >> 5);
	if (((data[0] >> 2) & 0x07) == 0x00)
		l2_SingleChannelAssCom();
	else if (((data[0] >> 4) & 1) == 0x01)
		l2_HoppingChannelAssCom();
	else
		OUTF("xxx0??xxx UNKNOWN %d\n", (data[0] >> 3) & 0x3);
}

static void
l2_RRassignComplete()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s\n", id_list_get(list_ChannelRelease, data[0]));
	data++;
}

static void
ChannelDescriptionTwo()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Timeslot number: %d\n", BitRowFill(data[0], 0x07), data[0] & 0x07);
	OUTF("%s Channel Description: %s\n", BitRowFill(data[0], 0xf8), id_list_get(list_ChannelDescriptionTwo, data[0] >> 3));

	data++;
}

static void
l2_SingleChannelAssCom()
{
	int freq = (data[0] & 0x03) << 8;

	data++;
	if (data >= end)
		RETTRUNK();
	freq |= data[0];
	OUTF("........ Absolute RF channel number: %u\n", freq);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Power Level: %u\n", BitRowFill(data[0], 0x1f), data[0] & 0x1f);
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 0x63)
		return;
	if (++data >= end)
		RETTRUNK();
	OUTF("%s\n", id_list_get(list_ChannelMode, data[0]));
}

static void
l2_HoppingChannelAssCom()
{
	OUTF("FIXME %s\n", __func__);
}

static void
CCalerting()
{
	if (data >= end)
		RETTRUNK();
	if (data[0] != 0x1e)
		return;

	data++;
	ProgressIndicator();
}

static void
ProgressIndicator()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s Length of IE Progress Indicator: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s Coding: %s\n", BitRowFill(data[0], 0x60), id_list_get(list_CodingStandard, (data[0] >> 5) & 0x03));
	OUTF("%s Location: %s\n", BitRowFill(data[0], 0x0f), id_list_get(list_Location, data[0] & 0x0f));
	if (++data >= end)
		RETTRUNK();
	OUTF("%s\n", id_list_get(list_Progress, data[0] & 0x7f));
	data++;
}

static void
CCsetup()
{
	if (data >= end)
		RETTRUNK();

	while (data < end)
	{
		if (data[0] == 0x04)
		{
			OUTF("00000100 Bearer Capability\n");
			data++;
			BearerCap();
		} else if (data[0] == 0x1e) {
			OUTF("00011110 Progress Indicator\n");
			ProgressIndicator();
			return;
		} else if (data[0] == 0x5e) {
			OUTF("01011110 Called Party BCD Number\n");
			data++;
			BCDNumber();
		} else {
			OUTF("%s FIXME\n", BitRowFill(data[0], 0xff));
			break;
		}
	}

	data++;
}

static void
Cause()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s Length of Cause: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();

	OUTF("%s Coding: %s\n", BitRowFill(data[0], 0x60), id_list_get(list_CodingStandard, (data[0] >> 5) & 0x03));
	OUTF("%s Location: %s\n", BitRowFill(data[0], 0x0f), id_list_get(list_Location, data[0] & 0x0f));
	if (++data >= end)
		RETTRUNK();

	OUTF("%s Cause: %s\n", BitRowFill(data[0], 0x7f), id_list_get(list_Cause, data[0] & 0x7f));
	data++;
}

static void
l2_RRclassmarkChange()
{
	if (data >= end)
		RETTRUNK();
	MSClassMarkTwo();
	if (data >= end)
		RETTRUNK();
	if (data[0] == 0x20)
	{
		OUTF("00100000 Class Mark 3\n");
		data++;
		ClassMarkThree();
	}
}

static void
MultiSupportTwo()
{
	int c = 0;
	if (data + 1 >= end)
		RETTRUNK();
	OUTF("0110---- P-GSM, E-GSM, R-GSM supported, DSC 1800 not supported\n");

	for (c = 3; c >= 0; c--)
	{
		if ((data[0] >> c) & 0x1)
			OUTF("%s A5/%d available\n", BitRowFill(data[0], 1<<c), 4+c);
		else
			OUTF("%s A5/%d not available\n", BitRowFill(data[0], 1<<c), 4+c);
	}
	data++;
	OUTF("%s Associated Radio capability 1 Power Class: %d\n", BitRowFill(data[0], 0xf), data[0] & 0xf);
	OUTF("%s Associated Radio capability 2 Power Class: %d\n", BitRowFill(data[0], 0xf0), data[0] >> 4);
}

static void
ClassMarkThree()
{
	unsigned char c;

	if (data + 1 >= end)
		RETTRUNK();
	OUTF("%s Length: %d\n", BitRowFill(data[0], 0xff), data[0]);
	data++;

	c = data[0] >> 4;
	if ((c == 0x5) || (c == 0x6))
		MultiSupportTwo();
	else
		OUTF("FIXME\n");
}

static void
SmsProtocolDataValidity()
{
	unsigned char b7,b6,b5,b4;

	if (data >= end)
		RETTRUNK();
	OUTF("%s Protocol Identifier: 0x%.2X\n", BitRowFill(data[0], 0xff), data[0]);

	b7 = (data[0] >> 7) & 0x1;
	b6 = (data[0] >> 6) & 0x1;
	b5 = (data[0] >> 5) & 0x1;
	b4 = (data[0] >> 4) & 0x1;

	if ( !b7 && !b6 && b5) {
		OUTF("001----- Telematic Interworking\n");
	} 
	if ( !b7 && b6 ) {
		switch ( (data[0] & 0x3f ) ) {
			case 0x1f:
			OUTF("01011111  Return Call Message\n");
			break;
			case 0x3d:
			OUTF("01111101  ME Data Download\n");
			break;
			case 0x3e:
			OUTF("01111110  ME De-personalization SMS\n");
			break;
			case 0x3f:
			OUTF("01111111  SIM Data download\n");
			break;
			default:break;
		}
		
	} 

	if (data[0] == 0x00)
		OUTF("00000000  normal\n");

	if (data[0] == 0x40)
		OUTF("00101000  SMS PING\n");

	if (data[0] == 0x7f) {
		nfo.flags |= GSMSP_NFO_SIMDATADL;
	//	printf("set GSMSP_NFO_SIMDATADL=1\n");
	} else
		nfo.flags &= ~GSMSP_NFO_SIMDATADL;

	data++;
	sms_dcs();
}

static void
BearerCap()
{
	int len;
	char extension = 0;

	if (data >= end)
		RETTRUNK();

	len = data[0];
	OUTF("%s Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (data + len > end)
		len = end - data;
	if (++data >= end)
		RETTRUNK();

	if ((data[0] >> 7) & 0x1)
	{
		extension = 1;
		OUTF("1------- Extension: yes\n");
	} else {
		OUTF("0------- Extension: no\n");
	//	extenstion = 0;
	}

	OUTF("%s Radio Channel: %s\n", BitRowFill(data[0], 0x60), id_list_get(list_RadioChannelReq, (data[0] >> 5) & 0x03));
	if ((data[0] >> 4) & 1)
		OUTF("---1---- Coding Standard: reserved\n");
	else
		OUTF("---0---- Coding Standard: GSM\n");
	if ((data[0] >> 3) & 1)
		OUTF("----1--- Transfer Mode: Packet\n");
	else
		OUTF("----0--- Transfer Mode: Circuit\n");

	OUTF("%s Transfer Capability: %s\n", BitRowFill(data[0], 0x07), id_list_get(list_TransferCap, data[0] & 0x07));
	data++;

	if (extension)
	{
		OUTF("FIXME: Stuff missing here\n");
		/* FIXME: can be followed by antoher estension etc!*/
	}

	if ((data[0] >> 7) & 0x1)
	{
		extension = 1;
		OUTF("1------- Extension: yes\n");
	} else {
		OUTF("0------- Extension: no\n");
	//	extenstion = 0;
	}

	if ((data[0] >> 6) & 0x01)
		OUTF("-1------ Compression: yes\n");
	else
		OUTF("-0------ Compression: no\n");

	OUTF("%s Duplex Mode: %s\n", BitRowFill(data[0], 0x8), id_list_get(list_Duplex, data[0] & 0x8));
	if ((data[0] >> 1) & 0x1)
		OUTF("------1- Rate Request: Data 4.8 kb/s, full rate, n. transp. 6kb req\n");
	data++;

	if ((data[0] >> 7) & 0x1)
	{
		extension = 1;
		OUTF("1------- Extension: yes\n");
	} else {
		OUTF("0------- Extension: no\n");
	//	extenstion = 0;
	}
	OUTF("%s Rate Adaptation: %s\n", BitRowFill(data[0], 0x18), id_list_get(list_Rate, data[0] & 0x18));
	OUTF("%s Signalling Access Protocol: %s\n", BitRowFill(data[0], 0x7), id_list_get(list_Signalling, data[0] & 0x7));	
	data++;
	if ((data[0] & 0x1) == 1)
		OUTF("-------1 Asynchronous\n");
	else
		OUTF("-------0 Synchronous\n");
	data++;
	/* FIXME: some octets might continue here, depending
	 * on extension */
	OUTF("FIXME: some data might be in extentions\n");
}

static void
AuthenticationRequest()
{
	char rand[16 * 2 + 1];

	if (data >= end)
		RETTRUNK();

	OUTF("%s Cipher Key Sequence Number: %u\n", BitRowFill(data[0], 0x07), data[0] & 0x07);
	data++;
	if (data + 16 > end)
		RETTRUNK();

	snprintf(rand, sizeof rand, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 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]);
	OUTF("-------- RAND: %s\n", rand);
	data += 16;
}

static void
LocationUpdateRequest()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Cipher Key Sequence Number: %d\n", BitRowFill(data[0], 0x70), (data[0] >> 4) & 0x7);
	if (data[0] & 0x8)
		OUTF("----1--- Follow-on request pending\n");
	else
		OUTF("----0--- No follow-on request pending\n");
	OUTF("%s Location Update: %s\n", BitRowFill(data[0], 0x3), id_list_get(list_TypeOfLocationUpdate, data[0] & 0x03));
	data++;
	l2_MccMncLac();
	MSClassMarkOne();
	l2_MobId();
}
		

static void
AuthenticationResponse()
{
	char sres[4 * 2 + 1];

	if (data + 4 > end)
		RETTRUNK();
	snprintf(sres, sizeof sres, "%02x%02x%02x%02x", data[0], data[1], data[2], data[3]);
	OUTF("-------- SRES: %s\n", sres);
	data += 4;
}


static void
sms_dcs()
{
	if (data >= end)
		RETTRUNK();

	if (data[0] == 0)
	{
		OUTF("00000000 Default Data Coding Scheme (default Alphabet)\n");
		nfo.flags |= GSMSP_NFO_DEFAULTALPHABET;
		data++;
		return;
	}

	if (((data[0] >> 6) & 0x03) == 0)
	{
		OUTF("00------ General Data Coding\n");
		if ((data[0] >> 5) & 1)
			OUTF("--1----- Compressed (TS 03.42)\n");
		else
			OUTF("--0----- Not compressed\n");
		if ((data[0] >> 4) & 1)
			OUTF("---1---- Message class meaning: yes\n");
		else
			OUTF("---0---- Message class meaning: no\n");


		OUTF("%s Coding: %s\n", BitRowFill(data[0], 0x0c), id_list_get(list_alphabet, (data[0] >> 5) & 0x03));
		/* If bit 4 is set then bit 0..1 have a Message Class meaning */
		if ((data[0] >> 4) & 1)
		{
			OUTF("%s Message Class: %s\n", BitRowFill(data[0], 0x03), id_list_get(list_MessageClassMeaning, data[0] & 0x03));
		} else {

			OUTF("%s Message Class: No meaning (see bit4)\n", BitRowFill(data[0], 0x03));
		}
	} else if ((((data[0] >> 6) & 0x03) == 0x03) && (((data[0] >> 4) & 0x03) != 0x03)) {
		if (((data[0] >> 4) & 0x0f) == 0x0c) {
			OUTF("1100---- Message Waiting Indicator Group: Discard Message\n");
		} else if (((data[0] >> 4) & 0x0f) == 0x0d) {
			OUTF("1101---- Message Waiting Indicator Group: Store Message\n");
		} else if (((data[0] >> 4) & 0x0f) == 0x0e) {
			OUTF("1110---- Message Waiting Indicator Group: Store Message\n");
		}
		OUTF("FIXME GSM 03.38\n");
	} else if (((data[0] >> 4) & 0x0f) == 0x0f) {
		OUTF("1111---- Data Coding/Message Class\n");
		OUTF("%s reserved\n", BitRowFill(data[0], 0x08));
		if ((data[0] >> 2) & 1)
			OUTF("-----1-- Message coding: 8 bit\n");
		else {
			OUTF("-----0-- Message coding: default alphabet\n");
			nfo.flags |= GSMSP_NFO_DEFAULTALPHABET;
		}
		OUTF("%s Message Class: %s\n", BitRowFill(data[0], 0x03), id_list_get(list_MessageClassMeaning, data[0] & 0x03));
	} else {
		OUTF("%s reserved\n", BitRowFill(data[0], 0xff));
	}

	data++;
}

#define FlipBCD(val)	((val & 0x0f) << 4) | (val >> 4)

static void
SmscTimestamp()
{
	if (data + 6 >= end)
		RETTRUNK();

	OUTF("-------- SMSC Timestamp: %02x-%02x-%02x %02x:%02x:%02x (TZ %02x)\n", FlipBCD(data[0]), FlipBCD(data[1]), FlipBCD(data[2]), FlipBCD(data[3]), FlipBCD(data[4]), FlipBCD(data[5]), FlipBCD(data[6]));

	data += 7;
}


static void
simdatadownload()
{
	unsigned char chl; /* command header length */
	int	i;

	OUTF("-------- Length of CPL: 0x%.4X (%u)\n", (data[0]<<8)+data[1],(data[0]<<8)+data[1]);
	data+=2;
	chl = data[0];
	OUTF("-------- Command Header Length: 0x%.2X (%u)\n",chl,chl);
	data++;
	// SPI
	OUTF("%s SPI : 0x%.2X\n",BitRowFill(data[0], 0xff), data[0]);
	switch (data[0] & 0x3) {
		case 0x00:
			OUTF("------00  No RC, CC or DS\n");
			break;
		case 0x01:
			OUTF("------01  Redudancy Check\n");
			break;
		case 0x02:
			OUTF("------10  Cryptographic Checksum\n");
			break;
		case 0x03:
			OUTF("------11  Digital Signature\n");
			break;
		default: break;
	}
	if ( ((data[0]>>2)& 0x01) )
		OUTF("-----1--  Ciphering\n");
	else
		OUTF("-----0--  No Ciphering\n");
	
	switch ( (data[0]>>3) & 0x03 ) {
		case 0x00:
			OUTF("---00---  No counter available\n");
			break;
		case 0x01:
			OUTF("---01---  Counter available; no replay or sequence checking\n");
			break;
		case 0x02:
			OUTF("---10---  Process if counter is higher\n");
			break;
		case 0x03:
			OUTF("---11---  Process if counter is 1 higher\n");
			break;
		default:break;
	}
	data++;
	OUTF("%s  PoR : 0x%.2X\n", BitRowFill(data[0], 0xff), data[0]); //FIXME:implement me
	switch (data[0] & 0x03) {
		case 0x00:
			OUTF("------00   No PoR-reply to sending entity\n");
			break;
		case 0x01:
			OUTF("------01   PoR required to be send to sending entity\n");
			break;
		case 0x02:
			OUTF("------10   PoR required only on error\n");
			break;
		case 0x03:
			OUTF("--------   reserved\n");
			break;
		default:break;
	}
	switch ( (data[0]>>2) & 0x03) {
		case 0x00:
			OUTF("----00--   No RC/CC/DS applied to PoR\n");
			break;
		case 0x01:
			OUTF("----01--   PoR with simple RC\n");
			break;
		case 0x02:
			OUTF("----10--   PoR with CC\n");
			break;
		case 0x03:
			OUTF("----11--   PoR with DS\n");
			break;
		default:break;
	}
	if ( (data[0]>>4) & 0x01 ) 
			OUTF("---1----   PoR via SMS-SUBMIT\n");
		else
			OUTF("---0----   PoR via SMS-Deliver-Report\n");


	data++;

	// KIc
	OUTF("%s  KIc: 0x%.2X\n",BitRowFill(data[0], 0xff), data[0]);

	switch(data[0] & 0x03) {
		case 0x00:
		OUTF("------00   Algorithm known implicitly\n");
		break;
		case 0x01:
		OUTF("------01   DES\n");
		break;
		case 0x02:
		OUTF("------10   Reserved.\n");
		break;
		case 0x03:
		OUTF("------11   properietary Implementation\n");
		break;
		default:break;
	}
	
	switch ( (data[0]>>2) & 0x03) {
		case 0x00:
		OUTF("----00--   DES-CBC\n");
		break;
		case 0x01:
		OUTF("----01--   3DES-outer-CBC 2 different Keys\n");
		break;
		case 0x02:
		OUTF("----10--   3DES-outer-CBC 3 different Keys\n");
		break;
		case 0x03:
		OUTF("----11--   DES-ECB\n");
		break;
		default:break;
	}
	OUTF("XXX-----   Key Number: %.2X\n", data[0]>>4 );
	data++;
	
	// KId
        OUTF("%s  KId: 0x%.2X\n",BitRowFill(data[0], 0xff), data[0]);

        switch(data[0] & 0x03) {
                case 0x00:
                OUTF("------00   Algorithm known implicitly\n");
                break;
                case 0x01:
                OUTF("------01   DES\n");
                break;
                case 0x02:
                OUTF("------10   Reserved.\n");
                break;
                case 0x03:
                OUTF("------11   properietary Implementation\n");
                break;
                default:break;
        }

        switch ( (data[0]>>2) & 0x03) {
                case 0x00:
                OUTF("----00--   DES-CBC\n");
                break;
                case 0x01:
                OUTF("----01--   3DES-outer-CBC 2 different Keys\n");
                break;
                case 0x02:
                OUTF("----10--   3DES-outer-CBC 3 different Keys\n");
                break;
                case 0x03:
                OUTF("----11--   DES-ECB\n");
                break;
                default:break;
        }
        OUTF("XXX-----   Key Number: %.2X\n", data[0]>>5 );
        data++;

	OUTF("--------  Toolkit Application Reference (TAR): 0x%.2X 0x%.2X 0x%.2X\n", data[0],data[1],data[2]);
	data+=3;
	OUTF("--------  Counter (CNTR): 0x%.2X 0x%.2X 0x%.2X 0x%.2X 0x%.2X\n",data[0], data[1], data[2], data[3], data[4]);
	data+=5;

	// print the remaining bytes of the header, may be redudancy check eg
	for (i=0; i<chl-12;i++) {
		OUTF("%s  %.2X\n",BitRowFill(data[0], 0xff), data[0]);
		data++;
	}

}
	

static void
TypeOfIdentity()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s Type of Identity: %s\n", BitRowFill(data[0], 0x07), id_list_get(list_TypeOfIdentity, data[0] & 0x07));
	data++;
}

static void
l2_NonCallSS()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s SendSequenceNumber: %u\n", BitRowFill(data[0], 0xc0), data[0] >> 6);
	if ((data[0] & 0x3f) == 0x3b)
	{
		OUTF("--111011 Facility Register\n");
		data++;
		l2_FacilityRegister();
	} else if ((data[0] & 0x3f) == 0x2a) {
		OUTF("--101010 CCReleaseComplete\n");
		data++;
		l2_CCReleaseComplete();
	} else {
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0x3f));
		data++;
	}
}

static void
l2_FacilityRegister()
{
	if (data >= end)
		RETTRUNK();

	if (data[0] != 0x1c)
	{
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00011100 Information Element: Facility\n");
	data++;
	l2_Facility();
}

static void
l2_Facility()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	switch (data[0])
	{
	case 0xa1: /* 10100001 */
		OUTF("10100001  Invoke\n");
		data++;
		l2_FacilityInvoke();
		break;
	case 0xa2: /* 10100010 */
		OUTF("10100010  ReturnResult\n");
		data++;
		l2_FacilityReturnResult();
		break;
	default:
		OUTF("%s  UNKNOWN\n", BitRowFill(data[0], 0xff));
		break;
	}
}

static void
l2_FacilityHeader()
{
	if (data >= end)
		RETTRUNK();
	OUTF("%s  Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 0x02)
	{
		OUTF("%s  Type: UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00000010  Type: Integer\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s  Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	OUTF("%s  Invoke ID: %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
}

static void
l2_FacilityInvoke()
{
	l2_FacilityHeader();
	if (data >= end)
		RETTRUNK();
	if (data[0] != 0x02)
	{
		OUTF("%s   Type: UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00000010   Type: Integer\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s   Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();

	switch (data[0])
	{
	case 0x3b:
		OUTF("00111011   Unstructured SS Request\n");
		data++;
		l2_UssRequest();
		break;
	case 0x13:
		OUTF("00010011   Unsturcutred SS Data\n");
		data++;
		l2_UssData();
		break;
	default:
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xff));
		break;
	}
}

static void
l2_UssRequest()
{
	if (data >= end)
		RETTRUNK();

	if (data[0] != 0x30)
	{
		OUTF("%s Sequence: UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00110000    Sequence: ussd-Arg\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s    Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 0x04)
	{
		OUTF("%s    Octet String: UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00000100    Octet String\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s    Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();

	OUTF("%s    Coding Sheme Number: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 0x04)
	{
		OUTF("%s     Octet String: UNKNOWN\n", BitRowFill(data[0], 0xff));
		return;
	}
	OUTF("00000100     Octet String\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s     Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
}

static void
l2_UssData()
{
	char buf[32];
	int len;

	if (data >= end)
		RETTRUNK();
	if (data[0] != 0x16)
	{
		OUTF("%s UNKNWON\n", BitRowFill(data[0], 0xff));
		return;
	}
	if (++data >= end)
		RETTRUNK();
	len = data[0];
	OUTF("%s   Length: %d\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	if (data + len > end)
		RETTRUNK();
	snprintf(buf, sizeof buf, "%s", data);
	buf[len] = '\0';
	OUTF("--------   String: %s\n", buf);
	data += len;
}

static void
l2_CCReleaseComplete()
{
	if (data >= end)
		RETTRUNK();
	if (data[0] == 0x08)
	{
		OUTF("00001000 Cause [FIXME]\n");
		data++;
		return;
	} else if (data[0] == 0x1c) { /* 00011100 */
		OUTF("00011100 Facility\n");
		data++;
		l2_Facility();
	} else {
		OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xff));
		data++;
		return;
	}
}

static void
l2_FacilityReturnResult()
{
	l2_FacilityHeader();
	if (data >= end)
		RETTRUNK();
	if (data[0] != 0x30) /* 00110000 */
		goto err;
	OUTF("00110000 Sequence: Result Info\n");
	if (++data >= end)
		RETTRUNK();
	OUTF("%s  Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 2)
		goto err;
	OUTF("00000010   Integer\n");
	if (++data >= end)
		RETTRUNK();
	if (data[0] != 1)
		goto err;
	OUTF("00000001   Length: 1\n");
	if (++data >= end)
		RETTRUNK();
	switch (data[0])
	{
	case 0x13:
		OUTF("00010011   Unsturcutred SS Data\n");
		data++;
		l2_UssData();
		break;
	default:
		goto err;
	}

	return;
err:
	OUTF("%s UNKNOWN\n", BitRowFill(data[0], 0xff));
	data++;
}

static void
MeasurmentReport()
{
	int c, max;

	if (data + 1 >= end)
		RETTRUNK();

	if ((data[0] >> 7) & 0x1)
		OUTF("1------- BA used: yes\n");
	else
		OUTF("0------- BA used: no\n");

	if ((data[0] >> 6) & 0x1)
		OUTF("-1------ Discontinous Transmission: yes\n");
	else
		OUTF("-0------ Discontinous Transmission: no\n");
	OUTF("%s RxLev Full Serving Cell: %d dB\n", BitRowFill(data[0], 0x3f), -110 + (data[0] & 0x3f));
	data++;
	if ((data[0] >> 7) & 0x1)
		OUTF("1------- BA used: yes\n");
	else
		OUTF("0------- BA used: no\n");
	if ((data[0] >> 6) & 0x1)
		OUTF("-1------ MEAS Valid: no\n");
	else
		OUTF("-0------ MEAS Valid: yes\n");
	OUTF("%s RxLev Sub Serving Cell: %d dB\n", BitRowFill(data[0], 0x3f), -110 + (data[0] & 0x3f));
	data++;
	
	OUTF("%s Rx Quality Full Serving Cell: %s\n", BitRowFill(data[0], 0x70), id_list_get(list_RxQual, (data[0] >> 3) & 0x07));
	OUTF("%s Rx Quality Sub Serving Cell: %s\n", BitRowFill(data[0], 0x7), id_list_get(list_RxQual, data[0] & 0x07));
	data++;
	if (data >= end)
		RETTRUNK();
	max = data[0] & 0x7;
	OUTF("%s Number of neighbouring cell measurements: %d\n", BitRowFill(data[0], 0x7), data[0] & 0x7);
	c = 1;
	data++;
	while (c <= max)
	{
		if (data + 2 >= end)
			RETTRUNK();
		OUTF("%s RxLev Neighbour Cell %d: %d dB\n", BitRowFill(data[0], 0x3f), c, -100 + (data[0] & 0x3f));
		data++;
		OUTF("%s Bcch Freq MCell %d: %d\n", BitRowFill(data[0], 0x1f), c, data[0] & 0x1f);
		data++;
		OUTF("%s BTS Identity Code: %d\n", BitRowFill(data[0], 0x3f), data[0] & 0x3f);
		data++;
		c++;
	}
	/* FIXME: I think something iswrong here */
	OUTF("FIXME\n");
}

static void
BCDNumber()
{
	if (data >= end)
		RETTRUNK();

	OUTF("%s Length: %u\n", BitRowFill(data[0], 0xff), data[0]);
	data++;
	OUTF("%s Type of number: %s\n", BitRowFill(data[0], 0x70), id_list_get(list_TypeNumber, data[0] & 0x70));
	//OUTF("%s Number plan: %s\n", BitRowFill(data[0], 0xf), id_list_get(list_data[0] & 0xf);
}

personal git repositories of Harald Welte. Your mileage may vary