blob: e4ed0f1d8b03f3561036deb9a0ce9b4dd87e8e70 [file] [log] [blame]
Harald Weltedfbc42c2012-11-15 12:29:15 +01001/* liesten to meas_feed on UDP and write it to sqlite3 database */
2
3/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <string.h>
23#include <errno.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27
28#include <netinet/in.h>
29
30#include <osmocom/core/socket.h>
31#include <osmocom/core/utils.h>
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/select.h>
34#include <osmocom/core/talloc.h>
35
36#include <osmocom/gsm/gsm_utils.h>
37
38#include <openbsc/meas_feed.h>
39
40#include "meas_db.h"
41
42static struct osmo_fd udp_ofd;
43static struct meas_db_state *db;
44
45static int handle_msg(struct msgb *msg)
46{
47 struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg);
48 struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg);
49 const char *scenario;
50 time_t now = time(NULL);
51
52 if (mfh->version != MEAS_FEED_VERSION)
53 return -EINVAL;
54
55 if (mfh->msg_type != MEAS_FEED_MEAS)
56 return -EINVAL;
57
58 if (strlen(mfm->scenario))
59 scenario = mfm->scenario;
60 else
61 scenario = NULL;
62
63 meas_db_insert(db, mfm->imsi, mfm->name, now,
64 scenario, &mfm->mr);
65
66 return 0;
67}
68
69static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
70{
71 int rc;
72
73 if (what & BSC_FD_READ) {
74 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
75
76 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
77 if (rc < 0)
78 return rc;
79 msgb_put(msg, rc);
80 handle_msg(msg);
81 msgb_free(msg);
82 }
83
84 return 0;
85}
86
87int main(int argc, char **argv)
88{
89 char *db_fname;
90 int rc;
91
92 if (argc < 2) {
93 fprintf(stderr, "You have to specify the database file name\n");
94 exit(2);
95 }
96
97 db_fname = argv[1];
98
99 udp_ofd.cb = udp_fd_cb;
100 rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM,
101 IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
102 if (rc < 0) {
103 fprintf(stderr, "Unable to create UDP listen socket\n");
104 exit(1);
105 }
106
107 db = meas_db_open(NULL, db_fname);
108 if (!db) {
109 fprintf(stderr, "Unable to open database\n");
110 exit(1);
111 }
112
113 /* FIXME: timer-based BEGIN/COMMIT */
114
115 while (1) {
116 osmo_select_main(0);
117 };
118
119 meas_db_close(db);
120
121 exit(0);
122}
123