forked from imoyer/gestalt
-
Notifications
You must be signed in to change notification settings - Fork 10
/
interfaces.py
592 lines (499 loc) · 20.5 KB
/
interfaces.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#----IMPORTS------------
import serial #for connecting to serial ports
import os
import sys
import platform
import time
import Queue
import threading
import socket
import itertools
from pygestalt.utilities import notice
from pygestalt import packets
from pygestalt import functions
from pygestalt import core
#----INTERFACE CLASSES------------
class interfaceShell(object):
'''Allows both the node and shell node to access the interface by acting as an intermediary.'''
def __init__(self, Interface = None, owner = None):
self.set(Interface, owner)
def set(self, Interface, owner = None):
'''Updates the interface contained by the shell'''
if owner: self.owner = owner #update owner
self.Interface = Interface #update interface
if Interface and self.owner: self.Interface.owner = self.owner #if interface, set owner
if Interface: Interface.initAfterSet() #initializes after owner is set, so that owner is reported correctly to user.
def setOwner(self, owner):
'''Updates the owner of the interface contained by the shell'''
self.owner = owner #used in the port acquisition process
if self.Interface:
self.Interface.owner = owner
def __getattr__(self, attribute):
'''Forwards attribute calls to the linked interface.'''
return getattr(self.Interface, attribute)
class baseInterface(object):
''' This base class could eventually provide a common foundation for all interfaces '''
def initAfterSet(self):
'''This method gets called when an interface is set into an interface shell.'''
pass
class socketInterface(baseInterface):
def __init__(self, IPAddress = '', IPPort = 27272): #all avaliable interfaces, port 27272
self.receiveIPAddress = IPAddress
self.receiveIPPort = IPPort
class socketUDPServer(socketInterface):
def initAfterSet(self): #gets called once interface is set into shell.
self.receiveSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP, would be socket.SOCK_STREAM for TCP
self.receiveSocket.bind((self.receiveIPAddress, self.receiveIPPort)) #bind to socket
notice(self, "opened socket on " + str(self.receiveIPAddress) + " port " + str(self.receiveIPPort))
self.transmitSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def receive(self):
data, addr = self.receiveSocket.recvfrom(1024)
notice(self, "'" + str(data) + "' from " + str(addr))
return (data, addr)
def transmit(self, remoteIPAddress, remoteIPPort, data):
# self.transmitSocket.sendto(data, (remoteIPAddress, remoteIPPort))
self.receiveSocket.sendto(data, (remoteIPAddress, remoteIPPort))
class devInterface(baseInterface):
''' Base class for interfaces mounted in the /dev/ folder.'''
def deviceScan(self, searchTerm):
'''returns available ports that match the search term'''
ports = os.listdir('/dev/')
matchingPorts = []
for port in ports:
if searchTerm in port:
matchingPorts.append('/dev/' + port)
return matchingPorts
def getSearchTerms(self, interfaceType):
'''returns the likely prefix for a serial port based on the operating system and device type'''
#define search strings in format {'OperatingSystem':'SearchString'}
ftdi = {'Darwin':'tty.usbserial-'}
lufa = {'Darwin':'tty.usbmodem'}
genericSerial = {'Darwin': 'tty.'}
searchStrings = {'ftdi':ftdi, 'lufa':lufa, 'genericSerial':genericSerial}
opSys = platform.system() #nominally detects the system
if interfaceType in searchStrings:
if opSys in searchStrings[interfaceType]:
return searchStrings[interfaceType][opSys]
else:
notice('getSearchTerm', 'operating system support not found for interface type '+ interfaceType)
return False
else:
notice('getSearchTerm', 'interface support not found for interface type ' + interfaceType)
return False
def waitForNewPort(self, searchTerms = '', timeout = 10):
'''Scans for a new port to appear in /dev/ and returns the name of the port.
Search terms is a list that can contain several terms. For now only implemented for one term.'''
timerCount = 0
devPorts = self.deviceScan(searchTerms)
numDevPorts = len(devPorts)
while True:
time.sleep(0.25)
timerCount += 0.25
if timerCount > timeout:
notice(self.owner, 'TIMOUT in acquiring a port.')
return False
currentDevPorts = self.deviceScan(searchTerms)
numCurrentDevPorts = len(currentDevPorts)
#port has been unplugged... update number of ports
if numCurrentDevPorts < numDevPorts:
devPorts = list(currentDevPorts)
numDevPorts = numCurrentDevPorts
elif numCurrentDevPorts > numDevPorts:
return list(set(currentDevPorts) - set(devPorts)) #returns all ports that just appeared
class serialInterface(devInterface):
'''Provides an interface to nodes connected thru a serial port on the host machine.'''
def __init__(self, baudRate, portName = None, interfaceType = None, owner = None, timeOut = 0.2, flowControl = None):
self.baudRate = baudRate
self.portName = portName
self.timeOut = timeOut
self.isConnected = False
self.owner = owner
#self.owner gets set by the interface shell, and contains a reference to the owning object
#this is useful to refer to the name of the object when acquiring the interface
self.port = None #will be replaced with a serial object when port is acquired
self.interfaceType = interfaceType
self.transmitQueue = Queue.Queue() #a queue is used to allow multiple threads to call transmit simultaneously.
def initAfterSet(self):
#if port name is provided, auto-connect
if self.portName:
self.connect(self.portName)
elif self.interfaceType: #if an interface type is provided, auto-acquire
self.acquirePort(self.interfaceType)
def getAvailablePorts(self, ports):
'''tests all provided ports and returns a subset of ports that are available'''
availablePorts = []
for port in ports:
try:
openPort = serial.Serial(port)
openPort.close()
availablePorts += [port]
except serial.SerialException, e:
continue
return availablePorts
def acquirePort(self, interfaceType = None):
'''Discovers and connects to a port by waiting for a new device to be plugged in.'''
#get search terms
if interfaceType:
searchTerm = self.getSearchTerms(interfaceType)
else:
searchTerm = self.getSearchTerms('genericSerial')
#try to find a single port that's open. This is good for when only a single device is attached.
availablePorts = self.getAvailablePorts(self.deviceScan(searchTerm))
if len(availablePorts) == 1:
self.portName = availablePorts[0]
return self.connect()
else:
notice(self.owner, "trying to acquire. Please plug me in.")
newPorts = self.waitForNewPort(searchTerm, 10) #wait for new port
if newPorts:
if len(newPorts) > 1:
notice(self.owner, 'Could not acquire. Multiple ports plugged in simultaneously.')
return False
else:
self.portName = newPorts[0]
return self.connect()
else: return False
def connect(self, portName = None):
'''Locates port for interface on host machine.'''
#check if port has been provided explicitly to connect function
if portName:
self.connectToPort(portName)
return True
#port hasn't been provided to connect function, check if assigned on instantiation
elif self.portName:
self.connectToPort(self.portName)
return True
#no port name provided
else:
notice(self, 'no port name provided.')
return False
def connectToPort(self, portName):
'''Actually connects the interface to the port'''
try:
self.port = serial.Serial(portName, self.baudRate, timeout = self.timeOut)
self.port.flushInput()
self.port.flushOutput()
notice(self, "port " + str(portName) + " connected succesfully.")
time.sleep(2) #some serial ports require a brief amount of time between opening and transmission
self.isConnected = True
self.startTransmitter()
return True
except:
notice(self, "error opening serial port "+ str(portName))
return False
def disconnect(self):
'''Disconnects from serial port.'''
self.port.close()
self.isConnected = False
def setDTR(self):
'''Used to reset the Arduino hardware.'''
if self.port:
self.port.setDTR()
return
def setTimeout(self, timeout):
'''Sets timeout for receiving on port.'''
if self.port:
try:
self.port.timeout = timeout
except:
notice(self, "could not set timeout: " + sys.exc_info()[0])
def startTransmitter(self):
'''Starts the transmit thread.'''
self.transmitter = self.transmitThread(self.transmitQueue, self.port)
self.transmitter.daemon = True
self.transmitter.start()
def transmit(self, data):
'''Sends request for data to be transmitted over the serial port. Format is as a list.'''
if self.isConnected:
self.transmitQueue.put(data) #converts to list in case data comes in as a string.
else: notice(self, 'serialInterface is not connected.')
class transmitThread(threading.Thread):
'''Handles transmitting data over the serial port.'''
def __init__(self, transmitQueue, port):
threading.Thread.__init__(self) #initialize threading superclass
self.transmitQueue = transmitQueue
self.port = port
def run(self):
'''Code run by the transmit thread.'''
while True:
transmitState, transmitPacket = self.getTransmitPacket()
if transmitState:
if self.port:
self.port.write(serialize(transmitPacket))
else: notice(self, 'Cannot Transmit - No Serial Port Initialized')
time.sleep(0.0005)
def getTransmitPacket(self):
'''Tries to fetch a packet from the transmit queue.'''
try:
return True, self.transmitQueue.get()
except:
return False, None
def receive(self):
'''Grabs one byte from the serial port.'''
if self.port:
return self.port.read()
else:
return None
def flushInput(self):
'''Flushes the input buffer.'''
self.port.flushInput()
return
class gestaltInterface(baseInterface):
'''Interface to Gestalt nodes based on the Gestalt protocol.'''
def __init__(self, name = None, interface = None, owner = None):
self.name = name #name becomes important for networked gestalt
self.owner = owner
self.interface = interfaceShell(interface, self) #uses the interfaceShell object for connecting to sub-interface
self.receiveQueue = Queue.Queue()
self.CRC = CRC()
self.nodeManager = self.nodeManager() #used to map network addresses (physical devices) to nodes
self.startInterfaceThreads() #this will start the receiver, packetRouter, channelPriority, and channelAccess threads.
#define standard gestalt packet
self.gestaltPacket = packets.packet(template = [ packets.pInteger('startByte', 1),
packets.pList('address', 2),
packets.pInteger('port', 1),
packets.pLength(),
packets.pList('payload')])
def validateIP(self, IP):
'''Makes sure that an IP address isn't already in use on the interface.'''
if str(IP) in self.nodeManager.address_node: return False
else: return True
class nodeManager(object):
'''Manages all nodes under the control of this interface.'''
def __init__(self):
self.node_address = {} #node : address
self.address_node = {} #address: node
def updateNodesAddresses(self, node, address):
oldNode = None
oldAddress = None
if str(address) in self.address_node: oldNode = self.address_node[str(address)]
if node in self.node_address: oldAddress = self.node_address[node]
if str(oldAddress) in self.address_node: self.address_node.pop(str(oldAddress))
if oldNode in self.node_address: self.node_address.pop(oldNode)
self.address_node.update({str(address):node})
self.node_address.update({node:address})
def getIP(self, node):
'''Returns IP address for a given node.'''
if node in self.node_address: return self.node_address[node]
else: return False
def getNode(self, IP):
IP = str(IP)
if IP in self.address_node: return self.address_node[IP]
else: return False
def assignNode(self, node, address):
'''Assigns a given node to the interface on a particular address.'''
self.nodeManager.updateNodesAddresses(node, address)
def transmit(self, virtualNode, port, packetSet, mode):
'''Transmits a packet set over the interface immediately.'''
#--BUILD START BYTE TABLE--
startByteTable = {'unicast': 72, 'multicast': 138} #unicast transmits to addressed node, multicast to all nodes on network
if mode in startByteTable:
startByte = startByteTable[mode]
else:
startByte = startByteTable['unicast']
#--TRANSMIT PACKETS--
address = self.nodeManager.getIP(virtualNode)
for packet in packetSet:
packetRoutable = self.gestaltPacket({'startByte':startByte, 'address': address, 'port':port, 'payload':packet}) #build packet
packetWChecksum = self.CRC(packetRoutable) #generate CRC
self.interface.transmit(packetWChecksum) #transmit packet thru interface
def commit(self, actionObject):
'''Puts actionObjects or actionSets into the channelPriority queue.'''
self.channelPriority.putActionObject(actionObject)
def startInterfaceThreads(self):
'''Initiates the receiver thread.'''
#START RECEIVE THREAD
self.receiver = self.receiveThread(self)
self.receiver.daemon = True
self.receiver.start()
#START ROUTER THREAD
self.packetRouter = self.packetRouterThread(self)
self.packetRouter.daemon = True
self.packetRouter.start()
#START CHANNEL PRIORITY THREAD
self.channelPriority = self.channelPriorityThread(self)
self.channelPriority.daemon = True
self.channelPriority.start()
#START CHANNEL ACCESS THREAD
self.channelAccess = self.channelAccessThread(self)
self.channelAccess.daemon = True
self.channelAccess.start()
class receiveThread(threading.Thread):
'''Gets packets from the network interface, interpreting them, and pushing them to the router queue.'''
def __init__(self, interface):
threading.Thread.__init__(self)
self.interface = interface
# print "GESTALT INTERFACE RECEIVE THREAD INITIALIZED"
def run(self):
packet = []
inPacket = False
packetPosition = 0
packetLength = 5
while True:
byte = self.interface.interface.receive() #get byte
if byte:
byte = ord(byte) #converts char to byte
if not inPacket:
if byte == 72 or byte == 138: #waits for start byte
inPacket = True
packet += [byte] #adds start byte to packet
packetPosition += 1 #increment packet position
continue #otherwise rejects packet
else: #in a packet
packet += [byte] #append byte to packet
if packetPosition == 4: #byte 2 contains the packet position
packetLength = byte
packetPosition += 1 #increment packet position
continue
if packetPosition < packetLength:
packetPosition += 1 #increment packet position
continue
if packetPosition == packetLength:
if self.interface.CRC.validate(packet): self.interface.packetRouter.routerQueue.put(packet[:len(packet)-1]) #check CRC, then send to router (minus CRC)
#initialize packet
packet = []
inPacket = False
packetPosition = 0
packetLength = 5
time.sleep(0.0005)
class packetRouterThread(threading.Thread):
'''Routes packets to their matching service routines, and executes the service routine within this thread.'''
def __init__(self, interface):
threading.Thread.__init__(self)
self.interface = interface
self.routerQueue = Queue.Queue()
def run(self):
while True:
routerState, routerPacket = self.getRouterPacket()
if routerState:
parsedPacket = self.interface.gestaltPacket.decode(routerPacket)
address = parsedPacket['address']
port = parsedPacket['port']
data = parsedPacket['payload']
destinationNode = self.interface.nodeManager.getNode(address)
if not destinationNode:
print "PACEKT RECEIVED FOR UNKNOWN ADDRESS "+ str(address)
continue
destinationNode.route(port, data)
time.sleep(0.0005)
def getRouterPacket(self):
try:
return True, self.routerQueue.get()
except:
return False, None
class channelAccessThread(threading.Thread):
'''Controls when action objects have access to the interface.
channelAccessQueue contains a serialized list of actionObjects which have been cleared for transmission and
are waiting for access to the channel.'''
def __init__(self, interface):
threading.Thread.__init__(self)
self.interface = interface
self.channelAccessQueue = Queue.Queue()
#might add another queue here for when actionObjects need to be transmitted immediately.
def run(self):
while True:
accessQueueState, actionObject = self.getActionObject() #get the next action object from the queue.
if accessQueueState:
actionObject.grantAccess() #actionObject now has control of the channel.
time.sleep(0.0005)
def getActionObject(self):
try:
return True, self.channelAccessQueue.get()
except:
return False, None
def putActionObject(self, actionObject):
self.channelAccessQueue.put(actionObject)
return True
class channelPriorityThread(threading.Thread):
'''Releases actionObjects to the channelAccessQueue, and when necessary first serializes actionSets into a series of action objects.'''
def __init__(self, interface):
threading.Thread.__init__(self)
self.interface = interface
self.channelPriorityQueue = Queue.Queue()
def run(self):
while True:
priorityQueueState, actionObject = self.getActionObject() #get the next actionObject or actionSet from the queue.
if priorityQueueState:
while not actionObject.isReleased(): #wait for actionObject or actionSet to be released
time.sleep(0.0005)
for thisActionObject in self.serialize(actionObject):
self.interface.channelAccess.putActionObject(thisActionObject) #transfer action object to the channel access thread.
time.sleep(0.0005)
def serialize(self, actionObject):
'''serializes actionSets into actionObjects.'''
if actionObject._type_ == 'actionObject': return [actionObject] #note _type_ is defined in the actionObject class
if actionObject._type_ == 'actionSet':
if actionObject.actionObjects[0]._type_ == 'actionObject':
#actionSet contains actionObjects rather than actionSequences
actionObjectStream = [thisActionObject for thisActionObject in actionObject.actionObjects]
actionObjectStream += [actionObject.actionObjects[0].virtualNode.syncRequest()] #generates a syncRequest.
return actionObjectStream
if actionObject.actionObjects[0]._type_ == 'actionSequence':
actionObjectStream = []
actionSequences = [actionSequence.actionObjects for actionSequence in actionObject.actionObjects]
syncLists = zip(*actionSequences) #takes parallel slices of actionObjects in the provided actionSequences.
for syncList in syncLists:
actionObjectStream += syncList
actionObjectStream += [syncList[0].virtualNode.syncRequest()]
return actionObjectStream
return []
def getActionObject(self):
try:
return True, self.channelPriorityQueue.get()
except:
return False, None
def putActionObject(self, actionObject):
self.channelPriorityQueue.put(actionObject)
return True
#----UTILITY CLASSES---------------
class CRC():
'''Generates CRC bytes and checks CRC validated packets.'''
def __init__(self):
self.polynomial = 7 #CRC-8: ATM=7, Dallas-Maxim = 49
self.crcTableGen()
def calculateByteCRC(self, byteValue):
'''Calculates Bytes in the CRC Table.'''
for i in range(8):
byteValue = byteValue << 1
if (byteValue//256) == 1:
byteValue = byteValue - 256
byteValue = byteValue ^ self.polynomial
return byteValue
def crcTableGen(self):
'''Generates a CRC table to make CRC generation faster.'''
self.crcTable = []
for i in range(256):
self.crcTable += [self.calculateByteCRC(i)]
def __call__(self, packet):
'''Generates CRC for an input packet.'''
#INITIALIZE CRC ALGORITHM
crc = 0
crcByte = 0
#CALCULATE CRC AND CONVERT preOutput BYTES TO CHR
output = []
for byte in packet:
crcByte = byte^crc
crc = self.crcTable[crcByte]
output += [byte]
output += [crc] #write crc to output
return output #NOTE: OUTPUT HAS BEEN CONVERTED TO A STRING
def validate(self, packet): #NOTE: ASSUMES INPUT IS A LIST OF NUMBERS
'''Checks CRC byte against packet.'''
crc = 0
crcByte = 0
packetLength = len(packet)
for char in packet[0:packetLength]:
crcByte = char^crc
crc = self.crcTable[crcByte]
if crc != 0: return False #CRC doesn't match
else: return True #CRC matches
#----METHODS-----------------------
def serialize(packet):
'''Converts packet into a string for transmission over a serial port.'''
if type(packet) == list:
return ''.join([chr(byte) for byte in packet])
elif type(packet) == str:
return packet
else:
print "Error: Packet must be either a list or a string."
return False