blob: d4a8884875009ae7271b4af83f9681fc204b13dc [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
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 <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
32#include <openbsc/msgb.h>
33#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
35#include <openbsc/gsm_utils.h>
36#include <openbsc/gsm_subscriber.h>
37#include <openbsc/gsm_04_08.h>
38#include <openbsc/abis_rsl.h>
39#include <openbsc/chan_alloc.h>
40#include <openbsc/signal.h>
41#include <openbsc/talloc.h>
42#include <openbsc/transaction.h>
43
44struct bsc_handover {
45 struct llist_head list;
46
47 struct gsm_lchan *old_lchan;
48 struct gsm_lchan *new_lchan;
49
50 struct timer_list T3103;
51
52 u_int8_t ho_ref;
53};
54
55static LLIST_HEAD(bsc_handovers);
56
57static struct bsc_handover *bsc_ho_by_new_lchan(struct gsm_lchan *new_lchan)
58{
59 struct bsc_handover *ho;
60
61 llist_for_each_entry(ho, &bsc_handovers, list) {
62 if (ho->new_lchan == new_lchan)
63 return ho;
64 }
65
66 return NULL;
67}
68
69static struct bsc_handover *bsc_ho_by_old_lchan(struct gsm_lchan *old_lchan)
70{
71 struct bsc_handover *ho;
72
73 llist_for_each_entry(ho, &bsc_handovers, list) {
74 if (ho->old_lchan == old_lchan)
75 return ho;
76 }
77
78 return NULL;
79}
80
81/* Hand over the specified logical channel to the specified new BTS.
82 * This is the main entry point for the actual handover algorithm,
83 * after it has decided it wants to initiate HO to a specific BTS */
84int bsc_handover_start(struct gsm_lchan *old_lchan, struct gsm_bts *bts)
85{
86 struct gsm_lchan *new_lchan;
87 struct bsc_handover *ho;
88 int rc;
89
90 new_lchan = lchan_alloc(bts, old_lchan->type);
91 if (!new_lchan)
92 return -ENOSPC;
93
94 ho = talloc_zero(NULL, struct bsc_handover);
95 if (!ho) {
96 lchan_free(new_lchan);
97 return -ENOMEM;
98 }
99 ho->old_lchan = old_lchan;
100 ho->new_lchan = new_lchan;
101
102 /* FIXME: do we have a better idea of the timing advance? */
103 rc = rsl_chan_activate_lchan(new_lchan, RSL_ACT_INTER_ASYNC, 0);
104 if (rc < 0) {
105 talloc_free(ho);
106 lchan_free(new_lchan);
107 return rc;
108 }
109
110 llist_add(&ho->list, &bsc_handovers);
111 /* we continue in the SS_LCHAN handler / ho_chan_activ_ack */
112
113 return 0;
114}
115
116/* T3103 expired: Handover has failed without HO COMPLETE or HO FAIL */
117static void ho_T3103_cb(void *_ho)
118{
119 struct bsc_handover *ho = _ho;
120
121 lchan_free(ho->new_lchan);
122 llist_del(&ho->list);
123 talloc_free(ho);
124}
125
126/* RSL has acknowledged activation of the new lchan */
127static int ho_chan_activ_ack(struct gsm_lchan *new_lchan)
128{
129 struct bsc_handover *ho;
130 int rc;
131
132 ho = bsc_ho_by_new_lchan(new_lchan);
133 if (!ho)
134 return -ENODEV;
135
136 /* we can now send the 04.08 HANDOVER COMMAND to the MS
137 * using the old lchan */
138
139 rc = gsm48_send_ho_cmd(ho->old_lchan, new_lchan, 0);
140
141 /* start T3103. We can continue either with T3103 expiration,
142 * 04.08 HANDOVER COMPLETE or 04.08 HANDOVER FAIL */
143 ho->T3103.cb = ho_T3103_cb;
144 bsc_schedule_timer(&ho->T3103, 10, 0);
145
146 return 0;
147}
148
149/* RSL has not acknowledged activation of the new lchan */
150static int ho_chan_activ_nack(struct gsm_lchan *new_lchan)
151{
152 struct bsc_handover *ho;
153
154 ho = bsc_ho_by_new_lchan(new_lchan);
155 if (!ho)
156 return -ENODEV;
157
158 llist_del(&ho->list);
159 talloc_free(ho);
160
161 /* FIXME: maybe we should try to allocate a new LCHAN here? */
162
163 return 0;
164}
165
166/* GSM 04.08 HANDOVER COMPLETE has been received on new channel */
167static int ho_gsm48_ho_compl(struct gsm_lchan *new_lchan)
168{
169 struct bsc_handover *ho;
170
171 ho = bsc_ho_by_new_lchan(new_lchan);
172 if (!ho)
173 return -ENODEV;
174
175 bsc_del_timer(&ho->T3103);
176 llist_del(&ho->list);
177
178 /* do something to re-route the actual speech frames ! */
179 //tch_remap(ho->old_lchan, ho->new_lchan);
180
181 /* release old lchan */
182 put_lchan(ho->old_lchan);
183
184 talloc_free(ho);
185
186 return 0;
187}
188
189/* GSM 04.08 HANDOVER FAIL has been received */
190static int ho_gsm48_ho_fail(struct gsm_lchan *old_lchan)
191{
192 struct bsc_handover *ho;
193
194 ho = bsc_ho_by_old_lchan(old_lchan);
195 if (!ho)
196 return -ENODEV;
197
198 bsc_del_timer(&ho->T3103);
199 llist_del(&ho->list);
200 put_lchan(ho->new_lchan);
201 talloc_free(ho);
202
203 return 0;
204}
205
206/* GSM 08.58 HANDOVER DETECT has been received */
207static int ho_rsl_detect(struct gsm_lchan *new_lchan)
208{
209 struct bsc_handover *ho;
210
211 ho = bsc_ho_by_old_lchan(new_lchan);
212 if (!ho)
213 return -ENODEV;
214
215 /* FIXME: do we actually want to do something here ? */
216
217 return 0;
218}
219
220static int ho_logic_sig_cb(unsigned int subsys, unsigned int signal,
221 void *handler_data, void *signal_data)
222{
223 struct gsm_lchan *lchan;
224
225 switch (subsys) {
226 case SS_LCHAN:
227 lchan = signal_data;
228 switch (signal) {
229 case S_LCHAN_ACTIVATE_ACK:
230 return ho_chan_activ_ack(lchan);
231 case S_LCHAN_ACTIVATE_NACK:
232 return ho_chan_activ_nack(lchan);
233 case S_LCHAN_HANDOVER_DETECT:
234 return ho_rsl_detect(lchan);
235 case S_LCHAN_HANDOVER_COMPL:
236 return ho_gsm48_ho_compl(lchan);
237 case S_LCHAN_HANDOVER_FAIL:
238 return ho_gsm48_ho_fail(lchan);
239 }
240 break;
241 default:
242 break;
243 }
244
245 return 0;
246}
247
248static __attribute__((constructor)) void on_dso_load_ho_logic(void)
249{
250 register_signal_handler(SS_LCHAN, ho_logic_sig_cb, NULL);
251}