Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 1 | /* The concept of a subscriber as seen by the BSC */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 108d58a | 2010-06-30 09:22:31 +0800 | [diff] [blame] | 4 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010 by On Waves |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <assert.h> |
| 30 | |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 31 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
| 33 | #include <openbsc/paging.h> |
| 34 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 35 | |
| 36 | LLIST_HEAD(active_subscribers); |
| 37 | void *tall_subscr_ctx; |
| 38 | void *tall_sub_req_ctx; |
| 39 | |
| 40 | /* for the gsm_subscriber.c */ |
| 41 | struct 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 | */ |
| 52 | struct 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 Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 61 | /* 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 | */ |
| 70 | static 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 Freyther | aa62bb9 | 2009-08-22 11:18:13 +0200 | [diff] [blame] | 76 | /* There is no request anymore... */ |
| 77 | if (llist_empty(&subscr->requests)) |
| 78 | return -1; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 79 | |
| 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 | |
Holger Hans Peter Freyther | 108d58a | 2010-06-30 09:22:31 +0800 | [diff] [blame] | 92 | subscr_put(subscr); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 93 | talloc_free(request); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 98 | { |
| 99 | struct subscr_request *request; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 100 | int rc; |
| 101 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 102 | assert(!llist_empty(&subscr->requests)); |
| 103 | |
| 104 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 105 | rc = paging_request(subscr->net, subscr, request->channel_type, |
| 106 | subscr_paging_cb, subscr); |
| 107 | |
| 108 | /* paging failed, quit now */ |
| 109 | if (rc <= 0) { |
| 110 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, |
Holger Hans Peter Freyther | 2b2362f | 2010-06-16 15:17:59 +0800 | [diff] [blame] | 111 | NULL, NULL, subscr); |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 112 | } |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Holger Hans Peter Freyther | 3ea77a5 | 2010-06-30 12:56:41 +0800 | [diff] [blame] | 115 | char *subscr_name(struct gsm_subscriber *subscr) |
| 116 | { |
| 117 | if (strlen(subscr->name)) |
| 118 | return subscr->name; |
| 119 | |
| 120 | return subscr->imsi; |
| 121 | } |
| 122 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 123 | struct gsm_subscriber *subscr_alloc(void) |
| 124 | { |
| 125 | struct gsm_subscriber *s; |
| 126 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 127 | s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 128 | if (!s) |
| 129 | return NULL; |
| 130 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 131 | llist_add_tail(&s->entry, &active_subscribers); |
| 132 | s->use_count = 1; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 133 | s->tmsi = GSM_RESERVED_TMSI; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 134 | |
| 135 | INIT_LLIST_HEAD(&s->requests); |
| 136 | |
| 137 | return s; |
| 138 | } |
| 139 | |
| 140 | static void subscr_free(struct gsm_subscriber *subscr) |
| 141 | { |
| 142 | llist_del(&subscr->entry); |
| 143 | talloc_free(subscr); |
| 144 | } |
| 145 | |
| 146 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 147 | { |
| 148 | subscr->use_count++; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 149 | DEBUGP(DREF, "subscr %s usage increases usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 150 | subscr->extension, subscr->use_count); |
| 151 | return subscr; |
| 152 | } |
| 153 | |
| 154 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 155 | { |
| 156 | subscr->use_count--; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 157 | DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 158 | subscr->extension, subscr->use_count); |
| 159 | if (subscr->use_count <= 0) |
| 160 | subscr_free(subscr); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | void subscr_get_channel(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 3567284 | 2009-08-20 13:33:51 +0200 | [diff] [blame] | 165 | int type, gsm_cbfn *cbfn, void *param) |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 166 | { |
| 167 | struct subscr_request *request; |
| 168 | |
| 169 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 170 | if (!request) { |
| 171 | if (cbfn) |
| 172 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 173 | NULL, NULL, param); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | memset(request, 0, sizeof(*request)); |
Holger Hans Peter Freyther | 108d58a | 2010-06-30 09:22:31 +0800 | [diff] [blame] | 178 | request->subscr = subscr_get(subscr); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 179 | request->channel_type = type; |
| 180 | request->cbfn = cbfn; |
| 181 | request->param = param; |
| 182 | |
| 183 | /* |
| 184 | * FIXME: We might be able to assign more than one |
| 185 | * channel, e.g. voice and SMS submit at the same |
| 186 | * time. |
| 187 | */ |
| 188 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 189 | /* add to the list, send a request */ |
| 190 | llist_add_tail(&request->entry, &subscr->requests); |
| 191 | subscr_send_paging_request(subscr); |
| 192 | } else { |
| 193 | /* this will be picked up later, from subscr_put_channel */ |
| 194 | llist_add_tail(&request->entry, &subscr->requests); |
| 195 | } |
| 196 | } |
| 197 | |
Holger Hans Peter Freyther | 5e58a0e | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 198 | void subscr_put_channel(struct gsm_subscriber_connection *conn) |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 199 | { |
| 200 | /* |
| 201 | * FIXME: Continue with other requests now... by checking |
| 202 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 203 | * of the lchan after having asked the next requestee to handle |
| 204 | * the channel. |
| 205 | */ |
| 206 | /* |
| 207 | * FIXME: is the lchan is of a different type we could still |
| 208 | * issue an immediate assignment for another channel and then |
| 209 | * close this one. |
| 210 | */ |
| 211 | /* |
| 212 | * Currently we will drop the last ref of the lchan which |
| 213 | * will result in a channel release on RSL and we will start |
| 214 | * the paging. This should work most of the time as the MS |
| 215 | * will listen to the paging requests before we timeout |
| 216 | */ |
| 217 | |
Holger Hans Peter Freyther | 5e58a0e | 2010-06-16 14:02:41 +0800 | [diff] [blame] | 218 | if (conn->subscr && !llist_empty(&conn->subscr->requests)) |
| 219 | subscr_send_paging_request(conn->subscr); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 220 | } |
| 221 | |