-
Notifications
You must be signed in to change notification settings - Fork 17
/
humod.py
547 lines (466 loc) · 17.9 KB
/
humod.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
###########################################################################
# Sint Wind PI
# Copyright 2012 by Tonino Tarsi <tony.tarsi@gmail.com>
# Modem comunications based on Slawek Ligus pyhumod-0.03 module
#
# Please refer to the LICENSE file for conditions
# Visit http://www.vololiberomontecucco.it
#
##########################################################################
#
"""This module defines the base Modem() class."""
import serial
import threading
import Queue
import time
import os
import errors
import actions
import at_commands as atc
import signal
import config
import subprocess
from TTLib import *
import globalvars
import re
class Interpreter(threading.Thread):
"""Interpreter thread."""
def __init__(self, modem, queue, patterns):
self.active = True
self.queue = queue
self.patterns = patterns
self.modem = modem
threading.Thread.__init__(self)
def run(self):
"""Keep interpreting messages while active attribute is set."""
while self.active:
self.interpret(self.queue.get())
def stop(self):
self.active = False
def interpret(self, message):
"""Match message pattern with an action to take.
Arguments:
message -- string received from the modem.
"""
#print "D- Interpreter :" + message
for pattern_action in self.patterns:
pattern, action = pattern_action
if pattern.search(message):
action(self.modem, message)
break
else:
actions.null_action(self.modem, message)
class QueueFeeder(threading.Thread):
"""Queue feeder thread."""
def __init__(self, queue, ctrl_port, ctrl_lock):
self.active = True
self.queue = queue
self.ctrl_port = ctrl_port
self.ctrl_lock = ctrl_lock
threading.Thread.__init__(self)
def run(self):
"""Start the feeder thread."""
while self.active:
self.ctrl_lock.acquire()
try:
# set timeout
input_line = self.ctrl_port.readline()
#print "Debug- QueueFeeder : " + input_line
self.queue.put(input_line)
except:
log("Error in ctrl_port.readline")
systemRestart()
finally:
self.ctrl_lock.release()
# Putting the thread on idle between releasing
# and acquiring the lock for 100ms
time.sleep(2)
while ( globalvars.bAnswering):
#print "QueueFeeder sleep"
time.sleep(1)
def stop(self):
"""Stop the queue feeder thread."""
self.active = False
self.ctrl_port.write('\r\n')
def reset(self):
try:
self.ctrl_port.close()
self.ctrl_port.open()
self.ctrl_port.send_at("Z","",False) # Echo Disabled
self.ctrl_port.send_at("E0","",False) # Echo Disabled
self.ctrl_port.send_at("^CURC=0","",False) # disable periodic message
self.ctrl_port.send_at("+CVHU=0","",False) # enable hang-up voice call
self.ctrl_port.send_at("H","",False)
self.ctrl_port.send_at("+CMGF=1","",False)
except:
pass
class Prober(object):
"""Class responsible for reading in and queueing of control data."""
def __init__(self, modem):
self.queue = Queue.Queue()
self._interpreter = None
self._feeder = None
self.modem = modem
self.patterns = None
def _stop_interpreter(self):
"""Stop the interpreter."""
self._interpreter.active = False
self._interpreter.queue.put('')
def _start_interpreter(self):
"""Instanciate and start a new interpreter."""
self._interpreter = Interpreter(self.modem, self.queue, self.patterns)
self._interpreter.start()
def start(self, patterns=None):
"""Start the prober.
Starts two threads, an instance of QueueFeeder and Interpreter.
"""
self.patterns = patterns
if not patterns:
self.patterns = actions.STANDARD_ACTIONS
if self._feeder:
raise errors.HumodUsageError('Prober already started.')
else:
self._feeder = QueueFeeder(self.queue, self.modem.ctrl_port,
self.modem.ctrl_lock)
self._feeder.start()
self._start_interpreter()
def stop(self):
"""Stop the prober."""
if self._feeder:
self._stop_interpreter()
self._feeder.stop()
self._feeder = None
else:
raise errors.HumodUsageError('Prober not started.')
# pylint: disable-msg=R0904
# pylint: disable-msg=R0903
# pylint: disable-msg=R0902
# pylint: disable-msg=R0901
class ModemPort(serial.Serial):
"""Class extending serial.Serial by humod specific methods."""
def doRead(self,term,tout):
matcher = re.compile(term) #gives you the ability to search for anything
buff = ""
tic = time.clock()
buff += self.read(128)
# you can use if not ('\n' in buff) too if you don't like re
while ((time.clock - tic) < tout) and (not matcher.search(buff)):
buff += self.read(128)
return buff
def send_at(self, cmd, suffix, prefixed=True):
"""Send serial text to the modem.
Arguments:
self -- serial port to send to,
text -- text value to send,
prefixed -- boolean determining weather to strip the AT
command prefix from each output line.
Returns:
List of strings.
"""
self.write('AT%s%s\r' % (cmd, suffix))
#print "D: - Sending " + ('AT%s%s\r' % (cmd, suffix))
# Read in the echoed text.
# Check for errors and raise exception with specific error code.
input_line = self.readline()
errors.check_for_errors(input_line)
# Return the result.
if prefixed:
# If the text being sent is an AT command, only relevant context
# answer (starting with '+command:' value) will be returned by
#return_data(). Otherwise any string will be returned.
return self.return_data(cmd)
else:
return self.return_data()
def read_waiting(self):
"""Clear the serial port by reading all data waiting in it."""
return self.read(self.inWaiting())
def return_data(self, command=None):
"""Read until exit status is returned.
Returns:
data: List of right-stripped strings containing output
of the command.
Raises:
AtCommandError: If an error is returned by the modem.
"""
data = list()
while 1:
# Read in one line of input.
input_line = self.readline().rstrip()
# Check for errors and raise exception with specific error code.
errors.check_for_errors(input_line)
if input_line == 'OK':
return data
# Append only related data (starting with "command" contents).
if command:
if input_line.startswith(command):
prefix_length = len(command)+2
data.append(input_line[prefix_length:])
else:
# Append only non-empty data.
if input_line:
data.append(input_line)
class ConnectionStatus(object):
"""Data structure representing current state of the modem."""
def __init__(self):
"""Constructor for ConnectionStatus class."""
self.rssi = 0
self.uplink = 0
self.downlink = 0
self.bytes_tx = 0
self.bytes_rx = 0
self.link_uptime = 0
self.mode = None
def report(self):
"""Print connection status report."""
format = '%20s : %5s'
mapping = (('Signal Strength', self.rssi),
('Bytes rx', self.bytes_rx),
('Bytes tx', self.bytes_tx),
('Uplink (B/s)', self.uplink),
('Downlink (B/s)', self.downlink),
('Seconds uptime', self.link_uptime),
('Mode', self.mode))
print
for item in mapping:
print format % item
class Modem(atc.SetCommands, atc.GetCommands, atc.ShowCommands,
atc.InteractiveCommands, atc.EnterCommands):
"""Class representing a modem."""
# pylint: disable-msg=R0901
# pylint: disable-msg=R0904
status = ConnectionStatus()
#baudrate = 9600
PPPD_PARAMS = ['modem', 'crtscts', 'defaultroute', 'usehostname', '-detach', 'noauth' ,
'noipdefault', 'call', 'humod', 'user', 'ppp', 'usepeerdns',
'idle', '0', 'logfd', '8']
pppd_params = PPPD_PARAMS
_pppd_pid = None
_dial_num = '*99#'
def __init__(self, data,audio, ctrl,cfg):
"""Open a serial connection to the modem."""
self.cfg = cfg
self.IP = "None"
self.bAnswering = False
_dial_num = self.cfg.dialnum
if ( not cfg.usedongle ):
return
self.data_port = ModemPort()
self.data_port.setPort(data)
self.data_port.setBaudrate(self.cfg.modem_baudrate)
self.audio_port = ModemPort()
self.audio_port.setPort(audio)
self.audio_port.setBaudrate(self.cfg.audio_baudrate)
self.ctrl_port = ModemPort(ctrl, self.cfg.ctrl__baudrate,
timeout=self.cfg.prober_timeout)
self.ctrl_lock = threading.Lock()
self.prober = Prober(self)
atc.SetCommands.__init__(self)
atc.GetCommands.__init__(self)
atc.EnterCommands.__init__(self)
atc.InteractiveCommands.__init__(self)
atc.ShowCommands.__init__(self)
# Tony
self.ctrl_port.send_at("Z","",False) # Echo Disabled
self.ctrl_port.send_at("E0","",False) # Echo Disabled
self.ctrl_port.send_at("^CURC=0","",False) # disable periodic message
#self.ctrl_port.send_at("+CVHU=0","",False) # enable hang-up voice call
self.ctrl_port.send_at("H","",False)
# def answerCall(self,listOfMessages): # Tony 2012
# """answer a call with a list of message provided."""
# audio_port = self.audio_port
# audio_port.open()
# self.answer()
# self.set_destination_port(2)
#
# time.sleep(0.5)
# for message in listOfMessages:
# time.sleep(0.2)
# if ( not os.path.exists(message)):
# log( "ERROR : File not found : " + message)
# continue
# f = open(message, "rb")
# while True:
# chunk = f.read(320)
# if chunk:
# #print "sending chunk"
# audio_port.write(chunk)
# time.sleep( 0.020 )
# else:
# break
# log( "DEBUG : Message sended : " )
# time.sleep(1)
# self.hangup()
# log( "DEBUG : hangup " )
# time.sleep(1)
# audio_port.close()
# log( "DEBUG : audio_port.close " )
def answerCallNew(self,listOfMessages): # Tony 2012
try:
globalvars.bAnswering = True
"""answer a call with a list of message provided."""
audio_port = self.audio_port
audio_port.open()
self.answer()
self.set_destination_port(2)
memfile = ""
for message in listOfMessages:
if ( not os.path.exists(message)):
log( "WARNING : File not found : " + message)
continue
f = open(message, "rb")
chunk = f.read()
memfile += chunk
i = 0
while ( (i) < len(memfile) ):
fine = min(len(memfile),i+320)
chunk = memfile[i:fine]
audio_port.write(chunk)
time.sleep( 0.020 )
#time.sleep( 0.020 )
i += 320
#log( "DEBUG : Message sended : " )
time.sleep(1)
self.hangup()
#log( "DEBUG : hangup " )
time.sleep(1)
globalvars.bAnswering = False
audio_port.close()
#log( "DEBUG : audio_port.close " )
return True
except:
log("Error in answering call. May be user has hangup")
globalvars.bAnswering = False
audio_port.close()
return False
def connectwvdial(self):
if ( self._pppd_pid != None ) :
self.disconnectwvdial()
if os.name != 'nt':
pid = os.fork()
else:
pid = 0
if pid:
self._pppd_pid = pid
else:
try:
logFile = datetime.datetime.now().strftime("log/wvdial_%d%m%Y.log")
os.system("wvdial -C " + self.cfg.operator + ".conf 2>> " + logFile)
except:
raise errors.PppdError('An error while starting wvdial.')
def disconnectwvdial(self):
"""Disconnect the modem."""
if self._pppd_pid:
#print self._pppd_pid
os.kill(self._pppd_pid, 15)
os.kill(self._pppd_pid+1, 15)
os.kill(self._pppd_pid+2, 15)
#os.kill(self._pppd_pid,signal.SIGINT) # tony
# os.waitpid(self._pppd_pid, 2)
self._pppd_pid = None
# print self._pppd_pid
# self.IP = "None"
# log("Disconnected")
return True
else:
log( "Not connected")
return False
def tryconnect(self, dialtone_check):
"""Use pppd to connect to the network."""
try:
# Modem is not connected if _pppd_pid is set to None.
if not self._pppd_pid:
data_port = self.data_port
#log( "Openind data")
data_port.open()
#log( "Opened")
data_port.write('ATZ\r\n')
print data_port.return_data()
data_port.write('ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0\r\n')
print data_port.return_data()
data_port.write('AT+CGDCONT=1,"IP","internet.wind",,\r\n')
print data_port.return_data()
if not dialtone_check:
data_port.write('ATX3\r\n')
print data_port.return_data()
data_port.write('ATDT%s\r\n' % self._dial_num)
print data_port.readline()
status = data_port.readline()
print status
if status.startswith('CONNECT' ) :
# pppd_args = [defaults.PPPD_PATH, self.baudrate, self.data_port.port] + self.pppd_params
pppd_args = [self.pppd_path, self.data_port.port ,self.baudrate ] + self.pppd_params
pid = os.fork()
if pid:
self._pppd_pid = pid
else:
try:
os.execv(self.pppd_path, pppd_args)
except:
raise errors.PppdError('An error while starting pppd.')
else:
last_pppd_result = os.waitpid(self._pppd_pid, os.WNOHANG)
if last_pppd_result != (0, 0):
# Reconnect.
self._pppd_pid = None
self.connect(dialtone_check)
else:
# Modem already connected.
raise errors.HumodUsageError('Modem already connected.')
return True
except:
return False
def connect(self, dialtone_check=True):
for i in range(1,5):
if self.tryconnect(dialtone_check):
return True
time.sleep(1)
return False
def disconnect(self):
"""Disconnect the modem."""
if self._pppd_pid:
#os.kill(self._pppd_pid, 15)
os.kill(self._pppd_pid,signal.SIGINT) # tony
os.waitpid(self._pppd_pid, 0)
self._pppd_pid = None
os.system('echo "AT0" > /dev/ttyUSB0')
self.data_port.close()
return True
else:
return False
#raise errors.HumodUsageError('Not connected.')
if __name__ == '__main__':
configfile = 'swpi.cfg'
if not os.path.isfile(configfile):
"Configuration file not found"
exit(1)
cfg = config.config(configfile)
modem = Modem(cfg.dongleDataPort,cfg.dongleAudioPort,cfg.dongleCtrlPort,cfg)
print modem
print ""
log( "Modem Model : " + modem.show_model())
log( "Revision : " + modem.show_revision())
log( "Modem Serial Number : " + modem.show_sn())
log( "Pin Status : " + modem.get_pin_status())
log( "Device Center : " + modem.get_service_center()[0] + " " + str(modem.get_service_center()[1]))
log( "Signal quality : " + str(modem.get_rssi()))
log( "Checking new sms messages...")
smslist = modem.sms_list()
for message in smslist:
print message
#modem.enable_textmode(True)
modem.enable_clip(True)
modem.enable_nmi(True)
# log("trying to send sms to %s" % cfg.number_to_send)
# modem.sms_send(cfg.number_to_send, "prova")
# log( "Trying to connect to internet with 3G dongle ....")
# time.sleep(1)
# modem.connectwvdial()
# time.sleep(2)
# waitForIP()
# IP = getIP()
# if IP != None:
# log("Connected with IP :" + IP)
# time.sleep(10)
#
# log( "Trying to disconnect 3G dongle ....")
# modem.disconnectwvdial()