This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUdoo.py
440 lines (371 loc) · 11.8 KB
/
Udoo.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python
__author__ = 'David'
from serial import Serial
from time import sleep
from socket import socket, AF_INET, SOCK_STREAM, SOCK_DGRAM, gethostbyname, gethostname, SOL_SOCKET, SO_REUSEADDR, gethostbyname_ex
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
from sys import argv, exit
'''VARS'''
#Global
defend = "<%=EOF=%>"
#USB
usb = None
#End USB
#CLIENT TCP
tsock = None
useTCP = False
TCPip = None
TCPport = None
TCon = False
#END
#SERVER TCP
SERVERTHREAD = None
useTCPserver = False
TCPSERVport = None
TCPsend = False
TCPrecv = False
TCPclose = False
TCPval = "N/A" #None crashes it
#END
#SERVER UDP
SERVERTHREADUDP = None
useUDPserver = False
UDPSERVport = None
UDPsend = False
UDPrecv = False
UDPval = "N/A" #None crashes it
#END
#CLIENT UDP
usock = socket(AF_INET, SOCK_DGRAM)
useUDP = False
UDPip = None
UDPport = None
#END
try:
ser = Serial("/dev/ttymxc3", 115200) #For udoo dual or quad
except:
ser = Serial("/dev/ttyMCC", 115200) #For udoo neo
def find(full, finds):
if full.find(finds) != -1:
return True
return False
def fFind(full, finds):
if str("<%="+finds+"=%>") in full:
return True
return False
def sub(full, starter, ender):
return full[full.index("<%="+starter+"=%>")+(len("<%="+starter+"=%>")):full.index(ender)]
def readLine():
return ser.readline()
def ready():
writeLine("OKOKOK")
def writeLine(towrite):
ser.write(towrite)
def customCommand(command):
print Popen(command, shell=True, stdout=PIPE, stderr=STDOUT).communicate()[0]
def parseSend(type, toSend):
global defend
writeLine("<%="+type+"=%>"+toSend+defend)
def parseRec(type):
global defend
return sub(readLine(), type, defend)
def there():
try:
parseSend("OK", "OK")
get = parseRec("OK")
if find(get, "OK"):
print "Found the SAM3x\n"
except:
print "Failed to find the SAM3x\nMake sure you put\nudoo.init()\nIn your arduino code\n Or reset device" \
+ "\nContinuing, this will use Serial line..."
#####USB######
def serialStart(port, bitrate):
global usb
usb = Serial(port, bitrate)
ready()
def serialWrite(toWrite):
global usb
usb.write(str(toWrite))
ready()
def serialRead():
global usb
parseSend("SERIALread", str(usb.readline()))
def serialStop():
global usb
usb.close()
ready()
####END USB####
######STORAGE#######
def readFile(directory):
print "Reading from %s" % str(directory)
with open(directory, "r") as read:
toRet = read.read()
parseSend("PULLfile",str(toRet).replace("\n","\\n"))
read.close()
def writeFile(directory, toWrite):
print "Writing to %s" % str(directory)
with open(directory, "w") as write:
write.write(str(toWrite).replace("\\n","\n"))
write.close()
ready()
def appendLine(directory, toAppend):
print "Appending to %s" % str(directory)
with open(directory, "a+") as append:
append.write(str(toAppend).replace("\\n","\n"))
append.close()
ready()
def readFileLine(directory, lines):
print "Pulling line: " +str(lines)+ " from %s" % str(directory)
with open(directory, "r") as line:
toRet = line.read().splitlines()[int(lines)].replace("\n","")
line.close()
parseSend("PULLline", toRet)
#####END STORAGE####
########TCP CLIENT###############
def TCPstart(full):
global TCPip, TCPport, useTCP, tsock, defend
TCPip = str(sub(full, "TCPstart", "<%=PORT=%>"))
TCPport = int(sub(full, "PORT", defend))
print "The TCPsocket IP is " + TCPip + " and the port " + str(TCPport)
useTCP = True
called = True
ready()
def TCPsend(toSend):
global useTCP, TCPip, TCPport, TCon, tsock
sending = str(toSend)
if useTCP:
print "Sending TCP packet: %s" % sending
if not TCon:
tsock = socket(AF_INET, SOCK_STREAM)
tsock.connect((TCPip, TCPport))
TCon = True
tsock.send(sending)
ready()
def TCPrecv():
global useTCP, TCPip, TCPport, TCon, tsock
if useTCP:
print "Waiting for packet..."
if not TCon:
tsock = socket(AF_INET, SOCK_STREAM)
tsock.connect((TCPip, TCPport))
TCon = True
recv = str(tsock.recv(1024))
print "Got: %s" % recv
#writeLine(recv)
parseSend("TCPrecv",recv)
def TCPclose():
global tsock, TCon
if TCon:
print "Closing TCP client"
tsock.close()
TCon = False
ready()
########END TCP CLIENT###############
############TCP SERVER###############
def TCPserver():
global useTCPserver, TCPSERVport, TCPsend, TCPrecv, TCPclose, TCPval
tserv = socket(AF_INET, SOCK_STREAM)
tserv.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
tserv.bind(('',TCPSERVport))
print "Started TCP Server on port %d" % (TCPSERVport)
tserv.listen(1)
closed = True
TCPsend = False
TCPclose = False
TCPval = "N/A"
TCPrecv = False
while useTCPserver:
print "\nWaiting for connection with client...\n"
conn, addr = tserv.accept()
print "Got client at %s" % str(addr[0])
parseSend("CLIENT","CLIENTCON")
closed = False
while not TCPsend and not TCPrecv and not TCPclose:
sleep(0.01)
while not closed:
if TCPsend:
print "Sending to client"
conn.sendall(TCPval)
ready()
TCPsend = False
TCPval = ""
if TCPrecv:
print "Waiting for input"
got = conn.recv(1024)
print "Got from client %s" % str(got)
TCPrecv = False
parseSend("TCPSERVrecv",str(got))
if TCPclose:
print "Closing the client"
conn.close()
ready()
TCPclose = False
closed = True
closed = False
print "Closed..."
tserv.close()
#################END TCP SERVER####################
############UDP SERVER###############
def UDPserver():
global useUDPserver, UDPSERVport, UDPsend, UDPrecv, UDPval
userv = socket(AF_INET, SOCK_DGRAM)
userv.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
userv.bind(("",UDPSERVport))
print "Started UDP Server on port %d" % (UDPSERVport)
RECV = False
UDPsend = False
UDPval = "N/A"
UDPrecv = False
ready()
while useUDPserver:
while not UDPsend and not UDPrecv:
sleep(0.001)
if UDPsend and RECV:
print "Sending to client"
userv.sendto(UDPval, addr)
ready()
UDPsend = False
UDPval = ""
if UDPrecv:
print "Waiting for input"
data, addr = userv.recvfrom(1024)
print "Got from client %s" % str(data)
UDPrecv = False
parseSend("UDPSERVrecv",str(data))
RECV = True
print "Closed..."
userv.close()
#################END UDP SERVER####################
########UDP CLIENT###############
def UDPstart(full):
global UDPip, UDPport, useUDP, usock
UDPip = str(sub(full, "UDPstart", "<%=PORT=%>"))
UDPport = int(sub(full, "PORT", defend))
print "The UDPsocket IP is " + UDPip + " and the port " + str(UDPport)
useUDP = True
called = True
ready()
def UDPsend(toSend):
global useUDP, UDPip, UDPport, usock
sending = str(toSend)
if useUDP:
print "Sending UDP packet: %s" % sending
usock.sendto(sending, (UDPip, UDPport))
ready()
def UDPrecv():
global useUDP, UDPip, UDPport, usock
if useUDP:
print "Waiting for packet..."
recv = str(usock.recvfrom(1024)[0])
print "Got %s" % recv
parseSend("UDPrecv",recv)
########END UDP CLIENT###############
def val():
global defend, useTCPserver, TCPSERVport, TCPsend, TCPrecv, TCPclose, TCPval, useUDPserver, UDPSERVport, UDPsend, UDPrecv, UDPclose, UDPval
recv = str(readLine())
if not find(recv, defend):
return None
#####STORAGE######
if fFind(recv, "READfile"):
readFile(str(sub(recv, "READfile", defend)))
if fFind(recv, "WRITEfile"):
direct = str(sub(recv,"WRITEfile","<%=TOWRITE=%>"))
toWrite = str(sub(recv,"TOWRITE", defend))
writeFile(direct, toWrite)
if fFind(recv, "APPENDfile"):
direct = str(sub(recv,"APPENDfile","<%=TOWRITE=%>"))
toWrite = str(sub(recv,"TOWRITE", defend))
appendLine(direct, toWrite)
if fFind(recv, "READline"):
direct = str(sub(recv,"READline","<%=LINES=%>"))
lines = str(sub(recv,"LINES", defend))
readFileLine(direct, int(lines))
###END STORAGE###
######TCP CLIENT#######
if fFind(recv, "TCPsend"):
TCPsend(sub(recv, "TCPsend", defend))
if fFind(recv, "TCPclose"):
TCPclose()
if fFind(recv, "TCPstart"):
TCPstart(recv)
if fFind(recv, "TCPrecv"):
TCPrecv()
#####END TCP CLIENT#####
######TCP SERVER#######
if fFind(recv, "TCPSERVsend"):
TCPval = str(sub(recv, "TCPSERVsend", defend))
sleep(0.07) #Make sure loop has finished
TCPsend = True
if fFind(recv, "TCPSERVclose"):
TCPclose = True
if fFind(recv, "TCPSERVstop"):
useTCPserver = False
ready()
if fFind(recv, "TCPSERVstart"):
useTCPserver = True
TCPSERVport = int(str(sub(recv,"TCPSERVstart",defend)))
SERVERTHREAD = Thread(target=TCPserver)
SERVERTHREAD.start()
sleep(0.5)
ready()
if fFind(recv, "TCPSERVrecv"):
TCPrecv = True
#####END TCP SERVER#####
######UDP SERVER#######
if fFind(recv, "UDPSERVsend"):
UDPval = str(sub(recv, "UDPSERVsend", defend))
sleep(0.07) #Make sure loop has finished
UDPsend = True
if fFind(recv, "UDPSERVstop"):
useUDPserver = False
ready()
if fFind(recv, "UDPSERVstart"):
useUDPserver = True
UDPSERVport = int(str(sub(recv,"UDPSERVstart",defend)))
SERVERTHREADUDP = Thread(target=UDPserver)
SERVERTHREADUDP.start()
if fFind(recv, "UDPSERVrecv"):
UDPrecv = True
#####END UDP SERVER#####
######UDP CLIENT#######
if fFind(recv, "UDPsend"):
UDPsend(sub(recv, "UDPsend", defend))
if fFind(recv, "UDPstart"):
UDPstart(recv)
if fFind(recv, "UDPrecv"):
UDPrecv()
#####END TCP CLIENT#####
######USB########
if fFind(recv, "SERIALstart"):
serialStart(str(sub(recv, "SERIALstart", "<%=BIT=%>")), int(str(sub(recv,"BIT",defend))))
if fFind(recv, "SERIALstop"):
serialStop()
if fFind(recv, "SERIALwrite"):
serialWrite(str(sub(recv, "SERIALwrite", defend)))
if fFind(recv, "SERIALread"):
serialRead()
####END USB#####
if fFind(recv, "DEBUG"):
print "Debug: %s" % str(sub(recv, "DEBUG", defend)).replace("\\n","\n")
ready()
if fFind(recv, "GETIP"):
IP = ([l for l in ([ip for ip in gethostbyname_ex(gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket(AF_INET, SOCK_DGRAM)]][0][1]]) if l][0][0]) #Got this snippet online
parseSend("GETIP", str(IP))
print "Current device IP: %s" % str(IP)
if fFind(recv, "LINUX"):
Thread(target=customCommand,args=[sub(recv, "LINUX", defend)]).start()
def main():
print "Welcome to the Udoo Libray by David Smerkous\nThis is the debug console, Taking care of everything for you" \
+ "\n\nSearching for the Sam3x (Arduino part)"
there()
print "\nWaiting for command...\n\n"
while True:
val()
def onExit():
ser.close()
if __name__ == "__main__":
try:
main()
finally:
onExit()