1#!/usr/bin/env python3
2#
3#   Copyright 2016 - 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.
16"""
17Sanity tests for voice tests in telephony
18"""
19import time
20
21from acts.controllers.anritsu_lib._anritsu_utils import AnritsuError
22from acts.controllers.anritsu_lib.md8475a import MD8475A
23from acts.controllers.anritsu_lib.md8475a import CBCHSetup
24from acts.controllers.anritsu_lib.md8475a import CTCHSetup
25from acts.test_utils.tel.anritsu_utils import ETWS_WARNING_EARTHQUAKETSUNAMI
26from acts.test_utils.tel.anritsu_utils import ETWS_WARNING_OTHER_EMERGENCY
27from acts.test_utils.tel.anritsu_utils import cb_serial_number
28from acts.test_utils.tel.anritsu_utils import etws_receive_verify_message_lte_wcdma
29from acts.test_utils.tel.anritsu_utils import set_system_model_gsm
30from acts.test_utils.tel.anritsu_utils import set_system_model_lte
31from acts.test_utils.tel.anritsu_utils import set_system_model_wcdma
32from acts.test_utils.tel.anritsu_utils import set_usim_parameters
33from acts.test_utils.tel.anritsu_utils import set_post_sim_params
34from acts.test_utils.tel.tel_defines import NETWORK_MODE_CDMA
35from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_ONLY
36from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_UMTS
37from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
38from acts.test_utils.tel.tel_defines import RAT_1XRTT
39from acts.test_utils.tel.tel_defines import RAT_GSM
40from acts.test_utils.tel.tel_defines import RAT_LTE
41from acts.test_utils.tel.tel_defines import RAT_WCDMA
42from acts.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000
43from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
44from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
45from acts.test_utils.tel.tel_defines import RAT_FAMILY_UMTS
46from acts.test_utils.tel.tel_test_utils import ensure_network_rat
47from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
48from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
49from acts.test_utils.tel.tel_test_utils import start_qxdm_loggers
50from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
51from acts.test_decorators import test_tracker_info
52
53WAIT_TIME_BETWEEN_REG_AND_MSG = 15  # default 15 sec
54
55
56class TelLabEtwsTest(TelephonyBaseTest):
57    SERIAL_NO = cb_serial_number()
58
59    def setup_class(self):
60        super().setup_class()
61        self.ad = self.android_devices[0]
62        self.ad.sim_card = getattr(self.ad, "sim_card", None)
63        self.md8475a_ip_address = self.user_params[
64            "anritsu_md8475a_ip_address"]
65        self.wlan_option = self.user_params.get("anritsu_wlan_option", False)
66        self.md8475_version = self.user_params.get("md8475", "A")
67        self.ad.adb.shell("settings put secure cmas_additional_broadcast_pkg "
68                          "com.googlecode.android_scripting")
69        self.wait_time_between_reg_and_msg = self.user_params.get(
70            "wait_time_between_reg_and_msg", WAIT_TIME_BETWEEN_REG_AND_MSG)
71
72        try:
73            self.anritsu = MD8475A(self.md8475a_ip_address, self.wlan_option,
74                                   self.md8475_version)
75        except AnritsuError:
76            self.log.error("Error in connecting to Anritsu Simulator")
77            return False
78        return True
79
80    def setup_test(self):
81        if getattr(self, "qxdm_log", True):
82            start_qxdm_loggers(self.log, self.android_devices)
83        ensure_phones_idle(self.log, self.android_devices)
84        toggle_airplane_mode(self.log, self.ad, True)
85        return True
86
87    def teardown_test(self):
88        self.log.info("Stopping Simulation")
89        self.anritsu.stop_simulation()
90        toggle_airplane_mode(self.log, self.ad, True)
91
92    def teardown_class(self):
93        self.anritsu.disconnect()
94        return True
95
96    def _send_receive_etws_message(self, set_simulation_func, rat, message_id,
97                                   warning_message):
98        try:
99            [self.bts1] = set_simulation_func(self.anritsu, self.user_params,
100                                              self.ad.sim_card)
101            set_usim_parameters(self.anritsu, self.ad.sim_card)
102            if rat == RAT_LTE:
103                set_post_sim_params(self.anritsu, self.user_params,
104                                    self.ad.sim_card)
105            self.anritsu.start_simulation()
106
107            if rat == RAT_LTE:
108                preferred_network_setting = NETWORK_MODE_LTE_GSM_WCDMA
109                rat_family = RAT_FAMILY_LTE
110            elif rat == RAT_WCDMA:
111                self.bts1.wcdma_ctch = CTCHSetup.CTCH_ENABLE
112                self.ad.droid.telephonyToggleDataConnection(False)
113                preferred_network_setting = NETWORK_MODE_GSM_UMTS
114                rat_family = RAT_FAMILY_UMTS
115            elif rat == RAT_GSM:
116                self.bts1.gsm_cbch = CBCHSetup.CBCH_ENABLE
117                self.ad.droid.telephonyToggleDataConnection(False)
118                preferred_network_setting = NETWORK_MODE_GSM_ONLY
119                rat_family = RAT_FAMILY_GSM
120            elif rat == RAT_1XRTT:
121                preferred_network_setting = NETWORK_MODE_CDMA
122                rat_family = RAT_FAMILY_CDMA2000
123            else:
124                self.log.error("No valid RAT provided for ETWS test.")
125                return False
126
127            if not ensure_network_rat(
128                    self.log,
129                    self.ad,
130                    preferred_network_setting,
131                    rat_family,
132                    toggle_apm_after_setting=True):
133                self.log.error(
134                    "Failed to set rat family {}, preferred network:{}".format(
135                        rat_family, preferred_network_setting))
136                return False
137
138            self.anritsu.wait_for_registration_state()
139            if not etws_receive_verify_message_lte_wcdma(
140                    self.log, self.ad, self.anritsu,
141                    next(TelLabEtwsTest.SERIAL_NO), message_id,
142                    warning_message):
143                self.log.error("Phone {} Failed to receive ETWS message"
144                               .format(self.ad.serial))
145                return False
146        except AnritsuError as e:
147            self.log.error("Error in connection with Anritsu Simulator: " +
148                           str(e))
149            return False
150        except Exception as e:
151            self.log.error("Exception during ETWS send/receive: " + str(e))
152            return False
153        return True
154
155    def test_carrier_tmobile(self):
156        """ Sets the Carrier to TMO.
157        Returns: None
158        """
159        setattr(self.ad, "sim_card", "FiTMO")
160
161    def test_carrier_sprint(self):
162        """ Sets the Carrier to SPR.
163        Returns: None
164        """
165        setattr(self.ad, "sim_card", "FiSPR")
166
167    def test_carrier_uscc(self):
168        """ Sets the Carrier to USCC.
169        Returns: None
170        """
171        setattr(self.ad, "sim_card", "FiUSCC")
172
173    """ Tests Begin """
174
175    @test_tracker_info(uuid="af4a00d0-9a91-45d5-9f65-9541e64a57f2")
176    @TelephonyBaseTest.tel_test_wrap
177    def test_etws_earthquake_tsunami_lte(self):
178        """ETWS Earthquake and Tsunami warning message reception on LTE
179
180        Tests the capability of device to receive and inform the user
181        about the ETWS Earthquake and Tsunami warning message when camped on
182        LTE newtork
183
184        Steps:
185        1. Make Sure Phone is camped on LTE network
186        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
187
188        Expected Result:
189        Phone receives ETWS Earthquake and Tsunami warning message
190
191        Returns:
192            True if pass; False if fail
193        """
194        return self._send_receive_etws_message(set_system_model_lte, RAT_LTE,
195                                               ETWS_WARNING_EARTHQUAKETSUNAMI,
196                                               "LTE Earthquake and Tsunami")
197
198    @test_tracker_info(uuid="03785878-0319-413c-9190-d4e08f0edc33")
199    @TelephonyBaseTest.tel_test_wrap
200    def test_etws_other_emergency_lte(self):
201        """ETWS Other emergency warning message reception on LTE
202
203        Tests the capability of device to receive and inform the user
204        about the ETWS Other emergency warning message when camped on
205        LTE newtork
206
207        Steps:
208        1. Make Sure Phone is camped on LTE network
209        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
210
211        Expected Result:
212        Phone receives ETWS Earthquake and Tsunami warning message
213
214        Returns:
215            True if pass; False if fail
216        """
217        return self._send_receive_etws_message(set_system_model_lte, RAT_LTE,
218                                               ETWS_WARNING_OTHER_EMERGENCY,
219                                               "LTE ETWS Other Emergency")
220
221    @test_tracker_info(uuid="1ef4a5d7-9ceb-49eb-8ec7-5538625c8bd4")
222    @TelephonyBaseTest.tel_test_wrap
223    def test_etws_earthquake_tsunami_wcdma(self):
224        """ETWS Earthquake and Tsunami warning message reception on WCDMA
225
226        Tests the capability of device to receive and inform the user
227        about the ETWS Earthquake and Tsunami warning message when camped on
228        WCDMA newtork
229
230        Steps:
231        1. Make Sure Phone is camped on WCDMA network
232        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
233
234        Expected Result:
235        Phone receives ETWS Earthquake and Tsunami warning message
236
237        Returns:
238            True if pass; False if fail
239        """
240        return self._send_receive_etws_message(
241            set_system_model_wcdma, RAT_WCDMA, ETWS_WARNING_EARTHQUAKETSUNAMI,
242            "WCDMA Earthquake and Tsunami")
243
244    @test_tracker_info(uuid="71dc9650-d00a-4533-99f5-5cc301c21334")
245    @TelephonyBaseTest.tel_test_wrap
246    def test_etws_other_emergency_wcdma(self):
247        """ETWS Other emergency warning message reception on WCDMA
248
249        Tests the capability of device to receive and inform the user
250        about the ETWS Other emergency warning message when camped on
251        WCDMA newtork
252
253        Steps:
254        1. Make Sure Phone is camped on WCDMA network
255        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
256
257        Expected Result:
258        Phone receives ETWS Earthquake and Tsunami warning message
259
260        Returns:
261            True if pass; False if fail
262        """
263        return self._send_receive_etws_message(
264            set_system_model_wcdma, RAT_WCDMA, ETWS_WARNING_OTHER_EMERGENCY,
265            "WCDMA ETWS Other Emergency")
266
267    @test_tracker_info(uuid="a9fd9c0e-21bf-41d1-81d2-c34679052fe0")
268    @TelephonyBaseTest.tel_test_wrap
269    def test_etws_earthquake_tsunami_gsm(self):
270        """ETWS Earthquake and Tsunami warning message reception on GSM
271
272        Tests the capability of device to receive and inform the user
273        about the ETWS Earthquake and Tsunami warning message when camped on
274        GSM newtork
275
276        Steps:
277        1. Make Sure Phone is camped on GSM network
278        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
279
280        Expected Result:
281        Phone receives ETWS Earthquake and Tsunami warning message
282
283        Returns:
284            True if pass; False if fail
285        """
286        return self._send_receive_etws_message(set_system_model_gsm, RAT_GSM,
287                                               ETWS_WARNING_EARTHQUAKETSUNAMI,
288                                               "GSM Earthquake and Tsunami")
289
290    @test_tracker_info(uuid="0ae42f8d-1720-449c-9200-e88f7f1d2cbe")
291    @TelephonyBaseTest.tel_test_wrap
292    def test_etws_other_emergency_gsm(self):
293        """ETWS Other emergency warning message reception on GSM
294
295        Tests the capability of device to receive and inform the user
296        about the ETWS Other emergency warning message when camped on
297        GSM newtork
298
299        Steps:
300        1. Make Sure Phone is camped on GSM network
301        2. Send ETWS Earthquake and Tsunami warning message from Anritsu
302
303        Expected Result:
304        Phone receives ETWS Earthquake and Tsunami warning message
305
306        Returns:
307            True if pass; False if fail
308        """
309        return self._send_receive_etws_message(set_system_model_gsm, RAT_GSM,
310                                               ETWS_WARNING_OTHER_EMERGENCY,
311                                               "GSM ETWS Other Emergency")
312
313    """ Tests End """
314