Search in sources :

Example 31 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by ParanoidAndroid.

the class BluetoothSocket method connect.

/**
     * Attempt to connect to a remote device.
     * <p>This method will block until a connection is made or the connection
     * fails. If this method returns without an exception then this socket
     * is now connected.
     * <p>Creating new connections to
     * remote Bluetooth devices should not be attempted while device discovery
     * is in progress. Device discovery is a heavyweight procedure on the
     * Bluetooth adapter and will significantly slow a device connection.
     * Use {@link BluetoothAdapter#cancelDiscovery()} to cancel an ongoing
     * discovery. Discovery is not managed by the Activity,
     * but is run as a system service, so an application should always call
     * {@link BluetoothAdapter#cancelDiscovery()} even if it
     * did not directly request a discovery, just to be sure.
     * <p>{@link #close} can be used to abort this call from another thread.
     * @throws IOException on error, for example connection failure
     */
public void connect() throws IOException {
    if (mDevice == null)
        throw new IOException("Connect is called on null device");
    try {
        if (mSocketState == SocketState.CLOSED)
            throw new IOException("socket closed");
        IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
        if (bluetoothProxy == null)
            throw new IOException("Bluetooth is off");
        mPfd = bluetoothProxy.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags());
        synchronized (this) {
            if (DBG)
                Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
            if (mSocketState == SocketState.CLOSED)
                throw new IOException("socket closed");
            if (mPfd == null)
                throw new IOException("bt socket connect failed");
            FileDescriptor fd = mPfd.getFileDescriptor();
            mSocket = new LocalSocket(fd);
            mSocketIS = mSocket.getInputStream();
            mSocketOS = mSocket.getOutputStream();
        }
        int channel = readInt(mSocketIS);
        if (channel <= 0)
            throw new IOException("bt socket connect failed");
        mPort = channel;
        waitSocketSignal(mSocketIS);
        synchronized (this) {
            if (mSocketState == SocketState.CLOSED)
                throw new IOException("bt socket closed");
            mSocketState = SocketState.CONNECTED;
        }
    } catch (RemoteException e) {
        Log.e(TAG, Log.getStackTraceString(new Throwable()));
    }
}
Also used : LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) RemoteException(android.os.RemoteException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 32 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by ParanoidAndroid.

the class LocalSocketTest method testBasic.

@SmallTest
public void testBasic() throws Exception {
    LocalServerSocket ss;
    LocalSocket ls;
    LocalSocket ls1;
    ss = new LocalServerSocket("android.net.LocalSocketTest");
    ls = new LocalSocket();
    ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
    ls1 = ss.accept();
    // Test trivial read and write
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    // Test getting credentials
    Credentials c = ls1.getPeerCredentials();
    MoreAsserts.assertNotEqual(0, c.getPid());
    // Test sending and receiving file descriptors
    ls.setFileDescriptorsForSend(new FileDescriptor[] { FileDescriptor.in });
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
    assertEquals(1, out.length);
    // Test multible byte write and available()
    ls1.getOutputStream().write(new byte[] { 0, 1, 2, 3, 4, 5 }, 1, 5);
    assertEquals(1, ls.getInputStream().read());
    assertEquals(4, ls.getInputStream().available());
    byte[] buffer = new byte[16];
    int countRead;
    countRead = ls.getInputStream().read(buffer, 1, 15);
    assertEquals(4, countRead);
    assertEquals(2, buffer[1]);
    assertEquals(3, buffer[2]);
    assertEquals(4, buffer[3]);
    assertEquals(5, buffer[4]);
    // Try various array-out-of-bound cases
    try {
        ls.getInputStream().read(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    // Try read of length 0
    ls.getOutputStream().write(42);
    countRead = ls1.getInputStream().read(buffer, 0, 0);
    assertEquals(0, countRead);
    assertEquals(42, ls1.getInputStream().read());
    ss.close();
    ls.close();
    try {
        ls.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls.getInputStream().read();
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls1.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    // Try read on socket whose peer has closed
    assertEquals(-1, ls1.getInputStream().read());
    ls1.close();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) Credentials(android.net.Credentials) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 33 with LocalSocket

use of android.net.LocalSocket in project platform_frameworks_base by android.

the class LocalSocketTest method testBasic.

@SmallTest
public void testBasic() throws Exception {
    LocalServerSocket ss;
    LocalSocket ls;
    LocalSocket ls1;
    ss = new LocalServerSocket("android.net.LocalSocketTest");
    ls = new LocalSocket();
    ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
    ls1 = ss.accept();
    // Test trivial read and write
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    // Test getting credentials
    Credentials c = ls1.getPeerCredentials();
    MoreAsserts.assertNotEqual(0, c.getPid());
    // Test sending and receiving file descriptors
    ls.setFileDescriptorsForSend(new FileDescriptor[] { FileDescriptor.in });
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
    assertEquals(1, out.length);
    // Test multible byte write and available()
    ls1.getOutputStream().write(new byte[] { 0, 1, 2, 3, 4, 5 }, 1, 5);
    assertEquals(1, ls.getInputStream().read());
    assertEquals(4, ls.getInputStream().available());
    byte[] buffer = new byte[16];
    int countRead;
    countRead = ls.getInputStream().read(buffer, 1, 15);
    assertEquals(4, countRead);
    assertEquals(2, buffer[1]);
    assertEquals(3, buffer[2]);
    assertEquals(4, buffer[3]);
    assertEquals(5, buffer[4]);
    // Try various array-out-of-bound cases
    try {
        ls.getInputStream().read(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    // Try read of length 0
    ls.getOutputStream().write(42);
    countRead = ls1.getInputStream().read(buffer, 0, 0);
    assertEquals(0, countRead);
    assertEquals(42, ls1.getInputStream().read());
    ss.close();
    ls.close();
    try {
        ls.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls.getInputStream().read();
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls1.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    // Try read on socket whose peer has closed
    assertEquals(-1, ls1.getInputStream().read());
    ls1.close();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) Credentials(android.net.Credentials) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 34 with LocalSocket

use of android.net.LocalSocket in project platform_frameworks_base by android.

the class NetworkTest method testBindSocketOfLocalSocketThrows.

@SmallTest
public void testBindSocketOfLocalSocketThrows() throws Exception {
    final LocalSocket mLocalClient = new LocalSocket();
    mLocalClient.bind(new LocalSocketAddress("testClient"));
    assertTrue(mLocalClient.getFileDescriptor().valid());
    try {
        mNetwork.bindSocket(mLocalClient.getFileDescriptor());
        fail("SocketException not thrown");
    } catch (SocketException expected) {
    }
    final LocalServerSocket mLocalServer = new LocalServerSocket("testServer");
    mLocalClient.connect(mLocalServer.getLocalSocketAddress());
    assertTrue(mLocalClient.isConnected());
    try {
        mNetwork.bindSocket(mLocalClient.getFileDescriptor());
        fail("SocketException not thrown");
    } catch (SocketException expected) {
    }
}
Also used : SocketException(java.net.SocketException) LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 35 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by DirtyUnicorns.

the class ProcessList method openLmkdSocket.

private static boolean openLmkdSocket() {
    try {
        sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET);
        sLmkdSocket.connect(new LocalSocketAddress("lmkd", LocalSocketAddress.Namespace.RESERVED));
        sLmkdOutputStream = sLmkdSocket.getOutputStream();
    } catch (IOException ex) {
        Slog.w(TAG, "lowmemorykiller daemon socket open failed");
        sLmkdSocket = null;
        return false;
    }
    return true;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Aggregations

LocalSocket (android.net.LocalSocket)51 IOException (java.io.IOException)44 LocalSocketAddress (android.net.LocalSocketAddress)32 FileDescriptor (java.io.FileDescriptor)26 ParcelFileDescriptor (android.os.ParcelFileDescriptor)17 LocalServerSocket (android.net.LocalServerSocket)14 SmallTest (android.test.suitebuilder.annotation.SmallTest)11 RemoteException (android.os.RemoteException)10 InputStream (java.io.InputStream)8 Credentials (android.net.Credentials)6 Message (android.os.Message)6 SocketException (java.net.SocketException)6 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SuppressLint (android.annotation.SuppressLint)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1