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