summaryrefslogtreecommitdiff
path: root/umtsmodem.py
blob: 59280e4ac92c490837bd270d982323c9f1d03be4 (plain)
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
#!/usr/bin/python

import serial
import sys
import re

modem_device = "/dev/ttyACM1"
apn = "web.vodafone.de"


def send_command(modem, cmd, text):
    modem.write(cmd)
    print text + ": ",
    response = modem.readlines()
    print ('ERROR\r\n' in response and "Error" or "OK")
    return response


def get_pin_state(modem):
    modem.write('AT+CPIN?\r')
    response = modem.readlines()
    if "+CPIN: SIM PIN\r\n" in response:
	return False
    elif "+CPIN: READY\r\n" in response:
	return True
    else:
	print "[get_pin_state] Unknown response",
	print response
	return False


def print_state(modem):
    modem.timeout = 0.1
    modem.write('AT+CSQ\r')
    response = modem.readlines()
    for line in response:
	if line.startswith('+CSQ: '):
	    signal = line.rstrip()[6:].split(',')[0]
	    print "Signal: " + signal + "/31"
	    break

    modem.write('AT+CIMI\r')
    response = modem.readlines()
    for line in response:
	if re.match(r'^\d+$', line.strip()):  # check if line is a number (IMSI)
	    mcc = line[:3]
	    mnc = line[3:5]
	    break
    modem.write('AT+CREG=2\r')
    modem.readlines()
    modem.write('AT+CREG?\r')
    response = modem.readlines()
    modem.write('AT+CREG=0\r')
    modem.readlines()
    for line in response:
	if line.startswith('+CREG: '):
	    results = line.rstrip()[7:].split(',')
	    if len(results) == 4:
		lac = int(results[2].strip('"'), 16)
		cellid = int(results[3].strip('"'), 16)
	    else:
		lac = -1
		cellid = -1
	    break
    print "MCC: " + mcc + ",  MNC: " + mnc + ",  LAC: " + str(lac) + ",  CellID: " + str(cellid)



def start_modem(modem):
    if not get_pin_state(modem):
	pin = raw_input("Please enter PIN: ")
	send_command(modem, 'AT+CPIN="' + pin + '"\r', "Sending PIN")
    send_command(modem, 'AT+CFUN=1\r', "Starting modem")


def connect(modem):
    send_command(modem, 'AT+CGDCONT=1,"IP","' + apn + '"\r', "Setting up")
    send_command(modem, 'AT*ENAP=1,1\r', "Connecting")


def disconnect(modem):
    send_command(modem, 'AT*ENAP=0\r', "Disconnecting")


def stop_modem(modem):
    send_command(modem, 'AT+CFUN=4\r', "Stopping")


def show_usage():
    print "Usage: " + sys.argv[0] + " [start|stop|connect|disconnect|state]"

if len(sys.argv) != 2:
    show_usage()
    sys.exit(0)

modem = serial.Serial(modem_device, timeout=0.5)
modem.readlines()

if sys.argv[1] == "start":
    start_modem(modem)
elif sys.argv[1] == "stop":
    stop_modem(modem)
elif sys.argv[1] == "connect":
    start_modem(modem)
    connect(modem)
elif sys.argv[1] == "disconnect":
    disconnect(modem)
    stop_modem(modem)
elif sys.argv[1] == "state":
    print_state(modem)
elif sys.argv[1] == "cells":
    print_cells(modem)
else:
    show_usage()


modem.close()