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

bug 7592240

Change-Id: Ide1868da669c190cdfed90f7af3f739ec9da690b
diff --git a/core/java/android/bluetooth/BluetoothSocket.java b/core/java/android/bluetooth/BluetoothSocket.java
index 26bde19..d7a214d 100644
--- a/core/java/android/bluetooth/BluetoothSocket.java
+++ b/core/java/android/bluetooth/BluetoothSocket.java
@@ -300,7 +300,6 @@
         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 @@
                     mUuid, mPort, getSecurityFlags());
         } catch (RemoteException e) {
             Log.e(TAG, Log.getStackTraceString(new Throwable()));
-            // TODO(BT) right error code?
             return -1;
         }
 
@@ -388,8 +386,13 @@
     /*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 @@
             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 @@
                     mPfd.detachFd();
            }
         }
-        // TODO(BT) unbind proxy,
     }
 
     /*package */ void removeChannel() {
@@ -471,6 +471,8 @@
         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 @@
         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) +