From 3118191f59c066ec47e5e34c5ce28677b4351399 Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Thu, 30 Jun 2011 11:03:07 +0200 Subject: osmo_bsc: Track the last three locations. --- openbsc/src/osmo-bsc/osmo_bsc_main.c | 54 +++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'openbsc') diff --git a/openbsc/src/osmo-bsc/osmo_bsc_main.c b/openbsc/src/osmo-bsc/osmo_bsc_main.c index 4ae37a1a9..69622bbe8 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_main.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_main.c @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -171,6 +172,7 @@ static void signal_handler(int signal) } struct location { + struct llist_head list; unsigned long age; int valid; double lat; @@ -178,12 +180,38 @@ struct location { double height; }; -static struct location myloc; +static LLIST_HEAD(locations); + +void cleanup_locations() +{ + struct location *myloc, *tmp; + int i = 0; + + LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n"); + llist_for_each_entry_safe(myloc, tmp, &locations, list) { + i++; + if (i > 3) { + LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n"); + llist_del(&myloc->list); + talloc_free(myloc); + } + } + LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i); +} CTRL_CMD_DEFINE(net_loc, "location"); int get_net_loc(struct ctrl_cmd *cmd, void *data) { - cmd->reply = talloc_asprintf(cmd, "%lu,%i,%f,%f,%f", myloc.age, myloc.valid, myloc.lat, myloc.lon, myloc.height); + struct location *myloc; + + if (llist_empty(&locations)) { + cmd->reply = talloc_asprintf(cmd, "0,0,0,0,0"); + return CTRL_CMD_REPLY; + } else { + myloc = llist_entry(locations.next, struct location, list); + } + + cmd->reply = talloc_asprintf(cmd, "%lu,%i,%f,%f,%f", myloc->age, myloc->valid, myloc->lat, myloc->lon, myloc->height); if (!cmd->reply) { cmd->reply = "OOM"; return CTRL_CMD_ERROR; @@ -195,11 +223,19 @@ int get_net_loc(struct ctrl_cmd *cmd, void *data) int set_net_loc(struct ctrl_cmd *cmd, void *data) { char *saveptr, *lat, *lon, *height, *age, *valid, *tmp; + struct location *myloc; tmp = talloc_strdup(cmd, cmd->value); if (!tmp) goto oom; + myloc = talloc_zero(tall_bsc_ctx, struct location); + if (!myloc) { + talloc_free(tmp); + goto oom; + } + INIT_LLIST_HEAD(&myloc->list); + age = strtok_r(tmp, ",", &saveptr); valid = strtok_r(NULL, ",", &saveptr); @@ -207,13 +243,17 @@ int set_net_loc(struct ctrl_cmd *cmd, void *data) lon = strtok_r(NULL, ",", &saveptr); height = strtok_r(NULL, "\0", &saveptr); - myloc.age = atol(age); - myloc.valid = atoi(valid); - myloc.lat = atof(lat); - myloc.lon = atof(lon); - myloc.height = atof(height); + myloc->age = atol(age); + myloc->valid = atoi(valid); + myloc->lat = atof(lat); + myloc->lon = atof(lon); + myloc->height = atof(height); talloc_free(tmp); + /* Add location to the end of the list */ + llist_add(&myloc->list, &locations); + cleanup_locations(); + return get_net_loc(cmd, data); oom: cmd->reply = "OOM"; -- cgit v1.2.3