1#!/usr/bin/env python3
2#
3#   Copyright 2021 - Google
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
17from queue import Empty
18from acts_contrib.test_utils.tel.tel_defines import OverrideNetworkContainer
19from acts_contrib.test_utils.tel.tel_defines import DisplayInfoContainer
20from acts_contrib.test_utils.tel.tel_defines import EventDisplayInfoChanged
21
22def is_current_network_5g_nsa(ad, sub_id = None, mmwave = None, timeout=30):
23    """Verifies 5G NSA override network type
24    Args:
25        ad: android device object.
26        sub_id: The target SIM for querying.
27        mmwave: True to detect 5G millimeter wave, False to detect sub-6,
28            None to detect both.
29        timeout: max time to wait for event.
30
31    Returns:
32        True: if data is on nsa5g NSA
33        False: if data is not on nsa5g NSA
34    """
35    sub_id = sub_id if sub_id else ad.droid.subscriptionGetDefaultDataSubId()
36
37    def _nsa_display_monitor(ad, sub_id, mmwave, timeout):
38        ad.ed.clear_events(EventDisplayInfoChanged)
39        ad.droid.telephonyStartTrackingDisplayInfoChangeForSubscription(sub_id)
40        if mmwave:
41            nsa_band = OverrideNetworkContainer.OVERRIDE_NETWORK_TYPE_NR_MMWAVE
42        else:
43            nsa_band = OverrideNetworkContainer.OVERRIDE_NETWORK_TYPE_NR_NSA
44        try:
45            event = ad.ed.wait_for_event(
46                    EventDisplayInfoChanged,
47                    ad.ed.is_event_match,
48                    timeout=timeout,
49                    field=DisplayInfoContainer.OVERRIDE,
50                    value=nsa_band)
51            ad.log.info("Got expected event %s", event)
52            return True
53        except Empty:
54            ad.log.info("No event for display info change with <%s>", nsa_band)
55            ad.screenshot("5g_nsa_icon_checking")
56            return False
57        finally:
58            ad.droid.telephonyStopTrackingServiceStateChangeForSubscription(
59                sub_id)
60
61    if mmwave is None:
62        return _nsa_display_monitor(
63            ad, sub_id, mmwave=False, timeout=timeout) or _nsa_display_monitor(
64            ad, sub_id, mmwave=True, timeout=timeout)
65    else:
66        return _nsa_display_monitor(ad, sub_id, mmwave, timeout)
67
68
69def is_current_network_5g_sa(ad, sub_id = None, mmwave = None):
70    """Verifies 5G SA override network type
71
72    Args:
73        ad: android device object.
74        sub_id: The target SIM for querying.
75        mmwave: True to detect 5G millimeter wave, False to detect sub-6,
76            None to detect both.
77
78    Returns:
79        True: if data is on 5g SA
80        False: if data is not on 5g SA
81    """
82    sub_id = sub_id if sub_id else ad.droid.subscriptionGetDefaultDataSubId()
83    current_rat = ad.droid.telephonyGetCurrentDataNetworkTypeForSubscription(
84        sub_id)
85    # TODO(richardwychang): check SA MMWAVE when function ready.
86    sa_type = ['NR',]
87    if mmwave is None:
88        if current_rat in sa_type:
89            ad.log.debug("Network is currently connected to %s", current_rat)
90            return True
91        else:
92            ad.log.error(
93                "Network is currently connected to %s, Expected on %s",
94                current_rat, sa_type)
95            ad.screenshot("5g_sa_icon_checking")
96            return False
97    elif mmwave:
98        ad.log.error("SA MMWAVE currently not support.")
99        return False
100    else:
101        if current_rat == 'NR':
102            ad.log.debug("Network is currently connected to %s", current_rat)
103            return True
104        else:
105            ad.log.error(
106                "Network is currently connected to %s, Expected on NR",
107                current_rat)
108            ad.screenshot("5g_sa_icon_checking")
109            return False
110
111
112def is_current_network_5g(ad, sub_id = None, nr_type = None, mmwave = None,
113                          timeout = 30):
114    """Verifies 5G override network type
115
116    Args:
117        ad: android device object
118        sub_id: The target SIM for querying.
119        nr_type: 'sa' for 5G standalone, 'nsa' for 5G non-standalone.
120        mmwave: True to detect 5G millimeter wave, False to detect sub-6,
121            None to detect both.
122        timeout: max time to wait for event.
123
124    Returns:
125        True: if data is on 5G regardless of SA or NSA
126        False: if data is not on 5G refardless of SA or NSA
127    """
128    sub_id = sub_id if sub_id else ad.droid.subscriptionGetDefaultDataSubId()
129
130    if nr_type == 'nsa':
131        return is_current_network_5g_nsa(
132            ad, sub_id=sub_id, mmwave=mmwave, timeout=timeout)
133    elif nr_type == 'sa':
134        return is_current_network_5g_sa(ad, sub_id=sub_id, mmwave=mmwave)
135    else:
136        return is_current_network_5g_nsa(
137            ad, sub_id=sub_id, mmwave=mmwave,
138            timeout=timeout) or is_current_network_5g_sa(
139                ad, sub_id=sub_id, mmwave=mmwave)
140