Android系統HAL層開發,編譯過程(hello)

來源:互聯網
上載者:User

在android2.3.1下進行HAL層的開發,先參照網上弄了個hello的demo,首先看下HAL層在android系統中的位置:


硬體驅動程式可以看做是在keinel層,HAL封裝了硬體驅動,然後再經過JNI介面的封裝才能給Java應用程式調用。

HAL層介面封裝的具體流程如下:

1)在../Android-2.3.1/hardware/libhardware/include/hardware這個目錄下添加hello.h標頭檔,具體可以參開目前的目錄下的overlay.h,

/***************************************************

*android_hal_hello_demo

*hello.h

***************************************************/

#ifndef ANDROID_HELLO_INTERFACE_H

#define ANDROID_HELLO_INTERFACE_H

#include <hardware/hardware.h>

__BEGIN_DECLS

#define HELLO_HARDWARE_MODULE_ID "hello"

struct hello_module_t {

struct hw_module_t common;

};

struct hello_device_t {

struct hw_device_t common;

int fd;

int (*get_val)(struct hello_device_t *dev,int val);

int (*set_val)(struct hello_device_t *dev,int val);

};

__END_DECLS;

#endif

2)在../Android-2.3.1/hardware/libhardware/modules這個目錄下建立hello檔案夾,並在此檔案夾中添加hello.c和Android.mk檔案,具體可以參考modules目錄下overlay檔案夾中的內容。

/******************************************************************************

*android_hal_hello_demo

*hello.c 

*****************************************************************************/

#include <hardware/hardware.h>

#include <hardware/hello.h>

#include <fcntl.h>

#include <errno.h>

#include <cutils/log.h>

#include <cutils/atomic.h>

#define LOG_TAG "hello_stub"

#define DEVICE_NAME "/dev/hello"

#define MODULE_NAME "Hello"

#define MODULE_AUTHOR "pwzhgx@163.com"

static int hello_device_open(const struct hw_module_t *module,const char *name,struct hw_device_t** device);

static int hello_device_close(struct hw_device_t* device);

static int hello_set_val(struct hello_device_t*dev,int val);

static int hello_get_val(struct hello_device_t *dev,int *val);

static struct hw_module_methods_t hello_module_methods  = {

open : hello_device_open

};

struct hello_module_t HAL_MODULE_INFO_SYM = {

common: {

tag: HARDWARE_MODULE_TAG,

  version_major: 1,

version_minor: 0,

id: HELLO_HARDWARE_MODULE_ID,

name: MODULE_NAME,

  author: MODULE_AUTHOR,

methods: &hello_module_methods,

}

};

static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {

  struct hello_device_t* dev;

dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));

 if(!dev) {

 LOGE("Hello Stub: failed to alloc space");

 return -EFAULT;

 }

 memset(dev, 0, sizeof(struct hello_device_t));

 dev->common.tag = HARDWARE_DEVICE_TAG;

 dev->common.version = 0;

 dev->common.module = (hw_module_t*)module;

 dev->common.close = hello_device_close;

 dev->set_val = hello_set_val;

 dev->get_val = hello_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; 

}

 *device = &(dev->common);

 LOGI("Hello Stub: open /dev/hello successfully.");

 return 0;

}

static int hello_device_close(struct hw_device_t* device) {

 struct hello_device_t* hello_device = (struct hello_device_t*)device;

 if(hello_device) {

 close(hello_device->fd);

 free(hello_device);

 }

 return 0;

}

static int hello_set_val(struct hello_device_t* dev, int val) {

 LOGI("Hello Stub: set value %d to device.", val);

 write(dev->fd, &val, sizeof(val));

 return 0;

}

static int hello_get_val(struct hello_device_t* dev, int* val) {

 if(!val) {

 LOGE("Hello Stub: error val pointer");

 return -EFAULT;

 }

 read(dev->fd, val, sizeof(*val));

 LOGI("Hello Stub: get value %d from device", *val);

 return 0;

}

/******************************************************************************

*android_hal_hello_demo

*Android.mk

*****************************************************************************/

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_PRELINK_MODULE := false

LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw

LOCAL_SHARED_LIBRARIES := liblog

LOCAL_SRC_FILES := hello.c

LOCAL_MODULE := hello.default

include $(BUILD_SHARED_LIBRARY)

到此HAL層的代碼基本已經弄好,接下來就是編譯,將其編譯成模組.so庫檔案。

3)如何編譯,這是一個新增的模組,比如你已經make過一次android的源碼了,此時就不要要重新make clean;make了,你只需要將模組編譯好,然後再make snod即可將新的模組編譯到鏡像中。在源碼的build目錄下有一個配置環境的指令碼envsetup.sh,此檔案包含了一下編譯工具m,mm,mmm,具體的功能你可以直接查看。這裡編譯使用到mmm。

在android源碼包中執行

[root@localhost Android-2.3.1]# sh build/envsetup.sh

[root@localhost Android-2.3.1]#croot

此時mmm工具已經在當前的環境中,當然就可以使用mmm來編譯模組了

[root@localhost Android-2.3.1]#mmm hardware/libhardware/modules/hello

然而出現了一些意想不到的錯誤

找不到liblog.so庫檔案,只能編譯一下產生liblog.so這個庫檔案才可以。

4)編譯產生liblog.so

[root@localhost Android-2.3.1]#make liblog

5)現在重新編譯

[root@localhost Android-2.3.1]#mmm hardware/libhardware/modules/hello

終於看到產生hello.default.so庫檔案了,接下來重新打包


6)重新打包鏡像

[root@localhost Android-2.3.1]make snod

7)接下來就是JNI封裝

待續。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.