Harald Welte | d597317 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 1 | /* UDP-Feed of measurement reports */ |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | |
Holger Hans Peter Freyther | bbb9d97 | 2015-08-01 23:46:56 +0000 | [diff] [blame] | 5 | #include <sys/socket.h> |
| 6 | |
Harald Welte | d597317 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/socket.h> |
| 9 | #include <osmocom/core/write_queue.h> |
| 10 | #include <osmocom/core/talloc.h> |
| 11 | |
| 12 | #include <osmocom/vty/command.h> |
| 13 | #include <osmocom/vty/vty.h> |
| 14 | |
| 15 | #include <openbsc/meas_rep.h> |
| 16 | #include <openbsc/signal.h> |
| 17 | #include <openbsc/gsm_subscriber.h> |
| 18 | #include <openbsc/meas_feed.h> |
| 19 | #include <openbsc/vty.h> |
| 20 | |
| 21 | #include "meas_feed.h" |
| 22 | |
| 23 | struct meas_feed_state { |
| 24 | struct osmo_wqueue wqueue; |
| 25 | char scenario[31+1]; |
| 26 | char *dst_host; |
| 27 | uint16_t dst_port; |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | static struct meas_feed_state g_mfs; |
| 32 | |
| 33 | static int process_meas_rep(struct gsm_meas_rep *mr) |
| 34 | { |
| 35 | struct msgb *msg; |
| 36 | struct meas_feed_meas *mfm; |
| 37 | struct gsm_subscriber *subscr; |
| 38 | |
| 39 | /* ignore measurements as long as we don't know who it is */ |
| 40 | if (!mr->lchan || !mr->lchan->conn || !mr->lchan->conn->subscr) |
| 41 | return 0; |
| 42 | |
| 43 | subscr = mr->lchan->conn->subscr; |
| 44 | |
| 45 | msg = msgb_alloc(sizeof(struct meas_feed_meas), "Meas. Feed"); |
| 46 | if (!msg) |
| 47 | return 0; |
| 48 | |
| 49 | /* fill in the header */ |
| 50 | mfm = (struct meas_feed_meas *) msgb_put(msg, sizeof(*mfm)); |
| 51 | mfm->hdr.msg_type = MEAS_FEED_MEAS; |
| 52 | mfm->hdr.version = MEAS_FEED_VERSION; |
| 53 | |
| 54 | /* fill in MEAS_FEED_MEAS specific header */ |
| 55 | strncpy(mfm->imsi, subscr->imsi, sizeof(mfm->imsi)-1); |
| 56 | mfm->imsi[sizeof(mfm->imsi)-1] = '\0'; |
| 57 | strncpy(mfm->name, subscr->name, sizeof(mfm->name)-1); |
| 58 | mfm->name[sizeof(mfm->name)-1] = '\0'; |
| 59 | strncpy(mfm->scenario, g_mfs.scenario, sizeof(mfm->scenario)); |
| 60 | mfm->scenario[sizeof(mfm->scenario)-1] = '\0'; |
| 61 | |
| 62 | /* copy the entire measurement report */ |
| 63 | memcpy(&mfm->mr, mr, sizeof(mfm->mr)); |
| 64 | |
Alexander Chemeris | 622a9e8 | 2015-08-20 20:48:39 -0400 | [diff] [blame] | 65 | /* copy channel information */ |
| 66 | /* we assume that the measurement report always belong to some timeslot */ |
| 67 | mfm->lchan_type = (uint8_t)mr->lchan->type; |
| 68 | mfm->pchan_type = (uint8_t)mr->lchan->ts->pchan; |
| 69 | mfm->bts_nr = mr->lchan->ts->trx->bts->nr; |
| 70 | mfm->trx_nr = mr->lchan->ts->trx->nr; |
| 71 | mfm->ts_nr = mr->lchan->ts->nr; |
| 72 | mfm->ss_nr = mr->lchan->nr; |
| 73 | |
Harald Welte | d597317 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 74 | /* and send it to the socket */ |
Holger Hans Peter Freyther | 7f1e13a | 2015-03-28 18:09:31 +0100 | [diff] [blame] | 75 | if (osmo_wqueue_enqueue(&g_mfs.wqueue, msg) != 0) |
| 76 | msgb_free(msg); |
Harald Welte | d597317 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int meas_feed_sig_cb(unsigned int subsys, unsigned int signal, |
| 82 | void *handler_data, void *signal_data) |
| 83 | { |
| 84 | struct lchan_signal_data *sdata = signal_data; |
| 85 | |
| 86 | if (subsys != SS_LCHAN) |
| 87 | return 0; |
| 88 | |
| 89 | if (signal == S_LCHAN_MEAS_REP) |
| 90 | process_meas_rep(sdata->mr); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int feed_write_cb(struct osmo_fd *ofd, struct msgb *msg) |
| 96 | { |
| 97 | return write(ofd->fd, msgb_data(msg), msgb_length(msg)); |
| 98 | } |
| 99 | |
Harald Welte | d6f81f8 | 2015-01-01 13:06:48 +0100 | [diff] [blame] | 100 | static int feed_read_cb(struct osmo_fd *ofd) |
Harald Welte | d597317 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 101 | { |
| 102 | int rc; |
| 103 | char buf[256]; |
| 104 | |
| 105 | rc = read(ofd->fd, buf, sizeof(buf)); |
| 106 | ofd->fd &= ~BSC_FD_READ; |
| 107 | |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | int meas_feed_cfg_set(const char *dst_host, uint16_t dst_port) |
| 112 | { |
| 113 | int rc; |
| 114 | int already_initialized = 0; |
| 115 | |
| 116 | if (g_mfs.wqueue.bfd.fd) |
| 117 | already_initialized = 1; |
| 118 | |
| 119 | |
| 120 | if (already_initialized && |
| 121 | !strcmp(dst_host, g_mfs.dst_host) && |
| 122 | dst_port == g_mfs.dst_port) |
| 123 | return 0; |
| 124 | |
| 125 | if (!already_initialized) { |
| 126 | osmo_wqueue_init(&g_mfs.wqueue, 10); |
| 127 | g_mfs.wqueue.write_cb = feed_write_cb; |
| 128 | g_mfs.wqueue.read_cb = feed_read_cb; |
| 129 | osmo_signal_register_handler(SS_LCHAN, meas_feed_sig_cb, NULL); |
| 130 | } |
| 131 | |
| 132 | if (already_initialized) { |
| 133 | osmo_wqueue_clear(&g_mfs.wqueue); |
| 134 | osmo_fd_unregister(&g_mfs.wqueue.bfd); |
| 135 | close(g_mfs.wqueue.bfd.fd); |
| 136 | /* don't set to zero, as that would mean 'not yet initialized' */ |
| 137 | g_mfs.wqueue.bfd.fd = -1; |
| 138 | } |
| 139 | rc = osmo_sock_init_ofd(&g_mfs.wqueue.bfd, AF_UNSPEC, SOCK_DGRAM, |
| 140 | IPPROTO_UDP, dst_host, dst_port, |
| 141 | OSMO_SOCK_F_CONNECT); |
| 142 | if (rc < 0) |
| 143 | return rc; |
| 144 | |
| 145 | g_mfs.wqueue.bfd.when &= ~BSC_FD_READ; |
| 146 | |
| 147 | if (g_mfs.dst_host) |
| 148 | talloc_free(g_mfs.dst_host); |
| 149 | g_mfs.dst_host = talloc_strdup(NULL, dst_host); |
| 150 | g_mfs.dst_port = dst_port; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | void meas_feed_cfg_get(char **host, uint16_t *port) |
| 156 | { |
| 157 | *port = g_mfs.dst_port; |
| 158 | *host = g_mfs.dst_host; |
| 159 | } |
| 160 | |
| 161 | void meas_feed_scenario_set(const char *name) |
| 162 | { |
| 163 | strncpy(g_mfs.scenario, name, sizeof(g_mfs.scenario)-1); |
| 164 | g_mfs.scenario[sizeof(g_mfs.scenario)-1] = '\0'; |
| 165 | } |
| 166 | |
| 167 | const char *meas_feed_scenario_get(void) |
| 168 | { |
| 169 | return g_mfs.scenario; |
| 170 | } |