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

Commit 8fd74ae3 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Use references in GATT client/server API (2/3)

This allows to get rid of some unnecessary null checks, and guarantee
proper values are passed.

Test: compilation test
Change-Id: I1b790dba0cfc3ab02984c3911d83a6f413a1e7e6
parent a94436a7
Loading
Loading
Loading
Loading
+44 −49
Original line number Diff line number Diff line
@@ -56,8 +56,8 @@ using base::Bind;
using base::Owned;
using std::vector;

extern bt_status_t btif_gattc_test_command_impl(int command,
                                                btgatt_test_params_t* params);
extern bt_status_t btif_gattc_test_command_impl(
    int command, const btgatt_test_params_t* params);
extern const btgatt_callbacks_t* bt_gatt_callbacks;

/*******************************************************************************
@@ -126,7 +126,7 @@ void btif_gattc_upstreams_evt(uint16_t event, char* p_param) {
      data.len = p_data->notify.len;

      HAL_CBACK(bt_gatt_callbacks, client->notify_cb, p_data->notify.conn_id,
                &data);
                data);

      if (p_data->notify.is_notify == false)
        BTA_GATTC_SendIndConfirm(p_data->notify.conn_id, p_data->notify.handle);
@@ -138,7 +138,7 @@ void btif_gattc_upstreams_evt(uint16_t event, char* p_param) {
      DVLOG(1) << "BTA_GATTC_OPEN_EVT " << p_data->open.remote_bda;
      HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id,
                p_data->open.status, p_data->open.client_if,
                &p_data->open.remote_bda);
                p_data->open.remote_bda);

      if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu) {
        HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb,
@@ -154,7 +154,7 @@ void btif_gattc_upstreams_evt(uint16_t event, char* p_param) {
    case BTA_GATTC_CLOSE_EVT: {
      HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id,
                p_data->status, p_data->close.client_if,
                &p_data->close.remote_bda);
                p_data->close.remote_bda);
      break;
    }

@@ -207,21 +207,19 @@ void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
void btm_read_rssi_cb(tBTM_RSSI_RESULTS* p_result) {
  if (!p_result) return;

  bt_bdaddr_t* addr = new bt_bdaddr_t;
  *addr = p_result->rem_bda;
  CLI_CBACK_IN_JNI(read_remote_rssi_cb, rssi_request_client_if,
                   base::Owned(addr), p_result->rssi, p_result->status);
                   p_result->rem_bda, p_result->rssi, p_result->status);
}

/*******************************************************************************
 *  Client API Functions
 ******************************************************************************/

bt_status_t btif_gattc_register_app(bt_uuid_t* uuid) {
bt_status_t btif_gattc_register_app(const bt_uuid_t& uuid) {
  CHECK_BTGATT_INIT();

  tBT_UUID bt_uuid;
  btif_to_bta_uuid(&bt_uuid, uuid);
  btif_to_bta_uuid(&bt_uuid, &uuid);

  return do_in_jni_thread(Bind(
      [](tBT_UUID bt_uuid) {
@@ -234,7 +232,7 @@ bt_status_t btif_gattc_register_app(bt_uuid_t* uuid) {
                        bt_uuid_t app_uuid;
                        bta_to_btif_uuid(&app_uuid, &bt_uuid);
                        HAL_CBACK(bt_gatt_callbacks, client->register_client_cb,
                                  status, client_id, &app_uuid);
                                  status, client_id, app_uuid);
                      },
                      bt_uuid, client_id, status));
                },
@@ -276,7 +274,7 @@ void btif_gattc_open_impl(int client_if, bt_bdaddr_t address, bool is_direct,
      BTM_BleGetVendorCapabilities(&vnd_capabilities);
      if (!vnd_capabilities.rpa_offloading) {
        HAL_CBACK(bt_gatt_callbacks, client->open_cb, 0, BT_STATUS_UNSUPPORTED,
                  client_if, &address);
                  client_if, address);
        return;
      }
    }
@@ -312,12 +310,12 @@ void btif_gattc_open_impl(int client_if, bt_bdaddr_t address, bool is_direct,
                 initiating_phys);
}

bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t* bd_addr,
bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t& bd_addr,
                            bool is_direct, int transport,
                            int initiating_phys) {
  CHECK_BTGATT_INIT();
  // Closure will own this value and free it.
  return do_in_jni_thread(Bind(&btif_gattc_open_impl, client_if, *bd_addr,
  return do_in_jni_thread(Bind(&btif_gattc_open_impl, client_if, bd_addr,
                               is_direct, transport, initiating_phys));
}

@@ -332,19 +330,20 @@ void btif_gattc_close_impl(int client_if, bt_bdaddr_t address, int conn_id) {
  BTA_GATTC_CancelOpen(client_if, address, false);
}

bt_status_t btif_gattc_close(int client_if, const bt_bdaddr_t* bd_addr,
bt_status_t btif_gattc_close(int client_if, const bt_bdaddr_t& bd_addr,
                             int conn_id) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(
      Bind(&btif_gattc_close_impl, client_if, *bd_addr, conn_id));
      Bind(&btif_gattc_close_impl, client_if, bd_addr, conn_id));
}

bt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t* bd_addr) {
bt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t& bd_addr) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(Bind(&BTA_GATTC_Refresh, *bd_addr));
  return do_in_jni_thread(Bind(&BTA_GATTC_Refresh, bd_addr));
}

bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t* filter_uuid) {
bt_status_t btif_gattc_search_service(int conn_id,
                                      const bt_uuid_t* filter_uuid) {
  CHECK_BTGATT_INIT();

  if (filter_uuid) {
@@ -358,11 +357,9 @@ bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t* filter_uuid) {
  }
}

void btif_gattc_discover_service_by_uuid(int conn_id, bt_uuid_t* p_uuid) {
  LOG_ASSERT(p_uuid);

void btif_gattc_discover_service_by_uuid(int conn_id, const bt_uuid_t& p_uuid) {
  tBT_UUID* uuid = new tBT_UUID;
  btif_to_bta_uuid(uuid, p_uuid);
  btif_to_bta_uuid(uuid, &p_uuid);
  do_in_jni_thread(
      Bind(&BTA_GATTC_DiscoverServiceByUuid, conn_id, base::Owned(uuid)));
}
@@ -420,12 +417,12 @@ void read_using_char_uuid_cb(uint16_t conn_id, tGATT_STATUS status,
                   base::Owned(params));
}

bt_status_t btif_gattc_read_using_char_uuid(int conn_id, bt_uuid_t* uuid,
bt_status_t btif_gattc_read_using_char_uuid(int conn_id, const bt_uuid_t& uuid,
                                            uint16_t s_handle,
                                            uint16_t e_handle, int auth_req) {
  CHECK_BTGATT_INIT();
  tBT_UUID bt_uuid;
  btif_to_bta_uuid(&bt_uuid, uuid);
  btif_to_bta_uuid(&bt_uuid, &uuid);
  return do_in_jni_thread(Bind(&BTA_GATTC_ReadUsingCharUuid, conn_id, bt_uuid,
                               s_handle, e_handle, auth_req,
                               read_using_char_uuid_cb, nullptr));
@@ -433,18 +430,15 @@ bt_status_t btif_gattc_read_using_char_uuid(int conn_id, bt_uuid_t* uuid,

void read_desc_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
                  uint16_t len, uint8_t* value, void* data) {
  btgatt_read_params_t* params = new btgatt_read_params_t;
  params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */;
  params->status = status;
  params->handle = handle;
  params->value.len = len;
  btgatt_read_params_t params;
  params.value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */;
  params.status = status;
  params.handle = handle;
  params.value.len = len;
  CHECK(len <= BTGATT_MAX_ATTR_LEN);
  if (len > 0) memcpy(params->value.value, value, len);
  if (len > 0) memcpy(params.value.value, value, len);

  // clang-tidy analyzer complains about |params| is leaked.  It doesn't know
  // that |param| will be freed by the callback function.
  CLI_CBACK_IN_JNI(read_descriptor_cb, conn_id, status,
                   base::Owned(params)); /* NOLINT */
  CLI_CBACK_IN_JNI(read_descriptor_cb, conn_id, status, params);
}

bt_status_t btif_gattc_read_char_descr(int conn_id, uint16_t handle,
@@ -493,7 +487,7 @@ bt_status_t btif_gattc_execute_write(int conn_id, int execute) {
}

void btif_gattc_reg_for_notification_impl(tBTA_GATTC_IF client_if,
                                          const bt_bdaddr_t bda,
                                          const bt_bdaddr_t& bda,
                                          uint16_t handle) {
  tBTA_GATT_STATUS status =
      BTA_GATTC_RegisterForNotifications(client_if, bda, handle);
@@ -504,17 +498,17 @@ void btif_gattc_reg_for_notification_impl(tBTA_GATTC_IF client_if,
}

bt_status_t btif_gattc_reg_for_notification(int client_if,
                                            const bt_bdaddr_t* bd_addr,
                                            const bt_bdaddr_t& bd_addr,
                                            uint16_t handle) {
  CHECK_BTGATT_INIT();

  return do_in_jni_thread(
      Bind(base::IgnoreResult(&btif_gattc_reg_for_notification_impl), client_if,
           *bd_addr, handle));
           bd_addr, handle));
}

void btif_gattc_dereg_for_notification_impl(tBTA_GATTC_IF client_if,
                                            const bt_bdaddr_t bda,
                                            const bt_bdaddr_t& bda,
                                            uint16_t handle) {
  tBTA_GATT_STATUS status =
      BTA_GATTC_DeregisterForNotifications(client_if, bda, handle);
@@ -525,21 +519,21 @@ void btif_gattc_dereg_for_notification_impl(tBTA_GATTC_IF client_if,
}

bt_status_t btif_gattc_dereg_for_notification(int client_if,
                                              const bt_bdaddr_t* bd_addr,
                                              const bt_bdaddr_t& bd_addr,
                                              uint16_t handle) {
  CHECK_BTGATT_INIT();

  return do_in_jni_thread(
      Bind(base::IgnoreResult(&btif_gattc_dereg_for_notification_impl),
           client_if, *bd_addr, handle));
           client_if, bd_addr, handle));
}

bt_status_t btif_gattc_read_remote_rssi(int client_if,
                                        const bt_bdaddr_t* bd_addr) {
                                        const bt_bdaddr_t& bd_addr) {
  CHECK_BTGATT_INIT();
  rssi_request_client_if = client_if;

  return do_in_jni_thread(Bind(base::IgnoreResult(&BTM_ReadRSSI), *bd_addr,
  return do_in_jni_thread(Bind(base::IgnoreResult(&BTM_ReadRSSI), bd_addr,
                               (tBTM_CMPL_CB*)btm_read_rssi_cb));
}

@@ -560,12 +554,12 @@ void btif_gattc_conn_parameter_update_impl(bt_bdaddr_t addr, int min_interval,
                               timeout);
}

bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t* bd_addr,
bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t& bd_addr,
                                             int min_interval, int max_interval,
                                             int latency, int timeout) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(
      Bind(base::IgnoreResult(&btif_gattc_conn_parameter_update_impl), *bd_addr,
      Bind(base::IgnoreResult(&btif_gattc_conn_parameter_update_impl), bd_addr,
           min_interval, max_interval, latency, timeout));
}

@@ -587,18 +581,19 @@ bt_status_t btif_gattc_read_phy(
  return BT_STATUS_SUCCESS;
}

int btif_gattc_get_device_type(const bt_bdaddr_t* bd_addr) {
int btif_gattc_get_device_type(const bt_bdaddr_t& bd_addr) {
  int device_type = 0;
  char bd_addr_str[18] = {0};

  bdaddr_to_string(bd_addr, bd_addr_str, sizeof(bd_addr_str));
  bdaddr_to_string(&bd_addr, bd_addr_str, sizeof(bd_addr_str));
  if (btif_config_get_int(bd_addr_str, "DevType", &device_type))
    return device_type;
  return 0;
}

bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params) {
  return btif_gattc_test_command_impl(command, params);
bt_status_t btif_gattc_test_command(int command,
                                    const btgatt_test_params_t& params) {
  return btif_gattc_test_command_impl(command, &params);
}

}  // namespace
+17 −17
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
      bt_uuid_t app_uuid;
      bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.uuid);
      HAL_CBACK(bt_gatt_callbacks, server->register_server_cb,
                p_data->reg_oper.status, p_data->reg_oper.server_if, &app_uuid);
                p_data->reg_oper.status, p_data->reg_oper.server_if, app_uuid);
      break;
    }

@@ -143,13 +143,13 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
                                     p_data->conn.transport);

      HAL_CBACK(bt_gatt_callbacks, server->connection_cb, p_data->conn.conn_id,
                p_data->conn.server_if, true, &p_data->conn.remote_bda);
                p_data->conn.server_if, true, p_data->conn.remote_bda);
      break;
    }

    case BTA_GATTS_DISCONNECT_EVT: {
      HAL_CBACK(bt_gatt_callbacks, server->connection_cb, p_data->conn.conn_id,
                p_data->conn.server_if, false, &p_data->conn.remote_bda);
                p_data->conn.server_if, false, p_data->conn.remote_bda);
      break;
    }

@@ -168,7 +168,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
    case BTA_GATTS_READ_CHARACTERISTIC_EVT: {
      HAL_CBACK(bt_gatt_callbacks, server->request_read_characteristic_cb,
                p_data->req_data.conn_id, p_data->req_data.trans_id,
                &p_data->req_data.remote_bda,
                p_data->req_data.remote_bda,
                p_data->req_data.p_data->read_req.handle,
                p_data->req_data.p_data->read_req.offset,
                p_data->req_data.p_data->read_req.is_long);
@@ -178,7 +178,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
    case BTA_GATTS_READ_DESCRIPTOR_EVT: {
      HAL_CBACK(bt_gatt_callbacks, server->request_read_descriptor_cb,
                p_data->req_data.conn_id, p_data->req_data.trans_id,
                &p_data->req_data.remote_bda,
                p_data->req_data.remote_bda,
                p_data->req_data.p_data->read_req.handle,
                p_data->req_data.p_data->read_req.offset,
                p_data->req_data.p_data->read_req.is_long);
@@ -190,7 +190,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
      vector<uint8_t> value(req.value, req.value + req.len);
      HAL_CBACK(bt_gatt_callbacks, server->request_write_characteristic_cb,
                p_data->req_data.conn_id, p_data->req_data.trans_id,
                &p_data->req_data.remote_bda, req.handle, req.offset,
                p_data->req_data.remote_bda, req.handle, req.offset,
                req.need_rsp, req.is_prep, value);
      break;
    }
@@ -200,7 +200,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
      vector<uint8_t> value(req.value, req.value + req.len);
      HAL_CBACK(bt_gatt_callbacks, server->request_write_descriptor_cb,
                p_data->req_data.conn_id, p_data->req_data.trans_id,
                &p_data->req_data.remote_bda, req.handle, req.offset,
                p_data->req_data.remote_bda, req.handle, req.offset,
                req.need_rsp, req.is_prep, value);
      break;
    }
@@ -208,7 +208,7 @@ static void btapp_gatts_handle_cback(uint16_t event, char* p_param) {
    case BTA_GATTS_EXEC_WRITE_EVT: {
      HAL_CBACK(bt_gatt_callbacks, server->request_exec_write_cb,
                p_data->req_data.conn_id, p_data->req_data.trans_id,
                &p_data->req_data.remote_bda,
                p_data->req_data.remote_bda,
                p_data->req_data.p_data->exec_write);
      break;
    }
@@ -266,10 +266,10 @@ static void btapp_gatts_cback(tBTA_GATTS_EVT event, tBTA_GATTS* p_data) {
/*******************************************************************************
 *  Server API Functions
 ******************************************************************************/
static bt_status_t btif_gatts_register_app(bt_uuid_t* bt_uuid) {
static bt_status_t btif_gatts_register_app(const bt_uuid_t& bt_uuid) {
  CHECK_BTGATT_INIT();
  tBT_UUID* uuid = new tBT_UUID;
  btif_to_bta_uuid(uuid, bt_uuid);
  btif_to_bta_uuid(uuid, &bt_uuid);

  return do_in_jni_thread(
      Bind(&BTA_GATTS_AppRegister, base::Owned(uuid), &btapp_gatts_cback));
@@ -280,7 +280,7 @@ static bt_status_t btif_gatts_unregister_app(int server_if) {
  return do_in_jni_thread(Bind(&BTA_GATTS_AppDeregister, server_if));
}

static void btif_gatts_open_impl(int server_if, bt_bdaddr_t address,
static void btif_gatts_open_impl(int server_if, const bt_bdaddr_t& address,
                                 bool is_direct, int transport_param) {
  // Ensure device is in inquiry database
  int addr_type = 0;
@@ -322,11 +322,11 @@ static void btif_gatts_open_impl(int server_if, bt_bdaddr_t address,
  BTA_GATTS_Open(server_if, address, is_direct, transport);
}

static bt_status_t btif_gatts_open(int server_if, const bt_bdaddr_t* bd_addr,
static bt_status_t btif_gatts_open(int server_if, const bt_bdaddr_t& bd_addr,
                                   bool is_direct, int transport) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(
      Bind(&btif_gatts_open_impl, server_if, *bd_addr, is_direct, transport));
      Bind(&btif_gatts_open_impl, server_if, bd_addr, is_direct, transport));
}

static void btif_gatts_close_impl(int server_if, const bt_bdaddr_t& address,
@@ -341,11 +341,11 @@ static void btif_gatts_close_impl(int server_if, const bt_bdaddr_t& address,
  BTA_GATTS_CancelOpen(server_if, address, false);
}

static bt_status_t btif_gatts_close(int server_if, const bt_bdaddr_t* bd_addr,
static bt_status_t btif_gatts_close(int server_if, const bt_bdaddr_t& bd_addr,
                                    int conn_id) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(
      Bind(&btif_gatts_close_impl, server_if, *bd_addr, conn_id));
      Bind(&btif_gatts_close_impl, server_if, bd_addr, conn_id));
}

static void add_service_impl(int server_if,
@@ -415,10 +415,10 @@ static void btif_gatts_send_response_impl(int conn_id, int trans_id, int status,

static bt_status_t btif_gatts_send_response(int conn_id, int trans_id,
                                            int status,
                                            btgatt_response_t* response) {
                                            const btgatt_response_t& response) {
  CHECK_BTGATT_INIT();
  return do_in_jni_thread(Bind(&btif_gatts_send_response_impl, conn_id,
                               trans_id, status, *response));
                               trans_id, status, response));
}

static bt_status_t btif_gatts_set_preferred_phy(const bt_bdaddr_t& bd_addr,
+1 −1
Original line number Diff line number Diff line
@@ -199,7 +199,7 @@ static tGATT_CBACK btif_test_callbacks = {btif_test_connect_cback,
 ******************************************************************************/

bt_status_t btif_gattc_test_command_impl(int command,
                                         btgatt_test_params_t* params) {
                                         const btgatt_test_params_t* params) {
  switch (command) {
    case 0x01: /* Enable */
    {
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ bool GattClientFactory::RegisterInstance(const UUID& uuid,
      hal::BluetoothGattInterface::Get()->GetClientHALInterface();
  bt_uuid_t app_uuid = uuid.GetBlueDroid();

  if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS) return false;
  if (hal_iface->register_client(app_uuid) != BT_STATUS_SUCCESS) return false;

  pending_calls_[uuid] = callback;

+2 −2
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ bool GattServer::SendResponse(const std::string& device_address, int request_id,
  bt_status_t result =
      hal::BluetoothGattInterface::Get()
          ->GetServerHALInterface()
          ->send_response(connection->conn_id, request_id, error, &response);
          ->send_response(connection->conn_id, request_id, error, response);
  if (result != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Failed to initiate call to send GATT response";
    return false;
@@ -583,7 +583,7 @@ bool GattServerFactory::RegisterInstance(const UUID& uuid,
      hal::BluetoothGattInterface::Get()->GetServerHALInterface();
  bt_uuid_t app_uuid = uuid.GetBlueDroid();

  if (hal_iface->register_server(&app_uuid) != BT_STATUS_SUCCESS) return false;
  if (hal_iface->register_server(app_uuid) != BT_STATUS_SUCCESS) return false;

  pending_calls_[uuid] = callback;

Loading