開發Android硬體抽象層代碼

來源:互聯網
上載者:User

1、開發Android硬體抽象層代碼

~/android-2.3_r1/hardware/libhardware

----include

----hardware

----freg.h

----hareware.h

-----modules

----freg

----freg.cpp

----Android.mk

hareware.h

.........#ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H#define ANDROID_INCLUDE_HARDWARE_HARDWARE_H#include #include #include __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. */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 */    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 a specific device */    int (*open)(const struct hw_module_t* module, const char* id,            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. */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 *pHmi == NULL */int hw_get_module(const char *id, const struct hw_module_t **module);........__END_DECLS#endif  /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */
hw_module_t結構體:

(1)在結構體hw_module_t的定義前面有一段注釋,意思是硬體抽象層中的每一個模組都必須自訂一個硬體抽象層模組結構體,而且它的第一個成員變數的類型必須為hw_module_t。

(2)硬體抽象層中的每一個模組都必須存在一個匯出符號HAL_MODULE_IFNO_SYM,即“HMI”,它指向一個自訂的硬體抽象層模組結構體。

(3)結構體hw_module_t的成員變數tag的值必須設定為HARWARE_MODULE_TAG,即設定為一個常量值('H'<<24|'W'<<16|'M'<<8|'T'),用來標誌這是一個硬體抽象層模組結構體。

(4)結構體hw_module_t的成員變數dso用來儲存載入硬體抽象層模組後得到的控制代碼值。

(5)結構體hw_module_t的成員變數methods定義了一個硬體抽象層模組的操作方法列表,它的類型為hw_module_methods_t,接下來我們就介紹它的定義。

hw_module_methods_t結構體:

(1)只有一個成員變數,它是一個函數指標,用來開啟硬體抽象層模組中的硬體裝置。其中,參數module表示要開啟的硬體裝置所在的模組;參數id表示要開啟的硬體裝置的ID;參數device是一個輸出參數,用來描述一個已經開啟的硬體裝置。由於一個硬體抽象層模組可能會包含多個硬體裝置。因此,在調用結構體hw_module_methods_t的成員變數open來開啟一個硬體裝置時,我們需要指定它的ID。

hw_device_t結構體:

(1)硬體抽象層模組中的每一個硬體裝置都必須自訂一個硬體裝置結構體,而且它的第一個成員變數的類型必須為hw_device_t。

(2)結構體hw_device_t的成員變數tag的值必須設定為HARDWARE_DEVICE_TAG,即設定為一個常量值('H'<<24|'W'<<16|'D'<<8|'T'),用來標誌這是一個硬體抽象層中的硬體裝置結構體。

(3)結構體hw_device_t的成員變數close是一個函數指標,它用來關閉一個硬體裝置。

至此,硬體抽象層模組介面的編寫規範就介紹完了。

freg.h

#ifndef ANDROID_FREG_INTERFACE_H#define ANDROID_FREG_INTERFACE_H#include __BEGIN_DECLS/** * The id of this module */#define FREG_HARDWARE_MODULE_ID "freg"/** * The id of this device */#define FREG_HARDWARE_DEVICE_ID "freg"struct freg_module_t {struct hw_module_t common;};struct freg_device_t {struct hw_device_t common;int fd;int (*set_val)(struct freg_device_t* dev, int val);int (*get_val)(struct freg_device_t* dev, int* val);};__END_DECLS#endif
宏FREG_HARDWARE_MODULE_ID和FREG_HARDWARE_DEVICE_ID分別用來描述模組ID和裝置ID。結構體freg_module_t用來描述自訂的模組結構體,它的第一個成員變數的類型為hw_module_t。結構體freg_device_t用來描述虛擬硬體裝置freg,它的第一個成員變數的類型為freg_device_t。此外,結構體freg_device_t還定義了其他三個成員變數,其中,成員變數fd是一個檔案描述符,用來描述開啟的裝置檔案/dev/freg,成員變數set_val和get_val是函數指標,它們分別用來寫和讀虛擬硬體裝置freg的寄存器val的內容。


freg.cpp

#define LOG_TAG "FregHALStub"#include #include #include #include #include #include #define DEVICE_NAME "/dev/freg"#define MODULE_NAME "Freg"#define MODULE_AUTHOR "shyluo@gmail.com"//因為在實現前,要用到這些函數,所以先定義static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);static int freg_device_close(struct hw_device_t* device);static int freg_set_val(struct freg_device_t* dev, int val);static int freg_get_val(struct freg_device_t* dev, int* val);static struct hw_module_methods_t freg_module_methods = {open: freg_device_open};struct freg_module_t HAL_MODULE_INFO_SYM = {//一個硬體抽象層模組必須匯出一個名稱為HAL_MODULE_INFO_SYM的符號common: {tag: HARDWARE_MODULE_TAG,//tag值必須設定為HARDWARE_MODULE_TAGversion_major: 1,version_minor: 0,id: FREG_HARDWARE_MODULE_ID,name: MODULE_NAME,author: MODULE_AUTHOR,methods: &freg_module_methods,}};static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {if(!strcmp(id, FREG_HARDWARE_DEVICE_ID)) {//首先要匹配IDstruct freg_device_t* dev;dev = (struct freg_device_t*)malloc(sizeof(struct freg_device_t));//分配freg_device_t結構體if(!dev) {LOGE("Failed to alloc space for freg_device_t.");return -EFAULT;}memset(dev, 0, sizeof(struct freg_device_t));dev->common.tag = HARDWARE_DEVICE_TAG;//必須初始化為HARDWARE_DEVICE_TAGdev->common.version = 0;dev->common.module = (hw_module_t*)module;dev->common.close = freg_device_close;//關閉函數dev->set_val = freg_set_val;dev->get_val = freg_get_val;if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {LOGE("Failed to open device file /dev/freg -- %s.", strerror(errno));free(dev);return -EFAULT;}*device = &(dev->common);LOGI("Open device file /dev/freg successfully.");return 0;}return -EFAULT;}static int freg_device_close(struct hw_device_t* device) {struct freg_device_t* freg_device = (struct freg_device_t*)device;if(freg_device) {close(freg_device->fd);free(freg_device);}return 0;}static int freg_set_val(struct freg_device_t* dev, int val) {if(!dev) {LOGE("Null dev pointer.");return -EFAULT;}LOGI("Set value %d to device file /dev/freg.", val);write(dev->fd, &val, sizeof(val));return 0;}static int freg_get_val(struct freg_device_t* dev, int* val) {if(!dev) {LOGE("Null dev pointer.");return -EFAULT;}if(!val) {LOGE("Null val pointer.");return -EFAULT;}read(dev->fd, val, sizeof(*val));LOGI("Get value %d from device file /dev/freg.", *val);return 0;}

在這段代碼中,最值得關注的就是模組變數HAL_MODULE_INFO_SYM的定義。按照硬體抽象層模組編寫規範,每一個硬體抽象層模組必須匯出一個名稱為HAL_MODULE_INFO_SYM的符號,它指向一個自訂的硬體抽象層模組結構體,而且它的第一個類型為hw_module_t的成員變數tag值必須設定為HARDWARE_MODULE_TAG。除此之外,還初始化了這個硬體抽象層模組結構體的版本號碼、ID、名稱、作者和操作方法列表等。


Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES := liblogLOCAL_SRC_FILES := freg.cppLOCAL_MODULE := freg.defaultinclude $(BUILD_SHARED_LIBRARY)
include $(BUILD_SHARED_LIBRARY),表示要將硬體抽象層模組編譯成一個動態連結程式庫檔案,名稱為freg.default。


2、編譯

編譯硬體抽象層:


產生的動態連結程式庫檔案,名稱為freg.default.so,儲存在out/target/product/generic喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3lzdGVtL2xpYi9od8S/wrzPwqGjPC9wPgo8cD48YnI+CjwvcD4KPHA+ICAgICAgtPKw/KO6PC9wPgo8cD48aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140609/20140609091226145.png" alt="n塊肢侂顧侁鐉笟喎?http://www.bkjia.com/kf/yidong/Android/" target="_blank" class="keylink">Android系統鏡像檔案system.img。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.