blob: dee89c0bcd51c3efcda139b4344a18dde808a9e4 [file] [log] [blame]
Holger Hans Peter Freyther9a23af62009-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>
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#include <assert.h>
29
Harald Weltef4625b12010-02-20 16:24:02 +010030#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020031#include <openbsc/gsm_subscriber.h>
32#include <openbsc/paging.h>
33#include <openbsc/debug.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020034
35LLIST_HEAD(active_subscribers);
36void *tall_subscr_ctx;
37void *tall_sub_req_ctx;
38
39/* for the gsm_subscriber.c */
40struct llist_head *subscr_bsc_active_subscriber(void)
41{
42 return &active_subscribers;
43}
44
45/*
46 * Struct for pending channel requests. This is managed in the
47 * llist_head requests of each subscriber. The reference counting
48 * should work in such a way that a subscriber with a pending request
49 * remains in memory.
50 */
51struct subscr_request {
52 struct llist_head entry;
53
54 /* back reference */
55 struct gsm_subscriber *subscr;
56
57 /* the requested channel type */
58 int channel_type;
59
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020060 /* the callback data */
61 gsm_cbfn *cbfn;
62 void *param;
63};
64
65/*
66 * We got the channel assigned and can now hand this channel
67 * over to one of our callbacks.
68 */
69static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
70 struct msgb *msg, void *data, void *param)
71{
72 struct subscr_request *request;
73 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
74
Holger Hans Peter Freytheraa62bb92009-08-22 11:18:13 +020075 /* There is no request anymore... */
76 if (llist_empty(&subscr->requests))
77 return -1;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020078
79 /*
80 * FIXME: What to do with paging requests coming during
81 * this callback? We must be sure to not start paging when
82 * we have an active connection to a subscriber and to make
83 * the subscr_put_channel work as required...
84 */
85 request = (struct subscr_request *)subscr->requests.next;
86 llist_del(&request->entry);
87 subscr->in_callback = 1;
88 request->cbfn(hooknum, event, msg, data, request->param);
89 subscr->in_callback = 0;
90
91 talloc_free(request);
92 return 0;
93}
94
95static void subscr_send_paging_request(struct gsm_subscriber *subscr)
96{
97 struct subscr_request *request;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +020098 int rc;
99
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200100 assert(!llist_empty(&subscr->requests));
101
102 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +0200103 rc = paging_request(subscr->net, subscr, request->channel_type,
104 subscr_paging_cb, subscr);
105
106 /* paging failed, quit now */
107 if (rc <= 0) {
108 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
109 NULL, NULL, request->param);
110 }
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200111}
112
113struct gsm_subscriber *subscr_alloc(void)
114{
115 struct gsm_subscriber *s;
116
Holger Hans Peter Freythere4533482009-10-29 02:29:45 +0100117 s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200118 if (!s)
119 return NULL;
120
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200121 llist_add_tail(&s->entry, &active_subscribers);
122 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200123 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200124
125 INIT_LLIST_HEAD(&s->requests);
126
127 return s;
128}
129
130static void subscr_free(struct gsm_subscriber *subscr)
131{
132 llist_del(&subscr->entry);
133 talloc_free(subscr);
134}
135
136struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
137{
138 subscr->use_count++;
Harald Welte57fde552009-12-24 11:46:44 +0100139 DEBUGP(DREF, "subscr %s usage increases usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200140 subscr->extension, subscr->use_count);
141 return subscr;
142}
143
144struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
145{
146 subscr->use_count--;
Harald Welte57fde552009-12-24 11:46:44 +0100147 DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200148 subscr->extension, subscr->use_count);
149 if (subscr->use_count <= 0)
150 subscr_free(subscr);
151 return NULL;
152}
153
154void subscr_get_channel(struct gsm_subscriber *subscr,
Holger Hans Peter Freyther35672842009-08-20 13:33:51 +0200155 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200156{
157 struct subscr_request *request;
158
159 request = talloc(tall_sub_req_ctx, struct subscr_request);
160 if (!request) {
161 if (cbfn)
162 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
163 NULL, NULL, param);
164 return;
165 }
166
167 memset(request, 0, sizeof(*request));
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200168 request->subscr = subscr;
169 request->channel_type = type;
170 request->cbfn = cbfn;
171 request->param = param;
172
173 /*
174 * FIXME: We might be able to assign more than one
175 * channel, e.g. voice and SMS submit at the same
176 * time.
177 */
178 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
179 /* add to the list, send a request */
180 llist_add_tail(&request->entry, &subscr->requests);
181 subscr_send_paging_request(subscr);
182 } else {
183 /* this will be picked up later, from subscr_put_channel */
184 llist_add_tail(&request->entry, &subscr->requests);
185 }
186}
187
188void subscr_put_channel(struct gsm_lchan *lchan)
189{
190 /*
191 * FIXME: Continue with other requests now... by checking
192 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
193 * of the lchan after having asked the next requestee to handle
194 * the channel.
195 */
196 /*
197 * FIXME: is the lchan is of a different type we could still
198 * issue an immediate assignment for another channel and then
199 * close this one.
200 */
201 /*
202 * Currently we will drop the last ref of the lchan which
203 * will result in a channel release on RSL and we will start
204 * the paging. This should work most of the time as the MS
205 * will listen to the paging requests before we timeout
206 */
207
208 put_lchan(lchan);
209
210 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
211 subscr_send_paging_request(lchan->subscr);
212}
213