blob: a2d703ca5612d7d23804eb91f9a5560c6bdd405c [file] [log] [blame]
Junyu Laicea928f2024-04-24 16:15:30 +08001# 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#
15# Licensed under the Apache License, Version 2.0 (the "License");
16# you may not use this file except in compliance with the License.
17# You may obtain a copy of the License at
18#
19# http://www.apache.org/licenses/LICENSE-2.0
20#
21# Unless required by applicable law or agreed to in writing, software
22# distributed under the License is distributed on an "AS IS" BASIS,
23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24# See the License for the specific language governing permissions and
25# limitations under the License.
26
27import base64
28import uuid
29
30from mobly import asserts
31from mobly.controllers import android_device
32
33
34class UpstreamType:
35 CELLULAR = 1
36 WIFI = 2
37
38
39def generate_uuid32_base64() -> str:
40 """Generates a UUID32 and encodes it in Base64.
41
42 Returns:
43 str: The Base64-encoded UUID32 string. Which is 22 characters.
44 """
45 # Strip padding characters to make it safer for hotspot name length limit.
46 return base64.b64encode(uuid.uuid1().bytes).decode("utf-8").strip("=")
47
48
49def setup_hotspot_and_client_for_upstream_type(
50 server_device: android_device,
51 client_device: android_device,
52 upstream_type: UpstreamType,
53) -> (str, int):
54 """Setup the hotspot with a connected client with the specified upstream type.
55
56 This creates a hotspot, make the client connect
57 to it, and verify the packet is forwarded by the hotspot.
58 And returns interface name of both if successful.
59 """
60 server = server_device.connectivity_multi_devices_snippet
61 client = client_device.connectivity_multi_devices_snippet
62
63 # Assert pre-conditions specific to each upstream type.
64 asserts.skip_if(not client.hasWifiFeature(), "Client requires Wifi feature")
65 asserts.skip_if(
66 not server.hasHotspotFeature(), "Server requires hotspot feature"
67 )
68 if upstream_type == UpstreamType.CELLULAR:
69 asserts.skip_if(
70 not server.hasTelephonyFeature(), "Server requires Telephony feature"
71 )
72 server.requestCellularAndEnsureDefault()
73 elif upstream_type == UpstreamType.WIFI:
74 asserts.skip_if(
75 not server.isStaApConcurrencySupported(),
76 "Server requires Wifi AP + STA concurrency",
77 )
78 server.ensureWifiIsDefault()
79 else:
80 raise ValueError(f"Invalid upstream type: {upstream_type}")
81
82 # Generate ssid/passphrase with random characters to make sure nearby devices won't
83 # connect unexpectedly. Note that total length of ssid cannot go over 32.
84 test_ssid = "HOTSPOT-" + generate_uuid32_base64()
85 test_passphrase = generate_uuid32_base64()
86
87 # Create a hotspot with fixed SSID and password.
88 hotspot_interface = server.startHotspot(test_ssid, test_passphrase)
89
90 # Make the client connects to the hotspot.
91 client_network = client.connectToWifi(test_ssid, test_passphrase)
92
93 return hotspot_interface, client_network
94
95
96def cleanup_tethering_for_upstream_type(
97 server_device: android_device, upstream_type: UpstreamType
98) -> None:
99 server = server_device.connectivity_multi_devices_snippet
100 if upstream_type == UpstreamType.CELLULAR:
101 server.unrequestCellular()
102 # Teardown the hotspot.
103 server.stopAllTethering()