/
main.py
156 lines (129 loc) · 3.92 KB
/
main.py
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
#!/usr/bin/env python
from SerialConnection import SerialConnection
from SocketConnection import SocketConnection
from Packet import Packet
from payload import *
import threading
import time
import sys
def quit():
print 'Exiting...'
global socketConnection
socketConnection.close()
sys.exit()
def sendLoop():
while(True):
global databuffer
while(len(databuffer) > 0):
currentMsg = databuffer.pop(0)
global socketConnection
socketConnection.sendData(currentMsg)
time.sleep(0.1)
def isNulls(data):
for i in range(0,len(data)):
if(data[i] != '\0'):
return False
return True
def getInput():
global printNulls
try:
print "Waiting for input socket connection"
listeningSocket = SocketConnection('',29877)
print "Input connection created"
if listeningSocket.connectAsReceiver():
print "Connected as receiver"
global isReady
if not isReady:
print "Waiting for ready message...."
while True:
data = listeningSocket.receiveData(1)
if data == '*':
print "Received ready message"
isReady = True
#threading.Thread(target = starTimeouter).start()
break
while True:
data = listeningSocket.receiveData(32)
if(not isNulls(data)):
print "Write: " + data.encode('hex_codec')
serial.write(data)
else:
print "NO LISTENING CONNECTION"
except:
pass
finally:
listeningSocket.close()
def starTimeouter():
global sendStars
global fakePacket
startTime = time.time()
while sendStars:
if(time.time() - startTime > 1):
startTime = time.time()
if fakePacket:
print "Sending Hash"
serial.write('#')
else:
print "Sending Star"
serial.write('*');
def isDebugMessage(data):
try:
if(data[0] == '\xFE'):
print "MESSAGE: " + str(data[1:])
open('msglog.txt','a').write(str(data[1:]))
return True
else:
return False
except:
return True
print sys.argv
try:
fakePacket = False
if len(sys.argv) == 2:
fakePacket = True
databuffer = []
isReady = False
printNulls = True
sendStars = True
serial = SerialConnection()
print "Serial created"
serial.connect("/dev/ttyUSB0")
print "Serial Connected..."
socketConnection = SocketConnection()
print "Socket created"
if(socketConnection.connectAsSender()):
threading.Thread(target = sendLoop).start()
threading.Thread(target = getInput).start()
print 'Thread started'
while not isReady:
time.sleep(0.1)
print "Sending speck ready message:",
if fakePacket:
serial.write('#')
print '#'
#serial.flush()
else:
serial.write('*')
print '*'
#serial.flush()
print "Removing nulls"
serial.removeInitialNulls()
#print serial.read(1)
sendStars = False
print "NULLS REMOVED"
if fakePacket:
print "Writing fake GPS packet"
serial.write('\x00\x00\x00\x01\x00\x00\x03\x4F\xE7\x3F\x00\x01\xE9\x36\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
print "Fake packet sent"
while True:
data = serial.read(32)
print "READ: " + str(data.encode('hex_codec'))
if(not isDebugMessage(data)):
packet = Packet(data)
print "PACKET: " + str(packet)
databuffer.append(data)
serial.close()
except:
pass
finally:
quit()