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