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

Commit 7681233c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Rely on Tethering mutex for TetherInterfaceSM"

parents ffcc6ac2 4bc8d6b1
Loading
Loading
Loading
Loading
+28 −28
Original line number Diff line number Diff line
@@ -262,7 +262,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
            TetherInterfaceStateMachine sm = mIfaces.get(iface);
            if (up) {
                if (sm == null) {
                    sm = new TetherInterfaceStateMachine(iface, mLooper, usb, mPublicSync,
                    sm = new TetherInterfaceStateMachine(iface, mLooper, usb,
                            mNMService, mStatsService, this);
                    mIfaces.put(iface, sm);
                    sm.start();
@@ -338,7 +338,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
                if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
                return;
            }
            sm = new TetherInterfaceStateMachine(iface, mLooper, usb, mPublicSync,
            sm = new TetherInterfaceStateMachine(iface, mLooper, usb,
                    mNMService, mStatsService, this);
            mIfaces.put(iface, sm);
            sm.start();
@@ -576,39 +576,40 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering

    public int tether(String iface) {
        if (DBG) Log.d(TAG, "Tethering " + iface);
        TetherInterfaceStateMachine sm = null;
        synchronized (mPublicSync) {
            sm = mIfaces.get(iface);
        }
            TetherInterfaceStateMachine sm = mIfaces.get(iface);
            if (sm == null) {
                Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
                return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
            }
        if (!sm.isAvailable() && !sm.isErrored()) {
            // Ignore the error status of the interface.  If the interface is available,
            // the errors are referring to past tethering attempts anyway.
            if (!sm.isAvailable()) {
                Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
                return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;

            }
            sm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
            return ConnectivityManager.TETHER_ERROR_NO_ERROR;
        }
    }

    public int untether(String iface) {
        if (DBG) Log.d(TAG, "Untethering " + iface);
        TetherInterfaceStateMachine sm = null;
        synchronized (mPublicSync) {
            sm = mIfaces.get(iface);
        }
            TetherInterfaceStateMachine sm = mIfaces.get(iface);
            if (sm == null) {
                Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
                return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
            }
        if (sm.isErrored()) {
            if (!sm.isTethered()) {
                Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
                return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
            }
            sm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED);
            return ConnectivityManager.TETHER_ERROR_NO_ERROR;
        }
    }

    public void untetherAll() {
        if (DBG) Log.d(TAG, "Untethering " + mIfaces);
@@ -618,9 +619,8 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
    }

    public int getLastTetherError(String iface) {
        TetherInterfaceStateMachine sm = null;
        synchronized (mPublicSync) {
            sm = mIfaces.get(iface);
            TetherInterfaceStateMachine sm = mIfaces.get(iface);
            if (sm == null) {
                Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
                        ", ignoring");
+12 −44
Original line number Diff line number Diff line
@@ -84,13 +84,10 @@ public class TetherInterfaceStateMachine extends StateMachine {
    private final boolean mUsb;
    private final String mIfaceName;

    private final Object mMutex;  // Protects the fields below.
    private boolean mAvailable;
    private boolean mTethered;
    private int mLastError;
    private String mMyUpstreamIfaceName;  // may change over time

    public TetherInterfaceStateMachine(String ifaceName, Looper looper, boolean usb, Object mutex,
    public TetherInterfaceStateMachine(String ifaceName, Looper looper, boolean usb,
                    INetworkManagementService nMService, INetworkStatsService statsService,
                    IControlsTethering tetherController) {
        super(ifaceName, looper);
@@ -99,7 +96,6 @@ public class TetherInterfaceStateMachine extends StateMachine {
        mTetherController = tetherController;
        mIfaceName = ifaceName;
        mUsb = usb;
        mMutex = mutex;
        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);

        mInitialState = new InitialState();
@@ -127,13 +123,10 @@ public class TetherInterfaceStateMachine extends StateMachine {
    }

    public int getLastError() {
        synchronized (mMutex) {
        return mLastError;
    }
    }

    private void setLastError(int error) {
        synchronized (mMutex) {
        mLastError = error;

        if (isErrored()) {
@@ -144,37 +137,18 @@ public class TetherInterfaceStateMachine extends StateMachine {
            }
        }
    }
    }

    public boolean isAvailable() {
        synchronized (mMutex) {
            return mAvailable;
        }
    }

    private void setAvailable(boolean available) {
        synchronized (mMutex) {
            mAvailable = available;
        }
        return getCurrentState() == mInitialState;
    }

    public boolean isTethered() {
        synchronized (mMutex) {
            return mTethered;
        }
    }

    private void setTethered(boolean tethered) {
        synchronized (mMutex) {
            mTethered = tethered;
        }
        return getCurrentState() == mTetheredState;
    }

    public boolean isErrored() {
        synchronized (mMutex) {
        return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
    }
    }

    // configured when we start tethering and unconfig'd on error or conclusion
    private boolean configureUsbIface(boolean enabled, String iface) {
@@ -212,8 +186,6 @@ public class TetherInterfaceStateMachine extends StateMachine {
    class InitialState extends State {
        @Override
        public void enter() {
            setAvailable(true);
            setTethered(false);
            mTetherController.sendTetherStateChangedBroadcast();
        }

@@ -241,7 +213,6 @@ public class TetherInterfaceStateMachine extends StateMachine {
    class TetheredState extends State {
        @Override
        public void enter() {
            setAvailable(false);
            if (mUsb) {
                if (!configureUsbIface(true, mIfaceName)) {
                    mTetherController.notifyInterfaceTetheringReadiness(false, TetherInterfaceStateMachine.this);
@@ -267,7 +238,6 @@ public class TetherInterfaceStateMachine extends StateMachine {
                return;
            }
            if (DBG) Log.d(TAG, "Tethered " + mIfaceName);
            setTethered(true);
            mTetherController.sendTetherStateChangedBroadcast();
        }

@@ -392,9 +362,7 @@ public class TetherInterfaceStateMachine extends StateMachine {
    class UnavailableState extends State {
        @Override
        public void enter() {
            setAvailable(false);
            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
            setTethered(false);
            mTetherController.sendTetherStateChangedBroadcast();
        }
    }
+2 −3
Original line number Diff line number Diff line
@@ -48,11 +48,10 @@ public class TetherInterfaceStateMachineTest {
    @Mock private InterfaceConfiguration mInterfaceConfiguration;

    private final TestLooper mLooper = new TestLooper();
    private final Object mMutex = new Object();
    private TetherInterfaceStateMachine mTestedSm;

    private void initStateMachine(boolean isUsb) {
        mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), isUsb, mMutex,
        mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), isUsb,
                mNMService, mStatsService, mTetherHelper);
        mTestedSm.start();
        // Starting the state machine always puts us in a consistent state and notifies
@@ -77,7 +76,7 @@ public class TetherInterfaceStateMachineTest {

    @Test
    public void startsOutAvailable() {
        mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), false, mMutex,
        mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(), false,
                mNMService, mStatsService, mTetherHelper);
        mTestedSm.start();
        mLooper.dispatchAll();