1#!/usr/bin/env python3
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the 'License');
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an 'AS IS' BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16import time
17
18import acts.controllers.anritsu_lib.md8475a as md8475a
19from acts.controllers.cellular_lib.LteSimulation import LteSimulation
20
21# Time to wait for Anritsu's IMS CSCF state change
22MAX_WAIT_TIME_IMS_CSCF_STATE = 30
23# default ims virtual network id for Anritsu ims call test.
24DEFAULT_IMS_VIRTUAL_NETWORK_ID = 1
25
26
27class LteImsSimulation(LteSimulation):
28
29    LTE_BASIC_SIM_FILE = 'VoLTE_ATT_Sim.wnssp'
30    LTE_BASIC_CELL_FILE = 'VoLTE_ATT_Cell.wnscp'
31
32    def attach(self):
33        """ After attaching verify the UE has registered with the IMS server.
34
35        Returns:
36            True if the phone was able to attach, False if not.
37        """
38
39        if not super().attach():
40            return False
41
42        # The phone should have registered with the IMS server before attaching.
43        # Make sure the IMS registration was successful by verifying the CSCF
44        # status is SIP IDLE.
45        if not _wait_for_ims_cscf_status(
46                self.log,
47                self.simulator.anritsu,
48                DEFAULT_IMS_VIRTUAL_NETWORK_ID,
49                md8475a.ImsCscfStatus.SIPIDLE.value):
50            self.log.error('UE failed to register with the IMS server.')
51            return False
52
53        return True
54
55
56def _wait_for_ims_cscf_status(log,
57                              anritsu_handle,
58                              virtual_network_id,
59                              status,
60                              timeout=MAX_WAIT_TIME_IMS_CSCF_STATE):
61    """ Wait for IMS CSCF to be in expected state.
62
63    Args:
64        log: log object
65        anritsu_handle: anritsu object
66        virtual_network_id: virtual network id to be monitored
67        status: expected status
68        timeout: wait time
69    """
70    sleep_interval = 1
71    wait_time = timeout
72    while wait_time > 0:
73        if status == anritsu_handle.get_ims_cscf_status(virtual_network_id):
74            return True
75        time.sleep(sleep_interval)
76        wait_time = wait_time - sleep_interval
77    return False
78