blob: 67b3fdbe60685d42cdbb01962049715bb13f66f9 [file] [log] [blame]
Harald Welteb4771a62012-11-11 10:58:51 +01001#include <string.h>
2#include <errno.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <stdio.h>
6
7#include <netinet/in.h>
8
9#include <cdk/cdk.h>
10
11#include <osmocom/core/socket.h>
12#include <osmocom/core/utils.h>
13#include <osmocom/core/msgb.h>
14#include <osmocom/core/select.h>
15#include <osmocom/core/talloc.h>
16
17#include <osmocom/gsm/gsm_utils.h>
18
19#include <openbsc/meas_feed.h>
20
21struct ms_state_uni {
22 CDKSLIDER *cdk;
23 CDKLABEL *cdk_label;
24
25 time_t last_update;
26 char label[32];
27 char *_lbl[1];
28};
29
30
31struct ms_state {
32 struct llist_head list;
33
34 char name[31+1];
35 char imsi[15+1];
36 struct gsm_meas_rep mr;
37
38 struct ms_state_uni ul;
39 struct ms_state_uni dl;
40};
41
42struct slider {
43 struct ms_state *ms;
44};
45
46struct state {
47 struct osmo_fd udp_ofd;
48 struct llist_head ms_list;
49
50 CDKSCREEN *cdkscreen;
51 WINDOW *curses_win;
52 struct slider sliders[8];
53};
54
55static struct state g_st;
56
57struct ms_state *find_ms(const char *imsi)
58{
59 struct ms_state *ms;
60
61 llist_for_each_entry(ms, &g_st.ms_list, list) {
62 if (!strcmp(ms->imsi, imsi))
63 return ms;
64 }
65 return NULL;
66}
67
68static struct ms_state *find_alloc_ms(const char *imsi)
69{
70 struct ms_state *ms;
71
72 ms = find_ms(imsi);
73 if (!ms) {
74 ms = talloc_zero(NULL, struct ms_state);
75 strncpy(ms->imsi, imsi, sizeof(ms->imsi)-1);
76 ms->ul._lbl[0] = ms->ul.label;
77 ms->dl._lbl[0] = ms->dl.label;
78 llist_add_tail(&ms->list, &g_st.ms_list);
79 }
80
81 return ms;
82}
83
84static int handle_meas(struct msgb *msg)
85{
86 struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg);
87 struct ms_state *ms = find_alloc_ms(mfm->imsi);
88 time_t now = time(NULL);
89
90 strncpy(ms->name, mfm->name, sizeof(ms->imsi)-1);
91 memcpy(&ms->mr, &mfm->mr, sizeof(ms->mr));
92 ms->ul.last_update = now;
93 if (ms->mr.flags & MEAS_REP_F_DL_VALID)
94 ms->dl.last_update = now;
95
96 /* move to head of list */
97 llist_del(&ms->list);
98 llist_add(&ms->list, &g_st.ms_list);
99
100 return 0;
101}
102
103static int handle_msg(struct msgb *msg)
104{
105 struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg);
106
107 if (mfh->version != MEAS_FEED_VERSION)
108 return -EINVAL;
109
110 switch (mfh->msg_type) {
111 case MEAS_FEED_MEAS:
112 handle_meas(msg);
113 break;
114 default:
115 break;
116 }
117}
118
119static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
120{
121 int rc;
122
123 if (what & BSC_FD_READ) {
124 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
125
126 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
127 if (rc < 0)
128 return rc;
129 msgb_put(msg, rc);
130 handle_msg(msg);
131 msgb_free(msg);
132 }
133
134 return 0;
135}
136
137
138static void destroy_dir(struct ms_state_uni *uni)
139{
140 if (uni->cdk) {
141 destroyCDKSlider(uni->cdk);
142 uni->cdk = NULL;
143 }
144 if (uni->cdk_label) {
145 destroyCDKLabel(uni->cdk_label);
146 uni->cdk_label = NULL;
147 }
148}
149
150#define DIR_UL 0
151#define DIR_DL 1
152static const char *dir_str[2] = {
153 [DIR_UL] = "UL",
154 [DIR_DL] = "DL",
155};
156
157void write_uni(struct ms_state *ms, struct ms_state_uni *msu,
158 struct gsm_rx_lev_qual *lq, int dir, int row)
159{
160
161 char label[128];
162 time_t now = time(NULL);
163 int color, pwr;
164
165 if (dir == DIR_UL) {
166 color = A_REVERSE | COLOR_PAIR (29) | ' ';
167 pwr = ms->mr.ms_l1.pwr;
168 } else {
169 color = A_REVERSE | COLOR_PAIR (19) | ' ',
170 pwr = ms->mr.bs_power;
171 }
172
173 snprintf(label, sizeof(label), "%s %s ", ms->imsi, dir_str[dir]);
174 msu->cdk = newCDKSlider(g_st.cdkscreen, 0, row, NULL, label, color,
175 -30, rxlev2dbm(lq->rx_lev), -110, -47,
176 1, 2, FALSE, FALSE);
177 //IsVisibleObj(ms->ul.cdk) = FALSE;
178 snprintf(msu->label, sizeof(msu->label), "%-2d %3u", pwr, now - msu->last_update);
179 msu->cdk_label = newCDKLabel(g_st.cdkscreen, RIGHT, row,
180 msu->_lbl, 1, FALSE, FALSE);
181}
182
183static void update_sliders(void)
184{
185 int num_vis_sliders = 0;
186 struct ms_state *ms;
187
188 /* remove all sliders */
189 llist_for_each_entry(ms, &g_st.ms_list, list) {
190 destroy_dir(&ms->ul);
191 destroy_dir(&ms->dl);
192
193 }
194
195 llist_for_each_entry(ms, &g_st.ms_list, list) {
196 struct gsm_rx_lev_qual *lq;
197 unsigned int row = num_vis_sliders*3;
198
199 if (ms->mr.flags & MEAS_REP_F_UL_DTX)
200 lq = &ms->mr.ul.sub;
201 else
202 lq = &ms->mr.ul.full;
203 write_uni(ms, &ms->ul, lq, DIR_UL, row);
204
205 if (ms->mr.flags & MEAS_REP_F_DL_DTX)
206 lq = &ms->mr.dl.sub;
207 else
208 lq = &ms->mr.dl.full;
209 write_uni(ms, &ms->dl, lq, DIR_DL, row+1);
210
211 num_vis_sliders++;
212 if (num_vis_sliders >= LINES/3)
213 break;
214 }
215
216 refreshCDKScreen(g_st.cdkscreen);
217
218}
219
220int main(int argc, char **argv)
221{
222 int rc;
223
224 printf("sizeof(gsm_meas_rep)=%u\n", sizeof(struct gsm_meas_rep));
225 printf("sizeof(meas_feed_meas)=%u\n", sizeof(struct meas_feed_meas));
226
227 INIT_LLIST_HEAD(&g_st.ms_list);
228 g_st.curses_win = initscr();
229 g_st.cdkscreen = initCDKScreen(g_st.curses_win);
230 initCDKColor();
231
232 g_st.udp_ofd.cb = udp_fd_cb;
233 rc = osmo_sock_init_ofd(&g_st.udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
234 if (rc < 0)
235 exit(1);
236
237 while (1) {
238 osmo_select_main(0);
239 update_sliders();
240 };
241
242 exit(0);
243}