summaryrefslogtreecommitdiff
path: root/gpsfake
blob: ba499ae6cf636d278d2b0654248dfd96a374602a (plain)
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
#!/usr/bin/env python
#
# gpsfake -- test harness for gpsd
#
# Simulates a GPS, playing back a logfile
# Most of the logic for this now lives in gosfake.py,
# factored out so we can write other test programs with it.

import sys, os, signal, time, getopt
import gpsfake

class Baton:
    "Ship progress indications to stderr."
    def __init__(self, prompt, endmsg=None):
        self.stream = sys.stderr
        self.stream.write(prompt + "... \010")
        self.stream.flush()
        self.count = 0
        self.endmsg = endmsg
        self.time = time.time()
        return

    def twirl(self, ch=None):
        if self.stream is None:
            return
        if ch:
            self.stream.write(ch)
        else:
            self.stream.write("-/|\\"[self.count % 4])
            self.stream.write("\010")
        self.count = self.count + 1
        self.stream.flush()
        return

    def end(self, msg=None):
        if msg == None:
            msg = self.endmsg
        if self.stream:
            self.stream.write("...(%2.2f sec) %s.\n" % (time.time() - self.time, msg))
        return

(options, arguments) = getopt.getopt(sys.argv[1:], "bc:D:ghilm:o:ps:")
progress = False
cycle = 1
monitor = ""
speed = 4800
linedump = False
pipe = False
promptme = False
doptions = ""
for (switch, val) in options:
    if (switch == '-b'):
        progress = True
    elif (switch == '-c'):
        cycle = float(val)
    elif (switch == '-D'):
        doptions += " -D " + val
    elif (switch == '-g'):
        monitor = "xterm -e gdb -tui --args "
    elif (switch == '-i'):
        linedump = promptme = True
    elif (switch == '-l'):
        linedump = True
    elif (switch == '-m'):
        monitor = val + " "
    elif (switch == '-o'):
        doptions = val
    elif (switch == '-p'):
        pipe = True
        cycle = 0.05
    elif (switch == '-s'):
        speed = int(val)
    elif (switch == '-h'):
        sys.stderr.write("usage: gpsfake [-h] [-l] [-m monitor] [--D debug] [-o options] [-p] [-s speed] [-c cycle] [-b] logfile\n")
        raise SystemExit,0
logfile = arguments[0]


def fakehook(linenumber, fakegps):
    if linenumber % len(fakegps.testload.sentences) == 0:
        if pipe and linenumber > 0:
            return False
        if not pipe:
            sys.stderr.write("gpsfake: log cycle begins.\n")
    time.sleep(cycle)
    if linedump and fakegps.testload.legend:
        if fakegps.testload.textual:
            ml = fakegps.testload.sentences[linenumber % len(fakegps.testload.sentences)].strip()
        else:
            ml = ""
        announce = fakegps.testload.legend % (linenumber % len(fakegps.testload.sentences) + 1) + ml
        if promptme:
            raw_input(announce + "? ")
        else:
            print announce
    if progress:
        baton.twirl()
    return True

if progress:
    baton = Baton("Processing %s" % logfile, "done")

test = gpsfake.TestSession(prefix=monitor, options=doptions)

if pipe:
    test.reporter = sys.stdout.write

try:
    try:
        test.gps_add(logfile, speed=speed, pred=fakehook)
    except gpsfake.PacketError, e:
        sys.stderr.write("gsfake: " + e.msg + "\n")
        raise SystemExit, 1
    except IOError:
        sys.stderr.write("gsfake: no such file as %s or file unreadable\n"%logfile)
        raise SystemExit, 1
    except OSError:
        sys.stderr.write("gpsfake: can't open pty.\n")
        raise SystemExit, 1

    test.client_add("w+r+\n")
finally:
    test.cleanup();

if progress:
    baton.end()

# gpsfake ends here