ublox-gsm-config: Create a daemon to keep control of gpios

This commit is contained in:
Alexandre Bard 2019-07-25 08:19:21 +02:00
parent 97b9c7c32e
commit df39850735
2 changed files with 30 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import time
import os
import configparser
import gpiod
import signal
config_dump = __import__("modem-config-dump")
SERIAL_DEV = '/dev/ttyACM0'
@ -13,6 +14,9 @@ WWAN_DEV = '/sys/class/net/wwan0'
SIM_CONFIG = '/etc/sim.conf'
APN_CONFIG = '/etc/apn.conf'
sim_sw = gpiod.find_line('SIM_SW')
gsm_pwr_en = gpiod.find_line('GSM_SUP_EN')
def execute_and_check(ser, cmd):
tries = 0
MAX_TRY = 5
@ -32,14 +36,10 @@ def execute_and_check(ser, cmd):
tries = 0
def init_sim_sw():
line = gpiod.find_line('SIM_SW')
line.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT)
line.set_value(1)
sim_sw.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[ 1 ])
def switch_sim():
line = gpiod.find_line('SIM_SW')
line.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT)
line.set_value(0)
sim_sw.set_value(0)
def sim_present(ser):
cmd = b'AT+CCID?'
@ -138,11 +138,11 @@ def factory_reset():
execute_and_check(ser, b'AT+UFACTORY=2,1')
reset_modem(ser)
def poweroff(gpio):
def poweroff():
print('poweroff')
if not os.path.exists(SERIAL_DEV):
print('serial interface is not available')
gpio.set_value(0)
gsm_pwr_en.set_value(0)
return -1
ser = serial.Serial(SERIAL_DEV, 115200, timeout=0.5)
ser.write(b'AT+CPWROFF\r')
@ -150,15 +150,27 @@ def poweroff(gpio):
time.sleep(7.5)
gpio.set_value(0)
gsm_pwr_en.set_value(0)
def signal_handler(sig, frame):
poweroff()
sys.exit(0)
def daemonize():
n = os.fork()
if n > 0:
f = open('/run/ublox-gsm-config.pid', 'w')
f.write(str(n))
return
signal.signal(signal.SIGTERM, signal_handler)
while True:
time.sleep(10)
def help():
print('Usage : {} [stop|factory-reset]'.format(sys.argv[0]))
def main():
line = gpiod.find_line('GSM_SUP_EN')
line.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT)
init_sim_sw()
if len(sys.argv) == 2:
if sys.argv[1] == 'stop':
return poweroff(line)
@ -167,8 +179,9 @@ def main():
else:
return help()
# Power on
line.set_value(1)
# Power modem
init_sim_sw()
gsm_pwr_en.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT, default_vals= [ 1 ])
# Wait on device
while not os.path.exists(SERIAL_DEV):
@ -195,6 +208,7 @@ def main():
apn_setup(ser)
config_dump.dump_modem_config(ser)
ser.close()
daemonize()
if __name__ == '__main__':
main()

View File

@ -3,11 +3,9 @@ Description=ublox GSM Modem configuration
Before=ModemManager.service
[Service]
Type=oneshot
Type=forking
ExecStart=/usr/bin/ublox-gsm-config
ExecStop=/usr/bin/ublox-gsm-config stop
RemainAfterExit=yes
TimeoutStopSec=20
PIDFile=/run/ublox-gsm-config.pid
[Install]
RequiredBy=ModemManager.service