summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/tricode/GSMHandler.py
diff options
context:
space:
mode:
authorTriatmoko2011-10-30 19:13:21 +0100
committerTriatmoko2011-10-30 19:13:21 +0100
commitdce0f2dd6e43541fbc956448b9a2a2b66b4ae30e (patch)
treeb85eca5b0ff719e26e9314bb69417d00a9a8afcf /For Weekly Test/tricode/GSMHandler.py
parentMerge branch 'master' of lab.ks.uni-freiburg.de:lsfks/projekte/gsm-selftest (diff)
downloadgsm-selftest-dce0f2dd6e43541fbc956448b9a2a2b66b4ae30e.tar.gz
gsm-selftest-dce0f2dd6e43541fbc956448b9a2a2b66b4ae30e.tar.xz
gsm-selftest-dce0f2dd6e43541fbc956448b9a2a2b66b4ae30e.zip
GSM Handler new version
Diffstat (limited to 'For Weekly Test/tricode/GSMHandler.py')
-rw-r--r--For Weekly Test/tricode/GSMHandler.py342
1 files changed, 342 insertions, 0 deletions
diff --git a/For Weekly Test/tricode/GSMHandler.py b/For Weekly Test/tricode/GSMHandler.py
new file mode 100644
index 0000000..afeb96f
--- /dev/null
+++ b/For Weekly Test/tricode/GSMHandler.py
@@ -0,0 +1,342 @@
+import ServerClass
+import GSMClass
+import LogFileClass
+from time import sleep
+import sys
+import setproctitle
+
+global portListen
+global portAddress
+
+
+if len(sys.argv) <> 4:
+ print "Error given command"
+ sys.exit(2)
+
+
+handler = sys.argv[3]
+portAddress = sys.argv[2]
+portListen = int(sys.argv[1])
+
+def initLogfile():
+ global nameOfLogFile
+
+ nameOfLogFile = str(handler)+' handler.log'
+
+baudRate = 19200
+
+initLogfile()
+logger = LogFileClass.Logging(nameOfLogFile)
+
+errorCount = 0
+
+logger.logEvent('')
+
+whileCounter =0
+
+#define global varibales
+global lastState
+global resetState
+global deviceError
+lastState = 0
+resetState = 0
+deviceError = 0
+
+def initSystem():
+ print 'init system'
+
+ global handlerSocket
+ global gsmDevice
+ global initTalkVar
+ global lastState
+ global initDevice
+ global numberToCall
+ global resetState
+ global deviceError
+
+ initTalkVar = 0 #variable used to know if we initialized the start talk
+ lastState = 0 #variable used to know
+ numberToCall = '000' #number to call
+ resetState = 0
+
+ handlerSocket = ServerClass.ServerHandler(portListen)
+ logger.logEvent('LISTEN ON PORT: '+str(portListen))
+ anyConnection = handlerSocket.openSocket()
+ #add this if you need it
+ gsmDevice = GSMClass.serialPort(portAddress, baudRate, 10)
+ initDevice = gsmDevice.portInit(5)
+ ########################################################
+
+ #add nice formating to the log file :)
+
+ print 'any connection ' + str(anyConnection)
+ print 'initDevice ' + str(initDevice)
+
+ if initDevice!= 1:
+ deviceError = 1
+ else:
+ deviceError = 0
+
+
+ if anyConnection == 1 and initDevice == 1:
+ logger.logEvent('CONNECTION ESTABLISHED AND DEVICE WORKING: ' + str(handlerSocket.connectedTo()))
+ return 1
+
+ elif anyConnection == 1 and initDevice != 0:
+ logger.logEvent('$connection established but device not working: ' + str(handlerSocket.connectedTo()))
+ resetState = 1
+ return 2
+ else:
+ logger.logEvent('$no connection')
+ logger.logEvent('$else case in init system' + str(anyConnection) + ' ' + str(initDevice) + ' ')
+ resetState = 1
+ return 0
+
+def receiveMessage(timeout):
+
+ message = str(handlerSocket.receiveData(timeout))
+ print 'I RECEIVED THE FOLLOWING MESSAGE' + message
+ if message == 'NO DATA':
+ print 'try to close the connection' + str(handlerSocket.closeConnection())
+ global resetState
+ resetState = 1
+ return 1
+ if message != '0' and message !='NO DATA':
+ print 'in receive message', message, lastState
+
+ if message == 'HELLO HANDLER' and lastState == 0:
+ outcome = initTalk()
+ if outcome == 1:
+ logger.logEvent('TALK INITIALIZATION SENT')
+ else:
+ logger.logEvent('$talk initialization not sent: ' + str(outcome))
+
+ elif message == 'RECEIVER' and lastState == 1:
+ outcome = initReceiver()
+ if outcome == 1:
+ logger.logEvent('RECEIVER READY SENT')
+ else:
+ logger.logEvent('$receiver ready not sent: ' + str(outcome))
+ return 7
+
+ elif message == 'RECEIVE START' and lastState == 2:
+ outcome = receiveStart()
+ print 'outcome ' + str(outcome)
+ if outcome == 1:
+ logger.logEvent('RECEIVE STATUS REPORTED')
+ return 2
+ else:
+ logger.logEvent('$receive status not reported: ' + str(outcome))
+ return 3
+
+ elif message[0:6] == 'CALLER' and lastState == 1:
+ outcome = initCaller()
+
+ global numberToCall
+ numberToCall = message[7:]
+ if outcome == 1:
+ logger.logEvent('CALLER READY SENT')
+ else:
+ logger.logEvent('$caller ready not sent: ' + str(outcome))
+ return 6
+
+ elif message == 'CALL START' and lastState == 4:
+ outcome = callStart(numberToCall)
+ print 'outcome ' + str(outcome)
+ if outcome == 1:
+ logger.logEvent('CALLER STATUS SENT')
+ return 4
+ else:
+ logger.logEvent('$caller status not sent: ' + str(outcome))
+ return 5
+
+ elif message == 'TERMINATE CONNECTION' and (lastState == 5 or lastState == 3):
+ outcome = terminateConnection()
+ if outcome == 1:
+ logger.logEvent('TERMINATED THE CONNECTION AFTER TEST')
+ else:
+ logger.logEvent('$connection could not be terminated after the test: ' + str(outcome))
+
+ elif message == 'TERMINATE CONNECTION':
+ outcome = terminateConnection()
+ if outcome == 1:
+ logger.logEvent('TERMINATED THE CONNECTION IN MIDDLE. IN STATE: ' + str(lastState) )
+ else:
+ logger.logEvent('$connection could not be terminated in middle: ' + str(outcome) + ' in state: ' + str(lastState))
+ else:
+ print message
+ outcome = other()
+ logger.logEvent('other appeared')
+
+ return 1
+ #return 0
+
+########INIT TALK PART########
+def initTalk():
+ print 'init talk'
+ #initialize the talk between handler and controller
+ global lastState
+ test = gsmDevice.portInit(2)
+ if test != 1:
+ test = gsmDevice.portInit(2)
+ if test != 1:
+ sendMessage = handlerSocket.sendData('DEVICE NOT READY')
+ else:
+ lastState = 1
+ sendMessage = handlerSocket.sendData('HELLO CONTROLLER')
+ else:
+ lastState = 1
+ sendMessage = handlerSocket.sendData('HELLO CONTROLLER')
+ return sendMessage
+##############################
+
+
+########RECEIVE PART########
+def initReceiver():
+ print 'initReceiver'
+
+ #init function to initialize the receiver
+ global lastState
+ lastState = 2
+ sendMessage = handlerSocket.sendData('RECEIVER READY')
+ return sendMessage
+
+def receiveStart():
+ print 'receiveStart'
+
+ #wait for a call and report if you received it and it was successfull or not
+ global lastState
+ global deviceError
+ lastState = 3
+ if deviceError == 0:
+ receiveCall = gsmDevice.receiveCall()
+ if receiveCall == 'TIMEOUT':
+ deviceError = 1
+ print 'device error in RECEIVE'
+ else:
+ receiveCall = 0
+
+ if deviceError == 0:
+ if receiveCall == 1:
+ callSuccess = 'CALL OK'
+ else:
+ callSuccess = 'CALL NOT OK'
+
+
+
+ sendMessage = handlerSocket.sendData(callSuccess)
+ if deviceError==0:
+ tryHangUp = gsmDevice.hangUp()
+ return sendMessage
+############################
+
+
+########CALL PART########
+def initCaller():
+ print 'initCaller1'
+
+ #initialize caller here
+ global lastState
+ lastState = 4
+ sendMessage = handlerSocket.sendData('CALLER READY')
+ return sendMessage
+
+def callStart(numberToCall):
+ print 'initCaller2'
+
+ #call the number here
+ global lastState
+ global deviceError
+ lastState = 5
+
+ callSuccess = 'CALL NOT OK'
+
+ if deviceError==0:
+ tryCall = gsmDevice.callNumber(numberToCall)
+ if tryCall == 'TIMEOUT':
+ deviceError = 1
+ print 'device error in CALL'
+ else:
+ tryCall=0
+
+
+ if tryCall != 1:
+ callSuccess = 'CALL NOT OK'
+ else:
+ print 'try call result'+ str(tryCall)
+ if tryCall != 'TIMEOUT':
+ sleep(2)
+ activeCall = gsmDevice.currentCall()
+ counter = 0
+ while(activeCall!=1):
+ sleep(1)
+ activeCall = gsmDevice.currentCall()
+ if counter == 10:
+ break
+ counter += 1
+
+ if activeCall == 1:
+ callSuccess = 'CALL OK'
+ else:
+ callSuccess = 'CALL NOT OK'
+
+ handResponse = handlerSocket.sendData(callSuccess)
+
+ if deviceError==0:
+ tryHangUp = gsmDevice.hangUp()
+
+ return handResponse
+#########################
+
+
+########TERMINATE PART########
+def terminateConnection():
+ print 'terminate connection'
+ global resetState
+ close = handlerSocket.closeConnection()
+ resetState = 1
+ return close
+##############################
+
+########TERMINATE PART########
+def other():
+ print 'other'
+ global lastState
+ global resetState
+
+ close = handlerSocket.closeConnection()
+ lastState = 8
+ resetState = 1
+ return 1
+##############################
+
+
+
+while 1:
+
+ test = initSystem()
+ if test == 1:
+ print 'initialized system'
+ receivedMessage = 0
+ while receivedMessage < 8 and resetState!= 1:
+ if receivedMessage == 4 or receivedMessage == 5 or receivedMessage == 2 or receivedMessage == 3:
+ receivedMessage = receiveMessage(10)
+ else:
+ receivedMessage = receiveMessage(30)
+ elif test ==2:
+ print 'initialized system but device not working'
+ logger.logEvent('initialized system but device not working')
+ receivedMessage = 0
+ while receivedMessage < 4 and resetState!= 1:
+ handlerSocket.sendData('DEVICE NOT READY')
+ if receivedMessage == 4 or receivedMessage == 5 or receivedMessage == 2 or receivedMessage == 3:
+ receivedMessage = receiveMessage(10)
+ else:
+ receivedMessage = receiveMessage(30)
+
+ elif test ==0:
+ print 'nobody can connect, reboot board, restart cellphone'
+ logger.logEvent('nobody can connect, reboot board, restart cellphone')
+
+ del handlerSocket
+ del gsmDevice