blob: 345544d49a4fffdcda190c6de74f8d50284e0817 [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
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;
97 assert(!llist_empty(&subscr->requests));
98
99 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther35672842009-08-20 13:33:51 +0200100 paging_request(subscr->net, subscr, request->channel_type,
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200101 subscr_paging_cb, subscr);
102}
103
104struct gsm_subscriber *subscr_alloc(void)
105{
106 struct gsm_subscriber *s;
107
108 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
109 if (!s)
110 return NULL;
111
112 memset(s, 0, sizeof(*s));
113 llist_add_tail(&s->entry, &active_subscribers);
114 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200115 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200116
117 INIT_LLIST_HEAD(&s->requests);
118
119 return s;
120}
121
122static void subscr_free(struct gsm_subscriber *subscr)
123{
124 llist_del(&subscr->entry);
125 talloc_free(subscr);
126}
127
128struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
129{
130 subscr->use_count++;
131 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
132 subscr->extension, subscr->use_count);
133 return subscr;
134}
135
136struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
137{
138 subscr->use_count--;
139 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
140 subscr->extension, subscr->use_count);
141 if (subscr->use_count <= 0)
142 subscr_free(subscr);
143 return NULL;
144}
145
146void subscr_get_channel(struct gsm_subscriber *subscr,
Holger Hans Peter Freyther35672842009-08-20 13:33:51 +0200147 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200148{
149 struct subscr_request *request;
150
151 request = talloc(tall_sub_req_ctx, struct subscr_request);
152 if (!request) {
153 if (cbfn)
154 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
155 NULL, NULL, param);
156 return;
157 }
158
159 memset(request, 0, sizeof(*request));
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200160 request->subscr = subscr;
161 request->channel_type = type;
162 request->cbfn = cbfn;
163 request->param = param;
164
165 /*
166 * FIXME: We might be able to assign more than one
167 * channel, e.g. voice and SMS submit at the same
168 * time.
169 */
170 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
171 /* add to the list, send a request */
172 llist_add_tail(&request->entry, &subscr->requests);
173 subscr_send_paging_request(subscr);
174 } else {
175 /* this will be picked up later, from subscr_put_channel */
176 llist_add_tail(&request->entry, &subscr->requests);
177 }
178}
179
180void subscr_put_channel(struct gsm_lchan *lchan)
181{
182 /*
183 * FIXME: Continue with other requests now... by checking
184 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
185 * of the lchan after having asked the next requestee to handle
186 * the channel.
187 */
188 /*
189 * FIXME: is the lchan is of a different type we could still
190 * issue an immediate assignment for another channel and then
191 * close this one.
192 */
193 /*
194 * Currently we will drop the last ref of the lchan which
195 * will result in a channel release on RSL and we will start
196 * the paging. This should work most of the time as the MS
197 * will listen to the paging requests before we timeout
198 */
199
200 put_lchan(lchan);
201
202 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
203 subscr_send_paging_request(lchan->subscr);
204}
205