osmo_bsc: Track the last three locations.

This commit is contained in:
Daniel Willmann 2011-06-30 11:03:07 +02:00 committed by Holger Hans Peter Freyther
parent fa2218cbc9
commit 3118191f59
1 changed files with 47 additions and 7 deletions

View File

@ -29,6 +29,7 @@
#include <openbsc/ipaccess.h>
#include <osmocom/core/application.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/gsm/protocol/gsm_12_21.h>
@ -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";