Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Paging helper and manager.... */ |
| 2 | /* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Relevant specs: |
| 23 | * 12.21: |
| 24 | * - 9.4.12 for CCCH Local Threshold |
| 25 | * |
| 26 | * 05.58: |
| 27 | * - 8.5.2 CCCH Load indication |
| 28 | * - 9.3.15 Paging Load |
| 29 | * |
| 30 | * Approach: |
| 31 | * - Send paging command to subscriber |
| 32 | * - On Channel Request we will remember the reason |
| 33 | * - After the ACK we will request the identity |
| 34 | * - Then we will send assign the gsm_subscriber and |
| 35 | * - and call a callback |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <assert.h> |
| 41 | |
| 42 | #include <openbsc/paging.h> |
| 43 | #include <openbsc/debug.h> |
| 44 | #include <openbsc/signal.h> |
| 45 | #include <openbsc/abis_rsl.h> |
| 46 | #include <openbsc/gsm_data.h> |
| 47 | |
| 48 | #define PAGING_TIMEOUT 1, 75000 |
| 49 | #define MAX_PAGING_REQUEST 750 |
| 50 | |
| 51 | static unsigned int calculate_group(struct gsm_bts *bts, struct gsm_subscriber *subscr) |
| 52 | { |
| 53 | int ccch_conf; |
| 54 | int bs_cc_chans; |
| 55 | int blocks; |
| 56 | unsigned int group; |
| 57 | |
| 58 | ccch_conf = bts->chan_desc.ccch_conf; |
| 59 | bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf); |
| 60 | /* code word + 2, as 2 channels equals 0x0 */ |
| 61 | blocks = rsl_number_of_paging_subchannels(bts); |
| 62 | group = get_paging_group(str_to_imsi(subscr->imsi), |
| 63 | bs_cc_chans, blocks); |
| 64 | return group; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Kill one paging request update the internal list... |
| 69 | */ |
| 70 | static void paging_remove_request(struct gsm_bts_paging_state *paging_bts, |
| 71 | struct gsm_paging_request *to_be_deleted) |
| 72 | { |
| 73 | /* Update the last_request if that is necessary */ |
| 74 | if (to_be_deleted == paging_bts->last_request) { |
| 75 | paging_bts->last_request = |
| 76 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
| 77 | if (&to_be_deleted->entry == &paging_bts->pending_requests) |
| 78 | paging_bts->last_request = NULL; |
| 79 | } |
| 80 | |
| 81 | bsc_del_timer(&to_be_deleted->T3113); |
| 82 | llist_del(&to_be_deleted->entry); |
| 83 | subscr_put(to_be_deleted->subscr); |
| 84 | free(to_be_deleted); |
| 85 | } |
| 86 | |
| 87 | static void page_ms(struct gsm_paging_request *request) |
| 88 | { |
| 89 | u_int8_t mi[128]; |
| 90 | unsigned long int tmsi; |
| 91 | unsigned int mi_len; |
| 92 | unsigned int page_group; |
| 93 | |
| 94 | DEBUGP(DPAG, "Going to send paging commands: '%s'\n", |
| 95 | request->subscr->imsi); |
| 96 | |
| 97 | page_group = calculate_group(request->bts, request->subscr); |
| 98 | tmsi = strtoul(request->subscr->tmsi, NULL, 10); |
| 99 | mi_len = generate_mid_from_tmsi(mi, tmsi); |
| 100 | rsl_paging_cmd(request->bts, page_group, mi_len, mi, |
| 101 | request->chan_type); |
| 102 | } |
| 103 | |
| 104 | static void paging_move_to_next(struct gsm_bts_paging_state *paging_bts) |
| 105 | { |
| 106 | paging_bts->last_request = |
| 107 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
| 108 | if (&paging_bts->last_request->entry == &paging_bts->pending_requests) |
| 109 | paging_bts->last_request = NULL; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * This is kicked by the periodic PAGING LOAD Indicator |
| 114 | * coming from abis_rsl.c |
| 115 | * |
| 116 | * We attempt to iterate once over the list of items but |
| 117 | * only upto available_slots. |
| 118 | */ |
| 119 | static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts) |
| 120 | { |
| 121 | struct gsm_paging_request *initial_request = NULL; |
| 122 | struct gsm_paging_request *current_request = NULL; |
| 123 | |
| 124 | /* |
| 125 | * Determine if the pending_requests list is empty and |
| 126 | * return then. |
| 127 | */ |
| 128 | if (llist_empty(&paging_bts->pending_requests)) { |
| 129 | paging_bts->last_request = NULL; |
| 130 | /* since the list is empty, no need to reschedule the timer */ |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (!paging_bts->last_request) |
| 135 | paging_bts->last_request = |
| 136 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
| 137 | |
| 138 | assert(paging_bts->last_request); |
| 139 | initial_request = paging_bts->last_request; |
| 140 | current_request = initial_request; |
| 141 | |
| 142 | do { |
| 143 | /* handle the paging request now */ |
| 144 | page_ms(current_request); |
| 145 | paging_bts->available_slots--; |
| 146 | |
| 147 | /* |
| 148 | * move to the next item. We might wrap around |
| 149 | * this means last_request will be NULL and we just |
| 150 | * call paging_page_to_next again. It it guranteed |
| 151 | * that the list is not empty. |
| 152 | */ |
| 153 | paging_move_to_next(paging_bts); |
| 154 | if (!paging_bts->last_request) |
| 155 | paging_bts->last_request = |
| 156 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
| 157 | current_request = paging_bts->last_request; |
| 158 | } while (paging_bts->available_slots > 0 |
| 159 | && initial_request != current_request); |
| 160 | |
| 161 | bsc_schedule_timer(&paging_bts->work_timer, 1, 0); |
| 162 | } |
| 163 | |
| 164 | static void paging_worker(void *data) |
| 165 | { |
| 166 | struct gsm_bts_paging_state *paging_bts = data; |
| 167 | |
| 168 | paging_handle_pending_requests(paging_bts); |
| 169 | } |
| 170 | |
| 171 | void paging_init(struct gsm_bts *bts) |
| 172 | { |
| 173 | bts->paging.bts = bts; |
| 174 | INIT_LLIST_HEAD(&bts->paging.pending_requests); |
| 175 | bts->paging.work_timer.cb = paging_worker; |
| 176 | bts->paging.work_timer.data = &bts->paging; |
| 177 | |
| 178 | /* Large number, until we get a proper message */ |
| 179 | bts->paging.available_slots = 100; |
| 180 | } |
| 181 | |
| 182 | static int paging_pending_request(struct gsm_bts_paging_state *bts, |
| 183 | struct gsm_subscriber *subscr) { |
| 184 | struct gsm_paging_request *req; |
| 185 | |
| 186 | llist_for_each_entry(req, &bts->pending_requests, entry) { |
| 187 | if (subscr == req->subscr) |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void paging_T3113_expired(void *data) |
| 195 | { |
| 196 | struct gsm_paging_request *req = (struct gsm_paging_request *)data; |
| 197 | struct paging_signal_data sig_data; |
| 198 | |
| 199 | DEBUGP(DPAG, "T3113 expired for request %p (%s)\n", |
| 200 | req, req->subscr->imsi); |
| 201 | |
| 202 | sig_data.subscr = req->subscr, |
| 203 | sig_data.bts = req->bts, |
| 204 | sig_data.lchan = NULL, |
| 205 | |
| 206 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
| 207 | if (req->cbfn) |
| 208 | req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, NULL, NULL, |
| 209 | req->cbfn_param); |
| 210 | paging_remove_request(&req->bts->paging, req); |
| 211 | } |
| 212 | |
| 213 | void paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
| 214 | int type, gsm_cbfn *cbfn, void *data) |
| 215 | { |
| 216 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 217 | struct gsm_paging_request *req; |
| 218 | |
| 219 | if (paging_pending_request(bts_entry, subscr)) { |
| 220 | DEBUGP(DPAG, "Paging request already pending\n"); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | req = (struct gsm_paging_request *)malloc(sizeof(*req)); |
| 225 | memset(req, 0, sizeof(*req)); |
| 226 | req->subscr = subscr_get(subscr); |
| 227 | req->bts = bts; |
| 228 | req->chan_type = type; |
| 229 | req->cbfn = cbfn; |
| 230 | req->cbfn_param = data; |
| 231 | req->T3113.cb = paging_T3113_expired; |
| 232 | req->T3113.data = req; |
| 233 | bsc_schedule_timer(&req->T3113, T3113_VALUE); |
| 234 | llist_add_tail(&req->entry, &bts_entry->pending_requests); |
| 235 | |
| 236 | if (!bsc_timer_pending(&bts_entry->work_timer)) |
| 237 | bsc_schedule_timer(&bts_entry->work_timer, 1, 0); |
| 238 | } |
| 239 | |
| 240 | /* we consciously ignore the type of the request here */ |
| 241 | void paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
| 242 | struct gsm_lchan *lchan) |
| 243 | { |
| 244 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 245 | struct gsm_paging_request *req, *req2; |
| 246 | |
| 247 | llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests, |
| 248 | entry) { |
| 249 | if (req->subscr == subscr) { |
Harald Welte | 876312f | 2009-06-10 11:21:55 +0800 | [diff] [blame] | 250 | if (lchan && req->cbfn) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 251 | req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED, |
| 252 | NULL, lchan, req->cbfn_param); |
| 253 | paging_remove_request(&bts->paging, req); |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots) |
| 260 | { |
| 261 | bts->paging.available_slots = free_slots; |
| 262 | } |