Add sim-config and reorder modem related scripts

BugzID: 56371
This commit is contained in:
Alexandre Bard 2019-04-01 12:43:27 +02:00
parent c3135842b5
commit d146532b31
7 changed files with 177 additions and 11 deletions

View File

@ -1,6 +1,6 @@
[Unit]
Before=ModemManager.service
After=ublox-config.service
After=sim-config.service
[Service]
Type=oneshot

View File

@ -0,0 +1,109 @@
#!/usr/bin/env python3
import serial
import os
import time
import configparser
import sys
CONFIG = '/etc/sim.conf'
SERIAL_DEV = '/dev/ttyACM0'
GPIO_PATH = '/sys/class/gpio/'
GPIO_SIM_SW = '44'
GPIO_SIM_RST = '57'
RST_FILE = '/tmp/modem-reset-required'
rst = False
def write_to_file(file, value):
f = open(file, 'w')
f.write(value)
f.close()
def reset_modem():
print("modem reset")
write_to_file(GPIO_PATH + 'export', GPIO_SIM_RST)
write_to_file(GPIO_PATH + 'gpio' + GPIO_SIM_RST + '/direction', 'out')
write_to_file(GPIO_PATH + 'gpio' + GPIO_SIM_RST + '/value', '1')
time.sleep(2)
write_to_file(GPIO_PATH + 'gpio' + GPIO_SIM_RST + '/value', '0')
print("reset done")
# Wait until modem rebooted
while True:
dirs = os.listdir("/dev")
for file in dirs:
if file == 'ttyACM2':
sys.exit(0);
time.sleep(1)
def is_rst_required():
global rst
if rst:
return True
elif os.path.exists(RST_FILE):
return True
else:
return False
def switch_sim():
global rst
write_to_file(GPIO_PATH + 'export', GPIO_SIM_SW)
write_to_file(GPIO_PATH + 'gpio' + GPIO_SIM_SW + '/direction', 'out')
write_to_file(GPIO_PATH + 'gpio' + GPIO_SIM_SW + '/value', '0')
rst = True
def sim_present(ser):
cmd = b'AT+CCID?'
while True:
try:
ser.flushInput()
ser.write(cmd)
ser.write(b'\r')
s = ser.read_until(b'OK')
if b'ERROR' in s:
return False
elif b'OK' in s:
return True
except Exception as e:
print("Exception : " + str(e))
time.sleep(1)
return s
def check_sim_presence():
while not os.path.exists(SERIAL_DEV):
time.sleep(1)
ser = serial.Serial(SERIAL_DEV, 115200, timeout=0.5)
while True:
try:
ser.read()
break
except Exception:
pass
if sim_present(ser):
print("sim present")
else:
print("no sim, switching to esim")
switch_sim()
ser.close()
def read_config():
config = configparser.ConfigParser()
config.read(CONFIG)
print(config.get('default', 'SIM'))
sim = config.get('default', 'SIM')
if sim == 'auto':
check_sim_presence()
elif sim == 'esim' or sim == 'ui-top':
switch_sim()
# ui-btm and main are default
if is_rst_required():
reset_modem()
read_config();

View File

@ -0,0 +1,11 @@
[Unit]
After=ublox-config.service
[Service]
Type=oneshot
ExecStart=/usr/bin/sim-config
[Install]
WantedBy=ModemManager.service

View File

@ -0,0 +1,7 @@
[default]
#SIM=esim
#SIM=main
#SIM=ui-top
#SIM=ui-btm
SIM=auto

View File

@ -0,0 +1,43 @@
SUMMARY = "Select the wanted sim card"
DESCRIPTION = "Allows to select which sim card to use from a config file"
AUTHOR = "Alexandre Bard"
SECTION = "connectivity"
LICENSE = "GPLv2+"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/GPL-2.0;md5=801f80980d171dd6425610833a22dbe6"
PR = "r2"
RDEPENDS_${PN}="python3-pyserial"
inherit systemd
SRC_URI = " \
file://sim-config.service \
file://sim-config.py \
file://sim.conf \
"
S = "${WORKDIR}"
INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
SYSTEMD_SERVICE_${PN} = "sim-config.service"
SYSTEMD_AUTO_ENABLE ?= "enable"
FILES_${PN}_append = " \
/lib \
/usr \
/etc \
"
do_install () {
install -d ${D}${systemd_unitdir}/system/
install -m 0644 sim-config.service ${D}${systemd_unitdir}/system/
install -d ${D}/usr/bin
install -m 0755 sim-config.py ${D}/usr/bin/sim-config
install -d ${D}/etc
install -m 0644 sim.conf ${D}/etc/
}

View File

@ -41,14 +41,10 @@ execute_and_check(b'AT+UUSBCONF=2,"ECM",0')
print("Resetting modem")
ser.write(b'AT+CFUN=16\r')
# Let the reset be done by sim-config to avoid 2 resets
# ser.write(b'AT+CFUN=16\r')
f = open('/tmp/modem-reset-required', 'w')
f.write('1')
f.close()
ser.close()
# Wait until modem rebooted
while True:
dirs = os.listdir("/dev")
for file in dirs:
if file == 'ttyACM2':
sys.exit(0);
time.sleep(1)

View File

@ -1,5 +1,5 @@
[Unit]
Before=ModemManager.service
Before=sim-config.service
[Service]
Type=oneshot