Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 1 | /* Convert measurement report feed into JSON feed printed to stdout. |
| 2 | * Each measurement report is printed as a separae JSON root entry. |
| 3 | * All measurement reports are separated by a new line. |
| 4 | */ |
| 5 | |
| 6 | /* (C) 2015 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co> |
| 7 | * With parts of code adopted from different places in OpenBSC. |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Affero General Public License as published by |
| 13 | * the Free Software Foundation; either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Affero General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Affero General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | */ |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <time.h> |
| 31 | |
| 32 | #include <netinet/in.h> |
| 33 | |
| 34 | #include <osmocom/core/socket.h> |
| 35 | #include <osmocom/core/msgb.h> |
| 36 | #include <osmocom/core/select.h> |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame] | 37 | #include <osmocom/core/application.h> |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 38 | |
| 39 | #include <osmocom/gsm/gsm_utils.h> |
| 40 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 41 | #include <osmocom/bsc/gsm_data.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 42 | #include <osmocom/bsc/meas_feed.h> |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 43 | |
| 44 | static void print_meas_rep_uni_json(struct gsm_meas_rep_unidir *mru) |
| 45 | { |
| 46 | printf("\"RXL-FULL\":%d, \"RXL-SUB\":%d, ", |
| 47 | rxlev2dbm(mru->full.rx_lev), |
| 48 | rxlev2dbm(mru->sub.rx_lev)); |
| 49 | printf("\"RXQ-FULL\":%d, \"RXQ-SUB\":%d", |
| 50 | mru->full.rx_qual, mru->sub.rx_qual); |
| 51 | } |
| 52 | |
| 53 | static void print_meas_rep_json(struct gsm_meas_rep *mr) |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | printf("\"NR\":%d", mr->nr); |
| 58 | |
| 59 | if (mr->flags & MEAS_REP_F_DL_DTX) |
| 60 | printf(", \"DTXd\":true"); |
| 61 | |
| 62 | printf(", \"UL_MEAS\":{"); |
| 63 | print_meas_rep_uni_json(&mr->ul); |
| 64 | printf("}"); |
| 65 | printf(", \"BS_POWER\":%d", mr->bs_power); |
| 66 | if (mr->flags & MEAS_REP_F_MS_TO) |
| 67 | printf(", \"MS_TO\":%d", mr->ms_timing_offset); |
| 68 | |
| 69 | if (mr->flags & MEAS_REP_F_MS_L1) { |
| 70 | printf(", \"L1_MS_PWR\":%d", mr->ms_l1.pwr); |
| 71 | printf(", \"L1_FPC\":%s", |
| 72 | mr->flags & MEAS_REP_F_FPC ? "true" : "false"); |
| 73 | printf(", \"L1_TA\":%u", mr->ms_l1.ta); |
| 74 | } |
| 75 | |
| 76 | if (mr->flags & MEAS_REP_F_UL_DTX) |
| 77 | printf(", \"DTXu\":true"); |
| 78 | if (mr->flags & MEAS_REP_F_BA1) |
| 79 | printf(", \"BA1\":true"); |
| 80 | if (mr->flags & MEAS_REP_F_DL_VALID) { |
| 81 | printf(", \"DL_MEAS\":{"); |
| 82 | print_meas_rep_uni_json(&mr->dl); |
| 83 | printf("}"); |
| 84 | } |
| 85 | |
| 86 | if (mr->num_cell == 7) |
| 87 | return; |
| 88 | printf(", \"NUM_NEIGH\":%u, \"NEIGH\":[", mr->num_cell); |
| 89 | for (i = 0; i < mr->num_cell; i++) { |
| 90 | struct gsm_meas_rep_cell *mrc = &mr->cell[i]; |
| 91 | if (i!=0) printf(", "); |
Keith | f01bd13 | 2017-02-25 13:46:37 +0100 | [diff] [blame] | 92 | printf("{\"IDX\":%u, \"ARFCN\":%u, \"BSIC\":%u, \"POWER\":%d}", |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 93 | mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev)); |
| 94 | } |
| 95 | printf("]"); |
| 96 | } |
| 97 | |
| 98 | static void print_chan_info_json(struct meas_feed_meas *mfm) |
| 99 | { |
| 100 | printf("\"lchan_type\":\"%s\", \"pchan_type\":\"%s\", " |
| 101 | "\"bts_nr\":%d, \"trx_nr\":%d, \"ts_nr\":%d, \"ss_nr\":%d", |
| 102 | gsm_lchant_name(mfm->lchan_type), gsm_pchan_name(mfm->pchan_type), |
| 103 | mfm->bts_nr, mfm->trx_nr, mfm->ts_nr, mfm->ss_nr); |
| 104 | } |
| 105 | |
| 106 | static void print_meas_feed_json(struct meas_feed_meas *mfm) |
| 107 | { |
| 108 | time_t now = time(NULL); |
| 109 | |
| 110 | printf("{"); |
| 111 | printf("\"time\":%ld, \"imsi\":\"%s\", \"name\":\"%s\", \"scenario\":\"%s\", ", |
| 112 | now, mfm->imsi, mfm->name, mfm->scenario); |
| 113 | |
| 114 | switch (mfm->hdr.version) { |
| 115 | case 1: |
| 116 | printf("\"chan_info\":{"); |
| 117 | print_chan_info_json(mfm); |
| 118 | printf("}, "); |
| 119 | /* no break, fall to version 0 */ |
| 120 | case 0: |
| 121 | printf("\"meas_rep\":{"); |
| 122 | print_meas_rep_json(&mfm->mr); |
| 123 | printf("}"); |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | printf("}\n"); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | static int handle_meas(struct msgb *msg) |
| 132 | { |
| 133 | struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg); |
| 134 | |
| 135 | print_meas_feed_json(mfm); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int handle_msg(struct msgb *msg) |
| 141 | { |
| 142 | struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg); |
| 143 | |
| 144 | if (mfh->version != MEAS_FEED_VERSION) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | switch (mfh->msg_type) { |
| 148 | case MEAS_FEED_MEAS: |
| 149 | handle_meas(msg); |
| 150 | break; |
| 151 | default: |
| 152 | break; |
| 153 | } |
Keith | f01bd13 | 2017-02-25 13:46:37 +0100 | [diff] [blame] | 154 | return 0; |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 158 | { |
| 159 | int rc; |
| 160 | |
Pau Espin Pedrol | 8e9f40a | 2020-05-09 19:17:12 +0200 | [diff] [blame] | 161 | if (what & OSMO_FD_READ) { |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 162 | struct msgb *msg = msgb_alloc(1024, "UDP Rx"); |
| 163 | |
| 164 | rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg)); |
| 165 | if (rc < 0) |
| 166 | return rc; |
| 167 | msgb_put(msg, rc); |
| 168 | handle_msg(msg); |
| 169 | msgb_free(msg); |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame] | 175 | /* default categories */ |
| 176 | static struct log_info_cat default_categories[] = { |
| 177 | }; |
| 178 | |
| 179 | static const struct log_info meas_json_log_info = { |
| 180 | .cat = default_categories, |
| 181 | .num_cat = ARRAY_SIZE(default_categories), |
| 182 | }; |
| 183 | |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 184 | int main(int argc, char **argv) |
| 185 | { |
Keith | 85fe610 | 2020-03-27 19:43:34 -0500 | [diff] [blame] | 186 | |
| 187 | void *tall_ctx = talloc_named_const(NULL, 0, "meas_json"); |
| 188 | osmo_init_logging2(tall_ctx, &meas_json_log_info); |
| 189 | |
Alexander Chemeris | c634063 | 2015-06-10 18:55:28 -0400 | [diff] [blame] | 190 | int rc; |
| 191 | struct osmo_fd udp_ofd; |
| 192 | |
| 193 | udp_ofd.cb = udp_fd_cb; |
| 194 | rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND); |
| 195 | if (rc < 0) |
| 196 | exit(1); |
| 197 | |
| 198 | while (1) { |
| 199 | osmo_select_main(0); |
| 200 | }; |
| 201 | |
| 202 | exit(0); |
| 203 | } |
Neels Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 204 | |
| 205 | void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} |
| 206 | int abis_rsl_rcvmsg(struct msgb *msg) { return 0; } |
Pau Espin Pedrol | f8d0389 | 2019-11-12 16:30:30 +0100 | [diff] [blame] | 207 | int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan) { return 0; } |
Pau Espin Pedrol | 8d4f94a | 2020-07-16 15:17:20 +0200 | [diff] [blame] | 208 | void pcu_info_update(struct gsm_bts *bts) {}; |
| 209 | int rsl_sacch_filling(struct gsm_bts_trx *trx, uint8_t type, const uint8_t *data, int len) { return 0; } |
| 210 | int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len) |
| 211 | { return 0; } |
| 212 | int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type) { return 0; } |