Tethering.java revision bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324
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;
465ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackbornimport android.os.UserHandle;
47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings;
48d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log;
49d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone;
51a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Savilleimport com.android.internal.telephony.PhoneConstants;
5264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.IState;
5364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.State;
5464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.StateMachine;
55cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkeyimport com.google.android.collect.Lists;
562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor;
582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter;
5904808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress;
604f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwaltimport java.net.Inet4Address;
61d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList;
62ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Collection;
632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
64ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Iterator;
652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
66cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey
67d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
68d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
74d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
75d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
76d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
77924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
78c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville    private final static boolean DBG = true;
79fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt    private final static boolean VDBG = false;
80d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
846fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private String[] mTetherableBluetoothRegexs;
85ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private Collection<Integer> mUpstreamIfaceTypes;
86ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
87b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // used to synchronize public access to members
88b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    private Object mPublicSync;
89b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
90ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE);
91ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI);
92ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN);
93ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
94ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // if we have to connect to mobile, what APN type should we use?  Calculated by examining the
95ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // upstream type list and the DUN_REQUIRED secure-setting
96ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE;
972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
98367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    private final INetworkManagementService mNMService;
99367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    private final INetworkStatsService mStatsService;
1003b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt    private final IConnectivityManager mConnService;
1017eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
1027eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
103dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
104030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
105d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
106d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
107d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
108bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
109b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt    private static final int USB_PREFIX_LENGTH        = 24;
110bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
111707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // USB is  192.168.42.1 and 255.255.255.0
112707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // Wifi is 192.168.43.1 and 255.255.255.0
113707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
114707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // with 255.255.255.0
115d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
1162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
117c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    private static final String[] DHCP_DEFAULT_RANGE = {
118c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
119c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
120c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
121c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.48.2", "192.168.48.254",
122c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    };
1232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
124bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt    private String[] mDefaultDnsServers;
125d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
126df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
1272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    private StateMachine mTetherMasterSM;
129a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
130a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1326c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private boolean mRndisEnabled;       // track the RNDIS function enabled state
1336c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private boolean mUsbTetherRequested; // true if USB tethering should be started
1346c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                                         // when RNDIS is enabled
135030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
136367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    public Tethering(Context context, INetworkManagementService nmService,
1373b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt            INetworkStatsService statsService, IConnectivityManager connService, Looper looper) {
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
139c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        mNMService = nmService;
140367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey        mStatsService = statsService;
1413b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt        mConnService = connService;
142dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
143d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
144b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        mPublicSync = new Object();
145b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
1462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
147d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1487eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1497eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1507eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1517eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
152dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
155bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
156d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
157770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood        filter.addAction(UsbManager.ACTION_USB_STATE);
1582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
159bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
160bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
161bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
162030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
163030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
164bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
165bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
166bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
1672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
169bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
170c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            mDhcpRange = DHCP_DEFAULT_RANGE;
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
17349348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        // load device config info
17449348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        updateConfiguration();
17549348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
17649348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        // TODO - remove and rely on real notifications of the current iface
177bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers = new String[2];
178bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers[0] = DNS_DEFAULT_SERVER1;
179bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers[1] = DNS_DEFAULT_SERVER2;
18049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt    }
18149348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
18249348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt    void updateConfiguration() {
183b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableUsbRegexs = mContext.getResources().getStringArray(
1842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
185b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableWifiRegexs = mContext.getResources().getStringArray(
1862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
187b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableBluetoothRegexs = mContext.getResources().getStringArray(
1886fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                com.android.internal.R.array.config_tether_bluetooth_regexs);
18949348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
19049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        int ifaceTypes[] = mContext.getResources().getIntArray(
191ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_types);
192b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        Collection<Integer> upstreamIfaceTypes = new ArrayList();
193ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        for (int i : ifaceTypes) {
194b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            upstreamIfaceTypes.add(new Integer(i));
195b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        }
196b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
197b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
198b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableUsbRegexs = tetherableUsbRegexs;
199b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableWifiRegexs = tetherableWifiRegexs;
200b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableBluetoothRegexs = tetherableBluetoothRegexs;
201b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mUpstreamIfaceTypes = upstreamIfaceTypes;
202ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
203ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
204ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        // check if the upstream type list needs to be modified due to secure-settings
205ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        checkDunRequired();
206d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
207d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2086143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen    public void interfaceStatusChanged(String iface, boolean up) {
209c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
2102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
21165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
212b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
213b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isWifi(iface)) {
214b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
215b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            } else if (isUsb(iface)) {
216b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
217b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                usb = true;
218b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            } else if (isBluetooth(iface)) {
219b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
220b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
221b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (found == false) return;
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2246143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen            if (up) {
2252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
226dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
231485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                if (isUsb(iface)) {
232485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                    // ignore usb0 down after enabling RNDIS
233485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                    // we will handle disconnect in interfaceRemoved instead
234fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                    if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
235485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                } else if (sm != null) {
2366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
241d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
242d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
243f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    public void interfaceLinkStateChanged(String iface, boolean up) {
244c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceLinkStateChanged " + iface + ", " + up);
24523eb297c771bd996fe8559178feb1a07be43ad72Irfan Sheriff        interfaceStatusChanged(iface, up);
246f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    }
247f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
248a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
249b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
250b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableUsbRegexs) {
251b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
252b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
253b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
254a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
255a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
256a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
257a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
258b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
259b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableWifiRegexs) {
260b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
261b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
262b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
263a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
264a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
265a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2666fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
267b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
268b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableBluetoothRegexs) {
269b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
270b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
271b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
2726fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2736fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2746c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
276c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
2772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
27865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
279b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
280b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isWifi(iface)) {
281b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
282b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
283b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isUsb(iface)) {
284b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
285b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                usb = true;
286b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
287b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isBluetooth(iface)) {
288b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
289b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
290b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (found == false) {
291b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
292b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return;
293b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
29465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
297c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
300dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
3012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
3032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
305d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
307c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
308b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
311c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (VDBG) {
312924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
313924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
3166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
318d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
319d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
320d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
32112b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall    public void limitReached(String limitName, String iface) {}
32212b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall
323db3c8678e5cbdfec011afaf25bde2091152c30adHaoyu Bai    public void interfaceClassDataActivityChanged(String label, boolean active) {}
324db3c8678e5cbdfec011afaf25bde2091152c30adHaoyu Bai
3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
326c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (DBG) Log.d(TAG, "Tethering " + iface);
3272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
328b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
330d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
334d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
338d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
341d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
342d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
344c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (DBG) Log.d(TAG, "Untethering " + iface);
3452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
346b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
352d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
356d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
363b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
365b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (sm == null) {
366b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
367b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ", ignoring");
368b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
369b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
370b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return sm.getLastError();
3715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
373d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
374b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO - move all private methods used only by the state machine into the state machine
375b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // to clarify what needs synchronized protection.
3762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
377d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3783b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt            if (!mConnService.isTetheringSupported()) return;
3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
381d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
382d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
387a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3896fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
391b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3966fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
4002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
403a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
404a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
4056fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
4066fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
4082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
4092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
4102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
411d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
412d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
413d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
41498c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
41598c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt                Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
4162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
4172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
4182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
4192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
4202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
4215ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackborn        mContext.sendStickyBroadcastAsUser(broadcast, UserHandle.ALL);
422fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt        if (DBG) {
423924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
424924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
425924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
4286fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
4346fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
4356fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4366fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4376fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4386fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4396fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
453db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
454db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
455db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
456db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
457db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
458db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
459db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
460a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
461a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
462a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
463a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
46450cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn        PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
46550cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                null, UserHandle.CURRENT);
466a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
467a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
468a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
469a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
470a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
471a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4726fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
473a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
474a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
475a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
476a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
477a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
478a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
479a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
480a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
481a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
48250cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn        notificationManager.notifyAsUser(null, mTetheredNotification.icon,
48350cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                mTetheredNotification, UserHandle.ALL);
484a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
485a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
486a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
487a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
488a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
489a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
49050cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
49150cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    UserHandle.ALL);
492a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
493a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
4962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
497d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
499770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
500b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                synchronized (Tethering.this.mPublicSync) {
5016c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
5026c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false);
5036c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    // start tethering if we have a request pending
5046c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    if (usbConnected && mRndisEnabled && mUsbTetherRequested) {
5056c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        tetherUsb(true);
5066c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    }
5076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mUsbTetherRequested = false;
5086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
5092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
510adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(
511adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                        ConnectivityManager.EXTRA_NETWORK_INFO);
512adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                if (networkInfo != null &&
513adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                        networkInfo.getDetailedState() != NetworkInfo.DetailedState.FAILED) {
514adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                    if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
515adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                    mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
516adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                }
517d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
518d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
519d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
520d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
5216c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private void tetherUsb(boolean enable) {
522c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "tetherUsb " + enable);
5233c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
52465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
52565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
526c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            ifaces = mNMService.listInterfaces();
52765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5283c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
52965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
532a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
5336c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                int result = (enable ? tether(iface) : untether(iface));
5346c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (result == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
5356c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    return;
53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
53765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
5396c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        Log.e(TAG, "unable start or stop USB tethering");
54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
543d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
544c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
54565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5466c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        // toggle the USB interfaces
5476c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        String[] ifaces = new String[0];
5483c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        try {
5496c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            ifaces = mNMService.listInterfaces();
5506c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        } catch (Exception e) {
5516c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
5526c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            return false;
5536c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        }
5546c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        for (String iface : ifaces) {
5556c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            if (isUsb(iface)) {
5566c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                InterfaceConfiguration ifcg = null;
5576c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                try {
5586c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    ifcg = mNMService.getInterfaceConfig(iface);
5596c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    if (ifcg != null) {
5606c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
561ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                        ifcg.setLinkAddress(new LinkAddress(addr, USB_PREFIX_LENGTH));
5626c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        if (enabled) {
563ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                            ifcg.setInterfaceUp();
5646c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        } else {
565ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                            ifcg.setInterfaceDown();
56665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
567ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                        ifcg.clearFlag("running");
5686c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        mNMService.setInterfaceConfig(iface, ifcg);
56965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
5706c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                } catch (Exception e) {
5716c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    Log.e(TAG, "Error configuring interface " + iface, e);
5726c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    return false;
57365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
57465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
5756c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood         }
57665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
57765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
57865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
57965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
580b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO - return copies so people can't tamper
5812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5896fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public String[] getTetherableBluetoothRegexs() {
5906fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return mTetherableBluetoothRegexs;
5916fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
5926fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
5936c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    public int setUsbTethering(boolean enable) {
594c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
5956c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
5966c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
597b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
5986c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            if (enable) {
5996c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (mRndisEnabled) {
6006c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    tetherUsb(true);
6016c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                } else {
6026c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mUsbTetherRequested = true;
6036c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
6046c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
6056c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            } else {
6066c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                tetherUsb(false);
6076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (mRndisEnabled) {
6086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    usbManager.setCurrentFunction(null, false);
6096c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
6106c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                mUsbTetherRequested = false;
6116c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            }
6126c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        }
6136c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
6146c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    }
6156c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
616ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public int[] getUpstreamIfaceTypes() {
617b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        int values[];
618b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
619b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            updateConfiguration();
620b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            values = new int[mUpstreamIfaceTypes.size()];
621b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
622b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
623b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                values[i] = iterator.next();
624b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
625ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
626ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        return values;
627c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
628c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
629ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public void checkDunRequired() {
630bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown        int secureSetting = Settings.Global.getInt(mContext.getContentResolver(),
631bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown                Settings.Global.TETHER_DUN_REQUIRED, 2);
632b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
633b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            // 2 = not set, 0 = DUN not required, 1 = DUN required
634b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (secureSetting != 2) {
635b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                int requiredApn = (secureSetting == 1 ?
636b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ConnectivityManager.TYPE_MOBILE_DUN :
637b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ConnectivityManager.TYPE_MOBILE_HIPRI);
638b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
639b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
640b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(MOBILE_TYPE);
641b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
642b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
643b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(HIPRI_TYPE);
644b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
645b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
646b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(DUN_TYPE);
647b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
648b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                } else {
649b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
650b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(DUN_TYPE);
651b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
652b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
653b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(MOBILE_TYPE);
654b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
655b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
656b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(HIPRI_TYPE);
657b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
658ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
659b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
660b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
661b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_DUN;
662ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            } else {
663b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_HIPRI;
664ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
66549348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        }
666c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
667c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
668b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO review API - maybe return ArrayList<String> here and below?
6692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
671b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
6722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
687cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey    public String[] getTetheredIfacePairs() {
688cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        final ArrayList<String> list = Lists.newArrayList();
689b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
690cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey            for (TetherInterfaceSM sm : mIfaces.values()) {
691cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                if (sm.isTethered()) {
692cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                    list.add(sm.mMyUpstreamIfaceName);
693cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                    list.add(sm.mIfaceName);
694cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                }
695cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey            }
696cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        }
697cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        return list.toArray(new String[list.size()]);
698cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey    }
699cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey
7002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
7012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
702b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
7032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
7042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
7052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
7062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
7072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
7122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
7132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
7195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
720b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
7215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
7225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
7235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
7245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
7255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
7265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
7305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
7315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
7325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
7345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
7355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7361ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //TODO: Temporary handling upstream change triggered without
7371ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      CONNECTIVITY_ACTION. Only to accomodate interface
7381ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      switch during HO.
7391ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      @see bug/4455071
7401ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    public void handleTetherIfaceChange() {
7411ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
7421ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    }
7431ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo
74464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherInterfaceSM extends StateMachine {
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
7466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
7472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
7486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
7492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
7506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
7512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
7526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
7532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
7546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
7552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
7566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
7586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
7606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
7626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
7646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
7666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
7676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
768951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mDefaultState;
7712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
77364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartingState;
77464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetheredState;
7752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mUnavailableState;
7772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
78565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
787dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
788dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
79065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
80864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville            IState current = getCurrentState();
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
8155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
8162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
819b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public int getLastError() {
820b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
821b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mLastError;
822b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
8245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
825b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setLastError(int error) {
826b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
827b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mLastError = error;
8285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
829b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (isErrored()) {
830b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUsb) {
831b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        // note everything's been unwound by this point so nothing to do on
832b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        // further error..
833b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        Tethering.this.configureUsbIface(false);
834b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
8355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
8365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
8375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
8385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
839b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isAvailable() {
840b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
841b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mAvailable;
842b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
845b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setAvailable(boolean available) {
846b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
847b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mAvailable = available;
848b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
851b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isTethered() {
852b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
853b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mTethered;
854b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
857b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setTethered(boolean tethered) {
858b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
859b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mTethered = tethered;
860b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
863b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isErrored() {
864b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
865b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
866b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
86964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class InitialState extends State {
8702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
879fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
8802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
8835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
89964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class StartingState extends State {
9002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
90365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
904d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
9056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
9085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
909951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
91065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
91165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
91265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
9132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
915951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
916951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
9172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
920fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
9212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
92765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
928d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
9295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
9305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
93165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
93265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
93365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
9342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
9352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
9435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
9442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
9492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
95764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetheredState extends State {
9582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
961c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.tetherInterface(mIfaceName);
9622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
9633b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                    Log.e(TAG, "Error Tethering: " + e.toString());
9645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
9655a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
966951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
9672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
969c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (DBG) Log.d(TAG, "Tethered " + mIfaceName);
9702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
9722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
974731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt
975b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            private void cleanupUpstream() {
976731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                if (mMyUpstreamIfaceName != null) {
977731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // note that we don't care about errors here.
978731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // sometimes interfaces are gone before we get
979731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // to remove their rules, which generates errors.
980731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // just do the best we can.
981731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    try {
982731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        // about to tear down NAT; gather remaining statistics
983731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        mStatsService.forceUpdate();
984731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    } catch (Exception e) {
985731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        if (VDBG) Log.e(TAG, "Exception in forceUpdate: " + e.toString());
986731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    }
987731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    try {
988731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
989731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    } catch (Exception e) {
990731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        if (VDBG) Log.e(TAG, "Exception in disableNat: " + e.toString());
991731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    }
992731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    mMyUpstreamIfaceName = null;
993731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                }
994731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                return;
995731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt            }
996731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt
9972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
999fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
10002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
10022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
10042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
1005731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1007c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
10146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
10152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
101665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
1017d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
10185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
10195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
102065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
102165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
10225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
10242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
10252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1026c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                        if (DBG) Log.d(TAG, "Untethered " + mIfaceName);
10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
10296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
10301039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
10311039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
10321039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
1033c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                            if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
10341039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
10351039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
1036731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
10386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
1039c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
10406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
10413b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                                Log.e(TAG, "Exception enabling Nat: " + e.toString());
10426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
1043c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
10446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
10466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
10476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
10486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
10496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
10516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
10526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
1062731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1064c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1075fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                        if (DBG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
107765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1078d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
108065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
108165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
109264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class UnavailableState extends State {
10932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
11002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
11012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
11052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
11062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
11092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
11102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
11112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
11122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
11132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
11165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
11175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
11182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
111965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
11202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
112264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherMasterSM extends StateMachine {
11232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
11276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
11286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
11292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
11306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
11316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1132951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
11332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
11352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
11362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
11372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
11382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
11392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
114164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetherModeAliveState;
11422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingEnabledErrorState;
114464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingDisabledErrorState;
114564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartTetheringErrorState;
114664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStopTetheringErrorState;
114764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetDnsForwardersErrorState;
11482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114968ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt        private ArrayList<TetherInterfaceSM> mNotifyList;
11502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11511039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
1152ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
11532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1154b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1155b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1159dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1160dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
117968ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt            mNotifyList = new ArrayList<TetherInterfaceSM>();
11802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
118364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetherMasterUtilState extends State {
11846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1187d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1188d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1191ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected String enableString(int apnType) {
1192ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                switch (apnType) {
1193ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_DUN:
1194ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_DUN_ALWAYS;
1195ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE:
1196ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_HIPRI:
1197ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_HIPRI;
1198ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
1199ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                return null;
1200ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
1201ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOnUpstreamMobileConnection(int apnType) {
12021039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
1203ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType == ConnectivityManager.TYPE_NONE) return false;
1204ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection();
1205a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                int result = PhoneConstants.APN_REQUEST_FAILED;
1206ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String enableString = enableString(apnType);
1207ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (enableString == null) return false;
1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
12093b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                    result = mConnService.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1210ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            enableString, new Binder());
1211d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
12131039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
1214a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_ALREADY_ACTIVE:
1215a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_REQUEST_STARTED:
1216ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = apnType;
12171039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
12181039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
12191039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
12206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
1221a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_REQUEST_FAILED:
12226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
12231039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
12246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
12256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
12266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1227d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1229ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOffUpstreamMobileConnection() {
1230c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                // ignore pending renewal requests
1231c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                ++mCurrentConnectionSequence;
1232ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) {
12336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
12343b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                        mConnService.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1235ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                                enableString(mMobileApnReserved));
12366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
12376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
12386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
1239ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1240d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1241d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1242d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1244d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1245c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(true);
1246d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1247951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1248d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1249d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1250d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1251c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.startTethering(mDhcpRange);
1252d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1253547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1254c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.stopTethering();
1255c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.startTethering(mDhcpRange);
1256547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1257547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1258547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1259547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1260d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1261d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1262bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    mNMService.setDnsForwarders(mDefaultDnsServers);
1263d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1264951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1265d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1266d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1267d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1268d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1270d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1271c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
1272d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1273d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1274d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1275d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1276d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1277c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
1278d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1279d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1280d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1281d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1282d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1283d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1284d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1285ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1286ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
1287ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                int upType = ConnectivityManager.TYPE_NONE;
1288ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String iface = null;
128905d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
129049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt                updateConfiguration();
129149348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1292b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                synchronized (mPublicSync) {
1293b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (VDBG) {
1294b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        Log.d(TAG, "chooseUpstreamType has upstream iface types:");
1295b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        for (Integer netType : mUpstreamIfaceTypes) {
1296b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            Log.d(TAG, " " + netType);
1297b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        }
129849348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt                    }
129949348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1300b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    for (Integer netType : mUpstreamIfaceTypes) {
1301b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        NetworkInfo info = null;
1302b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        try {
1303b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            info = mConnService.getNetworkInfo(netType.intValue());
1304b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        } catch (RemoteException e) { }
1305b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        if ((info != null) && info.isConnected()) {
1306b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            upType = netType.intValue();
1307b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            break;
1308b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        }
1309c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1310c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
13111039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
1312fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) {
1313ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn ="
1314ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            + mPreferredUpstreamMobileApn + ", got type=" + upType);
1315924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
1316ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1317ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                // if we're on DUN, put our own grab on it
1318ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_MOBILE_DUN ||
1319ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType == ConnectivityManager.TYPE_MOBILE_HIPRI) {
1320ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    turnOnUpstreamMobileConnection(upType);
1321c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                } else if (upType != ConnectivityManager.TYPE_NONE) {
1322c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                    /* If we've found an active upstream connection that's not DUN/HIPRI
1323c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * we should stop any outstanding DUN/HIPRI start requests.
1324c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     *
1325c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * If we found NONE we don't want to do this as we want any previous
1326c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * requests to keep trying to bring up something we can use.
1327c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     */
1328c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                    turnOffUpstreamMobileConnection();
13296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1330ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1331ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_NONE) {
1332ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    boolean tryAgainLater = true;
1333ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) &&
1334ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) {
1335ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        // we think mobile should be coming up - don't set a retry
1336ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        tryAgainLater = false;
13371039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
1338ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (tryAgainLater) {
13391039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
134077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
1341ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                } else {
1342ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    LinkProperties linkProperties = null;
1343ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
13443b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                        linkProperties = mConnService.getLinkProperties(upType);
1345ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1346bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    if (linkProperties != null) {
1347bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        iface = linkProperties.getInterfaceName();
1348bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        String[] dnsServers = mDefaultDnsServers;
1349bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        Collection<InetAddress> dnses = linkProperties.getDnses();
1350bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        if (dnses != null) {
13514f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            // we currently only handle IPv4
13524f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            ArrayList<InetAddress> v4Dnses =
13534f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                    new ArrayList<InetAddress>(dnses.size());
13544f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            for (InetAddress dnsAddress : dnses) {
13554f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                if (dnsAddress instanceof Inet4Address) {
13564f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                    v4Dnses.add(dnsAddress);
13574f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                }
13584f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            }
13594f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            if (v4Dnses.size() > 0) {
13604f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                dnsServers = NetworkUtils.makeStrings(v4Dnses);
13614f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            }
1362bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        }
1363bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        try {
1364bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                            mNMService.setDnsForwarders(dnsServers);
1365bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        } catch (Exception e) {
1366bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                            transitionTo(mSetDnsForwardersErrorState);
1367bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        }
1368bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    }
13696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1370b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
13716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1372ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1373b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1374fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1375b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
137668ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                for (TetherInterfaceSM sm : mNotifyList) {
13776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
13786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1389fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
139468ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
140068ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
14036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1414d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
141519b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14184f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
14194f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt
142019b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
142119b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
14226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
14236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
14246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
14256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
14266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
1427ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                turnOffUpstreamMobileConnection();
1428b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1432fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
143768ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1439b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1440b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
144468ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
144768ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                            if (DBG) Log.d(TAG, "TetherModeAlive removing notifyee " + who);
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1450d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
145168ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                            } else {
145268ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                if (DBG) {
145368ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                    Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
145468ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                            " live requests:");
145568ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                    for (Object o : mNotifyList) Log.d(TAG, "  " + o);
145668ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                }
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
145868ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        } else {
145968ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                           Log.e(TAG, "TetherModeAliveState UNREQUESTED has unknown who: " + who);
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
146305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
146405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
14666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
14672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
14696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
14706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
14711039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1472c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                            if (VDBG) {
1473924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1474924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1475924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
1476ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            turnOnUpstreamMobileConnection(mMobileApnReserved);
14776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
147865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
147901758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    case CMD_RETRY_UPSTREAM:
148001758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        chooseUpstreamType(mTryCell);
148101758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        mTryCell = !mTryCell;
148201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
148301758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    default:
148401758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        retValue = false;
148501758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
14862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
149164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class ErrorState extends State {
14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
15002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
15012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
15022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
15032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
15042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
15052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
15072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
15082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
15092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
15106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
15112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
15122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
15162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
15192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
15202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
15242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
15272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
15282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
15322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
15352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
15362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1537c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
15432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
15462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
15472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1548c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
15542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
15572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
15582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1559c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
15602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1562c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1565d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1566d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1567d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
15682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
15692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
15702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
15712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
15722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
15732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
15742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
15752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1577b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
1578b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println("mUpstreamIfaceTypes: ");
1579b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (Integer netType : mUpstreamIfaceTypes) {
1580b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                pw.println(" " + netType);
1581b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
158249348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1583b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println();
1584b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println("Tether state:");
15852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
158668ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                pw.println(" " + o);
15872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1588d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
15892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1591d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1592d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1593