ublox-gsm-config: Create a daemon to keep control of gpios
This commit is contained in:
		
							parent
							
								
									97b9c7c32e
								
							
						
					
					
						commit
						df39850735
					
				|  | @ -6,6 +6,7 @@ import time | ||||||
| import os | import os | ||||||
| import configparser | import configparser | ||||||
| import gpiod | import gpiod | ||||||
|  | import signal | ||||||
| config_dump = __import__("modem-config-dump") | config_dump = __import__("modem-config-dump") | ||||||
| 
 | 
 | ||||||
| SERIAL_DEV = '/dev/ttyACM0' | SERIAL_DEV = '/dev/ttyACM0' | ||||||
|  | @ -13,6 +14,9 @@ WWAN_DEV = '/sys/class/net/wwan0' | ||||||
| SIM_CONFIG = '/etc/sim.conf' | SIM_CONFIG = '/etc/sim.conf' | ||||||
| APN_CONFIG = '/etc/apn.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): | def execute_and_check(ser, cmd): | ||||||
|     tries = 0 |     tries = 0 | ||||||
|     MAX_TRY = 5 |     MAX_TRY = 5 | ||||||
|  | @ -32,14 +36,10 @@ def execute_and_check(ser, cmd): | ||||||
|         tries = 0 |         tries = 0 | ||||||
| 
 | 
 | ||||||
| def init_sim_sw(): | def init_sim_sw(): | ||||||
|     line = gpiod.find_line('SIM_SW') |     sim_sw.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[ 1 ]) | ||||||
|     line.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT) |  | ||||||
|     line.set_value(1) |  | ||||||
| 
 | 
 | ||||||
| def switch_sim(): | def switch_sim(): | ||||||
|     line = gpiod.find_line('SIM_SW') |     sim_sw.set_value(0) | ||||||
|     line.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT) |  | ||||||
|     line.set_value(0) |  | ||||||
| 
 | 
 | ||||||
| def sim_present(ser): | def sim_present(ser): | ||||||
|     cmd = b'AT+CCID?' |     cmd = b'AT+CCID?' | ||||||
|  | @ -138,11 +138,11 @@ def factory_reset(): | ||||||
|     execute_and_check(ser, b'AT+UFACTORY=2,1') |     execute_and_check(ser, b'AT+UFACTORY=2,1') | ||||||
|     reset_modem(ser) |     reset_modem(ser) | ||||||
| 
 | 
 | ||||||
| def poweroff(gpio): | def poweroff(): | ||||||
|     print('poweroff') |     print('poweroff') | ||||||
|     if not os.path.exists(SERIAL_DEV): |     if not os.path.exists(SERIAL_DEV): | ||||||
|         print('serial interface is not available') |         print('serial interface is not available') | ||||||
|         gpio.set_value(0) |         gsm_pwr_en.set_value(0) | ||||||
|         return -1 |         return -1 | ||||||
|     ser = serial.Serial(SERIAL_DEV, 115200, timeout=0.5) |     ser = serial.Serial(SERIAL_DEV, 115200, timeout=0.5) | ||||||
|     ser.write(b'AT+CPWROFF\r') |     ser.write(b'AT+CPWROFF\r') | ||||||
|  | @ -150,15 +150,27 @@ def poweroff(gpio): | ||||||
| 
 | 
 | ||||||
|     time.sleep(7.5) |     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(): | def help(): | ||||||
|     print('Usage : {} [stop|factory-reset]'.format(sys.argv[0])) |     print('Usage : {} [stop|factory-reset]'.format(sys.argv[0])) | ||||||
| 
 | 
 | ||||||
| def main(): | 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 len(sys.argv) == 2: | ||||||
|         if sys.argv[1] == 'stop': |         if sys.argv[1] == 'stop': | ||||||
|             return poweroff(line) |             return poweroff(line) | ||||||
|  | @ -167,8 +179,9 @@ def main(): | ||||||
|         else: |         else: | ||||||
|             return help() |             return help() | ||||||
| 
 | 
 | ||||||
|     # Power on |     # Power modem | ||||||
|     line.set_value(1) |     init_sim_sw() | ||||||
|  |     gsm_pwr_en.request(consumer='ublox-config', type=gpiod.LINE_REQ_DIR_OUT, default_vals= [ 1 ]) | ||||||
| 
 | 
 | ||||||
|     # Wait on device |     # Wait on device | ||||||
|     while not os.path.exists(SERIAL_DEV): |     while not os.path.exists(SERIAL_DEV): | ||||||
|  | @ -195,6 +208,7 @@ def main(): | ||||||
|     apn_setup(ser) |     apn_setup(ser) | ||||||
|     config_dump.dump_modem_config(ser) |     config_dump.dump_modem_config(ser) | ||||||
|     ser.close() |     ser.close() | ||||||
|  |     daemonize() | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     main() |     main() | ||||||
|  |  | ||||||
|  | @ -3,11 +3,9 @@ Description=ublox GSM Modem configuration | ||||||
| Before=ModemManager.service | Before=ModemManager.service | ||||||
| 
 | 
 | ||||||
| [Service] | [Service] | ||||||
| Type=oneshot | Type=forking | ||||||
| ExecStart=/usr/bin/ublox-gsm-config | ExecStart=/usr/bin/ublox-gsm-config | ||||||
| ExecStop=/usr/bin/ublox-gsm-config stop | PIDFile=/run/ublox-gsm-config.pid | ||||||
| RemainAfterExit=yes |  | ||||||
| TimeoutStopSec=20 |  | ||||||
| 
 | 
 | ||||||
| [Install] | [Install] | ||||||
| RequiredBy=ModemManager.service | RequiredBy=ModemManager.service | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue