1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.compatibility.common.tradefed.targetprep; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.config.OptionClass; 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.log.LogUtil.CLog; 24 import com.android.tradefed.targetprep.BuildError; 25 import com.android.tradefed.targetprep.TargetSetupError; 26 27 /** 28 * This preparer ensures that the device is connected to a network. The options "wifi-ssid" and 29 * "wifi-psk" allow users of the preparer to attempt connection to a network. If the options are 30 * provided, the preparer disconnects any existing network connection, and attempts to connect with 31 * the options provided. 32 * 33 * @throw TargetSetupError if device is not connected to a network and no options are given, or if 34 * the device fails to connect to the network specified in the options 35 */ 36 @OptionClass(alias = "wifi-check") 37 public class WifiCheck extends PreconditionPreparer { 38 39 private static final String WIFI_FEATURE = "android.hardware.wifi"; 40 41 @Option(name = "wifi-ssid", description = "Name of the WiFi network with which to connect") 42 protected String mWifiSsid = null; 43 44 @Option(name = "wifi-psk", 45 description = "The WPA-PSK associated with the wifi-ssid option") 46 protected String mWifiPsk = null; 47 hasWifiFeature(ITestDevice device)48 private boolean hasWifiFeature(ITestDevice device) throws DeviceNotAvailableException { 49 String pmFeatures = device.executeShellCommand("pm list features"); 50 return pmFeatures.contains(WIFI_FEATURE); 51 } 52 53 /** 54 * {@inheritDoc} 55 */ 56 @Override run(ITestDevice device, IBuildInfo buildInfo)57 public void run(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError, 58 BuildError, DeviceNotAvailableException { 59 60 if(!hasWifiFeature(device)) { 61 return; // skip this precondition check if device doesn't support wifi 62 } 63 64 if (mWifiSsid == null) { // no connection to create, check for existing connectivity 65 if (!device.checkConnectivity()) { 66 CLog.e( 67 "Device has no network connection, no ssid provided, some modules " 68 + "of CTS require an active network connection"); 69 return; 70 } 71 } else { // network provided in options, attempt to create new connection 72 CLog.i("Attempting connection to \"%s\"", mWifiSsid); 73 if (!device.connectToWifiNetwork(mWifiSsid, mWifiPsk)) { // attempt connection 74 CLog.e( 75 "Unable to connect to network \"%s\", some modules of CTS" 76 + "require an active network connection", 77 mWifiSsid); 78 return; 79 } 80 } 81 CLog.i("Wifi is connected"); 82 } 83 } 84