blob: d5a265b019929732c751cb2bd42f776a350490b2 [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 Freyther12b917d2010-07-29 02:27:27 +080042#define RF_CMD_GRACE '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
58/*
59 * Send a '1' when one TRX is online, otherwise send 0
60 */
61static void handle_query(struct osmo_bsc_rf_conn *conn)
62{
63 struct msgb *msg;
64 struct gsm_bts *bts;
65 char send = RF_CMD_OFF;
66
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +080067 llist_for_each_entry(bts, &conn->rf->gsm_network->bts_list, list) {
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080068 struct gsm_bts_trx *trx;
69 llist_for_each_entry(trx, &bts->trx_list, list) {
70 if (trx->nm_state.availability == NM_AVSTATE_OK &&
71 trx->nm_state.operational != NM_STATE_LOCKED) {
72 send = RF_CMD_ON;
73 break;
74 }
75 }
76 }
77
78 msg = msgb_alloc(10, "RF Query");
79 if (!msg) {
80 LOGP(DINP, LOGL_ERROR, "Failed to allocate response msg.\n");
81 return;
82 }
83
84 msg->l2h = msgb_put(msg, 1);
85 msg->l2h[0] = send;
86
87 if (write_queue_enqueue(&conn->queue, msg) != 0) {
88 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the answer.\n");
89 msgb_free(msg);
90 return;
91 }
92
93 return;
94}
95
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +010096static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +080097{
98 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +010099 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800100
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100101 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800102 dispatch_signal(SS_RF, val, &sig);
103}
104
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100105static int switch_rf_off(struct osmo_bsc_rf *rf)
106{
107 lock_each_trx(rf->gsm_network, 1);
108 send_signal(rf, S_RF_OFF);
109
110 return 0;
111}
112
113static void grace_timeout(void *_data)
114{
115 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
116
117 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
118 switch_rf_off(rf);
119}
120
121static int enter_grace(struct osmo_bsc_rf_conn *conn)
122{
123 struct osmo_bsc_rf *rf = conn->rf;
124
125 rf->grace_timeout.cb = grace_timeout;
126 rf->grace_timeout.data = rf;
127 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
128 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
129 rf->gsm_network->msc_data->mid_call_timeout);
130
131 send_signal(conn->rf, S_RF_GRACE);
132 return 0;
133}
134
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800135static int rf_read_cmd(struct bsc_fd *fd)
136{
137 struct osmo_bsc_rf_conn *conn = fd->data;
138 char buf[1];
139 int rc;
140
141 rc = read(fd->fd, buf, sizeof(buf));
142 if (rc != sizeof(buf)) {
143 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
144 bsc_unregister_fd(fd);
145 close(fd->fd);
146 write_queue_clear(&conn->queue);
147 talloc_free(conn);
148 return -1;
149 }
150
151 switch (buf[0]) {
152 case RF_CMD_QUERY:
153 handle_query(conn);
154 break;
155 case RF_CMD_OFF:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100156 bsc_del_timer(&conn->rf->grace_timeout);
157 switch_rf_off(conn->rf);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800158 break;
159 case RF_CMD_ON:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100160 bsc_del_timer(&conn->rf->grace_timeout);
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800161 lock_each_trx(conn->rf->gsm_network, 0);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100162 send_signal(conn->rf, S_RF_ON);
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800163 break;
164 case RF_CMD_GRACE:
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100165 enter_grace(conn);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800166 break;
167 default:
168 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
169 break;
170 }
171
172 return 0;
173}
174
175static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
176{
177 int rc;
178
179 rc = write(fd->fd, msg->data, msg->len);
180 if (rc != msg->len) {
181 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
182 return -1;
183 }
184
185 return 0;
186}
187
188static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
189{
190 struct osmo_bsc_rf_conn *conn;
191 struct osmo_bsc_rf *rf = bfd->data;
192 struct sockaddr_un addr;
193 socklen_t len = sizeof(addr);
194 int fd;
195
196 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
197 if (fd < 0) {
198 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
199 errno, strerror(errno));
200 return -1;
201 }
202
203 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
204 if (!conn) {
205 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
206 close(fd);
207 return -1;
208 }
209
210 write_queue_init(&conn->queue, 10);
211 conn->queue.bfd.data = conn;
212 conn->queue.bfd.fd = fd;
213 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
214 conn->queue.read_cb = rf_read_cmd;
215 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800216 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800217
218 if (bsc_register_fd(&conn->queue.bfd) != 0) {
219 close(fd);
220 talloc_free(conn);
221 return -1;
222 }
223
224 return 0;
225}
226
227struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
228{
229 unsigned int namelen;
230 struct sockaddr_un local;
231 struct bsc_fd *bfd;
232 struct osmo_bsc_rf *rf;
233 int rc;
234
235 rf = talloc_zero(NULL, struct osmo_bsc_rf);
236 if (!rf) {
237 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
238 return NULL;
239 }
240
241 bfd = &rf->listen;
242 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
243 if (bfd->fd < 0) {
244 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
245 errno, strerror(errno));
246 return NULL;
247 }
248
249 local.sun_family = AF_UNIX;
250 strncpy(local.sun_path, path, sizeof(local.sun_path));
251 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
252 unlink(local.sun_path);
253
254 /* we use the same magic that X11 uses in Xtranssock.c for
255 * calculating the proper length of the sockaddr */
256#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
257 local.sun_len = strlen(local.sun_path);
258#endif
259#if defined(BSD44SOCKETS) || defined(SUN_LEN)
260 namelen = SUN_LEN(&local);
261#else
262 namelen = strlen(local.sun_path) +
263 offsetof(struct sockaddr_un, sun_path);
264#endif
265
266 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
267 if (rc != 0) {
268 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
269 local.sun_path, errno, strerror(errno));
270 close(bfd->fd);
271 talloc_free(rf);
272 return NULL;
273 }
274
275 if (listen(bfd->fd, 0) != 0) {
276 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
277 close(bfd->fd);
278 talloc_free(rf);
279 return NULL;
280 }
281
282 bfd->when = BSC_FD_READ;
283 bfd->cb = rf_ctl_accept;
284 bfd->data = rf;
285
286 if (bsc_register_fd(bfd) != 0) {
287 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
288 close(bfd->fd);
289 talloc_free(rf);
290 return NULL;
291 }
292
293 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800294 rf->policy = S_RF_ON;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800295
296 return rf;
297}
298