Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 1 | #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> |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame^] | 16 | #include <osmocom/core/logging.h> |
| 17 | #include <osmocom/core/application.h> |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 18 | |
| 19 | #include <osmocom/gsm/gsm_utils.h> |
| 20 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 21 | #include <osmocom/bsc/meas_feed.h> |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 22 | |
| 23 | struct ms_state_uni { |
| 24 | CDKSLIDER *cdk; |
| 25 | CDKLABEL *cdk_label; |
| 26 | |
| 27 | time_t last_update; |
| 28 | char label[32]; |
| 29 | char *_lbl[1]; |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | struct ms_state { |
| 34 | struct llist_head list; |
| 35 | |
| 36 | char name[31+1]; |
| 37 | char imsi[15+1]; |
| 38 | struct gsm_meas_rep mr; |
| 39 | |
| 40 | struct ms_state_uni ul; |
| 41 | struct ms_state_uni dl; |
| 42 | }; |
| 43 | |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 44 | struct state { |
| 45 | struct osmo_fd udp_ofd; |
| 46 | struct llist_head ms_list; |
| 47 | |
| 48 | CDKSCREEN *cdkscreen; |
| 49 | WINDOW *curses_win; |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 50 | |
| 51 | CDKLABEL *cdk_title; |
| 52 | char *title; |
| 53 | |
| 54 | CDKLABEL *cdk_header; |
| 55 | char header[256]; |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static struct state g_st; |
| 59 | |
| 60 | struct ms_state *find_ms(const char *imsi) |
| 61 | { |
| 62 | struct ms_state *ms; |
| 63 | |
| 64 | llist_for_each_entry(ms, &g_st.ms_list, list) { |
| 65 | if (!strcmp(ms->imsi, imsi)) |
| 66 | return ms; |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | static struct ms_state *find_alloc_ms(const char *imsi) |
| 72 | { |
| 73 | struct ms_state *ms; |
| 74 | |
| 75 | ms = find_ms(imsi); |
| 76 | if (!ms) { |
| 77 | ms = talloc_zero(NULL, struct ms_state); |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 78 | osmo_strlcpy(ms->imsi, imsi, sizeof(ms->imsi)); |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 79 | ms->ul._lbl[0] = ms->ul.label; |
| 80 | ms->dl._lbl[0] = ms->dl.label; |
| 81 | llist_add_tail(&ms->list, &g_st.ms_list); |
| 82 | } |
| 83 | |
| 84 | return ms; |
| 85 | } |
| 86 | |
| 87 | static int handle_meas(struct msgb *msg) |
| 88 | { |
| 89 | struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg); |
| 90 | struct ms_state *ms = find_alloc_ms(mfm->imsi); |
| 91 | time_t now = time(NULL); |
| 92 | |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 93 | osmo_strlcpy(ms->name, mfm->name, sizeof(ms->name)); |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 94 | memcpy(&ms->mr, &mfm->mr, sizeof(ms->mr)); |
| 95 | ms->ul.last_update = now; |
| 96 | if (ms->mr.flags & MEAS_REP_F_DL_VALID) |
| 97 | ms->dl.last_update = now; |
| 98 | |
| 99 | /* move to head of list */ |
| 100 | llist_del(&ms->list); |
| 101 | llist_add(&ms->list, &g_st.ms_list); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int handle_msg(struct msgb *msg) |
| 107 | { |
| 108 | struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg); |
| 109 | |
| 110 | if (mfh->version != MEAS_FEED_VERSION) |
| 111 | return -EINVAL; |
| 112 | |
| 113 | switch (mfh->msg_type) { |
| 114 | case MEAS_FEED_MEAS: |
| 115 | handle_meas(msg); |
| 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
Martin Hauke | 4316cb2 | 2015-11-05 21:02:47 +0100 | [diff] [blame] | 120 | |
| 121 | return 0; |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 125 | { |
| 126 | int rc; |
| 127 | |
| 128 | if (what & BSC_FD_READ) { |
| 129 | struct msgb *msg = msgb_alloc(1024, "UDP Rx"); |
| 130 | |
| 131 | rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg)); |
| 132 | if (rc < 0) |
| 133 | return rc; |
| 134 | msgb_put(msg, rc); |
| 135 | handle_msg(msg); |
| 136 | msgb_free(msg); |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | static void destroy_dir(struct ms_state_uni *uni) |
| 144 | { |
| 145 | if (uni->cdk) { |
| 146 | destroyCDKSlider(uni->cdk); |
| 147 | uni->cdk = NULL; |
| 148 | } |
| 149 | if (uni->cdk_label) { |
| 150 | destroyCDKLabel(uni->cdk_label); |
| 151 | uni->cdk_label = NULL; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #define DIR_UL 0 |
| 156 | #define DIR_DL 1 |
| 157 | static const char *dir_str[2] = { |
| 158 | [DIR_UL] = "UL", |
| 159 | [DIR_DL] = "DL", |
| 160 | }; |
| 161 | |
Harald Welte | 98ba635 | 2012-11-11 12:33:30 +0100 | [diff] [blame] | 162 | static int colpair_by_qual(uint8_t rx_qual) |
| 163 | { |
| 164 | if (rx_qual == 0) |
| 165 | return 24; |
| 166 | else if (rx_qual <= 4) |
| 167 | return 32; |
| 168 | else |
| 169 | return 16; |
| 170 | } |
| 171 | |
| 172 | static int colpair_by_lev(int rx_lev) |
| 173 | { |
| 174 | if (rx_lev < -95) |
| 175 | return 16; |
| 176 | else if (rx_lev < -80) |
| 177 | return 32; |
| 178 | else |
| 179 | return 24; |
| 180 | } |
| 181 | |
| 182 | |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 183 | void write_uni(struct ms_state *ms, struct ms_state_uni *msu, |
| 184 | struct gsm_rx_lev_qual *lq, int dir, int row) |
| 185 | { |
| 186 | |
| 187 | char label[128]; |
| 188 | time_t now = time(NULL); |
Harald Welte | 98ba635 | 2012-11-11 12:33:30 +0100 | [diff] [blame] | 189 | int qual_col = colpair_by_qual(lq->rx_qual); |
| 190 | int lev_col = colpair_by_lev(rxlev2dbm(lq->rx_lev)); |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 191 | int color, pwr; |
| 192 | |
| 193 | if (dir == DIR_UL) { |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 194 | pwr = ms->mr.ms_l1.pwr; |
| 195 | } else { |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 196 | pwr = ms->mr.bs_power; |
| 197 | } |
| 198 | |
Harald Welte | 98ba635 | 2012-11-11 12:33:30 +0100 | [diff] [blame] | 199 | color = A_REVERSE | COLOR_PAIR(lev_col) | ' '; |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 200 | snprintf(label, sizeof(label), "%s %s ", ms->imsi, dir_str[dir]); |
| 201 | msu->cdk = newCDKSlider(g_st.cdkscreen, 0, row, NULL, label, color, |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 202 | COLS-40, rxlev2dbm(lq->rx_lev), -110, -47, |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 203 | 1, 2, FALSE, FALSE); |
| 204 | //IsVisibleObj(ms->ul.cdk) = FALSE; |
Max | 11e4e41 | 2017-04-20 13:07:58 +0200 | [diff] [blame] | 205 | snprintf(msu->label, sizeof(msu->label), "</%d>%1d<!%d> %3d %2u %2d %4u", |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 206 | qual_col, lq->rx_qual, qual_col, pwr, |
| 207 | ms->mr.ms_l1.ta, ms->mr.ms_timing_offset, |
| 208 | now - msu->last_update); |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 209 | msu->cdk_label = newCDKLabel(g_st.cdkscreen, RIGHT, row, |
| 210 | msu->_lbl, 1, FALSE, FALSE); |
| 211 | } |
| 212 | |
| 213 | static void update_sliders(void) |
| 214 | { |
| 215 | int num_vis_sliders = 0; |
| 216 | struct ms_state *ms; |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 217 | #define HEADER_LINES 2 |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 218 | |
| 219 | /* remove all sliders */ |
| 220 | llist_for_each_entry(ms, &g_st.ms_list, list) { |
| 221 | destroy_dir(&ms->ul); |
| 222 | destroy_dir(&ms->dl); |
| 223 | |
| 224 | } |
| 225 | |
| 226 | llist_for_each_entry(ms, &g_st.ms_list, list) { |
| 227 | struct gsm_rx_lev_qual *lq; |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 228 | unsigned int row = HEADER_LINES + num_vis_sliders*3; |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 229 | |
| 230 | if (ms->mr.flags & MEAS_REP_F_UL_DTX) |
| 231 | lq = &ms->mr.ul.sub; |
| 232 | else |
| 233 | lq = &ms->mr.ul.full; |
| 234 | write_uni(ms, &ms->ul, lq, DIR_UL, row); |
| 235 | |
| 236 | if (ms->mr.flags & MEAS_REP_F_DL_DTX) |
| 237 | lq = &ms->mr.dl.sub; |
| 238 | else |
| 239 | lq = &ms->mr.dl.full; |
| 240 | write_uni(ms, &ms->dl, lq, DIR_DL, row+1); |
| 241 | |
| 242 | num_vis_sliders++; |
| 243 | if (num_vis_sliders >= LINES/3) |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | refreshCDKScreen(g_st.cdkscreen); |
| 248 | |
| 249 | } |
| 250 | |
Harald Welte | 98ba635 | 2012-11-11 12:33:30 +0100 | [diff] [blame] | 251 | const struct value_string col_strs[] = { |
| 252 | { COLOR_WHITE, "white" }, |
| 253 | { COLOR_RED, "red" }, |
| 254 | { COLOR_GREEN, "green" }, |
| 255 | { COLOR_YELLOW, "yellow" }, |
| 256 | { COLOR_BLUE, "blue" }, |
| 257 | { COLOR_MAGENTA,"magenta" }, |
| 258 | { COLOR_CYAN, "cyan" }, |
| 259 | { COLOR_BLACK, "black" }, |
| 260 | { 0, NULL } |
| 261 | }; |
| 262 | |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame^] | 263 | /* default categories */ |
| 264 | static struct log_info_cat default_categories[] = { |
| 265 | }; |
| 266 | |
| 267 | static const struct log_info meas_vis_log_info = { |
| 268 | .cat = default_categories, |
| 269 | .num_cat = ARRAY_SIZE(default_categories), |
| 270 | }; |
| 271 | |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 272 | int main(int argc, char **argv) |
| 273 | { |
| 274 | int rc; |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 275 | char *header[1]; |
| 276 | char *title[1]; |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame^] | 277 | struct log_target *stderr_target; |
| 278 | |
| 279 | void *tall_ctx = talloc_named_const(NULL, 0, "meas_vis"); |
| 280 | osmo_init_logging2(tall_ctx, &meas_vis_log_info); |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 281 | |
Neels Hofmeyr | 4c2d4ab | 2016-09-16 02:31:17 +0200 | [diff] [blame] | 282 | msgb_talloc_ctx_init(NULL, 0); |
| 283 | |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 284 | printf("sizeof(gsm_meas_rep)=%u\n", sizeof(struct gsm_meas_rep)); |
| 285 | printf("sizeof(meas_feed_meas)=%u\n", sizeof(struct meas_feed_meas)); |
| 286 | |
| 287 | INIT_LLIST_HEAD(&g_st.ms_list); |
| 288 | g_st.curses_win = initscr(); |
| 289 | g_st.cdkscreen = initCDKScreen(g_st.curses_win); |
| 290 | initCDKColor(); |
| 291 | |
Daniel Willmann | 2dcdf44 | 2019-04-15 16:50:46 +0200 | [diff] [blame] | 292 | g_st.title = "OsmoBSC link quality monitor"; |
Harald Welte | 61c9156 | 2012-11-11 12:57:05 +0100 | [diff] [blame] | 293 | title[0] = g_st.title; |
| 294 | g_st.cdk_title = newCDKLabel(g_st.cdkscreen, CENTER, 0, title, 1, FALSE, FALSE); |
| 295 | |
| 296 | snprintf(g_st.header, sizeof(g_st.header), "Q Pwr TA TO Time"); |
| 297 | header[0] = g_st.header; |
| 298 | g_st.cdk_header = newCDKLabel(g_st.cdkscreen, RIGHT, 1, header, 1, FALSE, FALSE); |
| 299 | |
Harald Welte | 98ba635 | 2012-11-11 12:33:30 +0100 | [diff] [blame] | 300 | #if 0 |
| 301 | int i; |
| 302 | for (i = 0; i < 64; i++) { |
| 303 | short f, b; |
| 304 | pair_content(i, &f, &b); |
| 305 | attron(COLOR_PAIR(i)); |
| 306 | printw("%u: %u (%s) ", i, f, get_value_string(col_strs, f)); |
| 307 | printw("%u (%s)\n\r", b, get_value_string(col_strs, b)); |
| 308 | } |
| 309 | refresh(); |
| 310 | getch(); |
| 311 | exit(0); |
| 312 | #endif |
| 313 | |
Harald Welte | b4771a6 | 2012-11-11 10:58:51 +0100 | [diff] [blame] | 314 | g_st.udp_ofd.cb = udp_fd_cb; |
| 315 | rc = osmo_sock_init_ofd(&g_st.udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND); |
| 316 | if (rc < 0) |
| 317 | exit(1); |
| 318 | |
| 319 | while (1) { |
| 320 | osmo_select_main(0); |
| 321 | update_sliders(); |
| 322 | }; |
| 323 | |
| 324 | exit(0); |
| 325 | } |