blob: 6be9625f43685e7440a89c1b364c8a875e88d1e8 [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 Freyther2f4dbeb2010-06-30 14:29:53 +080028
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010029#include <osmocom/core/talloc.h>
30#include <osmocom/core/protocol/gsm_12_21.h>
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080031
32#include <sys/socket.h>
33#include <sys/un.h>
34
35#include <errno.h>
36#include <unistd.h>
37
38#define RF_CMD_QUERY '?'
39#define RF_CMD_OFF '0'
40#define RF_CMD_ON '1'
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010041#define RF_CMD_D_OFF 'd'
42#define RF_CMD_ON_G 'g'
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080043
44static int lock_each_trx(struct gsm_network *net, int lock)
45{
46 struct gsm_bts *bts;
47
48 llist_for_each_entry(bts, &net->bts_list, list) {
49 struct gsm_bts_trx *trx;
50 llist_for_each_entry(trx, &bts->trx_list, list) {
51 gsm_trx_lock_rf(trx, lock);
52 }
53 }
54
55 return 0;
56}
57
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010058static void send_resp(struct osmo_bsc_rf_conn *conn, char send)
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080059{
60 struct msgb *msg;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080061
62 msg = msgb_alloc(10, "RF Query");
63 if (!msg) {
64 LOGP(DINP, LOGL_ERROR, "Failed to allocate response msg.\n");
65 return;
66 }
67
68 msg->l2h = msgb_put(msg, 1);
69 msg->l2h[0] = send;
70
71 if (write_queue_enqueue(&conn->queue, msg) != 0) {
72 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the answer.\n");
73 msgb_free(msg);
74 return;
75 }
76
77 return;
78}
79
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010080
81/*
82 * Send a
83 * 'g' when we are in grace mode
84 * '1' when one TRX is online,
85 * '0' otherwise
86 */
87static void handle_query(struct osmo_bsc_rf_conn *conn)
88{
89 struct gsm_bts *bts;
90 char send = RF_CMD_OFF;
91
92 if (conn->rf->policy == S_RF_GRACE)
93 return send_resp(conn, RF_CMD_ON_G);
94
95 llist_for_each_entry(bts, &conn->rf->gsm_network->bts_list, list) {
96 struct gsm_bts_trx *trx;
97 llist_for_each_entry(trx, &bts->trx_list, list) {
98 if (trx->nm_state.availability == NM_AVSTATE_OK &&
99 trx->nm_state.operational != NM_STATE_LOCKED) {
100 send = RF_CMD_ON;
101 break;
102 }
103 }
104 }
105
106 send_resp(conn, send);
107}
108
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100109static void rf_check_cb(void *_data)
110{
111 struct gsm_bts *bts;
112 struct osmo_bsc_rf *rf = _data;
113
114 llist_for_each_entry(bts, &rf->gsm_network->bts_list, list) {
115 struct gsm_bts_trx *trx;
116
117 /* don't bother to check a booting or missing BTS */
118 if (!bts->oml_link || !is_ipaccess_bts(bts))
119 continue;
120
121 llist_for_each_entry(trx, &bts->trx_list, list) {
122 if (trx->nm_state.availability != NM_AVSTATE_OK ||
123 trx->nm_state.operational != NM_OPSTATE_ENABLED ||
124 trx->nm_state.administrative != NM_STATE_UNLOCKED) {
125 LOGP(DNM, LOGL_ERROR, "RF activation failed. Starting again.\n");
126 ipaccess_drop_oml(bts);
127 break;
128 }
129 }
130 }
131}
132
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100133static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800134{
135 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100136 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800137
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100138 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800139 dispatch_signal(SS_RF, val, &sig);
140}
141
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100142static int switch_rf_off(struct osmo_bsc_rf *rf)
143{
144 lock_each_trx(rf->gsm_network, 1);
145 send_signal(rf, S_RF_OFF);
146
147 return 0;
148}
149
150static void grace_timeout(void *_data)
151{
152 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
153
154 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
155 switch_rf_off(rf);
156}
157
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100158static int enter_grace(struct osmo_bsc_rf *rf)
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100159{
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100160 rf->grace_timeout.cb = grace_timeout;
161 rf->grace_timeout.data = rf;
162 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
163 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
164 rf->gsm_network->msc_data->mid_call_timeout);
165
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100166 send_signal(rf, S_RF_GRACE);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100167 return 0;
168}
169
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100170static void rf_delay_cmd_cb(void *data)
171{
172 struct osmo_bsc_rf *rf = data;
173
174 switch (rf->last_request) {
175 case RF_CMD_D_OFF:
176 rf->last_state_command = "RF Direct Off";
177 bsc_del_timer(&rf->rf_check);
178 bsc_del_timer(&rf->grace_timeout);
179 switch_rf_off(rf);
180 break;
181 case RF_CMD_ON:
182 rf->last_state_command = "RF Direct On";
183 bsc_del_timer(&rf->grace_timeout);
184 lock_each_trx(rf->gsm_network, 0);
185 send_signal(rf, S_RF_ON);
186 bsc_schedule_timer(&rf->rf_check, 3, 0);
187 break;
188 case RF_CMD_OFF:
189 rf->last_state_command = "RF Scheduled Off";
190 bsc_del_timer(&rf->rf_check);
191 enter_grace(rf);
192 break;
193 }
194}
195
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800196static int rf_read_cmd(struct bsc_fd *fd)
197{
198 struct osmo_bsc_rf_conn *conn = fd->data;
199 char buf[1];
200 int rc;
201
202 rc = read(fd->fd, buf, sizeof(buf));
203 if (rc != sizeof(buf)) {
204 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
205 bsc_unregister_fd(fd);
206 close(fd->fd);
207 write_queue_clear(&conn->queue);
208 talloc_free(conn);
209 return -1;
210 }
211
212 switch (buf[0]) {
213 case RF_CMD_QUERY:
214 handle_query(conn);
215 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100216 case RF_CMD_D_OFF:
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800217 case RF_CMD_ON:
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100218 case RF_CMD_OFF:
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100219 conn->rf->last_request = buf[0];
220 if (!bsc_timer_pending(&conn->rf->delay_cmd))
221 bsc_schedule_timer(&conn->rf->delay_cmd, 1, 0);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800222 break;
223 default:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100224 conn->rf->last_state_command = "Unknown command";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800225 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
226 break;
227 }
228
229 return 0;
230}
231
232static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
233{
234 int rc;
235
236 rc = write(fd->fd, msg->data, msg->len);
237 if (rc != msg->len) {
238 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
239 return -1;
240 }
241
242 return 0;
243}
244
245static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
246{
247 struct osmo_bsc_rf_conn *conn;
248 struct osmo_bsc_rf *rf = bfd->data;
249 struct sockaddr_un addr;
250 socklen_t len = sizeof(addr);
251 int fd;
252
253 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
254 if (fd < 0) {
255 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
256 errno, strerror(errno));
257 return -1;
258 }
259
260 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
261 if (!conn) {
262 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
263 close(fd);
264 return -1;
265 }
266
267 write_queue_init(&conn->queue, 10);
268 conn->queue.bfd.data = conn;
269 conn->queue.bfd.fd = fd;
270 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
271 conn->queue.read_cb = rf_read_cmd;
272 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800273 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800274
275 if (bsc_register_fd(&conn->queue.bfd) != 0) {
276 close(fd);
277 talloc_free(conn);
278 return -1;
279 }
280
281 return 0;
282}
283
284struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
285{
286 unsigned int namelen;
287 struct sockaddr_un local;
288 struct bsc_fd *bfd;
289 struct osmo_bsc_rf *rf;
290 int rc;
291
292 rf = talloc_zero(NULL, struct osmo_bsc_rf);
293 if (!rf) {
294 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
295 return NULL;
296 }
297
298 bfd = &rf->listen;
299 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
300 if (bfd->fd < 0) {
301 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
302 errno, strerror(errno));
303 return NULL;
304 }
305
306 local.sun_family = AF_UNIX;
307 strncpy(local.sun_path, path, sizeof(local.sun_path));
308 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
309 unlink(local.sun_path);
310
311 /* we use the same magic that X11 uses in Xtranssock.c for
312 * calculating the proper length of the sockaddr */
313#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
314 local.sun_len = strlen(local.sun_path);
315#endif
316#if defined(BSD44SOCKETS) || defined(SUN_LEN)
317 namelen = SUN_LEN(&local);
318#else
319 namelen = strlen(local.sun_path) +
320 offsetof(struct sockaddr_un, sun_path);
321#endif
322
323 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
324 if (rc != 0) {
325 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
326 local.sun_path, errno, strerror(errno));
327 close(bfd->fd);
328 talloc_free(rf);
329 return NULL;
330 }
331
332 if (listen(bfd->fd, 0) != 0) {
333 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
334 close(bfd->fd);
335 talloc_free(rf);
336 return NULL;
337 }
338
339 bfd->when = BSC_FD_READ;
340 bfd->cb = rf_ctl_accept;
341 bfd->data = rf;
342
343 if (bsc_register_fd(bfd) != 0) {
344 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
345 close(bfd->fd);
346 talloc_free(rf);
347 return NULL;
348 }
349
350 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800351 rf->policy = S_RF_ON;
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100352 rf->last_state_command = "";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800353
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100354 /* check the rf state */
355 rf->rf_check.data = rf;
356 rf->rf_check.cb = rf_check_cb;
357
Holger Hans Peter Freyther11762452011-02-14 23:41:42 +0100358 /* delay cmd handling */
359 rf->delay_cmd.data = rf;
360 rf->delay_cmd.cb = rf_delay_cmd_cb;
361
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800362 return rf;
363}
364