blob: e6fc1ea293a05c0ac1955df4b0b4472e727e2700 [file] [log] [blame]
Robert Greenwalt7b816022014-04-18 15:25:25 -07001/*
2 * Copyright (C) 2014 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
17package android.net;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Looper;
22import android.os.Message;
23import android.os.Messenger;
Robert Greenwalt7b816022014-04-18 15:25:25 -070024import android.util.Log;
Robert Greenwalt7b816022014-04-18 15:25:25 -070025
26import com.android.internal.util.AsyncChannel;
27import com.android.internal.util.Protocol;
28
Robert Greenwalt3192dec2014-05-27 13:20:24 -070029import java.util.ArrayList;
fenglu95ce8032015-05-01 17:04:36 -070030import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt7b816022014-04-18 15:25:25 -070031
32/**
Robert Greenwalt3192dec2014-05-27 13:20:24 -070033 * A Utility class for handling for communicating between bearer-specific
34 * code and ConnectivityService.
Robert Greenwalt7b816022014-04-18 15:25:25 -070035 *
36 * A bearer may have more than one NetworkAgent if it can simultaneously
37 * support separate networks (IMS / Internet / MMS Apns on cellular, or
Robert Greenwalt3192dec2014-05-27 13:20:24 -070038 * perhaps connections with different SSID or P2P for Wi-Fi).
Robert Greenwalt7b816022014-04-18 15:25:25 -070039 *
Robert Greenwalt7b816022014-04-18 15:25:25 -070040 * @hide
41 */
42public abstract class NetworkAgent extends Handler {
Paul Jensen31a94f42015-02-13 14:18:39 -050043 // Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown
44 // an exception.
45 public final int netId;
46
Robert Greenwalt3192dec2014-05-27 13:20:24 -070047 private volatile AsyncChannel mAsyncChannel;
Robert Greenwalt7b816022014-04-18 15:25:25 -070048 private final String LOG_TAG;
49 private static final boolean DBG = true;
Robert Greenwaltfc0c6892014-08-27 14:34:02 -070050 private static final boolean VDBG = false;
Robert Greenwalt7b816022014-04-18 15:25:25 -070051 private final Context mContext;
Robert Greenwalt3192dec2014-05-27 13:20:24 -070052 private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
fenglub15e72b2015-03-20 11:29:56 -070053 private volatile long mLastBwRefreshTime = 0;
54 private static final long BW_REFRESH_MIN_WIN_MS = 500;
fenglu95ce8032015-05-01 17:04:36 -070055 private boolean mPollLceScheduled = false;
56 private AtomicBoolean mPollLcePending = new AtomicBoolean(false);
Robert Greenwalt7b816022014-04-18 15:25:25 -070057
58 private static final int BASE = Protocol.BASE_NETWORK_AGENT;
59
60 /**
Robert Greenwalt7b816022014-04-18 15:25:25 -070061 * Sent by ConnectivityService to the NetworkAgent to inform it of
62 * suspected connectivity problems on its network. The NetworkAgent
63 * should take steps to verify and correct connectivity.
64 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070065 public static final int CMD_SUSPECT_BAD = BASE;
Robert Greenwalt7b816022014-04-18 15:25:25 -070066
67 /**
68 * Sent by the NetworkAgent (note the EVENT vs CMD prefix) to
69 * ConnectivityService to pass the current NetworkInfo (connection state).
70 * Sent when the NetworkInfo changes, mainly due to change of state.
71 * obj = NetworkInfo
72 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070073 public static final int EVENT_NETWORK_INFO_CHANGED = BASE + 1;
Robert Greenwalt7b816022014-04-18 15:25:25 -070074
75 /**
76 * Sent by the NetworkAgent to ConnectivityService to pass the current
77 * NetworkCapabilties.
78 * obj = NetworkCapabilities
79 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070080 public static final int EVENT_NETWORK_CAPABILITIES_CHANGED = BASE + 2;
Robert Greenwalt7b816022014-04-18 15:25:25 -070081
82 /**
83 * Sent by the NetworkAgent to ConnectivityService to pass the current
84 * NetworkProperties.
85 * obj = NetworkProperties
86 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070087 public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3;
Robert Greenwalt7b816022014-04-18 15:25:25 -070088
vandwalle2ab90892014-06-02 15:30:39 -070089 /* centralize place where base network score, and network score scaling, will be
90 * stored, so as we can consistently compare apple and oranges, or wifi, ethernet and LTE
91 */
92 public static final int WIFI_BASE_SCORE = 60;
93
Robert Greenwalt7b816022014-04-18 15:25:25 -070094 /**
95 * Sent by the NetworkAgent to ConnectivityService to pass the current
96 * network score.
Robert Greenwalt3192dec2014-05-27 13:20:24 -070097 * obj = network score Integer
Robert Greenwalt7b816022014-04-18 15:25:25 -070098 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070099 public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700100
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400101 /**
102 * Sent by the NetworkAgent to ConnectivityService to add new UID ranges
103 * to be forced into this Network. For VPNs only.
104 * obj = UidRange[] to forward
105 */
106 public static final int EVENT_UID_RANGES_ADDED = BASE + 5;
107
108 /**
109 * Sent by the NetworkAgent to ConnectivityService to remove UID ranges
110 * from being forced into this Network. For VPNs only.
111 * obj = UidRange[] to stop forwarding
112 */
113 public static final int EVENT_UID_RANGES_REMOVED = BASE + 6;
114
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -0700115 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900116 * Sent by ConnectivityService to the NetworkAgent to inform the agent of the
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700117 * networks status - whether we could use the network or could not, due to
118 * either a bad network configuration (no internet link) or captive portal.
119 *
120 * arg1 = either {@code VALID_NETWORK} or {@code INVALID_NETWORK}
121 */
Lorenzo Colitti60446162014-09-20 00:14:31 +0900122 public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7;
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700123
124 public static final int VALID_NETWORK = 1;
125 public static final int INVALID_NETWORK = 2;
126
Robert Greenwalte73cc462014-09-07 16:50:01 -0700127 /**
128 * Sent by the NetworkAgent to ConnectivityService to indicate this network was
129 * explicitly selected. This should be sent before the NetworkInfo is marked
130 * CONNECTED so it can be given special treatment at that time.
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900131 *
132 * obj = boolean indicating whether to use this network even if unvalidated
Robert Greenwalte73cc462014-09-07 16:50:01 -0700133 */
Lorenzo Colitti60446162014-09-20 00:14:31 +0900134 public static final int EVENT_SET_EXPLICITLY_SELECTED = BASE + 8;
Robert Greenwalte73cc462014-09-07 16:50:01 -0700135
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900136 /**
137 * Sent by ConnectivityService to the NetworkAgent to inform the agent of
138 * whether the network should in the future be used even if not validated.
139 * This decision is made by the user, but it is the network transport's
140 * responsibility to remember it.
141 *
142 * arg1 = 1 if true, 0 if false
143 */
144 public static final int CMD_SAVE_ACCEPT_UNVALIDATED = BASE + 9;
145
fenglub15e72b2015-03-20 11:29:56 -0700146 /** Sent by ConnectivityService to the NetworkAgent to inform the agent to pull
147 * the underlying network connection for updated bandwidth information.
148 */
149 public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10;
150
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700151 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
152 NetworkCapabilities nc, LinkProperties lp, int score) {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700153 this(looper, context, logTag, ni, nc, lp, score, null);
154 }
155
156 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
157 NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -0700158 super(looper);
159 LOG_TAG = logTag;
160 mContext = context;
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700161 if (ni == null || nc == null || lp == null) {
162 throw new IllegalArgumentException();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700163 }
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700164
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700165 if (VDBG) log("Registering NetworkAgent");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700166 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
167 Context.CONNECTIVITY_SERVICE);
Paul Jensen31a94f42015-02-13 14:18:39 -0500168 netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni),
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700169 new LinkProperties(lp), new NetworkCapabilities(nc), score, misc);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700170 }
171
172 @Override
173 public void handleMessage(Message msg) {
174 switch (msg.what) {
175 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700176 if (mAsyncChannel != null) {
177 log("Received new connection while already connected!");
178 } else {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700179 if (VDBG) log("NetworkAgent fully connected");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700180 AsyncChannel ac = new AsyncChannel();
181 ac.connected(null, this, msg.replyTo);
182 ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
183 AsyncChannel.STATUS_SUCCESSFUL);
184 synchronized (mPreConnectedQueue) {
185 mAsyncChannel = ac;
186 for (Message m : mPreConnectedQueue) {
187 ac.sendMessage(m);
188 }
189 mPreConnectedQueue.clear();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700190 }
191 }
192 break;
193 }
194 case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700195 if (VDBG) log("CMD_CHANNEL_DISCONNECT");
Robert Greenwalt7b816022014-04-18 15:25:25 -0700196 if (mAsyncChannel != null) mAsyncChannel.disconnect();
197 break;
198 }
199 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
200 if (DBG) log("NetworkAgent channel lost");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700201 // let the client know CS is done with us.
202 unwanted();
203 synchronized (mPreConnectedQueue) {
204 mAsyncChannel = null;
205 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700206 break;
207 }
208 case CMD_SUSPECT_BAD: {
209 log("Unhandled Message " + msg);
210 break;
211 }
fenglub15e72b2015-03-20 11:29:56 -0700212 case CMD_REQUEST_BANDWIDTH_UPDATE: {
fenglu95ce8032015-05-01 17:04:36 -0700213 long currentTimeMs = System.currentTimeMillis();
fenglub15e72b2015-03-20 11:29:56 -0700214 if (VDBG) {
215 log("CMD_REQUEST_BANDWIDTH_UPDATE request received.");
216 }
fenglu95ce8032015-05-01 17:04:36 -0700217 if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) {
218 mPollLceScheduled = false;
219 if (mPollLcePending.getAndSet(true) == false) {
220 pollLceData();
221 }
222 } else {
223 // deliver the request at a later time rather than discard it completely.
224 if (!mPollLceScheduled) {
225 long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS -
226 currentTimeMs + 1;
227 mPollLceScheduled = sendEmptyMessageDelayed(
228 CMD_REQUEST_BANDWIDTH_UPDATE, waitTime);
229 }
fenglub15e72b2015-03-20 11:29:56 -0700230 }
231 break;
232 }
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700233 case CMD_REPORT_NETWORK_STATUS: {
234 if (VDBG) {
235 log("CMD_REPORT_NETWORK_STATUS(" +
236 (msg.arg1 == VALID_NETWORK ? "VALID)" : "INVALID)"));
237 }
238 networkStatus(msg.arg1);
239 break;
240 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900241 case CMD_SAVE_ACCEPT_UNVALIDATED: {
242 saveAcceptUnvalidated(msg.arg1 != 0);
243 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700244 }
245 }
246
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700247 private void queueOrSendMessage(int what, Object obj) {
248 synchronized (mPreConnectedQueue) {
249 if (mAsyncChannel != null) {
250 mAsyncChannel.sendMessage(what, obj);
251 } else {
252 Message msg = Message.obtain();
253 msg.what = what;
254 msg.obj = obj;
255 mPreConnectedQueue.add(msg);
Lorenzo Colitti9a6a11a2014-05-24 02:25:55 +0900256 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700257 }
258 }
259
Robert Greenwalt7b816022014-04-18 15:25:25 -0700260 /**
261 * Called by the bearer code when it has new LinkProperties data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700262 */
263 public void sendLinkProperties(LinkProperties linkProperties) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700264 queueOrSendMessage(EVENT_NETWORK_PROPERTIES_CHANGED, new LinkProperties(linkProperties));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700265 }
266
267 /**
268 * Called by the bearer code when it has new NetworkInfo data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700269 */
270 public void sendNetworkInfo(NetworkInfo networkInfo) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700271 queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, new NetworkInfo(networkInfo));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700272 }
273
274 /**
275 * Called by the bearer code when it has new NetworkCapabilities data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700276 */
277 public void sendNetworkCapabilities(NetworkCapabilities networkCapabilities) {
fenglu95ce8032015-05-01 17:04:36 -0700278 mPollLcePending.set(false);
fenglub15e72b2015-03-20 11:29:56 -0700279 mLastBwRefreshTime = System.currentTimeMillis();
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700280 queueOrSendMessage(EVENT_NETWORK_CAPABILITIES_CHANGED,
281 new NetworkCapabilities(networkCapabilities));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700282 }
283
284 /**
285 * Called by the bearer code when it has a new score for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700286 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700287 public void sendNetworkScore(int score) {
Robert Greenwalt35f7a942014-09-09 14:46:37 -0700288 if (score < 0) {
289 throw new IllegalArgumentException("Score must be >= 0");
290 }
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700291 queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new Integer(score));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700292 }
293
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700294 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400295 * Called by the VPN code when it wants to add ranges of UIDs to be routed
296 * through the VPN network.
297 */
298 public void addUidRanges(UidRange[] ranges) {
299 queueOrSendMessage(EVENT_UID_RANGES_ADDED, ranges);
300 }
301
302 /**
303 * Called by the VPN code when it wants to remove ranges of UIDs from being routed
304 * through the VPN network.
305 */
306 public void removeUidRanges(UidRange[] ranges) {
307 queueOrSendMessage(EVENT_UID_RANGES_REMOVED, ranges);
308 }
309
310 /**
Robert Greenwalte73cc462014-09-07 16:50:01 -0700311 * Called by the bearer to indicate this network was manually selected by the user.
312 * This should be called before the NetworkInfo is marked CONNECTED so that this
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900313 * Network can be given special treatment at that time. If {@code acceptUnvalidated} is
314 * {@code true}, then the system will switch to this network. If it is {@code false} and the
315 * network cannot be validated, the system will ask the user whether to switch to this network.
316 * If the user confirms and selects "don't ask again", then the system will call
317 * {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever
318 * calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement
319 * {@link #saveAcceptUnvalidated} to respect the user's choice.
Robert Greenwalte73cc462014-09-07 16:50:01 -0700320 */
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900321 public void explicitlySelected(boolean acceptUnvalidated) {
322 queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, acceptUnvalidated);
Robert Greenwalte73cc462014-09-07 16:50:01 -0700323 }
324
325 /**
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700326 * Called when ConnectivityService has indicated they no longer want this network.
327 * The parent factory should (previously) have received indication of the change
328 * as well, either canceling NetworkRequests or altering their score such that this
329 * network won't be immediately requested again.
330 */
331 abstract protected void unwanted();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700332
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700333 /**
fenglub15e72b2015-03-20 11:29:56 -0700334 * Called when ConnectivityService request a bandwidth update. The parent factory
335 * shall try to overwrite this method and produce a bandwidth update if capable.
336 */
337 protected void pollLceData() {
338 }
339
340 /**
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700341 * Called when the system determines the usefulness of this network.
342 *
343 * Networks claiming internet connectivity will have their internet
344 * connectivity verified.
345 *
346 * Currently there are two possible values:
347 * {@code VALID_NETWORK} if the system is happy with the connection,
348 * {@code INVALID_NETWORK} if the system is not happy.
349 * TODO - add indications of captive portal-ness and related success/failure,
350 * ie, CAPTIVE_SUCCESS_NETWORK, CAPTIVE_NETWORK for successful login and detection
351 *
352 * This may be called multiple times as the network status changes and may
353 * generate false negatives if we lose ip connectivity before the link is torn down.
354 */
355 protected void networkStatus(int status) {
356 }
357
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900358 /**
359 * Called when the user asks to remember the choice to use this network even if unvalidated.
360 * The transport is responsible for remembering the choice, and the next time the user connects
361 * to the network, should explicitlySelected with {@code acceptUnvalidated} set to {@code true}.
362 * This method will only be called if {@link #explicitlySelected} was called with
363 * {@code acceptUnvalidated} set to {@code false}.
364 */
365 protected void saveAcceptUnvalidated(boolean accept) {
366 }
367
Robert Greenwalt7b816022014-04-18 15:25:25 -0700368 protected void log(String s) {
369 Log.d(LOG_TAG, "NetworkAgent: " + s);
370 }
371}