1 /* 2 * Copyright (C) 2011 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.wireless.tests; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.util.IRunUtil; 22 import com.android.tradefed.util.RunUtil; 23 24 /** 25 * Helper class to get device radio settings 26 */ 27 public class RadioHelper { 28 private static final String[] PING_SERVER_LIST = { 29 "www.google.com", "www.facebook.com", "www.bing.com", "www.ask.com", "www.yahoo.com" 30 }; 31 private static final int RETRY_ATTEMPTS = 3; 32 private static final int ACTIVATION_WAITING_TIME = 5 * 60 * 1000; // 5 minutes; 33 private static final String WIFI_ONLY = "wifi-only"; 34 /* Maximum time to wait for device to connect to data network */ 35 public static final int MAX_DATA_SETUP_TIME = 3 * 60 * 1000; // 3 minutes 36 private ITestDevice mDevice; 37 38 RadioHelper(ITestDevice device) { 39 mDevice = device; 40 } 41 42 /** 43 * Gets the {@link IRunUtil} instance to use. 44 */ 45 IRunUtil getRunUtil() { 46 return RunUtil.getDefault(); 47 } 48 49 /** 50 * Get phone type 0 - None, 1 - GSM, 2 - CDMA 51 */ 52 private String getPhoneType() throws DeviceNotAvailableException { 53 return mDevice.getProperty("gsm.current.phone-type"); 54 } 55 56 /** 57 * Get sim state 58 */ 59 private String getSimState() throws DeviceNotAvailableException { 60 return mDevice.getProperty("gsm.sim.state"); 61 } 62 63 /** 64 * Verify whether a device is a CDMA only device 65 * @return true for CDMA only device, false for GSM or LTE device 66 * @throws DeviceNotAvailableException 67 */ 68 public boolean isCdmaDevice() throws DeviceNotAvailableException { 69 // Wait 30 seconds for SIM to load 70 getRunUtil().sleep(30*1000); 71 String phoneType = null; 72 String simState = null; 73 for (int i = 0; i < RETRY_ATTEMPTS && (phoneType == null || simState == null); i++) { 74 phoneType = getPhoneType(); 75 simState = getSimState(); 76 CLog.d("phonetype: %s", phoneType); 77 CLog.d("gsm.sim.state: %s", simState); 78 RunUtil.getDefault().sleep(5 * 1000); 79 } 80 81 if (phoneType == null || simState == null) { 82 CLog.d("Error: phoneType or simState is null."); 83 return false; 84 } 85 86 if ((phoneType.compareToIgnoreCase("2") == 0) && 87 (simState.compareToIgnoreCase("UNKNOWN") == 0)) { 88 // GSM device as phoneType "1" 89 // LTE device should have SIM state set to "READY" 90 CLog.d("it is a CDMA device, return true"); 91 return true; 92 } 93 return false; 94 } 95 96 /** 97 * Verify whether a device is a Wi-Fi only device (e.g. Wingray) 98 */ 99 public boolean isWifiOnlyDevice() throws DeviceNotAvailableException { 100 return mDevice.getProperty("ro.carrier").contains(WIFI_ONLY); 101 } 102 103 public void resetBootComplete() throws DeviceNotAvailableException { 104 mDevice.executeShellCommand("setprop dev.bootcomplete 0"); 105 } 106 107 public boolean pingTest() throws DeviceNotAvailableException { 108 String failString = "ping: unknown host"; 109 // assume the chance that all servers are down is very small 110 for (int i = 0; i < PING_SERVER_LIST.length; i++ ) { 111 String host = PING_SERVER_LIST[i]; 112 CLog.d("Start ping test, ping %s", host); 113 String res = mDevice.executeShellCommand("ping -c 10 -w 100 " + host); 114 if (!res.contains(failString)) { 115 return true; 116 } 117 } 118 return false; 119 } 120 121 /** 122 * Activate a device if it is needed. 123 * @return true if the activation is successful. 124 * @throws DeviceNotAvailableException 125 */ 126 public boolean radioActivation() throws DeviceNotAvailableException { 127 if (isWifiOnlyDevice()) { 128 return true; 129 } 130 if (!isCdmaDevice()) { 131 // for GSM device and LTE device 132 CLog.d("not a CDMA device, no need to activiate the device"); 133 return true; 134 } else if (pingTest()) { 135 // for CDMA device which has been activiated (e.g. no radio updates) 136 CLog.d("CDMA device has been activated."); 137 return true; 138 } 139 140 // Activate a CDMA device which doesn't have data connection yet 141 for (int i = 0; i < RETRY_ATTEMPTS; i++ ) { 142 mDevice.executeShellCommand("radiooptions 8 *22899"); 143 long startTime = System.currentTimeMillis(); 144 while ((System.currentTimeMillis() - startTime) < ACTIVATION_WAITING_TIME) { 145 getRunUtil().sleep(30 * 1000); 146 if (pingTest()) { 147 return true; 148 } 149 } 150 } 151 return false; 152 } 153 154 /** 155 * Wait for device data setup 156 * 157 * @return true if data setup succeeded, false otherwise 158 */ 159 public boolean waitForDataSetup() throws DeviceNotAvailableException { 160 long startTime = System.currentTimeMillis(); 161 while ((System.currentTimeMillis() - startTime) < MAX_DATA_SETUP_TIME) { 162 getRunUtil().sleep(30 * 1000); 163 if (pingTest()) { 164 return true; 165 } 166 } 167 return false; 168 } 169 } 170