blob: 6d2a17435c718982ea0d6ffd279f6c636d09d41b [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
29#include <osmocore/talloc.h>
30#include <osmocore/protocol/gsm_12_21.h>
31
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 Freyther70c232f2010-11-22 19:09:38 +0100109static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800110{
111 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100112 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800113
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100114 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800115 dispatch_signal(SS_RF, val, &sig);
116}
117
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100118static int switch_rf_off(struct osmo_bsc_rf *rf)
119{
120 lock_each_trx(rf->gsm_network, 1);
121 send_signal(rf, S_RF_OFF);
122
123 return 0;
124}
125
126static void grace_timeout(void *_data)
127{
128 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
129
130 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
131 switch_rf_off(rf);
132}
133
134static int enter_grace(struct osmo_bsc_rf_conn *conn)
135{
136 struct osmo_bsc_rf *rf = conn->rf;
137
138 rf->grace_timeout.cb = grace_timeout;
139 rf->grace_timeout.data = rf;
140 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
141 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
142 rf->gsm_network->msc_data->mid_call_timeout);
143
144 send_signal(conn->rf, S_RF_GRACE);
145 return 0;
146}
147
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800148static int rf_read_cmd(struct bsc_fd *fd)
149{
150 struct osmo_bsc_rf_conn *conn = fd->data;
151 char buf[1];
152 int rc;
153
154 rc = read(fd->fd, buf, sizeof(buf));
155 if (rc != sizeof(buf)) {
156 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
157 bsc_unregister_fd(fd);
158 close(fd->fd);
159 write_queue_clear(&conn->queue);
160 talloc_free(conn);
161 return -1;
162 }
163
164 switch (buf[0]) {
165 case RF_CMD_QUERY:
166 handle_query(conn);
167 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100168 case RF_CMD_D_OFF:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100169 conn->rf->last_state_command = "RF Direct 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 Freyther37ac4202011-02-24 14:19:14 +0100174 conn->rf->last_state_command = "RF Direct On";
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100175 bsc_del_timer(&conn->rf->grace_timeout);
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800176 lock_each_trx(conn->rf->gsm_network, 0);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100177 send_signal(conn->rf, S_RF_ON);
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800178 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100179 case RF_CMD_OFF:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100180 conn->rf->last_state_command = "RF Scheduled Off";
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100181 enter_grace(conn);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800182 break;
183 default:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100184 conn->rf->last_state_command = "Unknown command";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800185 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
186 break;
187 }
188
189 return 0;
190}
191
192static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
193{
194 int rc;
195
196 rc = write(fd->fd, msg->data, msg->len);
197 if (rc != msg->len) {
198 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
199 return -1;
200 }
201
202 return 0;
203}
204
205static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
206{
207 struct osmo_bsc_rf_conn *conn;
208 struct osmo_bsc_rf *rf = bfd->data;
209 struct sockaddr_un addr;
210 socklen_t len = sizeof(addr);
211 int fd;
212
213 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
214 if (fd < 0) {
215 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
216 errno, strerror(errno));
217 return -1;
218 }
219
220 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
221 if (!conn) {
222 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
223 close(fd);
224 return -1;
225 }
226
227 write_queue_init(&conn->queue, 10);
228 conn->queue.bfd.data = conn;
229 conn->queue.bfd.fd = fd;
230 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
231 conn->queue.read_cb = rf_read_cmd;
232 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800233 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800234
235 if (bsc_register_fd(&conn->queue.bfd) != 0) {
236 close(fd);
237 talloc_free(conn);
238 return -1;
239 }
240
241 return 0;
242}
243
244struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
245{
246 unsigned int namelen;
247 struct sockaddr_un local;
248 struct bsc_fd *bfd;
249 struct osmo_bsc_rf *rf;
250 int rc;
251
252 rf = talloc_zero(NULL, struct osmo_bsc_rf);
253 if (!rf) {
254 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
255 return NULL;
256 }
257
258 bfd = &rf->listen;
259 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
260 if (bfd->fd < 0) {
261 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
262 errno, strerror(errno));
263 return NULL;
264 }
265
266 local.sun_family = AF_UNIX;
267 strncpy(local.sun_path, path, sizeof(local.sun_path));
268 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
269 unlink(local.sun_path);
270
271 /* we use the same magic that X11 uses in Xtranssock.c for
272 * calculating the proper length of the sockaddr */
273#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
274 local.sun_len = strlen(local.sun_path);
275#endif
276#if defined(BSD44SOCKETS) || defined(SUN_LEN)
277 namelen = SUN_LEN(&local);
278#else
279 namelen = strlen(local.sun_path) +
280 offsetof(struct sockaddr_un, sun_path);
281#endif
282
283 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
284 if (rc != 0) {
285 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
286 local.sun_path, errno, strerror(errno));
287 close(bfd->fd);
288 talloc_free(rf);
289 return NULL;
290 }
291
292 if (listen(bfd->fd, 0) != 0) {
293 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
294 close(bfd->fd);
295 talloc_free(rf);
296 return NULL;
297 }
298
299 bfd->when = BSC_FD_READ;
300 bfd->cb = rf_ctl_accept;
301 bfd->data = rf;
302
303 if (bsc_register_fd(bfd) != 0) {
304 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
305 close(bfd->fd);
306 talloc_free(rf);
307 return NULL;
308 }
309
310 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800311 rf->policy = S_RF_ON;
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100312 rf->last_state_command = "";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800313
314 return rf;
315}
316