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

Commit cbc2a743 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

OBEX Server Socket: Don't reject connection

OBEX Server socket should not reject new connection; instead, the user
should reject new connection if the user cannot handle it.

Bug: 72006198
Test: carkits MAP, PBAP; phones with OPP

Change-Id: I99a766daf4b5a17b25a652c4ef131e7ba7df26c8
parent 68e38401
Loading
Loading
Loading
Loading
+5 −28
Original line number Diff line number Diff line
@@ -55,12 +55,10 @@ public class ObexServerSockets {
    private final BluetoothServerSocket mRfcommSocket;
    private final BluetoothServerSocket mL2capSocket;
    /* Handles to the accept threads. Needed for shutdown. */
    private SocketAcceptThread mRfcommThread = null;
    private SocketAcceptThread mL2capThread = null;
    private SocketAcceptThread mRfcommThread;
    private SocketAcceptThread mL2capThread;

    private volatile boolean mConAccepted = false;

    private static volatile int sInstanceCounter = 0;
    private static volatile int sInstanceCounter;

    private ObexServerSockets(IObexConnectionHandler conHandler, BluetoothServerSocket rfcommSocket,
            BluetoothServerSocket l2capSocket) {
@@ -207,7 +205,6 @@ public class ObexServerSockets {
        if (D) {
            Log.d(mTag, "startAccept()");
        }
        prepareForNewConnect();

        mRfcommThread = new SocketAcceptThread(mRfcommSocket);
        mRfcommThread.start();
@@ -216,37 +213,17 @@ public class ObexServerSockets {
        mL2capThread.start();
    }

    /**
     * Set state to accept new incoming connection. Will cause the next incoming connection to be
     * Signaled through {@link IObexConnectionValidator#onConnect()};
     */
    public synchronized void prepareForNewConnect() {
        if (D) {
            Log.d(mTag, "prepareForNewConnect()");
        }
        mConAccepted = false;
    }

    /**
     * Called from the AcceptThreads to signal an incoming connection.
     * This is the entry point that needs to synchronize between the accept
     * threads, and ensure only a single connection is accepted.
     * {@link mAcceptedSocket} is used a state variable.
     * @param device the connecting device.
     * @param conSocket the socket associated with the connection.
     * @return true if the connection is accepted, false otherwise.
     */
    private synchronized boolean onConnect(BluetoothDevice device, BluetoothSocket conSocket) {
        if (D) {
            Log.d(mTag, "onConnect() socket: " + conSocket + " mConAccepted = " + mConAccepted);
        }
        if (!mConAccepted && mConHandler.onConnect(device, conSocket)) {
            mConAccepted = true; // TODO: Reset this when ready to accept new connection
            /* Signal the remaining threads to stop.
            shutdown(false); */ // UPDATE: TODO: remove - redesigned to keep running...
            return true;
            Log.d(mTag, "onConnect() socket: " + conSocket);
        }
        return false;
        return mConHandler.onConnect(device, conSocket);
    }

    /**
+7 −1
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ public class BluetoothMapMasInstance implements IObexConnectionHandler {

    private volatile boolean mInterrupted;              // Used to interrupt socket accept thread
    private volatile boolean mShutdown = false;         // Used to interrupt socket accept thread
    private volatile boolean mAcceptNewConnections = false;

    private Handler mServiceHandler = null;             // MAP service message handler
    private BluetoothMapService mMapService = null;     // Handle to the outer MAP service
@@ -283,10 +284,11 @@ public class BluetoothMapMasInstance implements IObexConnectionHandler {
        closeConnectionSocket();

        if (mServerSockets != null) {
            mServerSockets.prepareForNewConnect();
            mAcceptNewConnections = true;
        } else {

            mServerSockets = ObexServerSockets.create(this);
            mAcceptNewConnections = true;

            if (mServerSockets == null) {
                // TODO: Handle - was not handled before
@@ -469,6 +471,9 @@ public class BluetoothMapMasInstance implements IObexConnectionHandler {

    @Override
    public synchronized boolean onConnect(BluetoothDevice device, BluetoothSocket socket) {
        if (!mAcceptNewConnections) {
            return false;
        }
        /* Signal to the service that we have received an incoming connection.
         */
        boolean isValid = mMapService.onConnect(device, BluetoothMapMasInstance.this);
@@ -476,6 +481,7 @@ public class BluetoothMapMasInstance implements IObexConnectionHandler {
        if (isValid) {
            mRemoteDevice = device;
            mConnSocket = socket;
            mAcceptNewConnections = false;
        }

        return isValid;
+0 −1
Original line number Diff line number Diff line
@@ -69,7 +69,6 @@ class MnsObexServer extends ServerRequestHandler {
        if (VDBG) {
            Log.v(TAG, "onConnect");
        }
        mObexServerSockets.prepareForNewConnect();

        try {
            byte[] uuid = (byte[]) request.getHeader(HeaderSet.TARGET);
+4 −11
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import android.util.Log;
import android.webkit.MimeTypeMap;

import com.android.bluetooth.BluetoothObexTransport;
import com.android.bluetooth.ObexServerSockets;

import java.io.BufferedOutputStream;
import java.io.File;
@@ -99,13 +98,13 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler

    boolean mTimeoutMsgSent = false;

    private ObexServerSockets mServerSocket;
    private BluetoothOppService mBluetoothOppService;

    public BluetoothOppObexServerSession(Context context, ObexTransport transport,
            ObexServerSockets serverSocket) {
            BluetoothOppService service) {
        mContext = context;
        mTransport = transport;
        mServerSocket = serverSocket;
        mBluetoothOppService = service;
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mPartialWakeLock.setReferenceCounted(false);
@@ -659,13 +658,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler
            Log.d(TAG, "onClose");
        }
        releaseWakeLocks();

        if (mServerSocket != null) {
            if (D) {
                Log.d(TAG, "prepareForNewConnect");
            }
            mServerSocket.prepareForNewConnect();
        }
        mBluetoothOppService.acceptNewConnections();
        BluetoothOppUtility.cancelNotification(mContext);
        /* onClose could happen even before start() where mCallback is set */
        if (mCallback != null) {
+21 −5
Original line number Diff line number Diff line
@@ -133,16 +133,18 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
     */
    private CharArrayBuffer mNewChars;

    private boolean mListenStarted = false;
    private boolean mListenStarted;

    private boolean mMediaScanInProgress;

    private int mIncomingRetries = 0;
    private int mIncomingRetries;

    private ObexTransport mPendingConnection = null;
    private ObexTransport mPendingConnection;

    private int mOppSdpHandle = -1;

    boolean mAcceptNewConnections;

    private static final String INVISIBLE =
            BluetoothShare.VISIBILITY + "=" + BluetoothShare.VISIBILITY_HIDDEN;

@@ -389,7 +391,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                                Log.e(TAG, "close tranport error");
                            }
                            if (mServerSocket != null) {
                                mServerSocket.prepareForNewConnect();
                                acceptNewConnections();
                            }
                            mIncomingRetries = 0;
                            mPendingConnection = null;
@@ -414,6 +416,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        }
        stopListeners();
        mServerSocket = ObexServerSockets.createInsecure(this);
        acceptNewConnections();
        SdpManager sdpManager = SdpManager.getDefaultManager();
        if (sdpManager == null || mServerSocket == null) {
            Log.e(TAG, "ERROR:serversocket object is NULL  sdp manager :" + sdpManager
@@ -459,7 +462,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    /* suppose we auto accept an incoming OPUSH connection */
    private void createServerSession(ObexTransport transport) {
        mServerSession = new BluetoothOppObexServerSession(this, transport, mServerSocket);
        mServerSession = new BluetoothOppObexServerSession(this, transport, this);
        mServerSession.preStart();
        if (D) {
            Log.d(TAG, "Get ServerSession " + mServerSession.toString() + " for incoming connection"
@@ -1149,13 +1152,19 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    @Override
    public boolean onConnect(BluetoothDevice device, BluetoothSocket socket) {

        if (D) {
            Log.d(TAG, " onConnect BluetoothSocket :" + socket + " \n :device :" + device);
        }
        if (!mAcceptNewConnections) {
            Log.d(TAG, " onConnect BluetoothSocket :" + socket + " rejected");
            return false;
        }
        BluetoothObexTransport transport = new BluetoothObexTransport(socket);
        Message msg = mHandler.obtainMessage(MSG_INCOMING_BTOPP_CONNECTION);
        msg.obj = transport;
        msg.sendToTarget();
        mAcceptNewConnections = false;
        return true;
    }

@@ -1164,4 +1173,11 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        Log.d(TAG, " onAcceptFailed:");
        mHandler.sendMessage(mHandler.obtainMessage(START_LISTENER));
    }

    /**
     * Set mAcceptNewConnections to true to allow new connections.
     */
    void acceptNewConnections() {
        mAcceptNewConnections = true;
    }
}
Loading