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
33class WifiP2pManagerTest(WifiP2pBaseTest):
34    """Tests for APIs in Android's WifiP2pManager class.
35
36    Test Bed Requirement:
37    * At least two Android devices
38    * 3 Android devices for WifiP2pMultiPeersTest.py
39    """
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(go_dut)
104        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
105
106    @test_tracker_info(uuid="12bbe73a-5a6c-4307-9797-c77c7efdc4b5")
107    def test_p2p_connect_via_display_and_ping_and_reconnect(self):
108        """Verify the p2p connect via display functionality
109
110        Steps:
111        1. Request the connection which include discover the target device
112        2. check which dut is GO and which dut is GC
113        3. connection check via ping from GC to GO
114        4. disconnect
115        5. Trigger connect again from GO for reconnect test.
116        6. GO trigger disconnect
117        7. Trigger connect again from GC for reconnect test.
118        8. GC trigger disconnect
119        """
120        # Request the connection
121        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_DISPLAY)
122
123        if wp2putils.is_go(self.dut1):
124            go_dut = self.dut1
125            gc_dut = self.dut2
126        elif wp2putils.is_go(self.dut2):
127            go_dut = self.dut2
128            gc_dut = self.dut1
129
130        go_ip = wp2putils.p2p_go_ip(gc_dut)
131        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
132
133        # trigger disconnect
134        wp2putils.p2p_disconnect(self.dut1)
135        wp2putils.check_disconnect(self.dut2)
136        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
137
138        self.log.info("Reconnect test, triggered by GO")
139        # trigger reconnect from GO
140        go_dut.ed.clear_all_events()
141        gc_dut.ed.clear_all_events()
142        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_DISPLAY)
143        wp2putils.p2p_disconnect(go_dut)
144        wp2putils.check_disconnect(gc_dut)
145        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
146
147        # trigger reconnect from GC
148        self.log.info("Reconnect test, triggered by GC")
149        go_dut.ed.clear_all_events()
150        gc_dut.ed.clear_all_events()
151        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_DISPLAY)
152        wp2putils.p2p_disconnect(gc_dut)
153        wp2putils.check_disconnect(go_dut)
154        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
155
156    @test_tracker_info(uuid="efe88f57-5a08-4195-9592-2f6945a9d18a")
157    def test_p2p_connect_via_keypad_and_ping_and_reconnect(self):
158        """Verify the p2p connect via keypad functionality
159
160        Steps:
161        1. Request the connection which include discover the target device
162        2. check which dut is GO and which dut is GC
163        3. connection check via ping from GC to GO
164        4. disconnect
165        5. Trigger connect again from GO for reconnect test.
166        6. GO trigger disconnect
167        7. Trigger connect again from GC for reconnect test.
168        8. GC trigger disconnect
169        """
170        # Request the connection
171        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_KEYPAD)
172
173        if wp2putils.is_go(self.dut1):
174            go_dut = self.dut1
175            gc_dut = self.dut2
176        elif wp2putils.is_go(self.dut2):
177            go_dut = self.dut2
178            gc_dut = self.dut1
179
180        go_ip = wp2putils.p2p_go_ip(gc_dut)
181        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
182
183        # trigger disconnect
184        wp2putils.p2p_disconnect(self.dut1)
185        wp2putils.check_disconnect(self.dut2)
186        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
187
188        self.log.info("Reconnect test, triggered by GO")
189        # trigger reconnect from GO
190        go_dut.ed.clear_all_events()
191        gc_dut.ed.clear_all_events()
192        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_KEYPAD)
193        wp2putils.p2p_disconnect(go_dut)
194        wp2putils.check_disconnect(gc_dut)
195        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
196
197        # trigger reconnect from GC
198        self.log.info("Reconnect test, triggered by GC")
199        go_dut.ed.clear_all_events()
200        gc_dut.ed.clear_all_events()
201        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_KEYPAD)
202        wp2putils.p2p_disconnect(gc_dut)
203        wp2putils.check_disconnect(go_dut)
204        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
205