主要介绍一下从bluetooth service 到 bluetooth stack 到 hci layer 到 vendor层 bluetooth enable 的程序流程,有不对之处,敬请指正。
程序流程图:
1. 加载 bluetooth.default.so
static void classInitNative(JNIEnv* env, jclass clazz) {
int err;
hw_module_t* module;
......
err = hw_get_module(id, (hw_module_t const**)&module);
// 加载/system/lib/hw/或者/system/lib64/hw 目录下bluetooth.default.so
if (err == 0) {
hw_device_t* abstraction;
err = module->methods->open(module, id, &abstraction);
if (err == 0) {
bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
sBluetoothInterface = btStack->get_bluetooth_interface();
} else {
ALOGE("Error while opening Bluetooth library");
}
} else {
ALOGE("No Bluetooth Library found");
}
}
2. 设置bluetooth interface
static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
static bluetooth_device_t device = {
.common = {
.tag = HARDWARE_DEVICE_TAG,
.version = 0,
.close = close_bluetooth_stack,
},
.get_bluetooth_interface = bluetooth__get_bluetooth_interface
};
device.common.module = (struct hw_module_t *)module;
*abstraction = (struct hw_device_t *)&device;
return 0;
}
二. initNative
1. init stack
static void init_stack(void) {
semaphore_t *semaphore = semaphore_new(0);
thread_post(management_thread, event_init_stack, semaphore);
semaphore_wait(semaphore);
semaphore_free(semaphore);
}
2. 获取hal层,vendor层interface
获取read_data, transmit_data接口,实现hci层和vendor device的通信
const hci_t *hci_layer_get_interface() {
buffer_allocator = buffer_allocator_get_interface();
hal = hci_hal_get_interface();
btsnoop = btsnoop_get_interface();
hci_inject = hci_inject_get_interface();
packet_fragmenter = packet_fragmenter_get_interface();
vendor = vendor_get_interface();
low_power_manager = low_power_manager_get_interface();
init_layer_interface();
return &interface;
}
三. enableNative
1. 加载libbt-vendor.so, 并初始化
static bool vendor_open(
const uint8_t *local_bdaddr,
const hci_t *hci_interface) {
assert(lib_handle == NULL);
hci = hci_interface;
lib_handle = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
......
lib_interface = (bt_vendor_interface_t *)dlsym(lib_handle, VENDOR_LIBRARY_SYMBOL_NAME);
......
int status = lib_interface->init(&lib_callbacks, (unsigned char *)local_bdaddr);
}
......
}
2. hci layer发送VENDOR_CHIP_POWER_CONTROL 消息
vendor->send_command(VENDOR_CHIP_POWER_CONTROL, &power_state);
power up bluetooth device
3. hal_open, 打开/dev/smd3, smd2的设备节点
***
opcode defined in:
system/bt/stack/include/hcidefs.h