1# Copyright (C) 2024 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from mobly import asserts 16from mobly.controllers import android_device 17from net_tests_utils.host.python import tether_utils 18 19 20def assume_wifi_p2p_test_preconditions( 21 server_device: android_device, client_device: android_device 22) -> None: 23 """Preconditions check for running Wi-Fi P2P test.""" 24 server = server_device.connectivity_multi_devices_snippet 25 client = client_device.connectivity_multi_devices_snippet 26 27 # Assert pre-conditions 28 asserts.skip_if(not server.hasWifiFeature(), "Server requires Wifi feature") 29 asserts.skip_if(not client.hasWifiFeature(), "Client requires Wifi feature") 30 asserts.skip_if( 31 not server.isP2pSupported(), "Server requires Wi-fi P2P feature" 32 ) 33 asserts.skip_if( 34 not client.isP2pSupported(), "Client requires Wi-fi P2P feature" 35 ) 36 37 38def setup_wifi_p2p_server_and_client( 39 server_device: android_device, client_device: android_device 40) -> None: 41 """Set up the Wi-Fi P2P server and client, then connect them to establish a Wi-Fi P2P connection.""" 42 server = server_device.connectivity_multi_devices_snippet 43 client = client_device.connectivity_multi_devices_snippet 44 45 # Start Wi-Fi P2P on both server and client. 46 server.startWifiP2p() 47 client.startWifiP2p() 48 49 # Get the current device name 50 server_name = server.getDeviceName() 51 client_name = client.getDeviceName() 52 53 # Generate Wi-Fi P2P group passphrase with random characters. 54 group_name = "DIRECT-" + tether_utils.generate_uuid32_base64() 55 group_passphrase = tether_utils.generate_uuid32_base64() 56 57 # Server creates a Wi-Fi P2P group 58 server.createGroup(group_name, group_passphrase) 59 60 # Start Wi-Fi P2p peers discovery on both devices 61 server.startPeersDiscovery() 62 client.startPeersDiscovery() 63 64 # Ensure the target device has been discovered 65 server_address = client.ensureDeviceDiscovered(server_name) 66 client_address = server.ensureDeviceDiscovered(client_name) 67 68 # Server invites the device to the group 69 server.inviteDeviceToGroup(group_name, group_passphrase, client_address) 70 71 # Wait for a p2p connection changed intent to ensure the invitation has been 72 # received. 73 client.waitForP2pConnectionChanged(True, group_name) 74 # Accept the group invitation 75 client.acceptGroupInvitation(server_address) 76 77 # Server waits for connection request from client and accept joining 78 server.waitForPeerConnectionRequestAndAcceptJoining(client_address) 79 80 # Wait for a p2p connection changed intent to ensure joining the group 81 client.waitForP2pConnectionChanged(False, group_name) 82 83 # Ensure Wi-Fi P2P connected on both devices 84 client.ensureDeviceConnected(server_name) 85 server.ensureDeviceConnected(client_name) 86 87 88def cleanup_wifi_p2p( 89 server_device: android_device, client_device: android_device 90) -> None: 91 # Stop Wi-Fi P2P 92 server_device.connectivity_multi_devices_snippet.stopWifiP2p() 93 client_device.connectivity_multi_devices_snippet.stopWifiP2p() 94