blob: cdc21f56f9810aee7b7a085a84726490180e76d3 [file] [log] [blame]
Harald Welte798418a2009-11-29 22:56:14 +01001/* Handover Logic for Inter-BTS (Intra-BSC) Handover. This does not
2 * actually implement the handover algorithm/decision, but executes a
3 * handover decision */
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
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte798418a2009-11-29 22:56:14 +010012 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte798418a2009-11-29 22:56:14 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte798418a2009-11-29 22:56:14 +010021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
28#include <time.h>
29#include <netinet/in.h>
30
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020032#include <osmocom/bsc/debug.h>
33#include <osmocom/bsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/gsm/gsm_utils.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020035#include <osmocom/bsc/abis_rsl.h>
36#include <osmocom/bsc/chan_alloc.h>
37#include <osmocom/bsc/signal.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010038#include <osmocom/core/talloc.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020039#include <osmocom/bsc/bsc_subscriber.h>
40#include <osmocom/bsc/gsm_04_08_utils.h>
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +010041#include <osmocom/bsc/handover.h>
Neels Hofmeyr45e46d22018-02-15 14:10:12 +010042#include <osmocom/bsc/handover_cfg.h>
Harald Welte798418a2009-11-29 22:56:14 +010043
44static LLIST_HEAD(bsc_handovers);
Neels Hofmeyr45e46d22018-02-15 14:10:12 +010045static LLIST_HEAD(handover_decision_callbacks);
Harald Welte798418a2009-11-29 22:56:14 +010046
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +010047static void handover_free(struct bsc_handover *ho)
48{
49 osmo_timer_del(&ho->T3103);
50 llist_del(&ho->list);
51 talloc_free(ho);
52}
53
Harald Welte798418a2009-11-29 22:56:14 +010054static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
55{
56 struct bsc_handover *ho;
57
58 llist_for_each_entry(ho, &bsc_handovers, list) {
59 if (ho->new_lchan == new_lchan)
60 return ho;
61 }
62
63 return NULL;
64}
65
66static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
67{
68 struct bsc_handover *ho;
69
70 llist_for_each_entry(ho, &bsc_handovers, list) {
71 if (ho->old_lchan == old_lchan)
72 return ho;
73 }
74
75 return NULL;
76}
77
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +010078/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type.
79 * This is the main entry point for the actual handover algorithm, after the decision whether to initiate
80 * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */
Neels Hofmeyr45e46d22018-02-15 14:10:12 +010081int bsc_handover_start(enum hodec_id from_hodec_id, struct gsm_lchan *old_lchan, struct gsm_bts *new_bts,
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +010082 enum gsm_chan_t new_lchan_type)
Harald Welte798418a2009-11-29 22:56:14 +010083{
Neels Hofmeyr4e3db632018-02-12 17:25:04 +010084 struct gsm_network *network;
Harald Welte798418a2009-11-29 22:56:14 +010085 struct gsm_lchan *new_lchan;
86 struct bsc_handover *ho;
Neels Hofmeyrc766c9e2017-11-27 17:58:04 +010087 static uint8_t ho_ref = 0;
Harald Welte798418a2009-11-29 22:56:14 +010088 int rc;
Neels Hofmeyr4e3db632018-02-12 17:25:04 +010089 bool do_assignment = false;
Harald Welte798418a2009-11-29 22:56:14 +010090
Harald Welte66706812009-12-17 22:23:21 +010091 /* don't attempt multiple handovers for the same lchan at
92 * the same time */
93 if (bsc_ho_by_old_lchan(old_lchan))
94 return -EBUSY;
95
Neels Hofmeyr4e3db632018-02-12 17:25:04 +010096 if (!new_bts)
97 new_bts = old_lchan->ts->trx->bts;
98 do_assignment = (new_bts == old_lchan->ts->trx->bts);
Harald Welte8d77b952009-12-17 00:31:10 +010099
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100100 network = new_bts->network;
101
102 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100103
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800104 if (!old_lchan->conn) {
105 LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
106 return -ENOSPC;
107 }
108
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100109 DEBUGP(DHO, "(BTS %u trx %u ts %u lchan %u %s)->(BTS %u lchan %s) Beginning with handover operation...\n",
110 old_lchan->ts->trx->bts->nr,
111 old_lchan->ts->trx->nr,
112 old_lchan->ts->nr,
113 old_lchan->nr,
114 gsm_pchan_name(old_lchan->ts->pchan),
115 new_bts->nr,
116 gsm_lchant_name(new_lchan_type));
117
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +0100118 new_lchan = lchan_alloc(new_bts, new_lchan_type, 0);
Harald Welte8d77b952009-12-17 00:31:10 +0100119 if (!new_lchan) {
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +0100120 LOGP(DHO, LOGL_NOTICE, "No free channel for %s\n", gsm_lchant_name(new_lchan_type));
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100121 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_NO_CHANNEL]);
Harald Welte798418a2009-11-29 22:56:14 +0100122 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100123 }
Harald Welte798418a2009-11-29 22:56:14 +0100124
Holger Hans Peter Freyther90cdd282010-12-21 13:29:14 +0100125 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte798418a2009-11-29 22:56:14 +0100126 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100127 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100128 lchan_free(new_lchan);
129 return -ENOMEM;
130 }
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100131 ho->from_hodec_id = from_hodec_id;
Harald Welte798418a2009-11-29 22:56:14 +0100132 ho->old_lchan = old_lchan;
133 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100134 ho->ho_ref = ho_ref++;
Neels Hofmeyr3fb5dcf2018-02-14 13:51:36 +0100135 ho->inter_cell = !do_assignment;
136 ho->async = true;
Harald Welte8d77b952009-12-17 00:31:10 +0100137
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100138 LOGPHO(ho, LOGL_INFO, "Triggering %s\n", do_assignment? "Assignment" : "Handover");
139
Harald Welte8d77b952009-12-17 00:31:10 +0100140 /* copy some parameters from old lchan */
141 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
Neels Hofmeyr05a3a2d2018-02-12 17:25:04 +0100142 if (do_assignment) {
143 new_lchan->ms_power = old_lchan->ms_power;
144 new_lchan->rqd_ta = old_lchan->rqd_ta;
145 } else {
146 new_lchan->ms_power =
147 ms_pwr_ctl_lvl(new_bts->band, new_bts->ms_max_power);
148 /* FIXME: do we have a better idea of the timing advance? */
149 //new_lchan->rqd_ta = old_lchan->rqd_ta;
150 }
Harald Welte8d77b952009-12-17 00:31:10 +0100151 new_lchan->bs_power = old_lchan->bs_power;
152 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
153 new_lchan->tch_mode = old_lchan->tch_mode;
Neels Hofmeyref497b82018-02-12 17:28:07 +0100154 memcpy(&new_lchan->mr_ms_lv, &old_lchan->mr_ms_lv, sizeof(new_lchan->mr_ms_lv));
155 memcpy(&new_lchan->mr_bts_lv, &old_lchan->mr_bts_lv, sizeof(new_lchan->mr_bts_lv));
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800156
157 new_lchan->conn = old_lchan->conn;
158 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100159
Neels Hofmeyr5eaa4fb2017-11-27 17:57:15 +0100160 rc = rsl_chan_activate_lchan(new_lchan,
Neels Hofmeyr063868e2018-02-15 14:18:22 +0100161 ho->async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC,
Neels Hofmeyr5eaa4fb2017-11-27 17:57:15 +0100162 ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100163 if (rc < 0) {
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100164 LOGPHO(ho, LOGL_INFO, "%s Failure: activate lchan rc = %d\n",
165 do_assignment? "Assignment" : "Handover", rc);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800166 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freytherc8396672010-12-26 19:59:32 +0100167 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100168 talloc_free(ho);
169 lchan_free(new_lchan);
170 return rc;
171 }
172
Holger Hans Peter Freyther5eec9d92010-04-10 00:16:04 +0200173 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte798418a2009-11-29 22:56:14 +0100174 llist_add(&ho->list, &bsc_handovers);
175 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
176
177 return 0;
178}
179
Neels Hofmeyra2733ae2017-11-28 00:29:25 +0100180/* clear any operation for this connection */
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100181void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800182{
183 struct bsc_handover *ho;
184
185 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
186
187
188 if (!ho && conn->ho_lchan)
189 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
190
191 if (!ho) {
192 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
193 return;
194 }
195
196 conn->ho_lchan->conn = NULL;
197 conn->ho_lchan = NULL;
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100198
199 if (free_lchan)
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100200 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800201
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100202 handover_free(ho);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800203}
204
Harald Welte798418a2009-11-29 22:56:14 +0100205/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
206static void ho_T3103_cb(void *_ho)
207{
208 struct bsc_handover *ho = _ho;
Harald Welteffa55a42009-12-22 19:07:32 +0100209 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100210
Harald Welte8d77b952009-12-17 00:31:10 +0100211 DEBUGP(DHO, "HO T3103 expired\n");
Alexander Couzensb847a212016-08-02 11:34:11 +0200212 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_TIMEOUT]);
Harald Welte8d77b952009-12-17 00:31:10 +0100213
Holger Hans Peter Freytherd9c9f072010-06-30 13:04:13 +0800214 ho->new_lchan->conn->ho_lchan = NULL;
215 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100216 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100217 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100218}
219
220/* RSL has acknowledged activation of the new lchan */
221static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
222{
223 struct bsc_handover *ho;
Harald Welte798418a2009-11-29 22:56:14 +0100224
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100225 /* we need to check if this channel activation is related to
226 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100227 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100228 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100229 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100230
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100231 LOGPHO(ho, LOGL_INFO, "Channel Activate Ack, send %s COMMAND\n", ho->inter_cell? "HANDOVER" : "ASSIGNMENT");
Harald Welte798418a2009-11-29 22:56:14 +0100232
233 /* we can now send the 04.08 HANDOVER COMMAND to the MS
234 * using the old lchan */
235
Neels Hofmeyrb0370532018-02-15 14:18:58 +0100236 gsm48_send_ho_cmd(ho->old_lchan, new_lchan, new_lchan->ms_power, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100237
238 /* start T3103. We can continue either with T3103 expiration,
239 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200240 osmo_timer_setup(&ho->T3103, ho_T3103_cb, ho);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200241 osmo_timer_schedule(&ho->T3103, 10, 0);
Harald Welte798418a2009-11-29 22:56:14 +0100242
Harald Weltea0379022009-12-20 17:04:40 +0100243 /* create a RTP connection */
244 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
245 rsl_ipacc_crcx(new_lchan);
246
Harald Welte798418a2009-11-29 22:56:14 +0100247 return 0;
248}
249
250/* RSL has not acknowledged activation of the new lchan */
251static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
252{
253 struct bsc_handover *ho;
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100254 struct handover_decision_callbacks *hdc;
Harald Welte798418a2009-11-29 22:56:14 +0100255
256 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100257 if (!ho) {
Neels Hofmeyr8489b462018-02-15 14:19:38 +0100258 /* This lchan is not involved in a handover. */
259 return 0;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100260 }
Harald Welte798418a2009-11-29 22:56:14 +0100261
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100262 hdc = handover_decision_callbacks_get(ho->from_hodec_id);
263 if (hdc && hdc->on_ho_chan_activ_nack)
264 hdc->on_ho_chan_activ_nack(ho);
265
Holger Hans Peter Freyther2391b4c2010-12-21 13:30:17 +0100266 new_lchan->conn->ho_lchan = NULL;
267 new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100268 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100269
270 /* FIXME: maybe we should try to allocate a new LCHAN here? */
271
272 return 0;
273}
274
275/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
276static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
277{
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100278 struct gsm_network *net;
Harald Welte798418a2009-11-29 22:56:14 +0100279 struct bsc_handover *ho;
280
281 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100282 if (!ho) {
283 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100284 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100285 }
Harald Welte798418a2009-11-29 22:56:14 +0100286
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100287 net = new_lchan->ts->trx->bts->network;
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100288
289 LOGPHO(ho, LOGL_INFO, "%s Complete\n", ho->inter_cell ? "Handover" : "Assignment");
Harald Welte7c639a02009-12-25 23:02:50 +0100290
Alexander Couzensb847a212016-08-02 11:34:11 +0200291 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_COMPLETED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100292
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200293 osmo_timer_del(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100294
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800295 /* Replace the ho lchan with the primary one */
296 if (ho->old_lchan != new_lchan->conn->lchan)
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100297 LOGPHO(ho, LOGL_ERROR, "Primary lchan changed during handover.\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800298
299 if (new_lchan != new_lchan->conn->ho_lchan)
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100300 LOGPHO(ho, LOGL_ERROR, "Handover channel changed during this handover.\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800301
302 new_lchan->conn->ho_lchan = NULL;
303 new_lchan->conn->lchan = new_lchan;
304 ho->old_lchan->conn = NULL;
Harald Weltefe18d5c2009-12-17 17:14:43 +0100305
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100306 lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
Harald Welteade773f2009-12-21 13:29:19 +0100307
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100308 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100309 return 0;
310}
311
312/* GSM 04.08 HANDOVER FAIL has been received */
313static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
314{
Harald Welteffa55a42009-12-22 19:07:32 +0100315 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100316 struct bsc_handover *ho;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100317 struct gsm_lchan *new_lchan;
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100318 struct handover_decision_callbacks *hdc;
Harald Welte798418a2009-11-29 22:56:14 +0100319
320 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100321 if (!ho) {
322 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100323 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100324 }
Harald Welte798418a2009-11-29 22:56:14 +0100325
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100326 hdc = handover_decision_callbacks_get(ho->from_hodec_id);
327 if (hdc && hdc->on_ho_failure)
328 hdc->on_ho_failure(ho);
329
Alexander Couzensb847a212016-08-02 11:34:11 +0200330 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_FAILED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100331
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100332 new_lchan = ho->new_lchan;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800333
334 /* release the channel and forget about it */
335 ho->new_lchan->conn->ho_lchan = NULL;
336 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100337 handover_free(ho);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800338
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100339 lchan_release(new_lchan, 0, RSL_REL_LOCAL_END);
340
Harald Welte798418a2009-11-29 22:56:14 +0100341
342 return 0;
343}
344
345/* GSM 08.58 HANDOVER DETECT has been received */
346static int ho_rsl_detect(struct gsm_lchan *new_lchan)
347{
348 struct bsc_handover *ho;
349
Harald Welte6f7a5a72009-12-18 11:52:03 +0100350 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100351 if (!ho) {
352 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100353 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100354 }
Harald Welte798418a2009-11-29 22:56:14 +0100355
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100356 LOGPHO(ho, LOGL_DEBUG, "Handover RACH detected\n");
Neels Hofmeyrbe1131d2018-01-19 01:23:35 +0100357
358 /* This is just for logging on the DHO category. The actual MGCP switchover happens in
359 * osmo_bsc_mgcp.c by receiving the same S_LCHAN_HANDOVER_DETECT signal.
360 * (Calling mgcp_handover() directly currently breaks linking in utils/...) */
Harald Welte798418a2009-11-29 22:56:14 +0100361
362 return 0;
363}
364
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100365static int ho_meas_rep(struct gsm_meas_rep *mr)
366{
367 struct handover_decision_callbacks *hdc;
368 enum hodec_id hodec_id = ho_get_algorithm(mr->lchan->ts->trx->bts->ho);
369
370 hdc = handover_decision_callbacks_get(hodec_id);
371 if (!hdc || !hdc->on_measurement_report)
372 return 0;
373 hdc->on_measurement_report(mr);
374 return 0;
375}
376
Harald Welte798418a2009-11-29 22:56:14 +0100377static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
378 void *handler_data, void *signal_data)
379{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100380 struct lchan_signal_data *lchan_data;
Harald Welte798418a2009-11-29 22:56:14 +0100381 struct gsm_lchan *lchan;
382
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100383 lchan_data = signal_data;
Harald Welte798418a2009-11-29 22:56:14 +0100384 switch (subsys) {
385 case SS_LCHAN:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100386 lchan = lchan_data->lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100387 switch (signal) {
388 case S_LCHAN_ACTIVATE_ACK:
389 return ho_chan_activ_ack(lchan);
390 case S_LCHAN_ACTIVATE_NACK:
391 return ho_chan_activ_nack(lchan);
392 case S_LCHAN_HANDOVER_DETECT:
393 return ho_rsl_detect(lchan);
394 case S_LCHAN_HANDOVER_COMPL:
395 return ho_gsm48_ho_compl(lchan);
396 case S_LCHAN_HANDOVER_FAIL:
397 return ho_gsm48_ho_fail(lchan);
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100398 case S_LCHAN_MEAS_REP:
399 return ho_meas_rep(lchan_data->mr);
Harald Welte798418a2009-11-29 22:56:14 +0100400 }
401 break;
402 default:
403 break;
404 }
405
406 return 0;
407}
408
Neels Hofmeyra2733ae2017-11-28 00:29:25 +0100409/* Return the old lchan or NULL. This is meant for audio handling */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +0100410struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
411{
412 struct bsc_handover *ho;
413 ho = bsc_ho_by_new_lchan(new_lchan);
414 if (!ho)
415 return NULL;
416 return ho->old_lchan;
417}
418
Harald Welte798418a2009-11-29 22:56:14 +0100419static __attribute__((constructor)) void on_dso_load_ho_logic(void)
420{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200421 osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100422}
Andreas Eversberga9180002013-05-30 11:14:31 +0200423
424/* Count number of currently ongoing handovers
425 * inter_cell: if true, count only handovers between two cells. If false, count only handovers within one
426 * cell. */
427int bsc_ho_count(struct gsm_bts *bts, bool inter_cell)
428{
429 struct bsc_handover *ho;
430 int count = 0;
431
432 llist_for_each_entry(ho, &bsc_handovers, list) {
433 if (ho->inter_cell != inter_cell)
434 continue;
435 if (ho->new_lchan->ts->trx->bts == bts)
436 count++;
437 }
438
439 return count;
440}
Neels Hofmeyr45e46d22018-02-15 14:10:12 +0100441
442void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc)
443{
444 llist_add_tail(&hdc->entry, &handover_decision_callbacks);
445}
446
447struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id)
448{
449 struct handover_decision_callbacks *hdc;
450 llist_for_each_entry(hdc, &handover_decision_callbacks, entry) {
451 if (hdc->hodec_id == hodec_id)
452 return hdc;
453 }
454 return NULL;
455}