Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [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 | |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 41 | #include <openbsc/paging.h> |
| 42 | #include <openbsc/debug.h> |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 43 | #include <openbsc/abis_rsl.h> |
| 44 | #include <openbsc/gsm_04_08.h> |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 45 | |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 46 | #define PAGING_TIMEOUT 1, 75000 |
Holger Freyther | df892da | 2009-02-03 21:05:49 +0000 | [diff] [blame] | 47 | #define MAX_PAGING_REQUEST 750 |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 48 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 49 | static LLIST_HEAD(managed_bts); |
| 50 | |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 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 */ |
Holger Freyther | 152a147 | 2009-02-04 13:38:26 +0000 | [diff] [blame^] | 61 | blocks = rsl_number_of_paging_subchannels(bts); |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 62 | group = get_paging_group(str_to_imsi(subscr->imsi), |
| 63 | bs_cc_chans, blocks); |
| 64 | return group; |
| 65 | } |
| 66 | |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Kill one paging request update the internal list... |
| 69 | */ |
| 70 | static void page_remove_request(struct paging_bts *paging_bts) { |
| 71 | struct paging_request *to_be_deleted = paging_bts->last_request; |
| 72 | paging_bts->last_request = |
| 73 | (struct paging_request *)paging_bts->last_request->entry.next; |
| 74 | if (&to_be_deleted->entry == &paging_bts->pending_requests) |
| 75 | paging_bts->last_request = NULL; |
| 76 | llist_del(&to_be_deleted->entry); |
| 77 | free(to_be_deleted); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void page_handle_pending_requests(void *data) { |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 82 | u_int8_t mi[128]; |
| 83 | unsigned long int tmsi; |
| 84 | unsigned int mi_len; |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 85 | unsigned int pag_group; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 86 | struct paging_bts *paging_bts = (struct paging_bts *)data; |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 87 | struct paging_request *request = NULL; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 88 | |
| 89 | if (!paging_bts->last_request) |
| 90 | paging_bts->last_request = |
| 91 | (struct paging_request *)paging_bts->pending_requests.next; |
| 92 | if (&paging_bts->last_request->entry == &paging_bts->pending_requests) { |
| 93 | paging_bts->last_request = NULL; |
| 94 | return; |
| 95 | } |
| 96 | |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 97 | /* handle the paging request now */ |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 98 | request = paging_bts->last_request; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 99 | DEBUGP(DPAG, "Going to send paging commands: '%s'\n", |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 100 | request->subscr->imsi); |
| 101 | ++request->requests; |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 102 | |
| 103 | pag_group = calculate_group(paging_bts->bts, request->subscr); |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 104 | tmsi = strtoul(request->subscr->tmsi, NULL, 10); |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 105 | mi_len = generate_mid_from_tmsi(mi, tmsi); |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 106 | rsl_paging_cmd(paging_bts->bts, pag_group, mi_len, mi, |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 107 | request->chan_type); |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 108 | |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 109 | if (request->requests > MAX_PAGING_REQUEST) { |
Holger Freyther | bf83481 | 2009-01-27 23:46:19 +0000 | [diff] [blame] | 110 | page_remove_request(paging_bts); |
| 111 | } else { |
| 112 | /* move to the next item */ |
| 113 | paging_bts->last_request = |
| 114 | (struct paging_request *)paging_bts->last_request->entry.next; |
| 115 | if (&paging_bts->last_request->entry == &paging_bts->pending_requests) |
| 116 | paging_bts->last_request = NULL; |
| 117 | } |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 118 | |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 119 | schedule_timer(&paging_bts->page_timer, PAGING_TIMEOUT); |
| 120 | } |
| 121 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 122 | static int page_pending_request(struct paging_bts *bts, |
| 123 | struct gsm_subscriber *subscr) { |
| 124 | struct paging_request *req; |
| 125 | |
| 126 | llist_for_each_entry(req, &bts->pending_requests, entry) { |
| 127 | if (subscr == req->subscr) |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | struct paging_bts* page_allocate(struct gsm_bts *bts) { |
| 135 | struct paging_bts *page; |
| 136 | |
| 137 | page = (struct paging_bts *)malloc(sizeof(*page)); |
| 138 | memset(page, 0, sizeof(*page)); |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 139 | page->bts = bts; |
| 140 | INIT_LLIST_HEAD(&page->pending_requests); |
| 141 | page->page_timer.cb = page_handle_pending_requests; |
| 142 | page->page_timer.data = page; |
| 143 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 144 | llist_add_tail(&page->bts_list, &managed_bts); |
| 145 | |
| 146 | return page; |
| 147 | } |
| 148 | |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 149 | void page_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, int type) { |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 150 | struct paging_bts *bts_entry; |
| 151 | struct paging_request *req; |
| 152 | |
| 153 | req = (struct paging_request *)malloc(sizeof(*req)); |
Holger Freyther | 1525de0 | 2009-01-27 23:38:27 +0000 | [diff] [blame] | 154 | memset(req, 0, sizeof(*req)); |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 155 | req->subscr = subscr_get(subscr); |
| 156 | req->bts = bts; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 157 | req->chan_type = type; |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 158 | |
| 159 | llist_for_each_entry(bts_entry, &managed_bts, bts_list) { |
Holger Freyther | 1525de0 | 2009-01-27 23:38:27 +0000 | [diff] [blame] | 160 | if (bts == bts_entry->bts) { |
| 161 | if (!page_pending_request(bts_entry, subscr)) { |
| 162 | llist_add_tail(&req->entry, &bts_entry->pending_requests); |
| 163 | if (!timer_pending(&bts_entry->page_timer)) |
| 164 | schedule_timer(&bts_entry->page_timer, PAGING_TIMEOUT); |
| 165 | } else { |
| 166 | DEBUGP(DPAG, "Paging request already pending\n"); |
| 167 | } |
| 168 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | } |
| 172 | |
Harald Welte | d08de2f | 2009-01-11 04:38:55 +0000 | [diff] [blame] | 173 | DEBUGP(DPAG, "Paging request for not managed BTS\n"); |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 174 | free(req); |
| 175 | return; |
| 176 | } |