Neels Hofmeyr | 7299215 | 2020-09-19 02:36:08 +0200 | [diff] [blame] | 1 | /* OsmoSMLC cell locations configuration */ |
| 2 | /* |
| 3 | * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <limits.h> |
| 27 | #include <inttypes.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 32 | #include <osmocom/gsm/gsm0808_utils.h> |
| 33 | #include <osmocom/gsm/gad.h> |
| 34 | #include <osmocom/smlc/smlc_data.h> |
| 35 | #include <osmocom/smlc/smlc_vty.h> |
| 36 | #include <osmocom/smlc/cell_locations.h> |
| 37 | |
| 38 | static uint32_t ta_to_m(uint8_t ta) |
| 39 | { |
| 40 | return ((uint32_t)ta) * 550; |
| 41 | } |
| 42 | |
| 43 | static struct cell_location *cell_location_find(const struct gsm0808_cell_id *cell_id) |
| 44 | { |
| 45 | struct cell_location *cell_location; |
| 46 | llist_for_each_entry(cell_location, &g_smlc->cell_locations, entry) { |
| 47 | if (gsm0808_cell_ids_match(&cell_location->cell_id, cell_id, true)) |
| 48 | return cell_location; |
| 49 | } |
| 50 | llist_for_each_entry(cell_location, &g_smlc->cell_locations, entry) { |
| 51 | if (gsm0808_cell_ids_match(&cell_location->cell_id, cell_id, false)) |
| 52 | return cell_location; |
| 53 | } |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | int cell_location_from_ta(struct osmo_gad *location_estimate, |
| 58 | const struct gsm0808_cell_id *cell_id, |
| 59 | uint8_t ta) |
| 60 | { |
| 61 | const struct cell_location *cell; |
| 62 | cell = cell_location_find(cell_id); |
| 63 | if (!cell) |
| 64 | return -ENOENT; |
| 65 | |
| 66 | *location_estimate = (struct osmo_gad){ |
| 67 | .type = GAD_TYPE_ELL_POINT_UNC_CIRCLE, |
| 68 | .ell_point_unc_circle = { |
| 69 | .lat = cell->lat, |
| 70 | .lon = cell->lon, |
| 71 | .unc = osmo_gad_dec_unc(osmo_gad_enc_unc(ta_to_m(ta) * 1000)), |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static struct cell_location *cell_location_find_or_create(const struct gsm0808_cell_id *cell_id) |
| 79 | { |
| 80 | struct cell_location *cell_location = cell_location_find(cell_id); |
| 81 | if (!cell_location) { |
| 82 | cell_location = talloc_zero(g_smlc, struct cell_location); |
| 83 | OSMO_ASSERT(cell_location); |
| 84 | cell_location->cell_id = *cell_id; |
| 85 | llist_add_tail(&cell_location->entry, &g_smlc->cell_locations); |
| 86 | } |
| 87 | return cell_location; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | static const struct cell_location *cell_location_set(const struct gsm0808_cell_id *cell_id, int32_t lat, int32_t lon) |
| 92 | { |
| 93 | struct cell_location *cell_location = cell_location_find_or_create(cell_id); |
| 94 | cell_location->lat = lat; |
| 95 | cell_location->lon = lon; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int cell_location_remove(const struct gsm0808_cell_id *cell_id) |
| 100 | { |
| 101 | struct cell_location *cell_location = cell_location_find(cell_id); |
| 102 | if (!cell_location) |
| 103 | return -ENOENT; |
| 104 | llist_del(&cell_location->entry); |
| 105 | talloc_free(cell_location); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | #define LAC_CI_PARAMS "lac-ci <0-65535> <0-65535>" |
| 110 | #define LAC_CI_DOC "Cell location by LAC and CI\n" "LAC\n" "CI\n" |
| 111 | |
| 112 | #define CGI_PARAMS "cgi <0-999> <0-999> <0-65535> <0-65535>" |
| 113 | #define CGI_DOC "Cell location by Cell-Global ID\n" "MCC\n" "MNC\n" "LAC\n" "CI\n" |
| 114 | |
| 115 | #define LAT_LON_PARAMS "lat LATITUDE lon LONGITUDE" |
| 116 | #define LAT_LON_DOC "Global latitute coordinate\n" "Latitude floating-point number, -90.0 (S) to 90.0 (N)\n" \ |
| 117 | "Global longitude coordinate\n" "Longitude as floating-point number, -180.0 (W) to 180.0 (E)\n" |
| 118 | |
| 119 | static int vty_parse_lac_ci(struct vty *vty, struct gsm0808_cell_id *dst, const char **argv) |
| 120 | { |
| 121 | *dst = (struct gsm0808_cell_id){ |
| 122 | .id_discr = CELL_IDENT_LAC_AND_CI, |
| 123 | .id.lac_and_ci = { |
| 124 | .lac = atoi(argv[0]), |
| 125 | .ci = atoi(argv[1]), |
| 126 | }, |
| 127 | }; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int vty_parse_cgi(struct vty *vty, struct gsm0808_cell_id *dst, const char **argv) |
| 132 | { |
| 133 | *dst = (struct gsm0808_cell_id){ |
| 134 | .id_discr = CELL_IDENT_WHOLE_GLOBAL, |
| 135 | }; |
| 136 | struct osmo_cell_global_id *cgi = &dst->id.global; |
| 137 | const char *mcc = argv[0]; |
| 138 | const char *mnc = argv[1]; |
| 139 | const char *lac = argv[2]; |
| 140 | const char *ci = argv[3]; |
| 141 | |
| 142 | if (osmo_mcc_from_str(mcc, &cgi->lai.plmn.mcc)) { |
| 143 | vty_out(vty, "%% Error decoding MCC: %s%s", mcc, VTY_NEWLINE); |
| 144 | return -EINVAL; |
| 145 | } |
| 146 | |
| 147 | if (osmo_mnc_from_str(mnc, &cgi->lai.plmn.mnc, &cgi->lai.plmn.mnc_3_digits)) { |
| 148 | vty_out(vty, "%% Error decoding MNC: %s%s", mnc, VTY_NEWLINE); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | cgi->lai.lac = atoi(lac); |
| 153 | cgi->cell_identity = atoi(ci); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int vty_parse_location(struct vty *vty, const struct gsm0808_cell_id *cell_id, const char **argv) |
| 158 | { |
| 159 | const char *lat_str = argv[0]; |
| 160 | const char *lon_str = argv[1]; |
| 161 | int64_t val; |
| 162 | int32_t lat, lon; |
| 163 | |
| 164 | if (osmo_float_str_to_int(&val, lat_str, 6) |
| 165 | || val < -90000000 || val > 90000000) { |
| 166 | vty_out(vty, "%% Invalid latitude: '%s'%s", lat_str, VTY_NEWLINE); |
| 167 | return CMD_WARNING; |
| 168 | } |
| 169 | lat = val; |
| 170 | |
| 171 | if (osmo_float_str_to_int(&val, lon_str, 6) |
| 172 | || val < -180000000 || val > 180000000) { |
| 173 | vty_out(vty, "%% Invalid longitude: '%s'%s", lon_str, VTY_NEWLINE); |
| 174 | return CMD_WARNING; |
| 175 | } |
| 176 | lon = val; |
| 177 | |
| 178 | if (cell_location_set(cell_id, lat, lon)) { |
| 179 | vty_out(vty, "%% Failed to add cell location%s", VTY_NEWLINE); |
| 180 | return CMD_WARNING; |
| 181 | } |
| 182 | return CMD_SUCCESS; |
| 183 | } |
| 184 | |
| 185 | DEFUN(cfg_cells, cfg_cells_cmd, |
| 186 | "cells", |
| 187 | "Configure cell locations\n") |
| 188 | { |
| 189 | vty->node = CELLS_NODE; |
| 190 | return CMD_SUCCESS; |
| 191 | } |
| 192 | |
| 193 | DEFUN(cfg_cells_lac_ci, cfg_cells_lac_ci_cmd, |
| 194 | LAC_CI_PARAMS " " LAT_LON_PARAMS, |
| 195 | LAC_CI_DOC LAT_LON_DOC) |
| 196 | { |
| 197 | struct gsm0808_cell_id cell_id; |
| 198 | |
| 199 | if (vty_parse_lac_ci(vty, &cell_id, argv)) |
| 200 | return CMD_WARNING; |
| 201 | |
| 202 | return vty_parse_location(vty, &cell_id, argv + 2); |
| 203 | } |
| 204 | |
| 205 | DEFUN(cfg_cells_no_lac_ci, cfg_cells_no_lac_ci_cmd, |
| 206 | "no " LAC_CI_PARAMS, |
| 207 | NO_STR "Remove " LAC_CI_DOC) |
| 208 | { |
| 209 | struct gsm0808_cell_id cell_id; |
| 210 | |
| 211 | if (vty_parse_lac_ci(vty, &cell_id, argv)) |
| 212 | return CMD_WARNING; |
| 213 | if (cell_location_remove(&cell_id)) { |
| 214 | vty_out(vty, "%% cannot remove, no such entry%s", VTY_NEWLINE); |
| 215 | return CMD_WARNING; |
| 216 | } |
| 217 | return CMD_SUCCESS; |
| 218 | } |
| 219 | |
| 220 | DEFUN(cfg_cells_cgi, cfg_cells_cgi_cmd, |
| 221 | CGI_PARAMS " " LAT_LON_PARAMS, |
| 222 | CGI_DOC LAT_LON_DOC) |
| 223 | { |
| 224 | struct gsm0808_cell_id cell_id; |
| 225 | |
| 226 | if (vty_parse_cgi(vty, &cell_id, argv)) |
| 227 | return CMD_WARNING; |
| 228 | |
| 229 | return vty_parse_location(vty, &cell_id, argv + 4); |
| 230 | } |
| 231 | |
| 232 | DEFUN(cfg_cells_no_cgi, cfg_cells_no_cgi_cmd, |
| 233 | "no " CGI_PARAMS, |
| 234 | NO_STR "Remove " CGI_DOC) |
| 235 | { |
| 236 | struct gsm0808_cell_id cell_id; |
| 237 | |
| 238 | if (vty_parse_cgi(vty, &cell_id, argv)) |
| 239 | return CMD_WARNING; |
| 240 | if (cell_location_remove(&cell_id)) { |
| 241 | vty_out(vty, "%% cannot remove, no such entry%s", VTY_NEWLINE); |
| 242 | return CMD_WARNING; |
| 243 | } |
| 244 | return CMD_SUCCESS; |
| 245 | } |
| 246 | |
| 247 | /* The above are omnidirectional cells. If we add configuration sector antennae, it would add arguments to the above, |
| 248 | * something like this: |
| 249 | * cgi 001 01 23 42 lat 23.23 lon 42.42 arc 270 30 |
| 250 | */ |
| 251 | |
| 252 | struct cmd_node cells_node = { |
| 253 | CELLS_NODE, |
| 254 | "%s(config-cells)# ", |
| 255 | 1, |
| 256 | }; |
| 257 | |
| 258 | static int config_write_cells(struct vty *vty) |
| 259 | { |
| 260 | struct cell_location *cell; |
| 261 | const struct osmo_cell_global_id *cgi; |
| 262 | |
| 263 | if (llist_empty(&g_smlc->cell_locations)) |
| 264 | return 0; |
| 265 | |
| 266 | vty_out(vty, "cells%s", VTY_NEWLINE); |
| 267 | |
| 268 | llist_for_each_entry(cell, &g_smlc->cell_locations, entry) { |
| 269 | switch (cell->cell_id.id_discr) { |
| 270 | case CELL_IDENT_LAC_AND_CI: |
| 271 | vty_out(vty, " lac-ci %u %u", cell->cell_id.id.lac_and_ci.lac, cell->cell_id.id.lac_and_ci.ci); |
| 272 | break; |
| 273 | case CELL_IDENT_WHOLE_GLOBAL: |
| 274 | cgi = &cell->cell_id.id.global; |
| 275 | vty_out(vty, " cgi %s %s %u %u", |
| 276 | osmo_mcc_name(cgi->lai.plmn.mcc), |
| 277 | osmo_mnc_name(cgi->lai.plmn.mnc, cgi->lai.plmn.mnc_3_digits), |
| 278 | cgi->lai.lac, cgi->cell_identity); |
| 279 | break; |
| 280 | default: |
| 281 | vty_out(vty, " %% [unsupported cell id type: %d]", |
| 282 | cell->cell_id.id_discr); |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | vty_out(vty, " lat %s lon %s%s", |
| 287 | osmo_int_to_float_str_c(OTC_SELECT, cell->lat, 6), |
| 288 | osmo_int_to_float_str_c(OTC_SELECT, cell->lon, 6), |
| 289 | VTY_NEWLINE); |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | DEFUN(ve_show_cells, ve_show_cells_cmd, |
| 296 | "show cells", |
| 297 | SHOW_STR "Show configured cell locations\n") |
| 298 | { |
| 299 | if (llist_empty(&g_smlc->cell_locations)) { |
| 300 | vty_out(vty, "%% No cell locations are configured%s", VTY_NEWLINE); |
| 301 | return CMD_SUCCESS; |
| 302 | } |
| 303 | config_write_cells(vty); |
| 304 | return CMD_SUCCESS; |
| 305 | } |
| 306 | |
| 307 | int cell_locations_vty_init() |
| 308 | { |
| 309 | install_element(CONFIG_NODE, &cfg_cells_cmd); |
| 310 | install_node(&cells_node, config_write_cells); |
| 311 | install_element(CELLS_NODE, &cfg_cells_lac_ci_cmd); |
| 312 | install_element(CELLS_NODE, &cfg_cells_no_lac_ci_cmd); |
| 313 | install_element(CELLS_NODE, &cfg_cells_cgi_cmd); |
| 314 | install_element(CELLS_NODE, &cfg_cells_no_cgi_cmd); |
| 315 | install_element_ve(&ve_show_cells_cmd); |
| 316 | |
| 317 | return 0; |
| 318 | } |