summaryrefslogtreecommitdiffstats
path: root/For Weekly Test
diff options
context:
space:
mode:
Diffstat (limited to 'For Weekly Test')
-rwxr-xr-xFor Weekly Test/tricode/GSMClass.py304
-rwxr-xr-xFor Weekly Test/tricode/GSMHandler.py342
2 files changed, 0 insertions, 646 deletions
diff --git a/For Weekly Test/tricode/GSMClass.py b/For Weekly Test/tricode/GSMClass.py
deleted file mode 100755
index e994814..0000000
--- a/For Weekly Test/tricode/GSMClass.py
+++ /dev/null
@@ -1,304 +0,0 @@
-from serial import * #serial port library
-import string
-import sys
-import signal
-
-class TimeoutException(Exception):
- pass
-
-class serialPort():
-
- def __init__(self, portAddress, baudRate, timeout):
- self.portAddress = portAddress
- self.portExist = 0
- self.ser = 0
- self.baudRate = baudRate
- self.signalStrength = 0
- #self.callerConnected = -1
- self.timer = timeout
-
- self.debugMode = 0
-
- def portInit(self):
- portName = self.portAddress[-4:]
- portExist = os.popen('dmesg | grep ' + portName).read()
-
- if portExist == '':
- return 0
- else:
- try:
- self.ser = Serial(
- port=self.portAddress,
- baudrate=self.baudRate,
- bytesize=EIGHTBITS,
- parity=PARITY_NONE,
- stopbits=STOPBITS_ONE
- #timeout=0,
- #writeTimeout=0
- #xonxoff=0,
- #rtscts=0
- #interCharTimeout=None
- #I didn't need to set these variables :)
- )
-
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.write('AT\r')
-
- received = self.__receiveData()
-
- if received == 'OK':
- self.portExist = 1
- return 1 #cellphone is OK, receives commands
- elif received == 'TIMEOUT':
- return 'TIMEOUT'
- else:
- return 2 #cellphone has an error
-
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
-
- self.portExist = 3
- return 3
-
- def callNumber(self,numberToCall):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.write('ATD'+str(numberToCall)+';\r')
- if self.__receiveData() == 'OK':
- return 1
- else:
- return 4
-
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
-
- return 3
- else:
- return 0
-
-
- def hangUp(self):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.write('AT+CHUP\r')
- received = self.__receiveData()
- if received == 'OK':
- return 1
- elif received == 'ERROR':
- return 2 #other side hang up the call
- else:
- return 4
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
- return 3
- else:
- return 0
-
-
- def closePort(self):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.close()
- return 1
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
- return 3
- else:
- return 0
-
- def getSignalQuality(self):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.write('AT+CSQ\r')
- if self.__receiveData() == 'OK':
- if self.__receiveData == 'SIGNAL':
- return 1
- else:
- return 4
- else:
- return 2
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
- return 3
-
- else:
- return 0
-
- def receiveCall(self):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- if self.__receiveData() == 'RING':
- self.ser.write('ATA\r')
- if self.__receiveData()=='OK':
- return 1
- else:
- return 4
- else:
- return 2
-
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
- return 3
- else:
- return 0
-
- def currentCall(self):
- if self.portExist == 1:
- try:
- self.ser.flushInput() #clean the input buffer for serial port
- self.ser.write('AT+CLCC\r')
- received = self.__receiveData()
- if received == 'OK':
- return 2 #not yet in a call
- elif received == 'ACTIVE':
- return 1 #in a call
- elif received == 'HELD':
- return 5 #held call
- elif received == 'DIALING':
- return 6 #dialing
- elif received == 'ALERTING':
- return 7 #alerting the call
- elif received == 'INCOMING':
- return 8 #incoming call
- elif received == 'WAITING':
- return 9 #waiting for a call
-
- else:
- return received #in some other state
- except Exception, e:
- import traceback
- if self.debugMode == 1:
- print e
- print traceback.format_exc()
- return 3
- else:
- return 0
-
- def __receiveData(self):
- def timeout_handler(signum, frame):
- raise TimeoutException()
-
- old_handler = signal.signal(signal.SIGALRM, timeout_handler)
- signal.alarm(self.timer)
-
- bufferData = ''
- lines = ''
- line = ''
-
- try:
- while True:
- bufferData = bufferData + self.ser.read(self.ser.inWaiting()) #read the serial port and add it to the buffer variable
- if '\n' in bufferData: #if a new line character is found in the buffer then the cellphone has sent something
- lines = bufferData.split('\n') #parse the buffer variable with the new line character
- last_received = lines.pop(0) #put into last_received variable the first content from lines (FIFO)
-
- bufferData = '\n'.join(lines) #add a new line to the buffer variable
-
- last_received=last_received.split('\n') #parse the last received value with new lines
- line = last_received[0].replace(chr(13), '') #remove \r from the first parsed value in last_received and return me a nicely parsed value :)
- if self.debugMode == 1:
- if len(line) > 0:
- print line
-
- if line == 'OK':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'OK'
-
- elif line == 'ERROR':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'ERROR'
-
- elif line[0:11] == '+CME ERROR:':
- if self.debugMode == 1:
- print 'ERROR:', line
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'ERROR'
-
- elif line == 'RING':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'RING'
-
- elif line[0:5] == '+CSQ:': #+CSQ:
- space = int(string.find(line,' '))+1 #find the (space) sign
- coma = int(string.find(line,',')) #find the , (coma) sign
- self.signalStrength = (int(line[space:coma])*2)-113
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'SIGNAL'
-
- elif line == 'NO CARRIER':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'NO CARRIER'
-
- elif line == 'BUSY':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'BUSY'
-
- elif line[0:6] == '+CLCC:':
- #+CLCC: 1,0,
- #self.ser.flushInput()
- if line[11:12] == '0':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'ACTIVE'
- elif line[11:12] == '1':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'HELD'
- elif line[11:12] == '2':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'DIALING'
- elif line[11:12] == '3':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'ALERTING'
- elif line[11:12] == '4':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'INCOMING'
- elif line[11:12] == '5':
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'WAITING'
-
- except TimeoutException:
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- return 'TIMEOUT'
-
- except Exception, e:
- signal.signal(signal.SIGALRM, old_handler)
- signal.alarm(0)
- print 'NO GOOD'
- print e
- return 3
diff --git a/For Weekly Test/tricode/GSMHandler.py b/For Weekly Test/tricode/GSMHandler.py
deleted file mode 100755
index 76da327..0000000
--- a/For Weekly Test/tricode/GSMHandler.py
+++ /dev/null
@@ -1,342 +0,0 @@
-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 = GSMClass1.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