summaryrefslogtreecommitdiff
path: root/gpsfake
blob: 698c23edd0c8fa6cfa78adc12399d295e918e8d3 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
#
'''
gpsfake -- test harness for gpsd

Simulates one or more GPSes, playing back logfiles.
Most of the logic for this now lives in gps.fake,
factored out so we can write other test programs with it.
'''
#
# This file is Copyright (c) 2010 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause

# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import getopt
import os
import platform
import pty
import socket
import sys
import time

import gps
import gps.fake as gpsfake   # The "as" pacifies pychecker

gps_version = '3.19-dev'
if gps.__version__ != gps_version:
    sys.stderr.write("gpsfake: ERROR: need gps module version %s, got %s\n" %
                     (gps_version, gps.__version__))
    sys.exit(1)

try:
    my_input = raw_input
except NameError:
    my_input = input

# Get version of stdout for bytes data (NOP in Python 2)
bytesout = gps.get_bytes_stream(sys.stdout)


class Baton(object):
    "Ship progress indications to stderr."
    # By setting this > 1 we reduce the frequency of the twirl
    # and speed up test runs.  Should be relatively prime to the
    # nunber of baton states, otherwise it will cause beat artifacts
    # in the twirling.
    SPINNER_INTERVAL = 11

    def __init__(self, prompt, endmsg=None):
        self.stream = sys.stderr
        self.stream.write(prompt + "...")
        if os.isatty(self.stream.fileno()):
            self.stream.write(" \b")
        self.stream.flush()
        self.count = 0
        self.endmsg = endmsg
        self.time = time.time()
        return

    def twirl(self, ch=None):
        "Twirl the baton"
        if self.stream is None:
            return
        if os.isatty(self.stream.fileno()):
            if ch:
                self.stream.write(ch)
                self.stream.flush()
            elif self.count % Baton.SPINNER_INTERVAL == 0:
                self.stream.write("-/|\\"[self.count % 4])
                self.stream.write("\b")
                self.stream.flush()
        self.count = self.count + 1
        return

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


def hexdump(s):
    "Convert string to hex"
    rep = ""
    for c in s:
        rep += "%02x" % ord(c)
    return rep


def fakehook(linenumber, fakegps):
    "Do the real work"
    if not fakegps.testload.sentences:
        sys.stderr.write("fakegps: no sentences in test load.\n")
        raise SystemExit(1)
    if linenumber % len(fakegps.testload.sentences) == 0:
        if singleshot and linenumber > 0:
            return False
        if progress:
            baton.twirl('*\b')
        elif not singleshot:
            if not quiet:
                sys.stderr.write("gpsfake: log cycle of %s begins.\n"
                                 % fakegps.testload.name)
    time.sleep(cycle)
    if linedump and fakegps.testload.legend:
        ml = fakegps.testload.sentences[
            linenumber % len(fakegps.testload.sentences)].strip()
        if not fakegps.testload.textual:
            ml = hexdump(ml)
        announce = fakegps.testload.legend \
            % (linenumber % len(fakegps.testload.sentences) + 1) + ml
        if promptme:
            my_input(announce + "? ")
        else:
            print(announce)
    if progress:
        baton.twirl()
    return True


if __name__ == '__main__':
    def usage():
        "Print usage and exit"
        sys.stderr.write("""usage: gpsfake [OPTIONS] logfile...
    [-1]
    [-b]
    [-c cycle]
    [-D debug]
    [-g]
    [-h]
    [-i]
    [-l]
    [-m monitor]
    [-n]
    [-o options]
    [-p]
    [-P port]
    [-q]
    [-r initcmd]
    [-S]
    [-s speed]
    [-t]
    [-T]
    [-v]
    [-V]
    [-x]
""")
        raise SystemExit(0)

    try:
        (options, arguments) = getopt.getopt(sys.argv[1:],
                                             "1bc:D:ghilm:no:pP:qr:s:StTuvxV")
    except getopt.GetoptError as msg:
        print("gpsfake: " + str(msg))
        raise SystemExit(1)

    port = None
    progress = False
    cycle = 0.0
    monitor = ""
    speed = 4800
    linedump = False
    predump = False
    pipe = False
    singleshot = False
    promptme = False
    client_init = '?WATCH={"json":true,"nmea":true}'
    doptions = ""
    tcp = False
    udp = False
    verbose = 0
    slow = False
    quiet = False
    for (switch, val) in options:
        if switch == '-1':
            singleshot = True
        elif 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 == '-h':
            usage()
        elif switch == '-i':
            linedump = promptme = True
        elif switch == '-l':
            linedump = True
        elif switch == '-m':
            monitor = val + " "
        elif switch == '-n':
            doptions += " -n"
        elif switch == '-o':
            doptions = val
        elif switch == '-p':
            pipe = True
        elif switch == '-P':
            port = int(val)
        elif switch == '-q':
            quiet = True
        elif switch == '-r':
            client_init = val
        elif switch == '-s':
            speed = int(val)
        elif switch == '-S':
            slow = True
        elif switch == '-t':
            tcp = True
        elif switch == '-T':
            sys.stdout.write("sys %s platform %s: WRITE_PAD = %.5f\n"
                             % (sys.platform, platform.platform(),
                                gpsfake.GetDelay(slow)))
            raise SystemExit(0)
        elif switch == '-u':
            udp = True
        elif switch == '-v':
            verbose += 1
        elif switch == '-V':
            sys.stderr.write("gpsfake: Version %s\n" % gps_version)
            sys.exit(0)
        elif switch == '-x':
            predump = True

    try:
        pty.openpty()
    except (AttributeError, OSError):
        sys.stderr.write("gpsfake: ptys not available, falling back to UDP.\n")
        udp = True

    if not arguments:
        sys.stderr.write("gpsfake: requires at least one logfile argument.\n")
        raise SystemExit(1)

    if progress:
        baton = Baton("Processing %s" % ",".join(arguments), "done")
    elif not quiet:
        sys.stderr.write("Processing %s\n" % ",".join(arguments))

    # Don't allocate a private port when cycling logs for client testing.
    if port is None and not pipe:
        port = int(gps.GPSD_PORT)

    test = gpsfake.TestSession(prefix=monitor, port=port, options=doptions,
                               tcp=tcp, udp=udp, verbose=verbose,
                               predump=predump, slow=slow)

    if pipe:
        test.reporter = bytesout.write
        if verbose:
            progress = False
            test.progress = sys.stderr.write
    test.spawn()
    try:
        for logfile in arguments:
            try:
                test.gps_add(logfile, speed=speed, pred=fakehook,
                             oneshot=singleshot)
            except gpsfake.TestLoadError as e:
                sys.stderr.write("gpsfake: " + e.msg + "\n")
                raise SystemExit(1)
            except gpsfake.PacketError as e:
                sys.stderr.write("gpsfake: " + e.msg + "\n")
                raise SystemExit(1)
            except gpsfake.DaemonError as e:
                sys.stderr.write("gpsfake: " + e.msg + "\n")
                raise SystemExit(1)
            except IOError as e:
                if e.filename is None:
                    sys.stderr.write("gpsfake: unknown internal I/O error %s\n"
                                     % e)
                else:
                    sys.stderr.write("gpsfake: no such file as %s or "
                                     "file unreadable\n" % e.filename)
                raise SystemExit(1)
            except OSError:
                sys.stderr.write("gpsfake: can't open pty.\n")
                raise SystemExit(1)

        try:
            if pipe:
                test.client_add(client_init + "\n")
                # Give daemon time to get ready for the feeds.
                # Without a delay here there's a window for test
                # sentences to arrive before the watch takes effect.
                # This needs to increase if leading sentences in
                # test loads aren't being processed.
                # Until the ISYNC driver was introduced, 1 sec was
                # sufficient here. The extra 0.4s allows for the
                # additional two 200ms delays introduced by the
                # calls to gpsd_set_speed() in isync_detect()
                time.sleep(1.4)
            test.run()
        except socket.error as msg:
            sys.stderr.write("gpsfake: socket error %s.\n" % msg)
            raise SystemExit(1)
        except gps.client.json_error as e:
            sys.stderr.write("gpsfake: JSON error on line %s is %s.\n"
                             % (repr(e.data), e.explanation))
            raise SystemExit(1)
        except KeyboardInterrupt:
            sys.stderr.write("gpsfake: aborted\n")
            raise SystemExit(1)
    finally:
        test.cleanup()

    if progress:
        baton.end()

# The following sets edit modes for GNU EMACS
# Local Variables:
# mode:python
# End: