最近要做的東西可能涉及HAL的概念,看看<<android源代碼情景分析 >>
Headware Abstract Layer (HAL)
為什么Android的driver分UMD和KMD實(shí)現(xiàn)?
如果實(shí)現(xiàn)在UMD,遵循Apache license就可以閉源,做HAL層。
KMD遵循GPL,driver只提供簡(jiǎn)單硬件訪問(wèn)通道。android體系:
| Android體系 |
|---|
| App |
| App framework |
| lib&Runtime |
| HAL |
| ------------------上面是UMD,下面是KMD---------------------- |
| HAL |
| Driver |
| Process/Memory manager |
KMD Driver
HAL層
HAL層是一個(gè)硬件模塊,是一個(gè)動(dòng)態(tài)庫(kù)so,其命名有命名規(guī)范。
內(nèi)部:
| 結(jié)構(gòu)體 | |
|---|---|
| 硬件抽象層 | hw_module_t |
| 硬件設(shè)備 | hw_device_t |
命名規(guī)范
結(jié)構(gòu)體定義規(guī)范
//.h 聲明結(jié)構(gòu)體:
//自定義模塊結(jié)構(gòu)體,開(kāi)頭包含hw_module_t,
struct freg_module_t {
struct hw_module_t common; //里面有個(gè)hw_module_methods_t *method
};
//自定義設(shè)備結(jié)構(gòu)體,開(kāi)頭包含hw_device_t
struct freg_device_t {
struct hw_device_t common;
int fd;
int (*own_func)();
};
//cpp 定義結(jié)構(gòu)體:
//hw_module_mothods_t只有一個(gè)int (*open) (strcut hw_module_t* module, id, strcut hw_device_t** device)
struct hw_module_mothods_t freg_module_methods = {
.open : freg_device_open
};
struct freg_module_t HAL_MODULE_INFO_SYM = {
.common: { //填hw_module_t的結(jié)構(gòu)體變量
tag: HARDWARE_MODULE_TAG, //必須為此
...
methods : &freg_module_methods, //傳了open方法
}
}
int freg_device_open(struct hw_module_t *module, id, struct hw_device_t **device) {
struct frag_device_t *dev;
//填dev->common
//填dev->ownFunc = ownFunc();
}
//定義ownFunc();
//Andriod.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TQAGS := optional
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := freg.cpp
LOCAL_MODULE := freg.default
include $(BUILD_SHARED_LIBRARY)
HAL的加載
//誰(shuí)要使用HAL都要通過(guò)hw_get_module(struct hw_module_t **module)來(lái)拿到hw_module,
//這個(gè)module就是當(dāng)年定義的HAL_MODULE_INFO_SYM
int hw_get_module(id, struct hw_module_t **module)
{
//asscess(path)查找可用路徑
load(id, path, module);
}
int load(id, char *path, struct hw_module_t **pHmi)
{
void *handle = dlopen(path, RTLD_NOW);
//得到struct hal_module_info的地址
const char *sym = HAL_MODULE_INFO_SYM;
struct hw_module_t *hmi ;
hmi = (struct hw_module_t *)dlsym(handle, sym);
hmi->dso = handle;
*pHmi = hmi;
}