summaryrefslogtreecommitdiffstats
path: root/openbsc/src/libbsc/abis_om2000.c
blob: 9eadff268630e61797d37ee267360101474322d6 (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
/* Ericsson RBS 2xxx GSM O&M (OM2000) messages on the A-bis interface
 * implemented based on protocol trace analysis, no formal documentation */

/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>

#include <arpa/inet.h>

#include <osmocore/msgb.h>
#include <osmocore/tlv.h>
#include <osmocore/talloc.h>
#include <osmocore/utils.h>

#include <openbsc/gsm_data.h>
#include <openbsc/debug.h>
#include <openbsc/abis_nm.h>
#include <openbsc/abis_om2000.h>
#include <openbsc/signal.h>
#include <openbsc/e1_input.h>

#define OM_ALLOC_SIZE		1024
#define OM_HEADROOM_SIZE	128

/* use following functions from abis_nm.c:
	* om2k_msgb_alloc()
	* abis_om2k_sendmsg()
 */

struct abis_om2k_hdr {
	struct abis_om_hdr om;
	uint16_t msg_type;
	struct abis_om2k_mo mo;
	uint8_t data[0];
} __attribute__ ((packed));

enum abis_om2k_msgtype {
	OM2K_MSGT_ABORT_SP_CMD			= 0x0000,
	OM2K_MSGT_ABORT_SP_COMPL		= 0x0002,
	OM2K_MSGT_ALARM_REP_ACK			= 0x0004,
	OM2K_MSGT_ALARM_REP_NACK		= 0x0005,
	OM2K_MSGT_ALARM_REP			= 0x0006,
	OM2K_MSGT_ALARM_STATUS_REQ		= 0x0008,
	OM2K_MSGT_ALARM_STATUS_REQ_ACK		= 0x000a,
	OM2K_MSGT_ALARM_STATUS_REQ_REJ		= 0x000b,
	OM2K_MSGT_ALARM_STATUS_RES_ACK		= 0x000c,
	OM2K_MSGT_ALARM_STATUS_RES_NACK		= 0x000d,
	OM2K_MSGT_ALARM_STATUS_RES		= 0x000e,
	OM2K_MSGT_CAL_TIME_RESP			= 0x0010,
	OM2K_MSGT_CAL_TIME_REJ			= 0x0011,
	OM2K_MSGT_CAL_TIME_REQ			= 0x0012,

	OM2K_MSGT_CON_CONF_REQ			= 0x0014,
	OM2K_MSGT_CON_CONF_REQ_ACK		= 0x0016,
	OM2K_MSGT_CON_CONF_REQ_REJ		= 0x0017,
	OM2K_MSGT_CON_CONF_RES_ACK		= 0x0018,
	OM2K_MSGT_CON_CONF_RES_NACK		= 0x0019,
	OM2K_MSGT_CON_CONF_RES			= 0x001a,

	OM2K_MSGT_CONNECT_CMD			= 0x001c,
	OM2K_MSGT_CONNECT_COMPL			= 0x001e,
	OM2K_MSGT_CONNECT_REJ			= 0x001f,

	OM2K_MSGT_DISABLE_REQ			= 0x0028,
	OM2K_MSGT_DISABLE_REQ_ACK		= 0x002a,
	OM2K_MSGT_DISABLE_REQ_REJ		= 0x002b,
	OM2K_MSGT_DISABLE_RES_ACK		= 0x002c,
	OM2K_MSGT_DISABLE_RES_NACK		= 0x002d,
	OM2K_MSGT_DISABLE_RES			= 0x002e,
	OM2K_MSGT_DISCONNECT_CMD		= 0x0030,
	OM2K_MSGT_DISCONNECT_COMPL		= 0x0032,
	OM2K_MSGT_DISCONNECT_REJ		= 0x0033,
	OM2K_MSGT_ENABLE_REQ			= 0x0034,
	OM2K_MSGT_ENABLE_REQ_ACK		= 0x0036,
	OM2K_MSGT_ENABLE_REQ_REJ		= 0x0037,
	OM2K_MSGT_ENABLE_RES_ACK		= 0x0038,
	OM2K_MSGT_ENABLE_RES_NACK		= 0x0039,
	OM2K_MSGT_ENABLE_RES			= 0x003a,

	OM2K_MSGT_FAULT_REP_ACK			= 0x0040,
	OM2K_MSGT_FAULT_REP_NACK		= 0x0041,
	OM2K_MSGT_FAULT_REP			= 0x0042,

	OM2K_MSGT_IS_CONF_REQ			= 0x0060,
	OM2K_MSGT_IS_CONF_REQ_ACK		= 0x0062,
	OM2K_MSGT_IS_CONF_REQ_REJ		= 0x0063,
	OM2K_MSGT_IS_CONF_RES_ACK		= 0x0064,
	OM2K_MSGT_IS_CONF_RES_NACK		= 0x0065,
	OM2K_MSGT_IS_CONF_RES			= 0x0066,

	OM2K_MSGT_OP_INFO			= 0x0074,
	OM2K_MSGT_OP_INFO_ACK			= 0x0076,
	OM2K_MSGT_OP_INFO_REJ			= 0x0077,
	OM2K_MSGT_RESET_CMD		 	= 0x0078,
	OM2K_MSGT_RESET_COMPL			= 0x007a,
	OM2K_MSGT_RESET_REJ			= 0x007b,
	OM2K_MSGT_RX_CONF_REQ			= 0x007c,
	OM2K_MSGT_RX_CONF_REQ_ACK		= 0x007e,
	OM2K_MSGT_RX_CONF_REQ_REJ		= 0x007f,
	OM2K_MSGT_RX_CONF_RES_ACK		= 0x0080,
	OM2K_MSGT_RX_CONF_RES_NACK		= 0x0081,
	OM2K_MSGT_RX_CONF_RES			= 0x0082,
	OM2K_MSGT_START_REQ			= 0x0084,
	OM2K_MSGT_START_REQ_ACK			= 0x0086,
	OM2K_MSGT_START_REQ_REJ			= 0x0087,
	OM2K_MSGT_START_RES_ACK			= 0x0088,
	OM2K_MSGT_START_RES_NACK		= 0x0089,
	OM2K_MSGT_START_RES			= 0x008a,
	OM2K_MSGT_STATUS_REQ			= 0x008c,
	OM2K_MSGT_STATUS_RESP			= 0x008e,
	OM2K_MSGT_STATUS_REJ			= 0x008f,

	OM2K_MSGT_TEST_REQ			= 0x0094,
	OM2K_MSGT_TEST_REQ_ACK			= 0x0096,
	OM2K_MSGT_TEST_REQ_REJ			= 0x0097,
	OM2K_MSGT_TEST_RES_ACK			= 0x0098,
	OM2K_MSGT_TEST_RES_NACK			= 0x0099,
	OM2K_MSGT_TEST_RES			= 0x009a,

	OM2K_MSGT_TF_CONF_REQ			= 0x00a0,
	OM2K_MSGT_TF_CONF_REQ_ACK		= 0x00a2,
	OM2K_MSGT_TF_CONF_REQ_REJ		= 0x00a3,
	OM2K_MSGT_TF_CONF_RES_ACK		= 0x00a4,
	OM2K_MSGT_TF_CONF_RES_NACK		= 0x00a5,
	OM2K_MSGT_TF_CONF_RES			= 0x00a6,
	OM2K_MSGT_TS_CONF_REQ			= 0x00a8,
	OM2K_MSGT_TS_CONF_REQ_ACK		= 0x00aa,
	OM2K_MSGT_TS_CONF_REQ_REJ		= 0x00ab,
	OM2K_MSGT_TS_CONF_RES_ACK		= 0x00ac,
	OM2K_MSGT_TS_CONF_RES_NACK		= 0x00ad,
	OM2K_MSGT_TS_CONF_RES			= 0x00ae,
	OM2K_MSGT_TX_CONF_REQ			= 0x00b0,
	OM2K_MSGT_TX_CONF_REQ_ACK		= 0x00b2,
	OM2K_MSGT_TX_CONF_REQ_REJ		= 0x00b3,
	OM2K_MSGT_TX_CONF_RES_ACK		= 0x00b4,
	OM2K_MSGT_TX_CONF_RES_NACK		= 0x00b5,
	OM2K_MSGT_TX_CONF_RES			= 0x00b6,

	OM2K_MSGT_NEGOT_REQ_ACK			= 0x0104,
	OM2K_MSGT_NEGOT_REQ_NACK		= 0x0105,
	OM2K_MSGT_NEGOT_REQ			= 0x0106,
};

enum abis_om2k_dei {
	OM2K_DEI_BCC				= 0x06,
	OM2K_DEI_BS_AG_BKS_RES			= 0x07,
	OM2K_DEI_BSIC				= 0x09,
	OM2K_DEI_BA_PA_MFRMS			= 0x0a,
	OM2K_DEI_CBCH_INDICATOR			= 0x0b,
	OM2K_DEI_CCCH_OPTIONS			= 0x0c,
	OM2K_DEI_CAL_TIME			= 0x0d,
	OM2K_DEI_COMBINATION			= 0x0f,
	OM2K_DEI_CON_CONN_LIST			= 0x10,
	OM2K_DEI_DRX_DEV_MAX			= 0x12,
	OM2K_DEI_END_LIST_NR			= 0x13,
	OM2K_DEI_EXT_COND_MAP_1			= 0x14,
	OM2K_DEI_EXT_COND_MAP_2			= 0x15,
	OM2K_DEI_FILLING_MARKER			= 0x1c,
	OM2K_DEI_FN_OFFSET			= 0x1d,
	OM2K_DEI_FREQ_LIST			= 0x1e,
	OM2K_DEI_FREQ_SPEC_RX			= 0x1f,
	OM2K_DEI_FREQ_SPEC_TX			= 0x20,
	OM2K_DEI_HSN				= 0x21,
	OM2K_DEI_ICM_INDICATOR			= 0x22,
	OM2K_DEI_INT_FAULT_MAP_1A		= 0x23,
	OM2K_DEI_INT_FAULT_MAP_1B		= 0x24,
	OM2K_DEI_INT_FAULT_MAP_2A		= 0x25,
	OM2K_DEI_INT_FAULT_MAP_2A_EXT		= 0x26,
	OM2K_DEI_IS_CONN_LIST			= 0x27,
	OM2K_DEI_LIST_NR			= 0x28,
	OM2K_DEI_LOCAL_ACCESS			= 0x2a,
	OM2K_DEI_MAIO				= 0x2b,
	OM2K_DEI_MO_STATE			= 0x2c,
	OM2K_DEI_NY1				= 0x2d,
	OM2K_DEI_OP_INFO			= 0x2e,
	OM2K_DEI_POWER				= 0x2f,
	OM2K_DEI_REASON_CODE			= 0x32,
	OM2K_DEI_RX_DIVERSITY			= 0x33,
	OM2K_DEI_RESULT_CODE			= 0x35,
	OM2K_DEI_T3105				= 0x38,
	OM2K_DEI_TF_MODE			= 0x3a,
	OM2K_DEI_TS_NR				= 0x3c,
	OM2K_DEI_TSC				= 0x3d,
	OM2K_DEI_BTS_VERSION			= 0x40,
	OM2K_DEI_OML_IWD_VERSION		= 0x41,
	OM2K_DEI_RSL_IWD_VERSION		= 0x42,
	OM2K_DEI_OML_FUNC_MAP_1			= 0x43,
	OM2K_DEI_OML_FUNC_MAP_2			= 0x44,
	OM2K_DEI_RSL_FUNC_MAP_1			= 0x45,
	OM2K_DEI_RSL_FUNC_MAP_2			= 0x46,
	OM2K_DEI_EXT_RANGE			= 0x47,
	OM2K_DEI_REQ_IND			= 0x48,
	OM2K_DEI_REPL_UNIT_MAP			= 0x50,
	OM2K_DEI_LSC				= 0x79,
	OM2K_DEI_LSC_FILT_TIME			= 0x7a,
	OM2K_DEI_CALL_SUPV_TIME			= 0x7b,
	OM2K_DEI_HW_INFO_SIG			= 0x84,
	OM2K_DEI_TTA				= 0x87,
	OM2K_DEI_CAPA_SIG			= 0x8a,
	OM2K_DEI_NEGOT_REC1			= 0x90,
	OM2K_DEI_NEGOT_REC2			= 0x91,
	OM2K_DEI_ENCR_ALG			= 0x92,
	OM2K_DEI_INTERF_REJ_COMB		= 0x94,
	OM2K_DEI_FS_OFFSET			= 0x98,
	OM2K_DEI_EXT_COND_MAP_2_EXT		= 0x9c,
};

const struct tlv_definition om2k_att_tlvdef = {
	.def = {
		[OM2K_DEI_BCC] =		{ TLV_TYPE_TV },
		[OM2K_DEI_BS_AG_BKS_RES] =	{ TLV_TYPE_TV },
		[OM2K_DEI_BSIC] =		{ TLV_TYPE_TV },
		[OM2K_DEI_BA_PA_MFRMS] =	{ TLV_TYPE_TV },
		[OM2K_DEI_CBCH_INDICATOR] =	{ TLV_TYPE_TV },
		[OM2K_DEI_INT_FAULT_MAP_1A] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_INT_FAULT_MAP_1B] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_INT_FAULT_MAP_2A] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_INT_FAULT_MAP_2A_EXT]={ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_CCCH_OPTIONS] =	{ TLV_TYPE_TV },
		[OM2K_DEI_CAL_TIME] =		{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_COMBINATION] =	{ TLV_TYPE_TV },
		[OM2K_DEI_CON_CONN_LIST] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_DRX_DEV_MAX] =	{ TLV_TYPE_TV },
		[OM2K_DEI_END_LIST_NR] =	{ TLV_TYPE_TV },
		[OM2K_DEI_EXT_COND_MAP_1] =	{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_EXT_COND_MAP_2] =	{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_FILLING_MARKER] =	{ TLV_TYPE_TV },
		[OM2K_DEI_FN_OFFSET] =		{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_FREQ_LIST] =		{ TLV_TYPE_TLV },
		[OM2K_DEI_FREQ_SPEC_RX] =	{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_FREQ_SPEC_TX] =	{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_HSN] =		{ TLV_TYPE_TV },
		[OM2K_DEI_ICM_INDICATOR] =	{ TLV_TYPE_TV },
		[OM2K_DEI_IS_CONN_LIST] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_LIST_NR] =		{ TLV_TYPE_TV },
		[OM2K_DEI_LOCAL_ACCESS] =	{ TLV_TYPE_TV },
		[OM2K_DEI_MAIO] =		{ TLV_TYPE_TV },
		[OM2K_DEI_MO_STATE] =		{ TLV_TYPE_TV },
		[OM2K_DEI_NY1] =		{ TLV_TYPE_TV },
		[OM2K_DEI_OP_INFO] =		{ TLV_TYPE_TV },
		[OM2K_DEI_POWER] =		{ TLV_TYPE_TV },
		[OM2K_DEI_REASON_CODE] =	{ TLV_TYPE_TV },
		[OM2K_DEI_RX_DIVERSITY] =	{ TLV_TYPE_TV },
		[OM2K_DEI_RESULT_CODE] =	{ TLV_TYPE_TV },
		[OM2K_DEI_T3105] = 		{ TLV_TYPE_TV },
		[OM2K_DEI_TF_MODE] =		{ TLV_TYPE_TV },
		[OM2K_DEI_TS_NR] =		{ TLV_TYPE_TV },
		[OM2K_DEI_TSC] =		{ TLV_TYPE_TV },
		[OM2K_DEI_BTS_VERSION] =	{ TLV_TYPE_FIXED, 12 },
		[OM2K_DEI_OML_IWD_VERSION] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_RSL_IWD_VERSION] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_OML_FUNC_MAP_1] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_OML_FUNC_MAP_2] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_RSL_FUNC_MAP_1] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_RSL_FUNC_MAP_2] =	{ TLV_TYPE_TLV },
		[OM2K_DEI_EXT_RANGE] =		{ TLV_TYPE_TV },
		[OM2K_DEI_REQ_IND] =		{ TLV_TYPE_TV },
		[OM2K_DEI_REPL_UNIT_MAP] =	{ TLV_TYPE_FIXED, 6 },
		[OM2K_DEI_LSC] =		{ TLV_TYPE_TV },
		[OM2K_DEI_LSC_FILT_TIME] =	{ TLV_TYPE_TV },
		[OM2K_DEI_CALL_SUPV_TIME] =	{ TLV_TYPE_TV },
		[OM2K_DEI_HW_INFO_SIG] =	{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_TTA] =		{ TLV_TYPE_TV },
		[OM2K_DEI_CAPA_SIG] =		{ TLV_TYPE_FIXED, 2 },
		[OM2K_DEI_NEGOT_REC1] =		{ TLV_TYPE_TLV },
		[OM2K_DEI_NEGOT_REC2] =		{ TLV_TYPE_TLV },
		[OM2K_DEI_ENCR_ALG] =		{ TLV_TYPE_TV },
		[OM2K_DEI_INTERF_REJ_COMB] =	{ TLV_TYPE_TV },
		[OM2K_DEI_FS_OFFSET] =		{ TLV_TYPE_FIXED, 5 },
		[OM2K_DEI_EXT_COND_MAP_2_EXT] = { TLV_TYPE_FIXED, 4 },
	},
};

static const struct value_string om2k_msgcode_vals[] = {
	{ 0x0000, "Abort SP Command" },
	{ 0x0002, "Abort SP Complete" },
	{ 0x0004, "Alarm Report ACK" },
	{ 0x0005, "Alarm Report NACK" },
	{ 0x0006, "Alarm Report" },
	{ 0x0008, "Alarm Status Request" },
	{ 0x000a, "Alarm Status Request Accept" },
	{ 0x000b, "Alarm Status Request Reject" },
	{ 0x000c, "Alarm Status Result ACK" },
	{ 0x000d, "Alarm Status Result NACK" },
	{ 0x000e, "Alarm Status Result" },
	{ 0x0010, "Calendar Time Response" },
	{ 0x0011, "Calendar Time Reject" },
	{ 0x0012, "Calendar Time Request" },
	{ 0x0014, "CON Configuration Request" },
	{ 0x0016, "CON Configuration Request Accept" },
	{ 0x0017, "CON Configuration Request Reject" },
	{ 0x0018, "CON Configuration Result ACK" },
	{ 0x0019, "CON Configuration Result NACK" },
	{ 0x001a, "CON Configuration Result" },
	{ 0x001c, "Connect Command" },
	{ 0x001e, "Connect Complete" },
	{ 0x001f, "Connect Reject" },
	{ 0x0028, "Disable Request" },
	{ 0x002a, "Disable Request Accept" },
	{ 0x002b, "Disable Request Reject" },
	{ 0x002c, "Disable Result ACK" },
	{ 0x002d, "Disable Result NACK" },
	{ 0x002e, "Disable Result" },
	{ 0x0030, "Disconnect Command" },
	{ 0x0032, "Disconnect Complete" },
	{ 0x0033, "Disconnect Reject" },
	{ 0x0034, "Enable Request" },
	{ 0x0036, "Enable Request Accept" },
	{ 0x0037, "Enable Request Reject" },
	{ 0x0038, "Enable Result ACK" },
	{ 0x0039, "Enable Result NACK" },
	{ 0x003a, "Enable Result" },
	{ 0x003c, "Escape Downlink Normal" },
	{ 0x003d, "Escape Downlink NACK" },
	{ 0x003e, "Escape Uplink Normal" },
	{ 0x003f, "Escape Uplink NACK" },
	{ 0x0040, "Fault Report ACK" },
	{ 0x0041, "Fault Report NACK" },
	{ 0x0042, "Fault Report" },
	{ 0x0044, "File Package End Command" },
	{ 0x0046, "File Package End Result" },
	{ 0x0047, "File Package End Reject" },
	{ 0x0048, "File Relation Request" },
	{ 0x004a, "File Relation Response" },
	{ 0x004b, "File Relation Request Reject" },
	{ 0x004c, "File Segment Transfer" },
	{ 0x004e, "File Segment Transfer Complete" },
	{ 0x004f, "File Segment Transfer Reject" },
	{ 0x0050, "HW Information Request" },
	{ 0x0052, "HW Information Request Accept" },
	{ 0x0053, "HW Information Request Reject" },
	{ 0x0054, "HW Information Result ACK" },
	{ 0x0055, "HW Information Result NACK" },
	{ 0x0056, "HW Information Result" },
	{ 0x0060, "IS Configuration Request" },
	{ 0x0062, "IS Configuration Request Accept" },
	{ 0x0063, "IS Configuration Request Reject" },
	{ 0x0064, "IS Configuration Result ACK" },
	{ 0x0065, "IS Configuration Result NACK" },
	{ 0x0066, "IS Configuration Result" },
	{ 0x0068, "Load Data End" },
	{ 0x006a, "Load Data End Result" },
	{ 0x006b, "Load Data End Reject" },
	{ 0x006c, "Load Data Init" },
	{ 0x006e, "Load Data Init Accept" },
	{ 0x006f, "Load Data Init Reject" },
	{ 0x0070, "Loop Control Command" },
	{ 0x0072, "Loop Control Complete" },
	{ 0x0073, "Loop Control Reject" },
	{ 0x0074, "Operational Information" },
	{ 0x0076, "Operational Information Accept" },
	{ 0x0077, "Operational Information Reject" },
	{ 0x0078, "Reset Command" },
	{ 0x007a, "Reset Complete" },
	{ 0x007b, "Reset Reject" },
	{ 0x007c, "RX Configuration Request" },
	{ 0x007e, "RX Configuration Request Accept" },
	{ 0x007f, "RX Configuration Request Reject" },
	{ 0x0080, "RX Configuration Result ACK" },
	{ 0x0081, "RX Configuration Result NACK" },
	{ 0x0082, "RX Configuration Result" },
	{ 0x0084, "Start Request" },
	{ 0x0086, "Start Request Accept" },
	{ 0x0087, "Start Request Reject" },
	{ 0x0088, "Start Result ACK" },
	{ 0x0089, "Start Result NACK" },
	{ 0x008a, "Start Result" },
	{ 0x008c, "Status Request" },
	{ 0x008e, "Status Response" },
	{ 0x008f, "Status Reject" },
	{ 0x0094, "Test Request" },
	{ 0x0096, "Test Request Accept" },
	{ 0x0097, "Test Request Reject" },
	{ 0x0098, "Test Result ACK" },
	{ 0x0099, "Test Result NACK" },
	{ 0x009a, "Test Result" },
	{ 0x00a0, "TF Configuration Request" },
	{ 0x00a2, "TF Configuration Request Accept" },
	{ 0x00a3, "TF Configuration Request Reject" },
	{ 0x00a4, "TF Configuration Result ACK" },
	{ 0x00a5, "TF Configuration Result NACK" },
	{ 0x00a6, "TF Configuration Result" },
	{ 0x00a8, "TS Configuration Request" },
	{ 0x00aa, "TS Configuration Request Accept" },
	{ 0x00ab, "TS Configuration Request Reject" },
	{ 0x00ac, "TS Configuration Result ACK" },
	{ 0x00ad, "TS Configuration Result NACK" },
	{ 0x00ae, "TS Configuration Result" },
	{ 0x00b0, "TX Configuration Request" },
	{ 0x00b2, "TX Configuration Request Accept" },
	{ 0x00b3, "TX Configuration Request Reject" },
	{ 0x00b4, "TX Configuration Result ACK" },
	{ 0x00b5, "TX Configuration Result NACK" },
	{ 0x00b6, "TX Configuration Result" },
	{ 0x00bc, "DIP Alarm Report ACK" },
	{ 0x00bd, "DIP Alarm Report NACK" },
	{ 0x00be, "DIP Alarm Report" },
	{ 0x00c0, "DIP Alarm Status Request" },
	{ 0x00c2, "DIP Alarm Status Response" },
	{ 0x00c3, "DIP Alarm Status Reject" },
	{ 0x00c4, "DIP Quality Report I ACK" },
	{ 0x00c5, "DIP Quality Report I NACK" },
	{ 0x00c6, "DIP Quality Report I" },
	{ 0x00c8, "DIP Quality Report II ACK" },
	{ 0x00c9, "DIP Quality Report II NACK" },
	{ 0x00ca, "DIP Quality Report II" },
	{ 0x00dc, "DP Configuration Request" },
	{ 0x00de, "DP Configuration Request Accept" },
	{ 0x00df, "DP Configuration Request Reject" },
	{ 0x00e0, "DP Configuration Result ACK" },
	{ 0x00e1, "DP Configuration Result NACK" },
	{ 0x00e2, "DP Configuration Result" },
	{ 0x00e4, "Capabilities HW Info Report ACK" },
	{ 0x00e5, "Capabilities HW Info Report NACK" },
	{ 0x00e6, "Capabilities HW Info Report" },
	{ 0x00e8, "Capabilities Request" },
	{ 0x00ea, "Capabilities Request Accept" },
	{ 0x00eb, "Capabilities Request Reject" },
	{ 0x00ec, "Capabilities Result ACK" },
	{ 0x00ed, "Capabilities Result NACK" },
	{ 0x00ee, "Capabilities Result" },
	{ 0x00f0, "FM Configuration Request" },
	{ 0x00f2, "FM Configuration Request Accept" },
	{ 0x00f3, "FM Configuration Request Reject" },
	{ 0x00f4, "FM Configuration Result ACK" },
	{ 0x00f5, "FM Configuration Result NACK" },
	{ 0x00f6, "FM Configuration Result" },
	{ 0x00f8, "FM Report Request" },
	{ 0x00fa, "FM Report Response" },
	{ 0x00fb, "FM Report Reject" },
	{ 0x00fc, "FM Start Command" },
	{ 0x00fe, "FM Start Complete" },
	{ 0x00ff, "FM Start Reject" },
	{ 0x0100, "FM Stop Command" },
	{ 0x0102, "FM Stop Complete" },
	{ 0x0103, "FM Stop Reject" },
	{ 0x0104, "Negotiation Request ACK" },
	{ 0x0105, "Negotiation Request NACK" },
	{ 0x0106, "Negotiation Request" },
	{ 0x0108, "BTS Initiated Request ACK" },
	{ 0x0109, "BTS Initiated Request NACK" },
	{ 0x010a, "BTS Initiated Request" },
	{ 0x010c, "Radio Channels Release Command" },
	{ 0x010e, "Radio Channels Release Complete" },
	{ 0x010f, "Radio Channels Release Reject" },
	{ 0x0118, "Feature Control Command" },
	{ 0x011a, "Feature Control Complete" },
	{ 0x011b, "Feature Control Reject" },

	{ 0, NULL }
};

/* TS 12.21 Section 9.4: Attributes */
static const struct value_string om2k_attr_vals[] = {
	{ 0x00, "Accordance indication" },
	{ 0x01, "Alarm Id" },
	{ 0x02, "Alarm Data" },
	{ 0x03, "Alarm Severity" },
	{ 0x04, "Alarm Status" },
	{ 0x05, "Alarm Status Type" },
	{ 0x06, "BCC" },
	{ 0x07, "BS_AG_BKS_RES" },
	{ 0x09, "BSIC" },
	{ 0x0a, "BA_PA_MFRMS" },
	{ 0x0b, "CBCH Indicator" },
	{ 0x0c, "CCCH Options" },
	{ 0x0d, "Calendar Time" },
	{ 0x0f, "Channel Combination" },
	{ 0x10, "CON Connection List" },
	{ 0x11, "Data End Indication" },
	{ 0x12, "DRX_DEV_MAX" },
	{ 0x13, "End List Number" },
	{ 0x14, "External Condition Map Class 1" },
	{ 0x15, "External Condition Map Class 2" },
	{ 0x16, "File Relation Indication" },
	{ 0x17, "File Revision" },
	{ 0x18, "File Segment Data" },
	{ 0x19, "File Segment Length" },
	{ 0x1a, "File Segment Sequence Number" },
	{ 0x1b, "File Size" },
	{ 0x1c, "Filling Marker" },
	{ 0x1d, "FN Offset" },
	{ 0x1e, "Frequency List" },
	{ 0x1f, "Frequency Specifier RX" },
	{ 0x20, "Frequency Specifier TX" },
	{ 0x21, "HSN" },
	{ 0x22, "ICM Indicator" },
	{ 0x23, "Internal Fault Map Class 1A" },
	{ 0x24, "Internal Fault Map Class 1B" },
	{ 0x25, "Internal Fault Map Class 2A" },
	{ 0x26, "Internal Fault Map Class 2A Extension" },
	{ 0x27, "IS Connection List" },
	{ 0x28, "List Number" },
	{ 0x29, "File Package State Indication" },
	{ 0x2a, "Local Access State" },
	{ 0x2b, "MAIO" },
	{ 0x2c, "MO State" },
	{ 0x2d, "Ny1" },
	{ 0x2e, "Operational Information" },
	{ 0x2f, "Power" },
	{ 0x30, "RU Position Data" },
	{ 0x31, "Protocol Error" },
	{ 0x32, "Reason Code" },
	{ 0x33, "Receiver Diversity" },
	{ 0x34, "Replacement Unit Map" },
	{ 0x35, "Result Code" },
	{ 0x36, "RU Revision Data" },
	{ 0x38, "T3105" },
	{ 0x39, "Test Loop Setting" },
	{ 0x3a, "TF Mode" },
	{ 0x3b, "TF Compensation Value" },
	{ 0x3c, "Time Slot Number" },
	{ 0x3d, "TSC" },
	{ 0x3e, "RU Logical Id" },
	{ 0x3f, "RU Serial Number Data" },
	{ 0x40, "BTS Version" },
	{ 0x41, "OML IWD Version" },
	{ 0x42, "RWL IWD Version" },
	{ 0x43, "OML Function Map 1" },
	{ 0x44, "OML Function Map 2" },
	{ 0x45, "RSL Function Map 1" },
	{ 0x46, "RSL Function Map 2" },
	{ 0x47, "Extended Range Indicator" },
	{ 0x48, "Request Indicators" },
	{ 0x49, "DIP Alarm Condition Map" },
	{ 0x4a, "ES Incoming" },
	{ 0x4b, "ES Outgoing" },
	{ 0x4e, "SES Incoming" },
	{ 0x4f, "SES Outgoing" },
	{ 0x50, "Replacement Unit Map Extension" },
	{ 0x52, "UAS Incoming" },
	{ 0x53, "UAS Outgoing" },
	{ 0x58, "DF Incoming" },
	{ 0x5a, "DF Outgoing" },
	{ 0x5c, "SF" },
	{ 0x60, "S Bits Setting" },
	{ 0x61, "CRC-4 Use Option" },
	{ 0x62, "T Parameter" },
	{ 0x63, "N Parameter" },
	{ 0x64, "N1 Parameter" },
	{ 0x65, "N3 Parameter" },
	{ 0x66, "N4 Parameter" },
	{ 0x67, "P Parameter" },
	{ 0x68, "Q Parameter" },
	{ 0x69, "BI_Q1" },
	{ 0x6a, "BI_Q2" },
	{ 0x74, "ICM Boundary Parameters" },
	{ 0x77, "AFT" },
	{ 0x78, "AFT RAI" },
	{ 0x79, "Link Supervision Control" },
	{ 0x7a, "Link Supervision Filtering Time" },
	{ 0x7b, "Call Supervision Time" },
	{ 0x7c, "Interval Length UAS Incoming" },
	{ 0x7d, "Interval Length UAS Outgoing" },
	{ 0x7e, "ICM Channel Rate" },
	{ 0x7f, "Attribute Identifier" },
	{ 0x80, "FM Frequency List" },
	{ 0x81, "FM Frequency Report" },
	{ 0x82, "FM Percentile" },
	{ 0x83, "FM Clear Indication" },
	{ 0x84, "HW Info Signature" },
	{ 0x85, "MO Record" },
	{ 0x86, "TF Synchronisation Source" },
	{ 0x87, "TTA" },
	{ 0x88, "End Segment Number" },
	{ 0x89, "Segment Number" },
	{ 0x8a, "Capabilities Signature" },
	{ 0x8c, "File Relation List" },
	{ 0x90, "Negotiation Record I" },
	{ 0x91, "Negotiation Record II" },
	{ 0x92, "Encryption Algorithm" },
	{ 0x94, "Interference Rejection Combining" },
	{ 0x95, "Dedication Information" },
	{ 0x97, "Feature Code" },
	{ 0x98, "FS Offset" },
	{ 0x99, "ESB Timeslot" },
	{ 0x9a, "Master TG Instance" },
	{ 0x9b, "Master TX Chain Delay" },
	{ 0x9c, "External Condition Class 2 Extension" },
	{ 0x9d, "TSs MO State" },
	{ 0, NULL }
};

const struct value_string om2k_mo_class_short_vals[] = {
	{ 0x01, "TRXC" },
	{ 0x03, "TS" },
	{ 0x04, "TF" },
	{ 0x05, "IS" },
	{ 0x06, "CON" },
	{ 0x07, "DP" },
	{ 0x0a, "CF" },
	{ 0x0b, "TX" },
	{ 0x0c, "RX" },
	{ 0, NULL }
};

static struct msgb *om2k_msgb_alloc(void)
{
	return msgb_alloc_headroom(OM_ALLOC_SIZE, OM_HEADROOM_SIZE,
				   "OM2000");
}

static int abis_om2k_tlv_parse(struct tlv_parsed *tp, const u_int8_t *buf, int len)
{
	return tlv_parse(tp, &om2k_att_tlvdef, buf, len, 0, 0);
}

static char *om2k_mo_name(const struct abis_om2k_mo *mo)
{
	static char mo_buf[64];

	memset(mo_buf, 0, sizeof(mo_buf));
	snprintf(mo_buf, sizeof(mo_buf), "%s/%02x/%02x/%02x",
		 get_value_string(om2k_mo_class_short_vals, mo->class),
		 mo->bts, mo->assoc_so, mo->inst);
	return mo_buf;
}

static int abis_om2k_sendmsg(struct gsm_bts *bts, struct msgb *msg)
{
	struct abis_om2k_hdr *o2h;
	int to_trx_oml;

	msg->l2h = msg->data;
	o2h = (struct abis_om2k_hdr *) msg->l2h;

	/* Compute the length in the OML header */
	o2h->om.length = 6 + msgb_l2len(msg)-sizeof(*o2h);

	switch (o2h->mo.class) {
	case OM2K_MO_CLS_TRXC:
	case OM2K_MO_CLS_TX:
	case OM2K_MO_CLS_RX:
		/* Route through per-TRX OML Link to the appropriate TRX */
		to_trx_oml = 1;
		msg->trx = gsm_bts_trx_by_nr(bts, o2h->mo.inst);
		if (!msg->trx) {
			LOGP(DNM, LOGL_ERROR, "MO=%s Tx Dropping msg to "
				"non-existing TRX\n", om2k_mo_name(&o2h->mo));
			return -ENODEV;
		}
		break;
	case OM2K_MO_CLS_TS:
		/* Route through per-TRX OML Link to the appropriate TRX */
		to_trx_oml = 1;
		msg->trx = gsm_bts_trx_by_nr(bts, o2h->mo.assoc_so);
		if (!msg->trx) {
			LOGP(DNM, LOGL_ERROR, "MO=%s Tx Dropping msg to "
				"non-existing TRX\n", om2k_mo_name(&o2h->mo));
			return -ENODEV;
		}
		break;
	default:
		/* Route through the IXU/DXU OML Link */
		msg->trx = bts->c0;
		to_trx_oml = 0;
		break;
	}

	return _abis_nm_sendmsg(msg, to_trx_oml);
}

static void fill_om2k_hdr(struct abis_om2k_hdr *o2h, const struct abis_om2k_mo *mo,
			  uint16_t msg_type)
{
	o2h->om.mdisc = ABIS_OM_MDISC_FOM;
	o2h->om.placement = ABIS_OM_PLACEMENT_ONLY;
	o2h->om.sequence = 0;
	/* We fill o2h->om.length later during om2k_sendmsg() */
	o2h->msg_type = htons(msg_type);
	memcpy(&o2h->mo, mo, sizeof(o2h->mo));
}

const struct abis_om2k_mo om2k_mo_cf = { OM2K_MO_CLS_CF, 0, 0xFF, 0 };
const struct abis_om2k_mo om2k_mo_is = { OM2K_MO_CLS_IS, 0, 0xFF, 0 };
const struct abis_om2k_mo om2k_mo_con = { OM2K_MO_CLS_CON, 0, 0xFF, 0 };
const struct abis_om2k_mo om2k_mo_tf = { OM2K_MO_CLS_TF, 0, 0xFF, 0 };

static int abis_om2k_cal_time_resp(struct gsm_bts *bts)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;
	time_t tm_t;
	struct tm *tm;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &om2k_mo_cf, OM2K_MSGT_CAL_TIME_RESP);

	tm_t = time(NULL);
	tm = localtime(&tm_t);

	msgb_put_u8(msg, OM2K_DEI_CAL_TIME);
	msgb_put_u8(msg, tm->tm_year % 100);
	msgb_put_u8(msg, tm->tm_mon + 1);
	msgb_put_u8(msg, tm->tm_mday);
	msgb_put_u8(msg, tm->tm_hour);
	msgb_put_u8(msg, tm->tm_min);
	msgb_put_u8(msg, tm->tm_sec);

	return abis_om2k_sendmsg(bts, msg);
}

static int abis_om2k_tx_simple(struct gsm_bts *bts, const struct abis_om2k_mo *mo,
				uint8_t msg_type)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, mo, msg_type);

	DEBUGP(DNM, "Tx MO=%s %s\n", om2k_mo_name(mo),
		get_value_string(om2k_msgcode_vals, msg_type));

	return abis_om2k_sendmsg(bts, msg);
}

int abis_om2k_tx_reset_cmd(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_RESET_CMD);
}

int abis_om2k_tx_start_req(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_START_REQ);
}

int abis_om2k_tx_status_req(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_STATUS_REQ);
}

int abis_om2k_tx_connect_cmd(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_CONNECT_CMD);
}

int abis_om2k_tx_disconnect_cmd(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_DISCONNECT_CMD);
}

int abis_om2k_tx_test_req(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_TEST_REQ);
}

int abis_om2k_tx_enable_req(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_ENABLE_REQ);
}

int abis_om2k_tx_disable_req(struct gsm_bts *bts, const struct abis_om2k_mo *mo)
{
	return abis_om2k_tx_simple(bts, mo, OM2K_MSGT_DISABLE_REQ);
}

int abis_om2k_tx_op_info(struct gsm_bts *bts, const struct abis_om2k_mo *mo,
			 uint8_t operational)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, mo, OM2K_MSGT_OP_INFO);

	msgb_tv_put(msg, OM2K_DEI_OP_INFO, operational);

	DEBUGP(DNM, "Tx MO=%s %s\n", om2k_mo_name(mo),
		get_value_string(om2k_msgcode_vals, OM2K_MSGT_OP_INFO));

	return abis_om2k_sendmsg(bts, msg);
}

int abis_om2k_tx_is_conf_req(struct gsm_bts *bts, struct om2k_is_conn_grp *cg,
			     unsigned int num_cg )
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &om2k_mo_is, OM2K_MSGT_IS_CONF_REQ);

	msgb_tv_put(msg, OM2K_DEI_LIST_NR, 1);
	msgb_tv_put(msg, OM2K_DEI_END_LIST_NR, 1);

	msgb_tlv_put(msg, OM2K_DEI_IS_CONN_LIST,
		     num_cg * sizeof(*cg), (uint8_t *)cg);

	return abis_om2k_sendmsg(bts, msg);
}

int abis_om2k_tx_con_conf_req(struct gsm_bts *bts, uint8_t *data,
			     unsigned int len)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &om2k_mo_con, OM2K_MSGT_CON_CONF_REQ);

	msgb_tv_put(msg, OM2K_DEI_LIST_NR, 1);
	msgb_tv_put(msg, OM2K_DEI_END_LIST_NR, 1);

	msgb_tlv_put(msg, OM2K_DEI_CON_CONN_LIST, len, data);

	return abis_om2k_sendmsg(bts, msg);
}

static void om2k_trx_to_mo(struct abis_om2k_mo *mo,
			   const struct gsm_bts_trx *trx,
			   enum abis_om2k_mo_cls cls)
{
	mo->class = cls;
	mo->bts = 0;
	mo->inst = trx->nr;
	mo->assoc_so = 255;
}

static void om2k_ts_to_mo(struct abis_om2k_mo *mo,
			  const struct gsm_bts_trx_ts *ts)
{
	mo->class = OM2K_MO_CLS_TS;
	mo->bts = 0;
	mo->inst = ts->nr;
	mo->assoc_so = ts->trx->nr;
}

/* Configure a Receiver MO */
int abis_om2k_tx_rx_conf_req(struct gsm_bts_trx *trx)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;
	struct abis_om2k_mo mo;

	om2k_trx_to_mo(&mo, trx, OM2K_MO_CLS_RX);

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &mo, OM2K_MSGT_RX_CONF_REQ);

	msgb_tv16_put(msg, OM2K_DEI_FREQ_SPEC_RX, trx->arfcn);
	msgb_tv_put(msg, OM2K_DEI_RX_DIVERSITY, 0x03); /* A+B */

	return abis_om2k_sendmsg(trx->bts, msg);
}

/* Configure a Transmitter MO */
int abis_om2k_tx_tx_conf_req(struct gsm_bts_trx *trx)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;
	struct abis_om2k_mo mo;

	om2k_trx_to_mo(&mo, trx, OM2K_MO_CLS_TX);

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &mo, OM2K_MSGT_TX_CONF_REQ);

	msgb_tv16_put(msg, OM2K_DEI_FREQ_SPEC_TX, trx->arfcn);
	msgb_tv_put(msg, OM2K_DEI_POWER, trx->nominal_power-trx->max_power_red);
	msgb_tv_put(msg, OM2K_DEI_FILLING_MARKER, 0);	/* Filling enabled */
	msgb_tv_put(msg, OM2K_DEI_BCC, trx->bts->bsic & 0x7);
	/* Dedication Information is optional */

	return abis_om2k_sendmsg(trx->bts, msg);
}

enum abis_om2k_tf_mode {
	OM2K_TF_MODE_MASTER	= 0x00,
	OM2K_TF_MODE_STANDALONE	= 0x01,
	OM2K_TF_MODE_SLAVE	= 0x02,
	OM2K_TF_MODE_UNDEFINED	= 0xff,
};

static const uint8_t fs_offset_undef[5] = { 0xff, 0xff, 0xff, 0xff, 0xff };

int abis_om2k_tx_tf_conf_req(struct gsm_bts *bts)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &om2k_mo_tf, OM2K_MSGT_TF_CONF_REQ);

	msgb_tv_put(msg, OM2K_DEI_TF_MODE, OM2K_TF_MODE_STANDALONE);
	msgb_tv_fixed_put(msg, OM2K_DEI_FS_OFFSET,
			  sizeof(fs_offset_undef), fs_offset_undef);

	return abis_om2k_sendmsg(bts, msg);
}

static uint8_t pchan2comb(enum gsm_phys_chan_config pchan)
{
	switch (pchan) {
	case GSM_PCHAN_CCCH:
		return 4;
	case GSM_PCHAN_CCCH_SDCCH4:
		return 5;
	case GSM_PCHAN_SDCCH8_SACCH8C:
		return 3;
	case GSM_PCHAN_TCH_F:
	case GSM_PCHAN_TCH_H:
	case GSM_PCHAN_PDCH:
	case GSM_PCHAN_TCH_F_PDCH:
		return 8;
	default:
		return 0;
	}
}

static int put_freq_list(uint8_t *buf, uint16_t arfcn)
{
	buf[0] = 0x00; /* TX/RX address */
	buf[1] = (arfcn >> 8);
	buf[2] = (arfcn & 0xff);

	return 3;
}

/* Compute a frequency list in OM2000 fomrmat */
static int om2k_gen_freq_list(uint8_t *list, struct gsm_bts_trx_ts *ts)
{
	uint8_t *cur = list;
	int len;

	if (ts->hopping.enabled) {
		unsigned int i;
		for (i = 0; i < ts->hopping.arfcns.data_len*8; i++) {
			if (bitvec_get_bit_pos(&ts->hopping.arfcns, i))
				cur += put_freq_list(cur, i);
		}
	} else
		cur += put_freq_list(cur, ts->trx->arfcn);

	len = cur - list;

	return len;
}

int abis_om2k_tx_ts_conf_req(struct gsm_bts_trx_ts *ts)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;
	struct abis_om2k_mo mo;
	uint8_t freq_list[64*3]; /* BA max size: 64 ARFCN */
	int freq_list_len;

	om2k_ts_to_mo(&mo, ts);

	memset(freq_list, 0, sizeof(freq_list));
	freq_list_len = om2k_gen_freq_list(freq_list, ts);
	if (freq_list_len < 0)
		return freq_list_len;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, &mo, OM2K_MSGT_TS_CONF_REQ);

	msgb_tv_put(msg, OM2K_DEI_COMBINATION, pchan2comb(ts->pchan));
	msgb_tv_put(msg, OM2K_DEI_TS_NR, ts->nr);
	msgb_tlv_put(msg, OM2K_DEI_FREQ_LIST, freq_list_len, freq_list);
	msgb_tv_put(msg, OM2K_DEI_HSN, ts->hopping.hsn);
	msgb_tv_put(msg, OM2K_DEI_MAIO, ts->hopping.maio);
	msgb_tv_put(msg, OM2K_DEI_BSIC, ts->trx->bts->bsic);
	msgb_tv_put(msg, OM2K_DEI_RX_DIVERSITY, 0x03); /* A+B */
	msgb_tv16_put(msg, OM2K_DEI_FN_OFFSET, 0);
	msgb_tv_put(msg, OM2K_DEI_EXT_RANGE, 0); /* Off */
	/* Optional: Interference Rejection Combining */
	msgb_tv_put(msg, OM2K_DEI_INTERF_REJ_COMB, 0x00);
	switch (ts->pchan) {
	case GSM_PCHAN_CCCH:
	case GSM_PCHAN_CCCH_SDCCH4:
		msgb_tv_put(msg, OM2K_DEI_BA_PA_MFRMS, 0x06);
		msgb_tv_put(msg, OM2K_DEI_BS_AG_BKS_RES, 0x01);
		msgb_tv_put(msg, OM2K_DEI_DRX_DEV_MAX, 0x05);
		/* Repeat Paging/IMM.ASS: True, Allow Paging Type 3: Yes, Page for 5 seconds (default) */
		msgb_tv_put(msg, OM2K_DEI_CCCH_OPTIONS, 0x01);
		break;
	case GSM_PCHAN_SDCCH8_SACCH8C:
		msgb_tv_put(msg, OM2K_DEI_T3105, 0x04);
		msgb_tv_put(msg, OM2K_DEI_NY1, 35);
		msgb_tv_put(msg, OM2K_DEI_CBCH_INDICATOR, 0);
		msgb_tv_put(msg, OM2K_DEI_TSC, ts->trx->bts->tsc);
		/* Disable RF RESOURCE INDICATION on idle channels */
		msgb_tv_put(msg, OM2K_DEI_ICM_INDICATOR, 0);
		break;
	default:
		msgb_tv_put(msg, OM2K_DEI_T3105, 0x04);
		msgb_tv_put(msg, OM2K_DEI_NY1, 35);
		msgb_tv_put(msg, OM2K_DEI_CBCH_INDICATOR, 0);
		msgb_tv_put(msg, OM2K_DEI_TSC, ts->trx->bts->tsc);
		/* Disable RF RESOURCE INDICATION on idle channels */
		msgb_tv_put(msg, OM2K_DEI_ICM_INDICATOR, 0);
		msgb_tv_put(msg, OM2K_DEI_TTA, 10); /* Timer for Time Alignment */
		msgb_tv_put(msg, OM2K_DEI_LSC, 1); /* enabled */
		msgb_tv_put(msg, OM2K_DEI_LSC_FILT_TIME, 160);	/* units of 100ms */
		msgb_tv_put(msg, OM2K_DEI_CALL_SUPV_TIME, 8);
		msgb_tv_put(msg, OM2K_DEI_ENCR_ALG, 0x00);
		/* FIXME: do we need 9e/9f/a0? */
		break;
	}

	return abis_om2k_sendmsg(ts->trx->bts, msg);
}

static int abis_om2k_tx_negot_req_ack(struct gsm_bts *bts, const struct abis_om2k_mo *mo,
				      uint8_t *data, unsigned int len)
{
	struct msgb *msg = om2k_msgb_alloc();
	struct abis_om2k_hdr *o2k;

	o2k = (struct abis_om2k_hdr *) msgb_put(msg, sizeof(*o2k));
	fill_om2k_hdr(o2k, mo, OM2K_MSGT_NEGOT_REQ_ACK);

	msgb_tlv_put(msg, OM2K_DEI_NEGOT_REC2, len, data);

	DEBUGP(DNM, "Tx MO=%s %s\n", om2k_mo_name(mo),
		get_value_string(om2k_msgcode_vals, OM2K_MSGT_NEGOT_REQ_ACK));

	return abis_om2k_sendmsg(bts, msg);
}

struct iwd_version {
	uint8_t gen_char[3+1];
	uint8_t rev_char[3+1];
};

struct iwd_type {
	uint8_t num_vers;
	struct iwd_version v[8];
};

static int om2k_rx_negot_req(struct msgb *msg)
{
	struct abis_om2k_hdr *o2h = msgb_l2(msg);
	struct iwd_type iwd_types[16];
	uint8_t num_iwd_types = o2h->data[2];
	uint8_t *cur = o2h->data+3;
	unsigned int i, v;

	uint8_t out_buf[1024];
	uint8_t *out_cur = out_buf+1;
	uint8_t out_num_types = 0;

	memset(iwd_types, 0, sizeof(iwd_types));

	/* Parse the RBS-supported IWD versions into iwd_types array */
	for (i = 0; i < num_iwd_types; i++) {
		uint8_t num_versions = *cur++;
		uint8_t iwd_type = *cur++;

		iwd_types[iwd_type].num_vers = num_versions;

		for (v = 0; v < num_versions; v++) {
			struct iwd_version *iwd_v = &iwd_types[iwd_type].v[v];

			memcpy(iwd_v->gen_char, cur, 3);
			cur += 3;
			memcpy(iwd_v->rev_char, cur, 3);
			cur += 3;

			DEBUGP(DNM, "\tIWD Type %u Gen %s Rev %s\n", iwd_type,
				iwd_v->gen_char, iwd_v->rev_char);
		}
	}

	/* Select the last version for each IWD type */
	for (i = 0; i < ARRAY_SIZE(iwd_types); i++) {
		struct iwd_type *type = &iwd_types[i];
		struct iwd_version *last_v;

		if (type->num_vers == 0)
			continue;

		out_num_types++;

		last_v = &type->v[type->num_vers-1];

		*out_cur++ = i;
		memcpy(out_cur, last_v->gen_char, 3);
		out_cur += 3;
		memcpy(out_cur, last_v->rev_char, 3);
		out_cur += 3;
	}

	out_buf[0] = out_num_types;

	return abis_om2k_tx_negot_req_ack(msg->trx->bts, &o2h->mo, out_buf, out_cur - out_buf);
}

static int om2k_rx_start_res(struct msgb *msg)
{
	struct abis_om2k_hdr *o2h = msgb_l2(msg);
	int rc;

	rc = abis_om2k_tx_simple(msg->trx->bts, &o2h->mo, OM2K_MSGT_START_RES_ACK);
	rc = abis_om2k_tx_op_info(msg->trx->bts, &o2h->mo, 1);

	return rc;
}

static int om2k_rx_op_info_ack(struct msgb *msg)
{
	struct abis_om2k_hdr *o2h = msgb_l2(msg);

	/* FIXME: update Operational state in our structures */

	return 0;
}

const struct value_string om2k_result_strings[] = {
	{ 0x02, "Wrong state or out of sequence" },
	{ 0x03, "File error" },
	{ 0x04, "Fault, unspecified" },
	{ 0x05, "Tuning fault" },
	{ 0x06, "Protocol error" },
	{ 0x07, "MO not connected" },
	{ 0x08, "Parameter error" },
	{ 0x09, "Optional function not supported" },
	{ 0x0a, "Local access state LOCALLY DISCONNECTED" },
	{ 0, NULL }
};

static int om2k_rx_nack(struct msgb *msg)
{
	struct abis_om2k_hdr *o2h = msgb_l2(msg);
	uint16_t msg_type = ntohs(o2h->msg_type);
	struct tlv_parsed tp;

	LOGP(DNM, LOGL_ERROR, "Rx MO=%s %s", om2k_mo_name(&o2h->mo),
		get_value_string(om2k_msgcode_vals, msg_type));

	abis_om2k_tlv_parse(&tp, o2h->data, o2h->om.length - 6);
	if (TLVP_PRESENT(&tp, OM2K_DEI_REASON_CODE))
		LOGPC(DNM, LOGL_ERROR, ", Reason 0x%02x",
			*TLVP_VAL(&tp, OM2K_DEI_REASON_CODE));

	if (TLVP_PRESENT(&tp, OM2K_DEI_RESULT_CODE))
		LOGPC(DNM, LOGL_ERROR, ", Result %s",
			get_value_string(om2k_result_strings,
					 *TLVP_VAL(&tp, OM2K_DEI_RESULT_CODE)));
	LOGPC(DNM, LOGL_ERROR, "\n");

	return 0;
}

int abis_om2k_rcvmsg(struct msgb *msg)
{
	struct gsm_bts *bts = msg->trx->bts;
	struct abis_om2k_hdr *o2h = msgb_l2(msg);
	struct abis_om_hdr *oh = &o2h->om;
	uint16_t msg_type = ntohs(o2h->msg_type);
	int rc = 0;

	/* Various consistency checks */
	if (oh->placement != ABIS_OM_PLACEMENT_ONLY) {
		LOGP(DNM, LOGL_ERROR, "ABIS OML placement 0x%x not supported\n",
			oh->placement);
		if (oh->placement != ABIS_OM_PLACEMENT_FIRST)
			return -EINVAL;
	}
	if (oh->sequence != 0) {
		LOGP(DNM, LOGL_ERROR, "ABIS OML sequence 0x%x != 0x00\n",
			oh->sequence);
		return -EINVAL;
	}

	msg->l3h = (unsigned char *)o2h + sizeof(*o2h);

	if (oh->mdisc != ABIS_OM_MDISC_FOM) {
		LOGP(DNM, LOGL_ERROR, "unknown ABIS OM2000 message discriminator 0x%x\n",
			oh->mdisc);
		return -EINVAL;
	}

	DEBUGP(DNM, "Rx MO=%s %s (%s)\n", om2k_mo_name(&o2h->mo),
		get_value_string(om2k_msgcode_vals, msg_type),
		hexdump(msg->l2h, msgb_l2len(msg)));

	switch (msg_type) {
	case OM2K_MSGT_CAL_TIME_REQ:
		rc = abis_om2k_cal_time_resp(bts);
		break;
	case OM2K_MSGT_FAULT_REP:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_FAULT_REP_ACK);
		break;
	case OM2K_MSGT_NEGOT_REQ:
		rc = om2k_rx_negot_req(msg);
		break;
	case OM2K_MSGT_START_RES:
		rc = om2k_rx_start_res(msg);
		break;
	case OM2K_MSGT_OP_INFO_ACK:
		rc = om2k_rx_op_info_ack(msg);
		break;
	case OM2K_MSGT_IS_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_IS_CONF_RES_ACK);
		break;
	case OM2K_MSGT_CON_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_CON_CONF_RES_ACK);
		break;
	case OM2K_MSGT_TX_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_TX_CONF_RES_ACK);
		break;
	case OM2K_MSGT_RX_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_RX_CONF_RES_ACK);
		break;
	case OM2K_MSGT_TS_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_TS_CONF_RES_ACK);
		break;
	case OM2K_MSGT_TF_CONF_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_TF_CONF_RES_ACK);
		break;
	case OM2K_MSGT_CONNECT_COMPL:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_RESET_CMD);
		break;
	case OM2K_MSGT_RESET_COMPL:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_START_REQ);
		break;
	case OM2K_MSGT_ENABLE_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_ENABLE_RES_ACK);
		break;
	case OM2K_MSGT_DISABLE_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_DISABLE_RES_ACK);
		break;
	case OM2K_MSGT_TEST_RES:
		rc = abis_om2k_tx_simple(bts, &o2h->mo, OM2K_MSGT_TEST_RES_ACK);
		break;
	case OM2K_MSGT_STATUS_RESP:
		break;
	case OM2K_MSGT_START_REQ_ACK:
	case OM2K_MSGT_CON_CONF_REQ_ACK:
	case OM2K_MSGT_IS_CONF_REQ_ACK:
	case OM2K_MSGT_TX_CONF_REQ_ACK:
	case OM2K_MSGT_RX_CONF_REQ_ACK:
	case OM2K_MSGT_TS_CONF_REQ_ACK:
	case OM2K_MSGT_TF_CONF_REQ_ACK:
	case OM2K_MSGT_ENABLE_REQ_ACK:
	case OM2K_MSGT_ALARM_STATUS_REQ_ACK:
	case OM2K_MSGT_DISABLE_REQ_ACK:
		break;
	case OM2K_MSGT_START_REQ_REJ:
	case OM2K_MSGT_CONNECT_REJ:
	case OM2K_MSGT_OP_INFO_REJ:
	case OM2K_MSGT_DISCONNECT_REJ:
	case OM2K_MSGT_TEST_REQ_REJ:
	case OM2K_MSGT_CON_CONF_REQ_REJ:
	case OM2K_MSGT_IS_CONF_REQ_REJ:
	case OM2K_MSGT_TX_CONF_REQ_REJ:
	case OM2K_MSGT_RX_CONF_REQ_REJ:
	case OM2K_MSGT_TS_CONF_REQ_REJ:
	case OM2K_MSGT_TF_CONF_REQ_REJ:
	case OM2K_MSGT_ENABLE_REQ_REJ:
	case OM2K_MSGT_ALARM_STATUS_REQ_REJ:
	case OM2K_MSGT_DISABLE_REQ_REJ:
		rc = om2k_rx_nack(msg);
		break;
	default:
		LOGP(DNM, LOGL_NOTICE, "Rx unhandled OM2000 msg %s\n",
			get_value_string(om2k_msgcode_vals, msg_type));
	}

	msgb_free(msg);
	return rc;
}