77 lines
1.8 KiB
Python
Executable File
77 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import serial
|
|
import os
|
|
import time
|
|
import re
|
|
|
|
|
|
CONF_PATH = '/run/modem'
|
|
ser = serial.Serial('/dev/ttyACM0', 115200, timeout=0.5)
|
|
while True:
|
|
try:
|
|
ser.read()
|
|
break
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
os.makedirs(CONF_PATH, exist_ok=True)
|
|
config_full_file = open(CONF_PATH + '/modem-at.config', 'w')
|
|
config_file = open(CONF_PATH + '/modem.config', 'w')
|
|
|
|
|
|
def usb_profile_parser(output):
|
|
x = re.search('UUSBCONF:\s\d,"(.*)",', output)
|
|
return x.group(1)
|
|
|
|
def mode_parser(output):
|
|
x = re.search('UBMCONF:\s(\d)', output)
|
|
mode = int(x.group(1))
|
|
if mode == 1:
|
|
return "Router"
|
|
elif mode == 2:
|
|
return "Bridge"
|
|
else:
|
|
return "unsupported"
|
|
|
|
def apn_parser(output):
|
|
x = re.search('","(.*)",', output)
|
|
return x.group(1)
|
|
|
|
def default_parser(output):
|
|
x = re.search('(.*)\n.*\nOK',output)
|
|
return x.group(1)
|
|
|
|
def read_config(cmd, label='', parser=default_parser):
|
|
while True:
|
|
try:
|
|
ser.flushInput()
|
|
ser.write(cmd)
|
|
ser.write(b'\r')
|
|
s = ser.read_until(b'OK')
|
|
if(len(s.decode('utf-8')) < 4): # Avoid empty response just after reset
|
|
continue
|
|
break
|
|
except Exception as e:
|
|
print("Exception : " + str(e))
|
|
time.sleep(1)
|
|
continue
|
|
|
|
config_full_file.write(label + '\n')
|
|
config_full_file.write(s.decode('utf-8').strip("OK"))
|
|
|
|
config_file.write((label + ':').ljust(20))
|
|
config_file.write(parser(s.decode('utf-8')) + '\n')
|
|
|
|
read_config(b'AT+CGMI', 'VendorID')
|
|
read_config(b'AT+CGMM', 'ModelID')
|
|
read_config(b'AT+CGMR', 'Firmware')
|
|
|
|
config_full_file.write('-' * 80 + '\n' * 2)
|
|
|
|
read_config(b'AT+UCGDFLT?', 'Default APN', apn_parser)
|
|
read_config(b'AT+UBMCONF?', 'Mode', mode_parser)
|
|
read_config(b'AT+UUSBCONF?', 'USBprofile', usb_profile_parser)
|
|
|