blob: 4fd0ebb2e35d831351346068a77ebd008cc49dd6 [file] [log] [blame]
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +08001/*
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +01002 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010-2013 by On-Waves
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +08004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +08009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080018 *
19 */
20
21#include <openbsc/osmo_bsc_grace.h>
22#include <openbsc/osmo_bsc_rf.h>
23#include <openbsc/osmo_msc_data.h>
24#include <openbsc/gsm_04_80.h>
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010025#include <openbsc/gsm_subscriber.h>
26#include <openbsc/paging.h>
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080027#include <openbsc/signal.h>
28
Holger Hans Peter Freyther3d119f12012-08-30 16:43:28 +020029int bsc_grace_allow_new_connection(struct gsm_network *network, struct gsm_bts *bts)
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080030{
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010031 if (bts->excl_from_rf_lock)
Holger Hans Peter Freyther3d119f12012-08-30 16:43:28 +020032 return 1;
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +020033 return network->bsc_data->rf_ctrl->policy == S_RF_ON;
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080034}
35
Holger Hans Peter Freyther067ce6d2015-04-01 18:19:45 +020036
37static int normal_paging(struct gsm_subscriber *subscr, int chan_needed,
38 struct osmo_msc_data *msc)
39{
40 return paging_request(subscr->group->net, subscr, chan_needed, NULL,
41 msc);
42}
43
44static int locked_paging(struct gsm_subscriber *subscr, int chan_needed,
45 struct osmo_msc_data *msc)
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010046{
47 struct gsm_bts *bts = NULL;
48
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010049 /*
50 * Check if there is any BTS that is on for the given lac. Start
51 * with NULL and iterate through all bts.
52 */
53 do {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010054 bts = gsm_bts_by_lac(subscr->group->net, subscr->lac, bts);
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010055 if (!bts)
56 break;
57
58 /*
59 * continue if the BTS is not excluded from the lock
60 */
61 if (!bts->excl_from_rf_lock)
62 continue;
63
64 /*
65 * now page on this bts
66 */
67 paging_request_bts(bts, subscr, chan_needed, NULL, msc);
68 } while (1);
69
70 /* All bts are either off or in the grace period */
71 return 0;
Holger Hans Peter Freyther067ce6d2015-04-01 18:19:45 +020072}
73
74/**
75 * Try to not page if everything the cell is not on.
76 */
77int bsc_grace_paging_request(struct gsm_subscriber *subscr, int chan_needed,
78 struct osmo_msc_data *msc)
79{
80 if (subscr->group->net->bsc_data->rf_ctrl->policy == S_RF_ON)
81 return normal_paging(subscr, chan_needed, msc);
82 return locked_paging(subscr, chan_needed, msc);
Holger Hans Peter Freytherdc030962013-01-07 17:30:13 +010083}
84
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080085static int handle_sub(struct gsm_lchan *lchan, const char *text)
86{
87 struct gsm_subscriber_connection *conn;
88
89 /* only send it to TCH */
90 if (lchan->type != GSM_LCHAN_TCH_H && lchan->type != GSM_LCHAN_TCH_F)
91 return -1;
92
93 /* only send on the primary channel */
94 conn = lchan->conn;
95 if (!conn)
96 return -1;
97
98 if (conn->lchan != lchan)
99 return -1;
100
101 /* only when active */
102 if (lchan->state != LCHAN_S_ACTIVE)
103 return -1;
104
105 gsm0480_send_ussdNotify(conn, 0, text);
106 gsm0480_send_releaseComplete(conn);
107
108 return 0;
109}
110
111/*
112 * The place to handle the grace mode. Right now we will send
113 * USSD messages to the subscriber, in the future we might start
114 * a timer to have different modes for the grace period.
115 */
116static int handle_grace(struct gsm_network *network)
117{
118 int ts_nr, lchan_nr;
119 struct gsm_bts *bts;
120 struct gsm_bts_trx *trx;
121
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200122 if (!network->bsc_data->mid_call_txt)
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +0800123 return 0;
124
125 llist_for_each_entry(bts, &network->bts_list, list) {
126 llist_for_each_entry(trx, &bts->trx_list, list) {
127 for (ts_nr = 0; ts_nr < TRX_NR_TS; ++ts_nr) {
128 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
129 for (lchan_nr = 0; lchan_nr < TS_MAX_LCHAN; ++lchan_nr) {
130 handle_sub(&ts->lchan[lchan_nr],
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200131 network->bsc_data->mid_call_txt);
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +0800132 }
133 }
134 }
135 }
136 return 0;
137}
138
139static int handle_rf_signal(unsigned int subsys, unsigned int signal,
140 void *handler_data, void *signal_data)
141{
142 struct rf_signal_data *sig;
143
144 if (subsys != SS_RF)
145 return -1;
146
147 sig = signal_data;
148
149 if (signal == S_RF_GRACE)
150 handle_grace(sig->net);
151
152 return 0;
153}
154
155static __attribute__((constructor)) void on_dso_load_grace(void)
156{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200157 osmo_signal_register_handler(SS_RF, handle_rf_signal, NULL);
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +0800158}