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