亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 2114 0

參考文獻:

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

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

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

1 什么是HAL

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

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

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

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

1. 并不是所有的硬件設(shè)備都有標準的linux kernel的接口

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

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

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

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

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

3 解釋

一般來說,在寫HAL相關(guān)代碼時都得包含這個hardware.h頭文件,所以有必要先了解一下這個頭文件中的內(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.
 */
//每一個硬件模塊都每必須有一個名為HAL_MODULE_INFO_SYM的數(shù)據(jù)結(jié)構(gòu)變量,它的第一個成員的類型必須為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 {                 //硬件模塊方法列表的定義,這里只定義了一個open函數(shù)
    /** Open a specific device */
    int (*open)(const struct hw_module_t* module, const char* id, //注意這個open函數(shù)明確指出第三個參數(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.
 */
//每一個設(shè)備數(shù)據(jù)結(jié)構(gòu)的第一個成員函數(shù)必須是hw_device_t類型,其次才是各個公共方法和屬性
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),如果我們要寫一個自定義設(shè)備的驅(qū)動的HAL層時,我們得首先自定義兩個數(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的第一個成員必須是hw_module_t類型,其次才是模塊的一此相關(guān)信息,當然也可以不定義,
//這里就沒有定義模塊相關(guān)信息
struct xxx_module_t {
	struct hw_module_t common;
};

/*硬件接口結(jié)構(gòu)體*/
//見hardware.h中的hw_device_t的說明,要求自定義xxx_device_t的第一個成員必須是hw_device_t類型,其次才是其它的一些接口信息. 
struct xxx_device_t {
	struct hw_device_t common;
        //以下成員是HAL對上層提供的接口或一些屬性
       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)定義,這個才是HAL向上層提供接口函數(shù)的數(shù)據(jù)結(jié)構(gòu),其成員就是我們想要關(guān)心的接口函數(shù).

接下來我們在實現(xiàn)文件XXX.c文件中定義一個xxx_module_t的變量:

    /*模塊實例變量*/
struct xxx_module_t HAL_MODULE_INFO_SYM = {    //變量名必須為HAL_MODULE_INFO_SYM,這是強制要求的,你要寫Android的HAL就得遵循這個游戲規(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中包含一個函數(shù)列表xxx_module_methods,而這個成員函數(shù)列表是在本地自定義的。那么這個成員函數(shù)列是不是就是HAL向上層提供函數(shù)的地方呢?很失望,不是在這里,前面我們已經(jīng)說過了,是在xxx_device_t中定義的,這個xxx_module_methods實際上只提供了一個open函數(shù),就相當于只提供了一個模塊初始化函數(shù).其定義如下:

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

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

且看上面這個函數(shù)列表中唯一的一個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));//動態(tài)分配空間
	
	if(!dev) {
		LOGE("Hello Stub: failed to alloc space");
		return -EFAULT;
	}

	memset(dev, 0, sizeof(struct xxx_device_t));
        //對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;
        //對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)不是只包含了一個close接口嗎?)
	*device = &(dev->common);
	LOGI("Hello Stub: open /dev/hello successfully.");

	return 0;
}
  
經(jīng)驗告訴我們,一般在進行模塊初始化的時候,模塊的接口函數(shù)也會“注冊”,上面是模塊初始化函數(shù),那么接口注冊在哪?于是我們找到*device =&(dev->common);這行代碼,可問題是,這樣一來,返回給調(diào)用者不是&(dev->common)嗎?而這個dev->common僅僅只包含了一個模塊關(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;
  
這是方法列表的定義,明確要求了方法列表中有且只一個open方法,即相當于模塊初始化方法,且,這個方法的第三個參數(shù)明確指明了類型是struct hw_device_t **,而不是用戶自定義的xxx_device_t,這也就是解譯了在open函數(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)最終會返回給struce hello_device_t **類型的輸出變量device,換句話說,類型為hw_device_t的dev->common在初始化函數(shù)open返回后,會強制轉(zhuǎn)化為xxx_device_t來使用,終于明白了,原來如此!另外,在hardware.h中對xxx_device_t類型有說明,要求它的 第一個成員的類型必須是 hw_device_t,原來是為了HAL上層使用時的強制轉(zhuǎn)化的目的,如果xxx_device_t的第一個成員類型不是hw_device_t,那么HAL上層使用中強制轉(zhuǎn)化就沒有意義了,這個時候,就真的“看不到”HAL提供的接口了.


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


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

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

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

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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 香蕉成人啪国产精品视频综合网 | 欧美另类69xxxxx视频 | 67194在线午夜亚洲 | 亚洲视频色 | 四虎国产精品永久地址51 | 国产精品爱久久久久久久9999 | 久久久精品波多野结衣 | 欧美激情免费 | 久久综合日韩亚洲精品色 | 久久亚洲国产精品五月天 | 久久综合精品国产一区二区三区无 | 国产一区二区三区免费在线视频 | 在线观看欧美精品 | 五月天婷婷在线视频 | 色日韩| 福利院肉动漫视频在线观看 | 天天天天干 | 69做爰视频在线观看 | 久久久夜 | 伊人成人在线视频 | 99视频精品免费99在线 | 国产不卡视频在线观看 | 国产在线精品香蕉综合网一区 | 亚洲videos| 国产日产欧产精品网站 | 四虎日韩| 国产福利一区二区在线精品 | 欧美一级www毛片 | 欧美白人极品性喷潮 | 日本xxxxxbbbbb精品 | 国产精品高清在线观看 | 国产成人精品高清在线观看99 | 久久精品国产乱子伦多人 | 日日夜夜天天人人 | 免费视频一区二区三区四区 | 国产天天射 | 成年超爽大片免费视频播放 | 亚洲va欧美va国产va天堂 | 成人国产在线视频在线观看 | www.欧美| 免费的毛片|