Skip to content

Commit

Permalink
Merge pull request #393 from Stefal/lte_modem_support
Browse files Browse the repository at this point in the history
Better modem state control
  • Loading branch information
Stefal authored May 29, 2024
2 parents 3b1b355 + 2eda2c3 commit c93c72e
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 67 deletions.
1 change: 1 addition & 0 deletions tools/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ start_services() {
systemctl restart gpsd.service
systemctl restart chrony.service
systemctl start rtkbase_archive.timer
grep grep -qE ^modem_at_port=\'[[:alnum:]]+\' "${rtkbase_path}"/settings.conf && systemctl enable --now modem_check.timer
grep -q "receiver='Septentrio_Mosaic-X5'" "${rtkbase_path}"/settings.conf && systemctl enable --now rtkbase_gnss_web_proxy.service
echo '################################'
echo 'END OF INSTALLATION'
Expand Down
112 changes: 112 additions & 0 deletions tools/modem_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#! /usr/bin/env python3

import os
import sys
import time
import nmcli
from sim_modem import *

nmcli.disable_use_sudo()
CONN_NAME='Cellular Modem'
MODEM_PORT='/dev/ttymodemAT'
USE_PUBLIC_IP=True
def sleep(timeout, retry=10):
def the_real_decorator(function):
def wrapper(*args, **kwargs):
retries = 0
while retries < retry:
try:
value = function(*args, **kwargs)
if value is not None:
return value
except:
print(f'{function.__name__}: Sleeping for {timeout + retries*timeout} seconds')
time.sleep(timeout + retries*timeout)
retries += 1

return wrapper
return the_real_decorator

@sleep(10)
def check_modem():
nmcli.connection.show(CONN_NAME)
if nmcli.connection.show(CONN_NAME).get("GENERAL.STATE") == 'activated':
return True

@sleep(10, retry=2)
def check_network_registration():
try:
modem = Modem(MODEM_PORT)
network_reg = int(modem.get_network_registration_status().split(",")[1])
if network_reg == 1 or network_reg == 2 or network_reg == 5:
return True
else:
return False
except Exception as e:
print(e)
raise Exception
finally:
modem.close()

@sleep(10)
def get_public_ip_address():
try:
modem = Modem(MODEM_PORT)
public_ip = modem.get_ip_address()

except Exception as e:
print (e)
raise Exception
finally:
modem.close()
return public_ip

@sleep(10)
def get_in_use_ip_address():
return nmcli.connection.show(CONN_NAME)['IP4.ADDRESS[1]'].split('/')[0]

@sleep(10)
def ping(host):
res = os.system("ping -c 4 " + host + ' >/dev/null')
return res == 0

check_modem()
network_reg = check_network_registration()
ip_in_use = get_in_use_ip_address()
public_ip = get_public_ip_address()
ping_host = ping('caster.centipede.fr') or ping('pch.net')

if ip_in_use == None or public_ip == None or network_reg == False or ping_host == False:
print("Internal Ip address in use: ", ip_in_use)
print("Modem public Ip address: ", public_ip)
print("Ping caster.centipede.fr or pch.net", ping_host)
print("Modem problem. Switching to airplane mode and back to normal")
try:
print("Connecting to modem...")
modem = Modem(MODEM_PORT)
print("Sending AT+CFUN=0")
modem.custom_read_lines('AT+CFUN=0')
time.sleep(20)
print("Sending AT+CFUN=1")
modem.custom_read_lines('AT+CFUN=1')
except Exception as e:
print(e)
finally:
modem.close()

elif USE_PUBLIC_IP and ip_in_use != public_ip:
try:
print("Internal Ip address in use: ", ip_in_use)
print("Modem public Ip address: ", public_ip)
modem = Modem(MODEM_PORT)
modem.set_usbnetip_mode(1)
print("Request to switch to public IP address done!")
print("It could take a few minutes to be active")
except Exception as e:
print(e)
finally:
print("closing modem connexion")
modem.close()
#else:
# print("We are already using the public Ip")

5 changes: 4 additions & 1 deletion tools/modem_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def arg_parse():
print("Data network registration", modem.get_eps_network_registration_status())
print("Network mode selection: ", modem.get_network_mode())
print("Current network mode: ", modem.get_current_network_mode().name)
print("Current network name/operator: ", modem.get_network_operator())
try:
print("Current network name/operator: ", modem.get_network_operator())
except:
print("Current network name/operator: NO SERVICE")
print("EU system information: ", modem.get_eu_system_informations())
data_mode = modem.get_data_connection_mode()
if data_mode.name == 'ECM':
Expand Down
62 changes: 0 additions & 62 deletions tools/switch_to_public_ip.py

This file was deleted.

8 changes: 4 additions & 4 deletions unit/modem_public_ip.service → unit/modem_check.service
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[Unit]
Description=Switch Simcom modem to public ip address
Description=Check Simcom modem ip address
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
Type=simple
User={user}
ExecStart={python_path} {script_path}/tools/switch_to_public_ip.py
ExecStart={python_path} {script_path}/tools/modem_check.py
Restart=on-failure
RestartSec=30
ProtectHome=read-only
ProtectSystem=strict

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
File renamed without changes.

0 comments on commit c93c72e

Please sign in to comment.