blob: e892906231d4dd958dcad8f883293d4e10c4f997 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte59b04682009-06-10 05:40:52 +080029
Harald Weltea8379772009-06-20 22:36:41 +020030#include <openbsc/talloc.h>
Harald Welte59b04682009-06-10 05:40:52 +080031#include <openbsc/gsm_subscriber.h>
Holger Freyther93795ac2009-03-31 04:35:19 +020032#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/debug.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020034#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/db.h>
36
Harald Welte59b04682009-06-10 05:40:52 +080037LLIST_HEAD(active_subscribers);
Harald Weltea8379772009-06-20 22:36:41 +020038static void *tall_subscr_ctx;
39static void *tall_sub_req_ctx;
Harald Welte59b04682009-06-10 05:40:52 +080040
Holger Freyther8bdf7622009-04-18 13:48:55 +020041/*
42 * Struct for pending channel requests. This is managed in the
43 * llist_head requests of each subscriber. The reference counting
44 * should work in such a way that a subscriber with a pending request
45 * remains in memory.
46 */
47struct subscr_request {
48 struct llist_head entry;
49
50 /* back reference */
51 struct gsm_subscriber *subscr;
52
53 /* the requested channel type */
54 int channel_type;
55
56 /* the bts we have decided to use */
57 struct gsm_network *network;
58
59 /* the callback data */
60 gsm_cbfn *cbfn;
61 void *param;
62};
63
64/*
65 * We got the channel assigned and can now hand this channel
66 * over to one of our callbacks.
67 */
68static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
69 struct msgb *msg, void *data, void *param)
70{
71 struct subscr_request *request;
72 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
73
74 assert(!llist_empty(&subscr->requests));
75
76 /*
77 * FIXME: What to do with paging requests coming during
78 * this callback? We must be sure to not start paging when
79 * we have an active connection to a subscriber and to make
80 * the subscr_put_channel work as required...
81 */
82 request = (struct subscr_request *)subscr->requests.next;
83 llist_del(&request->entry);
84 subscr->in_callback = 1;
85 request->cbfn(hooknum, event, msg, data, request->param);
86 subscr->in_callback = 0;
87
Harald Weltea8379772009-06-20 22:36:41 +020088 talloc_free(request);
Holger Freyther8bdf7622009-04-18 13:48:55 +020089 return 0;
90}
91
92static void subscr_send_paging_request(struct gsm_subscriber *subscr)
93{
94 struct subscr_request *request;
95 assert(!llist_empty(&subscr->requests));
96
97 request = (struct subscr_request *)subscr->requests.next;
98 paging_request(request->network, subscr, request->channel_type,
99 subscr_paging_cb, subscr);
100}
101
Harald Welte59b04682009-06-10 05:40:52 +0800102struct gsm_subscriber *subscr_alloc(void)
103{
104 struct gsm_subscriber *s;
105
Harald Weltea8379772009-06-20 22:36:41 +0200106 if (!tall_subscr_ctx)
107 tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1,
108 "subscriber");
109
110 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
Harald Welte59b04682009-06-10 05:40:52 +0800111 if (!s)
112 return NULL;
113
114 memset(s, 0, sizeof(*s));
115 llist_add_tail(&s->entry, &active_subscribers);
116 s->use_count = 1;
117
Holger Freyther93795ac2009-03-31 04:35:19 +0200118 INIT_LLIST_HEAD(&s->requests);
119
Harald Welte59b04682009-06-10 05:40:52 +0800120 return s;
121}
122
123static void subscr_free(struct gsm_subscriber *subscr)
124{
125 llist_del(&subscr->entry);
Harald Weltea8379772009-06-20 22:36:41 +0200126 talloc_free(subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800127}
128
Harald Welteaae7a522009-07-23 19:21:02 +0200129struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
130 const char *tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800131{
132 struct gsm_subscriber *subscr;
133
134 /* we might have a record in memory already */
135 llist_for_each_entry(subscr, &active_subscribers, entry) {
136 if (strcmp(subscr->tmsi, tmsi) == 0)
137 return subscr_get(subscr);
138 }
139
Harald Welteaae7a522009-07-23 19:21:02 +0200140 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800141}
142
Harald Welteaae7a522009-07-23 19:21:02 +0200143struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
144 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +0800145{
146 struct gsm_subscriber *subscr;
147
148 llist_for_each_entry(subscr, &active_subscribers, entry) {
149 if (strcmp(subscr->imsi, imsi) == 0)
150 return subscr_get(subscr);
151 }
152
Harald Welteaae7a522009-07-23 19:21:02 +0200153 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800154}
155
Harald Welteaae7a522009-07-23 19:21:02 +0200156struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
157 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +0800158{
159 struct gsm_subscriber *subscr;
160
161 llist_for_each_entry(subscr, &active_subscribers, entry) {
162 if (strcmp(subscr->extension, ext) == 0)
163 return subscr_get(subscr);
164 }
165
Harald Welteaae7a522009-07-23 19:21:02 +0200166 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +0800167}
168
169int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
170{
171 /* FIXME: Migrate pending requests from one BSC to another */
172 switch (reason) {
173 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltea54b48d2009-07-23 18:56:43 +0200174 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800175 /* Indicate "attached to LAC" */
176 s->lac = bts->location_area_code;
177 break;
178 case GSM_SUBSCRIBER_UPDATE_DETACHED:
179 /* Only detach if we are currently in this area */
180 if (bts->location_area_code == s->lac)
181 s->lac = 0;
182
183 break;
184 default:
185 fprintf(stderr, "subscr_update with unknown reason: %d\n",
186 reason);
187 break;
188 };
189 return db_sync_subscriber(s);
190}
191
192struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
193{
194 subscr->use_count++;
195 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
196 subscr->extension, subscr->use_count);
197 return subscr;
198}
199
200struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
201{
202 subscr->use_count--;
203 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
204 subscr->extension, subscr->use_count);
205 if (subscr->use_count <= 0)
206 subscr_free(subscr);
207 return NULL;
208}
209
Holger Freyther93795ac2009-03-31 04:35:19 +0200210void subscr_get_channel(struct gsm_subscriber *subscr,
211 struct gsm_network *network, int type,
212 gsm_cbfn *cbfn, void *param)
213{
Holger Freyther8bdf7622009-04-18 13:48:55 +0200214 struct subscr_request *request;
215
Harald Weltea8379772009-06-20 22:36:41 +0200216 if (!tall_sub_req_ctx)
217 tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1,
218 "subscr_request");
219
220 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200221 if (!request) {
222 if (cbfn)
223 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
224 NULL, NULL, param);
225 return;
226 }
227
228 memset(request, 0, sizeof(*request));
229 request->network = network;
230 request->subscr = subscr;
231 request->channel_type = type;
232 request->cbfn = cbfn;
233 request->param = param;
234
235 /*
236 * FIXME: We might be able to assign more than one
237 * channel, e.g. voice and SMS submit at the same
238 * time.
239 */
240 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
241 /* add to the list, send a request */
242 llist_add_tail(&request->entry, &subscr->requests);
243 subscr_send_paging_request(subscr);
244 } else {
245 /* this will be picked up later, from subscr_put_channel */
246 llist_add_tail(&request->entry, &subscr->requests);
247 }
Holger Freyther93795ac2009-03-31 04:35:19 +0200248}
249
Harald Welte59b04682009-06-10 05:40:52 +0800250void subscr_put_channel(struct gsm_lchan *lchan)
251{
252 /*
253 * FIXME: Continue with other requests now... by checking
254 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
255 * of the lchan after having asked the next requestee to handle
256 * the channel.
257 */
Holger Freyther8bdf7622009-04-18 13:48:55 +0200258 /*
259 * FIXME: is the lchan is of a different type we could still
260 * issue an immediate assignment for another channel and then
261 * close this one.
262 */
263 /*
264 * Currently we will drop the last ref of the lchan which
265 * will result in a channel release on RSL and we will start
266 * the paging. This should work most of the time as the MS
267 * will listen to the paging requests before we timeout
268 */
269
Harald Welte59b04682009-06-10 05:40:52 +0800270 put_lchan(lchan);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200271
272 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
273 subscr_send_paging_request(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800274}
Holger Freyther93795ac2009-03-31 04:35:19 +0200275