blob: bdec2b89c2163e7bd455f12d11d16f4a6014a1be [file] [log] [blame]
Daniel Willmann5ff06af2011-08-05 12:20:58 +02001/* (C) 2011 by Daniel Willmann <daniel@totalueberwachung.de>
2 * (C) 2011 by On-Waves
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <openbsc/control_cmd.h>
21#include <openbsc/debug.h>
22#include <openbsc/gsm_data.h>
23#include <openbsc/osmo_bsc.h>
24#include <openbsc/osmo_bsc_rf.h>
25#include <openbsc/osmo_msc_data.h>
Daniel Willmann806d6542011-10-28 14:23:48 +020026#include <openbsc/signal.h>
Daniel Willmann5ff06af2011-08-05 12:20:58 +020027
28#include <osmocom/core/linuxlist.h>
Daniel Willmann806d6542011-10-28 14:23:48 +020029#include <osmocom/core/signal.h>
Daniel Willmann5ff06af2011-08-05 12:20:58 +020030#include <osmocom/core/talloc.h>
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <time.h>
35#include <unistd.h>
36
37void osmo_bsc_send_trap(struct ctrl_cmd *cmd, struct bsc_msc_connection *msc_con)
38{
39 struct ctrl_cmd *trap;
40 struct ctrl_handle *ctrl;
41 struct osmo_msc_data *msc_data;
42
43 msc_data = (struct osmo_msc_data *) msc_con->write_queue.bfd.data;
44 ctrl = msc_data->network->ctrl;
45
46 trap = ctrl_cmd_trap(cmd);
47 if (!trap) {
48 LOGP(DCTRL, LOGL_ERROR, "Failed to create trap.\n");
49 return;
50 }
51
52 ctrl_cmd_send_to_all(ctrl, trap);
53 ctrl_cmd_send(&msc_con->write_queue, trap);
54
55 talloc_free(trap);
56}
57
Daniel Willmann806d6542011-10-28 14:23:48 +020058CTRL_CMD_DEFINE(msc_connection_status, "msc_connection_status");
59static int msc_connection_status = 0;
60
61static int get_msc_connection_status(struct ctrl_cmd *cmd, void *data)
62{
63 if (msc_connection_status)
64 cmd->reply = "connected";
65 else
66 cmd->reply = "disconnected";
67 return CTRL_CMD_REPLY;
68}
69
70static int set_msc_connection_status(struct ctrl_cmd *cmd, void *data)
71{
72 return CTRL_CMD_ERROR;
73}
74
75static int verify_msc_connection_status(struct ctrl_cmd *cmd, const char *value, void *data)
76{
77 cmd->reply = "Read-only property";
78 return 1;
79}
80
81static int msc_connection_status_trap_cb(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data)
82{
83 struct ctrl_cmd *cmd;
84 struct gsm_network *gsmnet = (struct gsm_network *)handler_data;
85
86 if (signal == S_MSC_LOST && msc_connection_status == 1) {
87 LOGP(DCTRL, LOGL_DEBUG, "MSC connection lost, sending TRAP.\n");
88 msc_connection_status = 0;
89 } else if (signal == S_MSC_CONNECTED && msc_connection_status == 0) {
90 LOGP(DCTRL, LOGL_DEBUG, "MSC connection (re)established, sending TRAP.\n");
91 msc_connection_status = 1;
92 } else {
93 return 0;
94 }
95
96 cmd = ctrl_cmd_create(tall_bsc_ctx, CTRL_TYPE_TRAP);
97 if (!cmd) {
98 LOGP(DCTRL, LOGL_ERROR, "Trap creation failed.\n");
99 return 0;
100 }
101
102 cmd->id = "0";
103 cmd->variable = "msc_connection_status";
104
105 get_msc_connection_status(cmd, NULL);
106
107 ctrl_cmd_send_to_all(gsmnet->ctrl, cmd);
108
109 talloc_free(cmd);
110
111 return 0;
112}
113
114
Daniel Willmann11620112011-08-19 19:32:09 +0200115static int get_bts_loc(struct ctrl_cmd *cmd, void *data);
116
117static void generate_location_state_trap(struct gsm_bts *bts, struct bsc_msc_connection *msc_con)
118{
119 struct ctrl_cmd *cmd;
Daniel Willmann65924a52011-08-19 19:38:31 +0200120 const char *oper, *admin, *policy;
Daniel Willmann11620112011-08-19 19:32:09 +0200121
122 cmd = ctrl_cmd_create(msc_con, CTRL_TYPE_TRAP);
123 if (!cmd) {
124 LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP command.\n");
125 return;
126 }
127
128 cmd->id = "0";
Daniel Willmann6088f142011-08-25 16:37:45 +0200129 cmd->variable = talloc_asprintf(cmd, "bts.%i.location-state", bts->nr);
Daniel Willmann11620112011-08-19 19:32:09 +0200130
131 /* Prepare the location reply */
132 cmd->node = bts;
133 get_bts_loc(cmd, NULL);
134
Daniel Willmann65924a52011-08-19 19:38:31 +0200135 oper = osmo_bsc_rf_get_opstate_name(osmo_bsc_rf_get_opstate_by_bts(bts));
136 admin = osmo_bsc_rf_get_adminstate_name(osmo_bsc_rf_get_adminstate_by_bts(bts));
137 policy = osmo_bsc_rf_get_policy_name(osmo_bsc_rf_get_policy_by_bts(bts));
138
139 cmd->reply = talloc_asprintf_append(cmd->reply, ",%s,%s,%s", oper, admin, policy);
140
Daniel Willmann11620112011-08-19 19:32:09 +0200141 osmo_bsc_send_trap(cmd, msc_con);
142 talloc_free(cmd);
143}
144
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200145static const struct value_string valid_names[] = {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200146 { BTS_LOC_FIX_INVALID, "invalid" },
147 { BTS_LOC_FIX_2D, "fix2d" },
148 { BTS_LOC_FIX_3D, "fix3d" },
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200149 { 0, NULL }
150};
151
Daniel Willmanna5352a02011-08-05 14:08:58 +0200152static int location_equal(struct bts_location *a, struct bts_location *b)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200153{
154 return ((a->tstamp == b->tstamp) && (a->valid == b->valid) && (a->lat == b->lat) &&
155 (a->lon == b->lon) && (a->height == b->height));
156}
157
Daniel Willmanna5352a02011-08-05 14:08:58 +0200158static void cleanup_locations(struct llist_head *locations)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200159{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200160 struct bts_location *myloc, *tmp;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200161 int invalpos = 0, i = 0;
162
163 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
Daniel Willmanna5352a02011-08-05 14:08:58 +0200164 llist_for_each_entry_safe(myloc, tmp, locations, list) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200165 i++;
166 if (i > 3) {
167 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
168 llist_del(&myloc->list);
169 talloc_free(myloc);
Daniel Willmanna5352a02011-08-05 14:08:58 +0200170 } else if (myloc->valid == BTS_LOC_FIX_INVALID) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200171 /* Only capture the newest of subsequent invalid positions */
172 invalpos++;
173 if (invalpos > 1) {
174 LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
175 invalpos--;
176 i--;
177 llist_del(&myloc->list);
178 talloc_free(myloc);
179 }
180 } else {
181 invalpos = 0;
182 }
183 }
184 LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);
185}
186
Daniel Willmanna5352a02011-08-05 14:08:58 +0200187CTRL_CMD_DEFINE(bts_loc, "location");
188static int get_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200189{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200190 struct bts_location *curloc;
191 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
192 if (!bts) {
193 cmd->reply = "bts not found.";
194 return CTRL_CMD_ERROR;
195 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200196
Daniel Willmanna5352a02011-08-05 14:08:58 +0200197 if (llist_empty(&bts->loc_list)) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200198 cmd->reply = talloc_asprintf(cmd, "0,invalid,0,0,0");
199 return CTRL_CMD_REPLY;
200 } else {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200201 curloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200202 }
203
Daniel Willmanna5352a02011-08-05 14:08:58 +0200204 cmd->reply = talloc_asprintf(cmd, "%lu,%s,%f,%f,%f", curloc->tstamp,
205 get_value_string(valid_names, curloc->valid), curloc->lat, curloc->lon, curloc->height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200206 if (!cmd->reply) {
207 cmd->reply = "OOM";
208 return CTRL_CMD_ERROR;
209 }
210
211 return CTRL_CMD_REPLY;
212}
213
Daniel Willmanna5352a02011-08-05 14:08:58 +0200214static int set_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200215{
216 char *saveptr, *lat, *lon, *height, *tstamp, *valid, *tmp;
217 struct osmo_msc_data *msc;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200218 struct bts_location *curloc, *lastloc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200219 int ret;
220 struct gsm_network *gsmnet = (struct gsm_network *)data;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200221 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
222 if (!bts) {
223 cmd->reply = "bts not found.";
224 return CTRL_CMD_ERROR;
225 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200226
227 tmp = talloc_strdup(cmd, cmd->value);
228 if (!tmp)
229 goto oom;
230
Daniel Willmanna5352a02011-08-05 14:08:58 +0200231 curloc = talloc_zero(tall_bsc_ctx, struct bts_location);
232 if (!curloc) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200233 talloc_free(tmp);
234 goto oom;
235 }
Daniel Willmanna5352a02011-08-05 14:08:58 +0200236 INIT_LLIST_HEAD(&curloc->list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200237
238
239 tstamp = strtok_r(tmp, ",", &saveptr);
240 valid = strtok_r(NULL, ",", &saveptr);
241 lat = strtok_r(NULL, ",", &saveptr);
242 lon = strtok_r(NULL, ",", &saveptr);
243 height = strtok_r(NULL, "\0", &saveptr);
244
Daniel Willmanna5352a02011-08-05 14:08:58 +0200245 curloc->tstamp = atol(tstamp);
246 curloc->valid = get_string_value(valid_names, valid);
247 curloc->lat = atof(lat);
248 curloc->lon = atof(lon);
249 curloc->height = atof(height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200250 talloc_free(tmp);
251
Daniel Willmanna5352a02011-08-05 14:08:58 +0200252 lastloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200253
254 /* Add location to the end of the list */
Daniel Willmanna5352a02011-08-05 14:08:58 +0200255 llist_add(&curloc->list, &bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200256
Daniel Willmanna5352a02011-08-05 14:08:58 +0200257 ret = get_bts_loc(cmd, data);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200258
Daniel Willmanna5352a02011-08-05 14:08:58 +0200259 if (!location_equal(curloc, lastloc))
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200260 llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry)
Daniel Willmann11620112011-08-19 19:32:09 +0200261 generate_location_state_trap(bts, msc->msc_con);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200262
Daniel Willmanna5352a02011-08-05 14:08:58 +0200263 cleanup_locations(&bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200264
265 return ret;
266
267oom:
268 cmd->reply = "OOM";
269 return CTRL_CMD_ERROR;
270}
271
Daniel Willmanna5352a02011-08-05 14:08:58 +0200272static int verify_bts_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200273{
274 char *saveptr, *latstr, *lonstr, *heightstr, *tstampstr, *validstr, *tmp;
275 time_t tstamp;
276 int valid;
277 double lat, lon, height;
278
279 tmp = talloc_strdup(cmd, value);
280 if (!tmp)
281 return 1;
282
283 tstampstr = strtok_r(tmp, ",", &saveptr);
284 validstr = strtok_r(NULL, ",", &saveptr);
285 latstr = strtok_r(NULL, ",", &saveptr);
286 lonstr = strtok_r(NULL, ",", &saveptr);
287 heightstr = strtok_r(NULL, "\0", &saveptr);
288
289 if ((tstampstr == NULL) || (validstr == NULL) || (latstr == NULL) ||
290 (lonstr == NULL) || (heightstr == NULL))
291 goto err;
292
293 tstamp = atol(tstampstr);
294 valid = get_string_value(valid_names, validstr);
295 lat = atof(latstr);
296 lon = atof(lonstr);
297 height = atof(heightstr);
298 talloc_free(tmp);
299
Daniel Willmanna5352a02011-08-05 14:08:58 +0200300 if (((tstamp == 0) && (valid != BTS_LOC_FIX_INVALID)) || (lat < -90) || (lat > 90) ||
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200301 (lon < -180) || (lon > 180) || (valid < 0)) {
302 goto err;
303 }
304
305 return 0;
306err:
307 cmd->reply = talloc_strdup(cmd, "The format is <unixtime>,(invalid|fix2d|fix3d),<lat>,<lon>,<height>");
308 return 1;
309}
310
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200311CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
312static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
313{
314 cmd->reply = "get only works for the individual trx properties.";
315 return CTRL_CMD_ERROR;
316}
317
318static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
319{
320 int locked = atoi(cmd->value);
321 struct gsm_network *net = cmd->node;
322 struct gsm_bts *bts;
323 if (!net) {
324 cmd->reply = "net not found.";
325 return CTRL_CMD_ERROR;
326 }
327
328 llist_for_each_entry(bts, &net->bts_list, list) {
329 struct gsm_bts_trx *trx;
330 llist_for_each_entry(trx, &bts->trx_list, list) {
331 gsm_trx_lock_rf(trx, locked);
332 }
333 }
334
335 cmd->reply = talloc_asprintf(cmd, "%u", locked);
336 if (!cmd->reply) {
337 cmd->reply = "OOM.";
338 return CTRL_CMD_ERROR;
339 }
340
341 return CTRL_CMD_REPLY;
342}
343
344static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
345{
346 int locked = atoi(cmd->value);
347
348 if ((locked != 0) && (locked != 1))
349 return 1;
350
351 return 0;
352}
353
Daniel Willmann806d6542011-10-28 14:23:48 +0200354int bsc_ctrl_cmds_install(struct gsm_network *net)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200355{
Daniel Willmann5e95f452011-08-05 12:22:35 +0200356 int rc;
357
Daniel Willmanna5352a02011-08-05 14:08:58 +0200358 rc = ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_loc);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200359 if (rc)
360 goto end;
Daniel Willmann6088f142011-08-25 16:37:45 +0200361 rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_rf_lock);
Daniel Willmann806d6542011-10-28 14:23:48 +0200362 if (rc)
363 goto end;
364 rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_msc_connection_status);
365 if (rc)
366 goto end;
367 rc = osmo_signal_register_handler(SS_MSC, &msc_connection_status_trap_cb, net);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200368end:
369 return rc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200370}