您的位置:首页 > 产品设计 > UI/UE

Android4.2关于bluetooth在HAL层的分析(1)

2014-05-05 19:19 459 查看
1.一些常用的数据结构
hardware/libhardware/include/hardware.h中
定义了三个重要的结构:

struct hw_module_t; //模块类型

struct hw_module_methods_t; //模块方法

struct hw_device_t; //设备类型

hw_module_t中包含结构hw_module_methods_t

hw_module_methods_t只有一个唯一的Open方法,如下:

int (*open)(const struct hw_module_t* module, const char* id,struct
hw_device_t** device);


struct hw_device_t中包含hw_module_t结构和一个close方法如下:

int (*close)(struct hw_device_t* device);

这几个结构十分关键,是HAL层向上暴露的接口。

2.定义蓝牙HAL层结构

在hardware/libhardware/include/bluetooth.h中定义以下关键结构
首先是定义的蓝牙模块ID

#define BT_HARDWARE_MODULE_ID "bluetooth"
#define BT_STACK_MODULE_ID "bluetooth"
#define BT_STACK_TEST_MODULE_ID "bluetooth_test"

其次是定义的蓝牙硬件设备接口
//要求自定义xxx_device_t的第一个成员必须是hw_device_t类型,其次才是其它的一些接口信息.

typedef struct {
struct
hw_device_t common;

const bt_interface_t*
(*get_bluetooth_interface)();

} bluetooth_device_t;

除了这个结构,还必须定义关hw_module_t类型的实例HAL_MODULE_INFO_SYM。这个结构在Bluedroid的bluetooth.c中定义。
//变量名必须为HAL_MODULE_INFO_SYM,这是强制要求的,你要写Android的HAL就得遵循这个游戏规则

struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = BT_HARDWARE_MODULE_ID,
.name = "Bluetooth Stack",
.author = "The Android Open Source Project",
.methods = &bt_stack_module_methods
};
定义的bt_stack_module_methods如下:
static int open_bluetooth_stack (const struct hw_module_t* module, char const* name, struct hw_device_t**
abstraction)

{
bluetooth_device_t *stack = malloc(sizeof(bluetooth_device_t) );
memset(stack, 0, sizeof(bluetooth_device_t) );
stack->common.tag = HARDWARE_DEVICE_TAG;
stack->common.version = 0;
stack->common.module = (struct hw_module_t*)module;
stack->common.close = close_bluetooth_stack;
stack->get_bluetooth_interface = bluetooth__get_bluetooth_interface;
*abstraction = (struct hw_device_t*)stack; //GJ:强制转换,bluetooth_device_t的第一个成员为hw_device_t,传到上层
return 0;
}
static struct hw_module_methods_t bt_stack_module_methods =
{
.open = open_bluetooth_stack,
};

在/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中实现的是bluetooth的JNI代码,是上层Java与下层Bluedroid的接口。
在static void classInitNative(JNIEnv* env, jclass clazz)函数中有以下部分code:

char value[PROPERTY_VALUE_MAX];
property_get("bluetooth.mock_stack", value, ""); //
const char *id = (strcmp(value, "1")? BT_STACK_MODULE_ID : BT_STACK_TEST_MODULE_ID);
//JNI获取HAL层模块的通用方式,根据模块ID获得hw_module_t对象
err = hw_get_module(id, (hw_module_t const**)&module);
if (err == 0)
{
hw_device_t* abstraction;
err = module->methods->open(module, id, &abstraction);
if (err == 0)
{
//强制转换,bluetooth_module_t的第一个成员为abstraction
bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
sBluetoothInterface = btStack->get_bluetooth_interface(); //GJ:JAVA层的sBuletooth获得bluedroid中实现的bluetoothInterface
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: