Alexander Chemeris | b36e885 | 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> |
| 37 | |
| 38 | #include <osmocom/gsm/gsm_utils.h> |
| 39 | |
| 40 | #include <openbsc/gsm_data.h> |
| 41 | #include <openbsc/gsm_data_shared.h> |
| 42 | #include <openbsc/meas_feed.h> |
| 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 | 48d8640 | 2017-02-25 13:46:37 +0100 | [diff] [blame] | 92 | printf("{\"IDX\":%u, \"ARFCN\":%u, \"BSIC\":%u, \"POWER\":%d}", |
Alexander Chemeris | b36e885 | 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 | 48d8640 | 2017-02-25 13:46:37 +0100 | [diff] [blame] | 154 | return 0; |
Alexander Chemeris | b36e885 | 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 | |
| 161 | if (what & BSC_FD_READ) { |
| 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 | |
| 175 | int main(int argc, char **argv) |
| 176 | { |
| 177 | int rc; |
| 178 | struct osmo_fd udp_ofd; |
| 179 | |
| 180 | udp_ofd.cb = udp_fd_cb; |
| 181 | rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND); |
| 182 | if (rc < 0) |
| 183 | exit(1); |
| 184 | |
| 185 | while (1) { |
| 186 | osmo_select_main(0); |
| 187 | }; |
| 188 | |
| 189 | exit(0); |
| 190 | } |