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

Commit 08479a3c authored by Liang Li's avatar Liang Li Committed by Automerger Merge Worker
Browse files

Add bluetooth offload socket API am: 77fbb39b

parents 189614bb 77fbb39b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -29,8 +29,12 @@ interface IBluetoothSocketManager
{
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    @nullable ParcelFileDescriptor connectSocket(in BluetoothDevice device, int type, in @nullable ParcelUuid uuid, int port, int flag);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    @nullable ParcelFileDescriptor connectSocketwithOffload(in BluetoothDevice device, int type, in @nullable ParcelUuid uuid, int port, int flag, int dataPath, in String socketName, long hubId, long endpointId, int maximumPacketSize);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    @nullable ParcelFileDescriptor createSocketChannel(int type, in @nullable String serviceName, in @nullable ParcelUuid uuid, int port, int flag);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
    @nullable ParcelFileDescriptor createSocketChannelWithOffload(int type, in @nullable String serviceName, in @nullable ParcelUuid uuid, int port, int flag, int dataPath, in String socketName, long hubId, long endpointId, int maximumPacketSize);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)")
    void requestMaximumTxDataLength(in BluetoothDevice device);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED})")
+28 −7
Original line number Diff line number Diff line
@@ -1782,11 +1782,14 @@ static jboolean setBufferLengthMillisNative(JNIEnv* /* env */, jobject /* obj */
}

static jint connectSocketNative(JNIEnv* env, jobject /* obj */, jbyteArray address, jint type,
                                jbyteArray uuid, jint port, jint flag, jint callingUid) {
                                jbyteArray uuid, jint port, jint flag, jint callingUid,
                                jint dataPath, jstring socketName, jlong hubId, jlong endPointId,
                                jint maxRxPacketSize) {
  int socket_fd = INVALID_FD;
  jbyte* addr = nullptr;
  jbyte* uuidBytes = nullptr;
  Uuid btUuid;
  const char* nativeSocketName = nullptr;

  if (!sBluetoothSocketInterface) {
    goto done;
@@ -1799,9 +1802,13 @@ static jint connectSocketNative(JNIEnv* env, jobject /* obj */, jbyteArray addre
  }

  btUuid = Uuid::From128BitBE(reinterpret_cast<uint8_t*>(uuidBytes));
  if (socketName != nullptr) {
    nativeSocketName = env->GetStringUTFChars(socketName, nullptr);
  }
  if (sBluetoothSocketInterface->connect(reinterpret_cast<RawAddress*>(addr), (btsock_type_t)type,
                                         &btUuid, port, &socket_fd, flag,
                                         callingUid) != BT_STATUS_SUCCESS) {
                                         &btUuid, port, &socket_fd, flag, callingUid,
                                         (btsock_data_path_t)dataPath, nativeSocketName, hubId,
                                         endPointId, maxRxPacketSize) != BT_STATUS_SUCCESS) {
    socket_fd = INVALID_FD;
  }

@@ -1812,16 +1819,21 @@ done:
  if (uuidBytes) {
    env->ReleaseByteArrayElements(uuid, uuidBytes, 0);
  }
  if (nativeSocketName) {
    env->ReleaseStringUTFChars(socketName, nativeSocketName);
  }
  return socket_fd;
}

static jint createSocketChannelNative(JNIEnv* env, jobject /* obj */, jint type,
                                      jstring serviceName, jbyteArray uuid, jint port, jint flag,
                                      jint callingUid) {
                                      jint callingUid, jint dataPath, jstring socketName,
                                      jlong hubId, jlong endPointId, jint maxRxPacketSize) {
  int socket_fd = INVALID_FD;
  jbyte* uuidBytes = nullptr;
  Uuid btUuid;
  const char* nativeServiceName = nullptr;
  const char* nativeSocketName = nullptr;

  if (!sBluetoothSocketInterface) {
    goto done;
@@ -1835,9 +1847,14 @@ static jint createSocketChannelNative(JNIEnv* env, jobject /* obj */, jint type,
    goto done;
  }
  btUuid = Uuid::From128BitBE(reinterpret_cast<uint8_t*>(uuidBytes));
  if (socketName != nullptr) {
    nativeSocketName = env->GetStringUTFChars(socketName, nullptr);
  }

  if (sBluetoothSocketInterface->listen((btsock_type_t)type, nativeServiceName, &btUuid, port,
                                        &socket_fd, flag, callingUid) != BT_STATUS_SUCCESS) {
                                        &socket_fd, flag, callingUid, (btsock_data_path_t)dataPath,
                                        nativeSocketName, hubId, endPointId,
                                        maxRxPacketSize) != BT_STATUS_SUCCESS) {
    socket_fd = INVALID_FD;
  }

@@ -1848,6 +1865,9 @@ done:
  if (nativeServiceName) {
    env->ReleaseStringUTFChars(serviceName, nativeServiceName);
  }
  if (nativeSocketName) {
    env->ReleaseStringUTFChars(socketName, nativeSocketName);
  }
  return socket_fd;
}

@@ -2267,8 +2287,9 @@ int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env) {
          {"setBufferLengthMillisNative", "(II)Z",
           reinterpret_cast<void*>(setBufferLengthMillisNative)},
          {"getMetricIdNative", "([B)I", reinterpret_cast<void*>(getMetricIdNative)},
          {"connectSocketNative", "([BI[BIII)I", reinterpret_cast<void*>(connectSocketNative)},
          {"createSocketChannelNative", "(ILjava/lang/String;[BIII)I",
          {"connectSocketNative", "([BI[BIIIILjava/lang/String;JJI)I",
           reinterpret_cast<void*>(connectSocketNative)},
          {"createSocketChannelNative", "(ILjava/lang/String;[BIIIILjava/lang/String;JJI)I",
           reinterpret_cast<void*>(createSocketChannelNative)},
          {"requestMaximumTxDataLengthNative", "([B)V",
           reinterpret_cast<void*>(requestMaximumTxDataLengthNative)},
+69 −6
Original line number Diff line number Diff line
@@ -195,13 +195,56 @@ public class AdapterNativeInterface {
        return getMetricIdNative(address);
    }

    int connectSocket(byte[] address, int type, byte[] uuid, int port, int flag, int callingUid) {
        return connectSocketNative(address, type, uuid, port, flag, callingUid);
    int connectSocket(
            byte[] address,
            int type,
            byte[] uuid,
            int port,
            int flag,
            int callingUid,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize) {
        return connectSocketNative(
                address,
                type,
                uuid,
                port,
                flag,
                callingUid,
                dataPath,
                socketName,
                hubId,
                endpointId,
                maximumPacketSize);
    }

    int createSocketChannel(
            int type, String serviceName, byte[] uuid, int port, int flag, int callingUid) {
        return createSocketChannelNative(type, serviceName, uuid, port, flag, callingUid);
            int type,
            String serviceName,
            byte[] uuid,
            int port,
            int flag,
            int callingUid,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize) {
        return createSocketChannelNative(
                type,
                serviceName,
                uuid,
                port,
                flag,
                callingUid,
                dataPath,
                socketName,
                hubId,
                endpointId,
                maximumPacketSize);
    }

    void requestMaximumTxDataLength(byte[] address) {
@@ -359,10 +402,30 @@ public class AdapterNativeInterface {
    private native int getMetricIdNative(byte[] address);

    private native int connectSocketNative(
            byte[] address, int type, byte[] uuid, int port, int flag, int callingUid);
            byte[] address,
            int type,
            byte[] uuid,
            int port,
            int flag,
            int callingUid,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize);

    private native int createSocketChannelNative(
            int type, String serviceName, byte[] uuid, int port, int flag, int callingUid);
            int type,
            String serviceName,
            byte[] uuid,
            int port,
            int flag,
            int callingUid,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize);

    private native void requestMaximumTxDataLengthNative(byte[] address);

+151 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothSocketSettings;
import android.bluetooth.IBluetoothSocketManager;
import android.content.AttributionSource;
import android.os.Binder;
@@ -86,7 +87,85 @@ class BluetoothSocketManagerBinder extends IBluetoothSocketManager.Stub {
                                Utils.uuidToByteArray(uuid),
                                port,
                                flag,
                                Binder.getCallingUid()));
                                Binder.getCallingUid(),
                                0,
                                "",
                                0,
                                0,
                                0));
    }

    @Override
    public ParcelFileDescriptor connectSocketwithOffload(
            BluetoothDevice device,
            int type,
            ParcelUuid uuid,
            int port,
            int flag,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize) {

        enforceActiveUser();

        if (!Utils.checkConnectPermissionForPreflight(mService)) {
            return null;
        }

        if (dataPath != BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD) {
            mService.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            if (type != BluetoothSocket.TYPE_LE || !mService.isLeCocSocketOffloadSupported()) {
                throw new IllegalStateException("Unsupported socket type for offload " + type);
            }
        }

        String brEdrAddress =
                Flags.identityAddressNullIfNotKnown()
                        ? Utils.getBrEdrAddress(device)
                        : mService.getIdentityAddress(device.getAddress());

        Log.i(
                TAG,
                "connectSocketwithOffload: device="
                        + device
                        + ", type="
                        + type
                        + ", uuid="
                        + uuid
                        + ", port="
                        + port
                        + ", from "
                        + Utils.getUidPidString()
                        + ", dataPath="
                        + dataPath
                        + ", socketName="
                        + socketName
                        + ", hubId="
                        + hubId
                        + ", endpointId="
                        + endpointId
                        + ", maximumPacketSize="
                        + maximumPacketSize);

        return marshalFd(
                mService.getNative()
                        .connectSocket(
                                Utils.getBytesFromAddress(
                                        type == BluetoothSocket.TYPE_L2CAP_LE
                                                ? device.getAddress()
                                                : brEdrAddress),
                                type,
                                Utils.uuidToByteArray(uuid),
                                port,
                                flag,
                                Binder.getCallingUid(),
                                dataPath,
                                socketName,
                                hubId,
                                endpointId,
                                maximumPacketSize));
    }

    @Override
@@ -120,7 +199,77 @@ class BluetoothSocketManagerBinder extends IBluetoothSocketManager.Stub {
                                Utils.uuidToByteArray(uuid),
                                port,
                                flag,
                                Binder.getCallingUid()));
                                Binder.getCallingUid(),
                                0,
                                "",
                                0,
                                0,
                                0));
    }

    @Override
    public ParcelFileDescriptor createSocketChannelWithOffload(
            int type,
            String serviceName,
            ParcelUuid uuid,
            int port,
            int flag,
            int dataPath,
            String socketName,
            long hubId,
            long endpointId,
            int maximumPacketSize) {

        enforceActiveUser();

        if (!Utils.checkConnectPermissionForPreflight(mService)) {
            return null;
        }

        if (dataPath != BluetoothSocketSettings.DATA_PATH_NO_OFFLOAD) {
            mService.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            if (type != BluetoothSocket.TYPE_LE || !mService.isLeCocSocketOffloadSupported()) {
                throw new IllegalStateException("Unsupported socket type for offload " + type);
            }
        }

        Log.i(
                TAG,
                "createSocketChannelWithOffload: type="
                        + type
                        + ", serviceName="
                        + serviceName
                        + ", uuid="
                        + uuid
                        + ", port="
                        + port
                        + ", from "
                        + Utils.getUidPidString()
                        + ", dataPath="
                        + dataPath
                        + ", socketName="
                        + socketName
                        + ", hubId="
                        + hubId
                        + ", endpointId="
                        + endpointId
                        + ", maximumPacketSize="
                        + maximumPacketSize);

        return marshalFd(
                mService.getNative()
                        .createSocketChannel(
                                type,
                                serviceName,
                                Utils.uuidToByteArray(uuid),
                                port,
                                flag,
                                Binder.getCallingUid(),
                                dataPath,
                                socketName,
                                hubId,
                                endpointId,
                                maximumPacketSize));
    }

    @Override
+2 −2
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ package android.bluetooth {
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String, java.util.UUID) throws java.io.IOException;
    method @NonNull @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothServerSocket listenUsingL2capChannel() throws java.io.IOException;
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothServerSocket listenUsingRfcommWithServiceRecord(String, java.util.UUID) throws java.io.IOException;
    method @FlaggedApi("com.android.bluetooth.flags.socket_settings_api") @NonNull @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public android.bluetooth.BluetoothServerSocket listenUsingSocketSettings(@NonNull android.bluetooth.BluetoothSocketSettings) throws java.io.IOException;
    method @FlaggedApi("com.android.bluetooth.flags.socket_settings_api") @NonNull @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}, conditional=true) public android.bluetooth.BluetoothServerSocket listenUsingSocketSettings(@NonNull android.bluetooth.BluetoothSocketSettings) throws java.io.IOException;
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean setName(String);
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN) public boolean startDiscovery();
    method @Deprecated @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN) public boolean startLeScan(android.bluetooth.BluetoothAdapter.LeScanCallback);
@@ -1094,7 +1094,7 @@ package android.bluetooth {

  public final class BluetoothSocket implements java.io.Closeable {
    method public void close() throws java.io.IOException;
    method @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public void connect() throws java.io.IOException;
    method @FlaggedApi("com.android.bluetooth.flags.socket_settings_api") @RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED}, conditional=true) public void connect() throws java.io.IOException;
    method public int getConnectionType();
    method public java.io.InputStream getInputStream() throws java.io.IOException;
    method public int getMaxReceivePacketSize();
Loading