From d146532b31798142f5f23d3577c7afe093bb13a7 Mon Sep 17 00:00:00 2001 From: Alexandre Bard Date: Mon, 1 Apr 2019 12:43:27 +0200 Subject: [PATCH] Add sim-config and reorder modem related scripts BugzID: 56371 --- .../files/modem-config-dump.service | 2 +- .../sim-config/files/sim-config.py | 109 ++++++++++++++++++ .../sim-config/files/sim-config.service | 11 ++ .../sim-config/files/sim.conf | 7 ++ recipes-connectivity/sim-config/sim-config.bb | 43 +++++++ .../ublox-configuration/files/ublox-config.py | 14 +-- .../files/ublox-config.service | 2 +- 7 files changed, 177 insertions(+), 11 deletions(-) create mode 100755 recipes-connectivity/sim-config/files/sim-config.py create mode 100644 recipes-connectivity/sim-config/files/sim-config.service create mode 100644 recipes-connectivity/sim-config/files/sim.conf create mode 100644 recipes-connectivity/sim-config/sim-config.bb diff --git a/recipes-connectivity/modem-config-dump/files/modem-config-dump.service b/recipes-connectivity/modem-config-dump/files/modem-config-dump.service index 83262b9..f07ced9 100644 --- a/recipes-connectivity/modem-config-dump/files/modem-config-dump.service +++ b/recipes-connectivity/modem-config-dump/files/modem-config-dump.service @@ -1,6 +1,6 @@ [Unit] Before=ModemManager.service -After=ublox-config.service +After=sim-config.service [Service] Type=oneshot diff --git a/recipes-connectivity/sim-config/files/sim-config.py b/recipes-connectivity/sim-config/files/sim-config.py new file mode 100755 index 0000000..22e3579 --- /dev/null +++ b/recipes-connectivity/sim-config/files/sim-config.py @@ -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(); + + diff --git a/recipes-connectivity/sim-config/files/sim-config.service b/recipes-connectivity/sim-config/files/sim-config.service new file mode 100644 index 0000000..c014032 --- /dev/null +++ b/recipes-connectivity/sim-config/files/sim-config.service @@ -0,0 +1,11 @@ +[Unit] +After=ublox-config.service + +[Service] +Type=oneshot +ExecStart=/usr/bin/sim-config + +[Install] +WantedBy=ModemManager.service + + diff --git a/recipes-connectivity/sim-config/files/sim.conf b/recipes-connectivity/sim-config/files/sim.conf new file mode 100644 index 0000000..d4b6d96 --- /dev/null +++ b/recipes-connectivity/sim-config/files/sim.conf @@ -0,0 +1,7 @@ +[default] +#SIM=esim +#SIM=main +#SIM=ui-top +#SIM=ui-btm +SIM=auto + diff --git a/recipes-connectivity/sim-config/sim-config.bb b/recipes-connectivity/sim-config/sim-config.bb new file mode 100644 index 0000000..505c7c8 --- /dev/null +++ b/recipes-connectivity/sim-config/sim-config.bb @@ -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/ +} + diff --git a/recipes-connectivity/ublox-configuration/files/ublox-config.py b/recipes-connectivity/ublox-configuration/files/ublox-config.py index a74efcb..1c87319 100755 --- a/recipes-connectivity/ublox-configuration/files/ublox-config.py +++ b/recipes-connectivity/ublox-configuration/files/ublox-config.py @@ -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) diff --git a/recipes-connectivity/ublox-configuration/files/ublox-config.service b/recipes-connectivity/ublox-configuration/files/ublox-config.service index a5610bd..b103e4d 100644 --- a/recipes-connectivity/ublox-configuration/files/ublox-config.service +++ b/recipes-connectivity/ublox-configuration/files/ublox-config.service @@ -1,5 +1,5 @@ [Unit] -Before=ModemManager.service +Before=sim-config.service [Service] Type=oneshot