blob: 06eb86507e1c2927ecefbb415d6a54ab12d3e1ef [file] [log] [blame]
Harald Welte8d77b952009-12-17 00:31:10 +01001/* Handover Decision making for Inter-BTS (Intra-BSC) Handover. This
2 * only implements the handover algorithm/decision, but not execution
3 * of it */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdlib.h>
26#include <errno.h>
27
28#include <openbsc/msgb.h>
29#include <openbsc/debug.h>
30#include <openbsc/gsm_data.h>
31#include <openbsc/meas_rep.h>
32#include <openbsc/signal.h>
33#include <openbsc/talloc.h>
34#include <openbsc/handover.h>
35
36static int handover_to_arfcn_bsic(struct gsm_lchan *lchan,
37 u_int16_t arfcn, u_int8_t bsic)
38{
39 struct gsm_bts *new_bts;
40
41 /* resolve the gsm_bts structure for the best neighbor */
42 new_bts = gsm_bts_neighbor(lchan->ts->trx->bts, arfcn, bsic);
43 if (!new_bts) {
44 DEBUGP(DHO, "unable to determine neighbor BTS for ARFCN %u BSIC %u ?!?\n", arfcn, bsic);
45 return -EINVAL;
46 }
47
48 /* and actually try to handover to that cell */
49 return bsc_handover_start(lchan, new_bts);
50}
51
52#define RXLEV_HYST 3
53
54/* process an already parsed measurement report */
55static int process_meas_rep(struct gsm_meas_rep *mr)
56{
57 struct gsm_meas_rep_cell *mr_cell = NULL;
58 unsigned int best_better_db;
59 int i;
60
61 DEBUGP(DHO, "process meas res: ");
62
63 /* FIXME: implement actual averaging over multiple measurement
64 * reports */
65
66 /* find the best cell in this report that is at least RXLEV_HYST
67 * better than the current serving cell */
68 for (i = 0; i < mr->num_cell; i++) {
69 unsigned int better;
70 if (mr->cell[i].rxlev < mr->dl.full.rx_lev + RXLEV_HYST)
71 continue;
72
73 better = mr->cell[i].rxlev - mr->dl.full.rx_lev;
74 if (better > best_better_db) {
75 mr_cell = &mr->cell[i];
76 best_better_db = better;
77 }
78 }
79
80 if (mr_cell) {
81 DEBUGPC(DHO, "Cell on ARFCN %u is better, starting handover\n", mr_cell->arfcn);
82 return handover_to_arfcn_bsic(mr->lchan, mr_cell->arfcn,
83 mr_cell->bsic);
84 }
85
86 DEBUGPC(DHO, "No better cell\n");
87 return 0;
88}
89
90static int ho_dec_sig_cb(unsigned int subsys, unsigned int signal,
91 void *handler_data, void *signal_data)
92{
93 struct gsm_meas_rep *mr;
94
95 if (subsys != SS_LCHAN)
96 return 0;
97
98 switch (signal) {
99 case S_LCHAN_MEAS_REP:
100 mr = signal_data;
101 process_meas_rep(mr);
102 break;
103 }
104
105 return 0;
106}
107
108void on_dso_load_ho_dec(void)
109{
110 register_signal_handler(SS_LCHAN, ho_dec_sig_cb, NULL);
111}