Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 1 | /* (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 | |
| 35 | void 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 Willmann | 1162011 | 2011-08-19 19:32:09 +0200 | [diff] [blame^] | 56 | static int get_bts_loc(struct ctrl_cmd *cmd, void *data); |
| 57 | |
| 58 | static void generate_location_state_trap(struct gsm_bts *bts, struct bsc_msc_connection *msc_con) |
| 59 | { |
| 60 | struct ctrl_cmd *cmd; |
| 61 | |
| 62 | cmd = ctrl_cmd_create(msc_con, CTRL_TYPE_TRAP); |
| 63 | if (!cmd) { |
| 64 | LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP command.\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | cmd->id = "0"; |
| 69 | cmd->variable = talloc_asprintf(cmd, "net.bts.%i.location-state", bts->nr); |
| 70 | |
| 71 | /* Prepare the location reply */ |
| 72 | cmd->node = bts; |
| 73 | get_bts_loc(cmd, NULL); |
| 74 | |
| 75 | osmo_bsc_send_trap(cmd, msc_con); |
| 76 | talloc_free(cmd); |
| 77 | } |
| 78 | |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 79 | static const struct value_string valid_names[] = { |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 80 | { BTS_LOC_FIX_INVALID, "invalid" }, |
| 81 | { BTS_LOC_FIX_2D, "fix2d" }, |
| 82 | { BTS_LOC_FIX_3D, "fix3d" }, |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 83 | { 0, NULL } |
| 84 | }; |
| 85 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 86 | static int location_equal(struct bts_location *a, struct bts_location *b) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 87 | { |
| 88 | return ((a->tstamp == b->tstamp) && (a->valid == b->valid) && (a->lat == b->lat) && |
| 89 | (a->lon == b->lon) && (a->height == b->height)); |
| 90 | } |
| 91 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 92 | static void cleanup_locations(struct llist_head *locations) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 93 | { |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 94 | struct bts_location *myloc, *tmp; |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 95 | int invalpos = 0, i = 0; |
| 96 | |
| 97 | LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n"); |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 98 | llist_for_each_entry_safe(myloc, tmp, locations, list) { |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 99 | i++; |
| 100 | if (i > 3) { |
| 101 | LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n"); |
| 102 | llist_del(&myloc->list); |
| 103 | talloc_free(myloc); |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 104 | } else if (myloc->valid == BTS_LOC_FIX_INVALID) { |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 105 | /* Only capture the newest of subsequent invalid positions */ |
| 106 | invalpos++; |
| 107 | if (invalpos > 1) { |
| 108 | LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n"); |
| 109 | invalpos--; |
| 110 | i--; |
| 111 | llist_del(&myloc->list); |
| 112 | talloc_free(myloc); |
| 113 | } |
| 114 | } else { |
| 115 | invalpos = 0; |
| 116 | } |
| 117 | } |
| 118 | LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i); |
| 119 | } |
| 120 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 121 | CTRL_CMD_DEFINE(bts_loc, "location"); |
| 122 | static int get_bts_loc(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 123 | { |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 124 | struct bts_location *curloc; |
| 125 | struct gsm_bts *bts = (struct gsm_bts *) cmd->node; |
| 126 | if (!bts) { |
| 127 | cmd->reply = "bts not found."; |
| 128 | return CTRL_CMD_ERROR; |
| 129 | } |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 130 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 131 | if (llist_empty(&bts->loc_list)) { |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 132 | cmd->reply = talloc_asprintf(cmd, "0,invalid,0,0,0"); |
| 133 | return CTRL_CMD_REPLY; |
| 134 | } else { |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 135 | curloc = llist_entry(bts->loc_list.next, struct bts_location, list); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 138 | cmd->reply = talloc_asprintf(cmd, "%lu,%s,%f,%f,%f", curloc->tstamp, |
| 139 | get_value_string(valid_names, curloc->valid), curloc->lat, curloc->lon, curloc->height); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 140 | if (!cmd->reply) { |
| 141 | cmd->reply = "OOM"; |
| 142 | return CTRL_CMD_ERROR; |
| 143 | } |
| 144 | |
| 145 | return CTRL_CMD_REPLY; |
| 146 | } |
| 147 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 148 | static int set_bts_loc(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 149 | { |
| 150 | char *saveptr, *lat, *lon, *height, *tstamp, *valid, *tmp; |
| 151 | struct osmo_msc_data *msc; |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 152 | struct bts_location *curloc, *lastloc; |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 153 | int ret; |
| 154 | struct gsm_network *gsmnet = (struct gsm_network *)data; |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 155 | struct gsm_bts *bts = (struct gsm_bts *) cmd->node; |
| 156 | if (!bts) { |
| 157 | cmd->reply = "bts not found."; |
| 158 | return CTRL_CMD_ERROR; |
| 159 | } |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 160 | |
| 161 | tmp = talloc_strdup(cmd, cmd->value); |
| 162 | if (!tmp) |
| 163 | goto oom; |
| 164 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 165 | curloc = talloc_zero(tall_bsc_ctx, struct bts_location); |
| 166 | if (!curloc) { |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 167 | talloc_free(tmp); |
| 168 | goto oom; |
| 169 | } |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 170 | INIT_LLIST_HEAD(&curloc->list); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 171 | |
| 172 | |
| 173 | tstamp = strtok_r(tmp, ",", &saveptr); |
| 174 | valid = strtok_r(NULL, ",", &saveptr); |
| 175 | lat = strtok_r(NULL, ",", &saveptr); |
| 176 | lon = strtok_r(NULL, ",", &saveptr); |
| 177 | height = strtok_r(NULL, "\0", &saveptr); |
| 178 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 179 | curloc->tstamp = atol(tstamp); |
| 180 | curloc->valid = get_string_value(valid_names, valid); |
| 181 | curloc->lat = atof(lat); |
| 182 | curloc->lon = atof(lon); |
| 183 | curloc->height = atof(height); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 184 | talloc_free(tmp); |
| 185 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 186 | lastloc = llist_entry(bts->loc_list.next, struct bts_location, list); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 187 | |
| 188 | /* Add location to the end of the list */ |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 189 | llist_add(&curloc->list, &bts->loc_list); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 190 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 191 | ret = get_bts_loc(cmd, data); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 192 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 193 | if (!location_equal(curloc, lastloc)) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 194 | llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry) |
Daniel Willmann | 1162011 | 2011-08-19 19:32:09 +0200 | [diff] [blame^] | 195 | generate_location_state_trap(bts, msc->msc_con); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 196 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 197 | cleanup_locations(&bts->loc_list); |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 198 | |
| 199 | return ret; |
| 200 | |
| 201 | oom: |
| 202 | cmd->reply = "OOM"; |
| 203 | return CTRL_CMD_ERROR; |
| 204 | } |
| 205 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 206 | static int verify_bts_loc(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 207 | { |
| 208 | char *saveptr, *latstr, *lonstr, *heightstr, *tstampstr, *validstr, *tmp; |
| 209 | time_t tstamp; |
| 210 | int valid; |
| 211 | double lat, lon, height; |
| 212 | |
| 213 | tmp = talloc_strdup(cmd, value); |
| 214 | if (!tmp) |
| 215 | return 1; |
| 216 | |
| 217 | tstampstr = strtok_r(tmp, ",", &saveptr); |
| 218 | validstr = strtok_r(NULL, ",", &saveptr); |
| 219 | latstr = strtok_r(NULL, ",", &saveptr); |
| 220 | lonstr = strtok_r(NULL, ",", &saveptr); |
| 221 | heightstr = strtok_r(NULL, "\0", &saveptr); |
| 222 | |
| 223 | if ((tstampstr == NULL) || (validstr == NULL) || (latstr == NULL) || |
| 224 | (lonstr == NULL) || (heightstr == NULL)) |
| 225 | goto err; |
| 226 | |
| 227 | tstamp = atol(tstampstr); |
| 228 | valid = get_string_value(valid_names, validstr); |
| 229 | lat = atof(latstr); |
| 230 | lon = atof(lonstr); |
| 231 | height = atof(heightstr); |
| 232 | talloc_free(tmp); |
| 233 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 234 | if (((tstamp == 0) && (valid != BTS_LOC_FIX_INVALID)) || (lat < -90) || (lat > 90) || |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 235 | (lon < -180) || (lon > 180) || (valid < 0)) { |
| 236 | goto err; |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | err: |
| 241 | cmd->reply = talloc_strdup(cmd, "The format is <unixtime>,(invalid|fix2d|fix3d),<lat>,<lon>,<height>"); |
| 242 | return 1; |
| 243 | } |
| 244 | |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 245 | CTRL_CMD_DEFINE(net_rf_lock, "rf_locked"); |
| 246 | static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data) |
| 247 | { |
| 248 | cmd->reply = "get only works for the individual trx properties."; |
| 249 | return CTRL_CMD_ERROR; |
| 250 | } |
| 251 | |
| 252 | static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data) |
| 253 | { |
| 254 | int locked = atoi(cmd->value); |
| 255 | struct gsm_network *net = cmd->node; |
| 256 | struct gsm_bts *bts; |
| 257 | if (!net) { |
| 258 | cmd->reply = "net not found."; |
| 259 | return CTRL_CMD_ERROR; |
| 260 | } |
| 261 | |
| 262 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 263 | struct gsm_bts_trx *trx; |
| 264 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 265 | gsm_trx_lock_rf(trx, locked); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | cmd->reply = talloc_asprintf(cmd, "%u", locked); |
| 270 | if (!cmd->reply) { |
| 271 | cmd->reply = "OOM."; |
| 272 | return CTRL_CMD_ERROR; |
| 273 | } |
| 274 | |
| 275 | return CTRL_CMD_REPLY; |
| 276 | } |
| 277 | |
| 278 | static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data) |
| 279 | { |
| 280 | int locked = atoi(cmd->value); |
| 281 | |
| 282 | if ((locked != 0) && (locked != 1)) |
| 283 | return 1; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Daniel Willmann | 5e95f45 | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 288 | int bsc_ctrl_cmds_install() |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 289 | { |
Daniel Willmann | 5e95f45 | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 290 | int rc; |
| 291 | |
Daniel Willmann | a5352a0 | 2011-08-05 14:08:58 +0200 | [diff] [blame] | 292 | rc = ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_loc); |
Daniel Willmann | 5e95f45 | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 293 | if (rc) |
| 294 | goto end; |
| 295 | rc = ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock); |
Daniel Willmann | 5e95f45 | 2011-08-05 12:22:35 +0200 | [diff] [blame] | 296 | end: |
| 297 | return rc; |
Daniel Willmann | 5ff06af | 2011-08-05 12:20:58 +0200 | [diff] [blame] | 298 | } |