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> |
| 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 | |
| 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 | |
| 92 | talloc_free(request); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 97 | { |
| 98 | struct subscr_request *request; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 99 | int rc; |
| 100 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 101 | assert(!llist_empty(&subscr->requests)); |
| 102 | |
| 103 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 104 | 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 Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | struct gsm_subscriber *subscr_alloc(void) |
| 115 | { |
| 116 | struct gsm_subscriber *s; |
| 117 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 118 | s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 119 | if (!s) |
| 120 | return NULL; |
| 121 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 122 | llist_add_tail(&s->entry, &active_subscribers); |
| 123 | s->use_count = 1; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 124 | s->tmsi = GSM_RESERVED_TMSI; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 125 | |
| 126 | INIT_LLIST_HEAD(&s->requests); |
| 127 | |
| 128 | return s; |
| 129 | } |
| 130 | |
| 131 | static void subscr_free(struct gsm_subscriber *subscr) |
| 132 | { |
| 133 | llist_del(&subscr->entry); |
| 134 | talloc_free(subscr); |
| 135 | } |
| 136 | |
| 137 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 138 | { |
| 139 | subscr->use_count++; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 140 | DEBUGP(DREF, "subscr %s usage increases usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 141 | subscr->extension, subscr->use_count); |
| 142 | return subscr; |
| 143 | } |
| 144 | |
| 145 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 146 | { |
| 147 | subscr->use_count--; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 148 | DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 149 | subscr->extension, subscr->use_count); |
| 150 | if (subscr->use_count <= 0) |
| 151 | subscr_free(subscr); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | void subscr_get_channel(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 3567284 | 2009-08-20 13:33:51 +0200 | [diff] [blame] | 156 | int type, gsm_cbfn *cbfn, void *param) |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 157 | { |
| 158 | struct subscr_request *request; |
| 159 | |
| 160 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 161 | if (!request) { |
| 162 | if (cbfn) |
| 163 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 164 | NULL, NULL, param); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | memset(request, 0, sizeof(*request)); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 169 | request->subscr = subscr; |
| 170 | request->channel_type = type; |
| 171 | request->cbfn = cbfn; |
| 172 | request->param = param; |
| 173 | |
| 174 | /* |
| 175 | * FIXME: We might be able to assign more than one |
| 176 | * channel, e.g. voice and SMS submit at the same |
| 177 | * time. |
| 178 | */ |
| 179 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 180 | /* add to the list, send a request */ |
| 181 | llist_add_tail(&request->entry, &subscr->requests); |
| 182 | subscr_send_paging_request(subscr); |
| 183 | } else { |
| 184 | /* this will be picked up later, from subscr_put_channel */ |
| 185 | llist_add_tail(&request->entry, &subscr->requests); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 190 | { |
| 191 | /* |
| 192 | * FIXME: Continue with other requests now... by checking |
| 193 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 194 | * of the lchan after having asked the next requestee to handle |
| 195 | * the channel. |
| 196 | */ |
| 197 | /* |
| 198 | * FIXME: is the lchan is of a different type we could still |
| 199 | * issue an immediate assignment for another channel and then |
| 200 | * close this one. |
| 201 | */ |
| 202 | /* |
| 203 | * Currently we will drop the last ref of the lchan which |
| 204 | * will result in a channel release on RSL and we will start |
| 205 | * the paging. This should work most of the time as the MS |
| 206 | * will listen to the paging requests before we timeout |
| 207 | */ |
| 208 | |
| 209 | put_lchan(lchan); |
| 210 | |
| 211 | if (lchan->subscr && !llist_empty(&lchan->subscr->requests)) |
| 212 | subscr_send_paging_request(lchan->subscr); |
| 213 | } |
| 214 | |