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