blob: 83addd0a88d7e07a59036279a1e403768de21ffe [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
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
61 /* the bts we have decided to use */
62 struct gsm_network *network;
63
64 /* the callback data */
65 gsm_cbfn *cbfn;
66 void *param;
67};
68
69/*
70 * We got the channel assigned and can now hand this channel
71 * over to one of our callbacks.
72 */
73static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
74 struct msgb *msg, void *data, void *param)
75{
76 struct subscr_request *request;
77 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
78
79 assert(!llist_empty(&subscr->requests));
80
81 /*
82 * FIXME: What to do with paging requests coming during
83 * this callback? We must be sure to not start paging when
84 * we have an active connection to a subscriber and to make
85 * the subscr_put_channel work as required...
86 */
87 request = (struct subscr_request *)subscr->requests.next;
88 llist_del(&request->entry);
89 subscr->in_callback = 1;
90 request->cbfn(hooknum, event, msg, data, request->param);
91 subscr->in_callback = 0;
92
93 talloc_free(request);
94 return 0;
95}
96
97static void subscr_send_paging_request(struct gsm_subscriber *subscr)
98{
99 struct subscr_request *request;
100 assert(!llist_empty(&subscr->requests));
101
102 request = (struct subscr_request *)subscr->requests.next;
103 paging_request(request->network, subscr, request->channel_type,
104 subscr_paging_cb, subscr);
105}
106
107struct gsm_subscriber *subscr_alloc(void)
108{
109 struct gsm_subscriber *s;
110
111 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
112 if (!s)
113 return NULL;
114
115 memset(s, 0, sizeof(*s));
116 llist_add_tail(&s->entry, &active_subscribers);
117 s->use_count = 1;
118
119 INIT_LLIST_HEAD(&s->requests);
120
121 return s;
122}
123
124static void subscr_free(struct gsm_subscriber *subscr)
125{
126 llist_del(&subscr->entry);
127 talloc_free(subscr);
128}
129
130struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
131{
132 subscr->use_count++;
133 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
134 subscr->extension, subscr->use_count);
135 return subscr;
136}
137
138struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
139{
140 subscr->use_count--;
141 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
142 subscr->extension, subscr->use_count);
143 if (subscr->use_count <= 0)
144 subscr_free(subscr);
145 return NULL;
146}
147
148void subscr_get_channel(struct gsm_subscriber *subscr,
149 struct gsm_network *network, int type,
150 gsm_cbfn *cbfn, void *param)
151{
152 struct subscr_request *request;
153
154 request = talloc(tall_sub_req_ctx, struct subscr_request);
155 if (!request) {
156 if (cbfn)
157 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
158 NULL, NULL, param);
159 return;
160 }
161
162 memset(request, 0, sizeof(*request));
163 request->network = network;
164 request->subscr = subscr;
165 request->channel_type = type;
166 request->cbfn = cbfn;
167 request->param = param;
168
169 /*
170 * FIXME: We might be able to assign more than one
171 * channel, e.g. voice and SMS submit at the same
172 * time.
173 */
174 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
175 /* add to the list, send a request */
176 llist_add_tail(&request->entry, &subscr->requests);
177 subscr_send_paging_request(subscr);
178 } else {
179 /* this will be picked up later, from subscr_put_channel */
180 llist_add_tail(&request->entry, &subscr->requests);
181 }
182}
183
184void subscr_put_channel(struct gsm_lchan *lchan)
185{
186 /*
187 * FIXME: Continue with other requests now... by checking
188 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
189 * of the lchan after having asked the next requestee to handle
190 * the channel.
191 */
192 /*
193 * FIXME: is the lchan is of a different type we could still
194 * issue an immediate assignment for another channel and then
195 * close this one.
196 */
197 /*
198 * Currently we will drop the last ref of the lchan which
199 * will result in a channel release on RSL and we will start
200 * the paging. This should work most of the time as the MS
201 * will listen to the paging requests before we timeout
202 */
203
204 put_lchan(lchan);
205
206 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
207 subscr_send_paging_request(lchan->subscr);
208}
209