blob: ba8c1d26417e8873d7a2368c89edbdac7947a893 [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>
26
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/talloc.h>
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <time.h>
33#include <unistd.h>
34
35void osmo_bsc_send_trap(struct ctrl_cmd *cmd, struct bsc_msc_connection *msc_con)
36{
37 struct ctrl_cmd *trap;
38 struct ctrl_handle *ctrl;
39 struct osmo_msc_data *msc_data;
40
41 msc_data = (struct osmo_msc_data *) msc_con->write_queue.bfd.data;
42 ctrl = msc_data->network->ctrl;
43
44 trap = ctrl_cmd_trap(cmd);
45 if (!trap) {
46 LOGP(DCTRL, LOGL_ERROR, "Failed to create trap.\n");
47 return;
48 }
49
50 ctrl_cmd_send_to_all(ctrl, trap);
51 ctrl_cmd_send(&msc_con->write_queue, trap);
52
53 talloc_free(trap);
54}
55
Daniel Willmann11620112011-08-19 19:32:09 +020056static int get_bts_loc(struct ctrl_cmd *cmd, void *data);
57
58static void generate_location_state_trap(struct gsm_bts *bts, struct bsc_msc_connection *msc_con)
59{
60 struct ctrl_cmd *cmd;
Daniel Willmann65924a52011-08-19 19:38:31 +020061 const char *oper, *admin, *policy;
Daniel Willmann11620112011-08-19 19:32:09 +020062
63 cmd = ctrl_cmd_create(msc_con, CTRL_TYPE_TRAP);
64 if (!cmd) {
65 LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP command.\n");
66 return;
67 }
68
69 cmd->id = "0";
70 cmd->variable = talloc_asprintf(cmd, "net.bts.%i.location-state", bts->nr);
71
72 /* Prepare the location reply */
73 cmd->node = bts;
74 get_bts_loc(cmd, NULL);
75
Daniel Willmann65924a52011-08-19 19:38:31 +020076 oper = osmo_bsc_rf_get_opstate_name(osmo_bsc_rf_get_opstate_by_bts(bts));
77 admin = osmo_bsc_rf_get_adminstate_name(osmo_bsc_rf_get_adminstate_by_bts(bts));
78 policy = osmo_bsc_rf_get_policy_name(osmo_bsc_rf_get_policy_by_bts(bts));
79
80 cmd->reply = talloc_asprintf_append(cmd->reply, ",%s,%s,%s", oper, admin, policy);
81
Daniel Willmann11620112011-08-19 19:32:09 +020082 osmo_bsc_send_trap(cmd, msc_con);
83 talloc_free(cmd);
84}
85
Daniel Willmann5ff06af2011-08-05 12:20:58 +020086static const struct value_string valid_names[] = {
Daniel Willmanna5352a02011-08-05 14:08:58 +020087 { BTS_LOC_FIX_INVALID, "invalid" },
88 { BTS_LOC_FIX_2D, "fix2d" },
89 { BTS_LOC_FIX_3D, "fix3d" },
Daniel Willmann5ff06af2011-08-05 12:20:58 +020090 { 0, NULL }
91};
92
Daniel Willmanna5352a02011-08-05 14:08:58 +020093static int location_equal(struct bts_location *a, struct bts_location *b)
Daniel Willmann5ff06af2011-08-05 12:20:58 +020094{
95 return ((a->tstamp == b->tstamp) && (a->valid == b->valid) && (a->lat == b->lat) &&
96 (a->lon == b->lon) && (a->height == b->height));
97}
98
Daniel Willmanna5352a02011-08-05 14:08:58 +020099static void cleanup_locations(struct llist_head *locations)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200100{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200101 struct bts_location *myloc, *tmp;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200102 int invalpos = 0, i = 0;
103
104 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
Daniel Willmanna5352a02011-08-05 14:08:58 +0200105 llist_for_each_entry_safe(myloc, tmp, locations, list) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200106 i++;
107 if (i > 3) {
108 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
109 llist_del(&myloc->list);
110 talloc_free(myloc);
Daniel Willmanna5352a02011-08-05 14:08:58 +0200111 } else if (myloc->valid == BTS_LOC_FIX_INVALID) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200112 /* Only capture the newest of subsequent invalid positions */
113 invalpos++;
114 if (invalpos > 1) {
115 LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
116 invalpos--;
117 i--;
118 llist_del(&myloc->list);
119 talloc_free(myloc);
120 }
121 } else {
122 invalpos = 0;
123 }
124 }
125 LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);
126}
127
Daniel Willmanna5352a02011-08-05 14:08:58 +0200128CTRL_CMD_DEFINE(bts_loc, "location");
129static int get_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200130{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200131 struct bts_location *curloc;
132 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
133 if (!bts) {
134 cmd->reply = "bts not found.";
135 return CTRL_CMD_ERROR;
136 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200137
Daniel Willmanna5352a02011-08-05 14:08:58 +0200138 if (llist_empty(&bts->loc_list)) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200139 cmd->reply = talloc_asprintf(cmd, "0,invalid,0,0,0");
140 return CTRL_CMD_REPLY;
141 } else {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200142 curloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200143 }
144
Daniel Willmanna5352a02011-08-05 14:08:58 +0200145 cmd->reply = talloc_asprintf(cmd, "%lu,%s,%f,%f,%f", curloc->tstamp,
146 get_value_string(valid_names, curloc->valid), curloc->lat, curloc->lon, curloc->height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200147 if (!cmd->reply) {
148 cmd->reply = "OOM";
149 return CTRL_CMD_ERROR;
150 }
151
152 return CTRL_CMD_REPLY;
153}
154
Daniel Willmanna5352a02011-08-05 14:08:58 +0200155static int set_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200156{
157 char *saveptr, *lat, *lon, *height, *tstamp, *valid, *tmp;
158 struct osmo_msc_data *msc;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200159 struct bts_location *curloc, *lastloc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200160 int ret;
161 struct gsm_network *gsmnet = (struct gsm_network *)data;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200162 struct gsm_bts *bts = (struct gsm_bts *) cmd->node;
163 if (!bts) {
164 cmd->reply = "bts not found.";
165 return CTRL_CMD_ERROR;
166 }
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200167
168 tmp = talloc_strdup(cmd, cmd->value);
169 if (!tmp)
170 goto oom;
171
Daniel Willmanna5352a02011-08-05 14:08:58 +0200172 curloc = talloc_zero(tall_bsc_ctx, struct bts_location);
173 if (!curloc) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200174 talloc_free(tmp);
175 goto oom;
176 }
Daniel Willmanna5352a02011-08-05 14:08:58 +0200177 INIT_LLIST_HEAD(&curloc->list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200178
179
180 tstamp = strtok_r(tmp, ",", &saveptr);
181 valid = strtok_r(NULL, ",", &saveptr);
182 lat = strtok_r(NULL, ",", &saveptr);
183 lon = strtok_r(NULL, ",", &saveptr);
184 height = strtok_r(NULL, "\0", &saveptr);
185
Daniel Willmanna5352a02011-08-05 14:08:58 +0200186 curloc->tstamp = atol(tstamp);
187 curloc->valid = get_string_value(valid_names, valid);
188 curloc->lat = atof(lat);
189 curloc->lon = atof(lon);
190 curloc->height = atof(height);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200191 talloc_free(tmp);
192
Daniel Willmanna5352a02011-08-05 14:08:58 +0200193 lastloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200194
195 /* Add location to the end of the list */
Daniel Willmanna5352a02011-08-05 14:08:58 +0200196 llist_add(&curloc->list, &bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200197
Daniel Willmanna5352a02011-08-05 14:08:58 +0200198 ret = get_bts_loc(cmd, data);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200199
Daniel Willmanna5352a02011-08-05 14:08:58 +0200200 if (!location_equal(curloc, lastloc))
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200201 llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry)
Daniel Willmann11620112011-08-19 19:32:09 +0200202 generate_location_state_trap(bts, msc->msc_con);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200203
Daniel Willmanna5352a02011-08-05 14:08:58 +0200204 cleanup_locations(&bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200205
206 return ret;
207
208oom:
209 cmd->reply = "OOM";
210 return CTRL_CMD_ERROR;
211}
212
Daniel Willmanna5352a02011-08-05 14:08:58 +0200213static int verify_bts_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200214{
215 char *saveptr, *latstr, *lonstr, *heightstr, *tstampstr, *validstr, *tmp;
216 time_t tstamp;
217 int valid;
218 double lat, lon, height;
219
220 tmp = talloc_strdup(cmd, value);
221 if (!tmp)
222 return 1;
223
224 tstampstr = strtok_r(tmp, ",", &saveptr);
225 validstr = strtok_r(NULL, ",", &saveptr);
226 latstr = strtok_r(NULL, ",", &saveptr);
227 lonstr = strtok_r(NULL, ",", &saveptr);
228 heightstr = strtok_r(NULL, "\0", &saveptr);
229
230 if ((tstampstr == NULL) || (validstr == NULL) || (latstr == NULL) ||
231 (lonstr == NULL) || (heightstr == NULL))
232 goto err;
233
234 tstamp = atol(tstampstr);
235 valid = get_string_value(valid_names, validstr);
236 lat = atof(latstr);
237 lon = atof(lonstr);
238 height = atof(heightstr);
239 talloc_free(tmp);
240
Daniel Willmanna5352a02011-08-05 14:08:58 +0200241 if (((tstamp == 0) && (valid != BTS_LOC_FIX_INVALID)) || (lat < -90) || (lat > 90) ||
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200242 (lon < -180) || (lon > 180) || (valid < 0)) {
243 goto err;
244 }
245
246 return 0;
247err:
248 cmd->reply = talloc_strdup(cmd, "The format is <unixtime>,(invalid|fix2d|fix3d),<lat>,<lon>,<height>");
249 return 1;
250}
251
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200252CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
253static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
254{
255 cmd->reply = "get only works for the individual trx properties.";
256 return CTRL_CMD_ERROR;
257}
258
259static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
260{
261 int locked = atoi(cmd->value);
262 struct gsm_network *net = cmd->node;
263 struct gsm_bts *bts;
264 if (!net) {
265 cmd->reply = "net not found.";
266 return CTRL_CMD_ERROR;
267 }
268
269 llist_for_each_entry(bts, &net->bts_list, list) {
270 struct gsm_bts_trx *trx;
271 llist_for_each_entry(trx, &bts->trx_list, list) {
272 gsm_trx_lock_rf(trx, locked);
273 }
274 }
275
276 cmd->reply = talloc_asprintf(cmd, "%u", locked);
277 if (!cmd->reply) {
278 cmd->reply = "OOM.";
279 return CTRL_CMD_ERROR;
280 }
281
282 return CTRL_CMD_REPLY;
283}
284
285static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
286{
287 int locked = atoi(cmd->value);
288
289 if ((locked != 0) && (locked != 1))
290 return 1;
291
292 return 0;
293}
294
Daniel Willmann5e95f452011-08-05 12:22:35 +0200295int bsc_ctrl_cmds_install()
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200296{
Daniel Willmann5e95f452011-08-05 12:22:35 +0200297 int rc;
298
Daniel Willmanna5352a02011-08-05 14:08:58 +0200299 rc = ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_loc);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200300 if (rc)
301 goto end;
302 rc = ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200303end:
304 return rc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200305}