File: gpsdump.cc

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (129 lines) | stat: -rw-r--r-- 3,663 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "config.h"
#include "gpsdump.h"

GPSDump::GPSDump() {
    num_packets = 0;
    gpsf = NULL;
}

int GPSDump::OpenDump(const char *in_fname, const char *in_netfname) {
    if ((gpsf = fopen(in_fname, "wb")) == NULL) {
        snprintf(errstr, 1024, "GPSDump unable to open file: %s",
                 strerror(errno));
        return -1;
    }

    fname = in_fname;

    // Write the XML headers
    fprintf(gpsf, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!DOCTYPE gps-run SYSTEM \"http://kismetwireless.net/kismet-gps-2.9.1.dtd\">\n\n");

    // Write the start of the run
    time_t cur_time = time(0);

    fprintf(gpsf, "<gps-run gps-version=\"%d\" start-time=\"%.24s\">\n\n",
            GPS_VERSION, ctime(&cur_time));

    if (in_netfname != NULL) {
        fprintf(gpsf, "    <network-file>%s</network-file>\n\n", in_netfname);
    }

	fflush(gpsf);

    return 1;
}

int GPSDump::CloseDump(int in_unlink) {
    int ret = 1;

    if (gpsf) {
		fprintf(gpsf, "</gps-run>\n");

        fclose(gpsf);

		if (num_packets == 0 && in_unlink) {
			unlink(fname);
			ret = -1;
		}
	}

    gpsf = NULL;
    num_packets = 0;

    return ret;
}

int GPSDump::DumpPacket(packet_info *in_packinfo) {
    if (in_packinfo->gps_fix < 2)
        return 0;

    timeval ts;
    gettimeofday(&ts, NULL);

    fprintf(gpsf, "    <gps-point bssid=\"%s\" source=\"%s\" time-sec=\"%ld\" time-usec=\"%ld\" "
            "lat=\"%f\" lon=\"%f\" alt=\"%f\" spd=\"%f\" heading=\"%f\" fix=\"%d\" "
            "signal=\"%d\" noise=\"%d\"/>\n",
            in_packinfo->bssid_mac.Mac2String().c_str(),
            in_packinfo->source_mac.Mac2String().c_str(),
            (long int) in_packinfo->ts.tv_sec, (long int) in_packinfo->ts.tv_usec,
            in_packinfo->gps_lat, in_packinfo->gps_lon, in_packinfo->gps_alt,
            in_packinfo->gps_spd, in_packinfo->gps_heading, in_packinfo->gps_fix,
            in_packinfo->signal, in_packinfo->noise);

    num_packets++;

    return 1;
}

int GPSDump::DumpTrack(GPSD *in_gps) {
    float lat, lon, alt, spd, hed;
    int fix;

    // Bail if we don't have a lock
    if (in_gps->FetchMode() < 2)
        return 0;

    timeval ts;
    gettimeofday(&ts, NULL);

    in_gps->FetchLoc(&lat, &lon, &alt, &spd, &hed, &fix);

    int sig = 0, qual = 0, noise = 0;

    if (time(0) - last_info.ts.tv_sec < decay && last_info.quality != -1) {
        sig = last_info.signal;
        qual = last_info.quality;
        noise = last_info.noise;
    }

    fprintf(gpsf, "    <gps-point bssid=\"%s\" time-sec=\"%ld\" time-usec=\"%ld\" "
            "lat=\"%f\" lon=\"%f\" alt=\"%f\" spd=\"%f\" heading=\"%f\" fix=\"%d\" "
            "signal=\"%d\" quality=\"%d\" noise=\"%d\"/>\n",
            gps_track_bssid,
            (long int) ts.tv_sec, (long int) ts.tv_usec,
            lat, lon, alt, spd, hed, fix,
            sig, qual, noise);

    num_packets++;

    return 1;
}