summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2012-03-30 21:31:58 +0200
committerReiner Herrmann <reiner@reiner-h.de>2016-01-31 23:02:26 +0100
commitbceeea8a47acc2747093bdb0cd6a5ec0b900d836 (patch)
tree4fa9e6f720b63bf82d1324b3a9e9e8c88be67077
parent0d4d89054ae11780c719796d5218a944c76c9f66 (diff)
umtsmodem: improved usage help; added display of neighboring cells
-rwxr-xr-xumtsmodem.py143
1 files changed, 129 insertions, 14 deletions
diff --git a/umtsmodem.py b/umtsmodem.py
index 59280e4..b700584 100755
--- a/umtsmodem.py
+++ b/umtsmodem.py
@@ -1,18 +1,35 @@
#!/usr/bin/python
import serial
+import time
import sys
import re
modem_device = "/dev/ttyACM1"
apn = "web.vodafone.de"
+# TODO: AT*EGNCI (gsm-only)
+# AT*EWNCI (umts-only)
+# AT+COPS (operator select)
+#
+# *EGNCI: "26202","026E","00000000",6,50,24
+#
+# *EWNCI:
+# UARFCN,PSC,RSCP,ECNO,PathLoss
+# 10588 ,287,36 ,14 ,158
+#
+# UARFCN = Channel, RSCP = RSSI, CID = PSC
+# RNCid = CID_lang / 65536 (sometimes(?) == LAC)
+# CID = CID_lang % 65536
+#
def send_command(modem, cmd, text):
modem.write(cmd)
- print text + ": ",
+ if text != None:
+ print text + ": ",
response = modem.readlines()
- print ('ERROR\r\n' in response and "Error" or "OK")
+ if text != None:
+ print ('ERROR\r\n' in response and "Error" or "OK")
return response
@@ -29,6 +46,98 @@ def get_pin_state(modem):
return False
+# determine whether there is a gsm or umts connection
+# -1: unknown
+# 0: gsm
+# 2: umts
+def connection_mode(modem):
+ response = send_command(modem, 'AT+COPS?\r', None)
+ for line in response:
+ if line.startswith('+COPS: '):
+ values = line[7:].strip().split(',')
+ if len(values) == 1:
+ return -1
+ else:
+ return int(values[-1])
+ return -1
+
+
+def cell_info(modem):
+ cells = set()
+
+ # get GSM cell information
+ send_command(modem, 'AT+CFUN=5\r', None)
+ while connection_mode(modem) != 0:
+ time.sleep(1)
+
+ # get connected station
+ 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)
+ cid = int(results[3].strip('"'), 16)
+ break
+
+ modem.write('AT*EHNET=2\r')
+ response = modem.readlines()
+ for line in response:
+ if line.startswith('*EHNET: '):
+ mcc = int(line[8:].rstrip().strip('"')[:3])
+ mnc = int(line[8:].rstrip().strip('"')[3:])
+ cells.add((mcc, mnc, lac, cid))
+
+
+ # check stations until all are known (i.e. no '???' in response)
+ retry = True
+ while retry:
+ retry = False
+ response = send_command(modem, 'AT*EGNCI\r', None)
+ if 'ERROR\r\n' in response:
+ print "error"
+ retry = True
+ time.sleep(1)
+ continue
+
+ for line in response:
+ if line.startswith('*EGNCI: '):
+ values = line[8:].rstrip().split(',')
+ try:
+ mcc = int(values[0].strip('"')[:3])
+ mnc = int(values[0].strip('"')[3:])
+ lac = int(values[1].strip('"'), 16)
+ cid = int(values[2].strip('"'), 16)
+ if cid == 0:
+ continue
+ cells.add((mcc, mnc, lac, cid))
+ except:
+ retry = True
+ print "retrying"
+ time.sleep(2)
+ break
+
+ for (mcc, mnc, lac, cid) in cells:
+ print "MCC: " + str(mcc) + ", MNC: " + str(mnc) + ", LAC: " + str(lac) + ", CellID: " + str(cid)
+
+
+# # get UMTS cell information
+# send_command(modem, 'AT+CFUN=6\r', None)
+# time.sleep(10)
+# response = send_command(modem, 'AT*EWNCI\r', None)
+# for line in response:
+# if line.startswith('*EWNCI: '):
+# print line.rstrip()
+
+# # switch back to default mode
+# send_command(modem, 'AT+CFUN=1\r', None)
+
+
def print_state(modem):
modem.timeout = 0.1
modem.write('AT+CSQ\r')
@@ -39,13 +148,15 @@ def print_state(modem):
print "Signal: " + signal + "/31"
break
- modem.write('AT+CIMI\r')
+ mcc, mnc, lac, cid = -1, -1, -1, -1
+ modem.write('AT*EHNET=2\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]
+ if line.startswith('*EHNET: '):
+ mcc = line[8:].rstrip().strip('"')[:3]
+ mnc = line[8:].rstrip().strip('"')[3:]
break
+
modem.write('AT+CREG=2\r')
modem.readlines()
modem.write('AT+CREG?\r')
@@ -57,12 +168,9 @@ def print_state(modem):
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
+ cid = int(results[3].strip('"'), 16) % 65536 # last 2 bytes for umts
break
- print "MCC: " + mcc + ", MNC: " + mnc + ", LAC: " + str(lac) + ", CellID: " + str(cellid)
+ print "MCC: " + str(mcc) + ", MNC: " + str(mnc) + ", LAC: " + str(lac) + ", CellID: " + str(cid)
@@ -87,7 +195,13 @@ def stop_modem(modem):
def show_usage():
- print "Usage: " + sys.argv[0] + " [start|stop|connect|disconnect|state]"
+ print "Usage: " + sys.argv[0] + " <command>"
+ print "\tstart: switch on modem and unlock sim"
+ print "\tstop: switch modem to powersave state"
+ print "\tconnect: connect to internet"
+ print "\tdisconnect: disconnect from internet"
+ print "\tstate: show signal/base station info"
+ print "\tcellinfo: show info about all received cells"
if len(sys.argv) != 2:
show_usage()
@@ -108,8 +222,9 @@ elif sys.argv[1] == "disconnect":
stop_modem(modem)
elif sys.argv[1] == "state":
print_state(modem)
-elif sys.argv[1] == "cells":
- print_cells(modem)
+elif sys.argv[1] == "cellinfo":
+# start_modem(modem)
+ cell_info(modem)
else:
show_usage()