Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 19 | import static android.hardware.usb.UsbManager.USB_CONFIGURED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 20 | import static android.hardware.usb.UsbManager.USB_CONNECTED; |
| 21 | import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 23 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 24 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 25 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 26 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; |
| 27 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 28 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
| 29 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 30 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 31 | import static com.android.server.ConnectivityService.SHORT_ARG; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 32 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 33 | import android.app.Notification; |
| 34 | import android.app.NotificationManager; |
| 35 | import android.app.PendingIntent; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 36 | import android.bluetooth.BluetoothAdapter; |
| 37 | import android.bluetooth.BluetoothPan; |
| 38 | import android.bluetooth.BluetoothProfile; |
| 39 | import android.bluetooth.BluetoothProfile.ServiceListener; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 41 | import android.content.ComponentName; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 42 | import android.content.Context; |
| 43 | import android.content.Intent; |
| 44 | import android.content.IntentFilter; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 46 | import android.content.res.Resources; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 47 | import android.hardware.usb.UsbManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 48 | import android.net.ConnectivityManager; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 49 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 50 | import android.net.INetworkStatsService; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 51 | import android.net.IpPrefix; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 52 | import android.net.LinkAddress; |
Robert Greenwalt | 05d8936 | 2011-01-23 16:04:05 -0800 | [diff] [blame] | 53 | import android.net.LinkProperties; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 54 | import android.net.Network; |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 55 | import android.net.NetworkCapabilities; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 56 | import android.net.NetworkInfo; |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 57 | import android.net.NetworkRequest; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 58 | import android.net.NetworkState; |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 59 | import android.net.NetworkUtils; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 60 | import android.net.RouteInfo; |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 61 | import android.net.util.PrefixUtils; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 62 | import android.net.util.SharedLog; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 63 | import android.net.wifi.WifiManager; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 64 | import android.os.Binder; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 65 | import android.os.Bundle; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 66 | import android.os.Handler; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 67 | import android.os.INetworkManagementService; |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 68 | import android.os.Looper; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 69 | import android.os.Message; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 70 | import android.os.Parcel; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 71 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 72 | import android.os.ResultReceiver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 73 | import android.os.UserHandle; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 74 | import android.os.UserManager; |
| 75 | import android.os.UserManagerInternal; |
| 76 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 77 | import android.provider.Settings; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 78 | import android.telephony.CarrierConfigManager; |
Junda Liu | 3853158 | 2014-10-28 13:52:51 -0700 | [diff] [blame] | 79 | import android.telephony.TelephonyManager; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 80 | import android.text.TextUtils; |
Christopher Wiley | e949039 | 2016-05-26 15:57:29 -0700 | [diff] [blame] | 81 | import android.util.ArrayMap; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 82 | import android.util.Log; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 83 | import android.util.SparseArray; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 84 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 85 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 86 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 87 | import com.android.internal.notification.SystemNotificationChannels; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 88 | import com.android.internal.telephony.IccCardConstants; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 89 | import com.android.internal.telephony.TelephonyIntents; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 90 | import com.android.internal.util.DumpUtils; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 91 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 92 | import com.android.internal.util.MessageUtils; |
| 93 | import com.android.internal.util.Protocol; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 94 | import com.android.internal.util.State; |
| 95 | import com.android.internal.util.StateMachine; |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 96 | import com.android.server.LocalServices; |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 97 | import com.android.server.connectivity.tethering.IControlsTethering; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 98 | import com.android.server.connectivity.tethering.IPv6TetheringCoordinator; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 99 | import com.android.server.connectivity.tethering.OffloadController; |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 100 | import com.android.server.connectivity.tethering.SimChangeListener; |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 101 | import com.android.server.connectivity.tethering.TetherInterfaceStateMachine; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 102 | import com.android.server.connectivity.tethering.TetheringConfiguration; |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 103 | import com.android.server.connectivity.tethering.TetheringDependencies; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 104 | import com.android.server.connectivity.tethering.UpstreamNetworkMonitor; |
Lorenzo Colitti | f4e90ea | 2013-10-31 23:30:47 +0900 | [diff] [blame] | 105 | import com.android.server.net.BaseNetworkObserver; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 106 | |
| 107 | import java.io.FileDescriptor; |
| 108 | import java.io.PrintWriter; |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 109 | import java.net.Inet4Address; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 110 | import java.net.InetAddress; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 111 | import java.util.ArrayList; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 112 | import java.util.Arrays; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 113 | import java.util.Collection; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 114 | import java.util.HashMap; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 115 | import java.util.HashSet; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 116 | import java.util.Iterator; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 117 | import java.util.Map; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 118 | import java.util.Set; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 119 | import java.util.concurrent.atomic.AtomicInteger; |
| 120 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 121 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 122 | /** |
| 123 | * @hide |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 124 | * |
Christopher Wiley | 3b1d922 | 2016-05-20 16:44:04 -0700 | [diff] [blame] | 125 | * This class holds much of the business logic to allow Android devices |
| 126 | * to act as IP gateways via USB, BT, and WiFi interfaces. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 127 | */ |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 128 | public class Tethering extends BaseNetworkObserver { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 129 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 130 | private final static String TAG = Tethering.class.getSimpleName(); |
Joe Onorato | 12acbd7 | 2016-02-01 17:49:31 -0800 | [diff] [blame] | 131 | private final static boolean DBG = false; |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 132 | private final static boolean VDBG = false; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 133 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 134 | protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning"; |
| 135 | |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 136 | private static final Class[] messageClasses = { |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 137 | Tethering.class, TetherMasterSM.class, TetherInterfaceStateMachine.class |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 138 | }; |
| 139 | private static final SparseArray<String> sMagicDecoderRing = |
| 140 | MessageUtils.findMessageNames(messageClasses); |
| 141 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 142 | // {@link ComponentName} of the Service used to run tether provisioning. |
| 143 | private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources |
| 144 | .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable)); |
| 145 | |
| 146 | private static class TetherState { |
| 147 | public final TetherInterfaceStateMachine stateMachine; |
| 148 | public int lastState; |
| 149 | public int lastError; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 150 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 151 | public TetherState(TetherInterfaceStateMachine sm) { |
| 152 | stateMachine = sm; |
| 153 | // Assume all state machines start out available and with no errors. |
| 154 | lastState = IControlsTethering.STATE_AVAILABLE; |
| 155 | lastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 156 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 157 | |
| 158 | public boolean isCurrentlyServing() { |
| 159 | switch (lastState) { |
| 160 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 161 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 162 | return true; |
| 163 | default: |
| 164 | return false; |
| 165 | } |
| 166 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 167 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 168 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 169 | private final SharedLog mLog = new SharedLog(TAG); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 170 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 171 | // used to synchronize public access to members |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 172 | private final Object mPublicSync; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 173 | private final Context mContext; |
| 174 | private final ArrayMap<String, TetherState> mTetherStates; |
| 175 | private final BroadcastReceiver mStateReceiver; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 176 | private final INetworkManagementService mNMService; |
| 177 | private final INetworkStatsService mStatsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 178 | private final INetworkPolicyManager mPolicyManager; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 179 | private final Looper mLooper; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 180 | private final MockableSystemProperties mSystemProperties; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 181 | private final StateMachine mTetherMasterSM; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 182 | private final OffloadController mOffloadController; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 183 | private final UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 184 | // TODO: Figure out how to merge this and other downstream-tracking objects |
| 185 | // into a single coherent structure. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 186 | private final HashSet<TetherInterfaceStateMachine> mForwardedDownstreams; |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 187 | private final SimChangeListener mSimChange; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 188 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 189 | private volatile TetheringConfiguration mConfig; |
| 190 | private String mCurrentUpstreamIface; |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 191 | private Notification.Builder mTetheredNotificationBuilder; |
| 192 | private int mLastNotificationId; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 193 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 194 | private boolean mRndisEnabled; // track the RNDIS function enabled state |
| 195 | private boolean mUsbTetherRequested; // true if USB tethering should be started |
| 196 | // when RNDIS is enabled |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 197 | // True iff. WiFi tethering should be started when soft AP is ready. |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 198 | private boolean mWifiTetherRequested; |
| 199 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 200 | public Tethering(Context context, INetworkManagementService nmService, |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 201 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 202 | Looper looper, MockableSystemProperties systemProperties, |
| 203 | TetheringDependencies deps) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 204 | mLog.mark("constructed"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 205 | mContext = context; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 206 | mNMService = nmService; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 207 | mStatsService = statsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 208 | mPolicyManager = policyManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 209 | mLooper = looper; |
| 210 | mSystemProperties = systemProperties; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 211 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 212 | mPublicSync = new Object(); |
| 213 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 214 | mTetherStates = new ArrayMap<>(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 215 | |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 216 | mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 217 | mTetherMasterSM.start(); |
| 218 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 219 | final Handler smHandler = mTetherMasterSM.getHandler(); |
| 220 | mOffloadController = new OffloadController(smHandler, |
| 221 | deps.getOffloadHardwareInterface(smHandler, mLog), |
Lorenzo Colitti | 5a7dea1 | 2017-07-12 15:48:07 +0900 | [diff] [blame] | 222 | mContext.getContentResolver(), mNMService, |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 223 | mLog); |
Erik Kline | ebdb8c8 | 2017-01-10 17:37:54 +0900 | [diff] [blame] | 224 | mUpstreamNetworkMonitor = new UpstreamNetworkMonitor( |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 225 | mContext, mTetherMasterSM, mLog, TetherMasterSM.EVENT_UPSTREAM_CALLBACK); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 226 | mForwardedDownstreams = new HashSet<>(); |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 227 | mSimChange = new SimChangeListener( |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 228 | mContext, smHandler, () -> reevaluateSimCardProvisioning()); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 229 | |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 230 | mStateReceiver = new StateReceiver(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 231 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 232 | filter.addAction(UsbManager.ACTION_USB_STATE); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 233 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 234 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 235 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 236 | mContext.registerReceiver(mStateReceiver, filter, null, smHandler); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 237 | |
| 238 | filter = new IntentFilter(); |
Robert Greenwalt | 030f5e1 | 2010-03-10 16:41:03 -0800 | [diff] [blame] | 239 | filter.addAction(Intent.ACTION_MEDIA_SHARED); |
| 240 | filter.addAction(Intent.ACTION_MEDIA_UNSHARED); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 241 | filter.addDataScheme("file"); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 242 | mContext.registerReceiver(mStateReceiver, filter, null, smHandler); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 243 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 244 | UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class); |
| 245 | |
| 246 | // this check is useful only for some unit tests; example: ConnectivityServiceTest |
| 247 | if (userManager != null) { |
| 248 | userManager.addUserRestrictionsListener(new TetheringUserRestrictionListener(this)); |
| 249 | } |
| 250 | |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 251 | // load device config info |
| 252 | updateConfiguration(); |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Sreeram Ramachandran | 62a6131 | 2014-07-24 16:05:17 -0700 | [diff] [blame] | 255 | // We can't do this once in the Tethering() constructor and cache the value, because the |
| 256 | // CONNECTIVITY_SERVICE is registered only after the Tethering() constructor has completed. |
| 257 | private ConnectivityManager getConnectivityManager() { |
| 258 | return (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 259 | } |
| 260 | |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 261 | private WifiManager getWifiManager() { |
| 262 | return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| 263 | } |
| 264 | |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 265 | // NOTE: This is always invoked on the mLooper thread. |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 266 | private void updateConfiguration() { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 267 | mConfig = new TetheringConfiguration(mContext, mLog); |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 268 | mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 271 | private void maybeUpdateConfiguration() { |
| 272 | final int dunCheck = TetheringConfiguration.checkDunRequired(mContext); |
| 273 | if (dunCheck == mConfig.dunCheck) return; |
| 274 | updateConfiguration(); |
| 275 | } |
| 276 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 277 | @Override |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 278 | public void interfaceStatusChanged(String iface, boolean up) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 279 | // Never called directly: only called from interfaceLinkStateChanged. |
| 280 | // See NetlinkHandler.cpp:71. |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 281 | if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 282 | synchronized (mPublicSync) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 283 | if (up) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 284 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 285 | } else { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 286 | if (ifaceNameToType(iface) == ConnectivityManager.TETHERING_BLUETOOTH) { |
| 287 | stopTrackingInterfaceLocked(iface); |
Christopher Wiley | d30aaeb | 2016-07-08 09:33:50 -0700 | [diff] [blame] | 288 | } else { |
| 289 | // Ignore usb0 down after enabling RNDIS. |
| 290 | // We will handle disconnect in interfaceRemoved. |
| 291 | // Similarly, ignore interface down for WiFi. We monitor WiFi AP status |
| 292 | // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent. |
| 293 | if (VDBG) Log.d(TAG, "ignore interface down for " + iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 299 | @Override |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 300 | public void interfaceLinkStateChanged(String iface, boolean up) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 301 | interfaceStatusChanged(iface, up); |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 304 | private int ifaceNameToType(String iface) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 305 | final TetheringConfiguration cfg = mConfig; |
| 306 | |
| 307 | if (cfg.isWifi(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 308 | return ConnectivityManager.TETHERING_WIFI; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 309 | } else if (cfg.isUsb(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 310 | return ConnectivityManager.TETHERING_USB; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 311 | } else if (cfg.isBluetooth(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 312 | return ConnectivityManager.TETHERING_BLUETOOTH; |
| 313 | } |
| 314 | return ConnectivityManager.TETHERING_INVALID; |
| 315 | } |
| 316 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 317 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 318 | public void interfaceAdded(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 319 | if (VDBG) Log.d(TAG, "interfaceAdded " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 320 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 321 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 322 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 325 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 326 | public void interfaceRemoved(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 327 | if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 328 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 329 | stopTrackingInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 333 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 334 | if (!isTetherProvisioningRequired()) { |
| 335 | enableTetheringInternal(type, true, receiver); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (showProvisioningUi) { |
| 340 | runUiTetherProvisioningAndEnable(type, receiver); |
| 341 | } else { |
| 342 | runSilentTetherProvisioningAndEnable(type, receiver); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | public void stopTethering(int type) { |
| 347 | enableTetheringInternal(type, false, null); |
| 348 | if (isTetherProvisioningRequired()) { |
| 349 | cancelTetherProvisioningRechecks(type); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Check if the device requires a provisioning check in order to enable tethering. |
| 355 | * |
| 356 | * @return a boolean - {@code true} indicating tether provisioning is required by the carrier. |
| 357 | */ |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 358 | @VisibleForTesting |
| 359 | protected boolean isTetherProvisioningRequired() { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 360 | String[] provisionApp = mContext.getResources().getStringArray( |
| 361 | com.android.internal.R.array.config_mobile_hotspot_provision_app); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 362 | if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 363 | || provisionApp == null) { |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | // Check carrier config for entitlement checks |
| 368 | final CarrierConfigManager configManager = (CarrierConfigManager) mContext |
| 369 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Rebecca Silberstein | b07f140 | 2016-10-04 13:53:33 -0700 | [diff] [blame] | 370 | if (configManager != null && configManager.getConfig() != null) { |
| 371 | // we do have a CarrierConfigManager and it has a config. |
| 372 | boolean isEntitlementCheckRequired = configManager.getConfig().getBoolean( |
| 373 | CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL); |
| 374 | if (!isEntitlementCheckRequired) { |
| 375 | return false; |
| 376 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 377 | } |
| 378 | return (provisionApp.length == 2); |
| 379 | } |
| 380 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 381 | // Used by the SIM card change observation code. |
| 382 | // TODO: De-duplicate above code. |
| 383 | private boolean hasMobileHotspotProvisionApp() { |
| 384 | try { |
| 385 | if (!mContext.getResources().getString(com.android.internal.R.string. |
| 386 | config_mobile_hotspot_provision_app_no_ui).isEmpty()) { |
| 387 | Log.d(TAG, "re-evaluate provisioning"); |
| 388 | return true; |
| 389 | } |
| 390 | } catch (Resources.NotFoundException e) {} |
| 391 | Log.d(TAG, "no prov-check needed for new SIM"); |
| 392 | return false; |
| 393 | } |
| 394 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Enables or disables tethering for the given type. This should only be called once |
| 397 | * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks |
| 398 | * for the specified interface. |
| 399 | */ |
| 400 | private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 401 | boolean isProvisioningRequired = enable && isTetherProvisioningRequired(); |
| 402 | int result; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 403 | switch (type) { |
| 404 | case ConnectivityManager.TETHERING_WIFI: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 405 | result = setWifiTethering(enable); |
| 406 | if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 407 | scheduleProvisioningRechecks(type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 408 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 409 | sendTetherResult(receiver, result); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 410 | break; |
| 411 | case ConnectivityManager.TETHERING_USB: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 412 | result = setUsbTethering(enable); |
| 413 | if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 414 | scheduleProvisioningRechecks(type); |
| 415 | } |
| 416 | sendTetherResult(receiver, result); |
| 417 | break; |
| 418 | case ConnectivityManager.TETHERING_BLUETOOTH: |
| 419 | setBluetoothTethering(enable, receiver); |
| 420 | break; |
| 421 | default: |
| 422 | Log.w(TAG, "Invalid tether type."); |
| 423 | sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private void sendTetherResult(ResultReceiver receiver, int result) { |
| 428 | if (receiver != null) { |
| 429 | receiver.send(result, null); |
| 430 | } |
| 431 | } |
| 432 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 433 | private int setWifiTethering(final boolean enable) { |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 434 | int rval = ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 435 | final long ident = Binder.clearCallingIdentity(); |
| 436 | try { |
| 437 | synchronized (mPublicSync) { |
| 438 | mWifiTetherRequested = enable; |
| 439 | final WifiManager mgr = getWifiManager(); |
| 440 | if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) || |
| 441 | (!enable && mgr.stopSoftAp())) { |
| 442 | rval = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 443 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 444 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 445 | } finally { |
| 446 | Binder.restoreCallingIdentity(ident); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 447 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 448 | return rval; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 451 | private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) { |
| 452 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 453 | if (adapter == null || !adapter.isEnabled()) { |
| 454 | Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " + |
| 455 | (adapter == null)); |
| 456 | sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | adapter.getProfileProxy(mContext, new ServiceListener() { |
| 461 | @Override |
| 462 | public void onServiceDisconnected(int profile) { } |
| 463 | |
| 464 | @Override |
| 465 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 466 | ((BluetoothPan) proxy).setBluetoothTethering(enable); |
| 467 | // TODO: Enabling bluetooth tethering can fail asynchronously here. |
| 468 | // We should figure out a way to bubble up that failure instead of sending success. |
Jeremy Klein | 45ae04a | 2016-02-05 10:37:32 -0800 | [diff] [blame] | 469 | int result = ((BluetoothPan) proxy).isTetheringOn() == enable ? |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 470 | ConnectivityManager.TETHER_ERROR_NO_ERROR : |
| 471 | ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 472 | sendTetherResult(receiver, result); |
| 473 | if (enable && isTetherProvisioningRequired()) { |
| 474 | scheduleProvisioningRechecks(ConnectivityManager.TETHERING_BLUETOOTH); |
| 475 | } |
| 476 | adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); |
| 477 | } |
| 478 | }, BluetoothProfile.PAN); |
| 479 | } |
| 480 | |
| 481 | private void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 482 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 483 | sendUiTetherProvisionIntent(type, proxyReceiver); |
| 484 | } |
| 485 | |
| 486 | private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 487 | Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING); |
| 488 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 489 | intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); |
| 490 | final long ident = Binder.clearCallingIdentity(); |
| 491 | try { |
| 492 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 493 | } finally { |
| 494 | Binder.restoreCallingIdentity(ident); |
| 495 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /** |
Erik Kline | fdd4111 | 2017-01-12 17:02:51 +0900 | [diff] [blame] | 499 | * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result |
| 500 | * is successful before firing back up to the wrapped receiver. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 501 | * |
| 502 | * @param type The type of tethering being enabled. |
| 503 | * @param receiver A ResultReceiver which will be called back with an int resultCode. |
| 504 | * @return The proxy receiver. |
| 505 | */ |
| 506 | private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) { |
| 507 | ResultReceiver rr = new ResultReceiver(null) { |
| 508 | @Override |
| 509 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 510 | // If provisioning is successful, enable tethering, otherwise just send the error. |
| 511 | if (resultCode == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 512 | enableTetheringInternal(type, true, receiver); |
| 513 | } else { |
| 514 | sendTetherResult(receiver, resultCode); |
| 515 | } |
| 516 | } |
| 517 | }; |
| 518 | |
| 519 | // The following is necessary to avoid unmarshalling issues when sending the receiver |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 520 | // across processes. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 521 | Parcel parcel = Parcel.obtain(); |
| 522 | rr.writeToParcel(parcel,0); |
| 523 | parcel.setDataPosition(0); |
| 524 | ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel); |
| 525 | parcel.recycle(); |
| 526 | return receiverForSending; |
| 527 | } |
| 528 | |
| 529 | private void scheduleProvisioningRechecks(int type) { |
| 530 | Intent intent = new Intent(); |
| 531 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 532 | intent.putExtra(ConnectivityManager.EXTRA_SET_ALARM, true); |
| 533 | intent.setComponent(TETHER_SERVICE); |
| 534 | final long ident = Binder.clearCallingIdentity(); |
| 535 | try { |
| 536 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 537 | } finally { |
| 538 | Binder.restoreCallingIdentity(ident); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | private void runSilentTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
| 543 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 544 | sendSilentTetherProvisionIntent(type, proxyReceiver); |
| 545 | } |
| 546 | |
| 547 | private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 548 | Intent intent = new Intent(); |
| 549 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 550 | intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); |
| 551 | intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); |
| 552 | intent.setComponent(TETHER_SERVICE); |
| 553 | final long ident = Binder.clearCallingIdentity(); |
| 554 | try { |
| 555 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 556 | } finally { |
| 557 | Binder.restoreCallingIdentity(ident); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | private void cancelTetherProvisioningRechecks(int type) { |
| 562 | if (getConnectivityManager().isTetheringSupported()) { |
| 563 | Intent intent = new Intent(); |
| 564 | intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type); |
| 565 | intent.setComponent(TETHER_SERVICE); |
| 566 | final long ident = Binder.clearCallingIdentity(); |
| 567 | try { |
| 568 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 569 | } finally { |
| 570 | Binder.restoreCallingIdentity(ident); |
| 571 | } |
| 572 | } |
| 573 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 574 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 575 | // Used by the SIM card change observation code. |
| 576 | // TODO: De-duplicate with above code, where possible. |
| 577 | private void startProvisionIntent(int tetherType) { |
| 578 | final Intent startProvIntent = new Intent(); |
| 579 | startProvIntent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, tetherType); |
| 580 | startProvIntent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); |
| 581 | startProvIntent.setComponent(TETHER_SERVICE); |
| 582 | mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT); |
| 583 | } |
| 584 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 585 | public int tether(String iface) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 586 | return tether(iface, IControlsTethering.STATE_TETHERED); |
| 587 | } |
| 588 | |
| 589 | private int tether(String iface, int requestedState) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 590 | if (DBG) Log.d(TAG, "Tethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 591 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 592 | TetherState tetherState = mTetherStates.get(iface); |
| 593 | if (tetherState == null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 594 | Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 595 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 596 | } |
| 597 | // Ignore the error status of the interface. If the interface is available, |
| 598 | // the errors are referring to past tethering attempts anyway. |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 599 | if (tetherState.lastState != IControlsTethering.STATE_AVAILABLE) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 600 | Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 601 | return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 602 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 603 | // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's |
| 604 | // queue but not yet processed, this will be a no-op and it will not |
| 605 | // return an error. |
| 606 | // |
| 607 | // TODO: reexamine the threading and messaging model. |
| 608 | tetherState.stateMachine.sendMessage( |
| 609 | TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, requestedState); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 610 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 611 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 614 | public int untether(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 615 | if (DBG) Log.d(TAG, "Untethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 616 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 617 | TetherState tetherState = mTetherStates.get(iface); |
| 618 | if (tetherState == null) { |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 619 | Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); |
| 620 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 621 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 622 | if (!tetherState.isCurrentlyServing()) { |
| 623 | Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 624 | return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 625 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 626 | tetherState.stateMachine.sendMessage( |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 627 | TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 628 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 629 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 632 | public void untetherAll() { |
Christopher Wiley | fa6a486 | 2016-09-19 08:58:06 -0700 | [diff] [blame] | 633 | stopTethering(ConnectivityManager.TETHERING_WIFI); |
| 634 | stopTethering(ConnectivityManager.TETHERING_USB); |
| 635 | stopTethering(ConnectivityManager.TETHERING_BLUETOOTH); |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 638 | public int getLastTetherError(String iface) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 639 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 640 | TetherState tetherState = mTetherStates.get(iface); |
| 641 | if (tetherState == null) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 642 | Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + |
| 643 | ", ignoring"); |
| 644 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 645 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 646 | return tetherState.lastError; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 647 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 650 | // TODO: Figure out how to update for local hotspot mode interfaces. |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 651 | private void sendTetherStateChangedBroadcast() { |
Sreeram Ramachandran | 62a6131 | 2014-07-24 16:05:17 -0700 | [diff] [blame] | 652 | if (!getConnectivityManager().isTetheringSupported()) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 653 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 654 | final ArrayList<String> availableList = new ArrayList<>(); |
| 655 | final ArrayList<String> tetherList = new ArrayList<>(); |
| 656 | final ArrayList<String> localOnlyList = new ArrayList<>(); |
| 657 | final ArrayList<String> erroredList = new ArrayList<>(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 658 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 659 | boolean wifiTethered = false; |
| 660 | boolean usbTethered = false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 661 | boolean bluetoothTethered = false; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 662 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 663 | final TetheringConfiguration cfg = mConfig; |
| 664 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 665 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 666 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 667 | TetherState tetherState = mTetherStates.valueAt(i); |
| 668 | String iface = mTetherStates.keyAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 669 | if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 670 | erroredList.add(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 671 | } else if (tetherState.lastState == IControlsTethering.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 672 | availableList.add(iface); |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 673 | } else if (tetherState.lastState == IControlsTethering.STATE_LOCAL_ONLY) { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 674 | localOnlyList.add(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 675 | } else if (tetherState.lastState == IControlsTethering.STATE_TETHERED) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 676 | if (cfg.isUsb(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 677 | usbTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 678 | } else if (cfg.isWifi(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 679 | wifiTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 680 | } else if (cfg.isBluetooth(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 681 | bluetoothTethered = true; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 682 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 683 | tetherList.add(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 687 | final Intent bcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); |
| 688 | bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
Robert Greenwalt | 98c79e5 | 2011-07-28 11:51:11 -0700 | [diff] [blame] | 689 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 690 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, availableList); |
| 691 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); |
| 692 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, tetherList); |
| 693 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER, erroredList); |
| 694 | mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL); |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 695 | if (DBG) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 696 | Log.d(TAG, String.format( |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 697 | "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]", |
| 698 | "avail", TextUtils.join(",", availableList), |
| 699 | "local_only", TextUtils.join(",", localOnlyList), |
| 700 | "tether", TextUtils.join(",", tetherList), |
| 701 | "error", TextUtils.join(",", erroredList))); |
Robert Greenwalt | 924cc94 | 2010-06-28 10:26:19 -0700 | [diff] [blame] | 702 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 703 | |
| 704 | if (usbTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 705 | if (wifiTethered || bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 706 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 707 | } else { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 708 | showTetheredNotification(SystemMessage.NOTE_TETHER_USB); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 709 | } |
| 710 | } else if (wifiTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 711 | if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 712 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 713 | } else { |
Vinit Deshpande | 2576a80 | 2014-11-18 13:56:15 -0800 | [diff] [blame] | 714 | /* We now have a status bar icon for WifiTethering, so drop the notification */ |
| 715 | clearTetheredNotification(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 716 | } |
| 717 | } else if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 718 | showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 719 | } else { |
| 720 | clearTetheredNotification(); |
| 721 | } |
| 722 | } |
| 723 | |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 724 | private void showTetheredNotification(int id) { |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 725 | showTetheredNotification(id, true); |
| 726 | } |
| 727 | |
| 728 | @VisibleForTesting |
| 729 | protected void showTetheredNotification(int id, boolean tetheringOn) { |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 730 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 731 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 732 | if (notificationManager == null) { |
| 733 | return; |
| 734 | } |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 735 | int icon = 0; |
| 736 | switch(id) { |
| 737 | case SystemMessage.NOTE_TETHER_USB: |
| 738 | icon = com.android.internal.R.drawable.stat_sys_tether_usb; |
| 739 | break; |
| 740 | case SystemMessage.NOTE_TETHER_BLUETOOTH: |
| 741 | icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth; |
| 742 | break; |
| 743 | case SystemMessage.NOTE_TETHER_GENERAL: |
| 744 | default: |
| 745 | icon = com.android.internal.R.drawable.stat_sys_tether_general; |
| 746 | break; |
| 747 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 748 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 749 | if (mLastNotificationId != 0) { |
| 750 | if (mLastNotificationId == icon) { |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 751 | return; |
| 752 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 753 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Robert Greenwalt | 3cab6b0 | 2012-10-04 16:44:26 -0700 | [diff] [blame] | 754 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 755 | mLastNotificationId = 0; |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 758 | Intent intent = new Intent(); |
| 759 | intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); |
| 760 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); |
| 761 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 762 | PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0, |
| 763 | null, UserHandle.CURRENT); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 764 | |
| 765 | Resources r = Resources.getSystem(); |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 766 | final CharSequence title; |
| 767 | final CharSequence message; |
| 768 | |
| 769 | if (tetheringOn) { |
| 770 | title = r.getText(com.android.internal.R.string.tethered_notification_title); |
| 771 | message = r.getText(com.android.internal.R.string.tethered_notification_message); |
| 772 | } else { |
| 773 | title = r.getText(com.android.internal.R.string.disable_tether_notification_title); |
| 774 | message = r.getText(com.android.internal.R.string.disable_tether_notification_message); |
| 775 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 776 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 777 | if (mTetheredNotificationBuilder == null) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 778 | mTetheredNotificationBuilder = |
| 779 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 780 | mTetheredNotificationBuilder.setWhen(0) |
| 781 | .setOngoing(true) |
| 782 | .setColor(mContext.getColor( |
| 783 | com.android.internal.R.color.system_notification_accent_color)) |
| 784 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 785 | .setCategory(Notification.CATEGORY_STATUS); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 786 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 787 | mTetheredNotificationBuilder.setSmallIcon(icon) |
| 788 | .setContentTitle(title) |
| 789 | .setContentText(message) |
| 790 | .setContentIntent(pi); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 791 | mLastNotificationId = id; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 792 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 793 | notificationManager.notifyAsUser(null, mLastNotificationId, |
zhouzhijie | 130d459 | 2017-05-18 10:02:59 +0800 | [diff] [blame] | 794 | mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 797 | @VisibleForTesting |
| 798 | protected void clearTetheredNotification() { |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 799 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 800 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 801 | if (notificationManager != null && mLastNotificationId != 0) { |
| 802 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 803 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 804 | mLastNotificationId = 0; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 805 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 808 | private class StateReceiver extends BroadcastReceiver { |
Nick Kralevich | 70c117a | 2014-05-27 15:30:02 -0700 | [diff] [blame] | 809 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 810 | public void onReceive(Context content, Intent intent) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 811 | final String action = intent.getAction(); |
| 812 | if (action == null) return; |
| 813 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 814 | if (action.equals(UsbManager.ACTION_USB_STATE)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 815 | handleUsbAction(intent); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 816 | } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 817 | handleConnectivityAction(intent); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 818 | } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 819 | handleWifiApAction(intent); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 820 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 821 | updateConfiguration(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 824 | |
| 825 | private void handleConnectivityAction(Intent intent) { |
| 826 | final NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra( |
| 827 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 828 | if (networkInfo == null || |
| 829 | networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) { |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString()); |
| 834 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); |
| 835 | } |
| 836 | |
| 837 | private void handleUsbAction(Intent intent) { |
| 838 | final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 839 | final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 840 | final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 841 | |
| 842 | mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s", |
| 843 | usbConnected, usbConfigured, rndisEnabled)); |
| 844 | |
| 845 | // There are three types of ACTION_USB_STATE: |
| 846 | // |
| 847 | // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0) |
| 848 | // Meaning: USB connection has ended either because of |
| 849 | // software reset or hard unplug. |
| 850 | // |
| 851 | // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0) |
| 852 | // Meaning: the first stage of USB protocol handshake has |
| 853 | // occurred but it is not complete. |
| 854 | // |
| 855 | // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1) |
| 856 | // Meaning: the USB handshake is completely done and all the |
| 857 | // functions are ready to use. |
| 858 | // |
| 859 | // For more explanation, see b/62552150 . |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 860 | synchronized (Tethering.this.mPublicSync) { |
Erik Kline | 3897dc1 | 2017-08-07 18:11:22 -0700 | [diff] [blame] | 861 | // Always record the state of RNDIS. |
Erik Kline | 1290568 | 2017-08-08 20:53:46 -0700 | [diff] [blame] | 862 | // TODO: consider: |
| 863 | // final boolean disconnected = !usbConnected; |
| 864 | // if (disconnected) { |
| 865 | // mRndisEnabled = false; |
| 866 | // mUsbTetherRequested = false; |
| 867 | // return; |
| 868 | // } |
| 869 | // final boolean configured = usbConnected && usbConfigured; |
| 870 | // mRndisEnabled = configured ? rndisEnabled : false; |
| 871 | // if (!configured) return; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 872 | mRndisEnabled = rndisEnabled; |
Erik Kline | 3897dc1 | 2017-08-07 18:11:22 -0700 | [diff] [blame] | 873 | |
| 874 | if (usbConnected && !usbConfigured) { |
| 875 | // Nothing to do here (only CONNECTED, not yet CONFIGURED). |
| 876 | return; |
| 877 | } |
| 878 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 879 | // start tethering if we have a request pending |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 880 | if (usbConfigured && mRndisEnabled && mUsbTetherRequested) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 881 | tetherMatchingInterfaces( |
| 882 | IControlsTethering.STATE_TETHERED, |
| 883 | ConnectivityManager.TETHERING_USB); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 884 | } |
Erik Kline | 3897dc1 | 2017-08-07 18:11:22 -0700 | [diff] [blame] | 885 | |
| 886 | // TODO: Figure out how to remove the need for this variable. |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 887 | mUsbTetherRequested = false; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | private void handleWifiApAction(Intent intent) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 892 | final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); |
| 893 | final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME); |
| 894 | final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED); |
| 895 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 896 | synchronized (Tethering.this.mPublicSync) { |
| 897 | switch (curState) { |
| 898 | case WifiManager.WIFI_AP_STATE_ENABLING: |
| 899 | // We can see this state on the way to both enabled and failure states. |
| 900 | break; |
| 901 | case WifiManager.WIFI_AP_STATE_ENABLED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 902 | enableWifiIpServingLocked(ifname, ipmode); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 903 | break; |
| 904 | case WifiManager.WIFI_AP_STATE_DISABLED: |
| 905 | case WifiManager.WIFI_AP_STATE_DISABLING: |
| 906 | case WifiManager.WIFI_AP_STATE_FAILED: |
| 907 | default: |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 908 | disableWifiIpServingLocked(ifname, curState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 909 | break; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Alexandru-Andrei Rotaru | fa6d5c5 | 2017-07-18 16:49:22 +0100 | [diff] [blame] | 915 | @VisibleForTesting |
| 916 | protected static class TetheringUserRestrictionListener implements UserRestrictionsListener { |
| 917 | private final Tethering mWrapper; |
| 918 | |
| 919 | public TetheringUserRestrictionListener(Tethering wrapper) { |
| 920 | mWrapper = wrapper; |
| 921 | } |
| 922 | |
| 923 | public void onUserRestrictionsChanged(int userId, |
| 924 | Bundle newRestrictions, |
| 925 | Bundle prevRestrictions) { |
| 926 | final boolean newlyDisallowed = |
| 927 | newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING); |
| 928 | final boolean previouslyDisallowed = |
| 929 | prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_TETHERING); |
| 930 | final boolean tetheringDisallowedChanged = (newlyDisallowed != previouslyDisallowed); |
| 931 | |
| 932 | if (!tetheringDisallowedChanged) { |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | mWrapper.clearTetheredNotification(); |
| 937 | final boolean isTetheringActiveOnDevice = (mWrapper.getTetheredIfaces().length != 0); |
| 938 | |
| 939 | if (newlyDisallowed && isTetheringActiveOnDevice) { |
| 940 | mWrapper.showTetheredNotification( |
| 941 | com.android.internal.R.drawable.stat_sys_tether_general, false); |
| 942 | mWrapper.untetherAll(); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 947 | private void disableWifiIpServingLocked(String ifname, int apState) { |
| 948 | mLog.log("Canceling WiFi tethering request - AP_STATE=" + apState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 949 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 950 | // Regardless of whether we requested this transition, the AP has gone |
| 951 | // down. Don't try to tether again unless we're requested to do so. |
| 952 | // TODO: Remove this altogether, once Wi-Fi reliably gives us an |
| 953 | // interface name with every broadcast. |
| 954 | mWifiTetherRequested = false; |
| 955 | |
| 956 | if (!TextUtils.isEmpty(ifname)) { |
| 957 | final TetherState ts = mTetherStates.get(ifname); |
| 958 | if (ts != null) { |
| 959 | ts.stateMachine.unwanted(); |
| 960 | return; |
| 961 | } |
| 962 | } |
| 963 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 964 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 965 | TetherInterfaceStateMachine tism = mTetherStates.valueAt(i).stateMachine; |
| 966 | if (tism.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 967 | tism.unwanted(); |
| 968 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 969 | } |
| 970 | } |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 971 | |
| 972 | mLog.log("Error disabling Wi-Fi IP serving; " + |
| 973 | (TextUtils.isEmpty(ifname) ? "no interface name specified" |
| 974 | : "specified interface: " + ifname)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | private void enableWifiIpServingLocked(String ifname, int wifiIpMode) { |
| 978 | // Map wifiIpMode values to IControlsTethering serving states, inferring |
| 979 | // from mWifiTetherRequested as a final "best guess". |
| 980 | final int ipServingMode; |
| 981 | switch (wifiIpMode) { |
| 982 | case IFACE_IP_MODE_TETHERED: |
| 983 | ipServingMode = IControlsTethering.STATE_TETHERED; |
| 984 | break; |
| 985 | case IFACE_IP_MODE_LOCAL_ONLY: |
| 986 | ipServingMode = IControlsTethering.STATE_LOCAL_ONLY; |
| 987 | break; |
| 988 | default: |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 989 | mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode); |
| 990 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (!TextUtils.isEmpty(ifname)) { |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 994 | maybeTrackNewInterfaceLocked(ifname, ConnectivityManager.TETHERING_WIFI); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 995 | changeInterfaceState(ifname, ipServingMode); |
| 996 | } else { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 997 | mLog.e(String.format( |
| 998 | "Cannot enable IP serving in mode %s on missing interface name", |
| 999 | ipServingMode)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1003 | // TODO: Consider renaming to something more accurate in its description. |
| 1004 | // This method: |
| 1005 | // - allows requesting either tethering or local hotspot serving states |
| 1006 | // - handles both enabling and disabling serving states |
| 1007 | // - only tethers the first matching interface in listInterfaces() |
| 1008 | // order of a given type |
| 1009 | private void tetherMatchingInterfaces(int requestedState, int interfaceType) { |
| 1010 | if (VDBG) { |
| 1011 | Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")"); |
| 1012 | } |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1013 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1014 | String[] ifaces = null; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1015 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1016 | ifaces = mNMService.listInterfaces(); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1017 | } catch (Exception e) { |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 1018 | Log.e(TAG, "Error listing Interfaces", e); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1019 | return; |
| 1020 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1021 | String chosenIface = null; |
| 1022 | if (ifaces != null) { |
| 1023 | for (String iface : ifaces) { |
| 1024 | if (ifaceNameToType(iface) == interfaceType) { |
| 1025 | chosenIface = iface; |
| 1026 | break; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1030 | if (chosenIface == null) { |
| 1031 | Log.e(TAG, "could not find iface of type " + interfaceType); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1035 | changeInterfaceState(chosenIface, requestedState); |
| 1036 | } |
| 1037 | |
| 1038 | private void changeInterfaceState(String ifname, int requestedState) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1039 | final int result; |
| 1040 | switch (requestedState) { |
| 1041 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1042 | case IControlsTethering.STATE_AVAILABLE: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1043 | result = untether(ifname); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1044 | break; |
| 1045 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 1046 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1047 | result = tether(ifname, requestedState); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1048 | break; |
| 1049 | default: |
| 1050 | Log.wtf(TAG, "Unknown interface state: " + requestedState); |
| 1051 | return; |
| 1052 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1053 | if (result != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1054 | Log.e(TAG, "unable start or stop tethering on iface " + ifname); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 1055 | return; |
| 1056 | } |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1059 | public TetheringConfiguration getTetheringConfiguration() { |
| 1060 | return mConfig; |
| 1061 | } |
| 1062 | |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 1063 | public boolean hasTetherableConfiguration() { |
| 1064 | final TetheringConfiguration cfg = mConfig; |
| 1065 | final boolean hasDownstreamConfiguration = |
| 1066 | (cfg.tetherableUsbRegexs.length != 0) || |
| 1067 | (cfg.tetherableWifiRegexs.length != 0) || |
| 1068 | (cfg.tetherableBluetoothRegexs.length != 0); |
| 1069 | final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty(); |
| 1070 | |
| 1071 | return hasDownstreamConfiguration && hasUpstreamConfiguration; |
| 1072 | } |
| 1073 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1074 | // TODO - update callers to use getTetheringConfiguration(), |
| 1075 | // which has only final members. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1076 | public String[] getTetherableUsbRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1077 | return copy(mConfig.tetherableUsbRegexs); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1080 | public String[] getTetherableWifiRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1081 | return copy(mConfig.tetherableWifiRegexs); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1084 | public String[] getTetherableBluetoothRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1085 | return copy(mConfig.tetherableBluetoothRegexs); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1088 | public int setUsbTethering(boolean enable) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1089 | if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame] | 1090 | UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1091 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1092 | synchronized (mPublicSync) { |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1093 | if (enable) { |
| 1094 | if (mRndisEnabled) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1095 | final long ident = Binder.clearCallingIdentity(); |
| 1096 | try { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1097 | tetherMatchingInterfaces(IControlsTethering.STATE_TETHERED, |
| 1098 | ConnectivityManager.TETHERING_USB); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1099 | } finally { |
| 1100 | Binder.restoreCallingIdentity(ident); |
| 1101 | } |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1102 | } else { |
| 1103 | mUsbTetherRequested = true; |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 1104 | usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } else { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1107 | final long ident = Binder.clearCallingIdentity(); |
| 1108 | try { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1109 | tetherMatchingInterfaces(IControlsTethering.STATE_AVAILABLE, |
| 1110 | ConnectivityManager.TETHERING_USB); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1111 | } finally { |
| 1112 | Binder.restoreCallingIdentity(ident); |
| 1113 | } |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1114 | if (mRndisEnabled) { |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 1115 | usbManager.setCurrentFunction(null, false); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1116 | } |
| 1117 | mUsbTetherRequested = false; |
| 1118 | } |
| 1119 | } |
| 1120 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 1121 | } |
| 1122 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1123 | // TODO review API - figure out how to delete these entirely. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1124 | public String[] getTetheredIfaces() { |
| 1125 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1126 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1127 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1128 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1129 | if (tetherState.lastState == IControlsTethering.STATE_TETHERED) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1130 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1131 | } |
| 1132 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1133 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1134 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | public String[] getTetherableIfaces() { |
| 1138 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1139 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1140 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1141 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1142 | if (tetherState.lastState == IControlsTethering.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1143 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1147 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1150 | public String[] getTetheredDhcpRanges() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1151 | return mConfig.dhcpRanges; |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1154 | public String[] getErroredIfaces() { |
| 1155 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1156 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1157 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1158 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1159 | if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1160 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1164 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1167 | private void logMessage(State state, int what) { |
| 1168 | mLog.log(state.getName() + " got " + sMagicDecoderRing.get(what, Integer.toString(what))); |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1169 | } |
| 1170 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1171 | private boolean upstreamWanted() { |
| 1172 | if (!mForwardedDownstreams.isEmpty()) return true; |
| 1173 | |
| 1174 | synchronized (mPublicSync) { |
| 1175 | return mUsbTetherRequested || mWifiTetherRequested; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1179 | // Needed because the canonical source of upstream truth is just the |
| 1180 | // upstream interface name, |mCurrentUpstreamIface|. This is ripe for |
| 1181 | // future simplification, once the upstream Network is canonical. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1182 | private boolean pertainsToCurrentUpstream(NetworkState ns) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1183 | if (ns != null && ns.linkProperties != null && mCurrentUpstreamIface != null) { |
| 1184 | for (String ifname : ns.linkProperties.getAllInterfaceNames()) { |
| 1185 | if (mCurrentUpstreamIface.equals(ifname)) { |
| 1186 | return true; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1187 | } |
| 1188 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1189 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1190 | return false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1191 | } |
| 1192 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1193 | private void reevaluateSimCardProvisioning() { |
| 1194 | if (!hasMobileHotspotProvisionApp()) return; |
| 1195 | |
| 1196 | ArrayList<Integer> tethered = new ArrayList<>(); |
| 1197 | synchronized (mPublicSync) { |
| 1198 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1199 | TetherState tetherState = mTetherStates.valueAt(i); |
| 1200 | if (tetherState.lastState != IControlsTethering.STATE_TETHERED) { |
| 1201 | continue; // Skip interfaces that aren't tethered. |
| 1202 | } |
| 1203 | String iface = mTetherStates.keyAt(i); |
| 1204 | int interfaceType = ifaceNameToType(iface); |
| 1205 | if (interfaceType != ConnectivityManager.TETHERING_INVALID) { |
| 1206 | tethered.add(interfaceType); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | for (int tetherType : tethered) { |
| 1212 | startProvisionIntent(tetherType); |
| 1213 | } |
| 1214 | } |
| 1215 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1216 | class TetherMasterSM extends StateMachine { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1217 | private static final int BASE_MASTER = Protocol.BASE_TETHERING; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1218 | // an interface SM has requested Tethering/Local Hotspot |
| 1219 | static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1; |
| 1220 | // an interface SM has unrequested Tethering/Local Hotspot |
| 1221 | static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1222 | // upstream connection change - do the right thing |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1223 | static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1224 | // we don't have a valid upstream conn, check again after a delay |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1225 | static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1226 | // Events from NetworkCallbacks that we process on the master state |
| 1227 | // machine thread on behalf of the UpstreamNetworkMonitor. |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1228 | static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1229 | // we treated the error and want now to clear it |
| 1230 | static final int CMD_CLEAR_ERROR = BASE_MASTER + 6; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1231 | static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1232 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1233 | private final State mInitialState; |
| 1234 | private final State mTetherModeAliveState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1235 | |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1236 | private final State mSetIpForwardingEnabledErrorState; |
| 1237 | private final State mSetIpForwardingDisabledErrorState; |
| 1238 | private final State mStartTetheringErrorState; |
| 1239 | private final State mStopTetheringErrorState; |
| 1240 | private final State mSetDnsForwardersErrorState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1241 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1242 | // This list is a little subtle. It contains all the interfaces that currently are |
| 1243 | // requesting tethering, regardless of whether these interfaces are still members of |
| 1244 | // mTetherStates. This allows us to maintain the following predicates: |
| 1245 | // |
| 1246 | // 1) mTetherStates contains the set of all currently existing, tetherable, link state up |
| 1247 | // interfaces. |
| 1248 | // 2) mNotifyList contains all state machines that may have outstanding tethering state |
| 1249 | // that needs to be torn down. |
| 1250 | // |
| 1251 | // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList |
| 1252 | // so that the garbage collector does not clean up the state machine before it has a chance |
| 1253 | // to tear itself down. |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1254 | private final ArrayList<TetherInterfaceStateMachine> mNotifyList; |
| 1255 | private final IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1256 | private final OffloadWrapper mOffload; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1257 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1258 | private static final int UPSTREAM_SETTLE_TIME_MS = 10000; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1259 | |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 1260 | TetherMasterSM(String name, Looper looper) { |
| 1261 | super(name, looper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1262 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1263 | mInitialState = new InitialState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1264 | mTetherModeAliveState = new TetherModeAliveState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1265 | mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1266 | mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1267 | mStartTetheringErrorState = new StartTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1268 | mStopTetheringErrorState = new StopTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1269 | mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1270 | |
| 1271 | addState(mInitialState); |
| 1272 | addState(mTetherModeAliveState); |
| 1273 | addState(mSetIpForwardingEnabledErrorState); |
| 1274 | addState(mSetIpForwardingDisabledErrorState); |
| 1275 | addState(mStartTetheringErrorState); |
| 1276 | addState(mStopTetheringErrorState); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1277 | addState(mSetDnsForwardersErrorState); |
| 1278 | |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1279 | mNotifyList = new ArrayList<>(); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1280 | mIPv6TetheringCoordinator = new IPv6TetheringCoordinator(mNotifyList, mLog); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1281 | mOffload = new OffloadWrapper(); |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1282 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1283 | setInitialState(mInitialState); |
| 1284 | } |
| 1285 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1286 | class InitialState extends State { |
| 1287 | @Override |
| 1288 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1289 | logMessage(this, message.what); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1290 | switch (message.what) { |
| 1291 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1292 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1293 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
| 1294 | handleInterfaceServingStateActive(message.arg1, who); |
| 1295 | transitionTo(mTetherModeAliveState); |
| 1296 | break; |
| 1297 | case EVENT_IFACE_SERVING_STATE_INACTIVE: |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1298 | who = (TetherInterfaceStateMachine) message.obj; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1299 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
| 1300 | handleInterfaceServingStateInactive(who); |
| 1301 | break; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1302 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: |
| 1303 | // Silently ignore these for now. |
| 1304 | break; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1305 | default: |
| 1306 | return NOT_HANDLED; |
| 1307 | } |
| 1308 | return HANDLED; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1312 | protected boolean turnOnMasterTetherSettings() { |
| 1313 | final TetheringConfiguration cfg = mConfig; |
| 1314 | try { |
| 1315 | mNMService.setIpForwardingEnabled(true); |
| 1316 | } catch (Exception e) { |
| 1317 | mLog.e(e); |
| 1318 | transitionTo(mSetIpForwardingEnabledErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1321 | // TODO: Randomize DHCPv4 ranges, especially in hotspot mode. |
| 1322 | try { |
| 1323 | // TODO: Find a more accurate method name (startDHCPv4()?). |
| 1324 | mNMService.startTethering(cfg.dhcpRanges); |
| 1325 | } catch (Exception e) { |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1326 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1327 | mNMService.stopTethering(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1328 | mNMService.startTethering(cfg.dhcpRanges); |
| 1329 | } catch (Exception ee) { |
| 1330 | mLog.e(ee); |
| 1331 | transitionTo(mStartTetheringErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1332 | return false; |
| 1333 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1334 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1335 | mLog.log("SET master tether settings: ON"); |
| 1336 | return true; |
| 1337 | } |
Robert Greenwalt | 1039872 | 2010-12-17 15:20:36 -0800 | [diff] [blame] | 1338 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1339 | protected boolean turnOffMasterTetherSettings() { |
| 1340 | try { |
| 1341 | mNMService.stopTethering(); |
| 1342 | } catch (Exception e) { |
| 1343 | mLog.e(e); |
| 1344 | transitionTo(mStopTetheringErrorState); |
| 1345 | return false; |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1346 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1347 | try { |
| 1348 | mNMService.setIpForwardingEnabled(false); |
| 1349 | } catch (Exception e) { |
| 1350 | mLog.e(e); |
| 1351 | transitionTo(mSetIpForwardingDisabledErrorState); |
| 1352 | return false; |
| 1353 | } |
| 1354 | transitionTo(mInitialState); |
| 1355 | mLog.log("SET master tether settings: OFF"); |
| 1356 | return true; |
| 1357 | } |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1358 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1359 | protected void chooseUpstreamType(boolean tryCell) { |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 1360 | // We rebuild configuration on ACTION_CONFIGURATION_CHANGED, but we |
| 1361 | // do not currently know how to watch for changes in DUN settings. |
| 1362 | maybeUpdateConfiguration(); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1363 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1364 | final NetworkState ns = mUpstreamNetworkMonitor.selectPreferredUpstreamType( |
| 1365 | mConfig.preferredUpstreamIfaceTypes); |
| 1366 | if (ns == null) { |
| 1367 | if (tryCell) { |
| 1368 | mUpstreamNetworkMonitor.registerMobileNetworkRequest(); |
| 1369 | // We think mobile should be coming up; don't set a retry. |
| 1370 | } else { |
| 1371 | sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1372 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1373 | } |
| 1374 | setUpstreamNetwork(ns); |
| 1375 | } |
| 1376 | |
| 1377 | protected void setUpstreamNetwork(NetworkState ns) { |
| 1378 | String iface = null; |
| 1379 | if (ns != null && ns.linkProperties != null) { |
| 1380 | // Find the interface with the default IPv4 route. It may be the |
| 1381 | // interface described by linkProperties, or one of the interfaces |
| 1382 | // stacked on top of it. |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1383 | mLog.i("Finding IPv4 upstream interface on: " + ns.linkProperties); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1384 | RouteInfo ipv4Default = RouteInfo.selectBestRoute( |
| 1385 | ns.linkProperties.getAllRoutes(), Inet4Address.ANY); |
| 1386 | if (ipv4Default != null) { |
| 1387 | iface = ipv4Default.getInterface(); |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1388 | mLog.i("Found interface " + ipv4Default.getInterface()); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1389 | } else { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1390 | mLog.i("No IPv4 upstream interface, giving up."); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1391 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1392 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 1393 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1394 | if (iface != null) { |
| 1395 | setDnsForwarders(ns.network, ns.linkProperties); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1396 | } |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1397 | notifyDownstreamsOfNewUpstreamIface(iface); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1398 | if (ns != null && pertainsToCurrentUpstream(ns)) { |
| 1399 | // If we already have NetworkState for this network examine |
| 1400 | // it immediately, because there likely will be no second |
| 1401 | // EVENT_ON_AVAILABLE (it was already received). |
| 1402 | handleNewUpstreamNetworkState(ns); |
| 1403 | } else if (mCurrentUpstreamIface == null) { |
| 1404 | // There are no available upstream networks, or none that |
| 1405 | // have an IPv4 default route (current metric for success). |
| 1406 | handleNewUpstreamNetworkState(null); |
| 1407 | } |
| 1408 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1409 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1410 | protected void setDnsForwarders(final Network network, final LinkProperties lp) { |
| 1411 | // TODO: Set v4 and/or v6 DNS per available connectivity. |
| 1412 | String[] dnsServers = mConfig.defaultIPv4DNS; |
| 1413 | final Collection<InetAddress> dnses = lp.getDnsServers(); |
| 1414 | // TODO: Properly support the absence of DNS servers. |
| 1415 | if (dnses != null && !dnses.isEmpty()) { |
| 1416 | // TODO: remove this invocation of NetworkUtils.makeStrings(). |
| 1417 | dnsServers = NetworkUtils.makeStrings(dnses); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1418 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1419 | try { |
| 1420 | mNMService.setDnsForwarders(network, dnsServers); |
| 1421 | mLog.log(String.format( |
| 1422 | "SET DNS forwarders: network=%s dnsServers=%s", |
| 1423 | network, Arrays.toString(dnsServers))); |
| 1424 | } catch (Exception e) { |
| 1425 | // TODO: Investigate how this can fail and what exactly |
| 1426 | // happens if/when such failures occur. |
| 1427 | mLog.e("setting DNS forwarders failed, " + e); |
| 1428 | transitionTo(mSetDnsForwardersErrorState); |
| 1429 | } |
| 1430 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1431 | |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1432 | protected void notifyDownstreamsOfNewUpstreamIface(String ifaceName) { |
| 1433 | mLog.log("Notifying downstreams of upstream=" + ifaceName); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1434 | mCurrentUpstreamIface = ifaceName; |
| 1435 | for (TetherInterfaceStateMachine sm : mNotifyList) { |
| 1436 | sm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
| 1437 | ifaceName); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1438 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1439 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1440 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1441 | protected void handleNewUpstreamNetworkState(NetworkState ns) { |
| 1442 | mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1443 | mOffload.updateUpstreamNetworkState(ns); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1444 | } |
| 1445 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1446 | private void handleInterfaceServingStateActive(int mode, TetherInterfaceStateMachine who) { |
| 1447 | if (mNotifyList.indexOf(who) < 0) { |
| 1448 | mNotifyList.add(who); |
| 1449 | mIPv6TetheringCoordinator.addActiveDownstream(who, mode); |
| 1450 | } |
| 1451 | |
| 1452 | if (mode == IControlsTethering.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1453 | // No need to notify OffloadController just yet as there are no |
| 1454 | // "offload-able" prefixes to pass along. This will handled |
| 1455 | // when the TISM informs Tethering of its LinkProperties. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1456 | mForwardedDownstreams.add(who); |
| 1457 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1458 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1459 | mForwardedDownstreams.remove(who); |
| 1460 | } |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1461 | |
| 1462 | // If this is a Wi-Fi interface, notify WifiManager of the active serving state. |
| 1463 | if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
| 1464 | final WifiManager mgr = getWifiManager(); |
| 1465 | final String iface = who.interfaceName(); |
| 1466 | switch (mode) { |
| 1467 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1468 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1469 | break; |
| 1470 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1471 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1472 | break; |
| 1473 | default: |
| 1474 | Log.wtf(TAG, "Unknown active serving mode: " + mode); |
| 1475 | break; |
| 1476 | } |
| 1477 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | private void handleInterfaceServingStateInactive(TetherInterfaceStateMachine who) { |
| 1481 | mNotifyList.remove(who); |
| 1482 | mIPv6TetheringCoordinator.removeActiveDownstream(who); |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1483 | mOffload.excludeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1484 | mForwardedDownstreams.remove(who); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1485 | |
| 1486 | // If this is a Wi-Fi interface, tell WifiManager of any errors. |
| 1487 | if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
| 1488 | if (who.lastError() != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 1489 | getWifiManager().updateInterfaceIpState( |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1490 | who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1491 | } |
| 1492 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1493 | } |
| 1494 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1495 | private void handleUpstreamNetworkMonitorCallback(int arg1, Object o) { |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1496 | if (arg1 == UpstreamNetworkMonitor.NOTIFY_LOCAL_PREFIXES) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1497 | mOffload.sendOffloadExemptPrefixes((Set<IpPrefix>) o); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | final NetworkState ns = (NetworkState) o; |
| 1502 | |
| 1503 | if (ns == null || !pertainsToCurrentUpstream(ns)) { |
| 1504 | // TODO: In future, this is where upstream evaluation and selection |
| 1505 | // could be handled for notifications which include sufficient data. |
| 1506 | // For example, after CONNECTIVITY_ACTION listening is removed, here |
| 1507 | // is where we could observe a Wi-Fi network becoming available and |
| 1508 | // passing validation. |
| 1509 | if (mCurrentUpstreamIface == null) { |
| 1510 | // If we have no upstream interface, try to run through upstream |
| 1511 | // selection again. If, for example, IPv4 connectivity has shown up |
| 1512 | // after IPv6 (e.g., 464xlat became available) we want the chance to |
| 1513 | // notice and act accordingly. |
| 1514 | chooseUpstreamType(false); |
| 1515 | } |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | switch (arg1) { |
| 1520 | case UpstreamNetworkMonitor.EVENT_ON_AVAILABLE: |
| 1521 | // The default network changed, or DUN connected |
| 1522 | // before this callback was processed. Updates |
| 1523 | // for the current NetworkCapabilities and |
| 1524 | // LinkProperties have been requested (default |
| 1525 | // request) or are being sent shortly (DUN). Do |
| 1526 | // nothing until they arrive; if no updates |
| 1527 | // arrive there's nothing to do. |
| 1528 | break; |
| 1529 | case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES: |
| 1530 | handleNewUpstreamNetworkState(ns); |
| 1531 | break; |
| 1532 | case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES: |
| 1533 | setDnsForwarders(ns.network, ns.linkProperties); |
| 1534 | handleNewUpstreamNetworkState(ns); |
| 1535 | break; |
| 1536 | case UpstreamNetworkMonitor.EVENT_ON_LOST: |
| 1537 | // TODO: Re-evaluate possible upstreams. Currently upstream |
| 1538 | // reevaluation is triggered via received CONNECTIVITY_ACTION |
| 1539 | // broadcasts that result in being passed a |
| 1540 | // TetherMasterSM.CMD_UPSTREAM_CHANGED. |
| 1541 | handleNewUpstreamNetworkState(null); |
| 1542 | break; |
| 1543 | default: |
| 1544 | mLog.e("Unknown arg1 value: " + arg1); |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | class TetherModeAliveState extends State { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1550 | boolean mUpstreamWanted = false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1551 | boolean mTryCell = true; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 1552 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1553 | @Override |
| 1554 | public void enter() { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1555 | // If turning on master tether settings fails, we have already |
| 1556 | // transitioned to an error state; exit early. |
| 1557 | if (!turnOnMasterTetherSettings()) { |
| 1558 | return; |
| 1559 | } |
| 1560 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1561 | mSimChange.startListening(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1562 | mUpstreamNetworkMonitor.start(); |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 1563 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1564 | // TODO: De-duplicate with updateUpstreamWanted() below. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1565 | if (upstreamWanted()) { |
| 1566 | mUpstreamWanted = true; |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1567 | mOffload.start(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1568 | chooseUpstreamType(true); |
| 1569 | mTryCell = false; |
| 1570 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1571 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1572 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1573 | @Override |
| 1574 | public void exit() { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1575 | mOffload.stop(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1576 | mUpstreamNetworkMonitor.stop(); |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1577 | mSimChange.stopListening(); |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1578 | notifyDownstreamsOfNewUpstreamIface(null); |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1579 | handleNewUpstreamNetworkState(null); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1580 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1581 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1582 | private boolean updateUpstreamWanted() { |
| 1583 | final boolean previousUpstreamWanted = mUpstreamWanted; |
| 1584 | mUpstreamWanted = upstreamWanted(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1585 | if (mUpstreamWanted != previousUpstreamWanted) { |
| 1586 | if (mUpstreamWanted) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1587 | mOffload.start(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1588 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1589 | mOffload.stop(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1590 | } |
| 1591 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1592 | return previousUpstreamWanted; |
| 1593 | } |
| 1594 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1595 | @Override |
| 1596 | public boolean processMessage(Message message) { |
Erik Kline | 2210890 | 2017-07-06 16:40:06 +0900 | [diff] [blame] | 1597 | logMessage(this, message.what); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1598 | boolean retValue = true; |
| 1599 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1600 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1601 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1602 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1603 | handleInterfaceServingStateActive(message.arg1, who); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1604 | who.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1605 | mCurrentUpstreamIface); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1606 | // If there has been a change and an upstream is now |
| 1607 | // desired, kick off the selection process. |
| 1608 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1609 | if (!previousUpstreamWanted && mUpstreamWanted) { |
| 1610 | chooseUpstreamType(true); |
| 1611 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1612 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1613 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1614 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1615 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine) message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1616 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1617 | handleInterfaceServingStateInactive(who); |
| 1618 | |
| 1619 | if (mNotifyList.isEmpty()) { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1620 | // This transitions us out of TetherModeAliveState, |
| 1621 | // either to InitialState or an error state. |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1622 | turnOffMasterTetherSettings(); |
| 1623 | break; |
| 1624 | } |
| 1625 | |
| 1626 | if (DBG) { |
| 1627 | Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() + |
| 1628 | " live requests:"); |
| 1629 | for (TetherInterfaceStateMachine o : mNotifyList) { |
| 1630 | Log.d(TAG, " " + o); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1631 | } |
| 1632 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1633 | // If there has been a change and an upstream is no |
| 1634 | // longer desired, release any mobile requests. |
| 1635 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1636 | if (previousUpstreamWanted && !mUpstreamWanted) { |
| 1637 | mUpstreamNetworkMonitor.releaseMobileNetworkRequest(); |
| 1638 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1639 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1640 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1641 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: { |
| 1642 | final LinkProperties newLp = (LinkProperties) message.obj; |
| 1643 | if (message.arg1 == IControlsTethering.STATE_TETHERED) { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1644 | mOffload.updateDownstreamLinkProperties(newLp); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1645 | } else { |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1646 | mOffload.excludeDownstreamInterface(newLp.getInterfaceName()); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1647 | } |
| 1648 | break; |
| 1649 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1650 | case CMD_UPSTREAM_CHANGED: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1651 | updateUpstreamWanted(); |
| 1652 | if (!mUpstreamWanted) break; |
| 1653 | |
Erik Kline | fb41343 | 2017-02-14 18:26:04 +0900 | [diff] [blame] | 1654 | // Need to try DUN immediately if Wi-Fi goes down. |
| 1655 | chooseUpstreamType(true); |
| 1656 | mTryCell = false; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1657 | break; |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1658 | case CMD_RETRY_UPSTREAM: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1659 | updateUpstreamWanted(); |
| 1660 | if (!mUpstreamWanted) break; |
| 1661 | |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1662 | chooseUpstreamType(mTryCell); |
| 1663 | mTryCell = !mTryCell; |
| 1664 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1665 | case EVENT_UPSTREAM_CALLBACK: { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1666 | updateUpstreamWanted(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1667 | if (mUpstreamWanted) { |
| 1668 | handleUpstreamNetworkMonitorCallback(message.arg1, message.obj); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1669 | } |
| 1670 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1671 | } |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1672 | default: |
| 1673 | retValue = false; |
| 1674 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1675 | } |
| 1676 | return retValue; |
| 1677 | } |
| 1678 | } |
| 1679 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1680 | class ErrorState extends State { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1681 | private int mErrorNotification; |
| 1682 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1683 | @Override |
| 1684 | public boolean processMessage(Message message) { |
| 1685 | boolean retValue = true; |
| 1686 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1687 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
Erik Kline | 32179ff | 2017-07-04 18:28:11 +0900 | [diff] [blame] | 1688 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine) message.obj; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1689 | who.sendMessage(mErrorNotification); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1690 | break; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1691 | case CMD_CLEAR_ERROR: |
| 1692 | mErrorNotification = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 1693 | transitionTo(mInitialState); |
| 1694 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1695 | default: |
| 1696 | retValue = false; |
| 1697 | } |
| 1698 | return retValue; |
| 1699 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1700 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1701 | void notify(int msgType) { |
| 1702 | mErrorNotification = msgType; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1703 | for (TetherInterfaceStateMachine sm : mNotifyList) { |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1704 | sm.sendMessage(msgType); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1709 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1710 | class SetIpForwardingEnabledErrorState extends ErrorState { |
| 1711 | @Override |
| 1712 | public void enter() { |
| 1713 | Log.e(TAG, "Error in setIpForwardingEnabled"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1714 | notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | class SetIpForwardingDisabledErrorState extends ErrorState { |
| 1719 | @Override |
| 1720 | public void enter() { |
| 1721 | Log.e(TAG, "Error in setIpForwardingDisabled"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1722 | notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | class StartTetheringErrorState extends ErrorState { |
| 1727 | @Override |
| 1728 | public void enter() { |
| 1729 | Log.e(TAG, "Error in startTethering"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1730 | notify(TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1731 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1732 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1733 | } catch (Exception e) {} |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | class StopTetheringErrorState extends ErrorState { |
| 1738 | @Override |
| 1739 | public void enter() { |
| 1740 | Log.e(TAG, "Error in stopTethering"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1741 | notify(TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1742 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1743 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1744 | } catch (Exception e) {} |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | class SetDnsForwardersErrorState extends ErrorState { |
| 1749 | @Override |
| 1750 | public void enter() { |
| 1751 | Log.e(TAG, "Error in setDnsForwarders"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1752 | notify(TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1753 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1754 | mNMService.stopTethering(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1755 | } catch (Exception e) {} |
| 1756 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1757 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1758 | } catch (Exception e) {} |
| 1759 | } |
| 1760 | } |
Erik Kline | ed962a8 | 2017-07-06 19:49:35 +0900 | [diff] [blame] | 1761 | |
| 1762 | // A wrapper class to handle multiple situations where several calls to |
| 1763 | // the OffloadController need to happen together. |
| 1764 | // |
| 1765 | // TODO: This suggests that the interface between OffloadController and |
| 1766 | // Tethering is in need of improvement. Refactor these calls into the |
| 1767 | // OffloadController implementation. |
| 1768 | class OffloadWrapper { |
| 1769 | public void start() { |
| 1770 | mOffloadController.start(); |
| 1771 | sendOffloadExemptPrefixes(); |
| 1772 | } |
| 1773 | |
| 1774 | public void stop() { |
| 1775 | mOffloadController.stop(); |
| 1776 | } |
| 1777 | |
| 1778 | public void updateUpstreamNetworkState(NetworkState ns) { |
| 1779 | mOffloadController.setUpstreamLinkProperties( |
| 1780 | (ns != null) ? ns.linkProperties : null); |
| 1781 | } |
| 1782 | |
| 1783 | public void updateDownstreamLinkProperties(LinkProperties newLp) { |
| 1784 | // Update the list of offload-exempt prefixes before adding |
| 1785 | // new prefixes on downstream interfaces to the offload HAL. |
| 1786 | sendOffloadExemptPrefixes(); |
| 1787 | mOffloadController.notifyDownstreamLinkProperties(newLp); |
| 1788 | } |
| 1789 | |
| 1790 | public void excludeDownstreamInterface(String ifname) { |
| 1791 | // This and other interfaces may be in local-only hotspot mode; |
| 1792 | // resend all local prefixes to the OffloadController. |
| 1793 | sendOffloadExemptPrefixes(); |
| 1794 | mOffloadController.removeDownstreamInterface(ifname); |
| 1795 | } |
| 1796 | |
| 1797 | public void sendOffloadExemptPrefixes() { |
| 1798 | sendOffloadExemptPrefixes(mUpstreamNetworkMonitor.getLocalPrefixes()); |
| 1799 | } |
| 1800 | |
| 1801 | public void sendOffloadExemptPrefixes(final Set<IpPrefix> localPrefixes) { |
| 1802 | // Add in well-known minimum set. |
| 1803 | PrefixUtils.addNonForwardablePrefixes(localPrefixes); |
| 1804 | // Add tragically hardcoded prefixes. |
| 1805 | localPrefixes.add(PrefixUtils.DEFAULT_WIFI_P2P_PREFIX); |
| 1806 | |
| 1807 | // Maybe add prefixes or addresses for downstreams, depending on |
| 1808 | // the IP serving mode of each. |
| 1809 | for (TetherInterfaceStateMachine tism : mNotifyList) { |
| 1810 | final LinkProperties lp = tism.linkProperties(); |
| 1811 | |
| 1812 | switch (tism.servingMode()) { |
| 1813 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1814 | case IControlsTethering.STATE_AVAILABLE: |
| 1815 | // No usable LinkProperties in these states. |
| 1816 | continue; |
| 1817 | case IControlsTethering.STATE_TETHERED: |
| 1818 | // Only add IPv4 /32 and IPv6 /128 prefixes. The |
| 1819 | // directly-connected prefixes will be sent as |
| 1820 | // downstream "offload-able" prefixes. |
| 1821 | for (LinkAddress addr : lp.getAllLinkAddresses()) { |
| 1822 | final InetAddress ip = addr.getAddress(); |
| 1823 | if (ip.isLinkLocalAddress()) continue; |
| 1824 | localPrefixes.add(PrefixUtils.ipAddressAsPrefix(ip)); |
| 1825 | } |
| 1826 | break; |
| 1827 | case IControlsTethering.STATE_LOCAL_ONLY: |
| 1828 | // Add prefixes covering all local IPs. |
| 1829 | localPrefixes.addAll(PrefixUtils.localPrefixesFrom(lp)); |
| 1830 | break; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | mOffloadController.setLocalPrefixes(localPrefixes); |
| 1835 | } |
| 1836 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1837 | } |
| 1838 | |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1839 | @Override |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1840 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1841 | // Binder.java closes the resource for us. |
| 1842 | @SuppressWarnings("resource") |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1843 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1844 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1845 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1846 | pw.println("Tethering:"); |
| 1847 | pw.increaseIndent(); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1848 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 1849 | pw.println("Configuration:"); |
| 1850 | pw.increaseIndent(); |
| 1851 | final TetheringConfiguration cfg = mConfig; |
| 1852 | cfg.dump(pw); |
| 1853 | pw.decreaseIndent(); |
| 1854 | |
| 1855 | synchronized (mPublicSync) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1856 | pw.println("Tether state:"); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1857 | pw.increaseIndent(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1858 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1859 | final String iface = mTetherStates.keyAt(i); |
| 1860 | final TetherState tetherState = mTetherStates.valueAt(i); |
| 1861 | pw.print(iface + " - "); |
| 1862 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1863 | switch (tetherState.lastState) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1864 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1865 | pw.print("UnavailableState"); |
| 1866 | break; |
| 1867 | case IControlsTethering.STATE_AVAILABLE: |
| 1868 | pw.print("AvailableState"); |
| 1869 | break; |
| 1870 | case IControlsTethering.STATE_TETHERED: |
| 1871 | pw.print("TetheredState"); |
| 1872 | break; |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 1873 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1874 | pw.print("LocalHotspotState"); |
| 1875 | break; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1876 | default: |
| 1877 | pw.print("UnknownState"); |
| 1878 | break; |
| 1879 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1880 | pw.println(" - lastError = " + tetherState.lastError); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1881 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1882 | pw.println("Upstream wanted: " + upstreamWanted()); |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 1883 | pw.println("Current upstream interface: " + mCurrentUpstreamIface); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1884 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1885 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1886 | |
Lorenzo Colitti | c2519c5 | 2017-07-13 01:48:26 +0900 | [diff] [blame] | 1887 | pw.println("Hardware offload:"); |
| 1888 | pw.increaseIndent(); |
| 1889 | mOffloadController.dump(pw); |
| 1890 | pw.decreaseIndent(); |
| 1891 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1892 | pw.println("Log:"); |
| 1893 | pw.increaseIndent(); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1894 | if (argsContain(args, SHORT_ARG)) { |
| 1895 | pw.println("<log removed for brevity>"); |
| 1896 | } else { |
| 1897 | mLog.dump(fd, pw, args); |
| 1898 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1899 | pw.decreaseIndent(); |
| 1900 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1901 | pw.decreaseIndent(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1902 | } |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 1903 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1904 | private static boolean argsContain(String[] args, String target) { |
| 1905 | for (String arg : args) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 1906 | if (target.equals(arg)) return true; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1907 | } |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1911 | private IControlsTethering makeControlCallback(String ifname) { |
| 1912 | return new IControlsTethering() { |
| 1913 | @Override |
| 1914 | public void updateInterfaceState( |
| 1915 | TetherInterfaceStateMachine who, int state, int lastError) { |
| 1916 | notifyInterfaceStateChange(ifname, who, state, lastError); |
| 1917 | } |
| 1918 | |
| 1919 | @Override |
| 1920 | public void updateLinkProperties( |
| 1921 | TetherInterfaceStateMachine who, LinkProperties newLp) { |
| 1922 | notifyLinkPropertiesChanged(ifname, who, newLp); |
| 1923 | } |
| 1924 | }; |
| 1925 | } |
| 1926 | |
| 1927 | // TODO: Move into TetherMasterSM. |
| 1928 | private void notifyInterfaceStateChange( |
| 1929 | String iface, TetherInterfaceStateMachine who, int state, int error) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1930 | synchronized (mPublicSync) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1931 | final TetherState tetherState = mTetherStates.get(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1932 | if (tetherState != null && tetherState.stateMachine.equals(who)) { |
| 1933 | tetherState.lastState = state; |
| 1934 | tetherState.lastError = error; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1935 | } else { |
| 1936 | if (DBG) Log.d(TAG, "got notification from stale iface " + iface); |
| 1937 | } |
| 1938 | } |
| 1939 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1940 | mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error)); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1941 | |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 1942 | try { |
| 1943 | // Notify that we're tethering (or not) this interface. |
| 1944 | // This is how data saver for instance knows if the user explicitly |
| 1945 | // turned on tethering (thus keeping us from being in data saver mode). |
| 1946 | mPolicyManager.onTetheringChanged(iface, state == IControlsTethering.STATE_TETHERED); |
| 1947 | } catch (RemoteException e) { |
| 1948 | // Not really very much we can do here. |
| 1949 | } |
| 1950 | |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1951 | // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. |
| 1952 | // Thus we give a chance for TetherMasterSM to recover to InitialState |
| 1953 | // by sending CMD_CLEAR_ERROR |
| 1954 | if (error == ConnectivityManager.TETHER_ERROR_MASTER_ERROR) { |
| 1955 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); |
| 1956 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1957 | int which; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1958 | switch (state) { |
| 1959 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1960 | case IControlsTethering.STATE_AVAILABLE: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1961 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1962 | break; |
| 1963 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 1964 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1965 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1966 | break; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1967 | default: |
| 1968 | Log.wtf(TAG, "Unknown interface state: " + state); |
| 1969 | return; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1970 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1971 | mTetherMasterSM.sendMessage(which, state, 0, who); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1972 | sendTetherStateChangedBroadcast(); |
| 1973 | } |
| 1974 | |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1975 | private void notifyLinkPropertiesChanged(String iface, TetherInterfaceStateMachine who, |
| 1976 | LinkProperties newLp) { |
| 1977 | final int state; |
| 1978 | synchronized (mPublicSync) { |
| 1979 | final TetherState tetherState = mTetherStates.get(iface); |
| 1980 | if (tetherState != null && tetherState.stateMachine.equals(who)) { |
| 1981 | state = tetherState.lastState; |
| 1982 | } else { |
| 1983 | mLog.log("got notification from stale iface " + iface); |
| 1984 | return; |
| 1985 | } |
| 1986 | } |
| 1987 | |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1988 | mLog.log(String.format( |
| 1989 | "OBSERVED LinkProperties update iface=%s state=%s lp=%s", |
| 1990 | iface, IControlsTethering.getStateString(state), newLp)); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1991 | final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; |
| 1992 | mTetherMasterSM.sendMessage(which, state, 0, newLp); |
| 1993 | } |
| 1994 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1995 | private void maybeTrackNewInterfaceLocked(final String iface) { |
| 1996 | // If we don't care about this type of interface, ignore. |
| 1997 | final int interfaceType = ifaceNameToType(iface); |
| 1998 | if (interfaceType == ConnectivityManager.TETHERING_INVALID) { |
| 1999 | mLog.log(iface + " is not a tetherable iface, ignoring"); |
| 2000 | return; |
| 2001 | } |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 2002 | maybeTrackNewInterfaceLocked(iface, interfaceType); |
| 2003 | } |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2004 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 2005 | private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2006 | // If we have already started a TISM for this interface, skip. |
| 2007 | if (mTetherStates.containsKey(iface)) { |
| 2008 | mLog.log("active iface (" + iface + ") reported as added, ignoring"); |
| 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | mLog.log("adding TetheringInterfaceStateMachine for: " + iface); |
| 2013 | final TetherState tetherState = new TetherState( |
| 2014 | new TetherInterfaceStateMachine( |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2015 | iface, mLooper, interfaceType, mLog, mNMService, mStatsService, |
Erik Kline | a8e2e79 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 2016 | makeControlCallback(iface))); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 2017 | mTetherStates.put(iface, tetherState); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 2018 | tetherState.stateMachine.start(); |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 2019 | } |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 2020 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2021 | private void stopTrackingInterfaceLocked(final String iface) { |
| 2022 | final TetherState tetherState = mTetherStates.get(iface); |
| 2023 | if (tetherState == null) { |
| 2024 | mLog.log("attempting to remove unknown iface (" + iface + "), ignoring"); |
| 2025 | return; |
| 2026 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 2027 | tetherState.stateMachine.stop(); |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 2028 | mLog.log("removing TetheringInterfaceStateMachine for: " + iface); |
| 2029 | mTetherStates.remove(iface); |
| 2030 | } |
| 2031 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 2032 | private static String[] copy(String[] strarray) { |
| 2033 | return Arrays.copyOf(strarray, strarray.length); |
| 2034 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2035 | } |