blob: c06b1ce6bc3cc34c88ede84810c4063d654be0cb [file] [log] [blame]
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +02001/* The concept of a subscriber as seen by the BSC */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther66efcbc2010-06-30 09:22:31 +08004 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by On Waves
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <assert.h>
30
Harald Weltedfe6c7d2010-02-20 16:24:02 +010031#include <osmocore/talloc.h>
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020032#include <openbsc/gsm_subscriber.h>
33#include <openbsc/paging.h>
34#include <openbsc/debug.h>
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020035
36LLIST_HEAD(active_subscribers);
37void *tall_subscr_ctx;
38void *tall_sub_req_ctx;
39
40/* for the gsm_subscriber.c */
41struct llist_head *subscr_bsc_active_subscriber(void)
42{
43 return &active_subscribers;
44}
45
46/*
47 * Struct for pending channel requests. This is managed in the
48 * llist_head requests of each subscriber. The reference counting
49 * should work in such a way that a subscriber with a pending request
50 * remains in memory.
51 */
52struct subscr_request {
53 struct llist_head entry;
54
55 /* back reference */
56 struct gsm_subscriber *subscr;
57
58 /* the requested channel type */
59 int channel_type;
60
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020061 /* the callback data */
62 gsm_cbfn *cbfn;
63 void *param;
64};
65
66/*
67 * We got the channel assigned and can now hand this channel
68 * over to one of our callbacks.
69 */
70static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
71 struct msgb *msg, void *data, void *param)
72{
73 struct subscr_request *request;
74 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
75
Holger Hans Peter Freyther9f16a862009-08-22 11:18:13 +020076 /* There is no request anymore... */
77 if (llist_empty(&subscr->requests))
78 return -1;
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020079
80 /*
81 * FIXME: What to do with paging requests coming during
82 * this callback? We must be sure to not start paging when
83 * we have an active connection to a subscriber and to make
84 * the subscr_put_channel work as required...
85 */
86 request = (struct subscr_request *)subscr->requests.next;
87 llist_del(&request->entry);
88 subscr->in_callback = 1;
89 request->cbfn(hooknum, event, msg, data, request->param);
90 subscr->in_callback = 0;
91
Holger Hans Peter Freyther66efcbc2010-06-30 09:22:31 +080092 subscr_put(subscr);
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020093 talloc_free(request);
94 return 0;
95}
96
97static void subscr_send_paging_request(struct gsm_subscriber *subscr)
98{
99 struct subscr_request *request;
Holger Hans Peter Freyther049935b2009-08-21 02:35:28 +0200100 int rc;
101
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200102 assert(!llist_empty(&subscr->requests));
103
104 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther049935b2009-08-21 02:35:28 +0200105 rc = paging_request(subscr->net, subscr, request->channel_type,
106 subscr_paging_cb, subscr);
107
108 /* paging failed, quit now */
109 if (rc <= 0) {
110 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
Holger Hans Peter Freyther667cdd62010-06-16 15:17:59 +0800111 NULL, NULL, subscr);
Holger Hans Peter Freyther049935b2009-08-21 02:35:28 +0200112 }
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200113}
114
115struct gsm_subscriber *subscr_alloc(void)
116{
117 struct gsm_subscriber *s;
118
Holger Hans Peter Freyther5ea73132009-10-29 02:29:45 +0100119 s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber);
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200120 if (!s)
121 return NULL;
122
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200123 llist_add_tail(&s->entry, &active_subscribers);
124 s->use_count = 1;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200125 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200126
127 INIT_LLIST_HEAD(&s->requests);
128
129 return s;
130}
131
132static void subscr_free(struct gsm_subscriber *subscr)
133{
134 llist_del(&subscr->entry);
135 talloc_free(subscr);
136}
137
138struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
139{
140 subscr->use_count++;
Harald Welted0c19142009-12-24 11:46:44 +0100141 DEBUGP(DREF, "subscr %s usage increases usage to: %d\n",
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200142 subscr->extension, subscr->use_count);
143 return subscr;
144}
145
146struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
147{
148 subscr->use_count--;
Harald Welted0c19142009-12-24 11:46:44 +0100149 DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n",
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200150 subscr->extension, subscr->use_count);
151 if (subscr->use_count <= 0)
152 subscr_free(subscr);
153 return NULL;
154}
155
156void subscr_get_channel(struct gsm_subscriber *subscr,
Holger Hans Peter Freythere4e8bf42009-08-20 13:33:51 +0200157 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200158{
159 struct subscr_request *request;
160
161 request = talloc(tall_sub_req_ctx, struct subscr_request);
162 if (!request) {
163 if (cbfn)
164 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
165 NULL, NULL, param);
166 return;
167 }
168
169 memset(request, 0, sizeof(*request));
Holger Hans Peter Freyther66efcbc2010-06-30 09:22:31 +0800170 request->subscr = subscr_get(subscr);
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200171 request->channel_type = type;
172 request->cbfn = cbfn;
173 request->param = param;
174
175 /*
176 * FIXME: We might be able to assign more than one
177 * channel, e.g. voice and SMS submit at the same
178 * time.
179 */
180 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
181 /* add to the list, send a request */
182 llist_add_tail(&request->entry, &subscr->requests);
183 subscr_send_paging_request(subscr);
184 } else {
185 /* this will be picked up later, from subscr_put_channel */
186 llist_add_tail(&request->entry, &subscr->requests);
187 }
188}
189
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800190void subscr_put_channel(struct gsm_subscriber_connection *conn)
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200191{
192 /*
193 * FIXME: Continue with other requests now... by checking
194 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
195 * of the lchan after having asked the next requestee to handle
196 * the channel.
197 */
198 /*
199 * FIXME: is the lchan is of a different type we could still
200 * issue an immediate assignment for another channel and then
201 * close this one.
202 */
203 /*
204 * Currently we will drop the last ref of the lchan which
205 * will result in a channel release on RSL and we will start
206 * the paging. This should work most of the time as the MS
207 * will listen to the paging requests before we timeout
208 */
209
Holger Hans Peter Freytherbddd1522010-06-16 14:02:41 +0800210 if (conn->subscr && !llist_empty(&conn->subscr->requests))
211 subscr_send_paging_request(conn->subscr);
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200212}
213