Harald Welte | b9fa49a | 2012-11-15 12:29:15 +0100 | [diff] [blame] | 1 | /* 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 Hauke | e9914e6 | 2015-11-05 21:03:36 +0100 | [diff] [blame] | 27 | #include <time.h> |
Harald Welte | b9fa49a | 2012-11-15 12:29:15 +0100 | [diff] [blame] | 28 | |
| 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 | |
| 43 | static struct osmo_fd udp_ofd; |
| 44 | static struct meas_db_state *db; |
| 45 | |
| 46 | static 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 | |
| 70 | static 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 | |
| 88 | int main(int argc, char **argv) |
| 89 | { |
| 90 | char *db_fname; |
| 91 | int rc; |
| 92 | |
| 93 | if (argc < 2) { |
| 94 | fprintf(stderr, "You have to specify the database file name\n"); |
| 95 | exit(2); |
| 96 | } |
| 97 | |
| 98 | db_fname = argv[1]; |
| 99 | |
| 100 | udp_ofd.cb = udp_fd_cb; |
| 101 | rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM, |
| 102 | IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND); |
| 103 | if (rc < 0) { |
| 104 | fprintf(stderr, "Unable to create UDP listen socket\n"); |
| 105 | exit(1); |
| 106 | } |
| 107 | |
| 108 | db = meas_db_open(NULL, db_fname); |
| 109 | if (!db) { |
| 110 | fprintf(stderr, "Unable to open database\n"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | /* FIXME: timer-based BEGIN/COMMIT */ |
| 115 | |
| 116 | while (1) { |
| 117 | osmo_select_main(0); |
| 118 | }; |
| 119 | |
| 120 | meas_db_close(db); |
| 121 | |
| 122 | exit(0); |
| 123 | } |
| 124 | |