blob: 3062a6befe34f944fabaa896dbe1d3029a62208d [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
30#include <openbsc/gsm_subscriber.h>
Holger Freyther93795ac2009-03-31 04:35:19 +020031#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080032#include <openbsc/debug.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020033#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/db.h>
35
Harald Welte59b04682009-06-10 05:40:52 +080036LLIST_HEAD(active_subscribers);
37
Holger Freyther8bdf7622009-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 Welte59b04682009-06-10 05:40:52 +080099struct gsm_subscriber *subscr_alloc(void)
100{
101 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));
108 llist_add_tail(&s->entry, &active_subscribers);
109 s->use_count = 1;
110
Holger Freyther93795ac2009-03-31 04:35:19 +0200111 INIT_LLIST_HEAD(&s->requests);
112
Harald Welte59b04682009-06-10 05:40:52 +0800113 return s;
114}
115
116static void subscr_free(struct gsm_subscriber *subscr)
117{
118 llist_del(&subscr->entry);
119 free(subscr);
120}
121
122struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi)
123{
124 struct gsm_subscriber *subscr;
125
126 /* 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);
130 }
131
132 return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi);
133}
134
135struct gsm_subscriber *subscr_get_by_imsi(const char *imsi)
136{
137 struct gsm_subscriber *subscr;
138
139 llist_for_each_entry(subscr, &active_subscribers, entry) {
140 if (strcmp(subscr->imsi, imsi) == 0)
141 return subscr_get(subscr);
142 }
143
144 return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
145}
146
147struct 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
159int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
160{
161 /* FIXME: Migrate pending requests from one BSC to another */
162 switch (reason) {
163 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
164 /* Indicate "attached to LAC" */
165 s->lac = bts->location_area_code;
166 break;
167 case GSM_SUBSCRIBER_UPDATE_DETACHED:
168 /* Only detach if we are currently in this area */
169 if (bts->location_area_code == s->lac)
170 s->lac = 0;
171
172 break;
173 default:
174 fprintf(stderr, "subscr_update with unknown reason: %d\n",
175 reason);
176 break;
177 };
178 return db_sync_subscriber(s);
179}
180
181struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
182{
183 subscr->use_count++;
184 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
185 subscr->extension, subscr->use_count);
186 return subscr;
187}
188
189struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
190{
191 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)
195 subscr_free(subscr);
196 return NULL;
197}
198
Holger Freyther93795ac2009-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 Freyther8bdf7622009-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 Freyther93795ac2009-03-31 04:35:19 +0200233}
234
Harald Welte59b04682009-06-10 05:40:52 +0800235void 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 Freyther8bdf7622009-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
Harald Welte59b04682009-06-10 05:40:52 +0800255 put_lchan(lchan);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200256
257 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
258 subscr_send_paging_request(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800259}
Holger Freyther93795ac2009-03-31 04:35:19 +0200260