From 000bb0561b96c2d2789822416a612d2d80b812c6 Mon Sep 17 00:00:00 2001 From: Pavel Valach Date: Fri, 15 Oct 2021 09:39:16 +0200 Subject: [PATCH] new get_modem_pin function, which reads from multiple sources --- sms_functions.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/sms_functions.py b/sms_functions.py index bf582b5..4c4502f 100644 --- a/sms_functions.py +++ b/sms_functions.py @@ -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():