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

Commit 9900a6ed authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by android-build-merger
Browse files

Merge "Move connectSocket into IBluetoothSocketManager (2/3)"

am: 975e8973

Change-Id: I3c0ee48e3fc5dbeb30b47f39a0c20a322ccf449b
parents b48b7434 975e8973
Loading
Loading
Loading
Loading
+35 −2
Original line number Diff line number Diff line
@@ -20,14 +20,47 @@
#include <base/logging.h>
#include <binder/IPCThreadState.h>

using ::android::binder::Status;
using ::android::OK;
using ::android::String8;
using ::android::binder::Status;
using ::android::os::ParcelFileDescriptor;
using ::android::os::ParcelUuid;
using ::android::String8;

namespace android {
namespace bluetooth {

Status BluetoothSocketManagerBinderServer::connectSocket(
    const BluetoothDevice& device, int32_t type,
    const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
    std::unique_ptr<ParcelFileDescriptor>* _aidl_return) {
  if (!isCallerActiveUserOrManagedProfile()) {
    LOG(WARNING) << "connectSocket() - Not allowed for non-active users";
    return Status::ok();
  }

  ENFORCE_PERMISSION(PERMISSION_BLUETOOTH);

  IPCThreadState* ipc = IPCThreadState::self();

  int socket_fd = -1;
  bt_status_t status = socketInterface->connect(
      &device.address, (btsock_type_t)type, uuid ? &uuid->uuid : nullptr, port,
      &socket_fd, flag, ipc->getCallingUid());
  if (status != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Socket connection failed: " << +status;
    socket_fd = -1;
  }

  if (socket_fd < 0) {
    LOG(ERROR) << "Fail to create file descriptor on socket fd";
    return Status::ok();
  }

  _aidl_return->reset(new ParcelFileDescriptor());
  (*_aidl_return)->fd = socket_fd;
  return Status::ok();
}

Status BluetoothSocketManagerBinderServer::createSocketChannel(
    int32_t type, const std::unique_ptr<::android::String16>& serviceName,
    const std::unique_ptr<ParcelUuid>& uuid, int32_t port, int32_t flag,
+6 −0
Original line number Diff line number Diff line
@@ -34,6 +34,12 @@ class BluetoothSocketManagerBinderServer : public BnBluetoothSocketManager {
      : socketInterface(socketInterface) {}
  ~BluetoothSocketManagerBinderServer() override = default;

  Status connectSocket(
      const BluetoothDevice& device, int32_t type,
      const std::unique_ptr<::android::os::ParcelUuid>& uuid, int32_t port,
      int32_t flag,
      std::unique_ptr<::android::os::ParcelFileDescriptor>* _aidl_return);

  Status createSocketChannel(
      int32_t type, const std::unique_ptr<::android::String16>& serviceName,
      const std::unique_ptr<::android::os::ParcelUuid>& uuid, int32_t port,
+0 −40
Original line number Diff line number Diff line
@@ -1113,45 +1113,6 @@ static jboolean getRemoteServicesNative(JNIEnv* env, jobject obj,
  return (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

static int connectSocketNative(JNIEnv* env, jobject object, jbyteArray address,
                               jint type, jbyteArray uuidObj, jint channel,
                               jint flag, jint callingUid) {
  if (!sBluetoothSocketInterface) return -1;

  jbyte* addr = env->GetByteArrayElements(address, NULL);
  if (!addr) {
    ALOGE("failed to get Bluetooth device address");
    return -1;
  }

  Uuid uuid;
  if (uuidObj != NULL) {
    jbyte* tmp = env->GetByteArrayElements(uuidObj, NULL);
    if (!tmp) {
      ALOGE("failed to get uuid");
      env->ReleaseByteArrayElements(address, addr, 0);
      return -1;
    }

    uuid = Uuid::From128BitBE(reinterpret_cast<uint8_t*>(tmp));
    env->ReleaseByteArrayElements(uuidObj, tmp, 0);
  }

  int socket_fd = -1;
  bt_status_t status = sBluetoothSocketInterface->connect(
      (RawAddress*)addr, (btsock_type_t)type, uuidObj ? &uuid : nullptr,
      channel, &socket_fd, flag, callingUid);
  if (status != BT_STATUS_SUCCESS) {
    ALOGE("Socket connection failed: %d", status);
    socket_fd = -1;
  } else if (socket_fd < 0) {
    ALOGE("Fail to create file descriptor on socket fd");
  }

  env->ReleaseByteArrayElements(address, addr, 0);
  return socket_fd;
}

static jobject getSocketManagerNative(JNIEnv* env) {
  if (!socketManager.get())
    socketManager =
@@ -1256,7 +1217,6 @@ static JNINativeMethod sMethods[] = {
    {"pinReplyNative", "([BZI[B)Z", (void*)pinReplyNative},
    {"sspReplyNative", "([BIZI)Z", (void*)sspReplyNative},
    {"getRemoteServicesNative", "([B)Z", (void*)getRemoteServicesNative},
    {"connectSocketNative", "([BI[BIII)I", (void*)connectSocketNative},
    {"getSocketManagerNative", "()Landroid/os/IBinder;",
     (void*)getSocketManagerNative},
    {"setSystemUiUidNative", "(I)V", (void*)setSystemUiUidNative},
+0 −32
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.ParcelUuid;
import android.os.PowerManager;
import android.os.Process;
@@ -1460,21 +1459,6 @@ public class AdapterService extends Service {
            service.sendConnectionStateChange(device, profile, state, prevState);
        }

        @Override
        public ParcelFileDescriptor connectSocket(BluetoothDevice device, int type, ParcelUuid uuid,
                int port, int flag) {
            if (!Utils.checkCallerAllowManagedProfiles(mService)) {
                Log.w(TAG, "connectSocket() - Not allowed for non-active user");
                return null;
            }

            AdapterService service = getService();
            if (service == null) {
                return null;
            }
            return service.connectSocket(device, type, uuid, port, flag);
        }

        @Override
        public IBluetoothSocketManager getSocketManager() {
            AdapterService service = getService();
@@ -2155,18 +2139,6 @@ public class AdapterService extends Service {

    }

    ParcelFileDescriptor connectSocket(BluetoothDevice device, int type, ParcelUuid uuid, int port,
            int flag) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        int fd = connectSocketNative(Utils.getBytesFromAddress(device.getAddress()), type,
                Utils.uuidToByteArray(uuid), port, flag, Binder.getCallingUid());
        if (fd < 0) {
            errorLog("Failed to connect socket");
            return null;
        }
        return ParcelFileDescriptor.adoptFd(fd);
    }

    IBluetoothSocketManager getSocketManager() {
        android.os.IBinder obj = getSocketManagerNative();
        if (obj == null) {
@@ -2636,10 +2608,6 @@ public class AdapterService extends Service {

    private native int readEnergyInfo();

    // TODO(BT) move this to ../btsock dir
    private native int connectSocketNative(byte[] address, int type, byte[] uuid, int port,
            int flag, int callingUid);

    private native IBinder getSocketManagerNative();

    private native void setSystemUiUidNative(int systemUiUid);