第八期 基于模拟器的Helloworld HAL接口 《手机就是开发板》

https://blog.csdn.net/aggresss/article/details/53571487

    Android 下的驱动总能和HAL放在一起被提起,估计当年Google就是因为HAL被Linux开源社区踢出去的。大家知道 驱动程序可以分成两个部分:硬件控制和业务逻。因为驱动要编译进内核,即使编译成模块也受GPL制约,所以Googl将android系统的驱动程序分成两层,硬件控制层放到内核驱动里,业务逻辑部分放到HAL里,可以不用开源,保护了部分芯片商的利益,估计这事是高通和Google一起想出来的,因为他是最大的受益者。
    天天说HAL云里雾里的,这一期我们动手实践一个HAL。同样,源码在https://github.com/aggresss/PHDemo 的Code目录下的 hello_HAL 文件下,或者访问链接https://github.com/aggresss/PHDemo/tree/master/Code/hello_HAL
    需要进入 ./hardware/libhardware/include/hardware 目录,新建 hello.h 文件

#ifndef ANDROID_HELLO_INTERFACE_H  
#define ANDROID_HELLO_INTERFACE_H  
#include <hardware/hardware.h>  
  
__BEGIN_DECLS  
  
/*定义模块ID*/  
#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 (*set_val)(struct hello_device_t* dev, int val);  
    int (*get_val)(struct hello_device_t* dev, int* val);  
};  
  
__END_DECLS  
  
#endif  


然后 进入到 hardware/libhardware/modules 目录,新建hello目录,并添加hello.c文件

#define LOG_TAG "HelloStub"  
  
#include <hardware/hardware.h>  
#include <hardware/hello.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <cutils/log.h>  
#include <cutils/atomic.h>  
  
#define DEVICE_NAME "/dev/hello"  
#define MODULE_NAME "Hello"  
#define MODULE_AUTHOR "[email protected]"  
  
/*设备打开和关闭接口*/  
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) {  
        ALOGE("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) {  
        ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);  
        return -EFAULT;  
    }  
  
    *device = &(dev->common);  
    ALOGI("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) {  
    ALOGI("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) {  
        ALOGE("Hello Stub: error val pointer");  
        return -EFAULT;  
    }  
  
    read(dev->fd, val, sizeof(*val));  
  
    ALOGI("Hello Stub: get value %d from device", *val);  
  
    return 0;  
}  


继续在hello目录下新建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)

回到 hardware/libhardware/modules 目录 编辑目录下 Android.mk 文件,在 hardware_modules 变量中添加 hello 变量,这样hello模块就会自动编译

为了保证dev/hello设备正常访问,我们这里修改一下/dev/hello的权限,从AOSP根目录开始,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:

      /dev/hello 0666 root root


下面开始编译:

mmm hardware/libhardware/modules/hello


编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。
最后,我们运行一下虚拟机,看看HAL是否存在。
执行 make snod 重新生成镜像。

emulator -kernel ./kernel3.4/arch/arm/boot/zImage &
adb shell
cd /system/lib/hw
ls

    

如上图,红色方框中的hello.default.so就是我们编译生成的HAL,通过这个实验我们可以看到,Android中的HAL实质上是以 /system/lib/hw 目录中 共享链接库 的形式存在。下一期我们通过JNI方法来调用这个共享库。
 

猜你喜欢

转载自blog.csdn.net/wxh0000mm/article/details/86300811