blob: 6f21460c0d5a0c2aa52d358e3c3ab0052b3814a6 [file] [log] [blame]
Harald Welteb68899d2009-01-06 21:47:18 +00001/* 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 Welte2f1311b2009-01-06 23:50:39 +000038#include <stdio.h>
39#include <stdlib.h>
40
Harald Welteb68899d2009-01-06 21:47:18 +000041#include <openbsc/paging.h>
42#include <openbsc/debug.h>
Harald Welte2f1311b2009-01-06 23:50:39 +000043#include <openbsc/abis_rsl.h>
44#include <openbsc/gsm_04_08.h>
Harald Welteb68899d2009-01-06 21:47:18 +000045
Holger Freytherdf892da2009-02-03 21:05:49 +000046#define PAGING_TIMEOUT 0, 75000
47#define MAX_PAGING_REQUEST 750
Harald Welte38c2f132009-01-06 23:10:57 +000048
Harald Welteb68899d2009-01-06 21:47:18 +000049static LLIST_HEAD(managed_bts);
50
Harald Welte38c2f132009-01-06 23:10:57 +000051/*
52 * Kill one paging request update the internal list...
53 */
54static void page_remove_request(struct paging_bts *paging_bts) {
55 struct paging_request *to_be_deleted = paging_bts->last_request;
56 paging_bts->last_request =
57 (struct paging_request *)paging_bts->last_request->entry.next;
58 if (&to_be_deleted->entry == &paging_bts->pending_requests)
59 paging_bts->last_request = NULL;
60 llist_del(&to_be_deleted->entry);
61 free(to_be_deleted);
62}
63
64
65static void page_handle_pending_requests(void *data) {
Harald Welte2f1311b2009-01-06 23:50:39 +000066 u_int8_t mi[128];
67 unsigned long int tmsi;
68 unsigned int mi_len;
Harald Welte38c2f132009-01-06 23:10:57 +000069 struct paging_bts *paging_bts = (struct paging_bts *)data;
70
71 if (!paging_bts->last_request)
72 paging_bts->last_request =
73 (struct paging_request *)paging_bts->pending_requests.next;
74 if (&paging_bts->last_request->entry == &paging_bts->pending_requests) {
75 paging_bts->last_request = NULL;
76 return;
77 }
78
Harald Welte2f1311b2009-01-06 23:50:39 +000079 /* handle the paging request now */
Harald Welte38c2f132009-01-06 23:10:57 +000080 DEBUGP(DPAG, "Going to send paging commands: '%s'\n",
81 paging_bts->last_request->subscr->imsi);
Holger Freytherbf834812009-01-27 23:46:19 +000082 ++paging_bts->last_request->requests;
Harald Welte2f1311b2009-01-06 23:50:39 +000083 tmsi = strtoul(paging_bts->last_request->subscr->tmsi, NULL, 10);
84 mi_len = generate_mid_from_tmsi(mi, tmsi);
85 rsl_paging_cmd(paging_bts->bts, 1, mi_len, mi, RSL_CHANNEED_TCH_F);
86
Holger Freytherdf892da2009-02-03 21:05:49 +000087 if (paging_bts->last_request->requests > MAX_PAGING_REQUEST) {
Holger Freytherbf834812009-01-27 23:46:19 +000088 page_remove_request(paging_bts);
89 } else {
90 /* move to the next item */
91 paging_bts->last_request =
92 (struct paging_request *)paging_bts->last_request->entry.next;
93 if (&paging_bts->last_request->entry == &paging_bts->pending_requests)
94 paging_bts->last_request = NULL;
95 }
Harald Welte2f1311b2009-01-06 23:50:39 +000096
Harald Welte38c2f132009-01-06 23:10:57 +000097 schedule_timer(&paging_bts->page_timer, PAGING_TIMEOUT);
98}
99
Harald Welteb68899d2009-01-06 21:47:18 +0000100static int page_pending_request(struct paging_bts *bts,
101 struct gsm_subscriber *subscr) {
102 struct paging_request *req;
103
104 llist_for_each_entry(req, &bts->pending_requests, entry) {
105 if (subscr == req->subscr)
106 return 1;
107 }
108
109 return 0;
110}
111
112struct paging_bts* page_allocate(struct gsm_bts *bts) {
113 struct paging_bts *page;
114
115 page = (struct paging_bts *)malloc(sizeof(*page));
116 memset(page, 0, sizeof(*page));
Harald Welte38c2f132009-01-06 23:10:57 +0000117 page->bts = bts;
118 INIT_LLIST_HEAD(&page->pending_requests);
119 page->page_timer.cb = page_handle_pending_requests;
120 page->page_timer.data = page;
121
Harald Welteb68899d2009-01-06 21:47:18 +0000122 llist_add_tail(&page->bts_list, &managed_bts);
123
124 return page;
125}
126
Harald Welte38c2f132009-01-06 23:10:57 +0000127void page_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, int type) {
Harald Welteb68899d2009-01-06 21:47:18 +0000128 struct paging_bts *bts_entry;
129 struct paging_request *req;
130
131 req = (struct paging_request *)malloc(sizeof(*req));
Holger Freyther1525de02009-01-27 23:38:27 +0000132 memset(req, 0, sizeof(*req));
Harald Welteb68899d2009-01-06 21:47:18 +0000133 req->subscr = subscr_get(subscr);
134 req->bts = bts;
Harald Welte38c2f132009-01-06 23:10:57 +0000135 req->chan_type = type;
Harald Welteb68899d2009-01-06 21:47:18 +0000136
137 llist_for_each_entry(bts_entry, &managed_bts, bts_list) {
Holger Freyther1525de02009-01-27 23:38:27 +0000138 if (bts == bts_entry->bts) {
139 if (!page_pending_request(bts_entry, subscr)) {
140 llist_add_tail(&req->entry, &bts_entry->pending_requests);
141 if (!timer_pending(&bts_entry->page_timer))
142 schedule_timer(&bts_entry->page_timer, PAGING_TIMEOUT);
143 } else {
144 DEBUGP(DPAG, "Paging request already pending\n");
145 }
146
Harald Welteb68899d2009-01-06 21:47:18 +0000147 return;
148 }
149 }
150
Harald Welted08de2f2009-01-11 04:38:55 +0000151 DEBUGP(DPAG, "Paging request for not managed BTS\n");
Harald Welteb68899d2009-01-06 21:47:18 +0000152 free(req);
153 return;
154}