blob: 5032d0c3edc3792702a9d7abdf6efa40e1811bd8 [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>
Martin Hauke9270dc82015-11-05 21:03:36 +010027#include <time.h>
Harald Weltedfbc42c2012-11-15 12:29:15 +010028
29#include <netinet/in.h>
30
31#include <osmocom/core/socket.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/select.h>
35#include <osmocom/core/talloc.h>
36
37#include <osmocom/gsm/gsm_utils.h>
38
39#include <openbsc/meas_feed.h>
40
41#include "meas_db.h"
42
43static struct osmo_fd udp_ofd;
44static struct meas_db_state *db;
45
46static int handle_msg(struct msgb *msg)
47{
48 struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg);
49 struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg);
50 const char *scenario;
51 time_t now = time(NULL);
52
53 if (mfh->version != MEAS_FEED_VERSION)
54 return -EINVAL;
55
56 if (mfh->msg_type != MEAS_FEED_MEAS)
57 return -EINVAL;
58
59 if (strlen(mfm->scenario))
60 scenario = mfm->scenario;
61 else
62 scenario = NULL;
63
64 meas_db_insert(db, mfm->imsi, mfm->name, now,
65 scenario, &mfm->mr);
66
67 return 0;
68}
69
70static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
71{
72 int rc;
73
74 if (what & BSC_FD_READ) {
75 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
76
77 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
78 if (rc < 0)
79 return rc;
80 msgb_put(msg, rc);
81 handle_msg(msg);
82 msgb_free(msg);
83 }
84
85 return 0;
86}
87
88int main(int argc, char **argv)
89{
90 char *db_fname;
91 int rc;
92
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +020093 msgb_talloc_ctx_init(NULL, 0);
94
Harald Weltedfbc42c2012-11-15 12:29:15 +010095 if (argc < 2) {
96 fprintf(stderr, "You have to specify the database file name\n");
97 exit(2);
98 }
99
100 db_fname = argv[1];
101
102 udp_ofd.cb = udp_fd_cb;
103 rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM,
104 IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
105 if (rc < 0) {
106 fprintf(stderr, "Unable to create UDP listen socket\n");
107 exit(1);
108 }
109
110 db = meas_db_open(NULL, db_fname);
111 if (!db) {
112 fprintf(stderr, "Unable to open database\n");
113 exit(1);
114 }
115
116 /* FIXME: timer-based BEGIN/COMMIT */
117
118 while (1) {
119 osmo_select_main(0);
120 };
121
122 meas_db_close(db);
123
124 exit(0);
125}
126