blob: 092d6adf1dba028d5b77abf688ef7d4594a226d5 [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
Harald Welteb4771a62012-11-11 10:58:51 +010042struct state {
43 struct osmo_fd udp_ofd;
44 struct llist_head ms_list;
45
46 CDKSCREEN *cdkscreen;
47 WINDOW *curses_win;
Harald Welte61c91562012-11-11 12:57:05 +010048
49 CDKLABEL *cdk_title;
50 char *title;
51
52 CDKLABEL *cdk_header;
53 char header[256];
Harald Welteb4771a62012-11-11 10:58:51 +010054};
55
56static struct state g_st;
57
58struct ms_state *find_ms(const char *imsi)
59{
60 struct ms_state *ms;
61
62 llist_for_each_entry(ms, &g_st.ms_list, list) {
63 if (!strcmp(ms->imsi, imsi))
64 return ms;
65 }
66 return NULL;
67}
68
69static struct ms_state *find_alloc_ms(const char *imsi)
70{
71 struct ms_state *ms;
72
73 ms = find_ms(imsi);
74 if (!ms) {
75 ms = talloc_zero(NULL, struct ms_state);
76 strncpy(ms->imsi, imsi, sizeof(ms->imsi)-1);
77 ms->ul._lbl[0] = ms->ul.label;
78 ms->dl._lbl[0] = ms->dl.label;
79 llist_add_tail(&ms->list, &g_st.ms_list);
80 }
81
82 return ms;
83}
84
85static int handle_meas(struct msgb *msg)
86{
87 struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg);
88 struct ms_state *ms = find_alloc_ms(mfm->imsi);
89 time_t now = time(NULL);
90
91 strncpy(ms->name, mfm->name, sizeof(ms->imsi)-1);
92 memcpy(&ms->mr, &mfm->mr, sizeof(ms->mr));
93 ms->ul.last_update = now;
94 if (ms->mr.flags & MEAS_REP_F_DL_VALID)
95 ms->dl.last_update = now;
96
97 /* move to head of list */
98 llist_del(&ms->list);
99 llist_add(&ms->list, &g_st.ms_list);
100
101 return 0;
102}
103
104static int handle_msg(struct msgb *msg)
105{
106 struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg);
107
108 if (mfh->version != MEAS_FEED_VERSION)
109 return -EINVAL;
110
111 switch (mfh->msg_type) {
112 case MEAS_FEED_MEAS:
113 handle_meas(msg);
114 break;
115 default:
116 break;
117 }
118}
119
120static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
121{
122 int rc;
123
124 if (what & BSC_FD_READ) {
125 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
126
127 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
128 if (rc < 0)
129 return rc;
130 msgb_put(msg, rc);
131 handle_msg(msg);
132 msgb_free(msg);
133 }
134
135 return 0;
136}
137
138
139static void destroy_dir(struct ms_state_uni *uni)
140{
141 if (uni->cdk) {
142 destroyCDKSlider(uni->cdk);
143 uni->cdk = NULL;
144 }
145 if (uni->cdk_label) {
146 destroyCDKLabel(uni->cdk_label);
147 uni->cdk_label = NULL;
148 }
149}
150
151#define DIR_UL 0
152#define DIR_DL 1
153static const char *dir_str[2] = {
154 [DIR_UL] = "UL",
155 [DIR_DL] = "DL",
156};
157
Harald Welte98ba6352012-11-11 12:33:30 +0100158static int colpair_by_qual(uint8_t rx_qual)
159{
160 if (rx_qual == 0)
161 return 24;
162 else if (rx_qual <= 4)
163 return 32;
164 else
165 return 16;
166}
167
168static int colpair_by_lev(int rx_lev)
169{
170 if (rx_lev < -95)
171 return 16;
172 else if (rx_lev < -80)
173 return 32;
174 else
175 return 24;
176}
177
178
Harald Welteb4771a62012-11-11 10:58:51 +0100179void write_uni(struct ms_state *ms, struct ms_state_uni *msu,
180 struct gsm_rx_lev_qual *lq, int dir, int row)
181{
182
183 char label[128];
184 time_t now = time(NULL);
Harald Welte98ba6352012-11-11 12:33:30 +0100185 int qual_col = colpair_by_qual(lq->rx_qual);
186 int lev_col = colpair_by_lev(rxlev2dbm(lq->rx_lev));
Harald Welteb4771a62012-11-11 10:58:51 +0100187 int color, pwr;
188
189 if (dir == DIR_UL) {
Harald Welteb4771a62012-11-11 10:58:51 +0100190 pwr = ms->mr.ms_l1.pwr;
191 } else {
Harald Welteb4771a62012-11-11 10:58:51 +0100192 pwr = ms->mr.bs_power;
193 }
194
Harald Welte98ba6352012-11-11 12:33:30 +0100195 color = A_REVERSE | COLOR_PAIR(lev_col) | ' ';
Harald Welteb4771a62012-11-11 10:58:51 +0100196 snprintf(label, sizeof(label), "%s %s ", ms->imsi, dir_str[dir]);
197 msu->cdk = newCDKSlider(g_st.cdkscreen, 0, row, NULL, label, color,
Harald Welte61c91562012-11-11 12:57:05 +0100198 COLS-40, rxlev2dbm(lq->rx_lev), -110, -47,
Harald Welteb4771a62012-11-11 10:58:51 +0100199 1, 2, FALSE, FALSE);
200 //IsVisibleObj(ms->ul.cdk) = FALSE;
Harald Welte61c91562012-11-11 12:57:05 +0100201 snprintf(msu->label, sizeof(msu->label), "</%d>%1d<!%d> %3d %2u %2u %4u",
202 qual_col, lq->rx_qual, qual_col, pwr,
203 ms->mr.ms_l1.ta, ms->mr.ms_timing_offset,
204 now - msu->last_update);
Harald Welteb4771a62012-11-11 10:58:51 +0100205 msu->cdk_label = newCDKLabel(g_st.cdkscreen, RIGHT, row,
206 msu->_lbl, 1, FALSE, FALSE);
207}
208
209static void update_sliders(void)
210{
211 int num_vis_sliders = 0;
212 struct ms_state *ms;
Harald Welte61c91562012-11-11 12:57:05 +0100213#define HEADER_LINES 2
Harald Welteb4771a62012-11-11 10:58:51 +0100214
215 /* remove all sliders */
216 llist_for_each_entry(ms, &g_st.ms_list, list) {
217 destroy_dir(&ms->ul);
218 destroy_dir(&ms->dl);
219
220 }
221
222 llist_for_each_entry(ms, &g_st.ms_list, list) {
223 struct gsm_rx_lev_qual *lq;
Harald Welte61c91562012-11-11 12:57:05 +0100224 unsigned int row = HEADER_LINES + num_vis_sliders*3;
Harald Welteb4771a62012-11-11 10:58:51 +0100225
226 if (ms->mr.flags & MEAS_REP_F_UL_DTX)
227 lq = &ms->mr.ul.sub;
228 else
229 lq = &ms->mr.ul.full;
230 write_uni(ms, &ms->ul, lq, DIR_UL, row);
231
232 if (ms->mr.flags & MEAS_REP_F_DL_DTX)
233 lq = &ms->mr.dl.sub;
234 else
235 lq = &ms->mr.dl.full;
236 write_uni(ms, &ms->dl, lq, DIR_DL, row+1);
237
238 num_vis_sliders++;
239 if (num_vis_sliders >= LINES/3)
240 break;
241 }
242
243 refreshCDKScreen(g_st.cdkscreen);
244
245}
246
Harald Welte98ba6352012-11-11 12:33:30 +0100247const struct value_string col_strs[] = {
248 { COLOR_WHITE, "white" },
249 { COLOR_RED, "red" },
250 { COLOR_GREEN, "green" },
251 { COLOR_YELLOW, "yellow" },
252 { COLOR_BLUE, "blue" },
253 { COLOR_MAGENTA,"magenta" },
254 { COLOR_CYAN, "cyan" },
255 { COLOR_BLACK, "black" },
256 { 0, NULL }
257};
258
Harald Welteb4771a62012-11-11 10:58:51 +0100259int main(int argc, char **argv)
260{
261 int rc;
Harald Welte61c91562012-11-11 12:57:05 +0100262 char *header[1];
263 char *title[1];
Harald Welteb4771a62012-11-11 10:58:51 +0100264
265 printf("sizeof(gsm_meas_rep)=%u\n", sizeof(struct gsm_meas_rep));
266 printf("sizeof(meas_feed_meas)=%u\n", sizeof(struct meas_feed_meas));
267
268 INIT_LLIST_HEAD(&g_st.ms_list);
269 g_st.curses_win = initscr();
270 g_st.cdkscreen = initCDKScreen(g_st.curses_win);
271 initCDKColor();
272
Harald Welte61c91562012-11-11 12:57:05 +0100273 g_st.title = "OpenBSC link quality monitor";
274 title[0] = g_st.title;
275 g_st.cdk_title = newCDKLabel(g_st.cdkscreen, CENTER, 0, title, 1, FALSE, FALSE);
276
277 snprintf(g_st.header, sizeof(g_st.header), "Q Pwr TA TO Time");
278 header[0] = g_st.header;
279 g_st.cdk_header = newCDKLabel(g_st.cdkscreen, RIGHT, 1, header, 1, FALSE, FALSE);
280
Harald Welte98ba6352012-11-11 12:33:30 +0100281#if 0
282 int i;
283 for (i = 0; i < 64; i++) {
284 short f, b;
285 pair_content(i, &f, &b);
286 attron(COLOR_PAIR(i));
287 printw("%u: %u (%s) ", i, f, get_value_string(col_strs, f));
288 printw("%u (%s)\n\r", b, get_value_string(col_strs, b));
289 }
290 refresh();
291 getch();
292 exit(0);
293#endif
294
Harald Welteb4771a62012-11-11 10:58:51 +0100295 g_st.udp_ofd.cb = udp_fd_cb;
296 rc = osmo_sock_init_ofd(&g_st.udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
297 if (rc < 0)
298 exit(1);
299
300 while (1) {
301 osmo_select_main(0);
302 update_sliders();
303 };
304
305 exit(0);
306}