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