blob: 96e727e58d706fe45a391a02fb200ad8f25514c0 [file] [log] [blame]
Daniel Willmann5ff06af2011-08-05 12:20:58 +02001/* (C) 2011 by Daniel Willmann <daniel@totalueberwachung.de>
Holger Hans Peter Freyther23446842011-08-16 20:10:40 +02002 * (C) 2011 by Holger Hans Peter Freyther
Daniel Willmann5ff06af2011-08-05 12:20:58 +02003 * (C) 2011 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <openbsc/control_cmd.h>
22#include <openbsc/debug.h>
23#include <openbsc/gsm_data.h>
24#include <openbsc/osmo_bsc.h>
25#include <openbsc/osmo_bsc_rf.h>
26#include <openbsc/osmo_msc_data.h>
Daniel Willmann806d6542011-10-28 14:23:48 +020027#include <openbsc/signal.h>
Daniel Willmann5ff06af2011-08-05 12:20:58 +020028
29#include <osmocom/core/linuxlist.h>
Daniel Willmann806d6542011-10-28 14:23:48 +020030#include <osmocom/core/signal.h>
Daniel Willmann5ff06af2011-08-05 12:20:58 +020031#include <osmocom/core/talloc.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <time.h>
36#include <unistd.h>
37
38void osmo_bsc_send_trap(struct ctrl_cmd *cmd, struct bsc_msc_connection *msc_con)
39{
40 struct ctrl_cmd *trap;
41 struct ctrl_handle *ctrl;
42 struct osmo_msc_data *msc_data;
43
44 msc_data = (struct osmo_msc_data *) msc_con->write_queue.bfd.data;
45 ctrl = msc_data->network->ctrl;
46
47 trap = ctrl_cmd_trap(cmd);
48 if (!trap) {
49 LOGP(DCTRL, LOGL_ERROR, "Failed to create trap.\n");
50 return;
51 }
52
53 ctrl_cmd_send_to_all(ctrl, trap);
54 ctrl_cmd_send(&msc_con->write_queue, trap);
55
56 talloc_free(trap);
57}
58
Daniel Willmann806d6542011-10-28 14:23:48 +020059CTRL_CMD_DEFINE(msc_connection_status, "msc_connection_status");
60static int msc_connection_status = 0;
61
62static int get_msc_connection_status(struct ctrl_cmd *cmd, void *data)
63{
64 if (msc_connection_status)
65 cmd->reply = "connected";
66 else
67 cmd->reply = "disconnected";
68 return CTRL_CMD_REPLY;
69}
70
71static int set_msc_connection_status(struct ctrl_cmd *cmd, void *data)
72{
73 return CTRL_CMD_ERROR;
74}
75
76static int verify_msc_connection_status(struct ctrl_cmd *cmd, const char *value, void *data)
77{
78 cmd->reply = "Read-only property";
79 return 1;
80}
81
82static int msc_connection_status_trap_cb(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data)
83{
84 struct ctrl_cmd *cmd;
85 struct gsm_network *gsmnet = (struct gsm_network *)handler_data;
86
87 if (signal == S_MSC_LOST && msc_connection_status == 1) {
88 LOGP(DCTRL, LOGL_DEBUG, "MSC connection lost, sending TRAP.\n");
89 msc_connection_status = 0;
90 } else if (signal == S_MSC_CONNECTED && msc_connection_status == 0) {
91 LOGP(DCTRL, LOGL_DEBUG, "MSC connection (re)established, sending TRAP.\n");
92 msc_connection_status = 1;
93 } else {
94 return 0;
95 }
96
97 cmd = ctrl_cmd_create(tall_bsc_ctx, CTRL_TYPE_TRAP);
98 if (!cmd) {
99 LOGP(DCTRL, LOGL_ERROR, "Trap creation failed.\n");
100 return 0;
101 }
102
103 cmd->id = "0";
104 cmd->variable = "msc_connection_status";
105
106 get_msc_connection_status(cmd, NULL);
107
108 ctrl_cmd_send_to_all(gsmnet->ctrl, cmd);
109
110 talloc_free(cmd);
111
112 return 0;
113}
114
Daniel Willmann7a7c2f82011-11-03 16:23:08 +0100115CTRL_CMD_DEFINE(bts_connection_status, "bts_connection_status");
116static int bts_connection_status = 0;
117
118static int get_bts_connection_status(struct ctrl_cmd *cmd, void *data)
119{
120 if (bts_connection_status)
121 cmd->reply = "connected";
122 else
123 cmd->reply = "disconnected";
124 return CTRL_CMD_REPLY;
125}
126
127static int set_bts_connection_status(struct ctrl_cmd *cmd, void *data)
128{
129 return CTRL_CMD_ERROR;
130}
131
132static int verify_bts_connection_status(struct ctrl_cmd *cmd, const char *value, void *data)
133{
134 cmd->reply = "Read-only property";
135 return 1;
136}
137
138static int bts_connection_status_trap_cb(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data)
139{
140 struct ctrl_cmd *cmd;
141 struct gsm_network *gsmnet = (struct gsm_network *)handler_data;
142 struct gsm_bts *bts;
143 int bts_current_status;
144
145 if (signal != S_L_INP_TEI_DN && signal != S_L_INP_TEI_UP) {
146 return 0;
147 }
148
149 bts_current_status = 0;
150 /* Check if OML on at least one BTS is up */
151 llist_for_each_entry(bts, &gsmnet->bts_list, list) {
152 if (bts->oml_link) {
153 bts_current_status = 1;
154 break;
155 }
156 }
157 if (bts_connection_status == 0 && bts_current_status == 1) {
158 LOGP(DCTRL, LOGL_DEBUG, "BTS connection (re)established, sending TRAP.\n");
159 } else if (bts_connection_status == 1 && bts_current_status == 0) {
160 LOGP(DCTRL, LOGL_DEBUG, "No more BTS connected, sending TRAP.\n");
161 } else {
162 return 0;
163 }
164
165 cmd = ctrl_cmd_create(tall_bsc_ctx, CTRL_TYPE_TRAP);
166 if (!cmd) {
167 LOGP(DCTRL, LOGL_ERROR, "Trap creation failed.\n");
168 return 0;
169 }
170
171 bts_connection_status = bts_current_status;
172
173 cmd->id = "0";
174 cmd->variable = "bts_connection_status";
175
176 get_bts_connection_status(cmd, NULL);
177
178 ctrl_cmd_send_to_all(gsmnet->ctrl, cmd);
179
180 talloc_free(cmd);
181
182 return 0;
183}
Daniel Willmann806d6542011-10-28 14:23:48 +0200184
Daniel Willmann11620112011-08-19 19:32:09 +0200185static int get_bts_loc(struct ctrl_cmd *cmd, void *data);
186
187static void generate_location_state_trap(struct gsm_bts *bts, struct bsc_msc_connection *msc_con)
188{
189 struct ctrl_cmd *cmd;
Daniel Willmann65924a52011-08-19 19:38:31 +0200190 const char *oper, *admin, *policy;
Daniel Willmann11620112011-08-19 19:32:09 +0200191
192 cmd = ctrl_cmd_create(msc_con, CTRL_TYPE_TRAP);
193 if (!cmd) {
194 LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP command.\n");
195 return;
196 }
197
198 cmd->id = "0";
Daniel Willmann6088f142011-08-25 16:37:45 +0200199 cmd->variable = talloc_asprintf(cmd, "bts.%i.location-state", bts->nr);
Daniel Willmann11620112011-08-19 19:32:09 +0200200
201 /* Prepare the location reply */
202 cmd->node = bts;
203 get_bts_loc(cmd, NULL);
204
Daniel Willmann65924a52011-08-19 19:38:31 +0200205 oper = osmo_bsc_rf_get_opstate_name(osmo_bsc_rf_get_opstate_by_bts(bts));
206 admin = osmo_bsc_rf_get_adminstate_name(osmo_bsc_rf_get_adminstate_by_bts(bts));
207 policy = osmo_bsc_rf_get_policy_name(osmo_bsc_rf_get_policy_by_bts(bts));
208
209 cmd->reply = talloc_asprintf_append(cmd->reply, ",%s,%s,%s", oper, admin, policy);
210
Daniel Willmann11620112011-08-19 19:32:09 +0200211 osmo_bsc_send_trap(cmd, msc_con);
212 talloc_free(cmd);
213}
214
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200215static const struct value_string valid_names[] = {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200216 { BTS_LOC_FIX_INVALID, "invalid" },
217 { BTS_LOC_FIX_2D, "fix2d" },
218 { BTS_LOC_FIX_3D, "fix3d" },
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200219 { 0, NULL }
220};
221
Daniel Willmanna5352a02011-08-05 14:08:58 +0200222static int location_equal(struct bts_location *a, struct bts_location *b)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200223{
224 return ((a->tstamp == b->tstamp) && (a->valid == b->valid) && (a->lat == b->lat) &&
225 (a->lon == b->lon) && (a->height == b->height));
226}
227
Daniel Willmanna5352a02011-08-05 14:08:58 +0200228static void cleanup_locations(struct llist_head *locations)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200229{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200230 struct bts_location *myloc, *tmp;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200231 int invalpos = 0, i = 0;
232
233 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
Daniel Willmanna5352a02011-08-05 14:08:58 +0200234 llist_for_each_entry_safe(myloc, tmp, locations, list) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200235 i++;
236 if (i > 3) {
237 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
238 llist_del(&myloc->list);
239 talloc_free(myloc);
Daniel Willmanna5352a02011-08-05 14:08:58 +0200240 } else if (myloc->valid == BTS_LOC_FIX_INVALID) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200241 /* Only capture the newest of subsequent invalid positions */
242 invalpos++;
243 if (invalpos > 1) {
244 LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
245 invalpos--;
246 i--;
247 llist_del(&myloc->list);
248 talloc_free(myloc);
249 }
250 } else {
251 invalpos = 0;
252 }
253 }
254 LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);
255}
256
Daniel Willmanna5352a02011-08-05 14:08:58 +0200257CTRL_CMD_DEFINE(bts_loc, "location");
258static int get_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200259{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200260 struct bts_location *curloc;
261 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
262 if (!bts) {
263 cmd->reply = "bts not found.";
264 return CTRL_CMD_ERROR;
265 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200266
Daniel Willmanna5352a02011-08-05 14:08:58 +0200267 if (llist_empty(&bts->loc_list)) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200268 cmd->reply = talloc_asprintf(cmd, "0,invalid,0,0,0");
269 return CTRL_CMD_REPLY;
270 } else {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200271 curloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200272 }
273
Daniel Willmanna5352a02011-08-05 14:08:58 +0200274 cmd->reply = talloc_asprintf(cmd, "%lu,%s,%f,%f,%f", curloc->tstamp,
275 get_value_string(valid_names, curloc->valid), curloc->lat, curloc->lon, curloc->height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200276 if (!cmd->reply) {
277 cmd->reply = "OOM";
278 return CTRL_CMD_ERROR;
279 }
280
281 return CTRL_CMD_REPLY;
282}
283
Daniel Willmanna5352a02011-08-05 14:08:58 +0200284static int set_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200285{
286 char *saveptr, *lat, *lon, *height, *tstamp, *valid, *tmp;
287 struct osmo_msc_data *msc;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200288 struct bts_location *curloc, *lastloc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200289 int ret;
290 struct gsm_network *gsmnet = (struct gsm_network *)data;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200291 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
292 if (!bts) {
293 cmd->reply = "bts not found.";
294 return CTRL_CMD_ERROR;
295 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200296
297 tmp = talloc_strdup(cmd, cmd->value);
298 if (!tmp)
299 goto oom;
300
Daniel Willmanna5352a02011-08-05 14:08:58 +0200301 curloc = talloc_zero(tall_bsc_ctx, struct bts_location);
302 if (!curloc) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200303 talloc_free(tmp);
304 goto oom;
305 }
Daniel Willmanna5352a02011-08-05 14:08:58 +0200306 INIT_LLIST_HEAD(&curloc->list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200307
308
309 tstamp = strtok_r(tmp, ",", &saveptr);
310 valid = strtok_r(NULL, ",", &saveptr);
311 lat = strtok_r(NULL, ",", &saveptr);
312 lon = strtok_r(NULL, ",", &saveptr);
313 height = strtok_r(NULL, "\0", &saveptr);
314
Daniel Willmanna5352a02011-08-05 14:08:58 +0200315 curloc->tstamp = atol(tstamp);
316 curloc->valid = get_string_value(valid_names, valid);
317 curloc->lat = atof(lat);
318 curloc->lon = atof(lon);
319 curloc->height = atof(height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200320 talloc_free(tmp);
321
Daniel Willmanna5352a02011-08-05 14:08:58 +0200322 lastloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200323
324 /* Add location to the end of the list */
Daniel Willmanna5352a02011-08-05 14:08:58 +0200325 llist_add(&curloc->list, &bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200326
Daniel Willmanna5352a02011-08-05 14:08:58 +0200327 ret = get_bts_loc(cmd, data);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200328
Daniel Willmanna5352a02011-08-05 14:08:58 +0200329 if (!location_equal(curloc, lastloc))
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200330 llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry)
Daniel Willmann11620112011-08-19 19:32:09 +0200331 generate_location_state_trap(bts, msc->msc_con);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200332
Daniel Willmanna5352a02011-08-05 14:08:58 +0200333 cleanup_locations(&bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200334
335 return ret;
336
337oom:
338 cmd->reply = "OOM";
339 return CTRL_CMD_ERROR;
340}
341
Daniel Willmanna5352a02011-08-05 14:08:58 +0200342static int verify_bts_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200343{
344 char *saveptr, *latstr, *lonstr, *heightstr, *tstampstr, *validstr, *tmp;
345 time_t tstamp;
346 int valid;
347 double lat, lon, height;
348
349 tmp = talloc_strdup(cmd, value);
350 if (!tmp)
351 return 1;
352
353 tstampstr = strtok_r(tmp, ",", &saveptr);
354 validstr = strtok_r(NULL, ",", &saveptr);
355 latstr = strtok_r(NULL, ",", &saveptr);
356 lonstr = strtok_r(NULL, ",", &saveptr);
357 heightstr = strtok_r(NULL, "\0", &saveptr);
358
359 if ((tstampstr == NULL) || (validstr == NULL) || (latstr == NULL) ||
360 (lonstr == NULL) || (heightstr == NULL))
361 goto err;
362
363 tstamp = atol(tstampstr);
364 valid = get_string_value(valid_names, validstr);
365 lat = atof(latstr);
366 lon = atof(lonstr);
367 height = atof(heightstr);
368 talloc_free(tmp);
369
Daniel Willmanna5352a02011-08-05 14:08:58 +0200370 if (((tstamp == 0) && (valid != BTS_LOC_FIX_INVALID)) || (lat < -90) || (lat > 90) ||
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200371 (lon < -180) || (lon > 180) || (valid < 0)) {
372 goto err;
373 }
374
375 return 0;
376err:
377 cmd->reply = talloc_strdup(cmd, "The format is <unixtime>,(invalid|fix2d|fix3d),<lat>,<lon>,<height>");
378 return 1;
379}
380
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200381CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
382static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
383{
384 cmd->reply = "get only works for the individual trx properties.";
385 return CTRL_CMD_ERROR;
386}
387
388static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
389{
390 int locked = atoi(cmd->value);
391 struct gsm_network *net = cmd->node;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200392 if (!net) {
393 cmd->reply = "net not found.";
394 return CTRL_CMD_ERROR;
395 }
396
Holger Hans Peter Freyther7b6ea562011-08-16 14:29:53 +0200397 if (!net->bsc_data->rf_ctrl) {
398 cmd->reply = "RF Ctrl not enabled";
399 return CTRL_CMD_ERROR;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200400 }
401
Holger Hans Peter Freyther7b6ea562011-08-16 14:29:53 +0200402 osmo_bsc_rf_schedule_lock(net->bsc_data->rf_ctrl,
403 locked == 1 ? '0' : '1');
404
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200405 cmd->reply = talloc_asprintf(cmd, "%u", locked);
406 if (!cmd->reply) {
407 cmd->reply = "OOM.";
408 return CTRL_CMD_ERROR;
409 }
410
411 return CTRL_CMD_REPLY;
412}
413
414static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
415{
416 int locked = atoi(cmd->value);
417
418 if ((locked != 0) && (locked != 1))
419 return 1;
420
421 return 0;
422}
423
Holger Hans Peter Freyther23446842011-08-16 20:10:40 +0200424static int msc_signal_handler(unsigned int subsys, unsigned int signal,
425 void *handler_data, void *signal_data)
426{
427 struct ctrl_cmd *cmd;
428 struct msc_signal_data *msc;
429 struct gsm_network *net;
430 struct gsm_bts *bts;
431 char *loc_fmt = "net.bts.%i.location";
432
433 if (subsys != SS_MSC)
434 return 0;
435 if (signal != S_MSC_AUTHENTICATED)
436 return 0;
437
438 msc = signal_data;
439 cmd = ctrl_cmd_create(msc->data, CTRL_TYPE_TRAP);
440 if (!cmd) {
441 LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP for location.\n");
442 return 0;
443 }
444
445 cmd->id = "0";
446
447 net = msc->data->network;
448 llist_for_each_entry(bts, &net->bts_list, list) {
449 cmd->node = bts;
450 cmd->variable = talloc_asprintf(cmd, loc_fmt, bts->nr);
451 get_bts_loc(cmd, NULL);
452 osmo_bsc_send_trap(cmd, msc->data->msc_con);
453 talloc_free(cmd->variable);
454 }
455
456 talloc_free(cmd);
457 return 0;
458}
459
Daniel Willmann806d6542011-10-28 14:23:48 +0200460int bsc_ctrl_cmds_install(struct gsm_network *net)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200461{
Daniel Willmann5e95f452011-08-05 12:22:35 +0200462 int rc;
463
Daniel Willmanna5352a02011-08-05 14:08:58 +0200464 rc = ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_loc);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200465 if (rc)
466 goto end;
Daniel Willmann6088f142011-08-25 16:37:45 +0200467 rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_rf_lock);
Daniel Willmann806d6542011-10-28 14:23:48 +0200468 if (rc)
469 goto end;
470 rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_msc_connection_status);
471 if (rc)
472 goto end;
473 rc = osmo_signal_register_handler(SS_MSC, &msc_connection_status_trap_cb, net);
Daniel Willmann7a7c2f82011-11-03 16:23:08 +0100474 if (rc)
475 goto end;
Holger Hans Peter Freyther23446842011-08-16 20:10:40 +0200476 rc = osmo_signal_register_handler(SS_MSC, msc_signal_handler, NULL);
477 if (rc)
478 goto end;
Daniel Willmann7a7c2f82011-11-03 16:23:08 +0100479 rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_bts_connection_status);
480 if (rc)
481 goto end;
482 rc = osmo_signal_register_handler(SS_L_INPUT, &bts_connection_status_trap_cb, net);
Holger Hans Peter Freyther23446842011-08-16 20:10:40 +0200483
Daniel Willmann5e95f452011-08-05 12:22:35 +0200484end:
485 return rc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200486}