summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Server-Code-New/GSMClass.py
blob: b932ab57819c7d43300483f4953fe461988aa4b0 (plain) (blame)
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
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.timer1 = timeout
		self.start = 0
			
		self.debugMode = 0 
			
	def portInit(self, timeout1):
		portName = self.portAddress[-4:]
		portExist = os.popen('dmesg | grep ' + portName).read()
		self.timer = timeout1
		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.flushOutput() #clean the output buffer from the serial port
				self.ser.flushInput() #clean the input buffer for serial port
				self.ser.write('AT\r')
				
				received = self.__receiveData()
				self.start=1
				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()
		if self.start == 0:
			self.timer = 5
		else:
			self.timer = self.timer1

		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