aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/measurement.c
blob: 458f17f3f60fd31b3f4f5aaede3151167c922098 (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
#include <stdint.h>
#include <errno.h>

#include <osmocom/gsm/gsm_utils.h>

#include <osmo-bts/gsm_data.h>
#include <osmo-bts/logging.h>
#include <osmo-bts/measurement.h>

/* TS 05.08, Chapter 8.4.1 */
/* measurement period ends at fn % 104 == ? */
static const uint8_t tchf_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	12,
	[2] =	25,
	[3] =	38,
	[4] =	51,
	[5] =	64,
	[6] =	77,
	[7] =	90,
};
static const uint8_t tchh0_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	103,
	[2] =	25,
	[3] =	25,
	[4] =	51,
	[5] =	51,
	[6] =	77,
	[7] =	77,
};
static const uint8_t tchh1_meas_rep_fn104[] = {
	[0] =	12,
	[1] =	12,
	[2] =	38,
	[3] =	38,
	[4] =	64,
	[5] =	64,
	[6] =	90,
	[7] =	90,
};

static uint8_t ber10k_to_rxqual(uint32_t ber10k);

/* determine if a measurement period ends at the given frame number */
static int is_meas_complete(enum gsm_phys_chan_config pchan, unsigned int ts,
			    unsigned int subch, uint32_t fn)
{
	unsigned int fn_mod;
	const uint8_t *tbl;
	int rc = 0;

	if (ts >= 8)
		return -EINVAL;
	if (pchan >= _GSM_PCHAN_MAX)
		return -EINVAL;

	switch (pchan) {
	case GSM_PCHAN_TCH_F:
		fn_mod = fn % 104;
		if (tchf_meas_rep_fn104[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_TCH_H:
		fn_mod = fn % 104;
		if (subch == 0)	
			tbl = tchh0_meas_rep_fn104;
		else
			tbl = tchh1_meas_rep_fn104;
		if (tbl[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_SDCCH8_SACCH8C:
	case GSM_PCHAN_SDCCH8_SACCH8C_CBCH:
		fn_mod = fn % 102;
		if (fn_mod == 11)
			rc = 1;
		break;
	case GSM_PCHAN_CCCH_SDCCH4:
	case GSM_PCHAN_CCCH_SDCCH4_CBCH:
		fn_mod = fn % 102;
		if (fn_mod == 36)
			rc = 1;
		break;
	default:
		rc = 0;
		break;
	}
	return rc;
}

/* receive a L1 uplink measurement from L1 */
int lchan_new_ul_meas(struct gsm_lchan *lchan, struct bts_ul_meas *ulm)
{
	struct gsm_meas_rep_unidir *mru;
	uint32_t ber_full_sum = 0;
	uint32_t irssi_full_sum = 0;
	uint32_t ber_sub_sum = 0;
	uint32_t irssi_sub_sum = 0;
	int32_t taqb_sum = 0;
	unsigned int num_meas_sub = 0;
	int i;

	if (lchan->state != LCHAN_S_ACTIVE) {
		LOGP(DMEAS, LOGL_NOTICE, "%s measurement during state: %s\n",
			gsm_lchan_name(lchan), gsm_lchans_name(lchan->state));
	}

	if (lchan->meas.num_ul_meas >= ARRAY_SIZE(lchan->meas.uplink)) {
		LOGP(DMEAS, LOGL_NOTICE, "%s no space for uplink measurement\n",
			gsm_lchan_name(lchan));
		return -ENOSPC;
	}

	memcpy(&lchan->meas.uplink[lchan->meas.num_ul_meas], ulm,
		sizeof(*ulm));

	/* set measurement valid flag */
	if ((lchan->meas.num_ul_meas == MAX_NUM_MEAS_PREPROC - 1) &&  !(lchan->meas.flags & LC_UL_M_F_RES_VALID ))
		lchan->meas.flags |= LC_UL_M_F_RES_VALID;

	/* rollover measurement index*/
	lchan->meas.num_ul_meas = (lchan->meas.num_ul_meas + 1) % MAX_NUM_UL_MEAS;

	/* make sure we have enough measurement samples in buffer before calculation */
	if (!lchan->meas.flags & LC_UL_M_F_RES_VALID)
		return -EINPROGRESS;

	/* compute the actual measurements */
	/* step 1: add up */
	for (i = 0; i < MAX_NUM_UL_MEAS; i++) {
		struct bts_ul_meas *m = &lchan->meas.uplink[i];

		ber_full_sum += m->ber10k;
		irssi_full_sum += m->inv_rssi;
		taqb_sum += m->ta_offs_qbits;

		if (m->is_sub) {
			num_meas_sub++;
			ber_sub_sum += m->ber10k;
			irssi_sub_sum += m->inv_rssi;
		}
	}

	/* step 2: divide */
	ber_full_sum /= MAX_NUM_UL_MEAS;
	irssi_full_sum /= MAX_NUM_UL_MEAS;
	taqb_sum /= MAX_NUM_UL_MEAS;

	if (num_meas_sub) {
		ber_sub_sum = ber_sub_sum / num_meas_sub;
		irssi_sub_sum = irssi_sub_sum / num_meas_sub;
	}
/*
	DEBUGP(DMEAS, "%s Computed TA(% 4dqb) BER-FULL(%2u.%02u%%), RSSI-FULL(-%3udBm), "
		"BER-SUB(%2u.%02u%%), RSSI-SUB(-%3udBm)\n", gsm_lchan_name(lchan),
		taqb_sum, ber_full_sum/100,
		ber_full_sum%100, irssi_full_sum, ber_sub_sum/100, ber_sub_sum%100,
		irssi_sub_sum);
*/
	/* step 3: store results */
	mru = &lchan->meas.ul_res;
	mru->full.rx_lev = dbm2rxlev((int)irssi_full_sum * -1);
	mru->sub.rx_lev = dbm2rxlev((int)irssi_sub_sum * -1);
	mru->full.rx_qual = ber10k_to_rxqual(ber_full_sum);
	mru->sub.rx_qual = ber10k_to_rxqual(ber_sub_sum);

	return 0;
}

/* input: BER in steps of .01%, i.e. percent/100 */
static uint8_t ber10k_to_rxqual(uint32_t ber10k)
{
	/* 05.08 / 8.2.4 */
	if (ber10k < 20)
		return 0;
	if (ber10k < 40)
		return 1;
	if (ber10k < 80)
		return 2;
	if (ber10k < 160)
		return 3;
	if (ber10k < 320)
		return 4;
	if (ber10k < 640)
		return 5;
	if (ber10k < 1280)
		return 6;
	return 7;
}

static int lchan_meas_check_compute(struct gsm_lchan *lchan, uint32_t fn)
{
	struct gsm_meas_rep_unidir *mru;
	uint32_t ber_full_sum = 0;
	uint32_t irssi_full_sum = 0;
	uint32_t ber_sub_sum = 0;
	uint32_t irssi_sub_sum = 0;
	int32_t taqb_sum = 0;
	unsigned int num_meas_sub = 0;
	int i;

	/* if measurement period is not complete, abort */
	if (!is_meas_complete(lchan->ts->pchan, lchan->ts->nr,
			      lchan->nr, fn))
		return 0;

	/* if there are no measurements, skip computation */
	if (lchan->meas.num_ul_meas == 0)
		return 0;

	/* compute the actual measurements */

	/* step 1: add up */
	for (i = 0; i < lchan->meas.num_ul_meas; i++) {
		struct bts_ul_meas *m = &lchan->meas.uplink[i];

		ber_full_sum += m->ber10k;
		irssi_full_sum += m->inv_rssi;
		taqb_sum += m->ta_offs_qbits;

		if (m->is_sub) {
			num_meas_sub++;
			ber_sub_sum += m->ber10k;
			irssi_sub_sum += m->inv_rssi;
		}
	}

	/* step 2: divide */
	ber_full_sum = ber_full_sum / lchan->meas.num_ul_meas;
	irssi_full_sum = irssi_full_sum / lchan->meas.num_ul_meas;
	taqb_sum = taqb_sum / lchan->meas.num_ul_meas;

	if (num_meas_sub) {
		ber_sub_sum = ber_sub_sum / num_meas_sub;
		irssi_sub_sum = irssi_sub_sum / num_meas_sub;
	}

	DEBUGP(DMEAS, "%s Computed TA(% 4dqb) BER-FULL(%2u.%02u%%), RSSI-FULL(-%3udBm), "
		"BER-SUB(%2u.%02u%%), RSSI-SUB(-%3udBm)\n", gsm_lchan_name(lchan),
		taqb_sum, ber_full_sum/100,
		ber_full_sum%100, irssi_full_sum, ber_sub_sum/100, ber_sub_sum%100,
		irssi_sub_sum);

	/* store results */
	mru = &lchan->meas.ul_res;
	mru->full.rx_lev = dbm2rxlev((int)irssi_full_sum * -1);
	mru->sub.rx_lev = dbm2rxlev((int)irssi_sub_sum * -1);
	mru->full.rx_qual = ber10k_to_rxqual(ber_full_sum);
	mru->sub.rx_qual = ber10k_to_rxqual(ber_sub_sum);

	lchan->meas.flags |= LC_UL_M_F_RES_VALID;
	lchan->meas.num_ul_meas = 0;

	/* send a signal indicating computation is complete */

	return 1;
}

static int ts_meas_check_compute(struct gsm_bts_trx_ts *ts, uint32_t fn)
{
	int i;
	const int num_subslots = ts_subslots(ts);

	for (i = 0; i < num_subslots; ++i) {
		struct gsm_lchan *lchan = &ts->lchan[i];

		if (lchan->state != LCHAN_S_ACTIVE)
			continue;

		switch (lchan->type) {
		case GSM_LCHAN_SDCCH:
		case GSM_LCHAN_TCH_F:
		case GSM_LCHAN_TCH_H:
		case GSM_LCHAN_PDTCH:
			lchan_meas_check_compute(lchan, fn);
			break;
		default:
			break;
		}
	}
	return 0;
}

/* needs to be called once every TDMA frame ! */
int trx_meas_check_compute(struct gsm_bts_trx *trx, uint32_t fn)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
		struct gsm_bts_trx_ts *ts = &trx->ts[i];
		ts_meas_check_compute(ts, fn);
	}
	return 0;
}

void meas_preproc_rep_init(struct gsm_lchan *lchan) {
	int i;

	/* clear cached measurement reports */
	LOGP(DRSL, LOGL_NOTICE, "%s Clear measurement report\n", gsm_lchan_name(lchan));

	lchan->meas_preproc.meas_idx = 0;
	lchan->meas_preproc.meas_ave_idx = 0;
	lchan->meas.flags &= ~LC_AVE_F_RES_VALID;
	lchan->meas.flags &= ~LC_AVE_F_CACHE_VALID;
	memset(lchan->meas_preproc.ul_ave_vec, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.ul_ave_res, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.dl_ave_vec, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.dl_ave_res, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.ul_qual_ave_vec, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.ul_qual_ave_res, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.dl_qual_ave_vec, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.dl_qual_ave_res, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.ms_bts_ave_vec, 0, MAX_NUM_MEAS_PREPROC);
	memset(lchan->meas_preproc.ms_bts_ave_res, 0, MAX_NUM_MEAS_PREPROC);

	for (i = 0; i < ARRAY_SIZE(lchan->meas_preproc.cell); i++){
		lchan->meas_preproc.cell[i].bcch_freq = 0;
		lchan->meas_preproc.cell[i].bsic = 0;
		lchan->meas_preproc.cell[i].rxlev = 0;
		memset(lchan->meas_preproc.cell[i].rxlev_vec, 0, MAX_NUM_MEAS_PREPROC);
		memset(lchan->meas_preproc.cell[i].rxlev_ave_res, 0, MAX_NUM_MEAS_PREPROC);
	}

	/* clear HO cause lists */
	lchan->meas_preproc.rec_ho_causes = 0;
	lchan->meas_preproc.cur_ho_causes = 0;

	/* Cancel any pending timer */
	osmo_timer_del(&lchan->meas_preproc.preproc_ho_timer);
}


/* Calculate HO cause decision */
static int meas_ho_cause_decision(uint8_t *res_vec, int cur_idx, int p, int n, uint8_t res_thres, int sign) {
	int i, j, k;
	uint8_t vote = 0;

	LOGP(DMEAS, LOGL_DEBUG, "Cached res_vec P=%d out of N=%d, thres=%x, buf=%s \n", p, n, res_thres, osmo_hexdump(res_vec, n));

	j = cur_idx - n + 1;
	if (j < 0)
		j =  n + j;

	LOGP(DMEAS, LOGL_DEBUG, "cur_idx=%d, start_idx=%d\n", cur_idx, j);

	for(i = j; i < j + n; i++) {
		k = i % n;
		/*if there is a measurement result higher threshold, we increase HO detection counter*/
		if ((int)res_vec[k] * sign > (int)res_thres * sign) {
			vote++;
			LOGP(DMEAS, LOGL_DEBUG, "HO decision detected res=%x, thres=%x, vote=%d \n", res_vec[k], res_thres, vote);
		}
	}
	/*if HO detection counter bigger that p_out_of_n, we will return valid HO cause generation */
	if  (vote < p)
		return 0;

	return vote;
}

/* Update HO causes */
int meas_update_ho_causes(struct gsm_lchan *lchan)
{
	struct ipac_preproc_cfg preproc_cfg = lchan->ts->trx->trx_preproc_cfg;
	int num_cell = lchan->meas_preproc.num_cell;
	int rc, i, tx_rep_pending;
	uint8_t better_rxlev = 0;
	int power_budget;

	/*make sure average result is valid */
	if (!( lchan->meas.flags & LC_AVE_F_RES_VALID ))
		return -EINPROGRESS;

	/* make sure average result is valid */
	if (!( lchan->meas.flags & LC_AVE_F_CACHE_VALID ))
		return -EINPROGRESS;

	/* print log for testing purpose */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXLEV) {
		LOGP(DMEAS, LOGL_DEBUG, "%s MEAS_PREPROC_AVG SERVICING CELL rxlev_ul_avg=%3ddBm, rxlev_dl_avg=%3ddBm\n",
			gsm_lchan_name(lchan),
			rxlev2dbm(lchan->meas_preproc.ul_ave_res[lchan->meas_preproc.meas_ave_idx]),
			rxlev2dbm(lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx]));
	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXQUAL) {
		LOGP(DMEAS, LOGL_DEBUG, "%s MEAS_PREPROC_AVG SERVICING CELL rxqual_ul_avg=%d, rxqual_dl_avg=%d\n",
			gsm_lchan_name(lchan),
			lchan->meas_preproc.ul_qual_ave_res[lchan->meas_preproc.meas_ave_idx],
			lchan->meas_preproc.dl_qual_ave_res[lchan->meas_preproc.meas_ave_idx]);
	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_DIST) {
		LOGP(DMEAS, LOGL_DEBUG, "%s MEAS_PREPROC_AVG SERVICING CELL ms_ta_avg=%d\n",
			gsm_lchan_name(lchan),
			lchan->meas_preproc.ms_bts_ave_vec[lchan->meas_preproc.meas_ave_idx]);

	}

	for (i = 0; i < num_cell; i++) {
		LOGP(DMEAS, LOGL_DEBUG, "%s MEAS_PREPROC_AVG NCELL(%d) rxlev_avg=%3ddBm\n",
			gsm_lchan_name(lchan),
			i,
			rxlev2dbm(lchan->meas_preproc.cell[i].rxlev_ave_res[lchan->meas_preproc.meas_ave_idx]));
	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXLEV) {
		LOGP(DMEAS, LOGL_DEBUG, "%s Cached ul_ave_res %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ul_ave_res, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt),
				lchan->meas_preproc.meas_ave_idx);

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached dl_ave_res %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.dl_ave_res, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt),
				lchan->meas_preproc.meas_ave_idx);

		for (i = 0; i < num_cell; i++) {
			LOGP(DMEAS, LOGL_DEBUG, "%s Cached ncell(%d) rxlev_avg_res %s, idx=%d\n",
					gsm_lchan_name(lchan),
					i,
					osmo_hexdump(lchan->meas_preproc.cell[i].rxlev_ave_res, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt),
					lchan->meas_preproc.meas_ave_idx);
		}
	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXQUAL) {
		LOGP(DMEAS, LOGL_DEBUG, "%s Cached ul_qual_ave_res %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ul_qual_ave_res, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt),
				lchan->meas_preproc.meas_ave_idx);

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached dl_qual_ave_res %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.dl_qual_ave_res, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt),
				lchan->meas_preproc.meas_ave_idx);
	}

	/* FIXME : we do nothing if there is no neighbor CELL reported */
	if(!num_cell)
		return 0;

	/* find the best neighbor cell if possible */
	for (i = 0; i < num_cell; i++) {
		if ( lchan->meas_preproc.cell[i].rxlev_ave_res[lchan->meas_preproc.meas_ave_idx] > better_rxlev ) {
			better_rxlev = lchan->meas_preproc.cell[i].rxlev_ave_res[lchan->meas_preproc.meas_ave_idx];
			lchan->meas_preproc.better_ncell = i;
		}
	}

	/* make sure the average RXLEV of NCELL is above default cell RXLEV_MIN to start to evaluate HO */
	if( better_rxlev < preproc_cfg.ncell_dflts.rxlev_min_def) {
		lchan->meas_preproc.cur_ho_causes = 0;
		return 0;
	}

	/* update HO cause based on averaged RXLEV */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXLEV) {
		/* GSM 05.08 section A.3.2.2a */
		/* check for RXLEV UL*/
		rc = meas_ho_cause_decision(lchan->meas_preproc.ul_ave_res,
					lchan->meas_preproc.meas_ave_idx,
					preproc_cfg.ho_comp.p5,
					preproc_cfg.ho_comp.n5,
					preproc_cfg.ho_thresh.l_rxlev_ul_h,
					-1);
		if (rc) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_L_RXLEV_UL_H;

			LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_L_RXLEV_UL_H detected rxlev=%3ddBm, l_rxlev_ul_h=%3ddBm \n",
				gsm_lchan_name(lchan),
				rxlev2dbm(lchan->meas_preproc.ul_ave_res[lchan->meas_preproc.meas_ave_idx]),
				rxlev2dbm(preproc_cfg.ho_thresh.l_rxlev_ul_h));

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_L_RXLEV_UL_H;

		/* check for RXLEV DL */
		rc = meas_ho_cause_decision(lchan->meas_preproc.dl_ave_res,
								lchan->meas_preproc.meas_ave_idx,
								preproc_cfg.ho_comp.p5,
								preproc_cfg.ho_comp.n5,
								preproc_cfg.ho_thresh.l_rxlev_dl_h,
								-1);
		if (rc) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_L_RXLEV_DL_H;

			LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_L_RXLEV_DL_H detected rxlev=%3ddBm, l_rxlev_ul_h=%3ddBm \n",
				gsm_lchan_name(lchan),
				rxlev2dbm(lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx]),
				rxlev2dbm(preproc_cfg.ho_thresh.l_rxlev_dl_h));

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_L_RXLEV_DL_H;

		/* GSM 05.08 section A.3.2.2c */
		/* check for RXQUAL UL */
		rc = meas_ho_cause_decision(lchan->meas_preproc.ul_qual_ave_res,
					lchan->meas_preproc.meas_ave_idx,
					preproc_cfg.ho_comp.p7,
					preproc_cfg.ho_comp.n7,
					preproc_cfg.ho_thresh.l_rxqual_ul_h,
					1);
		if (rc) {
			/* check for RXLEV UL */
			rc = meas_ho_cause_decision(lchan->meas_preproc.ul_ave_res,
						lchan->meas_preproc.meas_ave_idx,
						preproc_cfg.ho_comp.p7,
						preproc_cfg.ho_comp.n7,
						preproc_cfg.ho_thresh.rxlev_ul_ih,
						1);
			if (rc) {
				lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_RXLEV_UL_IH;

				LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_RXLEV_UL_IH detected rxlev=%3ddBm, l_rxlev_ul_h=%3ddBm \n",
					gsm_lchan_name(lchan),
					rxlev2dbm(lchan->meas_preproc.ul_ave_res[lchan->meas_preproc.meas_ave_idx]),
					rxlev2dbm(preproc_cfg.ho_thresh.rxlev_ul_ih));
			}

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_RXLEV_UL_IH;

		/* check for RXQUAL DL */
		rc = meas_ho_cause_decision(lchan->meas_preproc.dl_qual_ave_res,
					lchan->meas_preproc.meas_ave_idx,
					preproc_cfg.ho_comp.p7,
					preproc_cfg.ho_comp.n7,
					preproc_cfg.ho_thresh.l_rxqual_dl_h,
					1);
		if (rc) {
			/* check for RXLEV DL */
			rc = meas_ho_cause_decision(lchan->meas_preproc.dl_ave_res,
						lchan->meas_preproc.meas_ave_idx,
						preproc_cfg.ho_comp.p7,
						preproc_cfg.ho_comp.n7,
						preproc_cfg.ho_thresh.rxlev_dl_ih,
						1);
			if (rc) {
				lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_RXLEV_DL_IH;

				LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_RXLEV_DL_IH detected rxlev=%3ddBm, l_rxlev_ul_h=%3ddBm \n",
					gsm_lchan_name(lchan),
					rxlev2dbm(lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx]),
					rxlev2dbm(preproc_cfg.ho_thresh.rxlev_dl_ih));
			}

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_RXLEV_DL_IH;

		/* GSM 05.08 section A.3.2.2e */
		power_budget = better_rxlev - lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx];

		if ((power_budget > 0) && (power_budget >  preproc_cfg.ncell_dflts.ho_margin_def)) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_POWER_BUDGET;

			LOGP(DMEAS, LOGL_NOTICE, "%s NCELL(%d) HO_RQD_CAUSE_POWER_BUDGET detected rxlev=%3ddBm, ncell_rxlev=%3ddBm \n",
				gsm_lchan_name(lchan),
				lchan->meas_preproc.better_ncell,
				rxlev2dbm(lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx]),
				rxlev2dbm(lchan->meas_preproc.cell[lchan->meas_preproc.better_ncell].rxlev_ave_res[lchan->meas_preproc.meas_ave_idx]));

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_POWER_BUDGET;

	}

	/* update HO cause based on averaged RXQUAL */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXQUAL) {
		/* GSM 05.08 section A.3.2.2b */
		rc = meas_ho_cause_decision(lchan->meas_preproc.ul_qual_ave_res,
					lchan->meas_preproc.meas_ave_idx,
					preproc_cfg.ho_comp.p6,
					preproc_cfg.ho_comp.n6,
					preproc_cfg.ho_thresh.l_rxqual_ul_h,
					1);

		if (rc) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_L_RXQUAL_UL_H;

			LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_L_RXQUAL_UL_H detected rxqual=%d, l_rxqual_ul_h=%d\n",
				gsm_lchan_name(lchan),
				lchan->meas_preproc.ul_qual_ave_res[lchan->meas_preproc.meas_ave_idx],
				preproc_cfg.ho_thresh.l_rxqual_ul_h);

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_L_RXQUAL_UL_H;

		rc = meas_ho_cause_decision(lchan->meas_preproc.dl_qual_ave_res,
			lchan->meas_preproc.meas_ave_idx,
			preproc_cfg.ho_comp.p6,
			preproc_cfg.ho_comp.n6,
			preproc_cfg.ho_thresh.l_rxqual_dl_h,
			1);

		if (rc) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_L_RXQUAL_DL_H;

			LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_L_RXQUAL_DL_H detected rxqual=%d, l_rxqual_dl_h=%d \n",
				gsm_lchan_name(lchan),
				lchan->meas_preproc.dl_qual_ave_res[lchan->meas_preproc.meas_ave_idx],
				preproc_cfg.ho_thresh.l_rxqual_dl_h);

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_L_RXQUAL_DL_H;

	}

	/* update HO cause based on averaged MS-BTS distance */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_DIST) {
		/* GSM 05.08 section 3.2.2d */
		/*validate threshold for the maximum distance between MS and current BTS for HO process to start */
		rc = meas_ho_cause_decision(lchan->meas_preproc.ms_bts_ave_res,
					lchan->meas_preproc.meas_ave_idx,
					preproc_cfg.ho_comp.p8,
					preproc_cfg.ho_comp.n8,
					preproc_cfg.ho_thresh.ms_range_max,
					1);

		if (rc) {
			lchan->meas_preproc.cur_ho_causes |= HO_RQD_CAUSE_MAX_MS_RANGE;

			LOGP(DMEAS, LOGL_NOTICE, "%s HO_RQD_CAUSE_MAX_MS_RANGE detected ms_ta=%d, ms_range_max=%d \n",
				gsm_lchan_name(lchan),
				lchan->meas_preproc.ms_bts_ave_res[lchan->meas_preproc.meas_ave_idx],
				preproc_cfg.ho_thresh.ms_range_max);

		} else
			lchan->meas_preproc.cur_ho_causes &= ~HO_RQD_CAUSE_MAX_MS_RANGE;
	}

	LOGP(DMEAS, LOGL_DEBUG, "%s IPAC HO_CAUSES: recorded=0x%x, current=0x%x\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.rec_ho_causes,
		lchan->meas_preproc.cur_ho_causes);

	/* nothing to do when no current HO cause */
	if (!lchan->meas_preproc.cur_ho_causes)
		return 0;

	/* only add new HO cause to recorded HO cause list */
	tx_rep_pending = 0;
	for(i = 0; i < HO_RQD_CAUSE_MAX; i++) {
		/* evaluate valid HO cause only */
		if (lchan->meas_preproc.cur_ho_causes & (1 << i)) {
			/* check if HO cause has not already in recorded list */
			if ((lchan->meas_preproc.cur_ho_causes & (1 << i)) == (lchan->meas_preproc.rec_ho_causes & (1 << i)))
				continue;

			/* not in the recorded list, update recorded list now */
			lchan->meas_preproc.rec_ho_causes |=  (1 << i);
			tx_rep_pending++;
			LOGP(DMEAS, LOGL_DEBUG, "%s IPAC HO_CAUSE pending %d: recorded=0x%x, current=0x%x\n",
				gsm_lchan_name(lchan),
				tx_rep_pending,
				lchan->meas_preproc.rec_ho_causes,
				lchan->meas_preproc.cur_ho_causes);

		}
	}

	if (tx_rep_pending)
		return tx_rep_pending;

	return 0;
}

/* Parse MS measurement report */
int meas_parse_ms_rep(struct msgb *msg, struct gsm_lchan *lchan)
{
	int i;
	struct gsm48_hdr *gh = msgb_l3(msg);
	uint8_t *l3_info = gh->data;
	struct bitvec *bv;

	lchan->meas_preproc.ms_pwr = ms_pwr_dbm(msg->trx->bts->band, lchan->meas.l1_info[0] >> 3);
	lchan->meas_preproc.ms_ta = lchan->meas.l1_info[1] >> 1;

	/* UL measurement */
	LOGP(DMEAS, LOGL_DEBUG, "%s L3 INFO hexdump: %s\n",
			gsm_lchan_name(lchan),
			osmo_hexdump(msgb_l3(msg),
			msgb_l3len(msg)));

	/* check if measurement is valid */
	if ( (l3_info[1] & 0x40) ) {
		return -1;
	}

	/* set DL MES_REP valid flag */
	lchan->meas.flags |= LC_DL_M_F_RES_VALID;

	/* BA_USED flag */
	if ( (l3_info[0] & 0x80) )
			lchan->meas.flags |= LC_NCELL_M_F_BA1;

	/* UL DTX flag */
	if ( (l3_info[0] & 0x40) )
		lchan->meas.flags |= LC_UL_M_F_DTX;

	LOGP(DMEAS, LOGL_DEBUG, "%s L3 INFO MEAS FLAGS 0x%02x\n",
			gsm_lchan_name(lchan),
			lchan->meas.flags);

	bv = talloc_zero(NULL, struct bitvec);

	bv->data_len = msgb_l3len(msg);
	bv->data = l3_info;
	bv->cur_bit = 2;

	lchan->meas_preproc.dl_res.full.rx_lev = bitvec_get_uint(bv, 6) & 0x3f;
	bv->cur_bit += 2;

	lchan->meas_preproc.dl_res.sub.rx_lev = bitvec_get_uint(bv, 6) & 0x3f;
	bv->cur_bit += 1;

	lchan->meas_preproc.dl_res.full.rx_qual = bitvec_get_uint(bv, 3) & 0x7;
	lchan->meas_preproc.dl_res.sub.rx_qual = bitvec_get_uint(bv, 3) & 0x7;

	/* neighbor cells */
	lchan->meas_preproc.num_cell = bitvec_get_uint(bv, 3) & 0x07;
	if ( lchan->meas_preproc.num_cell == 0 ) {
		for (i = 0; i < ARRAY_SIZE(lchan->meas_preproc.cell); i++){
			lchan->meas_preproc.cell[i].bcch_freq = 0;
			lchan->meas_preproc.cell[i].bsic = 0;
			lchan->meas_preproc.cell[i].rxlev = 0;
			memset(lchan->meas_preproc.cell[i].rxlev_vec, 0, MAX_NUM_MEAS_PREPROC);
			memset(lchan->meas_preproc.cell[i].rxlev_ave_res, 0, MAX_NUM_MEAS_PREPROC);
		}
	}

	/* NCELL info */
	for (i = 0 ; i < lchan->meas_preproc.num_cell; i++) {
		lchan->meas_preproc.cell[i].rxlev = bitvec_get_uint(bv, 6) & 0x3f;
		lchan->meas_preproc.cell[i].bcch_freq = bitvec_get_uint(bv, 5) & 0x1f;
		lchan->meas_preproc.cell[i].bsic = bitvec_get_uint(bv, 6) & 0x3f;

		LOGP(DMEAS, LOGL_DEBUG, "%s NCELL(%d) RXL-NCELL=%3ddBm BCCH-FREQ-NCELL=%d BSIC-NCELL=%d\n",
			gsm_lchan_name(lchan),
			i,
			rxlev2dbm(lchan->meas_preproc.cell[i].rxlev),
			lchan->meas_preproc.cell[i].bcch_freq,
			lchan->meas_preproc.cell[i].bsic);
	}
	/* copy UL measurement results */
	lchan->meas_preproc.ul_res = lchan->meas.ul_res;

	LOGP(DMEAS, LOGL_DEBUG, "%s RXL-FULL-UL=%3ddBm RXL-SUB-UL=%3ddBm\n",
		gsm_lchan_name(lchan),
		rxlev2dbm(lchan->meas_preproc.ul_res.full.rx_lev),
		rxlev2dbm(lchan->meas_preproc.ul_res.sub.rx_lev));

	LOGP(DMEAS, LOGL_DEBUG, "%s RXQ-FULL-UL=%d RXQ-SUB-UL=%d\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.ul_res.full.rx_qual,
		lchan->meas_preproc.ul_res.sub.rx_qual);

	LOGP(DMEAS, LOGL_DEBUG, "%s MS L1 TX PWR=%3ddBm MS L1 TA=%u\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.ms_pwr,
		lchan->meas_preproc.ms_ta);

	LOGP(DMEAS, LOGL_DEBUG, "%s RXL-FULL-DL=%3ddBm RXL-SUB-DL=%3ddBm\n",
		gsm_lchan_name(lchan),
		rxlev2dbm(lchan->meas_preproc.dl_res.full.rx_lev),
		rxlev2dbm(lchan->meas_preproc.dl_res.sub.rx_lev));

	LOGP(DMEAS, LOGL_DEBUG, "%s RXQ-FULL-DL=%d RXQ-SUB-DL=%d\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.dl_res.full.rx_qual,
		lchan->meas_preproc.dl_res.sub.rx_qual);

	/* free bit vector buffer */
	talloc_free(bv);

	return 0;
}

static int meas_preproc_unweighted_avg(int start_idx, int avg_len, uint8_t *in_buf, int buf_size) {
	int i, j;
	int avg_res = 0;

	for (i = start_idx; i < start_idx + avg_len; i++) {
		j = i % buf_size;
		avg_res += in_buf[j];
	}
	return (avg_res/avg_len);
}

int meas_preproc_avg(struct gsm_lchan *lchan)
{
	struct ipac_preproc_cfg preproc_cfg = lchan->ts->trx->trx_preproc_cfg;
	int i, start_idx;
	int ul_avg = 0;
	int dl_avg = 0;
	int ul_qual_avg = 0;
	int dl_qual_avg = 0;
	int ms_bts_avg = 0;
	int ncell_avg[6] = {0, 0, 0, 0, 0, 0};
	uint8_t avg_method = preproc_cfg.ave_cfg.ave_method;
	uint8_t num_cell = lchan->meas_preproc.num_cell;

	/* Only unweighted average method is supported for the moment*/
	if (!(preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_METHOD_UNWEIGHTED)) {
		LOGP(DMEAS, LOGL_ERROR, "%s Unsupported preprocessing averaging method 0x%02x\n",
					gsm_lchan_name(lchan),
					avg_method);
		return -2;
	}

	/*STEP1: cache measurement results*/
	LOGP(DMEAS, LOGL_DEBUG, "%s Caching input MS MEAS_REP (%d/%d)\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.meas_idx,
		preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);

	/* cache RXLEV */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXLEV) {
		LOGP(DMEAS, LOGL_DEBUG, "%s RSL_IPAC_EIE_MEAS_AVG_CFG param_id=0x%02x, h_reqave=0x%02x, avg_method=0x%02x, h_reqt=0x%02x \n",
			gsm_lchan_name(lchan),
			preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].param_id,
			preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqave,
			preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].ave_method,
			preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);

		lchan->meas_preproc.ul_ave_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.ul_res.full.rx_lev;
		lchan->meas_preproc.dl_ave_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.dl_res.full.rx_lev;

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached input ul_ave_vec %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ul_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt),
				lchan->meas_preproc.meas_idx);

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached input dl_ave_vec %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ul_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt),
				lchan->meas_preproc.meas_idx);
	}

	/* cache RXQUAL */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXQUAL) {
		LOGP(DMEAS, LOGL_DEBUG, "%s RSL_IPAC_EIE_MEAS_AVG_CFG param_id=0x%02x, h_reqave=0x%02x, avg_method=0x%02x, h_reqt=0x%02x \n",
			gsm_lchan_name(lchan),
			preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].param_id,
			preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqave,
			preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].ave_method,
			preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt);

		lchan->meas_preproc.ul_qual_ave_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.ul_res.full.rx_qual;
		lchan->meas_preproc.dl_qual_ave_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.dl_res.full.rx_qual;

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached input ul_qual_ave_vec %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ul_qual_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt),
				lchan->meas_preproc.meas_idx);

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached input dl_qual_ave_vec %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.dl_qual_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt),
				lchan->meas_preproc.meas_idx);
	}

	/* cacch MS-BTS distance */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_DIST) {
		LOGP(DMEAS, LOGL_DEBUG, "%s RSL_IPAC_EIE_MEAS_AVG_CFG param_id=0x%02x, h_reqave=0x%02x, avg_method=0x%02x, h_reqt=0x%02x \n",
			gsm_lchan_name(lchan),
			preproc_cfg.ms_ave_cfg[IPAC_MS_BTS_DIS_AVE].param_id,
			preproc_cfg.ms_ave_cfg[IPAC_MS_BTS_DIS_AVE].h_reqave,
			preproc_cfg.ms_ave_cfg[IPAC_MS_BTS_DIS_AVE].ave_method,
			preproc_cfg.ms_ave_cfg[IPAC_MS_BTS_DIS_AVE].h_reqt);

		lchan->meas_preproc.ms_bts_ave_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.ms_ta;

		LOGP(DMEAS, LOGL_DEBUG, "%s Cached input ms_bts_ave_vec %s, idx=%d\n",
				gsm_lchan_name(lchan),
				osmo_hexdump(lchan->meas_preproc.ms_bts_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt),
				lchan->meas_preproc.meas_idx);
	}

	/* neighbor cells RXLEV averaging */
	for (i = 0; i < num_cell; i++) {
		lchan->meas_preproc.cell[i].rxlev_vec[lchan->meas_preproc.meas_idx] = lchan->meas_preproc.cell[i].rxlev;
	}

	/* set measurement valid flag */
	if ((lchan->meas_preproc.meas_idx == preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt - 1) &&  !(lchan->meas.flags & LC_AVE_F_RES_VALID ))
		lchan->meas.flags |= LC_AVE_F_RES_VALID;

	/* rollover measurement index of input vectors*/
	lchan->meas_preproc.meas_idx = (lchan->meas_preproc.meas_idx + 1) % preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt;

	/*make sure we cache enough measurement results before averaging*/
	if (!(lchan->meas.flags & LC_AVE_F_RES_VALID))
		return -EINPROGRESS;

	/* STEP 2: Averaging process */
	LOGP(DMEAS, LOGL_DEBUG, "%s Caching MS MEAS_AVE_REP (%d/%d)\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.meas_ave_idx,
		preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);

	/* calculate start averaging index in order to average last h_reqave measurements */
	start_idx = lchan->meas_preproc.meas_idx - preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqave + 1;
	if (start_idx < 0)
		start_idx +=  preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt;

	LOGP(DMEAS, LOGL_DEBUG, "%s cur_idx=%d, start_idx=%d\n",
		gsm_lchan_name(lchan),
		lchan->meas_preproc.meas_idx,
		start_idx);

	/* servicing cell averaging */
	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXLEV) {
		/* averaging RXLEV */
		ul_avg = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqave, lchan->meas_preproc.ul_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);
		dl_avg = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqave, lchan->meas_preproc.dl_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);

		/* return averaged RXLEV results */
		lchan->meas_preproc.ul_ave_res[lchan->meas_preproc.meas_ave_idx] = ul_avg;
		lchan->meas_preproc.dl_ave_res[lchan->meas_preproc.meas_ave_idx] = dl_avg;

	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_RXQUAL) {
		/* averaging RXQUAL */
		ul_qual_avg = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqave, lchan->meas_preproc.ul_qual_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt);
		dl_qual_avg = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqave, lchan->meas_preproc.dl_qual_ave_vec, preproc_cfg.ms_ave_cfg[IPAC_RXQUAL_AVE].h_reqt);

		/* return averaged RXQUAL results */
		lchan->meas_preproc.ul_qual_ave_res[lchan->meas_preproc.meas_ave_idx] = ul_qual_avg;
		lchan->meas_preproc.dl_qual_ave_res[lchan->meas_preproc.meas_ave_idx] = dl_qual_avg;
	}

	if (preproc_cfg.meas_mode_flags & MEAS_PREPROC_AVG_PARAMID_DIST) {
		/* averaging MS-BTS distance */
		ms_bts_avg = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_MS_BTS_DIS_AVE].h_reqave, lchan->meas_preproc.ms_bts_ave_vec, MAX_NUM_MEAS_PREPROC);

		/* return averaged MS-BTS distance results */
		lchan->meas_preproc.ms_bts_ave_res[lchan->meas_preproc.meas_ave_idx] = ms_bts_avg;
	}

	/* neighbor cell RXLEV averaging */
	for (i = 0; i < num_cell; i++) {
		/* averaging neighbor cell RXLEV */
		ncell_avg[i] = meas_preproc_unweighted_avg(start_idx, preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqave, lchan->meas_preproc.cell[i].rxlev_vec, MAX_NUM_MEAS_PREPROC);

		/* return neighbor cell averaged RXLEV result */
		lchan->meas_preproc.cell[i].rxlev_ave_res[lchan->meas_preproc.meas_ave_idx] = ncell_avg[i];
	}

	/* set average valid flag */
	if ((lchan->meas_preproc.meas_ave_idx == preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt - 1) &&  !(lchan->meas.flags & LC_AVE_F_CACHE_VALID ))
		lchan->meas.flags |= LC_AVE_F_CACHE_VALID;

	/* roll-over measurement average results pointer */
	lchan->meas_preproc.meas_ave_idx = (lchan->meas_preproc.meas_ave_idx + 1) % (preproc_cfg.ms_ave_cfg[IPAC_RXLEV_AVE].h_reqt);

	/*make sure we cache enough average measurement results before exiting the routine*/
	if (!(lchan->meas.flags & LC_AVE_F_CACHE_VALID))
		return -EINPROGRESS;

	return 0;
}