blob: 77194ded488cd81dd1bc8bd5dee3386304264c40 [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);
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010076 osmo_strlcpy(ms->imsi, imsi, sizeof(ms->imsi));
Harald Welteb4771a62012-11-11 10:58:51 +010077 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
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010091 osmo_strlcpy(ms->name, mfm->name, sizeof(ms->name));
Harald Welteb4771a62012-11-11 10:58:51 +010092 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 }
Martin Hauke4316cb22015-11-05 21:02:47 +0100118
119 return 0;
Harald Welteb4771a62012-11-11 10:58:51 +0100120}
121
122static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
123{
124 int rc;
125
126 if (what & BSC_FD_READ) {
127 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
128
129 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
130 if (rc < 0)
131 return rc;
132 msgb_put(msg, rc);
133 handle_msg(msg);
134 msgb_free(msg);
135 }
136
137 return 0;
138}
139
140
141static void destroy_dir(struct ms_state_uni *uni)
142{
143 if (uni->cdk) {
144 destroyCDKSlider(uni->cdk);
145 uni->cdk = NULL;
146 }
147 if (uni->cdk_label) {
148 destroyCDKLabel(uni->cdk_label);
149 uni->cdk_label = NULL;
150 }
151}
152
153#define DIR_UL 0
154#define DIR_DL 1
155static const char *dir_str[2] = {
156 [DIR_UL] = "UL",
157 [DIR_DL] = "DL",
158};
159
Harald Welte98ba6352012-11-11 12:33:30 +0100160static int colpair_by_qual(uint8_t rx_qual)
161{
162 if (rx_qual == 0)
163 return 24;
164 else if (rx_qual <= 4)
165 return 32;
166 else
167 return 16;
168}
169
170static int colpair_by_lev(int rx_lev)
171{
172 if (rx_lev < -95)
173 return 16;
174 else if (rx_lev < -80)
175 return 32;
176 else
177 return 24;
178}
179
180
Harald Welteb4771a62012-11-11 10:58:51 +0100181void write_uni(struct ms_state *ms, struct ms_state_uni *msu,
182 struct gsm_rx_lev_qual *lq, int dir, int row)
183{
184
185 char label[128];
186 time_t now = time(NULL);
Harald Welte98ba6352012-11-11 12:33:30 +0100187 int qual_col = colpair_by_qual(lq->rx_qual);
188 int lev_col = colpair_by_lev(rxlev2dbm(lq->rx_lev));
Harald Welteb4771a62012-11-11 10:58:51 +0100189 int color, pwr;
190
191 if (dir == DIR_UL) {
Harald Welteb4771a62012-11-11 10:58:51 +0100192 pwr = ms->mr.ms_l1.pwr;
193 } else {
Harald Welteb4771a62012-11-11 10:58:51 +0100194 pwr = ms->mr.bs_power;
195 }
196
Harald Welte98ba6352012-11-11 12:33:30 +0100197 color = A_REVERSE | COLOR_PAIR(lev_col) | ' ';
Harald Welteb4771a62012-11-11 10:58:51 +0100198 snprintf(label, sizeof(label), "%s %s ", ms->imsi, dir_str[dir]);
199 msu->cdk = newCDKSlider(g_st.cdkscreen, 0, row, NULL, label, color,
Harald Welte61c91562012-11-11 12:57:05 +0100200 COLS-40, rxlev2dbm(lq->rx_lev), -110, -47,
Harald Welteb4771a62012-11-11 10:58:51 +0100201 1, 2, FALSE, FALSE);
202 //IsVisibleObj(ms->ul.cdk) = FALSE;
Max11e4e412017-04-20 13:07:58 +0200203 snprintf(msu->label, sizeof(msu->label), "</%d>%1d<!%d> %3d %2u %2d %4u",
Harald Welte61c91562012-11-11 12:57:05 +0100204 qual_col, lq->rx_qual, qual_col, pwr,
205 ms->mr.ms_l1.ta, ms->mr.ms_timing_offset,
206 now - msu->last_update);
Harald Welteb4771a62012-11-11 10:58:51 +0100207 msu->cdk_label = newCDKLabel(g_st.cdkscreen, RIGHT, row,
208 msu->_lbl, 1, FALSE, FALSE);
209}
210
211static void update_sliders(void)
212{
213 int num_vis_sliders = 0;
214 struct ms_state *ms;
Harald Welte61c91562012-11-11 12:57:05 +0100215#define HEADER_LINES 2
Harald Welteb4771a62012-11-11 10:58:51 +0100216
217 /* remove all sliders */
218 llist_for_each_entry(ms, &g_st.ms_list, list) {
219 destroy_dir(&ms->ul);
220 destroy_dir(&ms->dl);
221
222 }
223
224 llist_for_each_entry(ms, &g_st.ms_list, list) {
225 struct gsm_rx_lev_qual *lq;
Harald Welte61c91562012-11-11 12:57:05 +0100226 unsigned int row = HEADER_LINES + num_vis_sliders*3;
Harald Welteb4771a62012-11-11 10:58:51 +0100227
228 if (ms->mr.flags & MEAS_REP_F_UL_DTX)
229 lq = &ms->mr.ul.sub;
230 else
231 lq = &ms->mr.ul.full;
232 write_uni(ms, &ms->ul, lq, DIR_UL, row);
233
234 if (ms->mr.flags & MEAS_REP_F_DL_DTX)
235 lq = &ms->mr.dl.sub;
236 else
237 lq = &ms->mr.dl.full;
238 write_uni(ms, &ms->dl, lq, DIR_DL, row+1);
239
240 num_vis_sliders++;
241 if (num_vis_sliders >= LINES/3)
242 break;
243 }
244
245 refreshCDKScreen(g_st.cdkscreen);
246
247}
248
Harald Welte98ba6352012-11-11 12:33:30 +0100249const struct value_string col_strs[] = {
250 { COLOR_WHITE, "white" },
251 { COLOR_RED, "red" },
252 { COLOR_GREEN, "green" },
253 { COLOR_YELLOW, "yellow" },
254 { COLOR_BLUE, "blue" },
255 { COLOR_MAGENTA,"magenta" },
256 { COLOR_CYAN, "cyan" },
257 { COLOR_BLACK, "black" },
258 { 0, NULL }
259};
260
Harald Welteb4771a62012-11-11 10:58:51 +0100261int main(int argc, char **argv)
262{
263 int rc;
Harald Welte61c91562012-11-11 12:57:05 +0100264 char *header[1];
265 char *title[1];
Harald Welteb4771a62012-11-11 10:58:51 +0100266
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +0200267 msgb_talloc_ctx_init(NULL, 0);
268
Harald Welteb4771a62012-11-11 10:58:51 +0100269 printf("sizeof(gsm_meas_rep)=%u\n", sizeof(struct gsm_meas_rep));
270 printf("sizeof(meas_feed_meas)=%u\n", sizeof(struct meas_feed_meas));
271
272 INIT_LLIST_HEAD(&g_st.ms_list);
273 g_st.curses_win = initscr();
274 g_st.cdkscreen = initCDKScreen(g_st.curses_win);
275 initCDKColor();
276
Harald Welte61c91562012-11-11 12:57:05 +0100277 g_st.title = "OpenBSC link quality monitor";
278 title[0] = g_st.title;
279 g_st.cdk_title = newCDKLabel(g_st.cdkscreen, CENTER, 0, title, 1, FALSE, FALSE);
280
281 snprintf(g_st.header, sizeof(g_st.header), "Q Pwr TA TO Time");
282 header[0] = g_st.header;
283 g_st.cdk_header = newCDKLabel(g_st.cdkscreen, RIGHT, 1, header, 1, FALSE, FALSE);
284
Harald Welte98ba6352012-11-11 12:33:30 +0100285#if 0
286 int i;
287 for (i = 0; i < 64; i++) {
288 short f, b;
289 pair_content(i, &f, &b);
290 attron(COLOR_PAIR(i));
291 printw("%u: %u (%s) ", i, f, get_value_string(col_strs, f));
292 printw("%u (%s)\n\r", b, get_value_string(col_strs, b));
293 }
294 refresh();
295 getch();
296 exit(0);
297#endif
298
Harald Welteb4771a62012-11-11 10:58:51 +0100299 g_st.udp_ofd.cb = udp_fd_cb;
300 rc = osmo_sock_init_ofd(&g_st.udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
301 if (rc < 0)
302 exit(1);
303
304 while (1) {
305 osmo_select_main(0);
306 update_sliders();
307 };
308
309 exit(0);
310}