Add support for Ethernet tethering
Ethernet tethering can be started via
startTethering(TETHERING_ETHERNET).
Test: flashed, enabled ethernet tethering, verified internet access on
downstream.
Bug: 130840861
Change-Id: I34842acd94b972e440c3622f7617df10c18acf65
This commit is contained in:
@@ -136,6 +136,12 @@ public class TetheringManager {
|
|||||||
*/
|
*/
|
||||||
public static final int TETHERING_NCM = 4;
|
public static final int TETHERING_NCM = 4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ethernet tethering type.
|
||||||
|
* @see #startTethering(TetheringRequest, Executor, StartTetheringCallback)
|
||||||
|
*/
|
||||||
|
public static final int TETHERING_ETHERNET = 5;
|
||||||
|
|
||||||
public static final int TETHER_ERROR_NO_ERROR = 0;
|
public static final int TETHER_ERROR_NO_ERROR = 0;
|
||||||
public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
|
public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
|
||||||
public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
|
public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ public class IpServer extends StateMachine {
|
|||||||
private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24;
|
private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24;
|
||||||
private static final String WIFI_P2P_IFACE_ADDR = "192.168.49.1";
|
private static final String WIFI_P2P_IFACE_ADDR = "192.168.49.1";
|
||||||
private static final int WIFI_P2P_IFACE_PREFIX_LENGTH = 24;
|
private static final int WIFI_P2P_IFACE_PREFIX_LENGTH = 24;
|
||||||
|
private static final String ETHERNET_IFACE_ADDR = "192.168.50.1";
|
||||||
|
private static final int ETHERNET_IFACE_PREFIX_LENGTH = 24;
|
||||||
|
|
||||||
// TODO: have PanService use some visible version of this constant
|
// TODO: have PanService use some visible version of this constant
|
||||||
private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1";
|
private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1";
|
||||||
@@ -426,6 +428,10 @@ public class IpServer extends StateMachine {
|
|||||||
} else if (mInterfaceType == TetheringManager.TETHERING_WIFI_P2P) {
|
} else if (mInterfaceType == TetheringManager.TETHERING_WIFI_P2P) {
|
||||||
srvAddr = (Inet4Address) parseNumericAddress(WIFI_P2P_IFACE_ADDR);
|
srvAddr = (Inet4Address) parseNumericAddress(WIFI_P2P_IFACE_ADDR);
|
||||||
prefixLen = WIFI_P2P_IFACE_PREFIX_LENGTH;
|
prefixLen = WIFI_P2P_IFACE_PREFIX_LENGTH;
|
||||||
|
} else if (mInterfaceType == TetheringManager.TETHERING_ETHERNET) {
|
||||||
|
// TODO: randomize address for tethering too, similarly to wifi
|
||||||
|
srvAddr = (Inet4Address) parseNumericAddress(ETHERNET_IFACE_ADDR);
|
||||||
|
prefixLen = ETHERNET_IFACE_PREFIX_LENGTH;
|
||||||
} else {
|
} else {
|
||||||
// BT configures the interface elsewhere: only start DHCP.
|
// BT configures the interface elsewhere: only start DHCP.
|
||||||
// TODO: make all tethering types behave the same way, and delete the bluetooth
|
// TODO: make all tethering types behave the same way, and delete the bluetooth
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import static android.net.TetheringManager.EXTRA_ACTIVE_TETHER;
|
|||||||
import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER;
|
import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER;
|
||||||
import static android.net.TetheringManager.EXTRA_ERRORED_TETHER;
|
import static android.net.TetheringManager.EXTRA_ERRORED_TETHER;
|
||||||
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
|
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
|
||||||
|
import static android.net.TetheringManager.TETHERING_ETHERNET;
|
||||||
import static android.net.TetheringManager.TETHERING_INVALID;
|
import static android.net.TetheringManager.TETHERING_INVALID;
|
||||||
import static android.net.TetheringManager.TETHERING_NCM;
|
import static android.net.TetheringManager.TETHERING_NCM;
|
||||||
import static android.net.TetheringManager.TETHERING_USB;
|
import static android.net.TetheringManager.TETHERING_USB;
|
||||||
@@ -68,6 +69,7 @@ import android.content.IntentFilter;
|
|||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.EthernetManager;
|
||||||
import android.net.IIntResultListener;
|
import android.net.IIntResultListener;
|
||||||
import android.net.INetd;
|
import android.net.INetd;
|
||||||
import android.net.ITetheringEventCallback;
|
import android.net.ITetheringEventCallback;
|
||||||
@@ -112,6 +114,7 @@ import android.util.SparseArray;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.GuardedBy;
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.util.IndentingPrintWriter;
|
import com.android.internal.util.IndentingPrintWriter;
|
||||||
import com.android.internal.util.MessageUtils;
|
import com.android.internal.util.MessageUtils;
|
||||||
@@ -213,6 +216,13 @@ public class Tethering {
|
|||||||
private TetherStatesParcel mTetherStatesParcel;
|
private TetherStatesParcel mTetherStatesParcel;
|
||||||
private boolean mDataSaverEnabled = false;
|
private boolean mDataSaverEnabled = false;
|
||||||
|
|
||||||
|
@GuardedBy("mPublicSync")
|
||||||
|
private EthernetManager.TetheredInterfaceRequest mEthernetIfaceRequest;
|
||||||
|
@GuardedBy("mPublicSync")
|
||||||
|
private String mConfiguredEthernetIface;
|
||||||
|
@GuardedBy("mPublicSync")
|
||||||
|
private EthernetCallback mEthernetCallback;
|
||||||
|
|
||||||
public Tethering(TetheringDependencies deps) {
|
public Tethering(TetheringDependencies deps) {
|
||||||
mLog.mark("Tethering.constructed");
|
mLog.mark("Tethering.constructed");
|
||||||
mDeps = deps;
|
mDeps = deps;
|
||||||
@@ -463,6 +473,10 @@ public class Tethering {
|
|||||||
result = setNcmTethering(enable);
|
result = setNcmTethering(enable);
|
||||||
sendTetherResult(listener, result);
|
sendTetherResult(listener, result);
|
||||||
break;
|
break;
|
||||||
|
case TETHERING_ETHERNET:
|
||||||
|
result = setEthernetTethering(enable);
|
||||||
|
sendTetherResult(listener, result);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Log.w(TAG, "Invalid tether type.");
|
Log.w(TAG, "Invalid tether type.");
|
||||||
sendTetherResult(listener, TETHER_ERROR_UNKNOWN_IFACE);
|
sendTetherResult(listener, TETHER_ERROR_UNKNOWN_IFACE);
|
||||||
@@ -539,6 +553,57 @@ public class Tethering {
|
|||||||
}, BluetoothProfile.PAN);
|
}, BluetoothProfile.PAN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int setEthernetTethering(final boolean enable) {
|
||||||
|
final EthernetManager em = (EthernetManager) mContext.getSystemService(
|
||||||
|
Context.ETHERNET_SERVICE);
|
||||||
|
synchronized (mPublicSync) {
|
||||||
|
if (enable) {
|
||||||
|
mEthernetCallback = new EthernetCallback();
|
||||||
|
mEthernetIfaceRequest = em.requestTetheredInterface(mEthernetCallback);
|
||||||
|
} else {
|
||||||
|
if (mConfiguredEthernetIface != null) {
|
||||||
|
stopEthernetTetheringLocked();
|
||||||
|
mEthernetIfaceRequest.release();
|
||||||
|
}
|
||||||
|
mEthernetCallback = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TETHER_ERROR_NO_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopEthernetTetheringLocked() {
|
||||||
|
if (mConfiguredEthernetIface == null) return;
|
||||||
|
changeInterfaceState(mConfiguredEthernetIface, IpServer.STATE_AVAILABLE);
|
||||||
|
stopTrackingInterfaceLocked(mConfiguredEthernetIface);
|
||||||
|
mConfiguredEthernetIface = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class EthernetCallback implements EthernetManager.TetheredInterfaceCallback {
|
||||||
|
@Override
|
||||||
|
public void onAvailable(String iface) {
|
||||||
|
synchronized (mPublicSync) {
|
||||||
|
if (this != mEthernetCallback) {
|
||||||
|
// Ethernet callback arrived after Ethernet tethering stopped. Ignore.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
maybeTrackNewInterfaceLocked(iface, TETHERING_ETHERNET);
|
||||||
|
changeInterfaceState(iface, IpServer.STATE_TETHERED);
|
||||||
|
mConfiguredEthernetIface = iface;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUnavailable() {
|
||||||
|
synchronized (mPublicSync) {
|
||||||
|
if (this != mEthernetCallback) {
|
||||||
|
// onAvailable called after stopping Ethernet tethering.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stopEthernetTetheringLocked();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int tether(String iface) {
|
int tether(String iface) {
|
||||||
return tether(iface, IpServer.STATE_TETHERED);
|
return tether(iface, IpServer.STATE_TETHERED);
|
||||||
}
|
}
|
||||||
@@ -589,6 +654,7 @@ public class Tethering {
|
|||||||
stopTethering(TETHERING_WIFI_P2P);
|
stopTethering(TETHERING_WIFI_P2P);
|
||||||
stopTethering(TETHERING_USB);
|
stopTethering(TETHERING_USB);
|
||||||
stopTethering(TETHERING_BLUETOOTH);
|
stopTethering(TETHERING_BLUETOOTH);
|
||||||
|
stopTethering(TETHERING_ETHERNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getLastTetherError(String iface) {
|
int getLastTetherError(String iface) {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ android_test {
|
|||||||
certificate: "platform",
|
certificate: "platform",
|
||||||
srcs: [
|
srcs: [
|
||||||
"src/**/*.java",
|
"src/**/*.java",
|
||||||
|
"src/**/*.kt",
|
||||||
],
|
],
|
||||||
test_suites: [
|
test_suites: [
|
||||||
"device-tests",
|
"device-tests",
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.android.networkstack.tethering.tests.unit">
|
package="com.android.networkstack.tethering.tests.unit">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.TETHER_PRIVILEGED"/>
|
||||||
|
|
||||||
<application android:debuggable="true">
|
<application android:debuggable="true">
|
||||||
<uses-library android:name="android.test.runner" />
|
<uses-library android:name="android.test.runner" />
|
||||||
</application>
|
</application>
|
||||||
|
|||||||
Reference in New Issue
Block a user