blob: 54db44774d4b7124a0b1206a594b56a8e3a2c96c [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>
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
30#include <openbsc/talloc.h>
31#include <openbsc/gsm_subscriber.h>
32#include <openbsc/paging.h>
33#include <openbsc/debug.h>
34#include <openbsc/paging.h>
35
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
76 assert(!llist_empty(&subscr->requests));
77
78 /*
79 * FIXME: What to do with paging requests coming during
80 * this callback? We must be sure to not start paging when
81 * we have an active connection to a subscriber and to make
82 * the subscr_put_channel work as required...
83 */
84 request = (struct subscr_request *)subscr->requests.next;
85 llist_del(&request->entry);
86 subscr->in_callback = 1;
87 request->cbfn(hooknum, event, msg, data, request->param);
88 subscr->in_callback = 0;
89
90 talloc_free(request);
91 return 0;
92}
93
94static void subscr_send_paging_request(struct gsm_subscriber *subscr)
95{
96 struct subscr_request *request;
Holger Hans Peter Freyther049935b2009-08-21 02:35:28 +020097 int rc;
98
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020099 assert(!llist_empty(&subscr->requests));
100
101 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther049935b2009-08-21 02:35:28 +0200102 rc = paging_request(subscr->net, subscr, request->channel_type,
103 subscr_paging_cb, subscr);
104
105 /* paging failed, quit now */
106 if (rc <= 0) {
107 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
108 NULL, NULL, request->param);
109 }
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200110}
111
112struct gsm_subscriber *subscr_alloc(void)
113{
114 struct gsm_subscriber *s;
115
116 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
117 if (!s)
118 return NULL;
119
120 memset(s, 0, sizeof(*s));
121 llist_add_tail(&s->entry, &active_subscribers);
122 s->use_count = 1;
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200123 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther857e5e62009-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++;
139 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
140 subscr->extension, subscr->use_count);
141 return subscr;
142}
143
144struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
145{
146 subscr->use_count--;
147 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
148 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 Freythere4e8bf42009-08-20 13:33:51 +0200155 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther857e5e62009-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 Freyther857e5e62009-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