日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Android中HAL如何向上層提供接口總結(jié)

系統(tǒng) 2124 0

參考文獻(xiàn):

http://blog.csdn.net/luoshengyang/article/details/6573809

http://blog.csdn.net/hongtao_liu/article/details/6060734

建議閱讀本文時(shí)先瀏覽以上兩篇文章,本文是對(duì)上兩篇文章在HAL對(duì)上層接口話題的一個(gè)總結(jié).

1 什么是HAL

HAL的全稱是Hardware Abstraction Layer,即硬件抽象層.其架構(gòu)圖如下:

Android中HAL如何向上層提供接口總結(jié)

Android的HAL是為了保護(hù)一些硬件提供商的知識(shí)產(chǎn)權(quán)而提出的,是為了避開linux的GPL束縛。思路是把控制硬件的動(dòng)作都放到了Android HAL中,而linux driver僅僅完成一些簡(jiǎn)單的數(shù)據(jù)交互作用,甚至把硬件寄存器空間直接映射到user space。而Android是基于Aparch的license,因此硬件廠商可以只提供二進(jìn)制代碼,所以說Android只是一個(gè)開放的平臺(tái),并不是一個(gè)開源的平臺(tái)。也許也正是因?yàn)锳ndroid不遵從GPL,所以Greg Kroah-Hartman才在2.6.33內(nèi)核將Andorid驅(qū)動(dòng)從linux中刪除。GPL和硬件廠商目前還是有著無法彌合的裂痕。Android想要把這個(gè)問題處理好也是不容易的。

總結(jié)下來,Android HAL存在的原因主要有:

1. 并不是所有的硬件設(shè)備都有標(biāo)準(zhǔn)的linux kernel的接口

2. KERNEL DRIVER涉及到GPL的版權(quán)。某些設(shè)備制造商并不原因公開硬件驅(qū)動(dòng),所以才去用HAL方式繞過GPL。

3. 針對(duì)某些硬件,Android有一些特殊的需求.

2 與接口相關(guān)的幾個(gè)結(jié)構(gòu)體

首先來看三個(gè)與HAL對(duì)上層接口有關(guān)的幾個(gè)結(jié)構(gòu)體:

    struct hw_module_t;       //模塊類型
struct hw_module_methods_t;      //模塊方法
struct hw_device_t;              //設(shè)備類型
  
這幾個(gè)數(shù)據(jù)結(jié)構(gòu)是在Android工作目錄/hardware/libhardware/include/hardware/hardware.h文件中定義.

3 解釋

一般來說,在寫HAL相關(guān)代碼時(shí)都得包含這個(gè)hardware.h頭文件,所以有必要先了解一下這個(gè)頭文件中的內(nèi)容.

    /*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H
#define ANDROID_INCLUDE_HARDWARE_HARDWARE_H

#include <stdint.h>
#include <sys/cdefs.h>

#include <cutils/native_handle.h>
#include <system/graphics.h>

__BEGIN_DECLS

/*
 * Value for the hw_module_t.tag field
 */

#define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))

#define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
#define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')

struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;

/**
 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 * and the fields of this data structure must begin with hw_module_t
 * followed by module specific information.
 */
//每一個(gè)硬件模塊都每必須有一個(gè)名為HAL_MODULE_INFO_SYM的數(shù)據(jù)結(jié)構(gòu)變量,它的第一個(gè)成員的類型必須為hw_module_t
typedef struct hw_module_t {
    /** tag must be initialized to HARDWARE_MODULE_TAG */
    uint32_t tag;

    /** major version number for the module */
    uint16_t version_major;

    /** minor version number of the module */
    uint16_t version_minor;

    /** Identifier of module */
    const char *id;

    /** Name of this module */
    const char *name;

    /** Author/owner/implementor of the module */
    const char *author;

    /** Modules methods */
    //模塊方法列表,指向hw_module_methods_t*
    struct hw_module_methods_t* methods;

    /** module's dso */
    void* dso;

    /** padding to 128 bytes, reserved for future use */
    uint32_t reserved[32-7];

} hw_module_t;

typedef struct hw_module_methods_t {                 //硬件模塊方法列表的定義,這里只定義了一個(gè)open函數(shù)
    /** Open a specific device */
    int (*open)(const struct hw_module_t* module, const char* id, //注意這個(gè)open函數(shù)明確指出第三個(gè)參數(shù)的類型為struct hw_device_t**
            struct hw_device_t** device);
} hw_module_methods_t;

/**
 * Every device data structure must begin with hw_device_t
 * followed by module specific public methods and attributes.
 */
//每一個(gè)設(shè)備數(shù)據(jù)結(jié)構(gòu)的第一個(gè)成員函數(shù)必須是hw_device_t類型,其次才是各個(gè)公共方法和屬性
typedef struct hw_device_t {
    /** tag must be initialized to HARDWARE_DEVICE_TAG */
    uint32_t tag;

    /** version number for hw_device_t */
    uint32_t version;

    /** reference to the module this device belongs to */
    struct hw_module_t* module;

    /** padding reserved for future use */
    uint32_t reserved[12];

    /** Close this device */
    int (*close)(struct hw_device_t* device);

} hw_device_t;

/**
 * Name of the hal_module_info
 */
#define HAL_MODULE_INFO_SYM         HMI

/**
 * Name of the hal_module_info as a string
 */
#define HAL_MODULE_INFO_SYM_AS_STR  "HMI"

/**
 * Get the module info associated with a module by id.
 *
 * @return: 0 == success, <0 == error and *module == NULL
 */
int hw_get_module(const char *id, const struct hw_module_t **module);

/**
 * Get the module info associated with a module instance by class 'class_id'
 * and instance 'inst'.
 *
 * Some modules types necessitate multiple instances. For example audio supports
 * multiple concurrent interfaces and thus 'audio' is the module class
 * and 'primary' or 'a2dp' are module interfaces. This implies that the files
 * providing these modules would be named audio.primary.<variant>.so and
 * audio.a2dp.<variant>.so
 *
 * @return: 0 == success, <0 == error and *module == NULL
 */
int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module);

__END_DECLS

#endif  /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */

  

由以上內(nèi)容可以看出(typedef struct hw_module_t ,typedef struct hw_device_t),如果我們要寫一個(gè)自定義設(shè)備的驅(qū)動(dòng)的HAL層時(shí),我們得首先自定義兩個(gè)數(shù)據(jù)結(jié)構(gòu):

假設(shè)我們要做的設(shè)備名為XXX:

在頭文件中定義:XXX.h

    /*定義模塊ID*/
#define XXX_HARDWARE_MODULE_ID "XXX"

/*硬件模塊結(jié)構(gòu)體*/
//見hardware.h中的hw_module_t定義的說明,xxx_module_t的第一個(gè)成員必須是hw_module_t類型,其次才是模塊的一此相關(guān)信息,當(dāng)然也可以不定義,
//這里就沒有定義模塊相關(guān)信息
struct xxx_module_t {
	struct hw_module_t common;
};

/*硬件接口結(jié)構(gòu)體*/
//見hardware.h中的hw_device_t的說明,要求自定義xxx_device_t的第一個(gè)成員必須是hw_device_t類型,其次才是其它的一些接口信息. 
struct xxx_device_t {
	struct hw_device_t common;
        //以下成員是HAL對(duì)上層提供的接口或一些屬性
       int fd;
	int (*set_val)(struct xxx_device_t* dev, int val);
	int (*get_val)(struct xxx_device_t* dev, int* val);
};
  
注:特別注意xxx_device_t的結(jié)構(gòu)定義,這個(gè)才是HAL向上層提供接口函數(shù)的數(shù)據(jù)結(jié)構(gòu),其成員就是我們想要關(guān)心的接口函數(shù).

接下來我們?cè)趯?shí)現(xiàn)文件XXX.c文件中定義一個(gè)xxx_module_t的變量:

    /*模塊實(shí)例變量*/
struct xxx_module_t HAL_MODULE_INFO_SYM = {    //變量名必須為HAL_MODULE_INFO_SYM,這是強(qiáng)制要求的,你要寫Android的HAL就得遵循這個(gè)游戲規(guī)則,
                                               //見hardware.h中的hw_module_t的類型信息說明.
       common: {
		tag: HARDWARE_MODULE_TAG,
		version_major: 1,
		version_minor: 0,
		id: XXX_HARDWARE_MODULE_ID,    //頭文件中有定義
		name: MODULE_NAME,
		author: MODULE_AUTHOR,
		methods: &xxx_module_methods,  //模塊方法列表,在本地定義
	}
};
  

注意到上面有HAL_MODULE_INFO_SYM變量的成員common中包含一個(gè)函數(shù)列表xxx_module_methods,而這個(gè)成員函數(shù)列表是在本地自定義的。那么這個(gè)成員函數(shù)列是不是就是HAL向上層提供函數(shù)的地方呢?很失望,不是在這里,前面我們已經(jīng)說過了,是在xxx_device_t中定義的,這個(gè)xxx_module_methods實(shí)際上只提供了一個(gè)open函數(shù),就相當(dāng)于只提供了一個(gè)模塊初始化函數(shù).其定義如下:

    /*模塊方法表*/
static struct hw_module_methods_t xxx_module_methods = {
	open: xxx_device_open
};
  
注意到,上邊的函數(shù)列表中只列出了一個(gè)xxx_device_open函數(shù),這個(gè)函數(shù)也是需要在本地實(shí)現(xiàn)的一個(gè)函數(shù)。前面說過,這個(gè)函數(shù)只相當(dāng)于模塊初始化函數(shù)。

那么HAL又到底是怎么將xxx_device_t中定義的接口提供到上層去的呢?

且看上面這個(gè)函數(shù)列表中唯一的一個(gè)xxx_device_open的定義:

    static int xxx_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
	struct xxx_device_t* dev;
	dev = (struct hello_device_t*)malloc(sizeof(struct xxx_device_t));//動(dòng)態(tài)分配空間
	
	if(!dev) {
		LOGE("Hello Stub: failed to alloc space");
		return -EFAULT;
	}

	memset(dev, 0, sizeof(struct xxx_device_t));
        //對(duì)dev->common的內(nèi)容賦值,
       dev->common.tag = HARDWARE_DEVICE_TAG;
	dev->common.version = 0;
	dev->common.module = (hw_module_t*)module;
	dev->common.close = xxx_device_close;
        //對(duì)dev其它成員賦值
       dev->set_val = xxx_set_val;
	dev->get_val = xxx_get_val;

	if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
		LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));
		free(dev);
		return -EFAULT;
	}
        
        //輸出&(dev->common),輸出的并不是dev,而是&(dev->common)!(common內(nèi)不是只包含了一個(gè)close接口嗎?)
	*device = &(dev->common);
	LOGI("Hello Stub: open /dev/hello successfully.");

	return 0;
}
  
經(jīng)驗(yàn)告訴我們,一般在進(jìn)行模塊初始化的時(shí)候,模塊的接口函數(shù)也會(huì)“注冊(cè)”,上面是模塊初始化函數(shù),那么接口注冊(cè)在哪?于是我們找到*device =&(dev->common);這行代碼,可問題是,這樣一來,返回給調(diào)用者不是&(dev->common)嗎?而這個(gè)dev->common僅僅只包含了一個(gè)模塊關(guān)閉接口!到底怎么回事?為什么不直接返回dev,dev下不是提供所有HAL向上層接口嗎?

在回答上述問題之前,讓我們先看一下這xxx_device_open函數(shù)原型,還是在hardware.h頭文件中,找到下面幾行代碼:

    typedef struct hw_module_methods_t {
    /** Open a specific device */
    int (*open)(const struct hw_module_t* module, const char* id,
            struct hw_device_t** device);

} hw_module_methods_t;
  
這是方法列表的定義,明確要求了方法列表中有且只一個(gè)open方法,即相當(dāng)于模塊初始化方法,且,這個(gè)方法的第三個(gè)參數(shù)明確指明了類型是struct hw_device_t **,而不是用戶自定義的xxx_device_t,這也就是解譯了在open函數(shù)實(shí)現(xiàn)內(nèi)為什么輸出的是&(dev->common)而不是dev了,原來返回的類型在hardware.h中的open函數(shù)原型中明確指出只能返回hw_device_t類型.

可是,dev->common不是只包含close接口嗎?做為HAL的上層,它又是怎么"看得到"HAL提供的全部接口的呢?

接下來,讓我們來看看做為HAL上層,它又是怎么使用由HAL返回的dev->common的:

參考: 在Ubuntu為Android硬件抽象層(HAL)模塊編寫JNI方法提供Java訪問硬件服務(wù)接口 這篇文章,從中可以看到這么幾行代碼:

    /*通過硬件抽象層定義的硬件模塊打開接口打開硬件設(shè)備*/  
static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {  
     return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);  
}  
  
由此可見,返回的&(dev->common)最終會(huì)返回給struce hello_device_t **類型的輸出變量device,換句話說,類型為hw_device_t的dev->common在初始化函數(shù)open返回后,會(huì)強(qiáng)制轉(zhuǎn)化為xxx_device_t來使用,終于明白了,原來如此!另外,在hardware.h中對(duì)xxx_device_t類型有說明,要求它的 第一個(gè)成員的類型必須是 hw_device_t,原來是為了HAL上層使用時(shí)的強(qiáng)制轉(zhuǎn)化的目的,如果xxx_device_t的第一個(gè)成員類型不是hw_device_t,那么HAL上層使用中強(qiáng)制轉(zhuǎn)化就沒有意義了,這個(gè)時(shí)候,就真的“看不到”HAL提供的接口了.


此外,在hardware.h頭文件中,還有明確要求定義xxx_module_t類型時(shí),明確要求 第一個(gè)成員變量類型必須為 hw_module_t,這也是為了方便找到其第一個(gè)成員變量common,進(jìn)而找到本地定義的方法列表,從而調(diào)用open函數(shù)進(jìn)行模塊初始化.


綜上所述,HAL是通過 struct xxx_device_t 這個(gè)結(jié)構(gòu)體向上層提供接口的.

即:接口包含在 struct xxx_device_t 這個(gè)結(jié)構(gòu)體內(nèi)。

而具體執(zhí)行是通過struct xxx_module_t HAL_MODULE_INFO_SYM這個(gè)結(jié)構(gòu)體變量的函數(shù)列表成員下的open函數(shù)來返回給上層的.

Android中HAL如何向上層提供接口總結(jié)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!??!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 石楼县| 法库县| 隆尧县| 南和县| 邯郸市| 永新县| 玉屏| 崇仁县| 新源县| 囊谦县| 镇平县| 固原市| 修文县| 昭觉县| 石首市| 曲麻莱县| 古交市| 灵璧县| 兴山县| 浦城县| 化德县| 鄂尔多斯市| 梁河县| 墨玉县| 鞍山市| 五指山市| 大城县| 晋州市| 高密市| 乌鲁木齐市| 阿图什市| 汉沽区| 册亨县| 稷山县| 临桂县| 利川市| 娱乐| 苍南县| 商河县| 仁寿县| 台东市|