meta-netmodule-bsp/recipes-connectivity/ublox-configuration/files/modem-config-dump.py

68 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
import serial
import os
import time
import re
CONF_PATH = '/run/modem'
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(ser, config_file, full_config_file, 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
full_conf = label + '\n' + s.decode('utf-8').strip("OK")
conf = (label + ':').ljust(20) + parser(s.decode('utf-8')) + '\n'
full_config_file.write(full_conf)
config_file.write(conf)
def dump_modem_config(ser):
os.makedirs(CONF_PATH, exist_ok=True)
full_config_file = open(CONF_PATH + '/modem-at.config', 'w')
config_file = open(CONF_PATH + '/modem.config', 'w')
read_config(ser, config_file, full_config_file, b'AT+CGMI', 'VendorID')
read_config(ser, config_file, full_config_file, b'AT+CGMM', 'ModelID')
read_config(ser, config_file, full_config_file, b'AT+CGMR', 'Firmware')
full_config_file.write('-' * 80 + '\n' * 2)
read_config(ser, config_file, full_config_file, b'AT+UCGDFLT?', 'Default APN', apn_parser)
read_config(ser, config_file, full_config_file, b'AT+UBMCONF?', 'Mode', mode_parser)
read_config(ser, config_file, full_config_file, b'AT+UUSBCONF?', 'USBprofile', usb_profile_parser)