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