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. 16from acts import logger 17from acts.controllers.fuchsia_lib.base_lib import BaseLib 18 19COMMAND_SCAN = "wlan.scan" 20COMMAND_SCAN_FOR_BSS_INFO = "wlan.scan_for_bss_info" 21COMMAND_CONNECT = "wlan.connect" 22COMMAND_DISCONNECT = "wlan.disconnect" 23COMMAND_STATUS = "wlan.status" 24COMMAND_GET_IFACE_ID_LIST = "wlan.get_iface_id_list" 25COMMAND_GET_PHY_ID_LIST = "wlan.get_phy_id_list" 26COMMAND_DESTROY_IFACE = "wlan.destroy_iface" 27COMMAND_GET_COUNTRY = "wlan_phy.get_country" 28COMMAND_GET_DEV_PATH = "wlan_phy.get_dev_path" 29COMMAND_QUERY_IFACE = "wlan.query_iface" 30 31 32class FuchsiaWlanLib(BaseLib): 33 def __init__(self, addr, tc, client_id): 34 self.address = addr 35 self.test_counter = tc 36 self.client_id = client_id 37 self.log = logger.create_tagged_trace_logger(str(addr)) 38 39 def wlanStartScan(self): 40 """ Starts a wlan scan 41 42 Returns: 43 scan results 44 """ 45 test_cmd = COMMAND_SCAN 46 test_id = self.build_id(self.test_counter) 47 self.test_counter += 1 48 49 return self.send_command(test_id, test_cmd, {}) 50 51 def wlanScanForBSSInfo(self): 52 """ Scans and returns BSS info 53 54 Returns: 55 A dict mapping each seen SSID to a list of BSS Description IE 56 blocks, one for each BSS observed in the network 57 """ 58 test_cmd = COMMAND_SCAN_FOR_BSS_INFO 59 test_id = self.build_id(self.test_counter) 60 self.test_counter += 1 61 62 return self.send_command(test_id, test_cmd, {}) 63 64 def wlanConnectToNetwork(self, 65 target_ssid, 66 target_bss_desc, 67 target_pwd=None): 68 """ Triggers a network connection 69 Args: 70 target_ssid: the network to attempt a connection to 71 target_pwd: (optional) password for the target network 72 73 Returns: 74 boolean indicating if the connection was successful 75 """ 76 test_cmd = COMMAND_CONNECT 77 test_args = { 78 "target_ssid": target_ssid, 79 "target_pwd": target_pwd, 80 "target_bss_desc": target_bss_desc 81 } 82 test_id = self.build_id(self.test_counter) 83 self.test_counter += 1 84 85 return self.send_command(test_id, test_cmd, test_args) 86 87 def wlanDisconnect(self): 88 """ Disconnect any current wifi connections""" 89 test_cmd = COMMAND_DISCONNECT 90 test_id = self.build_id(self.test_counter) 91 self.test_counter += 1 92 93 return self.send_command(test_id, test_cmd, {}) 94 95 def wlanDestroyIface(self, iface_id): 96 """ Destroy WLAN interface by ID. 97 Args: 98 iface_id: the interface id. 99 100 Returns: 101 Dictionary, service id if success, error if error. 102 """ 103 test_cmd = COMMAND_DESTROY_IFACE 104 test_args = {"identifier": iface_id} 105 test_id = self.build_id(self.test_counter) 106 self.test_counter += 1 107 108 return self.send_command(test_id, test_cmd, test_args) 109 110 def wlanGetIfaceIdList(self): 111 """ Get a list if wlan interface IDs. 112 113 Returns: 114 Dictionary, service id if success, error if error. 115 """ 116 test_cmd = COMMAND_GET_IFACE_ID_LIST 117 test_id = self.build_id(self.test_counter) 118 self.test_counter += 1 119 120 return self.send_command(test_id, test_cmd, {}) 121 122 def wlanPhyIdList(self): 123 """ Get a list if wlan phy IDs. 124 125 Returns: 126 List of IDs if success, error if error. 127 """ 128 test_cmd = COMMAND_GET_PHY_ID_LIST 129 test_id = self.build_id(self.test_counter) 130 self.test_counter += 1 131 132 return self.send_command(test_id, test_cmd, {}) 133 134 def wlanStatus(self, iface_id=None): 135 """ Request connection status 136 137 Args: 138 iface_id: unsigned 16-bit int, the wlan interface id 139 (defaults to None) 140 141 Returns: 142 Client state summary containing WlanClientState and 143 status of various networks connections 144 """ 145 test_cmd = COMMAND_STATUS 146 test_args = {} 147 if iface_id: 148 test_args = {'iface_id': iface_id} 149 test_id = self.build_id(self.test_counter) 150 self.test_counter += 1 151 152 return self.send_command(test_id, test_cmd, test_args) 153 154 def wlanGetCountry(self, phy_id): 155 """ Reads the currently configured country for `phy_id`. 156 157 Args: 158 phy_id: unsigned 16-bit integer. 159 160 Returns: 161 Dictionary, String if success, error if error. 162 """ 163 test_cmd = COMMAND_GET_COUNTRY 164 test_args = {"phy_id": phy_id} 165 test_id = self.build_id(self.test_counter) 166 self.test_counter += 1 167 168 return self.send_command(test_id, test_cmd, test_args) 169 170 def wlanGetDevPath(self, phy_id): 171 """ Queries the device path for `phy_id`. 172 173 Args: 174 phy_id: unsigned 16-bit integer. 175 176 Returns: 177 Dictionary, String if success, error if error. 178 """ 179 test_cmd = COMMAND_GET_DEV_PATH 180 test_args = {"phy_id": phy_id} 181 test_id = self.build_id(self.test_counter) 182 self.test_counter += 1 183 184 return self.send_command(test_id, test_cmd, test_args) 185 186 def wlanQueryInterface(self, iface_id): 187 """ Retrieves interface info for given wlan iface id. 188 189 Args: 190 iface_id: unsigned 16-bit int, the wlan interface id. 191 192 Returns: 193 Dictionary, containing interface id, role, phy_id, phy_assigned_id 194 and mac addr. 195 """ 196 test_cmd = COMMAND_QUERY_IFACE 197 test_args = {'iface_id': iface_id} 198 test_id = self.build_id(self.test_counter) 199 self.test_counter += 1 200 201 return self.send_command(test_id, test_cmd, test_args) 202