本文基于 AOSP 的 android-11.0.0_r48 分支。
1. Overview
作用:btif 模块进入 JNI 的入口,一般最终会将消息传递到 Framework 层。
- btif_transfer_context() 会在 jni_thread 中处理消息
- 内容的具体执行方式如下,调用 btif_transfer_context() 传递以下参数
btif_transfer_context(p_cback, event, p_params, param_len, p_copy_cback)
相当于在 jni_thread 中执行
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);
}
- 从以上一系列源码可知,调用 btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, char* p_params, int param_len, tBTIF_COPY_CBACK* p_copy_cback) 相当于 在 jni_thread 中执行 p_cback(event, p_params)。
btif_transfer_context() 的调用举例如下图,各个模块调用它向 btif 上层上报信息。
以 btif_gattc_upstream_up() 为例,介绍该函数执行流程。
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;
}
......
}
}
该函数会对参数的 event 进行分发,这里以 BTA_GATTC_OPEN_EVT 消息为例介绍消息向上层传递的流程。从源码可知,主要有3个重要的函数和参数:
- HAL_CBACK
- bt_gatt_callbacks
- 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)
- 第一个参数为函数入口或函数列表的结构体;
- 第二个参数为函数名;
- 第三个及以后的参数,全部作为最终执行函数 P_CBACK 的参数。
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);
......
}
以上源码主要涉及到三部分内容:
- sGattIf 的初始化
- init 的执行流程
- 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 对外入口的获取函数
}
static btgatt_interface_t btgattInterface = { // 2 对外入口
.size = sizeof(btgattInterface),
.init = btif_gatt_init, // 外部调用 init 即调用 btif_gatt_init
.cleanup = btif_gatt_cleanup,
.client = &btgattClientInterface,
.server = &btgattServerInterface,
.scanner = nullptr,
.advertiser = nullptr
};
3.2.2 init 的执行流程
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 层,Framework 层如何处理该消息此处不分析。