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

Commit 377e0679 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by android-build-merger
Browse files

Merge "Use one type for UUID (3/5)" am: e2617708

am: 83ba314f

Change-Id: Ic2b1ae3bfa47339545700c6187c3cbde9191f91b
parents 3fe3e386 83ba314f
Loading
Loading
Loading
Loading
+17 −11
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@
#include <sys/prctl.h>
#include <sys/stat.h>

using bluetooth::Uuid;

namespace android {
// OOB_LE_BD_ADDR_SIZE is 6 bytes addres + 1 byte address type
#define OOB_LE_BD_ADDR_SIZE 7
@@ -1085,20 +1087,23 @@ static int connectSocketNative(JNIEnv* env, jobject object, jbyteArray address,
    return -1;
  }

  jbyte* uuid = NULL;
  Uuid uuid;
  if (uuidObj != NULL) {
    uuid = env->GetByteArrayElements(uuidObj, NULL);
    if (!uuid) {
    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, (const uint8_t*)uuid, channel,
      &socket_fd, flag, callingUid);
      (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;
@@ -1107,7 +1112,6 @@ static int connectSocketNative(JNIEnv* env, jobject object, jbyteArray address,
  }

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

@@ -1123,19 +1127,22 @@ static int createSocketChannelNative(JNIEnv* env, jobject object, jint type,
    service_name = env->GetStringUTFChars(name_str, NULL);
  }

  jbyte* uuid = NULL;
  Uuid uuid;
  if (uuidObj != NULL) {
    uuid = env->GetByteArrayElements(uuidObj, NULL);
    if (!uuid) {
    jbyte* tmp = env->GetByteArrayElements(uuidObj, NULL);
    if (!tmp) {
      ALOGE("failed to get uuid");
      if (service_name) env->ReleaseStringUTFChars(name_str, service_name);
      return -1;
    }

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

  int socket_fd = -1;
  bt_status_t status = sBluetoothSocketInterface->listen(
      (btsock_type_t)type, service_name, (const uint8_t*)uuid, channel,
      (btsock_type_t)type, service_name, uuidObj ? &uuid : nullptr, channel,
      &socket_fd, flag, callingUid);
  if (status != BT_STATUS_SUCCESS) {
    ALOGE("Socket listen failed: %d", status);
@@ -1145,7 +1152,6 @@ static int createSocketChannelNative(JNIEnv* env, jobject object, jint type,
  }

  if (service_name) env->ReleaseStringUTFChars(name_str, service_name);
  if (uuid) env->ReleaseByteArrayElements(uuidObj, uuid, 0);
  return socket_fd;
}

+30 −35
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@

#include <base/bind.h>
#include <string.h>
#include <array>
#include <memory>

#include <cutils/log.h>
@@ -38,34 +39,38 @@
#define asrt(s) \
  if (!(s)) ALOGE("%s(L%d): ASSERT %s failed! ##", __func__, __LINE__, #s)

#define BD_ADDR_LEN 6
using bluetooth::Uuid;

#define UUID_PARAMS(uuid) uuid_lsb(uuid), uuid_msb(uuid)

static void set_uuid(uint8_t* uuid, jlong uuid_msb, jlong uuid_lsb) {
  for (int i = 0; i != 8; ++i) {
    uuid[i] = (uuid_lsb >> (8 * i)) & 0xFF;
    uuid[i + 8] = (uuid_msb >> (8 * i)) & 0xFF;
static Uuid from_java_uuid(jlong uuid_msb, jlong uuid_lsb) {
  std::array<uint8_t, Uuid::kNumBytes128> uu;
  for (int i = 0; i < 8; i++) {
    uu[7 - i] = (uuid_msb >> (8 * i)) & 0xFF;
    uu[15 - i] = (uuid_lsb >> (8 * i)) & 0xFF;
  }
  return Uuid::From128BitBE(uu);
}

static uint64_t uuid_lsb(const bt_uuid_t& uuid) {
static uint64_t uuid_lsb(const Uuid& uuid) {
  uint64_t lsb = 0;

  for (int i = 7; i >= 0; i--) {
  auto uu = uuid.To128BitBE();
  for (int i = 8; i <= 15; i++) {
    lsb <<= 8;
    lsb |= uuid.uu[i];
    lsb |= uu[i];
  }

  return lsb;
}

static uint64_t uuid_msb(const bt_uuid_t& uuid) {
static uint64_t uuid_msb(const Uuid& uuid) {
  uint64_t msb = 0;

  for (int i = 15; i >= 8; i--) {
  auto uu = uuid.To128BitBE();
  for (int i = 0; i <= 7; i++) {
    msb <<= 8;
    msb |= uuid.uu[i];
    msb |= uu[i];
  }

  return msb;
@@ -192,8 +197,7 @@ static jobject mPeriodicScanCallbacksObj = NULL;
 * BTA client callbacks
 */

void btgattc_register_app_cb(int status, int clientIf,
                             const bt_uuid_t& app_uuid) {
void btgattc_register_app_cb(int status, int clientIf, const Uuid& app_uuid) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;
  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onClientRegistered, status,
@@ -556,7 +560,7 @@ static const btgatt_client_callbacks_t sGattClientCallbacks = {
 * BTA server callbacks
 */

void btgatts_register_app_cb(int status, int server_if, const bt_uuid_t& uuid) {
void btgatts_register_app_cb(int status, int server_if, const Uuid& uuid) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;
  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onServerRegistered, status,
@@ -939,10 +943,8 @@ static int gattClientGetDeviceTypeNative(JNIEnv* env, jobject object,
static void gattClientRegisterAppNative(JNIEnv* env, jobject object,
                                        jlong app_uuid_lsb,
                                        jlong app_uuid_msb) {
  bt_uuid_t uuid;

  if (!sGattIf) return;
  set_uuid(uuid.uu, app_uuid_msb, app_uuid_lsb);
  Uuid uuid = from_java_uuid(app_uuid_msb, app_uuid_lsb);
  sGattIf->client->register_client(uuid);
}

@@ -952,7 +954,7 @@ static void gattClientUnregisterAppNative(JNIEnv* env, jobject object,
  sGattIf->client->unregister_client(clientIf);
}

void btgattc_register_scanner_cb(bt_uuid_t app_uuid, uint8_t scannerId,
void btgattc_register_scanner_cb(const Uuid& app_uuid, uint8_t scannerId,
                                 uint8_t status) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;
@@ -964,8 +966,7 @@ static void registerScannerNative(JNIEnv* env, jobject object,
                                  jlong app_uuid_lsb, jlong app_uuid_msb) {
  if (!sGattIf) return;

  bt_uuid_t uuid;
  set_uuid(uuid.uu, app_uuid_msb, app_uuid_lsb);
  Uuid uuid = from_java_uuid(app_uuid_msb, app_uuid_lsb);
  sGattIf->scanner->RegisterScanner(
      base::Bind(&btgattc_register_scanner_cb, uuid));
}
@@ -1041,8 +1042,7 @@ static void gattClientSearchServiceNative(JNIEnv* env, jobject object,
                                          jlong service_uuid_msb) {
  if (!sGattIf) return;

  bt_uuid_t uuid;
  set_uuid(uuid.uu, service_uuid_msb, service_uuid_lsb);
  Uuid uuid = from_java_uuid(service_uuid_msb, service_uuid_lsb);
  sGattIf->client->search_service(conn_id, search_all ? 0 : &uuid);
}

@@ -1052,8 +1052,7 @@ static void gattClientDiscoverServiceByUuidNative(JNIEnv* env, jobject object,
                                                  jlong service_uuid_msb) {
  if (!sGattIf) return;

  bt_uuid_t uuid;
  set_uuid(uuid.uu, service_uuid_msb, service_uuid_lsb);
  Uuid uuid = from_java_uuid(service_uuid_msb, service_uuid_lsb);
  sGattIf->client->btif_gattc_discover_service_by_uuid(conn_id, uuid);
}

@@ -1077,8 +1076,7 @@ static void gattClientReadUsingCharacteristicUuidNative(
    jint s_handle, jint e_handle, jint authReq) {
  if (!sGattIf) return;

  bt_uuid_t uuid;
  set_uuid(uuid.uu, uuid_msb, uuid_lsb);
  Uuid uuid = from_java_uuid(uuid_msb, uuid_lsb);
  sGattIf->client->read_using_characteristic_uuid(conn_id, uuid, s_handle,
                                                  e_handle, authReq);
}
@@ -1299,9 +1297,8 @@ static void gattClientScanFilterAddRemoveNative(
    case 2:  // BTM_BLE_PF_SRVC_UUID
    case 3:  // BTM_BLE_PF_SRVC_SOL_UUID
    {
      bt_uuid_t uuid, uuid_mask;
      set_uuid(uuid.uu, uuid_msb, uuid_lsb);
      set_uuid(uuid_mask.uu, uuid_mask_msb, uuid_mask_lsb);
      Uuid uuid = from_java_uuid(uuid_msb, uuid_lsb);
      Uuid uuid_mask = from_java_uuid(uuid_mask_msb, uuid_mask_lsb);
      if (uuid_mask_lsb != 0 && uuid_mask_msb != 0)
        sGattIf->scanner->ScanFilterAddRemove(
            action, filt_type, filt_index, 0, 0, &uuid, &uuid_mask, NULL, 0, {},
@@ -1469,9 +1466,8 @@ static void gattClientReadScanReportsNative(JNIEnv* env, jobject object,
static void gattServerRegisterAppNative(JNIEnv* env, jobject object,
                                        jlong app_uuid_lsb,
                                        jlong app_uuid_msb) {
  bt_uuid_t uuid;
  if (!sGattIf) return;
  set_uuid(uuid.uu, app_uuid_msb, app_uuid_lsb);
  Uuid uuid = from_java_uuid(app_uuid_msb, app_uuid_lsb);
  sGattIf->server->register_server(uuid);
}

@@ -1566,7 +1562,7 @@ static void gattServerAddServiceNative(JNIEnv* env, jobject object,
    if (uuid.get() != NULL) {
      jlong uuid_msb = env->CallLongMethod(uuid.get(), uuidGetMsb);
      jlong uuid_lsb = env->CallLongMethod(uuid.get(), uuidGetLsb);
      set_uuid(curr.uuid.uu, uuid_msb, uuid_lsb);
      curr.uuid = from_java_uuid(uuid_msb, uuid_lsb);
    }

    fid = env->GetFieldID(gattDbElementClazz, "type", "I");
@@ -2046,8 +2042,7 @@ static void gattTestNative(JNIEnv* env, jobject object, jint command,

  RawAddress bt_bda1 = str2addr(env, bda1);

  bt_uuid_t uuid1;
  set_uuid(uuid1.uu, uuid1_msb, uuid1_lsb);
  Uuid uuid1 = from_java_uuid(uuid1_msb, uuid1_lsb);

  btgatt_test_params_t params;
  params.bda1 = &bt_bda1;
@@ -2224,4 +2219,4 @@ int register_com_android_bluetooth_gatt(JNIEnv* env) {
         jniRegisterNativeMethods(env, "com/android/bluetooth/gatt/GattService",
                                  sMethods, NELEM(sMethods));
}
}
}  // namespace android
+23 −39
Original line number Diff line number Diff line
@@ -25,29 +25,13 @@

#include <string.h>

static const uint8_t UUID_OBEX_OBJECT_PUSH[] = {
    0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
static const uint8_t UUID_PBAP_PSE[] = {0x00, 0x00, 0x11, 0x2F, 0x00, 0x00,
                                        0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
                                        0x5F, 0x9B, 0x34, 0xFB};
static const uint8_t UUID_MAP_MAS[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00,
                                       0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
                                       0x5F, 0x9B, 0x34, 0xFB};
static const uint8_t UUID_MAP_MNS[] = {0x00, 0x00, 0x11, 0x33, 0x00, 0x00,
                                       0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
                                       0x5F, 0x9B, 0x34, 0xFB};
static const uint8_t UUID_SAP[] = {0x00, 0x00, 0x11, 0x2D, 0x00, 0x00,
                                   0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
                                   0x5F, 0x9B, 0x34, 0xFB};
// TODO:
// Both the fact that the UUIDs are declared in multiple places, plus the fact
// that there is a mess of UUID comparison and shortening methods will have to
// be fixed.
// The btcore->uuid module should be used for all instances.

#define UUID_MAX_LENGTH 16
#define IS_UUID(u1, u2) !memcmp(u1, u2, UUID_MAX_LENGTH)
using bluetooth::Uuid;

static const Uuid UUID_OBEX_OBJECT_PUSH = Uuid::From16Bit(0x1105);
static const Uuid UUID_PBAP_PSE = Uuid::From16Bit(0x112F);
static const Uuid UUID_MAP_MAS = Uuid::From16Bit(0x1132);
static const Uuid UUID_MAP_MNS = Uuid::From16Bit(0x1133);
static const Uuid UUID_SAP = Uuid::From16Bit(0x112D);

namespace android {
static jmethodID method_sdpRecordFoundCallback;
@@ -59,8 +43,8 @@ static jmethodID method_sdpSapsRecordFoundCallback;

static const btsdp_interface_t* sBluetoothSdpInterface = NULL;

static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
                                uint8_t* uuid_in, int record_size,
static void sdp_search_callback(bt_status_t status, const RawAddress& bd_addr,
                                const Uuid& uuid_in, int record_size,
                                bluetooth_sdp_record* record);

btsdp_callbacks_t sBluetoothSdpCallbacks = {sizeof(sBluetoothSdpCallbacks),
@@ -135,8 +119,8 @@ static jboolean sdpSearchNative(JNIEnv* env, jobject obj, jbyteArray address,
  }
  ALOGD("%s UUID %.*s", __func__, 16, (uint8_t*)uuid);

  int ret = sBluetoothSdpInterface->sdp_search((RawAddress*)addr,
                                               (const uint8_t*)uuid);
  int ret = sBluetoothSdpInterface->sdp_search(
      (RawAddress*)addr, Uuid::From128BitBE((uint8_t*)uuid));
  if (ret != BT_STATUS_SUCCESS) {
    ALOGE("SDP Search initialization failed: %d", ret);
  }
@@ -146,8 +130,8 @@ static jboolean sdpSearchNative(JNIEnv* env, jobject obj, jbyteArray address,
  return (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
                                uint8_t* uuid_in, int count,
static void sdp_search_callback(bt_status_t status, const RawAddress& bd_addr,
                                const Uuid& uuid_in, int count,
                                bluetooth_sdp_record* records) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;
@@ -156,14 +140,14 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
      sCallbackEnv.get(), sCallbackEnv->NewByteArray(sizeof(RawAddress)));
  if (!addr.get()) return;

  ScopedLocalRef<jbyteArray> uuid(
      sCallbackEnv.get(), sCallbackEnv->NewByteArray(sizeof(bt_uuid_t)));
  ScopedLocalRef<jbyteArray> uuid(sCallbackEnv.get(),
                                  sCallbackEnv->NewByteArray(sizeof(Uuid)));
  if (!uuid.get()) return;

  sCallbackEnv->SetByteArrayRegion(addr.get(), 0, sizeof(RawAddress),
                                   (jbyte*)bd_addr);
  sCallbackEnv->SetByteArrayRegion(uuid.get(), 0, sizeof(bt_uuid_t),
                                   (jbyte*)uuid_in);
                                   (const jbyte*)&bd_addr);
  sCallbackEnv->SetByteArrayRegion(uuid.get(), 0, sizeof(Uuid),
                                   (const jbyte*)uuid_in.To128BitBE().data());

  ALOGD("%s: Status is: %d, Record count: %d", __func__, status, count);

@@ -179,7 +163,7 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
    }

    /* call the right callback according to the uuid*/
    if (IS_UUID(UUID_MAP_MAS, uuid_in)) {
    if (uuid_in == UUID_MAP_MAS) {
      sCallbackEnv->CallVoidMethod(
          sCallbacksObj, method_sdpMasRecordFoundCallback, (jint)status,
          addr.get(), uuid.get(), (jint)record->mas.mas_instance_id,
@@ -190,7 +174,7 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
          (jint)record->mas.supported_message_types, service_name.get(),
          more_results);

    } else if (IS_UUID(UUID_MAP_MNS, uuid_in)) {
    } else if (uuid_in == UUID_MAP_MNS) {
      sCallbackEnv->CallVoidMethod(
          sCallbacksObj, method_sdpMnsRecordFoundCallback, (jint)status,
          addr.get(), uuid.get(), (jint)record->mns.hdr.l2cap_psm,
@@ -199,7 +183,7 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
          (jint)record->mns.supported_features, service_name.get(),
          more_results);

    } else if (IS_UUID(UUID_PBAP_PSE, uuid_in)) {
    } else if (uuid_in == UUID_PBAP_PSE) {
      sCallbackEnv->CallVoidMethod(
          sCallbacksObj, method_sdpPseRecordFoundCallback, (jint)status,
          addr.get(), uuid.get(), (jint)record->pse.hdr.l2cap_psm,
@@ -209,7 +193,7 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
          (jint)record->pse.supported_repositories, service_name.get(),
          more_results);

    } else if (IS_UUID(UUID_OBEX_OBJECT_PUSH, uuid_in)) {
    } else if (uuid_in == UUID_OBEX_OBJECT_PUSH) {
      jint formats_list_size = record->ops.supported_formats_list_len;
      ScopedLocalRef<jbyteArray> formats_list(
          sCallbackEnv.get(), sCallbackEnv->NewByteArray(formats_list_size));
@@ -225,7 +209,7 @@ static void sdp_search_callback(bt_status_t status, RawAddress* bd_addr,
          (jint)record->ops.hdr.profile_version, service_name.get(),
          formats_list.get(), more_results);

    } else if (IS_UUID(UUID_SAP, uuid_in)) {
    } else if (uuid_in == UUID_SAP) {
      sCallbackEnv->CallVoidMethod(
          sCallbacksObj, method_sdpSapsRecordFoundCallback, (jint)status,
          addr.get(), uuid.get(), (jint)record->mas.hdr.rfcomm_channel_number,