| # Copyright 2013 The ChromiumOS Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Probes SIM card information from 'modem status'. |
| |
| Description |
| ----------- |
| |
| The first usage of this test is to insert sim card, record ICCID (IMSI) value, |
| then remove sim card. |
| A 'modem reset' is needed after plugging SIM card. |
| It is not needed after removing SIM card. |
| The second usage of this test is to make sure that SIM card is not present. |
| A 'modem reset' is needed to avoid the case that SIM card is inserted without |
| a 'modem reset'. |
| Before running this test, modem carrier should be set to Generic UMTS. |
| |
| Test Procedure |
| -------------- |
| |
| 1. Insert sim card. |
| 2. The test records ICCID (IMSI) value automatically. |
| 3. Remove sim card. |
| |
| Dependency |
| ---------- |
| - `modem` |
| |
| Examples |
| -------- |
| An example: |
| |
| .. test_list:: |
| |
| generic_cellular_examples:CellularTests.ProbeSim |
| |
| """ |
| |
| import logging |
| from typing import List, Optional |
| |
| from cros.factory.test import event_log # TODO(chuntsen): Deprecate event log. |
| from cros.factory.test.i18n import _ |
| from cros.factory.test.rf import cellular |
| from cros.factory.test import test_case |
| from cros.factory.test import test_ui |
| from cros.factory.testlog import testlog |
| from cros.factory.utils.arg_utils import Arg |
| from cros.factory.utils import process_utils |
| |
| |
| _INSERT_CHECK_PERIOD_SECS = 1 |
| _INSERT_CHECK_MAX_WAIT = 60 |
| |
| |
| def CheckSimPresence() -> Optional[str]: |
| """Return the ICCID (IMSI) if a sim card exists.""" |
| for sim in cellular.ProbeSimInfo(['Imsi']): |
| if sim[0] is not None: |
| return str(sim[0]) |
| return None |
| |
| |
| def CheckSimAbsence() -> bool: |
| for sim in cellular.ProbeSimInfo(['Imsi']): |
| if sim[0] is not None: |
| return False |
| return True |
| |
| |
| class ProbeSIMCardArgs: |
| only_check_simcard_not_present: bool |
| only_check_simcard_present: bool |
| poll_modem_status: bool |
| modem_reset_commands: List[List[str]] |
| enable_modem_reset: bool |
| |
| |
| |
| class ProbeSIMCardTest(test_case.TestCase): |
| related_components = (test_case.TestCategory.WWAN, ) |
| ARGS = [ |
| Arg('only_check_simcard_not_present', bool, |
| 'Only checks sim card is not present', default=False), |
| Arg('only_check_simcard_present', bool, |
| 'Only checks sim card is present', default=False), |
| Arg('poll_modem_status', bool, |
| 'Polls modem status until the status is available', default=False), |
| Arg('modem_reset_commands', list, |
| 'A list of commands to reset modem', default=[['modem', 'reset']]), |
| Arg('enable_modem_reset', bool, |
| 'If true, reset modem before check status.', default=True)] |
| |
| args: ProbeSIMCardArgs |
| ui: test_ui.StandardUI |
| |
| def setUp(self): |
| self.reset_commands = self.args.modem_reset_commands |
| |
| def runTest(self): |
| if self.args.only_check_simcard_present: |
| self.CheckSIMCardState(CheckSimPresence, |
| 'Fail to make sure sim card is present') |
| elif self.args.only_check_simcard_not_present: |
| self.CheckSIMCardState(CheckSimAbsence, |
| 'Fail to make sure sim card is not present') |
| else: |
| self.ResetModem() |
| self.ui.SetState(_('Please insert the SIM card')) |
| iccid = self.WaitForSIMCard(CheckSimPresence) |
| logging.info('ICCID: %s', iccid) |
| event_log.Log('SIM_CARD_DETECTION', ICCID=iccid) |
| testlog.LogParam('ICCID', iccid) |
| |
| self.ui.SetState(_('Detected! Please remove the SIM card')) |
| self.WaitForSIMCard(CheckSimAbsence) |
| |
| def ResetModem(self): |
| """Resets modem.""" |
| if self.args.enable_modem_reset: |
| for command in self.args.modem_reset_commands: |
| process_utils.Spawn(command, call=True, log=True) |
| self.Sleep(_INSERT_CHECK_PERIOD_SECS) |
| |
| def GetModemStatus(self): |
| """Gets modem status.""" |
| status = process_utils.SpawnOutput(['modem', 'status'], log=True) |
| if not status: |
| status += process_utils.SpawnOutput(['mmcli', '-L'], log=True) |
| return status |
| |
| def CheckSIMCardState(self, predicator, fail_string: str) -> None: |
| self.ui.SetState(_('Checking SIM card is present or not...')) |
| |
| self.ResetModem() |
| |
| output = predicator() |
| if self.args.poll_modem_status: |
| total_delay = 0 |
| while not output: |
| self.Sleep(_INSERT_CHECK_PERIOD_SECS) |
| total_delay += _INSERT_CHECK_PERIOD_SECS |
| if total_delay >= _INSERT_CHECK_MAX_WAIT: |
| self.FailTask( |
| f'Failed to detect sim in {int(_INSERT_CHECK_MAX_WAIT)} seconds') |
| output = predicator() |
| |
| logging.info('%s', self.GetModemStatus()) |
| if output: |
| return |
| self.fail(fail_string) |
| |
| def WaitForSIMCard(self, predicator): |
| while True: |
| logging.info('%s', self.GetModemStatus()) |
| match = predicator() |
| if match: |
| return match |
| |
| self.Sleep(_INSERT_CHECK_PERIOD_SECS) |