blob: 3a9c02b77d229439c9ffb7530356ef62ca8e21ce [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
Harald Welte98ba6352012-11-11 12:33:30 +0100157static int colpair_by_qual(uint8_t rx_qual)
158{
159 if (rx_qual == 0)
160 return 24;
161 else if (rx_qual <= 4)
162 return 32;
163 else
164 return 16;
165}
166
167static int colpair_by_lev(int rx_lev)
168{
169 if (rx_lev < -95)
170 return 16;
171 else if (rx_lev < -80)
172 return 32;
173 else
174 return 24;
175}
176
177
Harald Welteb4771a62012-11-11 10:58:51 +0100178void write_uni(struct ms_state *ms, struct ms_state_uni *msu,
179 struct gsm_rx_lev_qual *lq, int dir, int row)
180{
181
182 char label[128];
183 time_t now = time(NULL);
Harald Welte98ba6352012-11-11 12:33:30 +0100184 int qual_col = colpair_by_qual(lq->rx_qual);
185 int lev_col = colpair_by_lev(rxlev2dbm(lq->rx_lev));
Harald Welteb4771a62012-11-11 10:58:51 +0100186 int color, pwr;
187
188 if (dir == DIR_UL) {
Harald Welteb4771a62012-11-11 10:58:51 +0100189 pwr = ms->mr.ms_l1.pwr;
190 } else {
Harald Welteb4771a62012-11-11 10:58:51 +0100191 pwr = ms->mr.bs_power;
192 }
193
Harald Welte98ba6352012-11-11 12:33:30 +0100194 color = A_REVERSE | COLOR_PAIR(lev_col) | ' ';
Harald Welteb4771a62012-11-11 10:58:51 +0100195 snprintf(label, sizeof(label), "%s %s ", ms->imsi, dir_str[dir]);
196 msu->cdk = newCDKSlider(g_st.cdkscreen, 0, row, NULL, label, color,
Harald Welte98ba6352012-11-11 12:33:30 +0100197 COLS-50, rxlev2dbm(lq->rx_lev), -110, -47,
Harald Welteb4771a62012-11-11 10:58:51 +0100198 1, 2, FALSE, FALSE);
199 //IsVisibleObj(ms->ul.cdk) = FALSE;
Harald Welte98ba6352012-11-11 12:33:30 +0100200 snprintf(msu->label, sizeof(msu->label), "</%d>%1d<!%d> %-2d %3u",
201 qual_col, lq->rx_qual, qual_col, pwr, now - msu->last_update);
Harald Welteb4771a62012-11-11 10:58:51 +0100202 msu->cdk_label = newCDKLabel(g_st.cdkscreen, RIGHT, row,
203 msu->_lbl, 1, FALSE, FALSE);
204}
205
206static void update_sliders(void)
207{
208 int num_vis_sliders = 0;
209 struct ms_state *ms;
210
211 /* remove all sliders */
212 llist_for_each_entry(ms, &g_st.ms_list, list) {
213 destroy_dir(&ms->ul);
214 destroy_dir(&ms->dl);
215
216 }
217
218 llist_for_each_entry(ms, &g_st.ms_list, list) {
219 struct gsm_rx_lev_qual *lq;
220 unsigned int row = num_vis_sliders*3;
221
222 if (ms->mr.flags & MEAS_REP_F_UL_DTX)
223 lq = &ms->mr.ul.sub;
224 else
225 lq = &ms->mr.ul.full;
226 write_uni(ms, &ms->ul, lq, DIR_UL, row);
227
228 if (ms->mr.flags & MEAS_REP_F_DL_DTX)
229 lq = &ms->mr.dl.sub;
230 else
231 lq = &ms->mr.dl.full;
232 write_uni(ms, &ms->dl, lq, DIR_DL, row+1);
233
234 num_vis_sliders++;
235 if (num_vis_sliders >= LINES/3)
236 break;
237 }
238
239 refreshCDKScreen(g_st.cdkscreen);
240
241}
242
Harald Welte98ba6352012-11-11 12:33:30 +0100243const struct value_string col_strs[] = {
244 { COLOR_WHITE, "white" },
245 { COLOR_RED, "red" },
246 { COLOR_GREEN, "green" },
247 { COLOR_YELLOW, "yellow" },
248 { COLOR_BLUE, "blue" },
249 { COLOR_MAGENTA,"magenta" },
250 { COLOR_CYAN, "cyan" },
251 { COLOR_BLACK, "black" },
252 { 0, NULL }
253};
254
Harald Welteb4771a62012-11-11 10:58:51 +0100255int main(int argc, char **argv)
256{
257 int rc;
258
259 printf("sizeof(gsm_meas_rep)=%u\n", sizeof(struct gsm_meas_rep));
260 printf("sizeof(meas_feed_meas)=%u\n", sizeof(struct meas_feed_meas));
261
262 INIT_LLIST_HEAD(&g_st.ms_list);
263 g_st.curses_win = initscr();
264 g_st.cdkscreen = initCDKScreen(g_st.curses_win);
265 initCDKColor();
266
Harald Welte98ba6352012-11-11 12:33:30 +0100267#if 0
268 int i;
269 for (i = 0; i < 64; i++) {
270 short f, b;
271 pair_content(i, &f, &b);
272 attron(COLOR_PAIR(i));
273 printw("%u: %u (%s) ", i, f, get_value_string(col_strs, f));
274 printw("%u (%s)\n\r", b, get_value_string(col_strs, b));
275 }
276 refresh();
277 getch();
278 exit(0);
279#endif
280
Harald Welteb4771a62012-11-11 10:58:51 +0100281 g_st.udp_ofd.cb = udp_fd_cb;
282 rc = osmo_sock_init_ofd(&g_st.udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
283 if (rc < 0)
284 exit(1);
285
286 while (1) {
287 osmo_select_main(0);
288 update_sliders();
289 };
290
291 exit(0);
292}