Stack Function - btif_transfer_context()

本文基于 AOSP 的 android-11.0.0_r48 分支。

1. Overview

作用:btif 模塊進入 JNI 的入口,一般最終會將消息傳遞到 Framework 層。

  1. btif_transfer_context() 會在 jni_thread 中處理消息
  2. 內(nèi)容的具體執(zhí)行方式如下,調(diào)用 btif_transfer_context() 傳遞以下參數(shù)
btif_transfer_context(p_cback, event, p_params, param_len, p_copy_cback) 

相當于在 jni_thread 中執(zhí)行

p_cback(event, p_params);

2. btif_transfer_context() - Implementation

system/bt/btif/src/btif_core.cc

bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
                                  char* p_params, int param_len,
                                  tBTIF_COPY_CBACK* p_copy_cback) {
  tBTIF_CONTEXT_SWITCH_CBACK* p_msg = (tBTIF_CONTEXT_SWITCH_CBACK*)osi_malloc(
      sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len);

  /* allocate and send message that will be executed in btif context */
  p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
  p_msg->p_cb = p_cback;

  p_msg->event = event; /* callback event */

  /* check if caller has provided a copy callback to do the deep copy */
  if (p_copy_cback) {
    p_copy_cback(event, p_msg->p_param, p_params);
  } else if (p_params) {
    memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
  }

  btif_sendmsg(p_msg);

  return BT_STATUS_SUCCESS;
}
void btif_sendmsg(void* p_msg) {
  do_in_jni_thread(base::Bind(&bt_jni_msg_ready, p_msg));
}
static void bt_jni_msg_ready(void* context) {
  BT_HDR* p_msg = (BT_HDR*)context;

  switch (p_msg->event) {
    case BT_EVT_CONTEXT_SWITCH_EVT:
      btif_context_switched(p_msg);
      break;
    default:
      break;
  }
  osi_free(p_msg);
}
static void btif_context_switched(void* p_msg) {
  tBTIF_CONTEXT_SWITCH_CBACK* p = (tBTIF_CONTEXT_SWITCH_CBACK*)p_msg;

  /* each callback knows how to parse the data */
  if (p->p_cb) p->p_cb(p->event, p->p_param);
}
  • 從以上一系列源碼可知,調(diào)用 btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, char* p_params, int param_len, tBTIF_COPY_CBACK* p_copy_cback) 相當于 在 jni_thread 中執(zhí)行 p_cback(event, p_params)。

btif_transfer_context() 的調(diào)用舉例如下圖,各個模塊調(diào)用它向 btif 上層上報信息。
以 btif_gattc_upstream_up() 為例,介紹該函數(shù)執(zhí)行流程。

btif_transfer_context() 使用舉例

3. btif_gattc_upstream_up() - Flow

system/bt/btif/src/btif_gatt_client.cc

extern const btgatt_callbacks_t* bt_gatt_callbacks;



static void btif_gattc_upstreams_evt(uint16_t event, char* p_param) {

  tBTA_GATTC* p_data = (tBTA_GATTC*)p_param;
  switch (event) {
......
    case BTA_GATTC_OPEN_EVT: {
      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);

      if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu) {
        HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb,
                  p_data->open.conn_id, p_data->open.status, p_data->open.mtu);
      }

      if (p_data->open.status == GATT_SUCCESS)
        btif_gatt_check_encrypted_link(p_data->open.remote_bda,
                                       p_data->open.transport);
      break;
    }
......
  }
}

該函數(shù)會對參數(shù)的 event 進行分發(fā),這里以 BTA_GATTC_OPEN_EVT 消息為例介紹消息向上層傳遞的流程。從源碼可知,主要有3個重要的函數(shù)和參數(shù):

  1. HAL_CBACK
  2. bt_gatt_callbacks
  3. client->open_cb

3.1 HAL_CBACK - Implementation

system/bt/btif/include/btif_common.h

#define HAL_CBACK(P_CB, P_CBACK, ...)                              \
  do {                                                             \
    if ((P_CB) && (P_CB)->P_CBACK) {                               \
      BTIF_TRACE_API("%s: HAL %s->%s", __func__, #P_CB, #P_CBACK); \
      (P_CB)->P_CBACK(__VA_ARGS__);                                \
    } else {                                                       \
      ASSERTC(0, "Callback is NULL", 0);                           \
    }                                                              \
  } while (0)
  1. 第一個參數(shù)為函數(shù)入口或函數(shù)列表的結(jié)構(gòu)體;
  2. 第二個參數(shù)為函數(shù)名;
  3. 第三個及以后的參數(shù),全部作為最終執(zhí)行函數(shù) P_CBACK 的參數(shù)。

3.2 bt_gatt_callbacks 的初始化(賦值)

packages/apps/Bluetooth/jni/com_android_bluetooth_gatt.cpp

static void initializeNative(JNIEnv* env, jobject object) {
......
  sGattIf =
      (btgatt_interface_t*)btIf->get_profile_interface(BT_PROFILE_GATT_ID);
  bt_status_t status = sGattIf->init(&sGattCallbacks);
......
}

以上源碼主要涉及到三部分內(nèi)容:

  1. sGattIf 的初始化
  2. init 的執(zhí)行流程
  3. sGattCallbacks 賦值
3.2.1 sGattIf 的初始化

system/bt/btif/src/bluetooth.cc

static const void* get_profile_interface(const char* profile_id) {
  LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
  if (!interface_ready()) return NULL;
......
  if (is_profile(profile_id, BT_PROFILE_GATT_ID))
    return btif_gatt_get_interface();
  return NULL;
}
const btgatt_interface_t* btif_gatt_get_interface() {
  btgattInterface.scanner = get_ble_scanner_instance();
  btgattInterface.advertiser = get_ble_advertiser_instance();
  return &btgattInterface;                                       // 1   對外入口的獲取函數(shù)
}


static btgatt_interface_t btgattInterface = {                    // 2 對外入口
    .size = sizeof(btgattInterface),

    .init = btif_gatt_init,                         // 外部調(diào)用 init 即調(diào)用 btif_gatt_init
    .cleanup = btif_gatt_cleanup,

    .client = &btgattClientInterface,
    .server = &btgattServerInterface,
    .scanner = nullptr, 
    .advertiser = nullptr 
};
3.2.2 init 的執(zhí)行流程

system/bt/btif/src/btif_gatt.cc

const btgatt_callbacks_t* bt_gatt_callbacks = NULL;



static bt_status_t btif_gatt_init(const btgatt_callbacks_t* callbacks) {
  bt_gatt_callbacks = callbacks;
  return BT_STATUS_SUCCESS;
}
3.2.3 sGattCallbacks 賦值

具體值與類型源碼
system/bt/btif/src/bluetooth.cc

static const btgatt_callbacks_t sGattCallbacks = {
    sizeof(btgatt_callbacks_t), &sGattClientCallbacks, &sGattServerCallbacks,
    &sGattScannerCallbacks,
};
/** BT-GATT callbacks */
typedef struct {
  /** Set to sizeof(btgatt_callbacks_t) */
  size_t size;

  /** GATT Client callbacks */
  const btgatt_client_callbacks_t* client;

  /** GATT Server callbacks */
  const btgatt_server_callbacks_t* server;

  /** LE scanner callbacks */
  const btgatt_scanner_callbacks_t* scanner;
} btgatt_callbacks_t;

具體值與類型源碼
system/bt/btif/src/bluetooth.cc

static const btgatt_client_callbacks_t sGattClientCallbacks = {
    btgattc_register_app_cb,
    btgattc_open_cb,
    btgattc_close_cb,
    btgattc_search_complete_cb,
    btgattc_register_for_notification_cb,
    btgattc_notify_cb,
    btgattc_read_characteristic_cb,
    btgattc_write_characteristic_cb,
    btgattc_read_descriptor_cb,
    btgattc_write_descriptor_cb,
    btgattc_execute_write_cb,
    btgattc_remote_rssi_cb,
    btgattc_configure_mtu_cb,
    btgattc_congestion_cb,
    btgattc_get_gatt_db_cb,
    NULL, /* services_removed_cb */
    NULL, /* services_added_cb */
    btgattc_phy_updated_cb,
    btgattc_conn_updated_cb,
    btgattc_service_changed_cb,
};

system/bt/include/hardware/bt_gatt_client.h

typedef struct {
  register_client_callback register_client_cb;
  connect_callback open_cb;
  disconnect_callback close_cb;
  search_complete_callback search_complete_cb;
  register_for_notification_callback register_for_notification_cb;
  notify_callback notify_cb;
  read_characteristic_callback read_characteristic_cb;
  write_characteristic_callback write_characteristic_cb;
  read_descriptor_callback read_descriptor_cb;
  write_descriptor_callback write_descriptor_cb;
  execute_write_callback execute_write_cb;
  read_remote_rssi_callback read_remote_rssi_cb;
  configure_mtu_callback configure_mtu_cb;
  congestion_callback congestion_cb;
  get_gatt_db_callback get_gatt_db_cb;
  services_removed_callback services_removed_cb;
  services_added_callback services_added_cb;
  phy_updated_callback phy_updated_cb;
  conn_updated_callback conn_updated_cb;
  service_changed_callback service_changed_cb;
} btgatt_client_callbacks_t;

從以上兩個具體值與類型源碼可知:client ->open_cb 即為 btgattc_open_cb。

3.3 client->open_cb 即 btgattc_open_cb

void btgattc_open_cb(int conn_id, int status, int clientIf,
                     const RawAddress& bda) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;

  ScopedLocalRef<jstring> address(sCallbackEnv.get(),
                                  bdaddr2newjstr(sCallbackEnv.get(), &bda));
  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onConnected, clientIf,
                               conn_id, status, address.get());
}

packages/apps/Bluetooth/src/com/android/bluetooth/gatt/GattService.java

    void onConnected(int clientIf, int connId, int status, String address) throws RemoteException {
        if (status == 0) {
            mClientMap.addConnection(clientIf, connId, address);
            synchronized (mPermits) {
                mPermits.putIfAbsent(address, new AtomicBoolean(true));
            }
        }
        ClientMap.App app = mClientMap.getById(clientIf);
        if (app != null) {
            app.callback.onClientConnectionState(status, clientIf,
                    (status == BluetoothGatt.GATT_SUCCESS), address);
        }
    }

至此,消息傳遞到 Framework 層,F(xiàn)ramework 層如何處理該消息此處不分析。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容