Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2012 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.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 
     25 /**
     26  * A {@link ITargetPreparer} that configures wifi on the device if necessary.
     27  *
     28  * <p>Unlike {@link DeviceSetup}, this preparer works when adb is not root aka user builds.
     29  */
     30 @OptionClass(alias = "wifi")
     31 public class WifiPreparer extends BaseTargetPreparer implements ITargetCleaner {
     32 
     33     @Option(name="wifi-network", description="the name of wifi network to connect to.")
     34     private String mWifiNetwork = null;
     35 
     36     @Option(name="wifi-psk", description="WPA-PSK passphrase of wifi network to connect to.")
     37     private String mWifiPsk = null;
     38 
     39     @Option(name = "disconnect-wifi-after-test", description =
     40             "disconnect from wifi network after test completes.")
     41     private boolean mDisconnectWifiAfterTest = true;
     42 
     43     @Option(name = "monitor-network", description =
     44             "monitor network connectivity during test.")
     45     private boolean mMonitorNetwork = true;
     46 
     47     @Option(name = "skip", description = "skip the connectivity check and wifi setup")
     48     private boolean mSkip = false;
     49 
     50     @Option(name = "verify-only", description = "Skip setup and verify a wifi connection.")
     51     private boolean mVerifyOnly = false;
     52 
     53     /**
     54      * {@inheritDoc}
     55      */
     56     @Override
     57     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     58             BuildError, DeviceNotAvailableException {
     59         if (mSkip) {
     60             return;
     61         }
     62         if (mVerifyOnly) {
     63             if (!device.isWifiEnabled()) {
     64                 throw new TargetSetupError(
     65                         "The device does not have wifi enabled.", device.getDeviceDescriptor());
     66             } else if (!device.checkConnectivity()) {
     67                 throw new TargetSetupError(
     68                         "The device has no wifi connection.", device.getDeviceDescriptor());
     69             }
     70             return;
     71         }
     72 
     73         if (mWifiNetwork == null) {
     74             throw new TargetSetupError("wifi-network not specified", device.getDeviceDescriptor());
     75         }
     76 
     77         if (!device.connectToWifiNetworkIfNeeded(mWifiNetwork, mWifiPsk)) {
     78             throw new TargetSetupError(String.format("Failed to connect to wifi network %s on %s",
     79                     mWifiNetwork, device.getSerialNumber()), device.getDeviceDescriptor());
     80         }
     81 
     82         if (mMonitorNetwork) {
     83             device.enableNetworkMonitor();
     84         }
     85     }
     86 
     87     /**
     88      * {@inheritDoc}
     89      */
     90     @Override
     91     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
     92             throws DeviceNotAvailableException {
     93         if (mSkip || mVerifyOnly) {
     94             return;
     95         }
     96 
     97         if (e instanceof DeviceFailedToBootError) {
     98             CLog.d("boot failure: skipping wifi teardown");
     99             return;
    100         }
    101 
    102         if (mMonitorNetwork) {
    103             device.disableNetworkMonitor();
    104         }
    105 
    106         if (mWifiNetwork != null && mDisconnectWifiAfterTest && device.isWifiEnabled()) {
    107             if (!device.disconnectFromWifi()) {
    108                 CLog.w("Failed to disconnect from wifi network on %s", device.getSerialNumber());
    109                 return;
    110             }
    111             CLog.i("Successfully disconnected from wifi network on %s", device.getSerialNumber());
    112         }
    113     }
    114 }
    115