blob: 3062a6befe34f944fabaa896dbe1d3029a62208d [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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
Harald Welte75a983f2008-12-27 21:34:06 +000024#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Harald Welte8470bf22008-12-25 23:28:35 +000030#include <openbsc/gsm_subscriber.h>
Holger Freyther04866d42009-03-31 04:35:19 +020031#include <openbsc/paging.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000032#include <openbsc/debug.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020033#include <openbsc/paging.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Holger Freyther12aa50d2009-01-01 18:02:05 +000036LLIST_HEAD(active_subscribers);
37
Holger Freyther85a7b362009-04-18 13:48:55 +020038/*
39 * Struct for pending channel requests. This is managed in the
40 * llist_head requests of each subscriber. The reference counting
41 * should work in such a way that a subscriber with a pending request
42 * remains in memory.
43 */
44struct subscr_request {
45 struct llist_head entry;
46
47 /* back reference */
48 struct gsm_subscriber *subscr;
49
50 /* the requested channel type */
51 int channel_type;
52
53 /* the bts we have decided to use */
54 struct gsm_network *network;
55
56 /* the callback data */
57 gsm_cbfn *cbfn;
58 void *param;
59};
60
61/*
62 * We got the channel assigned and can now hand this channel
63 * over to one of our callbacks.
64 */
65static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
66 struct msgb *msg, void *data, void *param)
67{
68 struct subscr_request *request;
69 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
70
71 assert(!llist_empty(&subscr->requests));
72
73 /*
74 * FIXME: What to do with paging requests coming during
75 * this callback? We must be sure to not start paging when
76 * we have an active connection to a subscriber and to make
77 * the subscr_put_channel work as required...
78 */
79 request = (struct subscr_request *)subscr->requests.next;
80 llist_del(&request->entry);
81 subscr->in_callback = 1;
82 request->cbfn(hooknum, event, msg, data, request->param);
83 subscr->in_callback = 0;
84
85 free(request);
86 return 0;
87}
88
89static void subscr_send_paging_request(struct gsm_subscriber *subscr)
90{
91 struct subscr_request *request;
92 assert(!llist_empty(&subscr->requests));
93
94 request = (struct subscr_request *)subscr->requests.next;
95 paging_request(request->network, subscr, request->channel_type,
96 subscr_paging_cb, subscr);
97}
98
Harald Welte75a983f2008-12-27 21:34:06 +000099struct gsm_subscriber *subscr_alloc(void)
Harald Welte52b1f982008-12-23 20:25:15 +0000100{
Harald Welte75a983f2008-12-27 21:34:06 +0000101 struct gsm_subscriber *s;
102
103 s = malloc(sizeof(struct gsm_subscriber));
104 if (!s)
105 return NULL;
106
107 memset(s, 0, sizeof(*s));
Holger Freyther12aa50d2009-01-01 18:02:05 +0000108 llist_add_tail(&s->entry, &active_subscribers);
109 s->use_count = 1;
Harald Welte75a983f2008-12-27 21:34:06 +0000110
Holger Freyther04866d42009-03-31 04:35:19 +0200111 INIT_LLIST_HEAD(&s->requests);
112
Harald Welte75a983f2008-12-27 21:34:06 +0000113 return s;
Harald Welte52b1f982008-12-23 20:25:15 +0000114}
Harald Welte75a983f2008-12-27 21:34:06 +0000115
Holger Freyther12aa50d2009-01-01 18:02:05 +0000116static void subscr_free(struct gsm_subscriber *subscr)
Harald Welte52b1f982008-12-23 20:25:15 +0000117{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000118 llist_del(&subscr->entry);
Harald Welte75a983f2008-12-27 21:34:06 +0000119 free(subscr);
120}
121
Holger Freythera471a412009-01-04 03:47:05 +0000122struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000123{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000124 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000125
Holger Freyther12aa50d2009-01-01 18:02:05 +0000126 /* we might have a record in memory already */
127 llist_for_each_entry(subscr, &active_subscribers, entry) {
128 if (strcmp(subscr->tmsi, tmsi) == 0)
129 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000130 }
131
Holger Freyther12aa50d2009-01-01 18:02:05 +0000132 return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte75a983f2008-12-27 21:34:06 +0000133}
134
Holger Freythera471a412009-01-04 03:47:05 +0000135struct gsm_subscriber *subscr_get_by_imsi(const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000136{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000137 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000138
Holger Freyther12aa50d2009-01-01 18:02:05 +0000139 llist_for_each_entry(subscr, &active_subscribers, entry) {
140 if (strcmp(subscr->imsi, imsi) == 0)
141 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000142 }
143
Holger Freyther12aa50d2009-01-01 18:02:05 +0000144 return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000145}
146
Holger Freyther9c564b82009-02-09 23:39:20 +0000147struct gsm_subscriber *subscr_get_by_extension(const char *ext)
148{
149 struct gsm_subscriber *subscr;
150
151 llist_for_each_entry(subscr, &active_subscribers, entry) {
152 if (strcmp(subscr->extension, ext) == 0)
153 return subscr_get(subscr);
154 }
155
156 return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext);
157}
158
Holger Freyther4a49e772009-04-12 05:37:29 +0000159int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000160{
Holger Freyther4a49e772009-04-12 05:37:29 +0000161 /* FIXME: Migrate pending requests from one BSC to another */
162 switch (reason) {
163 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Holger Freyther6d5200b2009-06-02 02:54:38 +0000164 /* Indicate "attached to LAC" */
165 s->lac = bts->location_area_code;
Holger Freyther4a49e772009-04-12 05:37:29 +0000166 break;
167 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000168 /* Only detach if we are currently in this area */
169 if (bts->location_area_code == s->lac)
Holger Freyther6d5200b2009-06-02 02:54:38 +0000170 s->lac = 0;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000171
Holger Freyther4a49e772009-04-12 05:37:29 +0000172 break;
173 default:
174 fprintf(stderr, "subscr_update with unknown reason: %d\n",
175 reason);
176 break;
177 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000178 return db_sync_subscriber(s);
179}
180
181struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
182{
183 subscr->use_count++;
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000184 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
185 subscr->extension, subscr->use_count);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000186 return subscr;
187}
188
189struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
190{
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000191 subscr->use_count--;
192 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
193 subscr->extension, subscr->use_count);
194 if (subscr->use_count <= 0)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000195 subscr_free(subscr);
196 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
Holger Freythera1f92f02009-04-12 05:37:52 +0000198
Holger Freyther04866d42009-03-31 04:35:19 +0200199void subscr_get_channel(struct gsm_subscriber *subscr,
200 struct gsm_network *network, int type,
201 gsm_cbfn *cbfn, void *param)
202{
Holger Freyther85a7b362009-04-18 13:48:55 +0200203 struct subscr_request *request;
204
205 request = (struct subscr_request *)malloc(sizeof(*request));
206 if (!request) {
207 if (cbfn)
208 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
209 NULL, NULL, param);
210 return;
211 }
212
213 memset(request, 0, sizeof(*request));
214 request->network = network;
215 request->subscr = subscr;
216 request->channel_type = type;
217 request->cbfn = cbfn;
218 request->param = param;
219
220 /*
221 * FIXME: We might be able to assign more than one
222 * channel, e.g. voice and SMS submit at the same
223 * time.
224 */
225 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
226 /* add to the list, send a request */
227 llist_add_tail(&request->entry, &subscr->requests);
228 subscr_send_paging_request(subscr);
229 } else {
230 /* this will be picked up later, from subscr_put_channel */
231 llist_add_tail(&request->entry, &subscr->requests);
232 }
Holger Freyther04866d42009-03-31 04:35:19 +0200233}
234
Holger Freythera1f92f02009-04-12 05:37:52 +0000235void subscr_put_channel(struct gsm_lchan *lchan)
236{
237 /*
238 * FIXME: Continue with other requests now... by checking
239 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
240 * of the lchan after having asked the next requestee to handle
241 * the channel.
242 */
Holger Freyther85a7b362009-04-18 13:48:55 +0200243 /*
244 * FIXME: is the lchan is of a different type we could still
245 * issue an immediate assignment for another channel and then
246 * close this one.
247 */
248 /*
249 * Currently we will drop the last ref of the lchan which
250 * will result in a channel release on RSL and we will start
251 * the paging. This should work most of the time as the MS
252 * will listen to the paging requests before we timeout
253 */
254
Holger Freythera1f92f02009-04-12 05:37:52 +0000255 put_lchan(lchan);
Holger Freyther85a7b362009-04-18 13:48:55 +0200256
257 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
258 subscr_send_paging_request(lchan->subscr);
Holger Freythera1f92f02009-04-12 05:37:52 +0000259}
Holger Freyther04866d42009-03-31 04:35:19 +0200260