blob: 365b450f4badc5e44c596ce57458a5afc89e5edd [file] [log] [blame]
Alexander Chemerisc6340632015-06-10 18:55:28 -04001/* 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
Neels Hofmeyrc0164792017-09-04 15:15:32 +020040#include <osmocom/bsc/gsm_data.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020041#include <osmocom/bsc/meas_feed.h>
Alexander Chemerisc6340632015-06-10 18:55:28 -040042
43static void print_meas_rep_uni_json(struct gsm_meas_rep_unidir *mru)
44{
45 printf("\"RXL-FULL\":%d, \"RXL-SUB\":%d, ",
46 rxlev2dbm(mru->full.rx_lev),
47 rxlev2dbm(mru->sub.rx_lev));
48 printf("\"RXQ-FULL\":%d, \"RXQ-SUB\":%d",
49 mru->full.rx_qual, mru->sub.rx_qual);
50}
51
52static void print_meas_rep_json(struct gsm_meas_rep *mr)
53{
54 int i;
55
56 printf("\"NR\":%d", mr->nr);
57
58 if (mr->flags & MEAS_REP_F_DL_DTX)
59 printf(", \"DTXd\":true");
60
61 printf(", \"UL_MEAS\":{");
62 print_meas_rep_uni_json(&mr->ul);
63 printf("}");
64 printf(", \"BS_POWER\":%d", mr->bs_power);
65 if (mr->flags & MEAS_REP_F_MS_TO)
66 printf(", \"MS_TO\":%d", mr->ms_timing_offset);
67
68 if (mr->flags & MEAS_REP_F_MS_L1) {
69 printf(", \"L1_MS_PWR\":%d", mr->ms_l1.pwr);
70 printf(", \"L1_FPC\":%s",
71 mr->flags & MEAS_REP_F_FPC ? "true" : "false");
72 printf(", \"L1_TA\":%u", mr->ms_l1.ta);
73 }
74
75 if (mr->flags & MEAS_REP_F_UL_DTX)
76 printf(", \"DTXu\":true");
77 if (mr->flags & MEAS_REP_F_BA1)
78 printf(", \"BA1\":true");
79 if (mr->flags & MEAS_REP_F_DL_VALID) {
80 printf(", \"DL_MEAS\":{");
81 print_meas_rep_uni_json(&mr->dl);
82 printf("}");
83 }
84
85 if (mr->num_cell == 7)
86 return;
87 printf(", \"NUM_NEIGH\":%u, \"NEIGH\":[", mr->num_cell);
88 for (i = 0; i < mr->num_cell; i++) {
89 struct gsm_meas_rep_cell *mrc = &mr->cell[i];
90 if (i!=0) printf(", ");
Keithf01bd132017-02-25 13:46:37 +010091 printf("{\"IDX\":%u, \"ARFCN\":%u, \"BSIC\":%u, \"POWER\":%d}",
Alexander Chemerisc6340632015-06-10 18:55:28 -040092 mrc->neigh_idx, mrc->arfcn, mrc->bsic, rxlev2dbm(mrc->rxlev));
93 }
94 printf("]");
95}
96
97static void print_chan_info_json(struct meas_feed_meas *mfm)
98{
99 printf("\"lchan_type\":\"%s\", \"pchan_type\":\"%s\", "
100 "\"bts_nr\":%d, \"trx_nr\":%d, \"ts_nr\":%d, \"ss_nr\":%d",
101 gsm_lchant_name(mfm->lchan_type), gsm_pchan_name(mfm->pchan_type),
102 mfm->bts_nr, mfm->trx_nr, mfm->ts_nr, mfm->ss_nr);
103}
104
105static void print_meas_feed_json(struct meas_feed_meas *mfm)
106{
107 time_t now = time(NULL);
108
109 printf("{");
110 printf("\"time\":%ld, \"imsi\":\"%s\", \"name\":\"%s\", \"scenario\":\"%s\", ",
111 now, mfm->imsi, mfm->name, mfm->scenario);
112
113 switch (mfm->hdr.version) {
114 case 1:
115 printf("\"chan_info\":{");
116 print_chan_info_json(mfm);
117 printf("}, ");
118 /* no break, fall to version 0 */
119 case 0:
120 printf("\"meas_rep\":{");
121 print_meas_rep_json(&mfm->mr);
122 printf("}");
123 break;
124 }
125
126 printf("}\n");
127
128}
129
130static int handle_meas(struct msgb *msg)
131{
132 struct meas_feed_meas *mfm = (struct meas_feed_meas *) msgb_data(msg);
133
134 print_meas_feed_json(mfm);
135
136 return 0;
137}
138
139static int handle_msg(struct msgb *msg)
140{
141 struct meas_feed_hdr *mfh = (struct meas_feed_hdr *) msgb_data(msg);
142
143 if (mfh->version != MEAS_FEED_VERSION)
144 return -EINVAL;
145
146 switch (mfh->msg_type) {
147 case MEAS_FEED_MEAS:
148 handle_meas(msg);
149 break;
150 default:
151 break;
152 }
Keithf01bd132017-02-25 13:46:37 +0100153 return 0;
Alexander Chemerisc6340632015-06-10 18:55:28 -0400154}
155
156static int udp_fd_cb(struct osmo_fd *ofd, unsigned int what)
157{
158 int rc;
159
160 if (what & BSC_FD_READ) {
161 struct msgb *msg = msgb_alloc(1024, "UDP Rx");
162
163 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
164 if (rc < 0)
165 return rc;
166 msgb_put(msg, rc);
167 handle_msg(msg);
168 msgb_free(msg);
169 }
170
171 return 0;
172}
173
174int main(int argc, char **argv)
175{
176 int rc;
177 struct osmo_fd udp_ofd;
178
179 udp_ofd.cb = udp_fd_cb;
180 rc = osmo_sock_init_ofd(&udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 8888, OSMO_SOCK_F_BIND);
181 if (rc < 0)
182 exit(1);
183
184 while (1) {
185 osmo_select_main(0);
186 };
187
188 exit(0);
189}
Neels Hofmeyr31f525e2018-05-14 18:14:15 +0200190
191void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {}
192int abis_rsl_rcvmsg(struct msgb *msg) { return 0; }