1 /* 2 * Copyright (C) 2017 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.networkstack.tethering; 18 19 import android.app.usage.NetworkStatsManager; 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothPan; 22 import android.content.Context; 23 import android.net.INetd; 24 import android.net.ip.IpServer; 25 import android.os.Build; 26 import android.os.Handler; 27 import android.os.IBinder; 28 import android.os.Looper; 29 import android.os.SystemProperties; 30 import android.text.TextUtils; 31 32 import androidx.annotation.NonNull; 33 import androidx.annotation.RequiresApi; 34 35 import com.android.internal.util.StateMachine; 36 import com.android.net.module.util.SharedLog; 37 import com.android.networkstack.apishim.BluetoothPanShimImpl; 38 import com.android.networkstack.apishim.common.BluetoothPanShim; 39 import com.android.networkstack.tethering.metrics.TetheringMetrics; 40 import com.android.networkstack.tethering.wear.WearableConnectionManager; 41 42 import java.util.ArrayList; 43 44 45 /** 46 * Capture tethering dependencies, for injection. 47 * 48 * @hide 49 */ 50 public abstract class TetheringDependencies { 51 /** 52 * Get a reference to the BpfCoordinator to be used by tethering. 53 */ getBpfCoordinator( @onNull BpfCoordinator.Dependencies deps)54 public @NonNull BpfCoordinator getBpfCoordinator( 55 @NonNull BpfCoordinator.Dependencies deps) { 56 return new BpfCoordinator(deps); 57 } 58 59 /** 60 * Get a reference to the offload hardware interface to be used by tethering. 61 */ getOffloadHardwareInterface(Handler h, SharedLog log)62 public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) { 63 return new OffloadHardwareInterface(h, log); 64 } 65 66 /** 67 * Get a reference to the offload controller to be used by tethering. 68 */ 69 @NonNull getOffloadController(@onNull Handler h, @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps)70 public OffloadController getOffloadController(@NonNull Handler h, 71 @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps) { 72 final NetworkStatsManager statsManager = 73 (NetworkStatsManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE); 74 return new OffloadController(h, getOffloadHardwareInterface(h, log), 75 getContext().getContentResolver(), statsManager, log, deps); 76 } 77 78 79 /** 80 * Get a reference to the UpstreamNetworkMonitor to be used by tethering. 81 */ getUpstreamNetworkMonitor(Context ctx, StateMachine target, SharedLog log, int what)82 public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, StateMachine target, 83 SharedLog log, int what) { 84 return new UpstreamNetworkMonitor(ctx, target, log, what); 85 } 86 87 /** 88 * Get a reference to the IPv6TetheringCoordinator to be used by tethering. 89 */ getIPv6TetheringCoordinator( ArrayList<IpServer> notifyList, SharedLog log)90 public IPv6TetheringCoordinator getIPv6TetheringCoordinator( 91 ArrayList<IpServer> notifyList, SharedLog log) { 92 return new IPv6TetheringCoordinator(notifyList, log); 93 } 94 95 /** 96 * Get dependencies to be used by IpServer. 97 */ getIpServerDependencies()98 public abstract IpServer.Dependencies getIpServerDependencies(); 99 100 /** 101 * Get a reference to the EntitlementManager to be used by tethering. 102 */ getEntitlementManager(Context ctx, Handler h, SharedLog log, Runnable callback)103 public EntitlementManager getEntitlementManager(Context ctx, Handler h, SharedLog log, 104 Runnable callback) { 105 return new EntitlementManager(ctx, h, log, callback); 106 } 107 108 /** 109 * Generate a new TetheringConfiguration according to input sub Id. 110 */ generateTetheringConfiguration(Context ctx, SharedLog log, int subId)111 public TetheringConfiguration generateTetheringConfiguration(Context ctx, SharedLog log, 112 int subId) { 113 return new TetheringConfiguration(ctx, log, subId); 114 } 115 116 /** 117 * Get a reference to INetd to be used by tethering. 118 */ getINetd(Context context)119 public INetd getINetd(Context context) { 120 return INetd.Stub.asInterface( 121 (IBinder) context.getSystemService(Context.NETD_SERVICE)); 122 } 123 124 /** 125 * Get a reference to the TetheringNotificationUpdater to be used by tethering. 126 */ getNotificationUpdater(@onNull final Context ctx, @NonNull final Looper looper)127 public TetheringNotificationUpdater getNotificationUpdater(@NonNull final Context ctx, 128 @NonNull final Looper looper) { 129 return new TetheringNotificationUpdater(ctx, looper); 130 } 131 132 /** 133 * Get tethering thread looper. 134 */ getTetheringLooper()135 public abstract Looper getTetheringLooper(); 136 137 /** 138 * Get Context of TetheringSerice. 139 */ getContext()140 public abstract Context getContext(); 141 142 /** 143 * Get a reference to BluetoothAdapter to be used by tethering. 144 */ getBluetoothAdapter()145 public abstract BluetoothAdapter getBluetoothAdapter(); 146 147 /** 148 * Get SystemProperties which indicate whether tethering is denied. 149 */ isTetheringDenied()150 public boolean isTetheringDenied() { 151 return TextUtils.equals(SystemProperties.get("ro.tether.denied"), "true"); 152 } 153 154 /** 155 * Get a reference to PrivateAddressCoordinator to be used by Tethering. 156 */ getPrivateAddressCoordinator(Context ctx, TetheringConfiguration cfg)157 public PrivateAddressCoordinator getPrivateAddressCoordinator(Context ctx, 158 TetheringConfiguration cfg) { 159 return new PrivateAddressCoordinator(ctx, cfg); 160 } 161 162 /** 163 * Get BluetoothPanShim object to enable/disable bluetooth tethering. 164 * 165 * TODO: use BluetoothPan directly when mainline module is built with API 32. 166 */ getBluetoothPanShim(BluetoothPan pan)167 public BluetoothPanShim getBluetoothPanShim(BluetoothPan pan) { 168 return BluetoothPanShimImpl.newInstance(pan); 169 } 170 171 /** 172 * Get a reference to the TetheringMetrics to be used by tethering. 173 */ getTetheringMetrics()174 public TetheringMetrics getTetheringMetrics() { 175 return new TetheringMetrics(); 176 } 177 178 /** 179 * Returns the implementation of WearableConnectionManager. 180 */ 181 @RequiresApi(Build.VERSION_CODES.TIRAMISU) getWearableConnectionManager(Context ctx)182 public WearableConnectionManager getWearableConnectionManager(Context ctx) { 183 return new WearableConnectionManager(ctx); 184 } 185 } 186