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

Commit 56f33628 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

BTA Application registration refactor

This patch replace BTA state machine state associated with registration
of new application, together with BTA_GATTC_API_REG_EVT, and
tBTA_GATTC_API_REG. Instead, it uses closure to post registration task,
and callback for registration event.

Bug: 30622771
Test: sl4a ConcurrentBleAdvertiserTest GattConnectTest
Change-Id: I62d68485170ef5472237d62b285353f2c9bc8250
parent 692219b7
Loading
Loading
Loading
Loading
+12 −14
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@
#define LOG_TAG "bt_bta_dm"

#include <assert.h>
#include <base/bind.h>
#include <base/callback.h>
#include <string.h>

#include "bt_common.h"
@@ -4749,11 +4751,15 @@ void bta_dm_ble_get_energy_info(tBTA_DM_MSG* p_data) {
 *
 ******************************************************************************/
static void bta_dm_gattc_register(void) {
  tBT_UUID app_uuid = {LEN_UUID_128, {0}};

  if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
    memset(&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
    BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
    BTA_GATTC_AppRegister(bta_dm_gattc_callback,
                          base::Bind([](uint8_t client_id, uint8_t status) {
                            if (status == BTA_GATT_OK)
                              bta_dm_search_cb.client_if = client_id;
                            else
                              bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;

                          }));
  }
}

@@ -4900,7 +4906,8 @@ static void bta_dm_gatt_disc_complete(uint16_t conn_id,
 *
 * Function         bta_dm_close_gatt_conn
 *
 * Description      This function close the GATT connection after delay timeout.
 * Description      This function close the GATT connection after delay
 *timeout.
 *
 * Parameters:
 *
@@ -5004,15 +5011,6 @@ static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
  APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);

  switch (event) {
    case BTA_GATTC_REG_EVT:
      APPL_TRACE_DEBUG("BTA_GATTC_REG_EVT client_if = %d",
                       p_data->reg_oper.client_if);
      if (p_data->reg_oper.status == BTA_GATT_OK)
        bta_dm_search_cb.client_if = p_data->reg_oper.client_if;
      else
        bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
      break;

    case BTA_GATTC_OPEN_EVT:
      bta_dm_proc_open_evt(&p_data->open);
      break;
+8 −18
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@

#include <string.h>

#include <base/callback.h>
#include "bt_common.h"
#include "bt_target.h"
#include "bta_gattc_int.h"
@@ -157,22 +158,18 @@ void bta_gattc_disable() {
 * Returns          void
 *
 ******************************************************************************/
void bta_gattc_register(tBTA_GATTC_DATA* p_data) {
  tBTA_GATTC cb_data;
  uint8_t i;
  tBT_UUID* p_app_uuid = &p_data->api_reg.app_uuid;
void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_cback,
                        BtaAppRegisterCallback cb) {
  tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;

  uint8_t client_if = 0;
  APPL_TRACE_DEBUG("bta_gattc_register state %d", bta_gattc_cb.state);
  memset(&cb_data, 0, sizeof(cb_data));
  cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;

  /* check if  GATTC module is already enabled . Else enable */
  if (bta_gattc_cb.state == BTA_GATTC_STATE_DISABLED) {
    bta_gattc_enable();
  }
  /* todo need to check duplicate uuid */
  for (i = 0; i < BTA_GATTC_CL_MAX; i++) {
  for (uint8_t i = 0; i < BTA_GATTC_CL_MAX; i++) {
    if (!bta_gattc_cb.cl_rcb[i].in_use) {
      if ((p_app_uuid == NULL) ||
          (bta_gattc_cb.cl_rcb[i].client_if =
@@ -181,11 +178,11 @@ void bta_gattc_register(tBTA_GATTC_DATA* p_data) {
        status = BTA_GATT_ERROR;
      } else {
        bta_gattc_cb.cl_rcb[i].in_use = true;
        bta_gattc_cb.cl_rcb[i].p_cback = p_data->api_reg.p_cback;
        bta_gattc_cb.cl_rcb[i].p_cback = p_cback;
        memcpy(&bta_gattc_cb.cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));

        /* BTA use the same client interface as BTE GATT statck */
        cb_data.reg_oper.client_if = bta_gattc_cb.cl_rcb[i].client_if;
        client_if = bta_gattc_cb.cl_rcb[i].client_if;

        tBTA_GATTC_INT_START_IF* p_buf = (tBTA_GATTC_INT_START_IF*)osi_malloc(
            sizeof(tBTA_GATTC_INT_START_IF));
@@ -199,14 +196,7 @@ void bta_gattc_register(tBTA_GATTC_DATA* p_data) {
    }
  }

  /* callback with register event */
  if (p_data->api_reg.p_cback) {
    if (p_app_uuid != NULL)
      memcpy(&(cb_data.reg_oper.app_uuid), p_app_uuid, sizeof(tBT_UUID));

    cb_data.reg_oper.status = status;
    (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC*)&cb_data);
  }
  if (!cb.is_null()) cb.Run(client_if, status);
}
/*******************************************************************************
 *
+22 −23
Original line number Diff line number Diff line
@@ -26,8 +26,11 @@

#include <string.h>

#include <base/bind.h>
#include <base/bind_helpers.h>
#include <base/callback.h>
#include "bt_common.h"
#include "bta_closure_api.h"
#include "bta_gatt_api.h"
#include "bta_gattc_int.h"
#include "bta_sys.h"
@@ -63,33 +66,29 @@ void BTA_GATTC_Disable(void) {
  bta_sys_deregister(BTA_ID_GATTC);
}

/*******************************************************************************
 *
 * Function         BTA_GATTC_AppRegister
 *
 * Description      This function is called to register application callbacks
 *                    with BTA GATTC module.
 *
 * Parameters       p_app_uuid - applicaiton UUID
 *                  p_client_cb - pointer to the application callback function.
 *
 * Returns          None
 *
 ******************************************************************************/
void BTA_GATTC_AppRegister(tBT_UUID* p_app_uuid,
                           tBTA_GATTC_CBACK* p_client_cb) {
  tBTA_GATTC_API_REG* p_buf =
      (tBTA_GATTC_API_REG*)osi_malloc(sizeof(tBTA_GATTC_API_REG));
static void create_random_uuid(tBT_UUID* uuid) {
  uuid->len = LEN_UUID_128;

  for (int i = 0; i < 16; ++i) {
    uuid->uu.uuid128[i] = (uint8_t)(rand() % 256);
  }
}

/**
 * This function is called to register application callbacks with BTA GATTC
 * module. |client_cb| pointer to the application callback function.
 * |cb| one time callback when registration is finished
 */
void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
                           BtaAppRegisterCallback cb) {
  if (bta_sys_is_register(BTA_ID_GATTC) == false)
    bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);

  p_buf->hdr.event = BTA_GATTC_API_REG_EVT;
  if (p_app_uuid != NULL)
    memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
  p_buf->p_cback = p_client_cb;

  bta_sys_sendmsg(p_buf);
  // base::Owned will own and free app_uuid
  tBT_UUID* uuid = new tBT_UUID;
  create_random_uuid(uuid);
  do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_register, base::Owned(uuid),
                                         p_client_cb, std::move(cb)));
}

/*******************************************************************************
+2 −9
Original line number Diff line number Diff line
@@ -60,7 +60,6 @@ enum {
  BTA_GATTC_INT_DISCONN_EVT,

  BTA_GATTC_INT_START_IF_EVT,
  BTA_GATTC_API_REG_EVT,
  BTA_GATTC_API_DEREG_EVT,
  BTA_GATTC_API_DISABLE_EVT,
  BTA_GATTC_ENC_CMPL_EVT
@@ -88,12 +87,6 @@ typedef uint16_t tBTA_GATTC_INT_EVT;
#define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE

/* internal strucutre for GATTC register API  */
typedef struct {
  BT_HDR hdr;
  tBT_UUID app_uuid;
  tBTA_GATTC_CBACK* p_cback;
} tBTA_GATTC_API_REG;

typedef struct {
  BT_HDR hdr;
  tBTA_GATTC_IF client_if;
@@ -186,7 +179,6 @@ typedef struct {

typedef union {
  BT_HDR hdr;
  tBTA_GATTC_API_REG api_reg;
  tBTA_GATTC_API_DEREG api_dereg;
  tBTA_GATTC_API_OPEN api_conn;
  tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn;
@@ -362,7 +354,8 @@ extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event,

/* function processed outside SM */
extern void bta_gattc_disable();
extern void bta_gattc_register(tBTA_GATTC_DATA* p_data);
extern void bta_gattc_register(tBT_UUID* p_app_uuid, tBTA_GATTC_CBACK* p_data,
                               BtaAppRegisterCallback cb);
extern void bta_gattc_start_if(tBTA_GATTC_DATA* p_data);
extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg);
extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg);
+0 −6
Original line number Diff line number Diff line
@@ -372,10 +372,6 @@ bool bta_gattc_hdl_event(BT_HDR* p_msg) {
      bta_gattc_disable();
      break;

    case BTA_GATTC_API_REG_EVT:
      bta_gattc_register((tBTA_GATTC_DATA*)p_msg);
      break;

    case BTA_GATTC_INT_START_IF_EVT:
      bta_gattc_start_if((tBTA_GATTC_DATA*)p_msg);
      break;
@@ -473,8 +469,6 @@ static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) {
      return "BTA_GATTC_INT_DISCONN_EVT";
    case BTA_GATTC_INT_START_IF_EVT:
      return "BTA_GATTC_INT_START_IF_EVT";
    case BTA_GATTC_API_REG_EVT:
      return "BTA_GATTC_API_REG_EVT";
    case BTA_GATTC_API_DEREG_EVT:
      return "BTA_GATTC_API_DEREG_EVT";
    case BTA_GATTC_API_REFRESH_EVT:
Loading