Tethering.java revision 367d15ab1a33b6159447fa8542d4fa8ff148371c
1d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/* 2d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Copyright (C) 2010 The Android Open Source Project 3d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 4d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License"); 5d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * you may not use this file except in compliance with the License. 6d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * You may obtain a copy of the License at 7d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 8d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * http://www.apache.org/licenses/LICENSE-2.0 9d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 10d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Unless required by applicable law or agreed to in writing, software 11d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS, 12d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * See the License for the specific language governing permissions and 14d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * limitations under the License. 15d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */ 16d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 17d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpackage com.android.server.connectivity; 18d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 19d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.Notification; 20d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.NotificationManager; 21d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.PendingIntent; 22d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.BroadcastReceiver; 23d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Context; 24d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Intent; 25d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.IntentFilter; 262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.content.pm.PackageManager; 27d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.res.Resources; 28c4308f01c965571dc2354107c3574df113e397eeMike Lockwoodimport android.hardware.usb.UsbManager; 29d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.ConnectivityManager; 302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.IConnectivityManager; 31d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.INetworkManagementEventObserver; 32367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkeyimport android.net.INetworkStatsService; 33cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkeyimport android.net.InterfaceConfiguration; 34ed1264093234b1f1354cd5f669eb82a50bb56869Robert Greenwaltimport android.net.LinkAddress; 3505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwaltimport android.net.LinkProperties; 362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.NetworkInfo; 37e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwaltimport android.net.NetworkUtils; 382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Binder; 397eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwaltimport android.os.HandlerThread; 40d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.IBinder; 41d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.INetworkManagementService; 42dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwaltimport android.os.Looper; 432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Message; 44d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.RemoteException; 45d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.ServiceManager; 46d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings; 47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log; 48d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone; 5064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.IState; 5164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.State; 5264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.StateMachine; 53cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkeyimport com.google.android.collect.Lists; 542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor; 562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter; 5704808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress; 58d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList; 59ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Collection; 602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap; 61ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Iterator; 622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set; 63cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey 64d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/** 65d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide 662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * 672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout 682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * 692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing 70d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */ 71d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub { 72d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt private Context mContext; 74924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt private final static String TAG = "Tethering"; 75c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville private final static boolean DBG = true; 76c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville private final static boolean VDBG = false; 77d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // TODO - remove both of these - should be part of interface inspection/selection stuff 792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mTetherableUsbRegexs; 802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mTetherableWifiRegexs; 816fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang private String[] mTetherableBluetoothRegexs; 82ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private Collection<Integer> mUpstreamIfaceTypes; 83ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 84ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE); 85ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI); 86ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN); 87ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 88ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // if we have to connect to mobile, what APN type should we use? Calculated by examining the 89ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // upstream type list and the DUN_REQUIRED secure-setting 90ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; 912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 92367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey private final INetworkManagementService mNMService; 93367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey private final INetworkStatsService mStatsService; 947eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt private Looper mLooper; 957eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt private HandlerThread mThread; 96dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt 97030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces 98d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 99d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt private BroadcastReceiver mStateReceiver; 100d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 101bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt private static final String USB_NEAR_IFACE_ADDR = "192.168.42.129"; 102b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt private static final int USB_PREFIX_LENGTH = 24; 103bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt 104707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // USB is 192.168.42.1 and 255.255.255.0 105707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // Wifi is 192.168.43.1 and 255.255.255.0 106707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1 107707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // with 255.255.255.0 108d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt 1092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mDhcpRange; 110c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh private static final String[] DHCP_DEFAULT_RANGE = { 111c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254", 112c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254", 113c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254", 114c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.48.2", "192.168.48.254", 115c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh }; 1162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mDnsServers; 118d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8"; 119df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4"; 1202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 12164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private StateMachine mTetherMasterSM; 122a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 123a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private Notification mTetheredNotification; 1242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1256c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private boolean mRndisEnabled; // track the RNDIS function enabled state 1266c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private boolean mUsbTetherRequested; // true if USB tethering should be started 1276c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // when RNDIS is enabled 128030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt 129367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey public Tethering(Context context, INetworkManagementService nmService, 130367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey INetworkStatsService statsService, Looper looper) { 131d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt mContext = context; 132c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService = nmService; 133367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey mStatsService = statsService; 134dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt mLooper = looper; 135d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces = new HashMap<String, TetherInterfaceSM>(); 137d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1387eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt // make our own thread so we don't anr the system 1397eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mThread = new HandlerThread("Tethering"); 1407eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mThread.start(); 1417eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mLooper = mThread.getLooper(); 142dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper); 1432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherMasterSM.start(); 1442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 145bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mStateReceiver = new StateReceiver(); 146d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt IntentFilter filter = new IntentFilter(); 147770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood filter.addAction(UsbManager.ACTION_USB_STATE); 1482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 149bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mContext.registerReceiver(mStateReceiver, filter); 150bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt 151bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt filter = new IntentFilter(); 152030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt filter.addAction(Intent.ACTION_MEDIA_SHARED); 153030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt filter.addAction(Intent.ACTION_MEDIA_UNSHARED); 154bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt filter.addDataScheme("file"); 155bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mContext.registerReceiver(mStateReceiver, filter); 156bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt 1572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mDhcpRange = context.getResources().getStringArray( 1582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_dhcp_range); 159bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) { 160c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mDhcpRange = DHCP_DEFAULT_RANGE; 1612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherableUsbRegexs = context.getResources().getStringArray( 1642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_usb_regexs); 1652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherableWifiRegexs = context.getResources().getStringArray( 1662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_wifi_regexs); 1676fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang mTetherableBluetoothRegexs = context.getResources().getStringArray( 1686fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang com.android.internal.R.array.config_tether_bluetooth_regexs); 169ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int ifaceTypes[] = context.getResources().getIntArray( 170ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt com.android.internal.R.array.config_tether_upstream_types); 171ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes = new ArrayList(); 172ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (int i : ifaceTypes) { 173ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(new Integer(i)); 174ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 175ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 176ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // check if the upstream type list needs to be modified due to secure-settings 177ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt checkDunRequired(); 1782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // TODO - remove and rely on real notifications of the current iface 1802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mDnsServers = new String[2]; 181d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt mDnsServers[0] = DNS_DEFAULT_SERVER1; 182d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt mDnsServers[1] = DNS_DEFAULT_SERVER2; 183d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 184d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1856143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen public void interfaceStatusChanged(String iface, boolean up) { 186c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); 1872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean found = false; 18865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean usb = false; 189a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isWifi(iface)) { 190a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 191a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (isUsb(iface)) { 192a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 193a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usb = true; 1946fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (isBluetooth(iface)) { 1956fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang found = true; 1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (found == false) return; 1982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 2016143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen if (up) { 2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 203dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt sm = new TetherInterfaceSM(iface, mLooper, usb); 2042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.put(iface, sm); 2052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm.start(); 2062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else { 208485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood if (isUsb(iface)) { 209485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood // ignore usb0 down after enabling RNDIS 210485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood // we will handle disconnect in interfaceRemoved instead 211c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "ignoring interface down for " + iface); 212485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood } else if (sm != null) { 2136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN); 2142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.remove(iface); 2152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 218d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 219d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 220f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen public void interfaceLinkStateChanged(String iface, boolean up) { 221c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "interfaceLinkStateChanged " + iface + ", " + up); 22223eb297c771bd996fe8559178feb1a07be43ad72Irfan Sheriff interfaceStatusChanged(iface, up); 223f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen } 224f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen 225a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private boolean isUsb(String iface) { 226a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt for (String regex : mTetherableUsbRegexs) { 227a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (iface.matches(regex)) return true; 228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return false; 230a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 231a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 232a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt public boolean isWifi(String iface) { 233a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt for (String regex : mTetherableWifiRegexs) { 234a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (iface.matches(regex)) return true; 235a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 236a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return false; 237a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 238a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 2396fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang public boolean isBluetooth(String iface) { 2406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang for (String regex : mTetherableBluetoothRegexs) { 2416fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (iface.matches(regex)) return true; 2426fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2436fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang return false; 2446fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2456c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 2462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void interfaceAdded(String iface) { 247c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "interfaceAdded " + iface); 2482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean found = false; 24965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean usb = false; 250a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isWifi(iface)) { 251a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 2522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 253a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb(iface)) { 254a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 255a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usb = true; 256d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 2576fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (isBluetooth(iface)) { 2586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang found = true; 2596fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (found == false) { 261c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring"); 262d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt return; 263d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 26465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 2652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 2672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm != null) { 268c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring"); 2692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 2702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 271dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt sm = new TetherInterfaceSM(iface, mLooper, usb); 2722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.put(iface, sm); 2732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm.start(); 2742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 275c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "interfaceAdded :" + iface); 276d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 2782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void interfaceRemoved(String iface) { 279c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); 2802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 2822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 283c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) { 284924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring"); 285924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN); 2892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.remove(iface); 290d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 291d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 292d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 29312b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall public void limitReached(String limitName, String iface) {} 29412b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall 2955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int tether(String iface) { 296c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (DBG) Log.d(TAG, "Tethering " + iface); 2972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = null; 2982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm = mIfaces.get(iface); 300d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring"); 3035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (!sm.isAvailable() && !sm.isErrored()) { 3062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring"); 3075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; 308d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED); 3105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_NO_ERROR; 311d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 312d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int untether(String iface) { 314c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (DBG) Log.d(TAG, "Untethering " + iface); 3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = null; 3162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm = mIfaces.get(iface); 3182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 3192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 3202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); 3215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 322d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isErrored()) { 3242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring"); 3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; 326d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED); 3285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_NO_ERROR; 3295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 3315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int getLastTetherError(String iface) { 3325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt TetherInterfaceSM sm = null; 3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt synchronized (mIfaces) { 3345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt sm = mIfaces.get(iface); 3355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (sm == null) { 3375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring"); 3385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 3395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return sm.getLastError(); 3412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 342d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private void sendTetherStateChangedBroadcast() { 3442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 345c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 346d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt try { 347c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh if (!cm.isTetheringSupported()) return; 3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (RemoteException e) { 3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 350d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 351d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> availableList = new ArrayList<String>(); 3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> activeList = new ArrayList<String>(); 3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> erroredList = new ArrayList<String>(); 3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 356a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt boolean wifiTethered = false; 357a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt boolean usbTethered = false; 3586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang boolean bluetoothTethered = false; 359a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 3602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 3612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set ifaces = mIfaces.keySet(); 3622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object iface : ifaces) { 3632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 3642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm != null) { 3656fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (sm.isErrored()) { 3662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt erroredList.add((String)iface); 3672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (sm.isAvailable()) { 3682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt availableList.add((String)iface); 3692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (sm.isTethered()) { 370a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb((String)iface)) { 371a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usbTethered = true; 372a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (isWifi((String)iface)) { 373a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt wifiTethered = true; 3746fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (isBluetooth((String)iface)) { 3756fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang bluetoothTethered = true; 376a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 3772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt activeList.add((String)iface); 3782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 380d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 381d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 382d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); 38398c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | 38498c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 3852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, 3862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt availableList); 3872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList); 3882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER, 3892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt erroredList); 3902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mContext.sendStickyBroadcast(broadcast); 391c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) { 392924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " + 393924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt activeList.size() + ", " + erroredList.size()); 394924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 395a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 396a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (usbTethered) { 3976fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (wifiTethered || bluetoothTethered) { 398a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general); 399a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else { 400a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb); 401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (wifiTethered) { 4036fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (bluetoothTethered) { 4046fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general); 4056fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else { 4066fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi); 4076fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 4086fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (bluetoothTethered) { 4096fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth); 410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else { 411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt clearTetheredNotification(); 412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 414a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 415a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private void showTetheredNotification(int icon) { 416a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt NotificationManager notificationManager = 417a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); 418a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (notificationManager == null) { 419a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return; 420a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 421a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 422db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt if (mTetheredNotification != null) { 423db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt if (mTetheredNotification.icon == icon) { 424db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt return; 425db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt } 426db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt notificationManager.cancel(mTetheredNotification.icon); 427db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt } 428db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt 429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt Intent intent = new Intent(); 430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); 431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); 434a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 435a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt Resources r = Resources.getSystem(); 436a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title); 437a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt CharSequence message = r.getText(com.android.internal.R.string. 438a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt tethered_notification_message); 439a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 4406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (mTetheredNotification == null) { 441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification = new Notification(); 442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.when = 0; 443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.icon = icon; 445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND; 446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT; 447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.tickerText = title; 448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.setLatestEventInfo(mContext, title, message, pi); 449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt notificationManager.notify(mTetheredNotification.icon, mTetheredNotification); 451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 453a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private void clearTetheredNotification() { 454a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt NotificationManager notificationManager = 455a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); 456a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (notificationManager != null && mTetheredNotification != null) { 457a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt notificationManager.cancel(mTetheredNotification.icon); 458a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification = null; 459a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 4605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 4615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 4622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private class StateReceiver extends BroadcastReceiver { 463d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt public void onReceive(Context content, Intent intent) { 4642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String action = intent.getAction(); 465770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood if (action.equals(UsbManager.ACTION_USB_STATE)) { 4666c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood synchronized (Tethering.this) { 4676c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 4686c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false); 4696c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // start tethering if we have a request pending 4706c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (usbConnected && mRndisEnabled && mUsbTetherRequested) { 4716c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(true); 4726c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 4736c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = false; 4746c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 4752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 476c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION"); 4776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); 478d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 479d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 480d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 481d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 4826c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private void tetherUsb(boolean enable) { 483c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "tetherUsb " + enable); 4843c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood 48565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt String[] ifaces = new String[0]; 48665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt try { 487c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh ifaces = mNMService.listInterfaces(); 48865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } catch (Exception e) { 4893c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood Log.e(TAG, "Error listing Interfaces", e); 49065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return; 49165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 49265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt for (String iface : ifaces) { 493a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb(iface)) { 4946c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood int result = (enable ? tether(iface) : untether(iface)); 4956c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { 4966c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return; 49765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 49865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 49965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5006c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "unable start or stop USB tethering"); 50165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 50265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 50365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt // configured when we start tethering and unconfig'd on error or conclusion 504d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt private boolean configureUsbIface(boolean enabled) { 505c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "configureUsbIface(" + enabled + ")"); 50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 5076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // toggle the USB interfaces 5086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood String[] ifaces = new String[0]; 5093c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood try { 5106c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifaces = mNMService.listInterfaces(); 5116c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } catch (Exception e) { 5126c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "Error listing Interfaces", e); 5136c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return false; 5146c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5156c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood for (String iface : ifaces) { 5166c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (isUsb(iface)) { 5176c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood InterfaceConfiguration ifcg = null; 5186c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood try { 5196c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg = mNMService.getInterfaceConfig(iface); 5206c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (ifcg != null) { 5216c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR); 5226c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH); 5236c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (enabled) { 5246c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up"); 5256c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5266c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down"); 52765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5286c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", ""); 5296c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace(" "," "); 5306c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mNMService.setInterfaceConfig(iface, ifcg); 53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5326c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } catch (Exception e) { 5336c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "Error configuring interface " + iface, e); 5346c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return false; 53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5376c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return true; 54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 5422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableUsbRegexs() { 5432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTetherableUsbRegexs; 5442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 5452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 5462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableWifiRegexs() { 5472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTetherableWifiRegexs; 5482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 5492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 5506fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang public String[] getTetherableBluetoothRegexs() { 5516fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang return mTetherableBluetoothRegexs; 5526fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 5536fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang 5546c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood public int setUsbTethering(boolean enable) { 555c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); 5566c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE); 5576c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 5586c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood synchronized (this) { 5596c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (enable) { 5606c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (mRndisEnabled) { 5616c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(true); 5626c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5636c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = true; 5646c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false); 5656c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5666c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5676c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(false); 5686c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (mRndisEnabled) { 5696c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood usbManager.setCurrentFunction(null, false); 5706c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5716c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = false; 5726c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5736c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5746c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return ConnectivityManager.TETHER_ERROR_NO_ERROR; 5756c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5766c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 577ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt public int[] getUpstreamIfaceTypes() { 578ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int values[] = new int[mUpstreamIfaceTypes.size()]; 579ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator(); 580ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (int i=0; i < mUpstreamIfaceTypes.size(); i++) { 581ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt values[i] = iterator.next(); 582ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 583ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return values; 584c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 585c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt 586ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt public void checkDunRequired() { 587ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int requiredApn = ((Settings.Secure.getInt(mContext.getContentResolver(), 588ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Settings.Secure.TETHER_DUN_REQUIRED, 0) == 1) ? 589ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt ConnectivityManager.TYPE_MOBILE_DUN : 590ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt ConnectivityManager.TYPE_MOBILE_HIPRI); 591ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mPreferredUpstreamMobileApn != requiredApn) { 592ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) { 593ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) { 594ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(MOBILE_TYPE); 595ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 596ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) { 597ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(HIPRI_TYPE); 598ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 599ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) { 600ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(DUN_TYPE); 601ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 602ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } else { 603ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(DUN_TYPE)) { 604ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(DUN_TYPE); 605ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 606ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) { 607ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(MOBILE_TYPE); 608ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 609ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) { 610ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(HIPRI_TYPE); 611ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 612ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 613ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mPreferredUpstreamMobileApn = requiredApn; 614ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 615c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 616c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt 6172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetheredIfaces() { 6182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 6202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set keys = mIfaces.keySet(); 6212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object key : keys) { 6222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isTethered()) { 6242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt list.add((String)key); 6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String[] retVal = new String[list.size()]; 6292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (int i=0; i < list.size(); i++) { 6302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retVal[i] = list.get(i); 6312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retVal; 6332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 635cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey public String[] getTetheredIfacePairs() { 636cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey final ArrayList<String> list = Lists.newArrayList(); 637cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey synchronized (mIfaces) { 638cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey for (TetherInterfaceSM sm : mIfaces.values()) { 639cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey if (sm.isTethered()) { 640cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey list.add(sm.mMyUpstreamIfaceName); 641cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey list.add(sm.mIfaceName); 642cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey } 643cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey } 644cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey } 645cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey return list.toArray(new String[list.size()]); 646cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey } 647cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey 6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableIfaces() { 6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set keys = mIfaces.keySet(); 6522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object key : keys) { 6532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isAvailable()) { 6552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt list.add((String)key); 6562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String[] retVal = new String[list.size()]; 6602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (int i=0; i < list.size(); i++) { 6612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retVal[i] = list.get(i); 6622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retVal; 6642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 6665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public String[] getErroredIfaces() { 6675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt synchronized (mIfaces) { 6695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Set keys = mIfaces.keySet(); 6705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt for (Object key : keys) { 6715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (sm.isErrored()) { 6735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt list.add((String)key); 6745a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt String[] retVal = new String[list.size()]; 6785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt for (int i= 0; i< list.size(); i++) { 6795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt retVal[i] = list.get(i); 6805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return retVal; 6825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 6841ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo //TODO: Temporary handling upstream change triggered without 6851ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // CONNECTIVITY_ACTION. Only to accomodate interface 6861ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // switch during HO. 6871ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // @see bug/4455071 6881ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo public void handleTetherIfaceChange() { 6891ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); 6901ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo } 6911ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo 69264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherInterfaceSM extends StateMachine { 6932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it's not in tether mode 6946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_MODE_DEAD = 1; 6952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // request from the user that it wants to tether 6966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_REQUESTED = 2; 6972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // request from the user that it wants to untether 6986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_UNREQUESTED = 3; 6992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification that this interface is down 7006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_INTERFACE_DOWN = 4; 7012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification that this interface is up 7026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_INTERFACE_UP = 5; 7032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had an error turning on cellular dun 7046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_CELL_DUN_ERROR = 6; 7052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble enabling IP Forwarding 7066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_IP_FORWARDING_ENABLE_ERROR = 7; 7072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble disabling IP Forwarding 7086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_IP_FORWARDING_DISABLE_ERROR = 8; 7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble staring tethering 7106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_START_TETHERING_ERROR = 9; 7112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble stopping tethering 7126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_STOP_TETHERING_ERROR = 10; 7132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble setting the DNS forwarders 7146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_SET_DNS_FORWARDERS_ERROR = 11; 7156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // the upstream connection has changed 716951749ba2e014566553d17c512e947472951a060Wink Saville static final int CMD_TETHER_CONNECTION_CHANGED = 12; 7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 71864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mDefaultState; 7192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 72064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mInitialState; 72164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStartingState; 72264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mTetheredState; 7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 72464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mUnavailableState; 7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private boolean mAvailable; 7272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private boolean mTethered; 7285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt int mLastError; 7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String mIfaceName; 7316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt String mMyUpstreamIfaceName; // may change over time 7326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 73365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean mUsb; 7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 735dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt TetherInterfaceSM(String name, Looper looper, boolean usb) { 736dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt super(name, looper); 7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaceName = name; 73865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt mUsb = usb; 7395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mInitialState = new InitialState(); 7422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mInitialState); 7432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStartingState = new StartingState(); 7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStartingState); 7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetheredState = new TetheredState(); 7462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mTetheredState); 7472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mUnavailableState = new UnavailableState(); 7482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mUnavailableState); 7492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setInitialState(mInitialState); 7512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String toString() { 7542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String res = new String(); 7552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt res += mIfaceName + " - "; 75664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville IState current = getCurrentState(); 7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mInitialState) res += "InitialState"; 7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mStartingState) res += "StartingState"; 7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mTetheredState) res += "TetheredState"; 7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mUnavailableState) res += "UnavailableState"; 7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mAvailable) res += " - Available"; 7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mTethered) res += " - Tethered"; 7635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt res += " - lastError =" + mLastError; 7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return res; 7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public synchronized int getLastError() { 7685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return mLastError; 7695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt private synchronized void setLastError(int error) { 7725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt mLastError = error; 7735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7745a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (isErrored()) { 7755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (mUsb) { 7765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt // note everything's been unwound by this point so nothing to do on 7775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt // further error.. 7785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Tethering.this.configureUsbIface(false); 7795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 7842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isAvailable() { 7852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mAvailable; 7862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private synchronized void setAvailable(boolean available) { 7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mAvailable = available; 7902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isTethered() { 7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTethered; 7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private synchronized void setTethered(boolean tethered) { 7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTethered = tethered; 7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isErrored() { 8035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR); 8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 80664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class InitialState extends State { 8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(true); 8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(false); 8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 816c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "InitialState.processMessage what=" + message.what); 8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_REQUESTED: 8205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 8216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED, 8226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 8232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mStartingState); 8242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 8262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 8272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 83664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class StartingState extends State { 8372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 8392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 84065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 841d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(true)) { 8426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 8445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 8455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 846951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mInitialState); 84765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return; 84865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 84965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 8516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 852951749ba2e014566553d17c512e947472951a060Wink Saville // Skipping StartingState 853951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mTetheredState); 8542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 857c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "StartingState.processMessage what=" + message.what); 8582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 8592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 8602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // maybe a parent class? 8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_UNREQUESTED: 8626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 86465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 865d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 8665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 8675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 86865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt break; 86965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 87065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_CELL_DUN_ERROR: 8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_ENABLE_ERROR: 8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_DISABLE_ERROR: 8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_START_TETHERING_ERROR: 8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_STOP_TETHERING_ERROR: 8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_SET_DNS_FORWARDERS_ERROR: 8795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 8805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_MASTER_ERROR); 8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 8836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 89464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetheredState extends State { 8952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 8972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 898c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.tetherInterface(mIfaceName); 8992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 9005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR); 9015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 902951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mInitialState); 9032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 9042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 905c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (DBG) Log.d(TAG, "Tethered " + mIfaceName); 9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 9072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(true); 9082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 9092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 9112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 912c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "TetheredState.processMessage what=" + message.what); 9132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 9142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean error = false; 9152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 9162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_UNREQUESTED: 9172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 9186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 9195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt try { 920367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey // about to tear down NAT; gather remaining statistics 921367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey mStatsService.forceUpdate(); 922367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey 923c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 924b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 9256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 927c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 9306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 9316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 9326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 936c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 9385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 9395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR); 9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 9442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (message.what == CMD_TETHER_UNREQUESTED) { 94565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 946d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 9475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError( 9485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 94965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 95065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 9515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt transitionTo(mInitialState); 9522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (message.what == CMD_INTERFACE_DOWN) { 9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 9542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 955c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (DBG) Log.d(TAG, "Untethered " + mIfaceName); 9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_TETHER_CONNECTION_CHANGED: 9586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt String newUpstreamIfaceName = (String)(message.obj); 9591039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) || 9601039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt (mMyUpstreamIfaceName != null && 9611039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) { 962c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "Connection changed noop - dropping"); 9631039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt break; 9641039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt } 9656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 9666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 967367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey // about to tear down NAT; gather remaining statistics 968367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey mStatsService.forceUpdate(); 969367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey 970c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 971b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 9726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 974c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 9776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 9796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (newUpstreamIfaceName != null) { 9836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 984c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.enableNat(mIfaceName, newUpstreamIfaceName); 9856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 987c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 9906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR); 9916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt transitionTo(mInitialState); 9926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt return true; 9936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mMyUpstreamIfaceName = newUpstreamIfaceName; 9966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_CELL_DUN_ERROR: 9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_ENABLE_ERROR: 9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_DISABLE_ERROR: 10002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_START_TETHERING_ERROR: 10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_STOP_TETHERING_ERROR: 10022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_SET_DNS_FORWARDERS_ERROR: 10032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt error = true; 10042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // fall through 10052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_DEAD: 10066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 10075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt try { 1008367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey // about to tear down NAT; gather remaining statistics 1009367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey mStatsService.forceUpdate(); 1010367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey 1011c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 1012b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 10136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 10146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 1015c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 10166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 10175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 10186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 10196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 10206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 10216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 10222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1024c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 10252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 10265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 10275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR); 10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (error) { 10315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 10325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_MASTER_ERROR); 10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1035c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName); 10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 103765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 1038d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 10395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 104065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 104165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 105264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class UnavailableState extends State { 10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 10565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(false); 10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_UP: 10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 10722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt void setLastErrorAndTransitionToInitialState(int error) { 10765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(error); 10775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt transitionTo(mInitialState); 10782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 107965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 10802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 108264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherMasterSM extends StateMachine { 10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // an interface SM has requested Tethering 10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt static final int CMD_TETHER_MODE_REQUESTED = 1; 10852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // an interface SM has unrequested Tethering 10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt static final int CMD_TETHER_MODE_UNREQUESTED = 2; 10876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // upstream connection change - do the right thing 10886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_UPSTREAM_CHANGED = 3; 10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // we received notice that the cellular DUN connection is up 10906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_CELL_CONNECTION_RENEW = 4; 10916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // we don't have a valid upstream conn, check again after a delay 1092951749ba2e014566553d17c512e947472951a060Wink Saville static final int CMD_RETRY_UPSTREAM = 5; 10932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // This indicates what a timeout event relates to. A state that 10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // sends itself a delayed timeout event and handles incoming timeout events 10962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // should inc this when it is entered and whenever it sends a new timeout event. 10972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // We do not flush the old ones. 10982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private int mSequenceNumber; 10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 110064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mInitialState; 110164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mTetherModeAliveState; 11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 110364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetIpForwardingEnabledErrorState; 110464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetIpForwardingDisabledErrorState; 110564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStartTetheringErrorState; 110664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStopTetheringErrorState; 110764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetDnsForwardersErrorState; 11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private ArrayList mNotifyList; 11102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11111039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt private int mCurrentConnectionSequence; 1112ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private int mMobileApnReserved = ConnectivityManager.TYPE_NONE; 11132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1114b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt private String mUpstreamIfaceName = null; 1115b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt 11166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt private static final int UPSTREAM_SETTLE_TIME_MS = 10000; 11176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt private static final int CELL_CONNECTION_RENEW_MS = 40000; 11182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1119dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt TetherMasterSM(String name, Looper looper) { 1120dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt super(name, looper); 11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt //Add states 11232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mInitialState = new InitialState(); 11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mInitialState); 11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherModeAliveState = new TetherModeAliveState(); 11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mTetherModeAliveState); 11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); 11292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetIpForwardingEnabledErrorState); 11302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); 11312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetIpForwardingDisabledErrorState); 11322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStartTetheringErrorState = new StartTetheringErrorState(); 11332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStartTetheringErrorState); 11342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStopTetheringErrorState = new StopTetheringErrorState(); 11352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStopTetheringErrorState); 11362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); 11372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetDnsForwardersErrorState); 11382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList = new ArrayList(); 11402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setInitialState(mInitialState); 11412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 11422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 114364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherMasterUtilState extends State { 11446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true; 11456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE = false; 11466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 1147d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt @Override 1148d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt public boolean processMessage(Message m) { 1149d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1150d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1151ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected String enableString(int apnType) { 1152ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt switch (apnType) { 1153ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE_DUN: 1154ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return Phone.FEATURE_ENABLE_DUN_ALWAYS; 1155ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE: 1156ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE_HIPRI: 1157ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return Phone.FEATURE_ENABLE_HIPRI; 1158ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 1159ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return null; 1160ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 1161ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected boolean turnOnUpstreamMobileConnection(int apnType) { 11621039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt boolean retValue = true; 1163ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (apnType == ConnectivityManager.TYPE_NONE) return false; 1164ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection(); 1165d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 1166c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 11671039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt int result = Phone.APN_REQUEST_FAILED; 1168ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt String enableString = enableString(apnType); 1169ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (enableString == null) return false; 1170d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1171c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh result = cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, 1172ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt enableString, new Binder()); 1173d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1174d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 11751039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt switch (result) { 11766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_ALREADY_ACTIVE: 11776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_REQUEST_STARTED: 1178ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mMobileApnReserved = apnType; 11791039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW); 11801039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt m.arg1 = ++mCurrentConnectionSequence; 11811039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS); 11826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 11836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_REQUEST_FAILED: 11846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt default: 11851039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt retValue = false; 11866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 11876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 11886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 1189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return retValue; 1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1191ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected boolean turnOffUpstreamMobileConnection() { 1192ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) { 11936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 1194c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 11956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 1196c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, 1197ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt enableString(mMobileApnReserved)); 11986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 11996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt return false; 12006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1201ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mMobileApnReserved = ConnectivityManager.TYPE_NONE; 1202d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1203d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1204d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 12056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected boolean turnOnMasterTetherSettings() { 1206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1207c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(true); 1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1209951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mSetIpForwardingEnabledErrorState); 1210d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1211d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1213c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.startTethering(mDhcpRange); 1214d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1215547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt try { 1216c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 1217c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.startTethering(mDhcpRange); 1218547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt } catch (Exception ee) { 1219547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt transitionTo(mStartTetheringErrorState); 1220547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt return false; 1221547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt } 1222d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1223d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1224c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setDnsForwarders(mDnsServers); 1225d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1226951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mSetDnsForwardersErrorState); 1227d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1229d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1230d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 12316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected boolean turnOffMasterTetherSettings() { 1232d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1233c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 1234d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1235d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mStopTetheringErrorState); 1236d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1237d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1238d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1239c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 1240d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1241d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mSetIpForwardingDisabledErrorState); 1242d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1243d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1244d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mInitialState); 1245d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1246d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1247ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1248ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected void chooseUpstreamType(boolean tryCell) { 124905d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 125005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 1251ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int upType = ConnectivityManager.TYPE_NONE; 1252ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt String iface = null; 125305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt 1254ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (Integer netType : mUpstreamIfaceTypes) { 1255ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt NetworkInfo info = null; 1256ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt try { 1257ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt info = cm.getNetworkInfo(netType.intValue()); 1258ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } catch (RemoteException e) { } 1259ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if ((info != null) && info.isConnected()) { 1260ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt upType = netType.intValue(); 1261ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt break; 1262c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 1263c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 12641039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt 1265c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) { 1266ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn =" 1267ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt + mPreferredUpstreamMobileApn + ", got type=" + upType); 1268924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 1269ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1270ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // if we're on DUN, put our own grab on it 1271ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (upType == ConnectivityManager.TYPE_MOBILE_DUN || 1272ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt upType == ConnectivityManager.TYPE_MOBILE_HIPRI) { 1273ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOnUpstreamMobileConnection(upType); 12746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1275ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1276ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (upType == ConnectivityManager.TYPE_NONE) { 1277ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt boolean tryAgainLater = true; 1278ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) && 1279ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) { 1280ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // we think mobile should be coming up - don't set a retry 1281ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt tryAgainLater = false; 12821039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt } 1283ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (tryAgainLater) { 12841039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); 128577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt } 1286ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } else { 1287ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt LinkProperties linkProperties = null; 1288ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt try { 1289ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt linkProperties = cm.getLinkProperties(upType); 1290ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } catch (RemoteException e) { } 1291ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (linkProperties != null) iface = linkProperties.getInterfaceName(); 12926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1293b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt notifyTetheredOfNewUpstreamIface(iface); 12946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1295ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1296b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt protected void notifyTetheredOfNewUpstreamIface(String ifaceName) { 1297c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "notifying tethered with iface =" + ifaceName); 1298b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mUpstreamIfaceName = ifaceName; 12996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt for (Object o : mNotifyList) { 13006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM sm = (TetherInterfaceSM)o; 13016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED, 13026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ifaceName); 13032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 13076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt class InitialState extends TetherMasterUtilState { 13082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 13102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 1313c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what); 13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 13152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 13162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 1317ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt checkDunRequired(); 13182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 1319c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "Tether Mode requested by " + who.toString()); 13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.add(who); 13216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt transitionTo(mTetherModeAliveState); 13222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_UNREQUESTED: 13242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt who = (TetherInterfaceSM)message.obj; 1325c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who.toString()); 13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int index = mNotifyList.indexOf(who); 13272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (index != -1) { 13286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mNotifyList.remove(who); 13292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 13322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 13332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 13362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1339d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt class TetherModeAliveState extends TetherMasterUtilState { 134019b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; 13412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 134319b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass 134419b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt // or crazy tests cases will fail 13456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt chooseUpstreamType(mTryCell); 13466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTryCell = !mTryCell; 13476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt turnOnMasterTetherSettings(); // may transition us out 13486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 13496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt @Override 13506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt public void exit() { 1351ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOffUpstreamMobileConnection(); 1352b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt notifyTetheredOfNewUpstreamIface(null); 13532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 1356c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what); 13572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 13582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.add(who); 1362b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED, 1363b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mUpstreamIfaceName); 13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_UNREQUESTED: 13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt who = (TetherInterfaceSM)message.obj; 13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int index = mNotifyList.indexOf(who); 13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (index != -1) { 13692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.remove(index); 13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mNotifyList.isEmpty()) { 1371d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt turnOffMasterTetherSettings(); // transitions appropriately 13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_UPSTREAM_CHANGED: 137605d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt // need to try DUN immediately if Wifi goes down 137705d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; 13786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt chooseUpstreamType(mTryCell); 13796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTryCell = !mTryCell; 13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_CELL_CONNECTION_RENEW: 13826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // make sure we're still using a requested connection - may have found 13836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // wifi or something since then. 13841039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt if (mCurrentConnectionSequence == message.arg1) { 1385c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville if (VDBG) { 1386924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.d(TAG, "renewing mobile connection - requeuing for another " + 1387924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt CELL_CONNECTION_RENEW_MS + "ms"); 1388924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 1389ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOnUpstreamMobileConnection(mMobileApnReserved); 13906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 139165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt break; 139201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo case CMD_RETRY_UPSTREAM: 139301758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo chooseUpstreamType(mTryCell); 139401758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo mTryCell = !mTryCell; 139501758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo break; 139601758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo default: 139701758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo retValue = false; 139801758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo break; 13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 140464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class ErrorState extends State { 14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int mErrorNotification; 14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 14126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt who.sendMessage(mErrorNotification); 14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 14152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt void notify(int msgType) { 14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mErrorNotification = msgType; 14212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object o : mNotifyList) { 14222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = (TetherInterfaceSM)o; 14236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(msgType); 14242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetIpForwardingEnabledErrorState extends ErrorState { 14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setIpForwardingEnabled"); 14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR); 14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetIpForwardingDisabledErrorState extends ErrorState { 14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setIpForwardingDisabled"); 14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR); 14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class StartTetheringErrorState extends ErrorState { 14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in startTethering"); 14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR); 14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1450c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class StopTetheringErrorState extends ErrorState { 14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in stopTethering"); 14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR); 14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1461c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetDnsForwardersErrorState extends ErrorState { 14672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setDnsForwarders"); 14702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR); 14712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1472c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 14732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1475c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1478d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1479d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1480d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 14812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 14822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mContext.checkCallingOrSelfPermission( 14832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) { 14842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println("Permission Denial: can't dump ConnectivityService.Tether " + 14852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt "from from pid=" + Binder.getCallingPid() + ", uid=" + 14862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Binder.getCallingUid()); 14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(); 14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println("Tether state:"); 14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object o : mIfaces.values()) { 14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(" "+o.toString()); 14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1496d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(); 14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 1499d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1500d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt} 1501