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>
41 * structure of power and cell infos
45 struct sysinfo sysinfo;
46 static struct node_power *node_power_first = NULL;
47 static struct node_power **node_power_last_p = &node_power_first;
48 struct node_mcc *node_mcc_first = NULL;
49 int log_lines = 0, log_debug = 0;
52 static void nomem(void)
54 fprintf(stderr, "No mem!\n");
58 static void add_power()
60 struct node_power *node_power;
62 // printf("New Power\n");
63 /* append or insert to list */
64 node_power = calloc(1, sizeof(struct node_power));
67 *node_power_last_p = node_power;
68 node_power_last_p = &node_power->next;
69 memcpy(&node_power->power, &power, sizeof(power));
72 static void add_sysinfo()
74 struct gsm48_sysinfo s;
78 struct node_cell *cell;
79 struct node_meas *meas;
81 memset(&s, 0, sizeof(s));
85 gsm48_decode_sysinfo1(&s,
86 (struct gsm48_system_information_type_1 *) sysinfo.si1,
89 gsm48_decode_sysinfo2(&s,
90 (struct gsm48_system_information_type_2 *) sysinfo.si2,
92 if (sysinfo.si2bis[2])
93 gsm48_decode_sysinfo2bis(&s,
94 (struct gsm48_system_information_type_2bis *)
97 if (sysinfo.si2ter[2])
98 gsm48_decode_sysinfo2ter(&s,
99 (struct gsm48_system_information_type_2ter *)
103 gsm48_decode_sysinfo3(&s,
104 (struct gsm48_system_information_type_3 *) sysinfo.si3,
107 gsm48_decode_sysinfo4(&s,
108 (struct gsm48_system_information_type_4 *) sysinfo.si4,
111 mcc = get_node_mcc(s.mcc);
114 mnc = get_node_mnc(mcc, s.mnc);
117 lac = get_node_lac(mnc, s.lac);
120 cell = get_node_cell(lac, s.cell_id);
123 meas = add_node_meas(cell);
126 if (!cell->content) {
128 memcpy(&cell->sysinfo, &sysinfo, sizeof(sysinfo));
129 memcpy(&cell->s, &s, sizeof(s));
131 if (memcmp(&cell->sysinfo.si1, sysinfo.si1,
132 sizeof(sysinfo.si1))) {
134 fprintf(stderr, "FIXME: the cell changed sysinfo\n");
137 if (memcmp(&cell->sysinfo.si2, sysinfo.si2,
138 sizeof(sysinfo.si2)))
140 if (memcmp(&cell->sysinfo.si2bis, sysinfo.si2bis,
141 sizeof(sysinfo.si2bis)))
143 if (memcmp(&cell->sysinfo.si2ter, sysinfo.si2ter,
144 sizeof(sysinfo.si2ter)))
146 if (memcmp(&cell->sysinfo.si3, sysinfo.si3,
147 sizeof(sysinfo.si3)))
149 if (memcmp(&cell->sysinfo.si4, sysinfo.si4,
150 sizeof(sysinfo.si4)))
155 void kml_header(FILE *outfp, char *name)
158 fprintf(outfp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
161 fprintf(outfp, "<kml xmlns=\"http://www.opengis.net/kml/2.2\" "
162 "xmlns:gx=\"http://www.google.com/kml/ext/2.2\" "
163 "xmlns:kml=\"http://www.opengis.net/kml/2.2\" "
164 "xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
166 /* document open tag */
167 fprintf(outfp, "<Document>\n");
170 fprintf(outfp, "\t<Style id=\"sn_placemark_red_pushpin\">\n");
171 fprintf(outfp, "\t\t<IconStyle>\n");
172 fprintf(outfp, "\t\t\t<scale>1.1</scale>\n");
173 fprintf(outfp, "\t\t\t<Icon>\n");
174 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
175 "pushpin/red-pushpin.png</href>\n");
176 fprintf(outfp, "\t\t\t</Icon>\n");
177 fprintf(outfp, "\t\t</IconStyle>\n");
178 fprintf(outfp, "\t\t<ListStyle>\n");
179 fprintf(outfp, "\t\t</ListStyle>\n");
180 fprintf(outfp, "\t</Style>\n");
181 fprintf(outfp, "\t<Style id=\"sh_placemark_red_pushpin_highlight\">\n");
182 fprintf(outfp, "\t\t<IconStyle>\n");
183 fprintf(outfp, "\t\t\t<scale>1.3</scale>\n");
184 fprintf(outfp, "\t\t\t<Icon>\n");
185 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
186 "pushpin/red-pushpin.png</href>\n");
187 fprintf(outfp, "\t\t\t</Icon>\n");
188 fprintf(outfp, "\t\t</IconStyle>\n");
189 fprintf(outfp, "\t\t<ListStyle>\n");
190 fprintf(outfp, "\t\t</ListStyle>\n");
191 fprintf(outfp, "\t</Style>\n");
192 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_red_pushpin\">\n");
193 fprintf(outfp, "\t\t<Pair>\n");
194 fprintf(outfp, "\t\t\t<key>normal</key>\n");
195 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_red_pushpin"
197 fprintf(outfp, "\t\t</Pair>\n");
198 fprintf(outfp, "\t\t<Pair>\n");
199 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
200 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_red_pushpin_highlight"
202 fprintf(outfp, "\t\t</Pair>\n");
203 fprintf(outfp, "\t</StyleMap>\n");
205 fprintf(outfp, "\t<Style id=\"sn_placemark_grn_pushpin\">\n");
206 fprintf(outfp, "\t\t<IconStyle>\n");
207 fprintf(outfp, "\t\t\t<scale>1.1</scale>\n");
208 fprintf(outfp, "\t\t\t<Icon>\n");
209 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
210 "pushpin/grn-pushpin.png</href>\n");
211 fprintf(outfp, "\t\t\t</Icon>\n");
212 fprintf(outfp, "\t\t</IconStyle>\n");
213 fprintf(outfp, "\t\t<ListStyle>\n");
214 fprintf(outfp, "\t\t</ListStyle>\n");
215 fprintf(outfp, "\t</Style>\n");
216 fprintf(outfp, "\t<Style id=\"sh_placemark_grn_pushpin_highlight\">\n");
217 fprintf(outfp, "\t\t<IconStyle>\n");
218 fprintf(outfp, "\t\t\t<scale>1.3</scale>\n");
219 fprintf(outfp, "\t\t\t<Icon>\n");
220 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
221 "pushpin/grn-pushpin.png</href>\n");
222 fprintf(outfp, "\t\t\t</Icon>\n");
223 fprintf(outfp, "\t\t</IconStyle>\n");
224 fprintf(outfp, "\t\t<ListStyle>\n");
225 fprintf(outfp, "\t\t</ListStyle>\n");
226 fprintf(outfp, "\t</Style>\n");
227 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_grn_pushpin\">\n");
228 fprintf(outfp, "\t\t<Pair>\n");
229 fprintf(outfp, "\t\t\t<key>normal</key>\n");
230 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_grn_pushpin"
232 fprintf(outfp, "\t\t</Pair>\n");
233 fprintf(outfp, "\t\t<Pair>\n");
234 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
235 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_grn_pushpin_highlight"
237 fprintf(outfp, "\t\t</Pair>\n");
238 fprintf(outfp, "\t</StyleMap>\n");
241 fprintf(outfp, "\t<Style id=\"sn_placemark_circle\">\n");
242 fprintf(outfp, "\t\t<IconStyle>\n");
243 fprintf(outfp, "\t\t\t<scale>1.0</scale>\n");
244 fprintf(outfp, "\t\t\t<Icon>\n");
245 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
246 "shapes/placemark_circle.png</href>\n");
247 fprintf(outfp, "\t\t\t</Icon>\n");
248 fprintf(outfp, "\t\t</IconStyle>\n");
249 fprintf(outfp, "\t\t<ListStyle>\n");
250 fprintf(outfp, "\t\t</ListStyle>\n");
251 fprintf(outfp, "\t</Style>\n");
252 fprintf(outfp, "\t<Style id=\"sh_placemark_circle_highlight\">\n");
253 fprintf(outfp, "\t\t<IconStyle>\n");
254 fprintf(outfp, "\t\t\t<scale>1.2</scale>\n");
255 fprintf(outfp, "\t\t\t<Icon>\n");
256 fprintf(outfp, "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/"
257 "shapes/placemark_circle_highlight.png</href>\n");
258 fprintf(outfp, "\t\t\t</Icon>\n");
259 fprintf(outfp, "\t\t</IconStyle>\n");
260 fprintf(outfp, "\t\t<ListStyle>\n");
261 fprintf(outfp, "\t\t</ListStyle>\n");
262 fprintf(outfp, "\t</Style>\n");
263 fprintf(outfp, "\t<StyleMap id=\"msn_placemark_circle\">\n");
264 fprintf(outfp, "\t\t<Pair>\n");
265 fprintf(outfp, "\t\t\t<key>normal</key>\n");
266 fprintf(outfp, "\t\t\t<styleUrl>#sn_placemark_circle</styleUrl>\n");
267 fprintf(outfp, "\t\t</Pair>\n");
268 fprintf(outfp, "\t\t<Pair>\n");
269 fprintf(outfp, "\t\t\t<key>highlight</key>\n");
270 fprintf(outfp, "\t\t\t<styleUrl>#sh_placemark_circle_highlight"
272 fprintf(outfp, "\t\t</Pair>\n");
273 fprintf(outfp, "\t</StyleMap>\n");
276 void kml_footer(FILE *outfp)
278 /* document close tag */
279 fprintf(outfp, "</Document>\n");
282 fprintf(outfp, "</kml>\n");
286 void kml_meas(FILE *outfp, struct node_meas *meas, int n, uint16_t mcc,
287 uint16_t mnc, uint16_t lac, uint16_t cellid)
289 struct tm *tm = localtime(&meas->gmt);
291 fprintf(outfp, "\t\t\t\t\t<Placemark>\n");
292 fprintf(outfp, "\t\t\t\t\t\t<name>%d: %d</name>\n", n, meas->rxlev);
293 fprintf(outfp, "\t\t\t\t\t\t<description>\n");
294 fprintf(outfp, "MCC=%s MNC=%s\nLAC=%04x CELL-ID=%04x\n(%s %s)\n",
295 gsm_print_mcc(mcc), gsm_print_mnc(mnc), lac, cellid,
296 gsm_get_mcc(mcc), gsm_get_mnc(mcc, mnc));
297 fprintf(outfp, "\n%s", asctime(tm));
298 fprintf(outfp, "RX-LEV %d dBm\n", meas->rxlev);
300 fprintf(outfp, "TA=%d (%d-%d meter)\n", meas->ta,
301 (int)(GSM_TA_M * meas->ta),
302 (int)(GSM_TA_M * (meas->ta + 1)));
303 fprintf(outfp, "\t\t\t\t\t\t</description>\n");
304 fprintf(outfp, "\t\t\t\t\t\t<LookAt>\n");
305 fprintf(outfp, "\t\t\t\t\t\t\t<longitude>%.8f</longitude>\n",
307 fprintf(outfp, "\t\t\t\t\t\t\t<latitude>%.8f</latitude>\n",
309 fprintf(outfp, "\t\t\t\t\t\t\t<altitude>0</altitude>\n");
310 fprintf(outfp, "\t\t\t\t\t\t\t<tilt>0</tilt>\n");
311 fprintf(outfp, "\t\t\t\t\t\t\t<altitudeMode>relativeToGround"
312 "</altitudeMode>\n");
313 fprintf(outfp, "\t\t\t\t\t\t\t<gx:altitudeMode>relativeToSeaFloor"
314 "</gx:altitudeMode>\n");
315 fprintf(outfp, "\t\t\t\t\t\t</LookAt>\n");
316 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_circle"
318 fprintf(outfp, "\t\t\t\t\t\t<Point>\n");
319 fprintf(outfp, "\t\t\t\t\t\t\t<coordinates>%.8f,%.8f</coordinates>\n",
320 meas->longitude, meas->latitude);
321 fprintf(outfp, "\t\t\t\t\t\t</Point>\n");
322 fprintf(outfp, "\t\t\t\t\t</Placemark>\n");
325 static void print_si(void *priv, const char *fmt, ...)
328 FILE *outfp = (FILE *)priv;
332 vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
333 buffer[sizeof(buffer) - 1] = '\0';
337 fprintf(outfp, "%s", buffer);
340 double debug_long, debug_lat, debug_x_scale;
343 void kml_cell(FILE *outfp, struct node_cell *cell)
345 struct node_meas *meas;
346 double x, y, z, sum_x = 0, sum_y = 0, sum_z = 0, longitude, latitude;
352 if (meas->gps_valid && meas->ta_valid) {
353 geo2space(&x, &y, &z, meas->longitude, meas->latitude);
367 space2geo(&longitude, &latitude, x, y, z);
369 struct probe *probe_first = NULL, *probe,
370 **probe_last_p = &probe_first;
373 /* translate to flat surface */
375 x_scale = 1.0 / cos(meas->latitude / 180.0 * PI);
376 longitude = meas->longitude;
377 latitude = meas->latitude;
378 debug_x_scale = x_scale;
379 debug_long = longitude;
380 debug_lat = latitude;
383 if (meas->gps_valid && meas->ta_valid) {
384 probe = calloc(1, sizeof(struct probe));
387 probe->x = (meas->longitude - longitude) /
393 probe->y = meas->latitude - latitude;
394 probe->dist = GSM_TA_M * (0.5 +
396 (EQUATOR_RADIUS * PI / 180.0);
397 *probe_last_p = probe;
398 probe_last_p = &probe->next;
404 locate_cell(probe_first, &x, &y);
406 /* translate from flat surface */
407 longitude += x * x_scale;
410 else if (longitude >= 360)
415 while (probe_first) {
417 probe_first = probe->next;
427 fprintf(outfp, "\t\t\t\t\t<Placemark>\n");
428 fprintf(outfp, "\t\t\t\t\t\t<name>MCC=%s MNC=%s\nLAC=%04x "
429 "CELL-ID=%04x\n(%s %s)</name>\n", gsm_print_mcc(cell->s.mcc),
430 gsm_print_mnc(cell->s.mnc), cell->s.lac, cell->s.cell_id,
431 gsm_get_mcc(cell->s.mcc),
432 gsm_get_mnc(cell->s.mcc, cell->s.mnc));
433 fprintf(outfp, "\t\t\t\t\t\t<description>\n");
434 gsm48_sysinfo_dump(&cell->s, cell->sysinfo.arfcn, print_si, outfp,
436 fprintf(outfp, "\t\t\t\t\t\t</description>\n");
437 fprintf(outfp, "\t\t\t\t\t\t<LookAt>\n");
438 fprintf(outfp, "\t\t\t\t\t\t\t<longitude>%.8f</longitude>\n",
440 fprintf(outfp, "\t\t\t\t\t\t\t<latitude>%.8f</latitude>\n", latitude);
441 fprintf(outfp, "\t\t\t\t\t\t\t<altitude>0</altitude>\n");
442 fprintf(outfp, "\t\t\t\t\t\t\t<tilt>0</tilt>\n");
443 fprintf(outfp, "\t\t\t\t\t\t\t<altitudeMode>relativeToGround"
444 "</altitudeMode>\n");
445 fprintf(outfp, "\t\t\t\t\t\t\t<gx:altitudeMode>relativeToSeaFloor"
446 "</gx:altitudeMode>\n");
447 fprintf(outfp, "\t\t\t\t\t\t</LookAt>\n");
449 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_grn_"
450 "pushpin</styleUrl>\n");
452 fprintf(outfp, "\t\t\t\t\t\t<styleUrl>#msn_placemark_red_"
453 "pushpin</styleUrl>\n");
454 fprintf(outfp, "\t\t\t\t\t\t<Point>\n");
455 fprintf(outfp, "\t\t\t\t\t\t\t<coordinates>%.8f,%.8f</coordinates>\n",
456 longitude, latitude);
457 fprintf(outfp, "\t\t\t\t\t\t</Point>\n");
458 fprintf(outfp, "\t\t\t\t\t</Placemark>\n");
463 fprintf(outfp, "\t<Folder>\n");
464 fprintf(outfp, "\t\t<name>Lines</name>\n");
465 fprintf(outfp, "\t\t<open>0</open>\n");
466 fprintf(outfp, "\t\t<visibility>0</visibility>\n");
468 geo2space(&x, &y, &z, longitude, latitude);
472 if (meas->gps_valid) {
473 double mx, my, mz, dist;
475 geo2space(&mx, &my, &mz, meas->longitude,
477 dist = distinspace(x, y, z, mx, my, mz);
478 fprintf(outfp, "\t\t<Placemark>\n");
479 fprintf(outfp, "\t\t\t<name>Range</name>\n");
480 fprintf(outfp, "\t\t\t<description>\n");
481 fprintf(outfp, "Distance: %d\n", (int)dist);
482 fprintf(outfp, "TA=%d (%d-%d meter)\n", meas->ta,
483 (int)(GSM_TA_M * meas->ta),
484 (int)(GSM_TA_M * (meas->ta + 1)));
485 fprintf(outfp, "\t\t\t</description>\n");
486 fprintf(outfp, "\t\t\t<visibility>0</visibility>\n");
487 fprintf(outfp, "\t\t\t<LineString>\n");
488 fprintf(outfp, "\t\t\t\t<tessellate>1</tessellate>\n");
489 fprintf(outfp, "\t\t\t\t<coordinates>\n");
490 fprintf(outfp, "%.8f,%.8f\n", longitude, latitude);
491 fprintf(outfp, "%.8f,%.8f\n", meas->longitude,
493 fprintf(outfp, "\t\t\t\t</coordinates>\n");
494 fprintf(outfp, "\t\t\t</LineString>\n");
495 fprintf(outfp, "\t\t</Placemark>\n");
499 fprintf(outfp, "\t</Folder>\n");
502 int main(int argc, char *argv[])
507 struct node_mcc *mcc;
508 struct node_mnc *mnc;
509 struct node_lac *lac;
510 struct node_cell *cell;
511 struct node_meas *meas;
515 fprintf(stderr, "Usage: %s <file.log> <file.kml> "
516 "[lines] [debug]\n", argv[0]);
517 fprintf(stderr, "lines: Add lines between cell and "
518 "Measurement point\n");
519 fprintf(stderr, "debug: Add debugging of location algorithm.\n"
524 for (i = 3; i < argc; i++) {
525 if (!strcmp(argv[i], "lines"))
527 else if (!strcmp(argv[i], "debug"))
532 infp = fopen(argv[1], "r");
534 fprintf(stderr, "Failed to open '%s' for reading\n", argv[1]);
538 while ((type = read_log(infp))) {
540 case LOG_TYPE_SYSINFO:
551 if (!strcmp(argv[2], "-"))
554 outfp = fopen(argv[2], "w");
556 fprintf(stderr, "Failed to open '%s' for writing\n", argv[2]);
562 while (strchr(p, '/'))
563 p = strchr(p, '/') + 1;
565 kml_header(outfp, p);
566 mcc = node_mcc_first;
568 printf("MCC: %02x\n", mcc->mcc);
570 fprintf(outfp, "\t<Folder>\n");
571 fprintf(outfp, "\t\t<name>MCC %s (%s)</name>\n",
572 gsm_print_mcc(mcc->mcc), gsm_get_mcc(mcc->mcc));
573 fprintf(outfp, "\t\t<open>0</open>\n");
576 printf(" MNC: %02x\n", mnc->mnc);
578 fprintf(outfp, "\t\t<Folder>\n");
579 fprintf(outfp, "\t\t\t<name>MNC %s (%s)</name>\n",
580 gsm_print_mnc(mnc->mnc), gsm_get_mnc(mcc->mcc, mnc->mnc));
581 fprintf(outfp, "\t\t\t<open>0</open>\n");
584 printf(" LAC: %04x\n", lac->lac);
586 fprintf(outfp, "\t\t\t<Folder>\n");
587 fprintf(outfp, "\t\t\t\t<name>LAC %04x</name>\n", lac->lac);
588 fprintf(outfp, "\t\t\t\t<open>0</open>\n");
591 printf(" CELL: %04x\n", cell->cellid);
592 fprintf(outfp, "\t\t\t\t<Folder>\n");
593 fprintf(outfp, "\t\t\t\t\t<name>CELL-ID %04x</name>\n", cell->cellid);
594 fprintf(outfp, "\t\t\t\t\t<open>0</open>\n");
599 printf(" TA: %d\n", meas->ta);
601 kml_meas(outfp, meas, ++n, mcc->mcc, mnc->mnc,
602 lac->lac, cell->cellid);
605 kml_cell(outfp, cell);
607 fprintf(outfp, "\t\t\t\t</Folder>\n");
611 fprintf(outfp, "\t\t\t</Folder>\n");
615 fprintf(outfp, "\t\t</Folder>\n");
619 fprintf(outfp, "\t</Folder>\n");
625 fprintf(outfp, "\t<Folder>\n");
626 fprintf(outfp, "\t\t<name>Power</name>\n");
627 fprintf(outfp, "\t\t<open>0</open>\n");
628 power = node_power_first;
632 fprintf(outfp, "\t\t<Folder>\n");
633 fprintf(outfp, "\t\t\t<name>Power %d</name>\n", ++n);
634 fprintf(outfp, "\t\t\t<open>0</open>\n");
636 fprintf(outfp, "\t\t</Folder>\n");
640 fprintf(outfp, "\t</Folder>\n");