1 /* Conversion of logged cells to KML file */
3 /* (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #define GSM_TA_M 553.85
31 #define PI 3.1415926536
33 #include <osmocom/bb/common/osmocom_data.h>
34 #include <osmocom/bb/common/networks.h>
35 #include <osmocom/bb/common/logging.h>
42 * structure of power and cell infos
46 struct sysinfo sysinfo;
47 static struct node_power *node_power_first = NULL;
48 static struct node_power **node_power_last_p = &node_power_first;
49 struct node_mcc *node_mcc_first = NULL;
50 int log_lines = 0, log_debug = 0;
53 static void nomem(void)
55 fprintf(stderr, "No mem!\n");
59 static void add_power()
61 struct node_power *node_power;
63 // printf("New Power\n");
64 /* append or insert to list */
65 node_power = calloc(1, sizeof(struct node_power));
68 *node_power_last_p = node_power;
69 node_power_last_p = &node_power->next;
70 memcpy(&node_power->power, &power, sizeof(power));
73 static void print_si(void *priv, const char *fmt, ...)
76 FILE *outfp = (FILE *)priv;
80 vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
81 buffer[sizeof(buffer) - 1] = '\0';
85 fprintf(outfp, "%s", buffer);
88 static void add_sysinfo()
90 struct gsm48_sysinfo s;
94 struct node_cell *cell;
95 struct node_meas *meas;
97 memset(&s, 0, sizeof(s));
101 gsm48_decode_sysinfo1(&s,
102 (struct gsm48_system_information_type_1 *) sysinfo.si1,
105 gsm48_decode_sysinfo2(&s,
106 (struct gsm48_system_information_type_2 *) sysinfo.si2,
108 if (sysinfo.si2bis[2])
109 gsm48_decode_sysinfo2bis(&s,
110 (struct gsm48_system_information_type_2bis *)
113 if (sysinfo.si2ter[2])
114 gsm48_decode_sysinfo2ter(&s,
115 (struct gsm48_system_information_type_2ter *)
119 gsm48_decode_sysinfo3(&s,
120 (struct gsm48_system_information_type_3 *) sysinfo.si3,
123 gsm48_decode_sysinfo4(&s,
124 (struct gsm48_system_information_type_4 *) sysinfo.si4,
126 printf("--------------------------------------------------------------------------\n");
127 gsm48_sysinfo_dump(&s, sysinfo.arfcn, print_si, stdout, NULL);
128 mcc = get_node_mcc(s.mcc);
131 mnc = get_node_mnc(mcc, s.mnc);
134 lac = get_node_lac(mnc, s.lac);
137 cell = get_node_cell(lac, s.cell_id);
140 meas = add_node_meas(cell);
143 if (!cell->content) {
145 memcpy(&cell->sysinfo, &sysinfo, sizeof(sysinfo));
146 memcpy(&cell->s, &s, sizeof(s));
148 if (memcmp(&cell->sysinfo.si1, sysinfo.si1,
149 sizeof(sysinfo.si1))) {
151 fprintf(stderr, "FIXME: the cell changed sysinfo\n");
154 if (memcmp(&cell->sysinfo.si2, sysinfo.si2,
155 sizeof(sysinfo.si2)))
157 if (memcmp(&cell->sysinfo.si2bis, sysinfo.si2bis,
158 sizeof(sysinfo.si2bis)))
160 if (memcmp(&cell->sysinfo.si2ter, sysinfo.si2ter,
161 sizeof(sysinfo.si2ter)))
163 if (memcmp(&cell->sysinfo.si3, sysinfo.si3,
164 sizeof(sysinfo.si3)))
166 if (memcmp(&cell->sysinfo.si4, sysinfo.si4,
167 sizeof(sysinfo.si4)))
172 void kml_header(FILE *outfp, char *name)
175 fprintf(outfp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
178 fprintf(outfp, "<kml xmlns=\"http://www.opengis.net/kml/2.2\" "
179 "xmlns:gx=\"http://www.google.com/kml/ext/2.2\" "
180 "xmlns:kml=\"http://www.opengis.net/kml/2.2\" "
181 "xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
183 /* document open tag */
184 fprintf(outfp, "<Document>\n");
187 fprintf(outfp, "\t<Style id=\"sn_placemark_red_pushpin\">\n");
188 fprintf(outfp, "\t\t<IconStyle>\n");
189 fprintf(outfp, "\t\t\t<scale>1.1</scale>\n");
190 fprintf(outfp, "\t\t\t<Icon>\n");
191 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
192 "pushpin/red-pushpin.png</href>\n");
193 fprintf(outfp, "\t\t\t</Icon>\n");
194 fprintf(outfp, "\t\t</IconStyle>\n");
195 fprintf(outfp, "\t\t<ListStyle>\n");
196 fprintf(outfp, "\t\t</ListStyle>\n");
197 fprintf(outfp, "\t</Style>\n");
198 fprintf(outfp, "\t<Style id=\"sh_placemark_red_pushpin_highlight\">\n");
199 fprintf(outfp, "\t\t<IconStyle>\n");
200 fprintf(outfp, "\t\t\t<scale>1.3</scale>\n");
201 fprintf(outfp, "\t\t\t<Icon>\n");
202 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
203 "pushpin/red-pushpin.png</href>\n");
204 fprintf(outfp, "\t\t\t</Icon>\n");
205 fprintf(outfp, "\t\t</IconStyle>\n");
206 fprintf(outfp, "\t\t<ListStyle>\n");
207 fprintf(outfp, "\t\t</ListStyle>\n");
208 fprintf(outfp, "\t</Style>\n");
209 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_red_pushpin\">\n");
210 fprintf(outfp, "\t\t<Pair>\n");
211 fprintf(outfp, "\t\t\t<key>normal</key>\n");
212 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_red_pushpin"
214 fprintf(outfp, "\t\t</Pair>\n");
215 fprintf(outfp, "\t\t<Pair>\n");
216 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
217 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_red_pushpin_highlight"
219 fprintf(outfp, "\t\t</Pair>\n");
220 fprintf(outfp, "\t</StyleMap>\n");
222 fprintf(outfp, "\t<Style id=\"sn_placemark_grn_pushpin\">\n");
223 fprintf(outfp, "\t\t<IconStyle>\n");
224 fprintf(outfp, "\t\t\t<scale>1.1</scale>\n");
225 fprintf(outfp, "\t\t\t<Icon>\n");
226 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
227 "pushpin/grn-pushpin.png</href>\n");
228 fprintf(outfp, "\t\t\t</Icon>\n");
229 fprintf(outfp, "\t\t</IconStyle>\n");
230 fprintf(outfp, "\t\t<ListStyle>\n");
231 fprintf(outfp, "\t\t</ListStyle>\n");
232 fprintf(outfp, "\t</Style>\n");
233 fprintf(outfp, "\t<Style id=\"sh_placemark_grn_pushpin_highlight\">\n");
234 fprintf(outfp, "\t\t<IconStyle>\n");
235 fprintf(outfp, "\t\t\t<scale>1.3</scale>\n");
236 fprintf(outfp, "\t\t\t<Icon>\n");
237 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
238 "pushpin/grn-pushpin.png</href>\n");
239 fprintf(outfp, "\t\t\t</Icon>\n");
240 fprintf(outfp, "\t\t</IconStyle>\n");
241 fprintf(outfp, "\t\t<ListStyle>\n");
242 fprintf(outfp, "\t\t</ListStyle>\n");
243 fprintf(outfp, "\t</Style>\n");
244 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_grn_pushpin\">\n");
245 fprintf(outfp, "\t\t<Pair>\n");
246 fprintf(outfp, "\t\t\t<key>normal</key>\n");
247 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_grn_pushpin"
249 fprintf(outfp, "\t\t</Pair>\n");
250 fprintf(outfp, "\t\t<Pair>\n");
251 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
252 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_grn_pushpin_highlight"
254 fprintf(outfp, "\t\t</Pair>\n");
255 fprintf(outfp, "\t</StyleMap>\n");
258 fprintf(outfp, "\t<Style id=\"sn_placemark_circle\">\n");
259 fprintf(outfp, "\t\t<IconStyle>\n");
260 fprintf(outfp, "\t\t\t<scale>1.0</scale>\n");
261 fprintf(outfp, "\t\t\t<Icon>\n");
262 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
263 "shapes/placemark_circle.png</href>\n");
264 fprintf(outfp, "\t\t\t</Icon>\n");
265 fprintf(outfp, "\t\t</IconStyle>\n");
266 fprintf(outfp, "\t\t<ListStyle>\n");
267 fprintf(outfp, "\t\t</ListStyle>\n");
268 fprintf(outfp, "\t</Style>\n");
269 fprintf(outfp, "\t<Style id=\"sh_placemark_circle_highlight\">\n");
270 fprintf(outfp, "\t\t<IconStyle>\n");
271 fprintf(outfp, "\t\t\t<scale>1.2</scale>\n");
272 fprintf(outfp, "\t\t\t<Icon>\n");
273 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
274 "shapes/placemark_circle_highlight.png</href>\n");
275 fprintf(outfp, "\t\t\t</Icon>\n");
276 fprintf(outfp, "\t\t</IconStyle>\n");
277 fprintf(outfp, "\t\t<ListStyle>\n");
278 fprintf(outfp, "\t\t</ListStyle>\n");
279 fprintf(outfp, "\t</Style>\n");
280 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_circle\">\n");
281 fprintf(outfp, "\t\t<Pair>\n");
282 fprintf(outfp, "\t\t\t<key>normal</key>\n");
283 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_circle</styleUrl>\n");
284 fprintf(outfp, "\t\t</Pair>\n");
285 fprintf(outfp, "\t\t<Pair>\n");
286 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
287 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_circle_highlight"
289 fprintf(outfp, "\t\t</Pair>\n");
290 fprintf(outfp, "\t</StyleMap>\n");
293 void kml_footer(FILE *outfp)
295 /* document close tag */
296 fprintf(outfp, "</Document>\n");
299 fprintf(outfp, "</kml>\n");
303 void kml_meas(FILE *outfp, struct node_meas *meas, int n, uint16_t mcc,
304 uint16_t mnc, uint16_t lac, uint16_t cellid)
306 struct tm *tm = localtime(&meas->gmt);
308 fprintf(outfp, "\t\t\t\t\t<Placemark>\n");
309 fprintf(outfp, "\t\t\t\t\t\t<name>%d: %d</name>\n", n, meas->rxlev);
310 fprintf(outfp, "\t\t\t\t\t\t<description>\n");
311 fprintf(outfp, "MCC=%s MNC=%s\nLAC=%04x CELL-ID=%04x\n(%s %s)\n",
312 gsm_print_mcc(mcc), gsm_print_mnc(mnc), lac, cellid,
313 gsm_get_mcc(mcc), gsm_get_mnc(mcc, mnc));
314 fprintf(outfp, "\n%s", asctime(tm));
315 fprintf(outfp, "RX-LEV %d dBm\n", meas->rxlev);
317 fprintf(outfp, "TA=%d (%d-%d meter)\n", meas->ta,
318 (int)(GSM_TA_M * meas->ta),
319 (int)(GSM_TA_M * (meas->ta + 1)));
320 fprintf(outfp, "\t\t\t\t\t\t</description>\n");
321 fprintf(outfp, "\t\t\t\t\t\t<LookAt>\n");
322 fprintf(outfp, "\t\t\t\t\t\t\t<longitude>%.8f</longitude>\n",
324 fprintf(outfp, "\t\t\t\t\t\t\t<latitude>%.8f</latitude>\n",
326 fprintf(outfp, "\t\t\t\t\t\t\t<altitude>0</altitude>\n");
327 fprintf(outfp, "\t\t\t\t\t\t\t<tilt>0</tilt>\n");
328 fprintf(outfp, "\t\t\t\t\t\t\t<altitudeMode>relativeToGround"
329 "</altitudeMode>\n");
330 fprintf(outfp, "\t\t\t\t\t\t\t<gx:altitudeMode>relativeToSeaFloor"
331 "</gx:altitudeMode>\n");
332 fprintf(outfp, "\t\t\t\t\t\t</LookAt>\n");
333 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_circle"
335 fprintf(outfp, "\t\t\t\t\t\t<Point>\n");
336 fprintf(outfp, "\t\t\t\t\t\t\t<coordinates>%.8f,%.8f</coordinates>\n",
337 meas->longitude, meas->latitude);
338 fprintf(outfp, "\t\t\t\t\t\t</Point>\n");
339 fprintf(outfp, "\t\t\t\t\t</Placemark>\n");
342 double debug_long, debug_lat, debug_x_scale;
345 void kml_cell(FILE *outfp, struct node_cell *cell)
347 struct node_meas *meas;
348 double x, y, z, sum_x = 0, sum_y = 0, sum_z = 0, longitude, latitude;
354 if (meas->gps_valid && meas->ta_valid) {
355 geo2space(&x, &y, &z, meas->longitude, meas->latitude);
369 space2geo(&longitude, &latitude, x, y, z);
371 struct probe *probe_first = NULL, *probe,
372 **probe_last_p = &probe_first;
375 /* translate to flat surface */
377 x_scale = 1.0 / cos(meas->latitude / 180.0 * PI);
378 longitude = meas->longitude;
379 latitude = meas->latitude;
380 debug_x_scale = x_scale;
381 debug_long = longitude;
382 debug_lat = latitude;
385 if (meas->gps_valid && meas->ta_valid) {
386 probe = calloc(1, sizeof(struct probe));
389 probe->x = (meas->longitude - longitude) /
395 probe->y = meas->latitude - latitude;
396 probe->dist = GSM_TA_M * (0.5 +
398 (EQUATOR_RADIUS * PI / 180.0);
399 *probe_last_p = probe;
400 probe_last_p = &probe->next;
406 locate_cell(probe_first, &x, &y);
408 /* translate from flat surface */
409 longitude += x * x_scale;
412 else if (longitude >= 360)
417 while (probe_first) {
419 probe_first = probe->next;
429 fprintf(outfp, "\t\t\t\t\t<Placemark>\n");
430 fprintf(outfp, "\t\t\t\t\t\t<name>MCC=%s MNC=%s\nLAC=%04x "
431 "CELL-ID=%04x\n(%s %s)</name>\n", gsm_print_mcc(cell->s.mcc),
432 gsm_print_mnc(cell->s.mnc), cell->s.lac, cell->s.cell_id,
433 gsm_get_mcc(cell->s.mcc),
434 gsm_get_mnc(cell->s.mcc, cell->s.mnc));
435 fprintf(outfp, "\t\t\t\t\t\t<description>\n");
436 gsm48_sysinfo_dump(&cell->s, cell->sysinfo.arfcn, print_si, outfp,
438 fprintf(outfp, "\t\t\t\t\t\t</description>\n");
439 fprintf(outfp, "\t\t\t\t\t\t<LookAt>\n");
440 fprintf(outfp, "\t\t\t\t\t\t\t<longitude>%.8f</longitude>\n",
442 fprintf(outfp, "\t\t\t\t\t\t\t<latitude>%.8f</latitude>\n", latitude);
443 fprintf(outfp, "\t\t\t\t\t\t\t<altitude>0</altitude>\n");
444 fprintf(outfp, "\t\t\t\t\t\t\t<tilt>0</tilt>\n");
445 fprintf(outfp, "\t\t\t\t\t\t\t<altitudeMode>relativeToGround"
446 "</altitudeMode>\n");
447 fprintf(outfp, "\t\t\t\t\t\t\t<gx:altitudeMode>relativeToSeaFloor"
448 "</gx:altitudeMode>\n");
449 fprintf(outfp, "\t\t\t\t\t\t</LookAt>\n");
451 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_grn_"
452 "pushpin</styleUrl>\n");
454 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_red_"
455 "pushpin</styleUrl>\n");
456 fprintf(outfp, "\t\t\t\t\t\t<Point>\n");
457 fprintf(outfp, "\t\t\t\t\t\t\t<coordinates>%.8f,%.8f</coordinates>\n",
458 longitude, latitude);
459 fprintf(outfp, "\t\t\t\t\t\t</Point>\n");
460 fprintf(outfp, "\t\t\t\t\t</Placemark>\n");
465 fprintf(outfp, "\t<Folder>\n");
466 fprintf(outfp, "\t\t<name>Lines</name>\n");
467 fprintf(outfp, "\t\t<open>0</open>\n");
468 fprintf(outfp, "\t\t<visibility>0</visibility>\n");
470 geo2space(&x, &y, &z, longitude, latitude);
474 if (meas->gps_valid) {
475 double mx, my, mz, dist;
477 geo2space(&mx, &my, &mz, meas->longitude,
479 dist = distinspace(x, y, z, mx, my, mz);
480 fprintf(outfp, "\t\t<Placemark>\n");
481 fprintf(outfp, "\t\t\t<name>Range</name>\n");
482 fprintf(outfp, "\t\t\t<description>\n");
483 fprintf(outfp, "Distance: %d\n", (int)dist);
484 fprintf(outfp, "TA=%d (%d-%d meter)\n", meas->ta,
485 (int)(GSM_TA_M * meas->ta),
486 (int)(GSM_TA_M * (meas->ta + 1)));
487 fprintf(outfp, "\t\t\t</description>\n");
488 fprintf(outfp, "\t\t\t<visibility>0</visibility>\n");
489 fprintf(outfp, "\t\t\t<LineString>\n");
490 fprintf(outfp, "\t\t\t\t<tessellate>1</tessellate>\n");
491 fprintf(outfp, "\t\t\t\t<coordinates>\n");
492 fprintf(outfp, "%.8f,%.8f\n", longitude, latitude);
493 fprintf(outfp, "%.8f,%.8f\n", meas->longitude,
495 fprintf(outfp, "\t\t\t\t</coordinates>\n");
496 fprintf(outfp, "\t\t\t</LineString>\n");
497 fprintf(outfp, "\t\t</Placemark>\n");
501 fprintf(outfp, "\t</Folder>\n");
504 struct log_target *stderr_target;
506 int main(int argc, char *argv[])
511 struct node_mcc *mcc;
512 struct node_mnc *mnc;
513 struct node_lac *lac;
514 struct node_cell *cell;
515 struct node_meas *meas;
517 log_init(&log_info, NULL);
518 stderr_target = log_target_create_stderr();
519 log_add_target(stderr_target);
520 log_set_all_filter(stderr_target, 1);
521 log_parse_category_mask(stderr_target, "Dxxx");
522 log_set_log_level(stderr_target, LOGL_INFO);
526 fprintf(stderr, "Usage: %s <file.log> <file.kml> "
527 "[lines] [debug]\n", argv[0]);
528 fprintf(stderr, "lines: Add lines between cell and "
529 "Measurement point\n");
530 fprintf(stderr, "debug: Add debugging of location algorithm.\n"
535 for (i = 3; i < argc; i++) {
536 if (!strcmp(argv[i], "lines"))
538 else if (!strcmp(argv[i], "debug"))
543 infp = fopen(argv[1], "r");
545 fprintf(stderr, "Failed to open '%s' for reading\n", argv[1]);
549 while ((type = read_log(infp))) {
551 case LOG_TYPE_SYSINFO:
562 if (!strcmp(argv[2], "-"))
565 outfp = fopen(argv[2], "w");
567 fprintf(stderr, "Failed to open '%s' for writing\n", argv[2]);
573 while (strchr(p, '/'))
574 p = strchr(p, '/') + 1;
576 kml_header(outfp, p);
577 mcc = node_mcc_first;
579 printf("MCC: %02x\n", mcc->mcc);
581 fprintf(outfp, "\t<Folder>\n");
582 fprintf(outfp, "\t\t<name>MCC %s (%s)</name>\n",
583 gsm_print_mcc(mcc->mcc), gsm_get_mcc(mcc->mcc));
584 fprintf(outfp, "\t\t<open>0</open>\n");
587 printf(" MNC: %02x\n", mnc->mnc);
589 fprintf(outfp, "\t\t<Folder>\n");
590 fprintf(outfp, "\t\t\t<name>MNC %s (%s)</name>\n",
591 gsm_print_mnc(mnc->mnc), gsm_get_mnc(mcc->mcc, mnc->mnc));
592 fprintf(outfp, "\t\t\t<open>0</open>\n");
595 printf(" LAC: %04x\n", lac->lac);
597 fprintf(outfp, "\t\t\t<Folder>\n");
598 fprintf(outfp, "\t\t\t\t<name>LAC %04x</name>\n", lac->lac);
599 fprintf(outfp, "\t\t\t\t<open>0</open>\n");
602 printf(" CELL: %04x\n", cell->cellid);
603 fprintf(outfp, "\t\t\t\t<Folder>\n");
604 fprintf(outfp, "\t\t\t\t\t<name>CELL-ID %04x</name>\n", cell->cellid);
605 fprintf(outfp, "\t\t\t\t\t<open>0</open>\n");
610 printf(" TA: %d\n", meas->ta);
612 kml_meas(outfp, meas, ++n, mcc->mcc, mnc->mnc,
613 lac->lac, cell->cellid);
616 kml_cell(outfp, cell);
618 fprintf(outfp, "\t\t\t\t</Folder>\n");
622 fprintf(outfp, "\t\t\t</Folder>\n");
626 fprintf(outfp, "\t\t</Folder>\n");
630 fprintf(outfp, "\t</Folder>\n");
636 fprintf(outfp, "\t<Folder>\n");
637 fprintf(outfp, "\t\t<name>Power</name>\n");
638 fprintf(outfp, "\t\t<open>0</open>\n");
639 power = node_power_first;
643 fprintf(outfp, "\t\t<Folder>\n");
644 fprintf(outfp, "\t\t\t<name>Power %d</name>\n", ++n);
645 fprintf(outfp, "\t\t\t<open>0</open>\n");
647 fprintf(outfp, "\t\t</Folder>\n");
651 fprintf(outfp, "\t</Folder>\n");