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 | |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 31 | #include <openbsc/gsm_subscriber.h> |
| 32 | #include <openbsc/paging.h> |
| 33 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 34 | |
| 35 | LLIST_HEAD(active_subscribers); |
| 36 | void *tall_subscr_ctx; |
| 37 | void *tall_sub_req_ctx; |
| 38 | |
| 39 | /* for the gsm_subscriber.c */ |
| 40 | struct llist_head *subscr_bsc_active_subscriber(void) |
| 41 | { |
| 42 | return &active_subscribers; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Struct for pending channel requests. This is managed in the |
| 47 | * llist_head requests of each subscriber. The reference counting |
| 48 | * should work in such a way that a subscriber with a pending request |
| 49 | * remains in memory. |
| 50 | */ |
| 51 | struct subscr_request { |
| 52 | struct llist_head entry; |
| 53 | |
| 54 | /* back reference */ |
| 55 | struct gsm_subscriber *subscr; |
| 56 | |
| 57 | /* the requested channel type */ |
| 58 | int channel_type; |
| 59 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 60 | /* the callback data */ |
| 61 | gsm_cbfn *cbfn; |
| 62 | void *param; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * We got the channel assigned and can now hand this channel |
| 67 | * over to one of our callbacks. |
| 68 | */ |
| 69 | static int subscr_paging_cb(unsigned int hooknum, unsigned int event, |
| 70 | struct msgb *msg, void *data, void *param) |
| 71 | { |
| 72 | struct subscr_request *request; |
| 73 | struct gsm_subscriber *subscr = (struct gsm_subscriber *)param; |
| 74 | |
Holger Hans Peter Freyther | aa62bb9 | 2009-08-22 11:18:13 +0200 | [diff] [blame] | 75 | /* There is no request anymore... */ |
| 76 | if (llist_empty(&subscr->requests)) |
| 77 | return -1; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * FIXME: What to do with paging requests coming during |
| 81 | * this callback? We must be sure to not start paging when |
| 82 | * we have an active connection to a subscriber and to make |
| 83 | * the subscr_put_channel work as required... |
| 84 | */ |
| 85 | request = (struct subscr_request *)subscr->requests.next; |
| 86 | llist_del(&request->entry); |
| 87 | subscr->in_callback = 1; |
| 88 | request->cbfn(hooknum, event, msg, data, request->param); |
| 89 | subscr->in_callback = 0; |
| 90 | |
| 91 | talloc_free(request); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 96 | { |
| 97 | struct subscr_request *request; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 98 | int rc; |
| 99 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 100 | assert(!llist_empty(&subscr->requests)); |
| 101 | |
| 102 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | aca08d9 | 2009-08-21 02:35:28 +0200 | [diff] [blame] | 103 | rc = paging_request(subscr->net, subscr, request->channel_type, |
| 104 | subscr_paging_cb, subscr); |
| 105 | |
| 106 | /* paging failed, quit now */ |
| 107 | if (rc <= 0) { |
| 108 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, |
| 109 | NULL, NULL, request->param); |
| 110 | } |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | struct gsm_subscriber *subscr_alloc(void) |
| 114 | { |
| 115 | struct gsm_subscriber *s; |
| 116 | |
Holger Hans Peter Freyther | e453348 | 2009-10-29 02:29:45 +0100 | [diff] [blame] | 117 | s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 118 | if (!s) |
| 119 | return NULL; |
| 120 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 121 | llist_add_tail(&s->entry, &active_subscribers); |
| 122 | s->use_count = 1; |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 123 | s->tmsi = GSM_RESERVED_TMSI; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 124 | |
| 125 | INIT_LLIST_HEAD(&s->requests); |
| 126 | |
| 127 | return s; |
| 128 | } |
| 129 | |
| 130 | static void subscr_free(struct gsm_subscriber *subscr) |
| 131 | { |
| 132 | llist_del(&subscr->entry); |
| 133 | talloc_free(subscr); |
| 134 | } |
| 135 | |
| 136 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 137 | { |
| 138 | subscr->use_count++; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 139 | DEBUGP(DREF, "subscr %s usage increases usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 140 | subscr->extension, subscr->use_count); |
| 141 | return subscr; |
| 142 | } |
| 143 | |
| 144 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 145 | { |
| 146 | subscr->use_count--; |
Harald Welte | 57fde55 | 2009-12-24 11:46:44 +0100 | [diff] [blame] | 147 | DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n", |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 148 | subscr->extension, subscr->use_count); |
| 149 | if (subscr->use_count <= 0) |
| 150 | subscr_free(subscr); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | void subscr_get_channel(struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 3567284 | 2009-08-20 13:33:51 +0200 | [diff] [blame] | 155 | int type, gsm_cbfn *cbfn, void *param) |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 156 | { |
| 157 | struct subscr_request *request; |
| 158 | |
| 159 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 160 | if (!request) { |
| 161 | if (cbfn) |
| 162 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 163 | NULL, NULL, param); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | memset(request, 0, sizeof(*request)); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 168 | request->subscr = subscr; |
| 169 | request->channel_type = type; |
| 170 | request->cbfn = cbfn; |
| 171 | request->param = param; |
| 172 | |
| 173 | /* |
| 174 | * FIXME: We might be able to assign more than one |
| 175 | * channel, e.g. voice and SMS submit at the same |
| 176 | * time. |
| 177 | */ |
| 178 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 179 | /* add to the list, send a request */ |
| 180 | llist_add_tail(&request->entry, &subscr->requests); |
| 181 | subscr_send_paging_request(subscr); |
| 182 | } else { |
| 183 | /* this will be picked up later, from subscr_put_channel */ |
| 184 | llist_add_tail(&request->entry, &subscr->requests); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 189 | { |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 190 | struct gsm_subscriber_connection *conn = &lchan->conn; |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 209 | put_subscr_con(conn); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 210 | |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 211 | if (lchan->conn.subscr && !llist_empty(&lchan->conn.subscr->requests)) |
| 212 | subscr_send_paging_request(lchan->conn.subscr); |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 213 | } |
| 214 | |