blob: 3f608ec30aec8a69edcc9dceea871a97e4f9509a [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>
Harald Welte52b1f982008-12-23 20:25:15 +000028
Harald Welte8470bf22008-12-25 23:28:35 +000029#include <openbsc/gsm_subscriber.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000030#include <openbsc/debug.h>
Harald Welte75a983f2008-12-27 21:34:06 +000031#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Holger Freyther12aa50d2009-01-01 18:02:05 +000033
34LLIST_HEAD(active_subscribers);
35
Harald Welte75a983f2008-12-27 21:34:06 +000036struct gsm_subscriber *subscr_alloc(void)
Harald Welte52b1f982008-12-23 20:25:15 +000037{
Harald Welte75a983f2008-12-27 21:34:06 +000038 struct gsm_subscriber *s;
39
40 s = malloc(sizeof(struct gsm_subscriber));
41 if (!s)
42 return NULL;
43
44 memset(s, 0, sizeof(*s));
Holger Freyther12aa50d2009-01-01 18:02:05 +000045 llist_add_tail(&s->entry, &active_subscribers);
46 s->use_count = 1;
Harald Welte75a983f2008-12-27 21:34:06 +000047
48 return s;
Harald Welte52b1f982008-12-23 20:25:15 +000049}
Harald Welte75a983f2008-12-27 21:34:06 +000050
Holger Freyther12aa50d2009-01-01 18:02:05 +000051static void subscr_free(struct gsm_subscriber *subscr)
Harald Welte52b1f982008-12-23 20:25:15 +000052{
Holger Freyther12aa50d2009-01-01 18:02:05 +000053 llist_del(&subscr->entry);
Harald Welte75a983f2008-12-27 21:34:06 +000054 free(subscr);
55}
56
Holger Freythera471a412009-01-04 03:47:05 +000057struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +000058{
Holger Freyther12aa50d2009-01-01 18:02:05 +000059 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000060
Holger Freyther12aa50d2009-01-01 18:02:05 +000061 /* we might have a record in memory already */
62 llist_for_each_entry(subscr, &active_subscribers, entry) {
63 if (strcmp(subscr->tmsi, tmsi) == 0)
64 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000065 }
66
Holger Freyther12aa50d2009-01-01 18:02:05 +000067 return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte75a983f2008-12-27 21:34:06 +000068}
69
Holger Freythera471a412009-01-04 03:47:05 +000070struct gsm_subscriber *subscr_get_by_imsi(const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +000071{
Holger Freyther12aa50d2009-01-01 18:02:05 +000072 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +000073
Holger Freyther12aa50d2009-01-01 18:02:05 +000074 llist_for_each_entry(subscr, &active_subscribers, entry) {
75 if (strcmp(subscr->imsi, imsi) == 0)
76 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +000077 }
78
Holger Freyther12aa50d2009-01-01 18:02:05 +000079 return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +000080}
81
Holger Freyther9c564b82009-02-09 23:39:20 +000082struct gsm_subscriber *subscr_get_by_extension(const char *ext)
83{
84 struct gsm_subscriber *subscr;
85
86 llist_for_each_entry(subscr, &active_subscribers, entry) {
87 if (strcmp(subscr->extension, ext) == 0)
88 return subscr_get(subscr);
89 }
90
91 return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext);
92}
93
Holger Freyther4a49e772009-04-12 05:37:29 +000094int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +000095{
Holger Freyther4a49e772009-04-12 05:37:29 +000096 /* FIXME: Migrate pending requests from one BSC to another */
97 switch (reason) {
98 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Holger Freyther6d5200b2009-06-02 02:54:38 +000099 /* Indicate "attached to LAC" */
100 s->lac = bts->location_area_code;
Holger Freyther4a49e772009-04-12 05:37:29 +0000101 break;
102 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000103 /* Only detach if we are currently in this area */
104 if (bts->location_area_code == s->lac)
Holger Freyther6d5200b2009-06-02 02:54:38 +0000105 s->lac = 0;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000106
Holger Freyther4a49e772009-04-12 05:37:29 +0000107 break;
108 default:
109 fprintf(stderr, "subscr_update with unknown reason: %d\n",
110 reason);
111 break;
112 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000113 return db_sync_subscriber(s);
114}
115
116struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
117{
118 subscr->use_count++;
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000119 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
120 subscr->extension, subscr->use_count);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000121 return subscr;
122}
123
124struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
125{
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000126 subscr->use_count--;
127 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
128 subscr->extension, subscr->use_count);
129 if (subscr->use_count <= 0)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000130 subscr_free(subscr);
131 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000132}
Holger Freythera1f92f02009-04-12 05:37:52 +0000133
134void subscr_put_channel(struct gsm_lchan *lchan)
135{
136 /*
137 * FIXME: Continue with other requests now... by checking
138 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
139 * of the lchan after having asked the next requestee to handle
140 * the channel.
141 */
142 put_lchan(lchan);
143}