blob: 3dd7227dbe1be5de80f4bf6e8b07d322cc403130 [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>
Harald Welte798418a2009-11-29 22:56:14 +010042
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +010043#define LOGPHOLCHANTOLCHAN(lchan, new_lchan, level, fmt, args...) \
44 LOGP(DHODEC, level, "(BTS %u trx %u arfcn %u ts %u lchan %u %s)->(BTS %u trx %u arfcn %u ts %u lchan %u %s) (subscr %s) " fmt, \
45 lchan->ts->trx->bts->nr, \
46 lchan->ts->trx->nr, \
47 lchan->ts->trx->arfcn, \
48 lchan->ts->nr, \
49 lchan->nr, \
50 gsm_pchan_name(lchan->ts->pchan), \
51 new_lchan->ts->trx->bts->nr, \
52 new_lchan->ts->trx->nr, \
53 new_lchan->ts->trx->arfcn, \
54 new_lchan->ts->nr, \
55 new_lchan->nr, \
56 gsm_pchan_name(new_lchan->ts->pchan), \
57 bsc_subscr_name(lchan->conn->bsub), \
58 ## args)
59
60#define LOGPHO(struct_bsc_handover, level, fmt, args ...) \
61 LOGPHOLCHANTOLCHAN(struct_bsc_handover->old_lchan, struct_bsc_handover->new_lchan, level, fmt, ## args)
62
Harald Welte798418a2009-11-29 22:56:14 +010063struct bsc_handover {
64 struct llist_head list;
65
66 struct gsm_lchan *old_lchan;
67 struct gsm_lchan *new_lchan;
68
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020069 struct osmo_timer_list T3103;
Harald Welte798418a2009-11-29 22:56:14 +010070
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020071 uint8_t ho_ref;
Neels Hofmeyr5eaa4fb2017-11-27 17:57:15 +010072
73 bool inter_cell;
74 bool async;
Harald Welte798418a2009-11-29 22:56:14 +010075};
76
77static LLIST_HEAD(bsc_handovers);
78
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +010079static void handover_free(struct bsc_handover *ho)
80{
81 osmo_timer_del(&ho->T3103);
82 llist_del(&ho->list);
83 talloc_free(ho);
84}
85
Harald Welte798418a2009-11-29 22:56:14 +010086static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
87{
88 struct bsc_handover *ho;
89
90 llist_for_each_entry(ho, &bsc_handovers, list) {
91 if (ho->new_lchan == new_lchan)
92 return ho;
93 }
94
95 return NULL;
96}
97
98static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
99{
100 struct bsc_handover *ho;
101
102 llist_for_each_entry(ho, &bsc_handovers, list) {
103 if (ho->old_lchan == old_lchan)
104 return ho;
105 }
106
107 return NULL;
108}
109
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +0100110/*! Hand over the specified logical channel to the specified new BTS and possibly change the lchan type.
111 * This is the main entry point for the actual handover algorithm, after the decision whether to initiate
112 * HO to a specific BTS. To not change the lchan type, pass old_lchan->type. */
113int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *new_bts,
114 enum gsm_chan_t new_lchan_type)
Harald Welte798418a2009-11-29 22:56:14 +0100115{
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100116 struct gsm_network *network;
Harald Welte798418a2009-11-29 22:56:14 +0100117 struct gsm_lchan *new_lchan;
118 struct bsc_handover *ho;
Neels Hofmeyrc766c9e2017-11-27 17:58:04 +0100119 static uint8_t ho_ref = 0;
Harald Welte798418a2009-11-29 22:56:14 +0100120 int rc;
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100121 bool do_assignment = false;
Harald Welte798418a2009-11-29 22:56:14 +0100122
Harald Welte66706812009-12-17 22:23:21 +0100123 /* don't attempt multiple handovers for the same lchan at
124 * the same time */
125 if (bsc_ho_by_old_lchan(old_lchan))
126 return -EBUSY;
127
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100128 if (!new_bts)
129 new_bts = old_lchan->ts->trx->bts;
130 do_assignment = (new_bts == old_lchan->ts->trx->bts);
Harald Welte8d77b952009-12-17 00:31:10 +0100131
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100132 network = new_bts->network;
133
134 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_ATTEMPTED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100135
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800136 if (!old_lchan->conn) {
137 LOGP(DHO, LOGL_ERROR, "Old lchan lacks connection data.\n");
138 return -ENOSPC;
139 }
140
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100141 DEBUGP(DHO, "(BTS %u trx %u ts %u lchan %u %s)->(BTS %u lchan %s) Beginning with handover operation...\n",
142 old_lchan->ts->trx->bts->nr,
143 old_lchan->ts->trx->nr,
144 old_lchan->ts->nr,
145 old_lchan->nr,
146 gsm_pchan_name(old_lchan->ts->pchan),
147 new_bts->nr,
148 gsm_lchant_name(new_lchan_type));
149
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +0100150 new_lchan = lchan_alloc(new_bts, new_lchan_type, 0);
Harald Welte8d77b952009-12-17 00:31:10 +0100151 if (!new_lchan) {
Neels Hofmeyr6dff51d2018-02-12 16:56:41 +0100152 LOGP(DHO, LOGL_NOTICE, "No free channel for %s\n", gsm_lchant_name(new_lchan_type));
Neels Hofmeyr4e3db632018-02-12 17:25:04 +0100153 rate_ctr_inc(&network->bsc_ctrs->ctr[BSC_CTR_HANDOVER_NO_CHANNEL]);
Harald Welte798418a2009-11-29 22:56:14 +0100154 return -ENOSPC;
Harald Welte8d77b952009-12-17 00:31:10 +0100155 }
Harald Welte798418a2009-11-29 22:56:14 +0100156
Holger Hans Peter Freyther90cdd282010-12-21 13:29:14 +0100157 ho = talloc_zero(tall_bsc_ctx, struct bsc_handover);
Harald Welte798418a2009-11-29 22:56:14 +0100158 if (!ho) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100159 LOGP(DHO, LOGL_FATAL, "Out of Memory\n");
Harald Welte798418a2009-11-29 22:56:14 +0100160 lchan_free(new_lchan);
161 return -ENOMEM;
162 }
163 ho->old_lchan = old_lchan;
164 ho->new_lchan = new_lchan;
Harald Welte8d77b952009-12-17 00:31:10 +0100165 ho->ho_ref = ho_ref++;
Neels Hofmeyr3fb5dcf2018-02-14 13:51:36 +0100166 ho->inter_cell = !do_assignment;
167 ho->async = true;
Harald Welte8d77b952009-12-17 00:31:10 +0100168
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100169 LOGPHO(ho, LOGL_INFO, "Triggering %s\n", do_assignment? "Assignment" : "Handover");
170
Harald Welte8d77b952009-12-17 00:31:10 +0100171 /* copy some parameters from old lchan */
172 memcpy(&new_lchan->encr, &old_lchan->encr, sizeof(new_lchan->encr));
Neels Hofmeyr05a3a2d2018-02-12 17:25:04 +0100173 if (do_assignment) {
174 new_lchan->ms_power = old_lchan->ms_power;
175 new_lchan->rqd_ta = old_lchan->rqd_ta;
176 } else {
177 new_lchan->ms_power =
178 ms_pwr_ctl_lvl(new_bts->band, new_bts->ms_max_power);
179 /* FIXME: do we have a better idea of the timing advance? */
180 //new_lchan->rqd_ta = old_lchan->rqd_ta;
181 }
Harald Welte8d77b952009-12-17 00:31:10 +0100182 new_lchan->bs_power = old_lchan->bs_power;
183 new_lchan->rsl_cmode = old_lchan->rsl_cmode;
184 new_lchan->tch_mode = old_lchan->tch_mode;
Neels Hofmeyref497b82018-02-12 17:28:07 +0100185 memcpy(&new_lchan->mr_ms_lv, &old_lchan->mr_ms_lv, sizeof(new_lchan->mr_ms_lv));
186 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 +0800187
188 new_lchan->conn = old_lchan->conn;
189 new_lchan->conn->ho_lchan = new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100190
Neels Hofmeyr5eaa4fb2017-11-27 17:57:15 +0100191 rc = rsl_chan_activate_lchan(new_lchan,
Neels Hofmeyr063868e2018-02-15 14:18:22 +0100192 ho->async ? RSL_ACT_INTER_ASYNC : RSL_ACT_INTER_SYNC,
Neels Hofmeyr5eaa4fb2017-11-27 17:57:15 +0100193 ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100194 if (rc < 0) {
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100195 LOGPHO(ho, LOGL_INFO, "%s Failure: activate lchan rc = %d\n",
196 do_assignment? "Assignment" : "Handover", rc);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800197 new_lchan->conn->ho_lchan = NULL;
Holger Hans Peter Freytherc8396672010-12-26 19:59:32 +0100198 new_lchan->conn = NULL;
Harald Welte798418a2009-11-29 22:56:14 +0100199 talloc_free(ho);
200 lchan_free(new_lchan);
201 return rc;
202 }
203
Holger Hans Peter Freyther5eec9d92010-04-10 00:16:04 +0200204 rsl_lchan_set_state(new_lchan, LCHAN_S_ACT_REQ);
Harald Welte798418a2009-11-29 22:56:14 +0100205 llist_add(&ho->list, &bsc_handovers);
206 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
207
208 return 0;
209}
210
Neels Hofmeyra2733ae2017-11-28 00:29:25 +0100211/* clear any operation for this connection */
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100212void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan)
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800213{
214 struct bsc_handover *ho;
215
216 ho = bsc_ho_by_new_lchan(conn->ho_lchan);
217
218
219 if (!ho && conn->ho_lchan)
220 LOGP(DHO, LOGL_ERROR, "BUG: We lost some state.\n");
221
222 if (!ho) {
223 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
224 return;
225 }
226
227 conn->ho_lchan->conn = NULL;
228 conn->ho_lchan = NULL;
Holger Hans Peter Freytherebd50a62010-12-27 13:46:48 +0100229
230 if (free_lchan)
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100231 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800232
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100233 handover_free(ho);
Holger Hans Peter Freytherf2553a62010-06-30 12:58:14 +0800234}
235
Harald Welte798418a2009-11-29 22:56:14 +0100236/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
237static void ho_T3103_cb(void *_ho)
238{
239 struct bsc_handover *ho = _ho;
Harald Welteffa55a42009-12-22 19:07:32 +0100240 struct gsm_network *net = ho->new_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100241
Harald Welte8d77b952009-12-17 00:31:10 +0100242 DEBUGP(DHO, "HO T3103 expired\n");
Alexander Couzensb847a212016-08-02 11:34:11 +0200243 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_TIMEOUT]);
Harald Welte8d77b952009-12-17 00:31:10 +0100244
Holger Hans Peter Freytherd9c9f072010-06-30 13:04:13 +0800245 ho->new_lchan->conn->ho_lchan = NULL;
246 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100247 lchan_release(ho->new_lchan, 0, RSL_REL_LOCAL_END);
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100248 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100249}
250
251/* RSL has acknowledged activation of the new lchan */
252static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
253{
254 struct bsc_handover *ho;
Harald Welte798418a2009-11-29 22:56:14 +0100255
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100256 /* we need to check if this channel activation is related to
257 * a handover at all (and if, which particular handover) */
Harald Welte798418a2009-11-29 22:56:14 +0100258 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100259 if (!ho)
Harald Welte798418a2009-11-29 22:56:14 +0100260 return -ENODEV;
Harald Weltea9fa8dc2009-12-18 14:50:08 +0100261
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100262 LOGPHO(ho, LOGL_INFO, "Channel Activate Ack, send %s COMMAND\n", ho->inter_cell? "HANDOVER" : "ASSIGNMENT");
Harald Welte798418a2009-11-29 22:56:14 +0100263
264 /* we can now send the 04.08 HANDOVER COMMAND to the MS
265 * using the old lchan */
266
Neels Hofmeyrb0370532018-02-15 14:18:58 +0100267 gsm48_send_ho_cmd(ho->old_lchan, new_lchan, new_lchan->ms_power, ho->ho_ref);
Harald Welte798418a2009-11-29 22:56:14 +0100268
269 /* start T3103. We can continue either with T3103 expiration,
270 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200271 osmo_timer_setup(&ho->T3103, ho_T3103_cb, ho);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200272 osmo_timer_schedule(&ho->T3103, 10, 0);
Harald Welte798418a2009-11-29 22:56:14 +0100273
Harald Weltea0379022009-12-20 17:04:40 +0100274 /* create a RTP connection */
275 if (is_ipaccess_bts(new_lchan->ts->trx->bts))
276 rsl_ipacc_crcx(new_lchan);
277
Harald Welte798418a2009-11-29 22:56:14 +0100278 return 0;
279}
280
281/* RSL has not acknowledged activation of the new lchan */
282static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
283{
284 struct bsc_handover *ho;
285
286 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100287 if (!ho) {
Harald Welte25cf8242012-07-06 14:21:55 +0200288 LOGP(DHO, LOGL_INFO, "ACT NACK: unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100289 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100290 }
Harald Welte798418a2009-11-29 22:56:14 +0100291
Holger Hans Peter Freyther2391b4c2010-12-21 13:30:17 +0100292 new_lchan->conn->ho_lchan = NULL;
293 new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100294 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100295
296 /* FIXME: maybe we should try to allocate a new LCHAN here? */
297
298 return 0;
299}
300
301/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
302static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
303{
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100304 struct gsm_network *net;
Harald Welte798418a2009-11-29 22:56:14 +0100305 struct bsc_handover *ho;
306
307 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100308 if (!ho) {
309 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100310 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100311 }
Harald Welte798418a2009-11-29 22:56:14 +0100312
Holger Hans Peter Freyther9d3e2ec2010-12-26 20:34:26 +0100313 net = new_lchan->ts->trx->bts->network;
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100314
315 LOGPHO(ho, LOGL_INFO, "%s Complete\n", ho->inter_cell ? "Handover" : "Assignment");
Harald Welte7c639a02009-12-25 23:02:50 +0100316
Alexander Couzensb847a212016-08-02 11:34:11 +0200317 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_COMPLETED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100318
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200319 osmo_timer_del(&ho->T3103);
Harald Welte798418a2009-11-29 22:56:14 +0100320
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800321 /* Replace the ho lchan with the primary one */
322 if (ho->old_lchan != new_lchan->conn->lchan)
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100323 LOGPHO(ho, LOGL_ERROR, "Primary lchan changed during handover.\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800324
325 if (new_lchan != new_lchan->conn->ho_lchan)
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100326 LOGPHO(ho, LOGL_ERROR, "Handover channel changed during this handover.\n");
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800327
328 new_lchan->conn->ho_lchan = NULL;
329 new_lchan->conn->lchan = new_lchan;
330 ho->old_lchan->conn = NULL;
Harald Weltefe18d5c2009-12-17 17:14:43 +0100331
Holger Hans Peter Freytherd66777f2012-12-06 12:20:56 +0100332 lchan_release(ho->old_lchan, 0, RSL_REL_LOCAL_END);
Harald Welteade773f2009-12-21 13:29:19 +0100333
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100334 handover_free(ho);
Harald Welte798418a2009-11-29 22:56:14 +0100335 return 0;
336}
337
338/* GSM 04.08 HANDOVER FAIL has been received */
339static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
340{
Harald Welteffa55a42009-12-22 19:07:32 +0100341 struct gsm_network *net = old_lchan->ts->trx->bts->network;
Harald Welte798418a2009-11-29 22:56:14 +0100342 struct bsc_handover *ho;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100343 struct gsm_lchan *new_lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100344
345 ho = bsc_ho_by_old_lchan(old_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100346 if (!ho) {
347 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100348 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100349 }
Harald Welte798418a2009-11-29 22:56:14 +0100350
Alexander Couzensb847a212016-08-02 11:34:11 +0200351 rate_ctr_inc(&net->bsc_ctrs->ctr[BSC_CTR_HANDOVER_FAILED]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100352
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100353 new_lchan = ho->new_lchan;
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800354
355 /* release the channel and forget about it */
356 ho->new_lchan->conn->ho_lchan = NULL;
357 ho->new_lchan->conn = NULL;
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100358 handover_free(ho);
Holger Hans Peter Freythere071ab72010-06-30 12:40:10 +0800359
Holger Hans Peter Freytherd30ed6b2014-12-17 21:21:36 +0100360 lchan_release(new_lchan, 0, RSL_REL_LOCAL_END);
361
Harald Welte798418a2009-11-29 22:56:14 +0100362
363 return 0;
364}
365
366/* GSM 08.58 HANDOVER DETECT has been received */
367static int ho_rsl_detect(struct gsm_lchan *new_lchan)
368{
369 struct bsc_handover *ho;
370
Harald Welte6f7a5a72009-12-18 11:52:03 +0100371 ho = bsc_ho_by_new_lchan(new_lchan);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100372 if (!ho) {
373 LOGP(DHO, LOGL_ERROR, "unable to find HO record\n");
Harald Welte798418a2009-11-29 22:56:14 +0100374 return -ENODEV;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100375 }
Harald Welte798418a2009-11-29 22:56:14 +0100376
Neels Hofmeyrcbc999c2018-02-12 17:25:04 +0100377 LOGPHO(ho, LOGL_DEBUG, "Handover RACH detected\n");
Neels Hofmeyrbe1131d2018-01-19 01:23:35 +0100378
379 /* This is just for logging on the DHO category. The actual MGCP switchover happens in
380 * osmo_bsc_mgcp.c by receiving the same S_LCHAN_HANDOVER_DETECT signal.
381 * (Calling mgcp_handover() directly currently breaks linking in utils/...) */
Harald Welte798418a2009-11-29 22:56:14 +0100382
383 return 0;
384}
385
386static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
387 void *handler_data, void *signal_data)
388{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100389 struct lchan_signal_data *lchan_data;
Harald Welte798418a2009-11-29 22:56:14 +0100390 struct gsm_lchan *lchan;
391
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100392 lchan_data = signal_data;
Harald Welte798418a2009-11-29 22:56:14 +0100393 switch (subsys) {
394 case SS_LCHAN:
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +0100395 lchan = lchan_data->lchan;
Harald Welte798418a2009-11-29 22:56:14 +0100396 switch (signal) {
397 case S_LCHAN_ACTIVATE_ACK:
398 return ho_chan_activ_ack(lchan);
399 case S_LCHAN_ACTIVATE_NACK:
400 return ho_chan_activ_nack(lchan);
401 case S_LCHAN_HANDOVER_DETECT:
402 return ho_rsl_detect(lchan);
403 case S_LCHAN_HANDOVER_COMPL:
404 return ho_gsm48_ho_compl(lchan);
405 case S_LCHAN_HANDOVER_FAIL:
406 return ho_gsm48_ho_fail(lchan);
407 }
408 break;
409 default:
410 break;
411 }
412
413 return 0;
414}
415
Neels Hofmeyra2733ae2017-11-28 00:29:25 +0100416/* Return the old lchan or NULL. This is meant for audio handling */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +0100417struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan)
418{
419 struct bsc_handover *ho;
420 ho = bsc_ho_by_new_lchan(new_lchan);
421 if (!ho)
422 return NULL;
423 return ho->old_lchan;
424}
425
Harald Welte798418a2009-11-29 22:56:14 +0100426static __attribute__((constructor)) void on_dso_load_ho_logic(void)
427{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200428 osmo_signal_register_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
Harald Welte798418a2009-11-29 22:56:14 +0100429}
Andreas Eversberga9180002013-05-30 11:14:31 +0200430
431/* Count number of currently ongoing handovers
432 * inter_cell: if true, count only handovers between two cells. If false, count only handovers within one
433 * cell. */
434int bsc_ho_count(struct gsm_bts *bts, bool inter_cell)
435{
436 struct bsc_handover *ho;
437 int count = 0;
438
439 llist_for_each_entry(ho, &bsc_handovers, list) {
440 if (ho->inter_cell != inter_cell)
441 continue;
442 if (ho->new_lchan->ts->trx->bts == bts)
443 count++;
444 }
445
446 return count;
447}