blob: d6a56917730c13197f2266bcf74c698e96ecdc41 [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 Willmann5ff06af2011-08-05 12:20:58 +020056static const struct value_string valid_names[] = {
Daniel Willmanna5352a02011-08-05 14:08:58 +020057 { BTS_LOC_FIX_INVALID, "invalid" },
58 { BTS_LOC_FIX_2D, "fix2d" },
59 { BTS_LOC_FIX_3D, "fix3d" },
Daniel Willmann5ff06af2011-08-05 12:20:58 +020060 { 0, NULL }
61};
62
Daniel Willmanna5352a02011-08-05 14:08:58 +020063static int location_equal(struct bts_location *a, struct bts_location *b)
Daniel Willmann5ff06af2011-08-05 12:20:58 +020064{
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 Willmanna5352a02011-08-05 14:08:58 +020069static void cleanup_locations(struct llist_head *locations)
Daniel Willmann5ff06af2011-08-05 12:20:58 +020070{
Daniel Willmanna5352a02011-08-05 14:08:58 +020071 struct bts_location *myloc, *tmp;
Daniel Willmann5ff06af2011-08-05 12:20:58 +020072 int invalpos = 0, i = 0;
73
74 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
Daniel Willmanna5352a02011-08-05 14:08:58 +020075 llist_for_each_entry_safe(myloc, tmp, locations, list) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +020076 i++;
77 if (i > 3) {
78 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
79 llist_del(&myloc->list);
80 talloc_free(myloc);
Daniel Willmanna5352a02011-08-05 14:08:58 +020081 } else if (myloc->valid == BTS_LOC_FIX_INVALID) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +020082 /* 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 Willmanna5352a02011-08-05 14:08:58 +020098CTRL_CMD_DEFINE(bts_loc, "location");
99static int get_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200100{
Daniel Willmanna5352a02011-08-05 14:08:58 +0200101 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 Willmann5ff06af2011-08-05 12:20:58 +0200107
Daniel Willmanna5352a02011-08-05 14:08:58 +0200108 if (llist_empty(&bts->loc_list)) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200109 cmd->reply = talloc_asprintf(cmd, "0,invalid,0,0,0");
110 return CTRL_CMD_REPLY;
111 } else {
Daniel Willmanna5352a02011-08-05 14:08:58 +0200112 curloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200113 }
114
Daniel Willmanna5352a02011-08-05 14:08:58 +0200115 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 Willmann5ff06af2011-08-05 12:20:58 +0200117 if (!cmd->reply) {
118 cmd->reply = "OOM";
119 return CTRL_CMD_ERROR;
120 }
121
122 return CTRL_CMD_REPLY;
123}
124
Daniel Willmanna5352a02011-08-05 14:08:58 +0200125static int set_bts_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200126{
127 char *saveptr, *lat, *lon, *height, *tstamp, *valid, *tmp;
128 struct osmo_msc_data *msc;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200129 struct bts_location *curloc, *lastloc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200130 int ret;
131 struct gsm_network *gsmnet = (struct gsm_network *)data;
Daniel Willmanna5352a02011-08-05 14:08:58 +0200132 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
138 tmp = talloc_strdup(cmd, cmd->value);
139 if (!tmp)
140 goto oom;
141
Daniel Willmanna5352a02011-08-05 14:08:58 +0200142 curloc = talloc_zero(tall_bsc_ctx, struct bts_location);
143 if (!curloc) {
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200144 talloc_free(tmp);
145 goto oom;
146 }
Daniel Willmanna5352a02011-08-05 14:08:58 +0200147 INIT_LLIST_HEAD(&curloc->list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200148
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 Willmanna5352a02011-08-05 14:08:58 +0200156 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 Willmann5ff06af2011-08-05 12:20:58 +0200161 talloc_free(tmp);
162
Daniel Willmanna5352a02011-08-05 14:08:58 +0200163 lastloc = llist_entry(bts->loc_list.next, struct bts_location, list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200164
165 /* Add location to the end of the list */
Daniel Willmanna5352a02011-08-05 14:08:58 +0200166 llist_add(&curloc->list, &bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200167
Daniel Willmanna5352a02011-08-05 14:08:58 +0200168 ret = get_bts_loc(cmd, data);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200169
Daniel Willmanna5352a02011-08-05 14:08:58 +0200170 if (!location_equal(curloc, lastloc))
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200171 llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry)
172 osmo_bsc_send_trap(cmd, msc->msc_con);
173
Daniel Willmanna5352a02011-08-05 14:08:58 +0200174 cleanup_locations(&bts->loc_list);
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200175
176 return ret;
177
178oom:
179 cmd->reply = "OOM";
180 return CTRL_CMD_ERROR;
181}
182
Daniel Willmanna5352a02011-08-05 14:08:58 +0200183static int verify_bts_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200184{
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 Willmanna5352a02011-08-05 14:08:58 +0200211 if (((tstamp == 0) && (valid != BTS_LOC_FIX_INVALID)) || (lat < -90) || (lat > 90) ||
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200212 (lon < -180) || (lon > 180) || (valid < 0)) {
213 goto err;
214 }
215
216 return 0;
217err:
218 cmd->reply = talloc_strdup(cmd, "The format is <unixtime>,(invalid|fix2d|fix3d),<lat>,<lon>,<height>");
219 return 1;
220}
221
222CTRL_CMD_DEFINE(trx_rf_lock, "rf_locked");
223static int get_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
224{
225 struct gsm_bts_trx *trx = cmd->node;
226 if (!trx) {
227 cmd->reply = "trx not found.";
228 return CTRL_CMD_ERROR;
229 }
230
231 cmd->reply = talloc_asprintf(cmd, "%u", trx->mo.nm_state.administrative == NM_STATE_LOCKED ? 1 : 0);
232 return CTRL_CMD_REPLY;
233}
234
235static int set_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
236{
237 int locked = atoi(cmd->value);
238 struct gsm_bts_trx *trx = cmd->node;
239 if (!trx) {
240 cmd->reply = "trx not found.";
241 return CTRL_CMD_ERROR;
242 }
243
244 gsm_trx_lock_rf(trx, locked);
245
246 return get_trx_rf_lock(cmd, data);
247}
248
249static int verify_trx_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
250{
251 int locked = atoi(cmd->value);
252
253 if ((locked != 0) && (locked != 1))
254 return 1;
255
256 return 0;
257}
258
259CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
260static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
261{
262 cmd->reply = "get only works for the individual trx properties.";
263 return CTRL_CMD_ERROR;
264}
265
266static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
267{
268 int locked = atoi(cmd->value);
269 struct gsm_network *net = cmd->node;
270 struct gsm_bts *bts;
271 if (!net) {
272 cmd->reply = "net not found.";
273 return CTRL_CMD_ERROR;
274 }
275
276 llist_for_each_entry(bts, &net->bts_list, list) {
277 struct gsm_bts_trx *trx;
278 llist_for_each_entry(trx, &bts->trx_list, list) {
279 gsm_trx_lock_rf(trx, locked);
280 }
281 }
282
283 cmd->reply = talloc_asprintf(cmd, "%u", locked);
284 if (!cmd->reply) {
285 cmd->reply = "OOM.";
286 return CTRL_CMD_ERROR;
287 }
288
289 return CTRL_CMD_REPLY;
290}
291
292static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
293{
294 int locked = atoi(cmd->value);
295
296 if ((locked != 0) && (locked != 1))
297 return 1;
298
299 return 0;
300}
301
Daniel Willmann5e95f452011-08-05 12:22:35 +0200302int bsc_ctrl_cmds_install()
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200303{
Daniel Willmann5e95f452011-08-05 12:22:35 +0200304 int rc;
305
Daniel Willmanna5352a02011-08-05 14:08:58 +0200306 rc = ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_loc);
Daniel Willmann5e95f452011-08-05 12:22:35 +0200307 if (rc)
308 goto end;
309 rc = ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock);
310 if (rc)
311 goto end;
312 rc = ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_rf_lock);
313end:
314 return rc;
Daniel Willmann5ff06af2011-08-05 12:20:58 +0200315}