blob: 5cc985b7cb046efe09a9484a47530a5039d23bcb [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
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <openbsc/osmo_bsc_rf.h>
25#include <openbsc/debug.h>
26#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +080027#include <openbsc/signal.h>
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +010028#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080029
30#include <osmocore/talloc.h>
31#include <osmocore/protocol/gsm_12_21.h>
32
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 Freyther70c232f2010-11-22 19:09:38 +0100110static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800111{
112 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100113 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800114
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100115 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800116 dispatch_signal(SS_RF, val, &sig);
117}
118
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100119static int switch_rf_off(struct osmo_bsc_rf *rf)
120{
121 lock_each_trx(rf->gsm_network, 1);
122 send_signal(rf, S_RF_OFF);
123
124 return 0;
125}
126
127static void grace_timeout(void *_data)
128{
129 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
130
131 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
132 switch_rf_off(rf);
133}
134
135static int enter_grace(struct osmo_bsc_rf_conn *conn)
136{
137 struct osmo_bsc_rf *rf = conn->rf;
138
139 rf->grace_timeout.cb = grace_timeout;
140 rf->grace_timeout.data = rf;
141 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
142 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
143 rf->gsm_network->msc_data->mid_call_timeout);
144
145 send_signal(conn->rf, S_RF_GRACE);
146 return 0;
147}
148
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800149static int rf_read_cmd(struct bsc_fd *fd)
150{
151 struct osmo_bsc_rf_conn *conn = fd->data;
152 char buf[1];
153 int rc;
154
155 rc = read(fd->fd, buf, sizeof(buf));
156 if (rc != sizeof(buf)) {
157 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
158 bsc_unregister_fd(fd);
159 close(fd->fd);
160 write_queue_clear(&conn->queue);
161 talloc_free(conn);
162 return -1;
163 }
164
165 switch (buf[0]) {
166 case RF_CMD_QUERY:
167 handle_query(conn);
168 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100169 case RF_CMD_D_OFF:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100170 bsc_del_timer(&conn->rf->grace_timeout);
171 switch_rf_off(conn->rf);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800172 break;
173 case RF_CMD_ON:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100174 bsc_del_timer(&conn->rf->grace_timeout);
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800175 lock_each_trx(conn->rf->gsm_network, 0);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100176 send_signal(conn->rf, S_RF_ON);
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800177 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100178 case RF_CMD_OFF:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100179 enter_grace(conn);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800180 break;
181 default:
182 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
183 break;
184 }
185
186 return 0;
187}
188
189static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
190{
191 int rc;
192
193 rc = write(fd->fd, msg->data, msg->len);
194 if (rc != msg->len) {
195 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
196 return -1;
197 }
198
199 return 0;
200}
201
202static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
203{
204 struct osmo_bsc_rf_conn *conn;
205 struct osmo_bsc_rf *rf = bfd->data;
206 struct sockaddr_un addr;
207 socklen_t len = sizeof(addr);
208 int fd;
209
210 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
211 if (fd < 0) {
212 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
213 errno, strerror(errno));
214 return -1;
215 }
216
217 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
218 if (!conn) {
219 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
220 close(fd);
221 return -1;
222 }
223
224 write_queue_init(&conn->queue, 10);
225 conn->queue.bfd.data = conn;
226 conn->queue.bfd.fd = fd;
227 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
228 conn->queue.read_cb = rf_read_cmd;
229 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800230 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800231
232 if (bsc_register_fd(&conn->queue.bfd) != 0) {
233 close(fd);
234 talloc_free(conn);
235 return -1;
236 }
237
238 return 0;
239}
240
241struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
242{
243 unsigned int namelen;
244 struct sockaddr_un local;
245 struct bsc_fd *bfd;
246 struct osmo_bsc_rf *rf;
247 int rc;
248
249 rf = talloc_zero(NULL, struct osmo_bsc_rf);
250 if (!rf) {
251 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
252 return NULL;
253 }
254
255 bfd = &rf->listen;
256 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
257 if (bfd->fd < 0) {
258 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
259 errno, strerror(errno));
260 return NULL;
261 }
262
263 local.sun_family = AF_UNIX;
264 strncpy(local.sun_path, path, sizeof(local.sun_path));
265 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
266 unlink(local.sun_path);
267
268 /* we use the same magic that X11 uses in Xtranssock.c for
269 * calculating the proper length of the sockaddr */
270#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
271 local.sun_len = strlen(local.sun_path);
272#endif
273#if defined(BSD44SOCKETS) || defined(SUN_LEN)
274 namelen = SUN_LEN(&local);
275#else
276 namelen = strlen(local.sun_path) +
277 offsetof(struct sockaddr_un, sun_path);
278#endif
279
280 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
281 if (rc != 0) {
282 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
283 local.sun_path, errno, strerror(errno));
284 close(bfd->fd);
285 talloc_free(rf);
286 return NULL;
287 }
288
289 if (listen(bfd->fd, 0) != 0) {
290 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
291 close(bfd->fd);
292 talloc_free(rf);
293 return NULL;
294 }
295
296 bfd->when = BSC_FD_READ;
297 bfd->cb = rf_ctl_accept;
298 bfd->data = rf;
299
300 if (bsc_register_fd(bfd) != 0) {
301 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
302 close(bfd->fd);
303 talloc_free(rf);
304 return NULL;
305 }
306
307 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800308 rf->policy = S_RF_ON;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800309
310 return rf;
311}
312