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

Commit 2e650b34 authored by zzy's avatar zzy Committed by Matthew Xie
Browse files

timeout support for accept() and added check for signal size

bug 7592240

Change-Id: Ide1868da669c190cdfed90f7af3f739ec9da690b
parent a9cc57d0
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -300,7 +300,6 @@ public final class BluetoothSocket implements Closeable {
        if (mDevice == null) throw new IOException("Connect is called on null device");

        try {
            // TODO(BT) derive flag from auth and encrypt
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
            IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
            if (bluetoothProxy == null) throw new IOException("Bluetooth is off");
@@ -349,7 +348,6 @@ public final class BluetoothSocket implements Closeable {
                    mUuid, mPort, getSecurityFlags());
        } catch (RemoteException e) {
            Log.e(TAG, Log.getStackTraceString(new Throwable()));
            // TODO(BT) right error code?
            return -1;
        }

@@ -388,8 +386,13 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ BluetoothSocket accept(int timeout) throws IOException {
        BluetoothSocket acceptedSocket;
        if (mSocketState != SocketState.LISTENING) throw new IOException("bt socket is not in listen state");
        // TODO(BT) wait on an incoming connection
        if(timeout > 0) {
            Log.d(TAG, "accept() set timeout (ms):" + timeout);
           mSocket.setSoTimeout(timeout);
        }
        String RemoteAddr = waitSocketSignal(mSocketIS);
        if(timeout > 0)
            mSocket.setSoTimeout(0);
        synchronized(this)
        {
            if (mSocketState != SocketState.LISTENING)
@@ -397,8 +400,6 @@ public final class BluetoothSocket implements Closeable {
            acceptedSocket = acceptSocket(RemoteAddr);
            //quick drop the reference of the file handle
        }
        // TODO(BT) rfcomm socket only supports one connection, return this?
        // return this;
        return acceptedSocket;
    }

@@ -451,7 +452,6 @@ public final class BluetoothSocket implements Closeable {
                    mPfd.detachFd();
           }
        }
        // TODO(BT) unbind proxy,
    }

    /*package */ void removeChannel() {
@@ -471,6 +471,8 @@ public final class BluetoothSocket implements Closeable {
        ByteBuffer bb = ByteBuffer.wrap(sig);
        bb.order(ByteOrder.nativeOrder());
        int size = bb.getShort();
        if(size != SOCK_SIGNAL_SIZE)
            throw new IOException("Connection failure, wrong signal size: " + size);
        byte [] addr = new byte[6];
        bb.get(addr);
        int channel = bb.getInt();
@@ -487,7 +489,7 @@ public final class BluetoothSocket implements Closeable {
        while(left > 0) {
            int ret = is.read(b, b.length - left, left);
            if(ret <= 0)
                 throw new IOException("read failed, socket might closed, read ret: " + ret);
                 throw new IOException("read failed, socket might closed or timeout, read ret: " + ret);
            left -= ret;
            if(left != 0)
                Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) +