###############################################################################
# $Id: r2v.py
#
# Purpose:  test code to connect to and dump Mobile Action's i-gotU devices
#             device used for this is GT-100
# Author:   dustymugs, dustymugs@dspiral.net
#
###############################################################################
#
###############################################################################
# Copyright (c) 2008, dustymugs <dustymugs@dspiral.net>
#
# This program 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; version 2 of the License.
#
# This program 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 this program; if not, write to the Free Software foundation, Inc., 
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
###############################################################################

import serial, time, sys
from dhc import hex2char as h2c, char2hex as c2h, hexString

def csNMEA(STR):
	cs = 0
	for c in STR:
		if c == '$':
			continue
		if c == '*':
			break

		cs = cs ^ ord(c)
	return cs

def csSiRF(STR):
	cs = 0
	for c in STR:
		cs = cs + ord(c)
	return cs & 32767

def buildSiRF(payload):
	sq = h2c('A0') + h2c('A2')
	eq = h2c('B0') + h2c('B3')
	pl = ("%X" % len(payload))
	cs = csSiRF(payload)

	return sq + pl + payload + ("%X" % cs) + eq

def flush(r, count = 1):
	i = 0
	while i < count:
		r.flushOutput();r.flushInput()
		i = i + 1

def removeDataHeader(STR):
	if not STR.startswith(DATAHEADER):
		return STR
	return STR[3:]

NULL = chr(0)
BUFFERSIZE = 8192
DATAHEADER = hexString('93 10 00', ' ')
VALIDMSG = hexString('93 00 00', ' ')
ERRORMSG = hexString('93 FF FE', ' ')

f = open('test.ig', 'w')

s = serial.Serial()

s.port = 'com8'
s.baudrate = 115200
s.bytesize = 8
s.parity = 'N'
s.stopbits = 1
s.timeout = 2
s.queuesize = BUFFERSIZE

# open serial port
s.open()

# clean
time.sleep(1)
flush(s)

# connect
s.write(hexString('93 01 01 03 00 00 00 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('00 00 00 00 00 00 00 68', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'

flush(s)

# config dump ???
# i-gotU Desktop does this
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('00 00 00 00 00 00 00 4A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
s.read(s.inWaiting())

flush(s)

# ???
# i-gotU Desktop does this
s.write(hexString('93 0A 00 00 00 00 00 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
# generates a 13 byte response
# 93 00 0A 04 02 00 00 01 24 00 01 11 00
s.write(hexString('00 00 00 00 00 00 00 63', ' '))
time.sleep(0.05)
#print "****%s++++" % s.read(s.inWaiting())
s.read(s.inWaiting())

flush(s)

"""
dumps start with header 93 10 00
removing header makes dump 4096B
"""

"""

00 00 00 00 00 00 00 4A
10 00 00 00 00 00 00 3A
20 00 00 00 00 00 00 2A
30 00 00 00 00 00 00 1A
40 00 00 00 00 00 00 0A
50 00 00 00 00 00 00 FA
60 00 00 00 00 00 00 EA
70 00 00 00 00 00 00 DA
80 00 00 00 00 00 00 CA
90 00 00 00 00 00 00 BA
A0 00 00 00 00 00 00 AA
B0 00 00 00 00 00 00 9A
C0 00 00 00 00 00 00 8A
D0 00 00 00 00 00 00 7A
E0 00 00 00 00 00 00 6A
F0 00 00 00 00 00 00 5A
... and then what?

"""

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('F2 00 00 00 00 00 00 3A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)


"""

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('10 00 00 00 00 00 00 3A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('20 00 00 00 00 00 00 2A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('30 00 00 00 00 00 00 1A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('40 00 00 00 00 00 00 0A', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('50 00 00 00 00 00 00 FA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('60 00 00 00 00 00 00 EA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('70 00 00 00 00 00 00 DA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('80 00 00 00 00 00 00 CA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('90 00 00 00 00 00 00 BA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

# data dump ???
s.write(hexString('93 05 07 10 00 04 03 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('A0 00 00 00 00 00 00 AA', ' '))
time.sleep(0.5)
#print "****%s++++" % s.read(s.inWaiting())
f.write(removeDataHeader(s.read(s.inWaiting())))

flush(s)

"""

# disconnect
s.write(hexString('93 01 01 00 00 00 00 00', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'
s.write(hexString('00 00 00 00 00 00 00 6B', ' '))
time.sleep(0.05)
if s.read(3) == VALIDMSG:
	print 'valid'

# check that buffer has nothing at end
#time.sleep(1)
#print "****%s++++" % s.read(s.inWaiting())

s.close()

f.close()
