blob: 436474c0a95fb070561755722a0ec26a798beab4 [file] [log] [blame]
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +08001/* RF Ctl handling socket */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080020 *
21 */
22
23#include <openbsc/osmo_bsc_rf.h>
24#include <openbsc/debug.h>
25#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +080026#include <openbsc/signal.h>
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +010027#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther4b417462011-04-07 23:10:10 +020028#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080029
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010031#include <osmocom/gsm/protocol/gsm_12_21.h>
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080032
33#include <sys/socket.h>
34#include <sys/un.h>
35
36#include <errno.h>
37#include <unistd.h>
38
39#define RF_CMD_QUERY '?'
40#define RF_CMD_OFF '0'
41#define RF_CMD_ON '1'
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010042#define RF_CMD_D_OFF 'd'
43#define RF_CMD_ON_G 'g'
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080044
45static int lock_each_trx(struct gsm_network *net, int lock)
46{
47 struct gsm_bts *bts;
48
49 llist_for_each_entry(bts, &net->bts_list, list) {
50 struct gsm_bts_trx *trx;
51 llist_for_each_entry(trx, &bts->trx_list, list) {
52 gsm_trx_lock_rf(trx, lock);
53 }
54 }
55
56 return 0;
57}
58
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010059static void send_resp(struct osmo_bsc_rf_conn *conn, char send)
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080060{
61 struct msgb *msg;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080062
63 msg = msgb_alloc(10, "RF Query");
64 if (!msg) {
65 LOGP(DINP, LOGL_ERROR, "Failed to allocate response msg.\n");
66 return;
67 }
68
69 msg->l2h = msgb_put(msg, 1);
70 msg->l2h[0] = send;
71
72 if (write_queue_enqueue(&conn->queue, msg) != 0) {
73 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the answer.\n");
74 msgb_free(msg);
75 return;
76 }
77
78 return;
79}
80
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010081
82/*
83 * Send a
84 * 'g' when we are in grace mode
85 * '1' when one TRX is online,
86 * '0' otherwise
87 */
88static void handle_query(struct osmo_bsc_rf_conn *conn)
89{
90 struct gsm_bts *bts;
91 char send = RF_CMD_OFF;
92
93 if (conn->rf->policy == S_RF_GRACE)
94 return send_resp(conn, RF_CMD_ON_G);
95
96 llist_for_each_entry(bts, &conn->rf->gsm_network->bts_list, list) {
97 struct gsm_bts_trx *trx;
98 llist_for_each_entry(trx, &bts->trx_list, list) {
99 if (trx->nm_state.availability == NM_AVSTATE_OK &&
100 trx->nm_state.operational != NM_STATE_LOCKED) {
101 send = RF_CMD_ON;
102 break;
103 }
104 }
105 }
106
107 send_resp(conn, send);
108}
109
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100110static void rf_check_cb(void *_data)
111{
112 struct gsm_bts *bts;
113 struct osmo_bsc_rf *rf = _data;
114
115 llist_for_each_entry(bts, &rf->gsm_network->bts_list, list) {
116 struct gsm_bts_trx *trx;
117
118 /* don't bother to check a booting or missing BTS */
119 if (!bts->oml_link || !is_ipaccess_bts(bts))
120 continue;
121
122 llist_for_each_entry(trx, &bts->trx_list, list) {
123 if (trx->nm_state.availability != NM_AVSTATE_OK ||
124 trx->nm_state.operational != NM_OPSTATE_ENABLED ||
125 trx->nm_state.administrative != NM_STATE_UNLOCKED) {
126 LOGP(DNM, LOGL_ERROR, "RF activation failed. Starting again.\n");
127 ipaccess_drop_oml(bts);
128 break;
129 }
130 }
131 }
132}
133
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100134static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800135{
136 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100137 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800138
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100139 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800140 dispatch_signal(SS_RF, val, &sig);
141}
142
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100143static int switch_rf_off(struct osmo_bsc_rf *rf)
144{
145 lock_each_trx(rf->gsm_network, 1);
146 send_signal(rf, S_RF_OFF);
147
148 return 0;
149}
150
151static void grace_timeout(void *_data)
152{
153 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
154
155 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
156 switch_rf_off(rf);
157}
158
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100159static int enter_grace(struct osmo_bsc_rf *rf)
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100160{
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100161 rf->grace_timeout.cb = grace_timeout;
162 rf->grace_timeout.data = rf;
163 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
164 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
165 rf->gsm_network->msc_data->mid_call_timeout);
166
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100167 send_signal(rf, S_RF_GRACE);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100168 return 0;
169}
170
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100171static void rf_delay_cmd_cb(void *data)
172{
173 struct osmo_bsc_rf *rf = data;
174
175 switch (rf->last_request) {
176 case RF_CMD_D_OFF:
177 rf->last_state_command = "RF Direct Off";
178 bsc_del_timer(&rf->rf_check);
179 bsc_del_timer(&rf->grace_timeout);
180 switch_rf_off(rf);
181 break;
182 case RF_CMD_ON:
183 rf->last_state_command = "RF Direct On";
184 bsc_del_timer(&rf->grace_timeout);
185 lock_each_trx(rf->gsm_network, 0);
186 send_signal(rf, S_RF_ON);
187 bsc_schedule_timer(&rf->rf_check, 3, 0);
188 break;
189 case RF_CMD_OFF:
190 rf->last_state_command = "RF Scheduled Off";
191 bsc_del_timer(&rf->rf_check);
192 enter_grace(rf);
193 break;
194 }
195}
196
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800197static int rf_read_cmd(struct bsc_fd *fd)
198{
199 struct osmo_bsc_rf_conn *conn = fd->data;
200 char buf[1];
201 int rc;
202
203 rc = read(fd->fd, buf, sizeof(buf));
204 if (rc != sizeof(buf)) {
205 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
206 bsc_unregister_fd(fd);
207 close(fd->fd);
208 write_queue_clear(&conn->queue);
209 talloc_free(conn);
210 return -1;
211 }
212
213 switch (buf[0]) {
214 case RF_CMD_QUERY:
215 handle_query(conn);
216 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100217 case RF_CMD_D_OFF:
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800218 case RF_CMD_ON:
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100219 case RF_CMD_OFF:
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100220 conn->rf->last_request = buf[0];
221 if (!bsc_timer_pending(&conn->rf->delay_cmd))
222 bsc_schedule_timer(&conn->rf->delay_cmd, 1, 0);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800223 break;
224 default:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100225 conn->rf->last_state_command = "Unknown command";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800226 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
227 break;
228 }
229
230 return 0;
231}
232
233static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
234{
235 int rc;
236
237 rc = write(fd->fd, msg->data, msg->len);
238 if (rc != msg->len) {
239 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
240 return -1;
241 }
242
243 return 0;
244}
245
246static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
247{
248 struct osmo_bsc_rf_conn *conn;
249 struct osmo_bsc_rf *rf = bfd->data;
250 struct sockaddr_un addr;
251 socklen_t len = sizeof(addr);
252 int fd;
253
254 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
255 if (fd < 0) {
256 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
257 errno, strerror(errno));
258 return -1;
259 }
260
261 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
262 if (!conn) {
263 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
264 close(fd);
265 return -1;
266 }
267
268 write_queue_init(&conn->queue, 10);
269 conn->queue.bfd.data = conn;
270 conn->queue.bfd.fd = fd;
271 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
272 conn->queue.read_cb = rf_read_cmd;
273 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800274 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800275
276 if (bsc_register_fd(&conn->queue.bfd) != 0) {
277 close(fd);
278 talloc_free(conn);
279 return -1;
280 }
281
282 return 0;
283}
284
285struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
286{
287 unsigned int namelen;
288 struct sockaddr_un local;
289 struct bsc_fd *bfd;
290 struct osmo_bsc_rf *rf;
291 int rc;
292
293 rf = talloc_zero(NULL, struct osmo_bsc_rf);
294 if (!rf) {
295 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
296 return NULL;
297 }
298
299 bfd = &rf->listen;
300 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
301 if (bfd->fd < 0) {
302 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
303 errno, strerror(errno));
304 return NULL;
305 }
306
307 local.sun_family = AF_UNIX;
308 strncpy(local.sun_path, path, sizeof(local.sun_path));
309 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
310 unlink(local.sun_path);
311
312 /* we use the same magic that X11 uses in Xtranssock.c for
313 * calculating the proper length of the sockaddr */
314#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
315 local.sun_len = strlen(local.sun_path);
316#endif
317#if defined(BSD44SOCKETS) || defined(SUN_LEN)
318 namelen = SUN_LEN(&local);
319#else
320 namelen = strlen(local.sun_path) +
321 offsetof(struct sockaddr_un, sun_path);
322#endif
323
324 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
325 if (rc != 0) {
326 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
327 local.sun_path, errno, strerror(errno));
328 close(bfd->fd);
329 talloc_free(rf);
330 return NULL;
331 }
332
333 if (listen(bfd->fd, 0) != 0) {
334 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
335 close(bfd->fd);
336 talloc_free(rf);
337 return NULL;
338 }
339
340 bfd->when = BSC_FD_READ;
341 bfd->cb = rf_ctl_accept;
342 bfd->data = rf;
343
344 if (bsc_register_fd(bfd) != 0) {
345 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
346 close(bfd->fd);
347 talloc_free(rf);
348 return NULL;
349 }
350
351 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800352 rf->policy = S_RF_ON;
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100353 rf->last_state_command = "";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800354
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100355 /* check the rf state */
356 rf->rf_check.data = rf;
357 rf->rf_check.cb = rf_check_cb;
358
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100359 /* delay cmd handling */
360 rf->delay_cmd.data = rf;
361 rf->delay_cmd.cb = rf_delay_cmd_cb;
362
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800363 return rf;
364}
365