Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 463e433f authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "Tethering: Use UsbManager to enable/disable RNDIS"

parents 02392b41 3c2a2f67
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
@@ -172,21 +172,6 @@ interface INetworkManagementService
     */
    void detachPppd(String tty);

    /**
     * Turn on USB RNDIS support - this will turn off thinks like adb/mass-storage
     */
    void startUsbRNDIS();

    /**
     * Turn off USB RNDIS support
     */
    void stopUsbRNDIS();

    /**
     * Check the status of USB RNDIS support
     */
    boolean isUsbRNDISStarted();

    /**
     * Start Wifi Access Point
     */
+0 −47
Original line number Diff line number Diff line
@@ -78,7 +78,6 @@ class NetworkManagementService extends INetworkManagementService.Stub {
        public static final int IpFwdStatusResult         = 211;
        public static final int InterfaceGetCfgResult     = 213;
        public static final int SoftapStatusResult        = 214;
        public static final int UsbRNDISStatusResult      = 215;
        public static final int InterfaceRxCounterResult  = 216;
        public static final int InterfaceTxCounterResult  = 217;
        public static final int InterfaceRxThrottleResult = 218;
@@ -718,52 +717,6 @@ class NetworkManagementService extends INetworkManagementService.Stub {
        }
    }

    public void startUsbRNDIS() throws IllegalStateException {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
        try {
            mConnector.doCommand("usb startrndis");
        } catch (NativeDaemonConnectorException e) {
            throw new IllegalStateException(
                    "Error communicating to native daemon for starting RNDIS", e);
        }
    }

    public void stopUsbRNDIS() throws IllegalStateException {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
        try {
            mConnector.doCommand("usb stoprndis");
        } catch (NativeDaemonConnectorException e) {
            throw new IllegalStateException("Error communicating to native daemon", e);
        }
    }

    public boolean isUsbRNDISStarted() throws IllegalStateException {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_NETWORK_STATE, "NetworkManagementService");
        ArrayList<String> rsp;
        try {
            rsp = mConnector.doCommand("usb rndisstatus");
        } catch (NativeDaemonConnectorException e) {
            throw new IllegalStateException(
                    "Error communicating to native daemon to check RNDIS status", e);
        }

        for (String line : rsp) {
            String []tok = line.split(" ");
            int code = Integer.parseInt(tok[0]);
            if (code == NetdResponseCode.UsbRNDISStatusResult) {
                if (tok[3].equals("started"))
                    return true;
                return false;
            } else {
                throw new IllegalStateException(String.format("Unexpected response code %d", code));
            }
        }
        throw new IllegalStateException("Got an empty response");
    }

    public void startAccessPoint(WifiConfiguration wifiConfig, String wlanIface, String softapIface)
             throws IllegalStateException {
        mContext.enforceCallingOrSelfPermission(
+57 −43
Original line number Diff line number Diff line
@@ -471,11 +471,20 @@ public class Tethering extends INetworkManagementEventObserver.Stub {

    // used on cable insert/remove
    private void enableUsbIfaces(boolean enable) {
        // add/remove USB interfaces when USB is connected/disconnected
        for (String intf : mTetherableUsbRegexs) {
            if (enable) {
                interfaceAdded(intf);
            } else {
                interfaceRemoved(intf);
            }
        }

        String[] ifaces = new String[0];
        try {
            ifaces = mNMService.listInterfaces();
        } catch (Exception e) {
            Log.e(TAG, "Error listing Interfaces :" + e);
            Log.e(TAG, "Error listing Interfaces", e);
            return;
        }
        for (String iface : ifaces) {
@@ -493,20 +502,19 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
    private boolean enableUsbRndis(boolean enabled) {
        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");

        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
        if (usbManager == null) {
            Log.d(TAG, "could not get UsbManager");
            return false;
        }
        try {
            if (enabled) {
                synchronized (this) {
                    if (!mNMService.isUsbRNDISStarted()) {
                        mNMService.startUsbRNDIS();
                    }
                }
                usbManager.setPrimaryFunction(UsbManager.USB_FUNCTION_RNDIS);
            } else {
                if (mNMService.isUsbRNDISStarted()) {
                    mNMService.stopUsbRNDIS();
                }
                usbManager.setPrimaryFunction(null);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error toggling usb RNDIS :" + e);
            Log.e(TAG, "Error toggling usb RNDIS", e);
            return false;
        }
        return true;
@@ -516,12 +524,18 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
    private boolean configureUsbIface(boolean enabled) {
        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");

        if (enabled) {
            // must enable RNDIS first to create the interface
            enableUsbRndis(enabled);
        }

        try {
            // bring toggle the interfaces
            String[] ifaces = new String[0];
            try {
                ifaces = mNMService.listInterfaces();
            } catch (Exception e) {
            Log.e(TAG, "Error listing Interfaces :" + e);
                Log.e(TAG, "Error listing Interfaces", e);
                return false;
            }
            for (String iface : ifaces) {
@@ -542,11 +556,16 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                            mNMService.setInterfaceConfig(iface, ifcg);
                        }
                    } catch (Exception e) {
                    Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
                        Log.e(TAG, "Error configuring interface " + iface, e);
                        return false;
                    }
                }
             }
        } finally {
            if (!enabled) {
                enableUsbRndis(false);
            }
        }

        return true;
    }
@@ -853,17 +872,12 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                    transitionTo(mInitialState);
                    return;
                }
                if (mUsb) Tethering.this.enableUsbRndis(true);
                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
                setAvailable(false);
                setTethered(true);
                sendTetherStateChangedBroadcast();
            }
            @Override
            public void exit() {
                if (mUsb) Tethering.this.enableUsbRndis(false);
            }
            @Override
            public boolean processMessage(Message message) {
                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
                boolean retValue = true;
@@ -1201,7 +1215,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                try {
                    ifaces = mNMService.listInterfaces();
                } catch (Exception e) {
                    Log.e(TAG, "Error listing Interfaces :" + e);
                    Log.e(TAG, "Error listing Interfaces", e);
                    return null;
                }

@@ -1216,7 +1230,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                                    return iface;
                                }
                            } catch (Exception e) {
                                Log.e(TAG, "Error getting iface config :" + e);
                                Log.e(TAG, "Error getting iface config", e);
                                // ignore - try next
                                continue;
                            }
@@ -1267,7 +1281,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
                            }
                        }
                    } catch (RemoteException e) {
                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
                        Log.e(TAG, "RemoteException calling ConnectivityManager", e);
                        iface = null;
                    }
                }