blob: 868b3559914987197de28f4dbab1c46271c1a3a9 [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
Holger Hans Peter Freyther9a23af62009-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 Freytheraa62bb92009-08-22 11:18:13 +020076 /* There is no request anymore... */
77 if (llist_empty(&subscr->requests))
78 return -1;
Holger Hans Peter Freyther9a23af62009-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
92 talloc_free(request);
93 return 0;
94}
95
96static void subscr_send_paging_request(struct gsm_subscriber *subscr)
97{
98 struct subscr_request *request;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +020099 int rc;
100
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200101 assert(!llist_empty(&subscr->requests));
102
103 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +0200104 rc = paging_request(subscr->net, subscr, request->channel_type,
105 subscr_paging_cb, subscr);
106
107 /* paging failed, quit now */
108 if (rc <= 0) {
109 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
110 NULL, NULL, request->param);
111 }
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200112}
113
114struct gsm_subscriber *subscr_alloc(void)
115{
116 struct gsm_subscriber *s;
117
118 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
119 if (!s)
120 return NULL;
121
122 memset(s, 0, sizeof(*s));
123 llist_add_tail(&s->entry, &active_subscribers);
124 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200125 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-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++;
141 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
142 subscr->extension, subscr->use_count);
143 return subscr;
144}
145
146struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
147{
148 subscr->use_count--;
149 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
150 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 Freyther35672842009-08-20 13:33:51 +0200157 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther9a23af62009-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 Freyther9a23af62009-06-12 07:08:13 +0200170 request->subscr = subscr;
171 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
190void subscr_put_channel(struct gsm_lchan *lchan)
191{
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
210 put_lchan(lchan);
211
212 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
213 subscr_send_paging_request(lchan->subscr);
214}
215