OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import copy | 5 import copy |
6 import dbus | 6 import dbus |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import time | 9 import time |
10 | 10 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 config = pyauto.PyUITest.EvalDataFrom(self._ROUTER_CONFIG_FILE) | 244 config = pyauto.PyUITest.EvalDataFrom(self._ROUTER_CONFIG_FILE) |
245 strip_ip, routers = config['strip_ip'], config['routers'] | 245 strip_ip, routers = config['strip_ip'], config['routers'] |
246 | 246 |
247 self._SetupRouteForPowerStrip(strip_ip) | 247 self._SetupRouteForPowerStrip(strip_ip) |
248 self._wifi_power_strip = WifiPowerStrip(strip_ip, routers) | 248 self._wifi_power_strip = WifiPowerStrip(strip_ip, routers) |
249 | 249 |
250 self.RouterPower = self._wifi_power_strip.RouterPower | 250 self.RouterPower = self._wifi_power_strip.RouterPower |
251 self.TurnOffAllRouters = self._wifi_power_strip.TurnOffAllRouters | 251 self.TurnOffAllRouters = self._wifi_power_strip.TurnOffAllRouters |
252 self.GetRouterConfig = self._wifi_power_strip.GetRouterConfig | 252 self.GetRouterConfig = self._wifi_power_strip.GetRouterConfig |
253 | 253 |
254 def WaitUntilWifiNetworkAvailable(self, ssid, timeout=60, is_hidden=False): | |
255 """Waits until the given network is available. | |
256 | |
257 Routers that are just turned on may take up to 1 minute upon turning them | |
258 on to broadcast their SSID. | |
259 | |
260 Args: | |
261 ssid: SSID of the service we want to connect to. | |
262 timeout: timeout (in seconds) | |
263 | |
264 Raises: | |
265 Exception if timeout duration has been hit before wifi router is seen. | |
266 | |
267 Returns: | |
268 True, when the wifi network is seen within the timout period. | |
269 False, otherwise. | |
270 """ | |
271 def _GotWifiNetwork(): | |
272 # Returns non-empty array if desired SSID is available. | |
273 try: | |
274 return [wifi for wifi in | |
275 self.NetworkScan().get('wifi_networks', {}).values() | |
276 if wifi.get('name') == ssid] | |
277 except pyauto_errors.JSONInterfaceError: | |
278 # Temporary fix until crosbug.com/14174 is fixed. | |
279 # NetworkScan is only used in updating the list of networks so errors | |
280 # thrown by it are not critical to the results of wifi tests that use | |
281 # this method. | |
282 return False | |
283 | |
284 # The hidden AP's will always be on, thus we will assume it is ready to | |
285 # connect to. | |
286 if is_hidden: | |
287 return bool(_GotWifiNetwork()) | |
288 | |
289 return self.WaitUntil(_GotWifiNetwork, timeout=timeout, retry_sleep=1) | |
290 | |
291 def GetConnectedWifi(self): | |
292 """Returns the SSID of the currently connected wifi network. | |
293 | |
294 Returns: | |
295 The SSID of the connected network or None if we're not connected. | |
296 """ | |
297 service_list = self.GetNetworkInfo() | |
298 connected_service_path = service_list.get('connected_wifi') | |
299 if 'wifi_networks' in service_list and \ | |
300 connected_service_path in service_list['wifi_networks']: | |
301 return service_list['wifi_networks'][connected_service_path]['name'] | |
302 | |
303 def GetServicePath(self, ssid): | |
304 """Returns the service path associated with an SSID. | |
305 | |
306 Args: | |
307 ssid: String defining the SSID we are searching for. | |
308 | |
309 Returns: | |
310 The service path or None if SSID does not exist. | |
311 """ | |
312 service_list = self.GetNetworkInfo() | |
313 service_list = service_list.get('wifi_networks', []) | |
314 for service_path, service_obj in service_list.iteritems(): | |
315 if service_obj['name'] == ssid: | |
316 return service_path | |
317 return None | |
318 | |
319 def ConnectToWifiRouter(self, router_name, shared=True): | 254 def ConnectToWifiRouter(self, router_name, shared=True): |
320 """Connects to a router by name. | 255 """Connects to a router by name. |
321 | 256 |
322 Args: | 257 Args: |
323 router_name: The name of the router that is specified in the | 258 router_name: The name of the router that is specified in the |
324 configuration file. | 259 configuration file. |
325 """ | 260 """ |
326 router = self._wifi_power_strip.GetRouterConfig(router_name) | 261 router = self._wifi_power_strip.GetRouterConfig(router_name) |
327 assert router, 'Router with name %s is not defined ' \ | 262 assert router, 'Router with name %s is not defined ' \ |
328 'in the router configuration.' % router_name | 263 'in the router configuration.' % router_name |
329 security = router.get('security', 'SECURITY_NONE') | 264 security = router.get('security', 'SECURITY_NONE') |
330 passphrase = router.get('passphrase', '') | 265 passphrase = router.get('passphrase', '') |
331 | 266 |
332 # Branch off the connect calls depending on if the wifi network is hidden | 267 # Branch off the connect calls depending on if the wifi network is hidden |
333 # or not. | 268 # or not. |
334 error_string = None | 269 error_string = None |
335 if router.get('hidden'): | 270 if router.get('hidden'): |
336 error_string = self.ConnectToHiddenWifiNetwork(router['ssid'], security, | 271 error_string = self.ConnectToHiddenWifiNetwork(router['ssid'], security, |
337 passphrase) | 272 passphrase) |
338 else: | 273 else: |
339 service_path = self.GetServicePath(router['ssid']) | 274 service_path = self.GetServicePath(router['ssid']) |
340 assert service_path, 'Service with SSID %s is not present.' % \ | 275 assert service_path, 'Service with SSID %s is not present.' % \ |
341 router['ssid'] | 276 router['ssid'] |
342 | 277 |
343 logging.debug('Connecting to router %s.' % router_name) | 278 logging.debug('Connecting to router %s.' % router_name) |
344 error_string = self.ConnectToWifiNetwork(service_path, | 279 error_string = self.ConnectToWifiNetwork(service_path, |
345 password=passphrase, | 280 password=passphrase, |
346 shared=shared) | 281 shared=shared) |
347 return error_string | 282 return error_string |
OLD | NEW |