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


    正文 字体大小:

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

    (2013-09-10 14:58:58)
    标签:

    hal

    bluedroid

    android

    jni

    分类: Bluetooth
    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
            } 
     
     

    0

    阅读(363) 评论 (0) 收藏(0) 转载(0) 喜欢 打印举报
    已投稿到:
  • 相关阅读:
    php 高级 提高PHP代码的性能10条建议
    CSRF预防手段
    如何在PHP中防止SQL注入
    网络安全 如何防范 XSS 攻击
    php 算法知识 冒泡排序
    php 基础知识 常见面试题
    MySQL高级 InnoDB 和 MyISAM 的区别
    php 算法知识 猴子选大王
    MySQL高级-索引1
    [POI2007]POW-The Flood(并查集)
  • 原文地址:https://www.cnblogs.com/jeanschen/p/3535669.html
Copyright © 2020-2023  润新知