"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / gpscat
1 #!/usr/bin/env python
2 #
3 # This file is Copyright (c) 2010 by the GPSD project
4 # BSD terms apply: see the file COPYING in the distribution root for details.
5 #
6 # Display GPS output.  Hexify it if necessary.
7 #
8 import os, sys, termios, socket, select, getopt, curses.ascii
9 import gps.packet as sniffer
10
11 # The spec says 82, but some receivers (TN-200, GSW 2.3.2) output 86 characters
12 NMEA_MAX = 86
13
14 # Lowest debug level at which packet getter begins to emit messages, minus one
15 BASELEVEL = sniffer.LOG_IO
16
17 highhalf_latch = True
18
19 def hexdump(str):
20     dmp = ""
21     for (i, ch) in enumerate(str):
22         if curses.ascii.isprint(ord(ch)) or curses.ascii.isspace(ord(ch)):
23             dmp += ch
24         else:
25             dmp += "\\x%02x" % ord(ch)
26     return dmp
27
28 debuglevel = 0
29
30 def reporter(errlevel, msg):
31     if errlevel <= debuglevel:
32         sys.stdout.write(msg)
33
34 if __name__ == '__main__':
35     buf = ""
36     try:
37         try:
38             (options, arguments) = getopt.getopt(sys.argv[1:], "hps:tD:V")
39         except getopt.GetoptError, msg:
40             print "gpscat: " + str(msg)
41             raise SystemExit, 1
42
43         speed = None
44         parity = None
45         stopbits = None
46         rawmode = True
47         typeflag = False
48         for (switch, val) in options:
49             if switch == '-p':
50                 rawmode = False
51             elif switch == '-s':
52                 if val[-2] in ('N', 'E', 'O'):
53                     parity = val[-2]
54                     stopbits = int(val[-1])
55                     val = val[:-2]
56                 speed = int(val)
57             elif switch == '-t':
58                 typeflag = True
59                 rawmode = False
60             elif switch == '-D':
61                 debuglevel = BASELEVEL + int(val)
62             elif switch == '-h':
63                 sys.stderr.write("usage: gpscat [-s speed] serial-port\n")
64                 raise SystemExit, 0
65
66         if "rfcomm" in arguments[0]:     # Bluetooth special case
67             s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
68             s.connect((arguments[0], 1))
69             tty = s.fileno()
70         else:                            # Ordinary device
71             tty = os.open(arguments[0], os.O_RDWR)
72
73         if speed != None:
74             (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(tty)
75             try:
76                 ispeed = ospeed = eval("termios.B%d" % speed)
77             except AttributeError:
78                 sys.stderr.write("gpscat: unknown baud rate %d\n" % speed)
79                 raise SystemExit, 1
80             if stopbits:
81                 cflag &= ~termios.CSIZE
82                 cflag |= (termios.CS8, termios.CS7)[stopbits-1]
83             if parity:
84                 if parity == 'N':
85                     iflag &= ~termios.PARENB
86                     iflag &= ~termios.INPCK
87                 elif parity == 'O':
88                     iflag |= termios.INPCK
89                     cflag |= termios.PARENB
90                     cflag |= termios.PARODD
91                 elif parity == 'E':
92                     iflag |= termios.INPCK
93                     cflag |= termios.PARENB
94                     cflag &= ~termios.PARODD
95             termios.tcsetattr(tty, termios.TCSANOW,
96                          [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
97
98         poller = select.poll()
99         poller.register(tty, select.POLLIN)
100
101         buf = ""
102         if not rawmode:
103             getter = sniffer.new()
104             sniffer.register_report(reporter)
105         seqno = 0
106         while True:
107             (fd, event) = poller.poll()[0]
108             if fd == tty and event == select.POLLIN:
109                 if rawmode:
110                     buf += os.read(tty, NMEA_MAX)
111                     sys.stdout.write(hexdump(buf))
112                     buf = ""
113                 else:
114                     (length, ptype, packet) = getter.get(tty)
115                     seqno += 1
116                     if length == 0:
117                         break
118                     if typeflag:
119                         sys.stdout.write(`ptype` + " (" + `length` + "): " + hexdump(packet))
120                         sys.stdout.write("\n")
121                     else:
122                         sys.stdout.write(hexdump(packet) + "\n")
123     except KeyboardInterrupt:
124         if rawmode:
125             sys.stdout.write("\n")
126         raise SystemExit, 0
127
128 # Local variables:
129 # mode: python
130 # end:
131
132