1#!/usr/bin/env python3
2#
3#   Copyright 2018 - 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
17import acts.test_utils.wifi.wifi_test_utils as wutils
18import acts.utils
19import time
20
21from acts import asserts
22from acts import utils
23
24from acts.test_decorators import test_tracker_info
25from acts.test_utils.wifi.p2p.WifiP2pBaseTest import WifiP2pBaseTest
26from acts.test_utils.wifi.p2p import wifi_p2p_test_utils as wp2putils
27from acts.test_utils.wifi.p2p import wifi_p2p_const as p2pconsts
28
29WPS_PBC = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_PBC
30WPS_DISPLAY = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_DISPLAY
31WPS_KEYPAD = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_KEYPAD
32
33
34class WifiP2pManagerTest(WifiP2pBaseTest):
35    """Tests for APIs in Android's WifiP2pManager class.
36
37    Test Bed Requirement:
38    * At least two Android devices
39    * 3 Android devices for WifiP2pMultiPeersTest.py
40    """
41    def __init__(self, controllers):
42        WifiP2pBaseTest.__init__(self, controllers)
43
44    """Test Cases"""
45    @test_tracker_info(uuid="28ddb16c-2ce4-44da-92f9-701d0dacc321")
46    def test_p2p_discovery(self):
47        """Verify the p2p discovery functionality
48        Steps:
49        1. Discover the target device
50        2. Check the target device in peer list
51        """
52        self.log.info("Device discovery")
53        wp2putils.find_p2p_device(self.dut1, self.dut2)
54        wp2putils.find_p2p_device(self.dut2, self.dut1)
55
56    @test_tracker_info(uuid="0016e6db-9b46-44fb-a53e-10a81eee955e")
57    def test_p2p_connect_via_pbc_and_ping_and_reconnect(self):
58        """Verify the p2p connect via pbc functionality
59
60        Steps:
61        1. Request the connection which include discover the target device
62        2. check which dut is GO and which dut is GC
63        3. connection check via ping from GC to GO
64        4. disconnect
65        5. Trigger connect again from GO for reconnect test.
66        6. GO trigger disconnect
67        7. Trigger connect again from GC for reconnect test.
68        8. GC trigger disconnect
69        """
70        # Request the connection
71        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_PBC)
72
73        if wp2putils.is_go(self.dut1):
74            go_dut = self.dut1
75            gc_dut = self.dut2
76        elif wp2putils.is_go(self.dut2):
77            go_dut = self.dut2
78            gc_dut = self.dut1
79
80        go_ip = wp2putils.p2p_go_ip(gc_dut)
81        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
82
83        # trigger disconnect
84        wp2putils.p2p_disconnect(self.dut1)
85        wp2putils.check_disconnect(self.dut2)
86        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
87
88        self.log.info("Reconnect test, triggered by GO")
89        # trigger reconnect from GO
90        go_dut.ed.clear_all_events()
91        gc_dut.ed.clear_all_events()
92        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_PBC)
93        wp2putils.p2p_disconnect(go_dut)
94        wp2putils.check_disconnect(gc_dut)
95        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
96
97        # trigger reconnect from GC
98        self.log.info("Reconnect test, triggered by GC")
99        go_dut.ed.clear_all_events()
100        gc_dut.ed.clear_all_events()
101        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_PBC)
102        wp2putils.p2p_disconnect(gc_dut)
103        wp2putils.check_disconnect(
104            go_dut, timeout=p2pconsts.DEFAULT_GROUP_CLIENT_LOST_TIME)
105        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
106
107    @test_tracker_info(uuid="12bbe73a-5a6c-4307-9797-c77c7efdc4b5")
108    def test_p2p_connect_via_display_and_ping_and_reconnect(self):
109        """Verify the p2p connect via display functionality
110
111        Steps:
112        1. Request the connection which include discover the target device
113        2. check which dut is GO and which dut is GC
114        3. connection check via ping from GC to GO
115        4. disconnect
116        5. Trigger connect again from GO for reconnect test.
117        6. GO trigger disconnect
118        7. Trigger connect again from GC for reconnect test.
119        8. GC trigger disconnect
120        """
121        # Request the connection
122        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_DISPLAY)
123
124        if wp2putils.is_go(self.dut1):
125            go_dut = self.dut1
126            gc_dut = self.dut2
127        elif wp2putils.is_go(self.dut2):
128            go_dut = self.dut2
129            gc_dut = self.dut1
130
131        go_ip = wp2putils.p2p_go_ip(gc_dut)
132        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
133
134        # trigger disconnect
135        wp2putils.p2p_disconnect(self.dut1)
136        wp2putils.check_disconnect(self.dut2)
137        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
138
139        self.log.info("Reconnect test, triggered by GO")
140        # trigger reconnect from GO
141        go_dut.ed.clear_all_events()
142        gc_dut.ed.clear_all_events()
143        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_DISPLAY)
144        wp2putils.p2p_disconnect(go_dut)
145        wp2putils.check_disconnect(gc_dut)
146        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
147
148        # trigger reconnect from GC
149        self.log.info("Reconnect test, triggered by GC")
150        go_dut.ed.clear_all_events()
151        gc_dut.ed.clear_all_events()
152        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_DISPLAY)
153        wp2putils.p2p_disconnect(gc_dut)
154        wp2putils.check_disconnect(
155            go_dut, timeout=p2pconsts.DEFAULT_GROUP_CLIENT_LOST_TIME)
156        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
157
158    @test_tracker_info(uuid="efe88f57-5a08-4195-9592-2f6945a9d18a")
159    def test_p2p_connect_via_keypad_and_ping_and_reconnect(self):
160        """Verify the p2p connect via keypad functionality
161
162        Steps:
163        1. Request the connection which include discover the target device
164        2. check which dut is GO and which dut is GC
165        3. connection check via ping from GC to GO
166        4. disconnect
167        5. Trigger connect again from GO for reconnect test.
168        6. GO trigger disconnect
169        7. Trigger connect again from GC for reconnect test.
170        8. GC trigger disconnect
171        """
172        # Request the connection
173        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_KEYPAD)
174
175        if wp2putils.is_go(self.dut1):
176            go_dut = self.dut1
177            gc_dut = self.dut2
178        elif wp2putils.is_go(self.dut2):
179            go_dut = self.dut2
180            gc_dut = self.dut1
181
182        go_ip = wp2putils.p2p_go_ip(gc_dut)
183        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
184
185        # trigger disconnect
186        wp2putils.p2p_disconnect(self.dut1)
187        wp2putils.check_disconnect(self.dut2)
188        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
189
190        self.log.info("Reconnect test, triggered by GO")
191        # trigger reconnect from GO
192        go_dut.ed.clear_all_events()
193        gc_dut.ed.clear_all_events()
194        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_KEYPAD)
195        wp2putils.p2p_disconnect(go_dut)
196        wp2putils.check_disconnect(gc_dut)
197        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
198
199        # trigger reconnect from GC
200        self.log.info("Reconnect test, triggered by GC")
201        go_dut.ed.clear_all_events()
202        gc_dut.ed.clear_all_events()
203        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_KEYPAD)
204        wp2putils.p2p_disconnect(gc_dut)
205        wp2putils.check_disconnect(
206            go_dut, timeout=p2pconsts.DEFAULT_GROUP_CLIENT_LOST_TIME)
207        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
208