junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 android.net; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.IntRange; |
| 21 | import android.annotation.NonNull; |
junyulai | c7ea124 | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
paulhu | 311b769 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 24 | import android.os.Binder; |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 25 | import android.os.ParcelFileDescriptor; |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 27 | |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 28 | import java.io.IOException; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | import java.util.concurrent.Executor; |
| 32 | |
| 33 | /** |
| 34 | * Allows applications to request that the system periodically send specific packets on their |
| 35 | * behalf, using hardware offload to save battery power. |
| 36 | * |
| 37 | * To request that the system send keepalives, call one of the methods that return a |
| 38 | * {@link SocketKeepalive} object, such as {@link ConnectivityManager#createSocketKeepalive}, |
| 39 | * passing in a non-null callback. If the {@link SocketKeepalive} is successfully |
| 40 | * started, the callback's {@code onStarted} method will be called. If an error occurs, |
| 41 | * {@code onError} will be called, specifying one of the {@code ERROR_*} constants in this |
| 42 | * class. |
| 43 | * |
| 44 | * To stop an existing keepalive, call {@link SocketKeepalive#stop}. The system will call |
| 45 | * {@link SocketKeepalive.Callback#onStopped} if the operation was successful or |
| 46 | * {@link SocketKeepalive.Callback#onError} if an error occurred. |
junyulai | 15e26fb | 2019-04-17 15:22:46 +0800 | [diff] [blame] | 47 | * |
Aaron Huang | 59fc443 | 2019-06-05 17:09:29 +0800 | [diff] [blame] | 48 | * For cellular, the device MUST support at least 1 keepalive slot. |
| 49 | * |
| 50 | * For WiFi, the device SHOULD support keepalive offload. If it does not, it MUST reply with |
junyulai | 15e26fb | 2019-04-17 15:22:46 +0800 | [diff] [blame] | 51 | * {@link SocketKeepalive.Callback#onError} with {@code ERROR_UNSUPPORTED} to any keepalive offload |
Aaron Huang | 59fc443 | 2019-06-05 17:09:29 +0800 | [diff] [blame] | 52 | * request. If it does, it MUST support at least 3 concurrent keepalive slots. |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 53 | */ |
| 54 | public abstract class SocketKeepalive implements AutoCloseable { |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 55 | /** @hide */ |
| 56 | protected static final String TAG = "SocketKeepalive"; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 57 | |
paulhu | 311b769 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 58 | /** |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 59 | * Success. It indicates there is no error. |
paulhu | 311b769 | 2020-01-13 16:18:54 +0800 | [diff] [blame] | 60 | * @hide |
| 61 | */ |
| 62 | @SystemApi |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 63 | public static final int SUCCESS = 0; |
| 64 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 65 | /** |
| 66 | * No keepalive. This should only be internally as it indicates There is no keepalive. |
| 67 | * It should not propagate to applications. |
| 68 | * @hide |
| 69 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 70 | public static final int NO_KEEPALIVE = -1; |
| 71 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 72 | /** |
| 73 | * Data received. |
| 74 | * @hide |
| 75 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 76 | public static final int DATA_RECEIVED = -2; |
| 77 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 78 | /** |
| 79 | * The binder died. |
| 80 | * @hide |
| 81 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 82 | public static final int BINDER_DIED = -10; |
| 83 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 84 | /** |
| 85 | * The invalid network. It indicates the specified {@code Network} is not connected. |
| 86 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 87 | public static final int ERROR_INVALID_NETWORK = -20; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * The invalid IP addresses. Indicates the specified IP addresses are invalid. |
| 91 | * For example, the specified source IP address is not configured on the |
| 92 | * specified {@code Network}. |
| 93 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 94 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * The port is invalid. |
| 98 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 99 | public static final int ERROR_INVALID_PORT = -22; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * The length is invalid (e.g. too long). |
| 103 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 104 | public static final int ERROR_INVALID_LENGTH = -23; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * The interval is invalid (e.g. too short). |
| 108 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 109 | public static final int ERROR_INVALID_INTERVAL = -24; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * The socket is invalid. |
| 113 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 114 | public static final int ERROR_INVALID_SOCKET = -25; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * The socket is not idle. |
| 118 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 119 | public static final int ERROR_SOCKET_NOT_IDLE = -26; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 120 | |
junyulai | 72d2bd0 | 2019-09-03 18:51:04 +0800 | [diff] [blame] | 121 | /** |
| 122 | * The stop reason is uninitialized. This should only be internally used as initial state |
| 123 | * of stop reason, instead of propagating to application. |
| 124 | * @hide |
| 125 | */ |
| 126 | public static final int ERROR_STOP_REASON_UNINITIALIZED = -27; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 127 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 128 | /** |
| 129 | * The request is unsupported. |
| 130 | */ |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 131 | public static final int ERROR_UNSUPPORTED = -30; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * There was a hardware error. |
| 135 | */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 136 | public static final int ERROR_HARDWARE_ERROR = -31; |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Resources are insufficient (e.g. all hardware slots are in use). |
| 140 | */ |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 141 | public static final int ERROR_INSUFFICIENT_RESOURCES = -32; |
| 142 | |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 143 | /** |
| 144 | * There was no such slot. This should only be internally as it indicates |
| 145 | * a programming error in the system server. It should not propagate to |
| 146 | * applications. |
| 147 | * @hide |
| 148 | */ |
| 149 | @SystemApi |
| 150 | public static final int ERROR_NO_SUCH_SLOT = -33; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 151 | |
| 152 | /** @hide */ |
| 153 | @Retention(RetentionPolicy.SOURCE) |
| 154 | @IntDef(prefix = { "ERROR_" }, value = { |
| 155 | ERROR_INVALID_NETWORK, |
| 156 | ERROR_INVALID_IP_ADDRESS, |
| 157 | ERROR_INVALID_PORT, |
| 158 | ERROR_INVALID_LENGTH, |
| 159 | ERROR_INVALID_INTERVAL, |
| 160 | ERROR_INVALID_SOCKET, |
lifr | cbe8c09 | 2021-03-18 01:11:30 +0800 | [diff] [blame] | 161 | ERROR_SOCKET_NOT_IDLE, |
| 162 | ERROR_NO_SUCH_SLOT |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 163 | }) |
| 164 | public @interface ErrorCode {} |
| 165 | |
Chalard Jean | 7fe99f5 | 2020-03-24 23:16:35 +0900 | [diff] [blame] | 166 | /** @hide */ |
| 167 | @Retention(RetentionPolicy.SOURCE) |
| 168 | @IntDef(value = { |
| 169 | SUCCESS, |
| 170 | ERROR_INVALID_LENGTH, |
| 171 | ERROR_UNSUPPORTED, |
Chalard Jean | ef90250 | 2020-03-27 15:00:38 +0900 | [diff] [blame] | 172 | ERROR_INSUFFICIENT_RESOURCES, |
Chalard Jean | 7fe99f5 | 2020-03-24 23:16:35 +0900 | [diff] [blame] | 173 | }) |
| 174 | public @interface KeepaliveEvent {} |
| 175 | |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 176 | /** |
| 177 | * The minimum interval in seconds between keepalive packet transmissions. |
| 178 | * |
| 179 | * @hide |
| 180 | **/ |
| 181 | public static final int MIN_INTERVAL_SEC = 10; |
| 182 | |
| 183 | /** |
| 184 | * The maximum interval in seconds between keepalive packet transmissions. |
| 185 | * |
| 186 | * @hide |
| 187 | **/ |
| 188 | public static final int MAX_INTERVAL_SEC = 3600; |
| 189 | |
junyulai | 21c06d0 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 190 | /** |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 191 | * An exception that embarks an error code. |
| 192 | * @hide |
| 193 | */ |
| 194 | public static class ErrorCodeException extends Exception { |
| 195 | public final int error; |
| 196 | public ErrorCodeException(final int error, final Throwable e) { |
| 197 | super(e); |
| 198 | this.error = error; |
| 199 | } |
| 200 | public ErrorCodeException(final int error) { |
| 201 | this.error = error; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * This socket is invalid. |
| 207 | * See the error code for details, and the optional cause. |
| 208 | * @hide |
| 209 | */ |
| 210 | public static class InvalidSocketException extends ErrorCodeException { |
| 211 | public InvalidSocketException(final int error, final Throwable e) { |
| 212 | super(error, e); |
| 213 | } |
| 214 | public InvalidSocketException(final int error) { |
| 215 | super(error); |
| 216 | } |
| 217 | } |
| 218 | |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 219 | /** @hide */ |
| 220 | @NonNull protected final IConnectivityManager mService; |
| 221 | /** @hide */ |
| 222 | @NonNull protected final Network mNetwork; |
| 223 | /** @hide */ |
| 224 | @NonNull protected final ParcelFileDescriptor mPfd; |
| 225 | /** @hide */ |
| 226 | @NonNull protected final Executor mExecutor; |
| 227 | /** @hide */ |
| 228 | @NonNull protected final ISocketKeepaliveCallback mCallback; |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 229 | // TODO: remove slot since mCallback could be used to identify which keepalive to stop. |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 230 | /** @hide */ |
| 231 | @Nullable protected Integer mSlot; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 232 | |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 233 | /** @hide */ |
| 234 | public SocketKeepalive(@NonNull IConnectivityManager service, @NonNull Network network, |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 235 | @NonNull ParcelFileDescriptor pfd, |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 236 | @NonNull Executor executor, @NonNull Callback callback) { |
| 237 | mService = service; |
| 238 | mNetwork = network; |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 239 | mPfd = pfd; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 240 | mExecutor = executor; |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 241 | mCallback = new ISocketKeepaliveCallback.Stub() { |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 242 | @Override |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 243 | public void onStarted(int slot) { |
lucaslin | ad369e3 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 244 | final long token = Binder.clearCallingIdentity(); |
| 245 | try { |
| 246 | mExecutor.execute(() -> { |
| 247 | mSlot = slot; |
| 248 | callback.onStarted(); |
| 249 | }); |
| 250 | } finally { |
| 251 | Binder.restoreCallingIdentity(token); |
| 252 | } |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 253 | } |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 254 | |
| 255 | @Override |
| 256 | public void onStopped() { |
lucaslin | ad369e3 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 257 | final long token = Binder.clearCallingIdentity(); |
| 258 | try { |
| 259 | executor.execute(() -> { |
| 260 | mSlot = null; |
| 261 | callback.onStopped(); |
| 262 | }); |
| 263 | } finally { |
| 264 | Binder.restoreCallingIdentity(token); |
| 265 | } |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void onError(int error) { |
lucaslin | ad369e3 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 270 | final long token = Binder.clearCallingIdentity(); |
| 271 | try { |
| 272 | executor.execute(() -> { |
| 273 | mSlot = null; |
| 274 | callback.onError(error); |
| 275 | }); |
| 276 | } finally { |
| 277 | Binder.restoreCallingIdentity(token); |
| 278 | } |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void onDataReceived() { |
lucaslin | ad369e3 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 283 | final long token = Binder.clearCallingIdentity(); |
| 284 | try { |
| 285 | executor.execute(() -> { |
| 286 | mSlot = null; |
| 287 | callback.onDataReceived(); |
| 288 | }); |
| 289 | } finally { |
| 290 | Binder.restoreCallingIdentity(token); |
| 291 | } |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 292 | } |
| 293 | }; |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Request that keepalive be started with the given {@code intervalSec}. See |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 298 | * {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an exception |
| 299 | * when invoking start or stop of the {@link SocketKeepalive}, a {@link RemoteException} will be |
| 300 | * thrown into the {@code executor}. This is typically not important to catch because the remote |
| 301 | * party is the system, so if it is not in shape to communicate through binder the system is |
| 302 | * probably going down anyway. If the caller cares regardless, it can use a custom |
| 303 | * {@link Executor} to catch the {@link RemoteException}. |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 304 | * |
| 305 | * @param intervalSec The target interval in seconds between keepalive packet transmissions. |
| 306 | * The interval should be between 10 seconds and 3600 seconds, otherwise |
| 307 | * {@link #ERROR_INVALID_INTERVAL} will be returned. |
| 308 | */ |
| 309 | public final void start(@IntRange(from = MIN_INTERVAL_SEC, to = MAX_INTERVAL_SEC) |
| 310 | int intervalSec) { |
| 311 | startImpl(intervalSec); |
| 312 | } |
| 313 | |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 314 | /** @hide */ |
| 315 | protected abstract void startImpl(int intervalSec); |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 316 | |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 317 | /** |
| 318 | * Requests that keepalive be stopped. The application must wait for {@link Callback#onStopped} |
| 319 | * before using the object. See {@link SocketKeepalive}. |
| 320 | */ |
| 321 | public final void stop() { |
| 322 | stopImpl(); |
| 323 | } |
| 324 | |
Remi NGUYEN VAN | 7b92ff2 | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 325 | /** @hide */ |
| 326 | protected abstract void stopImpl(); |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 327 | |
| 328 | /** |
| 329 | * Deactivate this {@link SocketKeepalive} and free allocated resources. The instance won't be |
| 330 | * usable again if {@code close()} is called. |
| 331 | */ |
| 332 | @Override |
| 333 | public final void close() { |
| 334 | stop(); |
junyulai | 6114378 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 335 | try { |
| 336 | mPfd.close(); |
| 337 | } catch (IOException e) { |
| 338 | // Nothing much can be done. |
| 339 | } |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * The callback which app can use to learn the status changes of {@link SocketKeepalive}. See |
| 344 | * {@link SocketKeepalive}. |
| 345 | */ |
| 346 | public static class Callback { |
| 347 | /** The requested keepalive was successfully started. */ |
| 348 | public void onStarted() {} |
| 349 | /** The keepalive was successfully stopped. */ |
| 350 | public void onStopped() {} |
| 351 | /** An error occurred. */ |
| 352 | public void onError(@ErrorCode int error) {} |
junyulai | 9f87223 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 353 | /** The keepalive on a TCP socket was stopped because the socket received data. This is |
| 354 | * never called for UDP sockets. */ |
junyulai | a86defc | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 355 | public void onDataReceived() {} |
| 356 | } |
| 357 | } |