aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnetz/telegramm.c
blob: 09b44613deca90f454c60f5a331c8fcf09070144 (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
/* C-Netz telegramm transcoding
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define CHAN cnetz->sender.kanal

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "../common/debug.h"
#include "../common/timer.h"
#include "cnetz.h"
#include "dsp.h"
#include "sysinfo.h"
#include "telegramm.h"

/* debug coding */
//#define DEBUG_RAW       /* debug raw bits */
//#define DEBUG_BLOCK     /* debug interleaved blocks */
//#define DEBUG_CODER	/* debug block coder / decoder */

static const char *param_ja[] = {
	"nein",
	"ja",
};

static const char *param_betriebsart[] = {
	"Sprache klar",
	"Sprache invertiert",
};

static const char *param_gespraechsende[] = {
	"kein bevorstehendes Gespraechsende",
	"bevorstehendes Gespraechsende",
};

static const char *param_frequenz[2048];

static const char *param_anpassen[] = {
	"Sendeleistung erhoehen",
	"Sendeleistung erniedrigen",
};

static const char *param_opcode[64];

static const char *param_power[] = {
	"50-125 mW",
	"0.5-1 W",
	"4-8 W",
	"10-20 W",
};

static const char *param_sonderruf[] = {
	"Verbindungsaufbau gehend",
	"Sonderruf",
};

static const char *param_wskennung[] = {
	"keine Warteschlange",
	"Warteschlange",
	"Warteschlange blockiert",
	"Illegaler Parameter 3",
};

static const char *param_ausloesen[] = {
	"26 dB S/N + Pegel",
	"25 dB S/N + Pegel",
	"24 dB S/N + Pegel",
	"23 dB S/N + Pegel",
	"22 dB S/N + Pegel",
	"21 dB S/N + Pegel",
	"20 dB S/N + Pegel",
	"26 dB S/N",
	"25 dB S/N",
	"24 dB S/N",
	"23 dB S/N",
	"22 dB S/N",
	"21 dB S/N",
	"20 dB S/N",
	"19 dB S/N",
	"18 dB S/N",
};

static const char *param_ausloesegrund[] = {
	"gassenbesetzt (oder Sperre)",
	"teilnehmerbesetzt",
	"funktechnisch",
	"Illegaler Parameter 3",
};

static const char *param_genauigkeit[] = {
	"volle Genauigkeit",
	"bedingte Genauigkeit",
};

static const char *param_grenzwert[] = {
	"Illegaler Parameter 0",
	"> 15 dB S/N",
	"> 17 dB S/N",
	"> 19 dB S/N",
	"> 21 dB S/N",
	"> 25 dB S/N",
	"> 28 dB S/N",
	"> 32 dB S/N",
};

static const char *param_kennung[] = {
	"Test-FuFSt",
	"Normal-FuFSt",
	"Vorzugs-FuFSt",
	"FuFSt hoechster Prioritaet",
};

static const char *param_mittelung[] = {
	"2",
	"4",
	"8",
	"16",
	"32",
	"64",
	"Illegaler Parameter 6",
	"Illegaler Parameter 7",
	"Illegaler Parameter 8",
	"Illegaler Parameter 9",
	"Illegaler Parameter 10",
	"Illegaler Parameter 11",
	"Illegaler Parameter 12",
	"Illegaler Parameter 13",
	"Illegaler Parameter 14",
	"Illegaler Parameter 15",
};

static const char *param_entfernung[] = {
	"1,5 km",
	"2 km",
	"2,5 km",
	"3 km",
	"4 km",
	"5 km",
	"6 km",
	"7 km",
	"8 km",
	"10 km",
	"12 km",
	"14 km",
	"16 km",
	"17 km",
	"23 km",
	"30 km",
};

static const char *param_sperren[] = {
	"Ein- & Umbuchen / Gehende Verbindung",
	"Nur Ein- & Umbuchen",
	"Nur Gehende Verbindung",
	"gesperrt",
};

static const char *param_bewertung[] = {
	"Auswahl nach relativer Entfernungsbewertung",
	"Auswahl nach Pegelkreterium",
};

static const char *param_gueltig[] = {
	"gueltig",
	"ungueltig",
};

static const char *param_verkehrsanteil[] = {
	"Sonderfall",
	"1 Zeitschlitz",
	"2 Zeitschlitze",
	"3 Zeitschlitze",
	"4 Zeitschlitze",
	"5 Zeitschlitze",
	"6 Zeitschlitze",
	"7 Zeitschlitze",
	"8 Zeitschlitze",
	"9 Zeitschlitze",
	"10 Zeitschlitze",
	"11 Zeitschlitze",
	"12 Zeitschlitze",
	"13 Zeitschlitze",
	"14 Zeitschlitze",
	"15 Zeitschlitze",
	"16 Zeitschlitze",
	"17 Zeitschlitze",
	"18 Zeitschlitze",
	"19 Zeitschlitze",
	"10 Zeitschlitze",
	"21 Zeitschlitze",
	"22 Zeitschlitze",
	"23 Zeitschlitze",
	"24 Zeitschlitze",
	"25 Zeitschlitze",
	"26 Zeitschlitze",
	"27 Zeitschlitze",
	"28 Zeitschlitze",
	"29 Zeitschlitze",
	"30 Zeitschlitze",
	"31 Zeitschlitze",
};

static const char *param_signalisierung[] = {
	"Spontansignalisierung",
	"Signalisierung aus Wiederholstellung",
};

static const char *param_chipkarte[] = {
	"Magnetkarte",
	"Chipkarte",
};

static const char *param_auth[] = {
	"Authentifikation nicht durchfuehrbar",
	"Authentifikation durchfuehrbar",
};

static const char *param_reduzierung[] = {
	"4",
	"3",
	"2",
	"1",
};

static struct definition_parameter {
	char		digit;
	const char	*param_name;
	char		bits;
	const char	**value_names; /* points to a list of parameter names, NULL for integer */
} definition_parameter[] = {
	{ 'A',"FuZ-FuVSt-Nr.",				 5, NULL },
	{ 'B',"Betriebs-Art",				 2, param_betriebsart },
	{ 'C',"Ankuendigung Gespraechsende",		 1, param_gespraechsende },
	{ 'D',"Teilnehmergruppensperre",		 4, NULL },
	{ 'E',"Anzahl der gesperrten Teilnehmergruppen", 4, NULL },
	{ 'F',"FuZ-Rest-Nr.",				 8, NULL },
	{ 'G',"Gebuehren-Stand",	 		12, NULL },
	{ 'H',"OgK-Vorschlag",				10, param_frequenz },
	{ 'I',"FuZ-Nationalitaet",			 3, NULL },
	{ 'J',"Sendeleistungsanpassung",		 1, param_anpassen },
	{ 'K',"Frequenz-Nr.",				11, param_frequenz },
	{ 'L',"Art der Signalisierung im OgK",		 1, param_signalisierung },
	{ 'M',"OgK-Verkehrsanteil",			 5, param_verkehrsanteil },
	{ 'N',"FuTln-Nationalitaet",			 3, NULL },
	{ 'O',"OP-Code der Signalisierung",		 6, param_opcode },
	{ 'P',"Max. Sendeleistung",			 2, param_power },
	{ 'Q',"Kartenkennung",	 			 3, NULL },
	{ 'R',"Durchfuehrung der Ueberlastbehandlung",	 1, param_ja },
	{ 'S',"Sonderruf",				 1, param_sonderruf },
	{ 'T',"FuTln-Rest-Nr.",				16, NULL },
	{ 'U',"FuTln-Heimmat FuVSt-Nr.",		 5, NULL },
	{ 'V',"Sicherungs-Code",			16, NULL },
	{ 'W',"WS-Kennung",				 2, param_wskennung },
	{ 'X',"Wahlziffer beliebig 16 Ziffer",		64, NULL },
	{ 'Z',"Zeitschlitz-Nr.",			 5, NULL },
	{ 'a',"Grenzert fuer Ausloesen",		 4, param_ausloesen },
	{ 'b',"Chipkarten-FuTelG-Bit",			 1, param_chipkarte },
	{ 'c',"Ausloesegrund",				 2, param_ausloesegrund },
	{ 'd',"Bedingte Genauigkeit der FuFSt",		 1, param_genauigkeit },
	{ 'e',"Entfernung",				 8, param_entfernung },
	{ 'f',"Grenzwert fuer Einbuchen und Umbuchen",	 3, param_grenzwert },
	{ 'g',"Nachbarschafts-Prioritaets-Bit",		 1, NULL },
	{ 'h',"Herstellerkennung",			 5, NULL },
	{ 'i',"Hardwarestand des FuTelG",		 5, NULL },
	{ 'j',"Softwarestand des FuTelG",		 5, NULL },
	{ 'k',"Kennung FuFSt",				 2, param_kennung },
	{ 'l',"Authentifikationsbit",			 1, param_auth },
	{ 'm',"Mittelungs-Faktor fuer Ausloesen",	 4, param_mittelung },
	{ 'n',"Mittelungs-Faktor fuer Umschalten",	 4, param_mittelung },
	{ 'o',"Zufallszahl"			,	64, NULL },
	{ 'p',"Bewertung nach Pegel und Entfernung",	 1, param_bewertung },
	{ 'q',"Autorisierungsparameter",		64, NULL },
	{ 'r',"Entfernungsangabe der FuFSt",		 4, param_entfernung },
	{ 's',"Gueltigkeit des Gebuehrenstandes",	 1, param_gueltig },
	{ 't',"Test-Telefonteilnehmer-Geraet",		 1, param_ja },
	{ 'u',"Grenzwert fuer Umschalten",		 4, param_ausloesen },
	{ 'v',"Vermittlungtechnische Sperren",		 2, param_sperren },
	{ 'w',"Erweitertes Frequenzbandbit",		 1, NULL },
	{ 'y',"Reduzierungsfaktor",			 2, param_reduzierung },
	{ '_',"Illegaler Opcode",			64, NULL },
	{ 0  ,"",					 0, NULL },
};

static struct definition_parameter *get_parameter(char digit)
{
	struct definition_parameter *parameter = definition_parameter;

	for (parameter = definition_parameter; parameter->digit; parameter++) {
		if (parameter->digit == digit)
			return parameter;
	}

	return NULL;
}

static struct definition_opcode {
	const char *no_auth_bits, *auth_bits;
	const char *message_name;
	int block;
	const char *message_text;
} definition_opcode[64] = {
      /*   8888888877777777666666665555555544444444333333332222222211111111         message	block	text */
	{ "-bRLw---VVVVVVVVVVVVVVVVIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",
	  "-bRLwQQQ-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",       "EM(R)",	BLOCK_R,"Erstmeldung" },
	{ "-bRLw---VVVVVVVVVVVVVVVVIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",
	  "-bRLwQQQ-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",       "UM(R)",	BLOCK_R,"Umbuchantrag" },
	{ "SbRLw---VVVVVVVVVVVVVVVVIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",
	  "SbRLwQQQ-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",       "UWG(R)",	BLOCK_R,"Umbuchantrag bei Warteschlange (gehende Verbindung)" },
	{ "--RL-----hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "ATO(R)",	BLOCK_R,"Ausloesen des FuTelG im OgK-Betrieb bei WS" },
	{ "--RL--WW-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VWG(R)",	BLOCK_R,"Verbindungswunsch gehend" },
	{ "--RL-----hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "SRG(R)",	BLOCK_R,"Sonderruf (Notruf)" },
	{ "SbRLw---VVVVVVVVVVVVVVVVIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",
	  "SbRLwQQQ-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",       "NUG(R)",	BLOCK_R,"Verbindungswunsch gehend bei Nachbarschaftsunterstuetzung" },
	{ "-bRLw---VVVVVVVVVVVVVVVVIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",
	  "-bRLwQQQ-hhhhhiiiiijjjjjIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT",       "UWK(R)",	BLOCK_R,"Umbuchantrag bei Warteschlange (kommende Verbindung)" },
	{ "------------------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "MFT(M)",	BLOCK_M,"Meldung: Funktelefonteilnehmer" },
	{ "________________________________________________________________", NULL, "opcode 9",	BLOCK_I,"Illegaler Opcode" },
	{ "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", NULL, "WUE(M)",	BLOCK_M,"Wahluebertragung" },
	{ "________________________________________________________________", NULL, "opcode 11",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 12",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 13",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 14",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 15",BLOCK_I,"Illegaler Opcode" },
	{ "------dJ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "BEL(K)",	BLOCK_K,"Belegung" },
	{ "------dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VH(K)",	BLOCK_K,"Verbindung halten" },
	{ "------dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "RTAQ(K)",	BLOCK_K,"Quittung Rufton anschalten" },
	{ "------dJBB------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AH(K)",	BLOCK_K,"Abhebe-Signal" },
	{ "-----wdJBBCt----eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VH(V)",	BLOCK_V,"Verbindung halten" },
	{ "------dJ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AT(K)",	BLOCK_K,"Ausloesen durch Funktelefonteilnehmer" },
	{ "------dJBBC-------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AT(V)",	BLOCK_V,"Ausloesen durch Funktelefonteilnehmer" },
	{ "------dJBB------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "DSQ(K)",	BLOCK_K,"Durchschalten Quittung" },
	{ "-----wdJBBCt----eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "USAI(V)",	BLOCK_V,"Umschaltantrag intern" },
	{ "-----wdJBBCt----eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "USAE(V)",	BLOCK_V,"Umschaltantrag extern" },
	{ "------dJBB--------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "USTLN(K)",	BLOCK_K,"Umschalten Funktelefonteilnehmer" },
	{ "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", NULL, "ZFZQ(K)",	BLOCK_K,"Zufallszahlquittung" },
	{ "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", NULL, "AP(K)",	BLOCK_K,"Autorisierungsparameter" },
	{ "________________________________________________________________", NULL, "opcode 29",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 30",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 31",BLOCK_I,"Illegaler Opcode" },
	{ "PP-MMMMMDDDDEEEE------HHHHHHHHHHFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "MA(M)",	BLOCK_M,"Meldeaufruf" },
	{ "PPdZZZZZ-----KKKKKKKKKKKIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VAK(R)",	BLOCK_R,"Verbindungsaufbau kommend" },
	{ "________________________________________________________________", NULL, "opcode 34",BLOCK_I,"Illegaler Opcode" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "EBQ(R)",	BLOCK_R,"Einbuchquittung" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "UBQ(R)",	BLOCK_R,"Umbuchquittung" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "WSK(R)",	BLOCK_R,"Wartescglange kommend" },
	{ "PP-MMMMMDDDDEEEE------HHHHHHHHHHFFFFFFFF------------------------", NULL, "MLR(M)",	BLOCK_M,"Melde-Leer-Ruf" },
	{ "PPdZZZZZffflvvWW------yyIIIAAAAAFFFFFFFFkkgprrrrmmmmnnnnuuuuaaaa", NULL, "LR(R)",	BLOCK_R,"Leer-Ruf" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "ATQ(R)",	BLOCK_R,"Quittung fuer Ausloesen des FuTelG im OgK-Betrieb" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "SAR(R)",	BLOCK_R,"Sperraufruf" },
	{ "PP-MMMMMDDDDEEEE------HHHHHHHHHHFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "WAF(M)",	BLOCK_M,"Wahlaufforderung" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "WBP(R)",	BLOCK_R,"Wahlbestaetigung positiv" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "WBN(R)",	BLOCK_R,"Wahlbestaetigung negativ" },
	{ "PPdZZZZZ----------------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "WWBP(R)",	BLOCK_R,"Wahlbestaetigung positiv in Warteschlange" },
	{ "PPdZZZZZ-----KKKKKKKKKKKIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VAG(R)",	BLOCK_R,"Verbindungsaufbau gehend" },
	{ "PPdZZZZZ------cc--------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VA(R)",	BLOCK_R,"Vorzeitiges Ausloesen" },
	{ "PP----dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "BQ(K)",	BLOCK_K,"Belegungsquittung" },
	{ "PP----dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VHQ(K)",	BLOCK_K,"Quittung Verbindung halten" },
	{ "PP----dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "RTA(K)",	BLOCK_K,"Rufton anschalten" },
	{ "PP----dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AHQ(K)",	BLOCK_K,"Abhebe-Quittierung" },
	{ "PP----dJ--C-----eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VHQ1(V)",	BLOCK_V,"Verbindung halten Quittung 1" },
	{ "PP----dJ--CsGGGGGGGGGGGGIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "VHQ2(V)",	BLOCK_V,"Verbindung halten Quittung 2" },
	{ "PP----dJ------cc--------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AF(K)",	BLOCK_K,"Ausloesen durch FuFSt in konzentr. Signalisierung" },
	{ "PP----dJ------cc--------IIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "AF(V)",	BLOCK_V,"Ausloesen durch FuFSt in verteilter Signalisierung" },
	{ "PP----dJ--------eeeeeeeeIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "DSB(K)",	BLOCK_K,"Durchschaltung" },
	{ "PP----dJ-----KKKKKKKKKKKIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "DSBI(V)",	BLOCK_V,"Umschaltbefehl intern (neuer SpK in der gleichen FuZ)" },
	{ "PP----dJ-----KKKKKKKKKKKIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "USF(K)",	BLOCK_K,"Umschalten FuFst" },
	{ "PP----dJ-----KKKKKKKKKKKIIIAAAAAFFFFFFFFNNNUUUUUTTTTTTTTTTTTTTTT", NULL, "USBE(V)",	BLOCK_V,"Umschaltbefehl extern (neuer SpK in einer anderen Funkzelle)" },
	{ "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", NULL, "ZFZ(K)",	BLOCK_K,"Zufallszahl" },
	{ "________________________________________________________________", NULL, "opcode 61",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 62",BLOCK_I,"Illegaler Opcode" },
	{ "________________________________________________________________", NULL, "opcode 63",BLOCK_I,"Illegaler Opcode" },
};

const char *telegramm_name(uint8_t opcode)
{
	return definition_opcode[opcode].message_name;
}

int init_telegramm(void)
{
	struct definition_parameter *parameter;
	int i, j;
	const char *bits;
	char last_bit;
	int count_bits;

	/* copy no_auth_bits to auth_bits, if required
	 * check if the number of bits in a message matches the number of bits of a parameter */
	for (i = 0; i < 64; i++) {
		if (definition_opcode[i].auth_bits == NULL)
			definition_opcode[i].auth_bits = definition_opcode[i].no_auth_bits;
		for (bits = definition_opcode[i].no_auth_bits; ; bits = definition_opcode[i].auth_bits) {
			last_bit = '-';
			count_bits = 0;
			for (j = 0; j < 65; j++) { /* include termination character */
				if (last_bit != bits[j]) {
					if (last_bit != '-') {
						parameter = get_parameter(last_bit);
						if (!parameter) {
							printf("Message #%d has invalid digit '%c'\n", i, last_bit);
							return -1;
						}
						if (parameter->bits != count_bits) {
							printf("Message #%d has digit '%c' with %d bits, but parameter has %d bits\n", i, last_bit, count_bits, parameter->bits);
							return -1;
						}
					}
					last_bit = bits[j];
					count_bits = 0;
				}
				count_bits++;
			}
			if (bits == definition_opcode[i].auth_bits)
				break;
		}
	}

	/* generate frequency names */
	for (i = 0; i < 2048; i++) {
		char *frequenz = calloc(16, 1);
		if ((i & 1))
			sprintf(frequenz, "%.4f MHz", 465.750 - (double)(i+1) / 2.0 * 0.010);
		else
			sprintf(frequenz, "%.4f MHz", 465.750 - (double)i / 2.0 * 0.0125);
		param_frequenz[i] = frequenz;
	}

	/* generate opcode names */
	for (i = 0; i < 64; i++)
		param_opcode[i] = definition_opcode[i].message_name;

	return 0;
}

const char *telegramm2rufnummer(telegramm_t *telegramm)
{
	static char rufnummer[9];

	sprintf(rufnummer, "%d%d%05d", telegramm->futln_nationalitaet, telegramm->futln_heimat_fuvst_nr, telegramm->futln_rest_nr);

	return rufnummer;
}

static void decode_dialstring(char *number, uint64_t value)
{
	int index, max, shift;
	int digit;

	if ((value & 0x000000000000000f) == 0) {
		max = 16;
		index = 1;
		number[0] = '0';
	} else {
		max = value & 0x000000000000000f;
		index = 0;
	}
	shift = 4;
	while (index < max) {
		digit = (value >> shift) & 0xf;
		switch (digit) {
		case 0xb:
			digit = '*';
			break;
		case 0xc:
			digit = '#';
			break;
		case 0xa:
		case 0xd:
		case 0xe:
		case 0xf:
			digit = digit - 0xa + 'a';
			break;
		default:
			digit = digit + '0';
		}
		number[index] = digit;
		index++;
		shift += 4;
	}
	number[index] = '\0';
}

static int encode_dialstring(uint64_t *value, const char *number)
{
	int max, index, shift, digit;

	max = strlen(number);
	if (max > 16) {
		PDEBUG(DFRAME, DEBUG_NOTICE, "Given number '%s' has more than 16 digits\n", number);
		return -EINVAL;
	}

	if (max == 16) {
		if (number[0] != '0') {
			PDEBUG(DFRAME, DEBUG_NOTICE, "Given 16 digit number '%s' does not start with '0'\n", number);
			return -EINVAL;
		}
		*value = 0;
		index = 1;
	} else {
		*value = strlen(number);
		index = 0;
	}
	shift = 4;
	while (index < max) {
		digit = number[index];
		switch (digit) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			digit = digit - '0';
			break;
		case 'a':
		case 'd':
		case 'e':
		case 'f':
			digit = digit - 'a' + 0xa;
			break;
		case 'A':
		case 'D':
		case 'E':
		case 'F':
			digit = digit - 'A' + 0xa;
			break;
		case '*':
			digit = 0xb;
			break;
		case '#':
			digit = 0xc;
			break;
		default:
			return -EINVAL;
		}
		*value |= (uint64_t)digit << shift;
		index++;
		shift += 4;
	}

	return 0;
}

int match_fuz(cnetz_t *cnetz, telegramm_t *telegramm, int cell)
{
	if (telegramm->fuz_nationalitaet != si[cell].fuz_nat
	 || telegramm->fuz_fuvst_nr != si[cell].fuz_fuvst
	 || telegramm->fuz_rest_nr != si[cell].fuz_rest) {
	 	if (!cnetz->cell_auto)
			PDEBUG(DFRAME, DEBUG_NOTICE, "Ignoring message from mobile phone %d,%d,%d: Cell 'Funkzelle' does not match!\n", telegramm->futln_nationalitaet, telegramm->futln_heimat_fuvst_nr, telegramm->futln_rest_nr);
	 	return 0;
	}

	return 1;
}

int match_futln(telegramm_t *telegramm, uint8_t futln_nat, uint8_t futln_fuvst, uint16_t futln_rest)
{
	if (telegramm->futln_nationalitaet != futln_nat
	 || telegramm->futln_heimat_fuvst_nr != futln_fuvst
	 || telegramm->futln_rest_nr != futln_rest) {
		PDEBUG(DFRAME, DEBUG_NOTICE, "Ignoring message from mobile phone %d,%d,%d: Mobile station 'Funktelefongeraet' does not match!\n", telegramm->futln_nationalitaet, telegramm->futln_heimat_fuvst_nr, telegramm->futln_rest_nr);
	 	return 0;
	}

	return 1;
}

static void debug_parameter(char digit, uint64_t value)
{
	struct definition_parameter *parameter;

	parameter = get_parameter(digit);
	if (!parameter) {
		PDEBUG(DFRAME, DEBUG_ERROR, "Digit '%c' not found in definition_parameter list, please fix!\n", digit);
		abort();
	}
	if (parameter->value_names)
		PDEBUG(DFRAME, DEBUG_DEBUG, " (%c) %s : %s\n", digit, parameter->param_name, parameter->value_names[value]);
	else if (parameter->bits == 64)
		PDEBUG(DFRAME, DEBUG_DEBUG, " (%c) %s : 0x%016" PRIx64 "\n", digit, parameter->param_name, value);
	else if (digit == 'X') {
		char wahlziffern[17];
		decode_dialstring(wahlziffern, value);
		PDEBUG(DFRAME, DEBUG_DEBUG, " (%c) %s : '%s'\n", digit, parameter->param_name, wahlziffern);
	} else
		PDEBUG(DFRAME, DEBUG_DEBUG, " (%c) %s : %" PRIu64 "\n", digit, parameter->param_name, value);
}

/* encode telegram to 70 bits
 * bit order MSB
 */
static char *assemble_telegramm(const telegramm_t *telegramm, int debug)
{
	static char bits[71]; /* + termination char for debug */
	char parameter;
	const char *string;
	uint64_t value, val;
	int i, j;
	int rc;

	if (telegramm->opcode >= 64) {
		PDEBUG(DFRAME, DEBUG_ERROR, "Opcode '0x%x' exceeds bit range, please fix!\n", telegramm->opcode);
		abort();
	}

	PDEBUG(DFRAME, DEBUG_DEBUG, "Coding %s %s\n", definition_opcode[telegramm->opcode].message_name, definition_opcode[telegramm->opcode].message_text);

	/* copy opcode */
	for (i = 0; i < 6; i++)
		bits[5 - i] = (telegramm->opcode & (1 << i)) ? '1' : '0';

	/* copy parameters */
	string = definition_opcode[telegramm->opcode].no_auth_bits;
	for (i = 0; i < 64; i++) {
		parameter = string[63 - i];
		if (parameter == '-') {
			bits[69 - i] = '0';
			continue;
		}
		switch (parameter) {
		case 'A':
			value = telegramm->fuz_fuvst_nr;
			break;
		case 'B':
			value = telegramm->betriebs_art;
			break;
		case 'C':
			value = telegramm->ankuendigung_gespraechsende;
			break;
		case 'D':
			value = telegramm->teilnehmersperre;
			break;
		case 'E':
			value = telegramm->anzahl_gesperrter_teilnehmergruppen;
			break;
		case 'F':
			value = telegramm->fuz_rest_nr;
			break;
		case 'G':
			value = telegramm->gebuehren_stand;
			break;
		case 'H':
			value = telegramm->ogk_vorschlag;
			break;
		case 'I':
			value = telegramm->fuz_nationalitaet;
			break;
		case 'J':
			value = telegramm->sendeleistungsanpassung;
			break;
		case 'K':
			value = telegramm->frequenz_nr;
			break;
		case 'L':
			value = telegramm->art_der_signalisierung_im_ogk;
			break;
		case 'M':
			value = telegramm->ogk_verkehrsanteil;
			break;
		case 'N':
			value = telegramm->futln_nationalitaet;
			break;
		case 'P':
			value = telegramm->max_sendeleistung;
			break;
		case 'Q':
			value = telegramm->kartenkennung;
			break;
		case 'R':
			value = telegramm->durchfuehrung_der_ueberlastbehandlung;
			break;
		case 'S':
			value = telegramm->sonderruf;
			break;
		case 'T':
			value = telegramm->futln_rest_nr;
			break;
		case 'U':
			value = telegramm->futln_heimat_fuvst_nr;
			break;
		case 'V':
			value = telegramm->sicherungs_code;
			break;
		case 'W':
			value = telegramm->ws_kennung;
			break;
		case 'X':
			rc = encode_dialstring(&value, telegramm->wahlziffern);
			if (rc < 0) {
				PDEBUG(DFRAME, DEBUG_ERROR, "Illegal dial string '%s', please fix!\n", telegramm->wahlziffern);
				abort();
			}
			break;
		case 'Z':
			value = telegramm->zeitschlitz_nr;
			break;
		case 'a':
			value = telegramm->grenze_fuer_ausloesen;
			break;
		case 'b':
			value = telegramm->chipkarten_futelg_bit;
			break;
		case 'c':
			value = telegramm->ausloesegrund;
			break;
		case 'd':
			value = telegramm->bedingte_genauigkeit_der_fufst;
			break;
		case 'e':
			value = telegramm->entfernung;
			break;
		case 'f':
			value = telegramm->grenzwert_fuer_einbuchen_und_umbuchen;
			break;
		case 'g':
			value = telegramm->nachbarschafts_prioritaets_bit;
			break;
		case 'h':
			value = telegramm->herstellerkennung;
			break;
		case 'i':
			value = telegramm->hardware_des_futelg;
			break;
		case 'j':
			value = telegramm->software_des_futelg;
			break;
		case 'k':
			value = telegramm->kennung_fufst;
			break;
		case 'l':
			value = telegramm->authentifikationsbit;
			break;
		case 'm':
			value = telegramm->mittelungsfaktor_fuer_ausloesen;
			break;
		case 'n':
			value = telegramm->mittelungsfaktor_fuer_umschalten;
			break;
		case 'o':
			value = telegramm->zufallszahl;
			break;
		case 'p':
			value = telegramm->bewertung_nach_pegel_und_entfernung;
			break;
		case 'q':
			value = telegramm->authorisierungsparameter;
			break;
		case 'r':
			value = telegramm->entfernungsangabe_der_fufst;
			break;
		case 's':
			value = telegramm->gueltigkeit_des_gebuehrenstandes;
			break;
		case 't':
			value = telegramm->test_telefonteilnehmer_geraet;
			break;
		case 'u':
			value = telegramm->grenzwert_fuer_umschalten;
			break;
		case 'v':
			value = telegramm->vermittlungstechnische_sperren;
			break;
		case 'w':
			value = telegramm->erweitertes_frequenzbandbit;
			break;
		case 'y':
			value = telegramm->reduzierungsfaktor;
			break;
		case '_':
			value = telegramm->illegaler_opcode;
			break;
		default:
			PDEBUG(DFRAME, DEBUG_ERROR, "Parameter '%c' does not exist, please fix!\n", parameter);
			abort();
		}
		if (debug && debuglevel <= DEBUG_DEBUG)
			debug_parameter(parameter, value);
		val = value;
		for (j = 0; string[63 - i - j] == parameter; j++) {
			bits[69 - i - j] = (val & 1) ? '1' : '0';
			val >>= 1;
		}
		if (val)
			PDEBUG(DFRAME, DEBUG_ERROR, "Parameter '%c' value '0x%" PRIx64 "' exceeds bit range!\n", parameter, value);
		i += j - 1;
	}
	bits[70] = '\0';

	if (debug) {
		PDEBUG(DFRAME, DEBUG_DEBUG, "OOOOOO%s\n", string);
		PDEBUG(DFRAME, DEBUG_DEBUG, "%s\n", bits);
	}

	return bits;
}

/* decode telegram from 70 bits
 * bit order MSB
 */
static void disassemble_telegramm(telegramm_t *telegramm, const char *bits, int auth)
{
	uint64_t value;
	const char *string;
	char parameter;
	int i, j;

	memset(telegramm, 0, sizeof(*telegramm));

	/* copy opcode */
	value = 0;
	for (i = 0; i < 6; i++)
		value = (value << 1) | (bits[i] == '1');
	telegramm->opcode = value;

	PDEBUG(DFRAME, DEBUG_DEBUG, "Decoding %s %s\n", definition_opcode[telegramm->opcode].message_name, definition_opcode[telegramm->opcode].message_text);

	/* copy parameters */
	if (auth && bits[1]) /* auth flag and chip card flag */
		string = definition_opcode[telegramm->opcode].auth_bits;
	else
		string = definition_opcode[telegramm->opcode].no_auth_bits;
	for (i = 0; i < 64; i++) {
		parameter = string[63 - i];
		if (parameter == '-')
			continue;
		value = 0;
		for (j = 0; i + j < 64 && string[63 - i - j] == parameter; j++)
			value = (value >> 1) | ((uint64_t)(bits[69 - i - j] == '1') << 63);
		value >>= 64 - j;
		i += j - 1;
		if (debuglevel <= DEBUG_DEBUG)
			debug_parameter(parameter, value);
		switch (parameter) {
		case 'A':
			telegramm->fuz_fuvst_nr = value;
			break;
		case 'B':
			telegramm->betriebs_art = value;
			break;
		case 'C':
			telegramm->ankuendigung_gespraechsende = value;
			break;
		case 'D':
			telegramm->teilnehmersperre = value;
			break;
		case 'E':
			telegramm->anzahl_gesperrter_teilnehmergruppen = value;
			break;
		case 'F':
			telegramm->fuz_rest_nr = value;
			break;
		case 'G':
			telegramm->gebuehren_stand = value;
			break;
		case 'H':
			telegramm->ogk_vorschlag = value;
			break;
		case 'I':
			telegramm->fuz_nationalitaet = value;
			break;
		case 'J':
			telegramm->sendeleistungsanpassung = value;
			break;
		case 'K':
			telegramm->frequenz_nr = value;
			break;
		case 'L':
			telegramm->art_der_signalisierung_im_ogk = value;
			break;
		case 'M':
			telegramm->ogk_verkehrsanteil = value;
			break;
		case 'N':
			telegramm->futln_nationalitaet = value;
			break;
		case 'P':
			telegramm->max_sendeleistung = value;
			break;
		case 'Q':
			telegramm->kartenkennung = value;
			break;
		case 'R':
			telegramm->durchfuehrung_der_ueberlastbehandlung = value;
			break;
		case 'S':
			telegramm->sonderruf = value;
			break;
		case 'T':
			telegramm->futln_rest_nr = value;
			break;
		case 'U':
			telegramm->futln_heimat_fuvst_nr = value;
			break;
		case 'V':
			telegramm->sicherungs_code = value;
			break;
		case 'W':
			telegramm->ws_kennung = value;
			break;
		case 'X':
			decode_dialstring(telegramm->wahlziffern, value);
			break;
		case 'Z':
			telegramm->zeitschlitz_nr = value;
			break;
		case 'a':
			telegramm->grenze_fuer_ausloesen = value;
			break;
		case 'b':
			telegramm->chipkarten_futelg_bit = value;
			break;
		case 'c':
			telegramm->ausloesegrund = value;
			break;
		case 'd':
			telegramm->bedingte_genauigkeit_der_fufst = value;
			break;
		case 'e':
			telegramm->entfernung = value;
			break;
		case 'f':
			telegramm->grenzwert_fuer_einbuchen_und_umbuchen = value;
			break;
		case 'g':
			telegramm->nachbarschafts_prioritaets_bit = value;
			break;
		case 'h':
			telegramm->herstellerkennung = value;
			break;
		case 'i':
			telegramm->hardware_des_futelg = value;
			break;
		case 'j':
			telegramm->software_des_futelg = value;
			break;
		case 'k':
			telegramm->kennung_fufst = value;
			break;
		case 'l':
			telegramm->authentifikationsbit = value;
			break;
		case 'm':
			telegramm->mittelungsfaktor_fuer_ausloesen = value;
			break;
		case 'n':
			telegramm->mittelungsfaktor_fuer_umschalten = value;
			break;
		case 'o':
			telegramm->zufallszahl = value;
			break;
		case 'p':
			telegramm->bewertung_nach_pegel_und_entfernung = value;
			break;
		case 'q':
			telegramm->authorisierungsparameter = value;
			break;
		case 'r':
			telegramm->entfernungsangabe_der_fufst = value;
			break;
		case 's':
			telegramm->gueltigkeit_des_gebuehrenstandes = value;
			break;
		case 't':
			telegramm->test_telefonteilnehmer_geraet = value;
			break;
		case 'u':
			telegramm->grenzwert_fuer_umschalten = value;
			break;
		case 'v':
			telegramm->vermittlungstechnische_sperren = value;
			break;
		case 'w':
			telegramm->erweitertes_frequenzbandbit = value;
			break;
		case 'y':
			telegramm->reduzierungsfaktor = value;
			break;
		case '_':
			telegramm->illegaler_opcode = value;
			break;
		default:
			PDEBUG(DFRAME, DEBUG_ERROR, "Parameter '%c' does not exist, please fix!\n", parameter);
			abort();
		}
	}

	if (debuglevel <= DEBUG_DEBUG) {
		char debug_bits[71];

		memcpy(debug_bits, bits, 70);
		debug_bits[70] = '\0';
		PDEBUG(DFRAME, DEBUG_DEBUG, "OOOOOO%s\n", string);
		PDEBUG(DFRAME, DEBUG_DEBUG, "%s\n", debug_bits);
	}

}

static const char *barker_string = "11100010010";
static int16_t barker_code = 0x712; /* 11 bits: 11100010010 */
static uint8_t barker_decode[2048]; /* detected bits */

static char *blockcode[128] = {
/*	 0123456 = Nutzbits */
/*	           01234567 = Redundanzbits */
	"0000000" "00000000",
	"1000000" "11101000",
	"0100000" "01110100",
	"1100000" "10011100",
	"0010000" "00111010",
	"1010000" "11010010",
	"0110000" "01001110",
	"1110000" "10100110",
	"0001000" "00011101",
	"1001000" "11110101",
	"0101000" "01101001",
	"1101000" "10000001",
	"0011000" "00100111",
	"1011000" "11001111",
	"0111000" "01010011",
	"1111000" "10111011",
	"0000100" "11100110",
	"1000100" "00001110",
	"0100100" "10010010",
	"1100100" "01111010",
	"0010100" "11011100",
	"1010100" "00110100",
	"0110100" "10101000",
	"1110100" "01000000",
	"0001100" "11111011",
	"1001100" "00010011",
	"0101100" "10001111",
	"1101100" "01100111",
	"0011100" "11000001",
	"1011100" "00101001",
	"0111100" "10110101",
	"1111100" "01011101",
	"0000010" "01110011",
	"1000010" "10011011",
	"0100010" "00000111",
	"1100010" "11101111",
	"0010010" "01001001",
	"1010010" "10100001",
	"0110010" "00111101",
	"1110010" "11010101",
	"0001010" "01101110",
	"1001010" "10000110",
	"0101010" "00011010",
	"1101010" "11110010",
	"0011010" "01010100",
	"1011010" "10111100",
	"0111010" "00100000",
	"1111010" "11001000",
	"0000110" "10010101",
	"1000110" "01111101",
	"0100110" "11100001",
	"1100110" "00001001",
	"0010110" "10101111",
	"1010110" "01000111",
	"0110110" "11011011",
	"1110110" "00110011",
	"0001110" "10001000",
	"1001110" "01100000",
	"0101110" "11111100",
	"1101110" "00010100",
	"0011110" "10110010",
	"1011110" "01011010",
	"0111110" "11000110",
	"1111110" "00101110",
	"0000001" "11010001",
	"1000001" "00111001",
	"0100001" "10100101",
	"1100001" "01001101",
	"0010001" "11101011",
	"1010001" "00000011",
	"0110001" "10011111",
	"1110001" "01110111",
	"0001001" "11001100",
	"1001001" "00100100",
	"0101001" "10111000",
	"1101001" "01010000",
	"0011001" "11110110",
	"1011001" "00011110",
	"0111001" "10000010",
	"1111001" "01101010",
	"0000101" "00110111",
	"1000101" "11011111",
	"0100101" "01000011",
	"1100101" "10101011",
	"0010101" "00001101",
	"1010101" "11100101",
	"0110101" "01111001",
	"1110101" "10010001",
	"0001101" "00101010",
	"1001101" "11000010",
	"0101101" "01011110",
	"1101101" "10110110",
	"0011101" "00010000",
	"1011101" "11111000",
	"0111101" "01100100",
	"1111101" "10001100",
	"0000011" "10100010",
	"1000011" "01001010",
	"0100011" "11010110",
	"1100011" "00111110",
	"0010011" "10011000",
	"1010011" "01110000",
	"0110011" "11101100",
	"1110011" "00000100",
	"0001011" "10111111",
	"1001011" "01010111",
	"0101011" "11001011",
	"1101011" "00100011",
	"0011011" "10000101",
	"1011011" "01101101",
	"0111011" "11110001",
	"1111011" "00011001",
	"0000111" "01000100",
	"1000111" "10101100",
	"0100111" "00110000",
	"1100111" "11011000",
	"0010111" "01111110",
	"1010111" "10010110",
	"0110111" "00001010",
	"1110111" "11100010",
	"0001111" "01011001",
	"1001111" "10110001",
	"0101111" "00101101",
	"1101111" "11000101",
	"0011111" "01100011",
	"1011111" "10001011",
	"0111111" "00010111",
	"1111111" "11111111",
};

static uint16_t block_code[128];
static uint16_t block_decode[32768]; /* code word + flag / 0xffff=decode error */

int init_coding(void)
{
	int i, j, k;

	/* create table to decode barker code.
	 * ech table entry returns the number of detected bits */
	for (i = 0; i < 2048; i++) {
		int match = 0;
		for (j = 0; j < 11; j++) {
			/* check if i matches barker code at given bit j */
			if (((i ^ barker_code) & (0x400 >> j)) == 0)
				match++;
		}
		barker_decode[i] = match;
	}

	/* convert string to block code words */
	for (i = 0; i < 128; i++) {
		int word = 0;
		for (j = 0; j < 15; j++)
			word = (word << 1) + (blockcode[i][14 - j] - '0');
		if ((word & 0x7f) != i) {
			printf("Databits are wrong, expecting %d, but got %d\n", i, word & 0x7f);
			return -1;
		}
		block_code[i] = word;
	}

	/* check if redunancy of a single bit matches the combined redundancy */
	for (i = 0; i < 128; i++) {
		int r = 0;
		for (j = 0; j < 7; j++) {
			if ((i & (1 << j)))
				r ^= block_code[1 << j] >> 7;
		}
		if (r != block_code[i] >> 7) {
			printf("Redundancy bits are wrong\n");
			return -1;
		}
	}

	/* create table to decode one block code and return value + error */
	/* set all combinations invalid */
	for (i = 0; i < 32768; i++)
		block_decode[i] = 0xffff;
	for (i = 0; i < 128; i++) {
		int word;
		/* set all error free combinations valid */
		word = block_code[i];
		if (block_decode[word] != 0xffff) {
			printf("Overlap, please fix!\n");
			return -1;
		}
		block_decode[word] = i;
		/* set all one bit error combinations valid with flag */
		for (j = 0; j < 15; j++) {
			word = block_code[i];
			word ^= (1 << j);
			if (block_decode[word] != 0xffff) {
				printf("Overlap, please fix!\n");
				return -1;
			}
			block_decode[word] = i | 0x100; /* indicate 1 error */
			/* set all two bit error combinations valid with flag */
			for (k = j + 1; k < 15; k++) {
				word = block_code[i];
				word ^= (1 << j) | (1 << k);
				if (block_decode[word] != 0xffff) {
					printf("Overlap, please fix!\n");
					return -1;
				}
				block_decode[word] = i | 0x200; /* indicate 2 errors */
			}
		}
	}

#if 0
	int count = 0;
	for (i = 0; i < 32768; i++) {
		printf("%d,", (int16_t)block_decode[i]);
		if (block_decode[i] == 0xffff)
		count++;
	}
	printf("bad blocks = %d\n", count);
#endif

	return 0;
}

/* check for sync (3 * barker code) + 1 bit */
int detect_sync(uint64_t bitstream)
{
	int match;

	/* hack: ignore first 3 bits of first barker code */
	bitstream |= 0x380000000;

	/* metch 33 bits, not as specified by FTZ */
	match = barker_decode[(bitstream >> 23) & 0x7ff];
	if (match < 11)
		return 0;
	match += barker_decode[(bitstream >> 12) & 0x7ff];
	if (match < 22)
		return 0;
	match += barker_decode[(bitstream >> 1) & 0x7ff];
	if (match < 33)
		return 0;

	return 1;
}

/* encode data block
 * input: 70 data bits MSB first
 * output: 10*15 code words (LSB first)
 * FTZ 171 TR 60 / 5.1.1.3 */
static char *encode(const char *input)
{
	static char output[150];
	int16_t word;
	int i, j;

#ifdef DEBUG_CODER
	printf("Encoding block to transmit:\n");
	printf("0123456.01234567\n");
#endif
	for (i = 0; i < 10; i++) {
		word = 0;
		for (j = 0; j < 7; j++)
			word = (word << 1) | (input[(9 - i) * 7 + j] == '1');
		word = block_code[word];
		for (j = 0; j < 15; j++) {
			output[i * 15 + j] = ((word >> j) & 1) + '0';
#ifdef DEBUG_CODER
			printf("%c", output[i * 15 + j]);
			if (j == 6)
				printf(".");
#endif
		}
#ifdef DEBUG_CODER
		printf("\n");
#endif
	}

	return output;
}

/* decode data block
 * input: 10*15 code words (LSB first)
 * output: 70 data bits MSB first
 * FTZ 171 TR 60 / 5.1.1.3 */
static const char *decode(const char *input, int *_bit_errors)
{
	int failed = 0, warn = 0;
	char fail_str[11];
	static char output[70];
	uint16_t word;
	int i, j;

#ifdef DEBUG_CODER
	printf("Decoding received block:\n");
	printf("0123456.01234567 Without errors:  Error bits:\n");
#endif
	for (i = 0; i < 10; i++) {
		word = 0;
		for (j = 0; j < 15; j++) {
			word = (word << 1) | (input[i * 15 + 14 - j] == '1');
#ifdef DEBUG_CODER
			printf("%c", input[i * 15 + j]);
			if (j == 6)
				printf(".");
#endif
		}
		word = block_decode[word];
		for (j = 0; j < 7; j++) {
			output[(9 - i) * 7 + j] = ((word >> (6 - j)) & 1) + '0';
		}
		if (word > 0x2ff) {
			failed = 1;
			fail_str[i] = 'X';
		} else
		if (word > 0x1ff) {
			warn += 2;
			fail_str[i] = '2';
		}else
		if (word > 0xff) {
			warn += 1;
			fail_str[i] = '1';
		} else
			fail_str[i] = '.';
#ifdef DEBUG_CODER
		if (word > 0x2ff)
			printf("decode failed");
		else {
			printf(" ");
			for (j = 0; j < 15; j++) {
				printf("%c", blockcode[word & 0x7f][j]);
				if (j == 6)
					printf(".");
			}
			printf(" ");
			for (j = 0; j < 15; j++) {
				if (blockcode[word & 0x7f][j] != input[i * 15 + j])
					printf("*");
				else
					printf("-");
				if (j == 6)
					printf(".");
			}
		}
		printf("\n");
#endif
	}
	fail_str[10] = '\0';
	
	if (failed)
		PDEBUG(DFRAME, DEBUG_DEBUG, "Received Telegram with these block errors: '%s' (X = uncorrectable)\n", fail_str);
	else if (warn)
		PDEBUG(DFRAME, DEBUG_DEBUG, "Received Telegram with these block errors: '%s' (1 / 2 = correctable)\n", fail_str);
	else
		PDEBUG(DFRAME, DEBUG_DEBUG, "Received Telegram with no block errors.\n");

	if (failed)
		return NULL;
	*_bit_errors = warn;
	return output;
}

/* interleving of code words
 * input: 10*15 code words (LSB first)
 * output: stream of 33 sync + 1 + 150 interleaved bits
 * FTZ 171 TR 60 / 5.1.1.2 and 5.1.1.2 */
static char *interleave(const char *input)
{
	static char output[185]; /* + termination char for debug */
	int i, j;

	strcpy(output, barker_string);
	strcpy(output + 11, barker_string);
	strcpy(output + 22, barker_string);

	output[33] = '1';

#ifdef DEBUG_BLOCK
	printf("Interleaving block to transmit:\n");
#endif
	for (i = 0; i < 10; i++) {
		for (j = 0; j < 15; j++) {
			output[i + j * 10 + 34] = input[j + i * 15];
#ifdef DEBUG_BLOCK
			printf("%c", input[j + i * 15]);
#endif
		}
#ifdef DEBUG_BLOCK
		printf("\n");
#endif
	}

#ifdef DEBUG_RAW
	output[184] = '\0';
	printf("Raw TX: %s\n", output + 34);
#endif

	return output;
}

/* deinterleave of code words
 * input: stream of 150 interleaved bits
 * output: 10*15 code words (LSB first)
 * FTZ 171 TR 60 / 5.1.1.4 */
static const char *deinterleave(const char *input)
{
	static char output[150];
	int i, j;

#ifdef DEBUG_RAW
	char debug_bits[151];

	memcpy(debug_bits, input, 150);
	debug_bits[151] = '\0';
	printf("Raw RX: %s\n", debug_bits);
#endif

#ifdef DEBUG_BLOCK
	printf("Deinterleaving received block:\n");
#endif
	for (i = 0; i < 10; i++) {
		for (j = 0; j < 15; j++) {
			output[j + i * 15] = input[i + j * 10];
#ifdef DEBUG_BLOCK
			printf("%c", output[j + i * 15]);
#endif
		}
#ifdef DEBUG_BLOCK
		printf("\n");
#endif
	}

	return output;
}

void cnetz_decode_telegramm(cnetz_t *cnetz, const char *bits, double level, double sync_time, double jitter)
{
	telegramm_t telegramm;
	uint8_t opcode;
	int i;
	int block;
	int bit_errors;

	bits = deinterleave(bits);
	bits = decode(bits, &bit_errors);
	if (!bits)
		return;

	/* filter out mysterious zero-telegramm */
	for (i = 0; i < 70; i++) {
		if (bits[i] != bits[0])
			break;
	}
	if (i == 70) {
		PDEBUG(DFRAME, DEBUG_INFO, "Ignoring mysterious unmodulated telegramm (noise from phone's transmitter)\n");
		return;
	}

	disassemble_telegramm(&telegramm, bits, cnetz->auth);
	opcode = telegramm.opcode;
	telegramm.level = level;
	telegramm.sync_time = sync_time;
	telegramm.jitter = jitter;

	if (bit_errors)
		PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) Bit errors: %d %s\n", fabs(level) * 32767.0 / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, bit_errors, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)");
	else
		PDEBUG_CHAN(DDSP, DEBUG_INFO, "RX Level: %.0f%% Jitter: %.2f Sync Time: %.2f (TS %.2f) %s\n", fabs(level) * 32767.0 / cnetz->fsk_deviation * 100.0, jitter, sync_time, sync_time / 396.0, (level < 0) ? "NEGATIVE (phone's mode)" : "POSITIVE (base station's mode)");

	if (cnetz->sender.loopback) {
		PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm in loopback test mode (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
		cnetz_sync_frame(cnetz, sync_time, -1);
		return;
	}

	if (opcode >= 32) {
		PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm that is not used by mobile station, ignoring! (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
		return;
	}

	if (definition_opcode[opcode].block == BLOCK_I) {
		PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm that is an illegal opcode, ignoring! (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
		return;
	}

	/* auto select cell */
	if (cnetz->cell_auto) {
		sender_t *sender;
		cnetz_t *c;
		int nr;
		if (match_fuz(cnetz, &telegramm, 0)) {
			nr = 0;
selected:
			printf("***********************************************\n");
			printf("*** Autoselecting %stive FSK TX polarity! ***\n", (si[nr].flip_polarity) ? "nega" : "posi");
			printf("***********************************************\n");
			/* select on all transceivers */
			for (sender = sender_head; sender; sender = sender->next) {
				c = (cnetz_t *) sender;
				c->cell_auto = 0;
				c->cell_nr = nr;
			}
		} else if (match_fuz(cnetz, &telegramm, 1)) {
			nr = 1;
			goto selected;
		} else {
			PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm with no cell number, ignoring!\n");
			return;
		}
	}

	switch (cnetz->dsp_mode) {
	case DSP_MODE_OGK:
		if (definition_opcode[opcode].block != BLOCK_R && definition_opcode[opcode].block != BLOCK_M) {
			PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm that is not used OgK channel signaling, ignoring! (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
			return;
		}
		/* determine block by last timeslot sent and by message type */
		block = cnetz->sched_last_ts[cnetz->cell_nr] * 2;
		if (definition_opcode[opcode].block == BLOCK_M)
			block++;
		cnetz_receive_telegramm_ogk(cnetz, &telegramm, block);
		break;
	case DSP_MODE_SPK_K:
		if (definition_opcode[opcode].block != BLOCK_K) {
			PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm that is not used for concentrated signaling, ignoring! (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
			return;
		}
		cnetz_receive_telegramm_spk_k(cnetz, &telegramm);
		break;
	case DSP_MODE_SPK_V:
		if (definition_opcode[opcode].block != BLOCK_V) {
			PDEBUG(DFRAME, DEBUG_NOTICE, "Received Telegramm that is not used for distributed signaling, ignoring! (opcode %d = %s)\n", opcode, definition_opcode[opcode].message_name);
			return;
		}
		cnetz_receive_telegramm_spk_v(cnetz, &telegramm);
		break;
	default:
		;
	}
}

const char *cnetz_encode_telegramm(cnetz_t *cnetz)
{
	const telegramm_t *telegramm = NULL;
	uint8_t opcode;
	char *bits;

	switch (cnetz->dsp_mode) {
	case DSP_MODE_OGK:
		if (!cnetz->sched_r_m)
			telegramm = cnetz_transmit_telegramm_rufblock(cnetz);
		else
			telegramm = cnetz_transmit_telegramm_meldeblock(cnetz);
		break;
	case DSP_MODE_SPK_K:
		telegramm = cnetz_transmit_telegramm_spk_k(cnetz);
		break;
	case DSP_MODE_SPK_V:
		telegramm = cnetz_transmit_telegramm_spk_v(cnetz);
		break;
	default:
		;
	}

	opcode = telegramm->opcode;
	bits = assemble_telegramm(telegramm, (opcode != OPCODE_LR_R) && (opcode != OPCODE_MLR_M));
	bits = encode(bits);
	bits = interleave(bits);

	/* invert, if polarity of the cell is negative */
	if (si[cnetz->cell_nr].flip_polarity) {
		int i;

		for (i = 0; i < 184; i++)
			bits[i] ^= 1;
	}

	return bits;
}