blob: 3a81793356c13aa0cdfe1490093526025eb6c85c [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>
28
29#include <openbsc/gsm_subscriber.h>
Holger Freyther93795ac2009-03-31 04:35:19 +020030#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080031#include <openbsc/debug.h>
32#include <openbsc/db.h>
33
34
35LLIST_HEAD(active_subscribers);
36
37struct gsm_subscriber *subscr_alloc(void)
38{
39 struct gsm_subscriber *s;
40
41 s = malloc(sizeof(struct gsm_subscriber));
42 if (!s)
43 return NULL;
44
45 memset(s, 0, sizeof(*s));
46 llist_add_tail(&s->entry, &active_subscribers);
47 s->use_count = 1;
48
Holger Freyther93795ac2009-03-31 04:35:19 +020049 INIT_LLIST_HEAD(&s->requests);
50
Harald Welte59b04682009-06-10 05:40:52 +080051 return s;
52}
53
54static void subscr_free(struct gsm_subscriber *subscr)
55{
56 llist_del(&subscr->entry);
57 free(subscr);
58}
59
60struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi)
61{
62 struct gsm_subscriber *subscr;
63
64 /* we might have a record in memory already */
65 llist_for_each_entry(subscr, &active_subscribers, entry) {
66 if (strcmp(subscr->tmsi, tmsi) == 0)
67 return subscr_get(subscr);
68 }
69
70 return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi);
71}
72
73struct gsm_subscriber *subscr_get_by_imsi(const char *imsi)
74{
75 struct gsm_subscriber *subscr;
76
77 llist_for_each_entry(subscr, &active_subscribers, entry) {
78 if (strcmp(subscr->imsi, imsi) == 0)
79 return subscr_get(subscr);
80 }
81
82 return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
83}
84
85struct gsm_subscriber *subscr_get_by_extension(const char *ext)
86{
87 struct gsm_subscriber *subscr;
88
89 llist_for_each_entry(subscr, &active_subscribers, entry) {
90 if (strcmp(subscr->extension, ext) == 0)
91 return subscr_get(subscr);
92 }
93
94 return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext);
95}
96
97int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
98{
99 /* FIXME: Migrate pending requests from one BSC to another */
100 switch (reason) {
101 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
102 /* Indicate "attached to LAC" */
103 s->lac = bts->location_area_code;
104 break;
105 case GSM_SUBSCRIBER_UPDATE_DETACHED:
106 /* Only detach if we are currently in this area */
107 if (bts->location_area_code == s->lac)
108 s->lac = 0;
109
110 break;
111 default:
112 fprintf(stderr, "subscr_update with unknown reason: %d\n",
113 reason);
114 break;
115 };
116 return db_sync_subscriber(s);
117}
118
119struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
120{
121 subscr->use_count++;
122 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
123 subscr->extension, subscr->use_count);
124 return subscr;
125}
126
127struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
128{
129 subscr->use_count--;
130 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
131 subscr->extension, subscr->use_count);
132 if (subscr->use_count <= 0)
133 subscr_free(subscr);
134 return NULL;
135}
136
Holger Freyther93795ac2009-03-31 04:35:19 +0200137void subscr_get_channel(struct gsm_subscriber *subscr,
138 struct gsm_network *network, int type,
139 gsm_cbfn *cbfn, void *param)
140{
141 paging_request(network, subscr, type, cbfn, param);
142}
143
Harald Welte59b04682009-06-10 05:40:52 +0800144void subscr_put_channel(struct gsm_lchan *lchan)
145{
146 /*
147 * FIXME: Continue with other requests now... by checking
148 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
149 * of the lchan after having asked the next requestee to handle
150 * the channel.
151 */
152 put_lchan(lchan);
153}
Holger Freyther93795ac2009-03-31 04:35:19 +0200154