⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpsfake.py

📁 gpsd, a popular GPS daemon.
💻 PY
📖 第 1 页 / 共 2 页
字号:
"""$Id: gpsfake.py 4646 2007-12-28 04:45:02Z ckuethe $gpsfake.py -- classes for creating a controlled test environment around gpsd.The gpsfake(1) regression tester shipped with gpsd is a trivial wrapperaround this code.  For a more interesting usage example, see thevalgrind-audit script shipped with the gpsd code.To use this code, start by instantiating a TestSession class.  Use theprefix argument if you want to run the daemon under some kind of run-timemonitor like valgrind or gdb.  Here are some particularly useful possibilities:valgrind --tool=memcheck --gen-suppressions=yes --leak-check=yes    Run under Valgrind, checking for malloc errors and memory leaks.xterm -e gdb -tui --args    Run under gdb, controlled from a new xterm.You can use the options argument to pass in daemon options; normally you willuse this to set the debug-logging level.On initialization, the test object spawns an instance of gpsd with nodevices or clients attached, connected to a control socket.TestSession has methods to attach and detch fake GPSes. TheTestSession class simulates GPS devices for you with objects composedfrom a pty and a class instance that cycles sentences into the master sidefrom some specified logfile; gpsd reads the slave side.  A fake GPS isidentified by the string naming its slave device.TestSession also has methods to start and end client sessions.  Daemonresponses to a client are fed to a hook function which, by default,discards them.  You can change the hook to sys.stdout.write() to dumpresponses to standard output (this is what the gpsfake executabledoes) or do something more exotic. A client session is identified by asmall integer that counts the number of client session starts.There are a couple of convenience methods.  TestSession.wait() does nothing,allowing a specified number of seconds to elapse.  TestSession.client_query()ships commands to an open client session.TestSession does not currently capture the daemon's log output.  It isrun with -N, so the output will go to stderr (along with, for example,Valgrind notifications).Each FakeGPS instance tries to packetize the data from the logfile itis initialized with.  It looks for packet headers associated with commonpacket types such as NMEA, SiRF, TSIP, and Zodiac.  Additionally, the Typeheader in a logfile can be used to force the packet type, notably to RTCMwhich is fed to the daemon character by character,The TestSession code maintains a run queue of FakeGPS and gps.gs (client-session) objects. It repeatedly cycles through the run queue.  For eachclient session object in the queue, it tries to read data from gpsd.  Foreach fake GPS, it sends one line of stored data.  When a fake-GPS'sgo predicate becomes false, the fake GPS is removed from the run queue.There are two ways to use this code.  The more deterministic isnon-threaded mode: set up your client sessions and fake GPS devices,then call the run() method.  The run() method will terminate whenthere are no more objects in the run queue.  Note, you must havecreated at least one fake client or fake GPS before calling run(),otherwise it will terminate immediately.To allow for adding and removing clients while the test is running,run in threaded mode by calling the start() method.  This simply callsthe run method in a subthread, with locking of critical regions."""import sys, os, time, signal, pty, termios # fcntl, array, structimport exceptions, threading, socketimport gps# Define a per-character delay on writes so we won't spam the buffers# in the pty layer or gpsd itself.  The magic number here has to be# derived from observation.  If it's too high you'll slow the tests# down a lot.  If it's too low you'll get random spurious regression# failures that usually look like lines missing from the end of the# test output relative to the check file.  This number might have to# be adusted upward on faster machines.WRITE_PAD = 15.0class TestLoadError(exceptions.Exception):    def __init__(self, msg):        self.msg = msgclass TestLoad:    "Digest a logfile into a list of sentences we can cycle through."    def __init__(self, logfp, predump=False):        self.sentences = []	# This and .packtype are the interesting bits        self.logfp = logfp        self.predump = predump        self.logfile = logfp.name        self.type = None        self.serial = None        # Skip the comment header        while True:            first = logfp.read(1)            self.first = first;            if first == "#":                line = logfp.readline()                if line.strip().startswith("Type:"):                    if line.find("RTCM") > -1:                        self.type = "RTCM"                if "Serial:" in line:                    line = line[1:].strip()                    try:                        (xx, baud, params) = line.split()                        baud = int(baud)                        if params[0] in ('7', '8'):                            databits = int(params[0])                        else:                            raise ValueError                        if params[1] in ('N', 'O', 'E'):                            parity = params[1]                        else:                            raise ValueError                        if params[2] in ('1', '2'):                            stopbits = int(params[2])                        else:                            raise ValueError                    except (ValueError, IndexError):                        raise TestLoadError("bad serial-parameter spec in %s"%\                                            logfp.name)                                        self.serial = (baud, databits, parity, stopbits)            else:                break        # Grab the packets        while True:            packet = self.packet_get()            if self.predump:                print `packet`            if not packet or packet == "\n":                break            else:                self.sentences.append(packet)        # Look at the first packet to grok the GPS type        if self.sentences[0][0] == '$':            self.packtype = "NMEA"            self.legend = "gpsfake: line %d: "            self.idoffset = None            self.textual = True        elif self.sentences[0][0] == '\xff':            self.packtype = "Zodiac binary"            self.legend = "gpsfake: packet %d: "            self.idoffset = None            self.textual = True        elif self.sentences[0][0] == '\xa0':            self.packtype = "SiRF binary"            self.legend = "gpsfake: packet %d: "            self.idoffset = 3            self.textual = False        elif self.sentences[0][0] == '\x02':            self.packtype = "Navcom binary"            self.legend = "gpsfake: packet %d"            self.textual = False        elif self.sentences[0][0] == '\x10':            self.packtype = "TSIP binary"            self.legend = "gpsfake: packet %d: "            self.idoffset = 1            self.textual = False        elif self.sentences[0][0] == '\xb5':            self.packtype = "uBlox"            self.legend = "gpsfake: packet %d: "            self.idoffset = None            self.textual = False        elif self.sentences[0][0] == '\x3c':            self.packtype = "iTrax"            self.legend = "gpsfake: packet %d: "            self.idoffset = None            self.textual = False        elif self.type == "RTCM":            self.packtype = "RTCM"            self.legend = None            self.idoffset = None            self.textual = False        else:            sys.stderr.write("gpsfake: unknown log type (not NMEA or SiRF) can't handle it!\n")            self.sentences = None    def packet_get(self):        "Grab a packet.  Unlike the daemon's state machine, this assumes no noise."        if self.first == '':            first = self.logfp.read(1)        else:            first=self.first            self.first=''        if not first:            return None        elif self.type == "RTCM":            return first        elif first == '$':					# NMEA packet            return "$" + self.logfp.readline()        second = self.logfp.read(1)        if first == '\xa0' and second == '\xa2':		# SiRF packet            third = self.logfp.read(1)            fourth = self.logfp.read(1)            length = (ord(third) << 8) | ord(fourth)            return "\xa0\xa2" + third + fourth + self.logfp.read(length+4)        elif first == '\xff' and second == '\x81':		# Zodiac            third = self.logfp.read(1)            fourth = self.logfp.read(1)            fifth = self.logfp.read(1)            sixth = self.logfp.read(1)            #id = ord(third) | (ord(fourth) << 8)            ndata = ord(fifth) | (ord(sixth) << 8)            return "\xff\x81" + third + fourth + fifth + sixth + self.logfp.read(2*ndata+6)        elif first == '\x02' and second == '\x99':		# Navcom            third = self.logfp.read(1)	    fourth = self.logfp.read(1)            fifth = self.logfp.read(1)            sixth = self.logfp.read(1)            #id = ord(fourth)            ndata = ord(fifth) | (ord(sixth) << 8)            return "\x02\x99\x66" + fourth + fifth + sixth + self.logfp.read(ndata-2)        elif first == '\x10':					# TSIP            packet = first + second            delcnt = 0            while True:                next = self.logfp.read(1)                if not next:                    return ''                packet += next                if next == '\x10':                    delcnt += 1                elif next == '\x03':                    if delcnt % 2:                        break                else:                    delcnt = 0            return packet        elif first == '\xb5' and second == '\x62':		# ubx            third = self.logfp.read(1)            fourth = self.logfp.read(1)            fifth = self.logfp.read(1)            sixth = self.logfp.read(1)            # classid = third            # messageid = fourth            ndata = ord(fifth) | (ord(sixth) << 8)            return "\xb5\x62" + third + fourth + fifth + sixth + self.logfp.read(ndata+2)        elif first == '\x3c' and second == '\x21':		# italk            third = self.logfp.read(1)            fourth = self.logfp.read(1)            fifth = self.logfp.read(1)            sixth = self.logfp.read(1)            seventh = self.logfp.read(1)            # srcnode = third            # dstnode = fourth            # messageid = fifth            # transaction = sixth            ndata = (ord(seventh)+1)*2 + 1            return "\x3c\x21" + third + fourth + fifth + sixth + seventh + self.logfp.read(ndata)        elif first == "\n":	# Use this to ignore trailing EOF on logs            return "\n"        else:            raise PacketError("unknown packet type, leader %s (0x%x)" % (`first`, ord(first)))class PacketError(exceptions.Exception):    def __init__(self, msg):        self.msg = msgclass FakeGPS:    "A fake GPS is a pty with a test log ready to be cycled to it."    def __init__(self, logfp,                 speed=4800, databits=8, parity='N', stopbits=1,                 verbose=False, predump=False):        self.verbose = verbose        self.go_predicate = lambda: True        self.readers = 0        self.index = 0        self.speed = speed        baudrates = {            0: termios.B0,            50: termios.B50,            75: termios.B75,            110: termios.B110,            134: termios.B134,            150: termios.B150,            200: termios.B200,            300: termios.B300,            600: termios.B600,            1200: termios.B1200,            1800: termios.B1800,            2400: termios.B2400,            4800: termios.B4800,            9600: termios.B9600,            19200: termios.B19200,            38400: termios.B38400,            57600: termios.B57600,            115200: termios.B115200,            230400: termios.B230400,        }        speed = baudrates[speed]	# Throw an error if the speed isn't legal        if type(logfp) == type(""):            logfp = open(logfp, "r");                    self.testload = TestLoad(logfp, predump)        # FIXME: explicit arguments should probably override this        #if self.testload.serial:        #    (speed, databits, parity, stopbits) = self.testload.serial        (self.master_fd, self.slave_fd) = pty.openpty()        self.slave = os.ttyname(self.slave_fd)        ttyfp = open(self.slave, "rw")        (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(ttyfp.fileno())        cc[termios.VMIN] = 1	cflag &= ~(termios.PARENB | termios.PARODD | termios.CRTSCTS)	cflag |= termios.CREAD | termios.CLOCAL        iflag = oflag = lflag = 0 	iflag &=~ (termios.PARMRK | termios.INPCK) 	cflag &=~ (termios.CSIZE | termios.CSTOPB | termios.PARENB | termios.PARODD)        if databits == 7:            cflag |= termios.CS7        else:            cflag |= termios.CS8        if stopbits == 2:            cflag |= termios.CSTOPB 	if parity == 'E':

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -