new get_modem_pin function, which reads from multiple sources

This commit is contained in:
Pavel Valach 2021-10-15 09:39:16 +02:00
parent ec4236c58f
commit 000bb0561b

View File

@ -24,6 +24,32 @@ class SIMError(ModemManagerError):
def __str__(self):
return repr(self.value)
def get_modem_pin():
def read_modem_pin(path):
with open(path, 'r') as mp:
return mp.readline().rstrip('\n')
# Check multiple paths
paths = [
os.path.join(
os.getcwd(),
'modem_pin.txt'
),
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'modem_pin.txt'
),
'/etc/modem_pin.txt'
]
for p in paths:
if os.path.isfile(p):
return read_modem_pin(fpath)
raise Exception('modem_pin.txt not found in any directory')
def get_modem():
# Retry for 5 seconds and give the modems time
retries = 0
@ -56,17 +82,15 @@ def unlock_sim(modem):
logger.debug("Modem status: {}".format(props))
if props and props['state'] == 2:
# check if the modem_pin.txt file exists
with open('modem_pin.txt', 'r') as mp:
mypin = mp.readline().rstrip('\n')
logger.info("Trying to unlock %s", sim_path)
with SystemBus() as bus:
sim = bus.get('.ModemManager1', sim_path)
try:
sim.SendPin(mypin, timeout=10000)
except GLib.Error as exc:
remaining_attempts = modem.UnlockRetries
raise SIMError('Failed to unlock PIN, remaining attempts: {}'.format(remaining_attempts)) from exc
mypin = get_modem_pin()
logger.info("Trying to unlock %s", sim_path)
with SystemBus() as bus:
sim = bus.get('.ModemManager1', sim_path)
try:
sim.SendPin(mypin, timeout=10000)
except GLib.Error as exc:
remaining_attempts = modem.UnlockRetries
raise SIMError('Failed to unlock PIN, remaining attempts: {}'.format(remaining_attempts)) from exc
def unlock_and_enable_modem():