blob: ad0a37e9c7f7c5f0b6183821df84ef936ed1743e [file] [log] [blame]
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010 by On-Waves
4 * 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>
25#include <openbsc/signal.h>
26
Holger Hans Peter Freyther3d119f12012-08-30 16:43:28 +020027int bsc_grace_allow_new_connection(struct gsm_network *network, struct gsm_bts *bts)
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080028{
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +020029 if (!network->bsc_data->rf_ctrl)
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080030 return 1;
Holger Hans Peter Freyther3d119f12012-08-30 16:43:28 +020031 if (bts && bts->excl_from_rf_lock)
32 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
36static int handle_sub(struct gsm_lchan *lchan, const char *text)
37{
38 struct gsm_subscriber_connection *conn;
39
40 /* only send it to TCH */
41 if (lchan->type != GSM_LCHAN_TCH_H && lchan->type != GSM_LCHAN_TCH_F)
42 return -1;
43
44 /* only send on the primary channel */
45 conn = lchan->conn;
46 if (!conn)
47 return -1;
48
49 if (conn->lchan != lchan)
50 return -1;
51
52 /* only when active */
53 if (lchan->state != LCHAN_S_ACTIVE)
54 return -1;
55
56 gsm0480_send_ussdNotify(conn, 0, text);
57 gsm0480_send_releaseComplete(conn);
58
59 return 0;
60}
61
62/*
63 * The place to handle the grace mode. Right now we will send
64 * USSD messages to the subscriber, in the future we might start
65 * a timer to have different modes for the grace period.
66 */
67static int handle_grace(struct gsm_network *network)
68{
69 int ts_nr, lchan_nr;
70 struct gsm_bts *bts;
71 struct gsm_bts_trx *trx;
72
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +020073 if (!network->bsc_data->mid_call_txt)
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080074 return 0;
75
76 llist_for_each_entry(bts, &network->bts_list, list) {
77 llist_for_each_entry(trx, &bts->trx_list, list) {
78 for (ts_nr = 0; ts_nr < TRX_NR_TS; ++ts_nr) {
79 struct gsm_bts_trx_ts *ts = &trx->ts[ts_nr];
80 for (lchan_nr = 0; lchan_nr < TS_MAX_LCHAN; ++lchan_nr) {
81 handle_sub(&ts->lchan[lchan_nr],
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +020082 network->bsc_data->mid_call_txt);
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +080083 }
84 }
85 }
86 }
87 return 0;
88}
89
90static int handle_rf_signal(unsigned int subsys, unsigned int signal,
91 void *handler_data, void *signal_data)
92{
93 struct rf_signal_data *sig;
94
95 if (subsys != SS_RF)
96 return -1;
97
98 sig = signal_data;
99
100 if (signal == S_RF_GRACE)
101 handle_grace(sig->net);
102
103 return 0;
104}
105
106static __attribute__((constructor)) void on_dso_load_grace(void)
107{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200108 osmo_signal_register_handler(SS_RF, handle_rf_signal, NULL);
Holger Hans Peter Freytherbd76fab2010-09-16 00:20:56 +0800109}