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
}
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
}
相关文章推荐
- Bluetooth Security Manager
- UVA 12424 Answering Queries on a Tree (树链剖分)
- VC调试模式下does not contain debug information. Press OK to Continue
- enctype="multipart/form-data 无法通过request得到值
- NGUI的UITexture和UISprite的比较【NGUI3.5.8】
- 山东省第四届ACM Mountain Subsequences 解题报告
- uint8_t / uint16_t / uint32_t /uint64_t 的简单介绍(转载)
- EasyUI DataGrid根据字段动态合并单元格
- 为什么很多内置类如UITableViewController的delegate属性都是assign而不是retain的
- ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别
- LeetCode – Distinct Subsequences Total (Java)
- 关于UITableView的Cell复用谈谈的一些心得
- DataTable - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
- quickeys 过期解决办法
- ios学习记录 day44 UI20 Storyboard 高级可视化编程 + 单例
- 使用GPUImage渲染图片教程
- rabbitmq Access Control,quest用户不能远程访问
- uitableview 内的 uitextField 获得焦点后,显示键盘和table的frame变化,使uitextField不被遮挡
- 触摸textField以外的地方,键盘回收(两种方法)
- 怎么理解Stringbuffer是线程安全的stringbuilder是线程不安全的